23 |
|
} lat[MAXLATS+1]; /* latitudes */ |
24 |
|
} ANGLE_BASIS; |
25 |
|
|
26 |
< |
#define MAXABASES 5 /* limit on defined bases */ |
26 |
> |
#define MAXABASES 7 /* limit on defined bases */ |
27 |
|
|
28 |
|
static ANGLE_BASIS abase_list[MAXABASES] = { |
29 |
|
{ |
61 |
|
|
62 |
|
static int nabases = 3; /* current number of defined bases */ |
63 |
|
|
64 |
< |
#define FEQ(a,b) ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7) |
64 |
> |
#define FEQ(a,b) ((a)-(b) <= 1e-6 && (b)-(a) <= 1e-6) |
65 |
|
|
66 |
+ |
static int |
67 |
+ |
fequal(double a, double b) |
68 |
+ |
{ |
69 |
+ |
if (b != .0) |
70 |
+ |
a = a/b - 1.; |
71 |
+ |
return((a <= 1e-6) & (a >= -1e-6)); |
72 |
+ |
} |
73 |
+ |
|
74 |
|
// returns the name of the given tag |
75 |
|
#ifdef ezxml_name |
76 |
|
#undef ezxml_name |
208 |
|
|
209 |
|
|
210 |
|
static void |
211 |
< |
load_angle_basis( /* load BSDF angle basis */ |
211 |
> |
load_angle_basis( /* load custom BSDF angle basis */ |
212 |
|
ezxml_t wab |
213 |
|
) |
214 |
|
{ |
216 |
|
ezxml_t wbb; |
217 |
|
int i; |
218 |
|
|
219 |
< |
if (abname == NULL || !*abname) |
219 |
> |
if (!abname || !*abname) |
220 |
|
return; |
221 |
|
for (i = nabases; i--; ) |
222 |
< |
if (!strcmp(abname, abase_list[i].name)) |
223 |
< |
return; /* XXX assume it's the same */ |
222 |
> |
if (!strcasecmp(abname, abase_list[i].name)) |
223 |
> |
return; /* assume it's the same */ |
224 |
|
if (nabases >= MAXABASES) |
225 |
|
error(INTERNAL, "too many angle bases"); |
226 |
|
strcpy(abase_list[nabases].name, abname); |
235 |
|
if (!i) |
236 |
|
abase_list[nabases].lat[i].tmin = |
237 |
|
-abase_list[nabases].lat[i+1].tmin; |
238 |
< |
else if (!FEQ(atof(ezxml_txt(ezxml_child(ezxml_child(wbb, |
238 |
> |
else if (!fequal(atof(ezxml_txt(ezxml_child(ezxml_child(wbb, |
239 |
|
"ThetaBounds"), "LowerTheta"))), |
240 |
|
abase_list[nabases].lat[i].tmin)) |
241 |
|
error(WARNING, "theta values disagree in custom basis"); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
+ |
static double |
251 |
+ |
to_meters( /* return factor to convert given unit to meters */ |
252 |
+ |
const char *unit |
253 |
+ |
) |
254 |
+ |
{ |
255 |
+ |
if (unit == NULL) return(1.); /* safe assumption? */ |
256 |
+ |
if (!strcasecmp(unit, "Meter")) return(1.); |
257 |
+ |
if (!strcasecmp(unit, "Foot")) return(.3048); |
258 |
+ |
if (!strcasecmp(unit, "Inch")) return(.0254); |
259 |
+ |
if (!strcasecmp(unit, "Centimeter")) return(.01); |
260 |
+ |
if (!strcasecmp(unit, "Millimeter")) return(.001); |
261 |
+ |
sprintf(errmsg, "unknown dimensional unit '%s'", unit); |
262 |
+ |
error(USER, errmsg); |
263 |
+ |
} |
264 |
+ |
|
265 |
+ |
|
266 |
|
static void |
267 |
+ |
load_geometry( /* load geometric dimensions and description (if any) */ |
268 |
+ |
struct BSDF_data *dp, |
269 |
+ |
ezxml_t wdb |
270 |
+ |
) |
271 |
+ |
{ |
272 |
+ |
ezxml_t geom; |
273 |
+ |
double cfact; |
274 |
+ |
const char *fmt, *mgfstr; |
275 |
+ |
|
276 |
+ |
dp->dim[0] = dp->dim[1] = dp->dim[2] = 0; |
277 |
+ |
dp->mgf = NULL; |
278 |
+ |
if ((geom = ezxml_child(wdb, "Width")) != NULL) |
279 |
+ |
dp->dim[0] = atof(ezxml_txt(geom)) * |
280 |
+ |
to_meters(ezxml_attr(geom, "unit")); |
281 |
+ |
if ((geom = ezxml_child(wdb, "Height")) != NULL) |
282 |
+ |
dp->dim[1] = atof(ezxml_txt(geom)) * |
283 |
+ |
to_meters(ezxml_attr(geom, "unit")); |
284 |
+ |
if ((geom = ezxml_child(wdb, "Thickness")) != NULL) |
285 |
+ |
dp->dim[2] = atof(ezxml_txt(geom)) * |
286 |
+ |
to_meters(ezxml_attr(geom, "unit")); |
287 |
+ |
if ((geom = ezxml_child(wdb, "Geometry")) == NULL || |
288 |
+ |
(mgfstr = ezxml_txt(geom)) == NULL) |
289 |
+ |
return; |
290 |
+ |
if ((fmt = ezxml_attr(geom, "format")) != NULL && |
291 |
+ |
strcasecmp(fmt, "MGF")) { |
292 |
+ |
sprintf(errmsg, "unrecognized geometry format '%s'", fmt); |
293 |
+ |
error(WARNING, errmsg); |
294 |
+ |
return; |
295 |
+ |
} |
296 |
+ |
cfact = to_meters(ezxml_attr(geom, "unit")); |
297 |
+ |
dp->mgf = (char *)malloc(strlen(mgfstr)+32); |
298 |
+ |
if (dp->mgf == NULL) |
299 |
+ |
error(SYSTEM, "out of memory in load_geometry"); |
300 |
+ |
if (cfact < 0.99 || cfact > 1.01) |
301 |
+ |
sprintf(dp->mgf, "xf -s %.5f\n%s\nxf\n", cfact, mgfstr); |
302 |
+ |
else |
303 |
+ |
strcpy(dp->mgf, mgfstr); |
304 |
+ |
} |
305 |
+ |
|
306 |
+ |
|
307 |
+ |
static void |
308 |
|
load_bsdf_data( /* load BSDF distribution for this wavelength */ |
309 |
|
struct BSDF_data *dp, |
310 |
|
ezxml_t wdb |
315 |
|
char *sdata; |
316 |
|
int i; |
317 |
|
|
318 |
< |
if ((cbasis == NULL || !*cbasis) | (rbasis == NULL || !*rbasis)) { |
318 |
> |
if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) { |
319 |
|
error(WARNING, "missing column/row basis for BSDF"); |
320 |
|
return; |
321 |
|
} |
257 |
– |
/* XXX need to add routines for loading in foreign bases */ |
322 |
|
for (i = nabases; i--; ) |
323 |
< |
if (!strcmp(cbasis, abase_list[i].name)) { |
323 |
> |
if (!strcasecmp(cbasis, abase_list[i].name)) { |
324 |
|
dp->ninc = abase_list[i].nangles; |
325 |
|
dp->ib_priv = (void *)&abase_list[i]; |
326 |
|
dp->ib_vec = ab_getvecR; |
329 |
|
break; |
330 |
|
} |
331 |
|
if (i < 0) { |
332 |
< |
sprintf(errmsg, "unsupported ColumnAngleBasis '%s'", cbasis); |
332 |
> |
sprintf(errmsg, "undefined ColumnAngleBasis '%s'", cbasis); |
333 |
|
error(WARNING, errmsg); |
334 |
|
return; |
335 |
|
} |
336 |
|
for (i = nabases; i--; ) |
337 |
< |
if (!strcmp(rbasis, abase_list[i].name)) { |
337 |
> |
if (!strcasecmp(rbasis, abase_list[i].name)) { |
338 |
|
dp->nout = abase_list[i].nangles; |
339 |
|
dp->ob_priv = (void *)&abase_list[i]; |
340 |
|
dp->ob_vec = ab_getvec; |
343 |
|
break; |
344 |
|
} |
345 |
|
if (i < 0) { |
346 |
< |
sprintf(errmsg, "unsupported RowAngleBasis '%s'", cbasis); |
346 |
> |
sprintf(errmsg, "undefined RowAngleBasis '%s'", cbasis); |
347 |
|
error(WARNING, errmsg); |
348 |
|
return; |
349 |
|
} |
350 |
|
/* read BSDF data */ |
351 |
|
sdata = ezxml_txt(ezxml_child(wdb,"ScatteringData")); |
352 |
< |
if (sdata == NULL || !*sdata) { |
352 |
> |
if (!sdata || !*sdata) { |
353 |
|
error(WARNING, "missing BSDF ScatteringData"); |
354 |
|
return; |
355 |
|
} |
373 |
|
sdata++; |
374 |
|
if (*sdata) { |
375 |
|
sprintf(errmsg, "%d extra characters after BSDF ScatteringData", |
376 |
< |
strlen(sdata)); |
376 |
> |
(int)strlen(sdata)); |
377 |
|
error(WARNING, errmsg); |
378 |
|
} |
379 |
|
} |
385 |
|
) |
386 |
|
{ |
387 |
|
double *omega_iarr, *omega_oarr; |
388 |
< |
double dom, contrib, hemi_total; |
388 |
> |
double dom, contrib, hemi_total, full_total; |
389 |
|
int nneg; |
390 |
|
FVECT v; |
391 |
|
int i, o; |
454 |
|
BSDF_value(dp,i,o) = .0f; |
455 |
|
} |
456 |
|
} |
457 |
< |
if (hemi_total > 1.02) { |
457 |
> |
if (hemi_total > 1.01) { |
458 |
|
sprintf(errmsg, |
459 |
|
"incoming BSDF direction %d passes %.1f%% of light", |
460 |
|
i, 100.*hemi_total); |
465 |
|
sprintf(errmsg, "%d negative BSDF values (ignored)", nneg); |
466 |
|
error(WARNING, errmsg); |
467 |
|
} |
468 |
< |
/* reverse roles and check again */ |
468 |
> |
full_total = .0; /* reverse roles and check again */ |
469 |
|
for (o = 0; o < dp->nout; o++) { |
470 |
|
hemi_total = .0; |
471 |
|
for (i = dp->ninc; i--; ) |
472 |
|
hemi_total += BSDF_value(dp,i,o) * omega_iarr[i]; |
473 |
|
|
474 |
< |
if (hemi_total > 1.02) { |
474 |
> |
if (hemi_total > 1.01) { |
475 |
|
sprintf(errmsg, |
476 |
|
"outgoing BSDF direction %d collects %.1f%% of light", |
477 |
|
o, 100.*hemi_total); |
478 |
|
error(WARNING, errmsg); |
479 |
|
} |
480 |
+ |
full_total += hemi_total*omega_oarr[o]; |
481 |
|
} |
482 |
+ |
full_total /= PI; |
483 |
+ |
if (full_total > 1.00001) { |
484 |
+ |
sprintf(errmsg, "BSDF transfers %.4f%% of light", |
485 |
+ |
100.*full_total); |
486 |
+ |
error(WARNING, errmsg); |
487 |
+ |
} |
488 |
|
free(omega_iarr); free(omega_oarr); |
489 |
|
return(1); |
490 |
|
} |
491 |
|
|
492 |
+ |
|
493 |
|
struct BSDF_data * |
494 |
|
load_BSDF( /* load BSDF data from file */ |
495 |
|
char *fname |
526 |
|
return(NULL); |
527 |
|
} |
528 |
|
wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); |
529 |
+ |
if (strcasecmp(ezxml_txt(ezxml_child(ezxml_child(wtl, |
530 |
+ |
"DataDefinition"), "IncidentDataStructure")), |
531 |
+ |
"Columns")) { |
532 |
+ |
sprintf(errmsg, |
533 |
+ |
"BSDF \"%s\": unsupported IncidentDataStructure", |
534 |
+ |
path); |
535 |
+ |
error(WARNING, errmsg); |
536 |
+ |
ezxml_free(fl); |
537 |
+ |
return(NULL); |
538 |
+ |
} |
539 |
|
load_angle_basis(ezxml_child(ezxml_child(wtl, |
540 |
|
"DataDefinition"), "AngleBasis")); |
541 |
|
dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data)); |
542 |
+ |
load_geometry(dp, ezxml_child(wtl, "Material")); |
543 |
|
for (wld = ezxml_child(wtl, "WavelengthData"); |
544 |
|
wld != NULL; wld = wld->next) { |
545 |
< |
if (strcmp(ezxml_txt(ezxml_child(wld,"Wavelength")), "Visible")) |
545 |
> |
if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")), "Visible")) |
546 |
|
continue; |
547 |
|
wdb = ezxml_child(wld, "WavelengthDataBlock"); |
548 |
|
if (wdb == NULL) continue; |
549 |
< |
if (strcmp(ezxml_txt(ezxml_child(wdb,"WavelengthDataDirection")), |
549 |
> |
if (strcasecmp(ezxml_txt(ezxml_child(wdb,"WavelengthDataDirection")), |
550 |
|
"Transmission Front")) |
551 |
|
continue; |
552 |
|
load_bsdf_data(dp, wdb); /* load front BTDF */ |
570 |
|
{ |
571 |
|
if (b == NULL) |
572 |
|
return; |
573 |
+ |
if (b->mgf != NULL) |
574 |
+ |
free(b->mgf); |
575 |
|
if (b->bsdf != NULL) |
576 |
|
free(b->bsdf); |
577 |
|
free(b); |
678 |
|
getBSDF_xfm( /* compute BSDF orient. -> world orient. transform */ |
679 |
|
MAT4 xm, |
680 |
|
FVECT nrm, |
681 |
< |
UpDir ud |
681 |
> |
UpDir ud, |
682 |
> |
char *xfbuf |
683 |
|
) |
684 |
|
{ |
685 |
|
char *xfargs[7]; |
686 |
|
XF myxf; |
687 |
|
FVECT updir, xdest, ydest; |
688 |
+ |
int i; |
689 |
|
|
690 |
|
updir[0] = updir[1] = updir[2] = 0.; |
691 |
|
switch (ud) { |
716 |
|
fcross(ydest, nrm, xdest); |
717 |
|
xf(&myxf, addrot(xfargs, xdest, ydest, nrm), xfargs); |
718 |
|
copymat4(xm, myxf.xfm); |
719 |
+ |
if (xfbuf == NULL) |
720 |
+ |
return(1); |
721 |
+ |
/* return xf arguments as well */ |
722 |
+ |
for (i = 0; xfargs[i] != NULL; i++) { |
723 |
+ |
*xfbuf++ = ' '; |
724 |
+ |
strcpy(xfbuf, xfargs[i]); |
725 |
+ |
while (*xfbuf) ++xfbuf; |
726 |
+ |
} |
727 |
|
return(1); |
728 |
|
} |