1 |
+ |
#ifndef lint |
2 |
+ |
static const char RCSid[] = "$Id$"; |
3 |
+ |
#endif |
4 |
|
/* |
5 |
|
* bsdf_m.c |
6 |
|
* |
11 |
|
* |
12 |
|
*/ |
13 |
|
|
14 |
< |
#include <stdio.h> |
14 |
> |
#include "rtio.h" |
15 |
|
#include <stdlib.h> |
16 |
|
#include <math.h> |
14 |
– |
#include <strings.h> |
17 |
|
#include <ctype.h> |
18 |
|
#include "ezxml.h" |
19 |
|
#include "bsdf.h" |
20 |
|
#include "bsdf_m.h" |
21 |
|
|
20 |
– |
#ifndef FTINY |
21 |
– |
#define FTINY 1e-6 |
22 |
– |
#endif |
23 |
– |
|
22 |
|
/* Function return codes */ |
23 |
|
#define RC_GOOD 1 |
24 |
|
#define RC_FAIL 0 |
346 |
|
} |
347 |
|
free(ohma); |
348 |
|
/* need incoming solid angles, too? */ |
349 |
< |
if (dp->ninc < dp->nout || dp->ib_ohm != dp->ob_ohm || |
352 |
< |
dp->ib_priv != dp->ob_priv) { |
349 |
> |
if ((dp->ib_ohm != dp->ob_ohm) | (dp->ib_priv != dp->ob_priv)) { |
350 |
|
double ohm; |
351 |
|
for (i = dp->ninc; i--; ) |
352 |
|
if ((ohm = mBSDF_incohm(dp,i)) < df->minProjSA) |
355 |
|
return (df->maxHemi <= 1.01); |
356 |
|
} |
357 |
|
|
361 |
– |
/* skip integer in string */ |
362 |
– |
static char * |
363 |
– |
i_skip(char *s) |
364 |
– |
{ |
365 |
– |
while (isspace(*s)) |
366 |
– |
s++; |
367 |
– |
if (*s == '-' || *s == '+') |
368 |
– |
s++; |
369 |
– |
if (!isdigit(*s)) |
370 |
– |
return(NULL); |
371 |
– |
do |
372 |
– |
s++; |
373 |
– |
while (isdigit(*s)); |
374 |
– |
return(s); |
375 |
– |
} |
376 |
– |
|
377 |
– |
/* skip float in string */ |
378 |
– |
static char * |
379 |
– |
f_skip(char *s) |
380 |
– |
{ |
381 |
– |
register char *cp; |
382 |
– |
|
383 |
– |
while (isspace(*s)) |
384 |
– |
s++; |
385 |
– |
if (*s == '-' || *s == '+') |
386 |
– |
s++; |
387 |
– |
cp = s; |
388 |
– |
while (isdigit(*cp)) |
389 |
– |
cp++; |
390 |
– |
if (*cp == '.') { |
391 |
– |
cp++; s++; |
392 |
– |
while (isdigit(*cp)) |
393 |
– |
cp++; |
394 |
– |
} |
395 |
– |
if (cp == s) |
396 |
– |
return(NULL); |
397 |
– |
if (*cp == 'e' || *cp == 'E') |
398 |
– |
return(i_skip(cp+1)); |
399 |
– |
return(cp); |
400 |
– |
} |
401 |
– |
|
358 |
|
/* load BSDF distribution for this wavelength */ |
359 |
|
static int |
360 |
|
load_bsdf_data(SDData *sd, ezxml_t wdb, int rowinc) |
361 |
|
{ |
406 |
– |
char *cbasis = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis")); |
407 |
– |
char *rbasis = ezxml_txt(ezxml_child(wdb,"RowAngleBasis")); |
362 |
|
SDSpectralDF *df; |
363 |
|
SDMat *dp; |
364 |
|
char *sdata; |
365 |
+ |
int tback; |
366 |
|
int inbi, outbi; |
367 |
|
int i; |
413 |
– |
|
414 |
– |
if ((!cbasis || !*cbasis) | (!rbasis || !*rbasis)) { |
415 |
– |
sprintf(SDerrorDetail, "Missing column/row basis for BSDF '%s'", |
416 |
– |
sd->name); |
417 |
– |
return RC_FORMERR; |
418 |
– |
} |
368 |
|
/* allocate BSDF component */ |
369 |
|
sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection")); |
370 |
< |
if (!strcasecmp(sdata, "Transmission Front")) { |
370 |
> |
if ((tback = !strcasecmp(sdata, "Transmission Back")) || |
371 |
> |
(sd->tf == NULL && |
372 |
> |
!strcasecmp(sdata, "Transmission Front"))) { |
373 |
|
if (sd->tf != NULL) |
374 |
|
SDfreeSpectralDF(sd->tf); |
375 |
|
if ((sd->tf = SDnewSpectralDF(1)) == NULL) |
376 |
|
return RC_MEMERR; |
377 |
|
df = sd->tf; |
378 |
|
} else if (!strcasecmp(sdata, "Reflection Front")) { |
379 |
< |
if (sd->rf != NULL) |
429 |
< |
SDfreeSpectralDF(sd->rf); |
430 |
< |
if ((sd->rf = SDnewSpectralDF(1)) == NULL) |
431 |
< |
return RC_MEMERR; |
432 |
< |
df = sd->rf; |
433 |
< |
} else if (!strcasecmp(sdata, "Reflection Back")) { |
434 |
< |
if (sd->rb != NULL) |
379 |
> |
if (sd->rb != NULL) /* note back-front reversal */ |
380 |
|
SDfreeSpectralDF(sd->rb); |
381 |
|
if ((sd->rb = SDnewSpectralDF(1)) == NULL) |
382 |
|
return RC_MEMERR; |
383 |
|
df = sd->rb; |
384 |
+ |
} else if (!strcasecmp(sdata, "Reflection Back")) { |
385 |
+ |
if (sd->rf != NULL) /* note front-back reversal */ |
386 |
+ |
SDfreeSpectralDF(sd->rf); |
387 |
+ |
if ((sd->rf = SDnewSpectralDF(1)) == NULL) |
388 |
+ |
return RC_MEMERR; |
389 |
+ |
df = sd->rf; |
390 |
|
} else |
391 |
|
return RC_FAIL; |
392 |
+ |
/* XXX should also check "ScatteringDataType" for consistency? */ |
393 |
|
/* get angle bases */ |
394 |
|
sdata = ezxml_txt(ezxml_child(wdb,"ColumnAngleBasis")); |
395 |
|
if (!sdata || !*sdata) { |
398 |
|
return RC_FORMERR; |
399 |
|
} |
400 |
|
for (inbi = nabases; inbi--; ) |
401 |
< |
if (!strcasecmp(cbasis, abase_list[inbi].name)) |
401 |
> |
if (!strcasecmp(sdata, abase_list[inbi].name)) |
402 |
|
break; |
403 |
|
if (inbi < 0) { |
404 |
< |
sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'", |
453 |
< |
cbasis); |
404 |
> |
sprintf(SDerrorDetail, "Undefined ColumnAngleBasis '%s'", sdata); |
405 |
|
return RC_FORMERR; |
406 |
|
} |
407 |
|
sdata = ezxml_txt(ezxml_child(wdb,"RowAngleBasis")); |
411 |
|
return RC_FORMERR; |
412 |
|
} |
413 |
|
for (outbi = nabases; outbi--; ) |
414 |
< |
if (!strcasecmp(rbasis, abase_list[outbi].name)) |
414 |
> |
if (!strcasecmp(sdata, abase_list[outbi].name)) |
415 |
|
break; |
416 |
|
if (outbi < 0) { |
417 |
< |
sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", cbasis); |
417 |
> |
sprintf(SDerrorDetail, "Undefined RowAngleBasis '%s'", sdata); |
418 |
|
return RC_FORMERR; |
419 |
|
} |
420 |
|
/* allocate BSDF matrix */ |
421 |
|
dp = SDnewMatrix(abase_list[inbi].nangles, abase_list[outbi].nangles); |
422 |
|
if (dp == NULL) |
423 |
|
return RC_MEMERR; |
424 |
< |
dp->ib_priv = (void *)&abase_list[inbi]; |
425 |
< |
dp->ob_priv = (void *)&abase_list[outbi]; |
424 |
> |
dp->ib_priv = &abase_list[inbi]; |
425 |
> |
dp->ob_priv = &abase_list[outbi]; |
426 |
|
if (df == sd->tf) { |
427 |
< |
dp->ib_vec = ab_getvecR; |
428 |
< |
dp->ib_ndx = ab_getndxR; |
429 |
< |
dp->ob_vec = ab_getvec; |
430 |
< |
dp->ob_ndx = ab_getndx; |
427 |
> |
if (tback) { |
428 |
> |
dp->ib_vec = &ab_getvecR; |
429 |
> |
dp->ib_ndx = &ab_getndxR; |
430 |
> |
dp->ob_vec = &ab_getvec; |
431 |
> |
dp->ob_ndx = &ab_getndx; |
432 |
> |
} else { |
433 |
> |
dp->ib_vec = &ab_getvec; |
434 |
> |
dp->ib_ndx = &ab_getndx; |
435 |
> |
dp->ob_vec = &ab_getvecR; |
436 |
> |
dp->ob_ndx = &ab_getndxR; |
437 |
> |
} |
438 |
|
} else if (df == sd->rf) { |
439 |
< |
dp->ib_vec = ab_getvec; |
440 |
< |
dp->ib_ndx = ab_getndx; |
441 |
< |
dp->ob_vec = ab_getvec; |
442 |
< |
dp->ob_ndx = ab_getndx; |
439 |
> |
dp->ib_vec = &ab_getvec; |
440 |
> |
dp->ib_ndx = &ab_getndx; |
441 |
> |
dp->ob_vec = &ab_getvec; |
442 |
> |
dp->ob_ndx = &ab_getndx; |
443 |
|
} else /* df == sd->rb */ { |
444 |
< |
dp->ib_vec = ab_getvecR; |
445 |
< |
dp->ib_ndx = ab_getndxR; |
446 |
< |
dp->ob_vec = ab_getvecR; |
447 |
< |
dp->ob_ndx = ab_getndxR; |
444 |
> |
dp->ib_vec = &ab_getvecR; |
445 |
> |
dp->ib_ndx = &ab_getndxR; |
446 |
> |
dp->ob_vec = &ab_getvecR; |
447 |
> |
dp->ob_ndx = &ab_getndxR; |
448 |
|
} |
449 |
< |
dp->ib_ohm = ab_getohm; |
450 |
< |
dp->ob_ohm = ab_getohm; |
449 |
> |
dp->ib_ohm = &ab_getohm; |
450 |
> |
dp->ob_ohm = &ab_getohm; |
451 |
|
df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */ |
452 |
|
df->comp[0].dist = dp; |
453 |
|
df->comp[0].func = &SDhandleMtx; |
459 |
|
return RC_FORMERR; |
460 |
|
} |
461 |
|
for (i = 0; i < dp->ninc*dp->nout; i++) { |
462 |
< |
char *sdnext = f_skip(sdata); |
462 |
> |
char *sdnext = fskip(sdata); |
463 |
|
if (sdnext == NULL) { |
464 |
|
sprintf(SDerrorDetail, |
465 |
|
"Bad/missing BSDF ScatteringData in '%s'", |
516 |
|
c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]); |
517 |
|
dv->cieY += ymin; |
518 |
|
} |
519 |
< |
df->maxHemi -= dv->cieY; /* correct minimum hemispherical */ |
520 |
< |
dv->spec.clock++; /* make sure everything is set */ |
519 |
> |
df->maxHemi -= dv->cieY; /* adjust minimum hemispherical */ |
520 |
> |
/* make sure everything is set */ |
521 |
|
c_ccvt(&dv->spec, C_CSXY+C_CSSPEC); |
522 |
|
} |
523 |
|
|
524 |
|
/* Load a BSDF matrix from an open XML file */ |
525 |
|
SDError |
526 |
< |
SDloadMtx(SDData *sd, ezxml_t fl) |
526 |
> |
SDloadMtx(SDData *sd, ezxml_t wtl) |
527 |
|
{ |
528 |
< |
ezxml_t wtl, wld, wdb; |
528 |
> |
ezxml_t wld, wdb; |
529 |
|
int rowIn; |
530 |
|
struct BSDF_data *dp; |
531 |
|
char *txt; |
532 |
|
int rval; |
533 |
|
|
534 |
< |
if (strcmp(ezxml_name(fl), "WindowElement")) { |
534 |
> |
txt = ezxml_txt(ezxml_child(ezxml_child(wtl, |
535 |
> |
"DataDefinition"), "IncidentDataStructure")); |
536 |
> |
if (txt == NULL || !*txt) { |
537 |
|
sprintf(SDerrorDetail, |
538 |
< |
"BSDF \"%s\": top level node not 'WindowElement'", |
538 |
> |
"BSDF \"%s\": missing IncidentDataStructure", |
539 |
|
sd->name); |
540 |
|
return SDEformat; |
541 |
|
} |
582 |
– |
wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); |
583 |
– |
txt = ezxml_txt(ezxml_child(ezxml_child(wtl, |
584 |
– |
"DataDefinition"), "IncidentDataStructure")); |
542 |
|
if (!strcasecmp(txt, "Rows")) |
543 |
|
rowIn = 1; |
544 |
|
else if (!strcasecmp(txt, "Columns")) |
553 |
|
rval = load_angle_basis(ezxml_child(ezxml_child(wtl, |
554 |
|
"DataDefinition"), "AngleBasis")); |
555 |
|
if (rval < 0) |
556 |
< |
goto err_return; |
556 |
> |
return convert_errcode(rval); |
557 |
|
/* load BSDF components */ |
558 |
|
for (wld = ezxml_child(wtl, "WavelengthData"); |
559 |
|
wld != NULL; wld = wld->next) { |
563 |
|
for (wdb = ezxml_child(wld, "WavelengthDataBlock"); |
564 |
|
wdb != NULL; wdb = wdb->next) |
565 |
|
if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0) |
566 |
< |
goto err_return; |
566 |
> |
return convert_errcode(rval); |
567 |
|
} |
568 |
|
/* separate diffuse components */ |
569 |
|
extract_diffuse(&sd->rLambFront, sd->rf); |
571 |
|
extract_diffuse(&sd->tLamb, sd->tf); |
572 |
|
/* return success */ |
573 |
|
return SDEnone; |
617 |
– |
err_return: /* jump here on failure */ |
618 |
– |
if (sd->rf != NULL) { |
619 |
– |
SDfreeSpectralDF(sd->rf); |
620 |
– |
sd->rf = NULL; |
621 |
– |
} |
622 |
– |
if (sd->rb != NULL) { |
623 |
– |
SDfreeSpectralDF(sd->rb); |
624 |
– |
sd->rb = NULL; |
625 |
– |
} |
626 |
– |
if (sd->tf != NULL) { |
627 |
– |
SDfreeSpectralDF(sd->tf); |
628 |
– |
sd->tf = NULL; |
629 |
– |
} |
630 |
– |
return convert_errcode(rval); |
574 |
|
} |
575 |
|
|
576 |
|
/* Get Matrix BSDF value */ |
599 |
|
SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist) |
600 |
|
{ |
601 |
|
const SDMat *dp = (const SDMat *)dist; |
602 |
< |
|
603 |
< |
if (!(qflags & SDqueryInc+SDqueryOut)) |
602 |
> |
double inc_psa, out_psa; |
603 |
> |
/* check arguments */ |
604 |
> |
if ((psa == NULL) | (vec == NULL) | (dp == NULL)) |
605 |
|
return SDEargument; |
606 |
< |
if (qflags & SDqueryInc) { |
607 |
< |
double inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec)); |
608 |
< |
if (inc_psa < .0) |
609 |
< |
return SDEinternal; |
610 |
< |
switch (qflags & SDqueryMin+SDqueryMax) { |
611 |
< |
case SDqueryMax: |
612 |
< |
if (inc_psa > psa[0]) |
613 |
< |
psa[0] = inc_psa; |
614 |
< |
break; |
615 |
< |
case SDqueryMin+SDqueryMax: |
672 |
< |
if (inc_psa > psa[1]) |
673 |
< |
psa[1] = inc_psa; |
674 |
< |
/* fall through */ |
675 |
< |
case SDqueryMin: |
676 |
< |
if (inc_psa < psa[0]) |
677 |
< |
psa[0] = inc_psa; |
678 |
< |
break; |
679 |
< |
case 0: |
606 |
> |
/* get projected solid angles */ |
607 |
> |
inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec)); |
608 |
> |
out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec)); |
609 |
> |
|
610 |
> |
switch (qflags) { /* record based on flag settings */ |
611 |
> |
case SDqueryVal: |
612 |
> |
psa[0] = .0; |
613 |
> |
/* fall through */ |
614 |
> |
case SDqueryMax: |
615 |
> |
if (inc_psa > psa[0]) |
616 |
|
psa[0] = inc_psa; |
617 |
< |
break; |
618 |
< |
} |
617 |
> |
if (out_psa > psa[0]) |
618 |
> |
psa[0] = out_psa; |
619 |
> |
break; |
620 |
> |
case SDqueryMin+SDqueryMax: |
621 |
> |
if (inc_psa > psa[0]) |
622 |
> |
psa[1] = inc_psa; |
623 |
> |
if (out_psa > psa[0]) |
624 |
> |
psa[1] = out_psa; |
625 |
> |
/* fall through */ |
626 |
> |
case SDqueryMin: |
627 |
> |
if ((inc_psa > .0) & (inc_psa < psa[0])) |
628 |
> |
psa[0] = inc_psa; |
629 |
> |
if ((out_psa > .0) & (out_psa < psa[0])) |
630 |
> |
psa[0] = out_psa; |
631 |
> |
break; |
632 |
|
} |
633 |
< |
if (qflags & SDqueryOut) { |
634 |
< |
double out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec)); |
686 |
< |
if (out_psa < .0) |
687 |
< |
return SDEinternal; |
688 |
< |
switch (qflags & SDqueryMin+SDqueryMax) { |
689 |
< |
case SDqueryMax: |
690 |
< |
if (out_psa > psa[0]) |
691 |
< |
psa[0] = out_psa; |
692 |
< |
break; |
693 |
< |
case SDqueryMin+SDqueryMax: |
694 |
< |
if (out_psa > psa[1]) |
695 |
< |
psa[1] = out_psa; |
696 |
< |
/* fall through */ |
697 |
< |
case SDqueryMin: |
698 |
< |
if (out_psa < psa[0]) |
699 |
< |
psa[0] = out_psa; |
700 |
< |
break; |
701 |
< |
case 0: |
702 |
< |
psa[(qflags&SDqueryInc)!=0] = out_psa; |
703 |
< |
break; |
704 |
< |
} |
705 |
< |
} |
706 |
< |
return SDEnone; |
633 |
> |
/* make sure it's legal */ |
634 |
> |
return (psa[0] <= .0) ? SDEinternal : SDEnone; |
635 |
|
} |
636 |
|
|
637 |
|
/* Compute new cumulative distribution from BSDF */ |
673 |
|
int reverse; |
674 |
|
SDMatCDst myCD; |
675 |
|
SDMatCDst *cd, *cdlast; |
676 |
< |
|
677 |
< |
if (dp == NULL) |
676 |
> |
/* check arguments */ |
677 |
> |
if ((inVec == NULL) | (dp == NULL)) |
678 |
|
return NULL; |
679 |
|
memset(&myCD, 0, sizeof(myCD)); |
680 |
|
myCD.indx = mBSDF_incndx(dp, inVec); |
729 |
|
const SDMatCDst *mcd = (const SDMatCDst *)cdp; |
730 |
|
const unsigned target = randX*maxval; |
731 |
|
int i, iupper, ilower; |
732 |
+ |
/* check arguments */ |
733 |
+ |
if ((outVec == NULL) | (mcd == NULL)) |
734 |
+ |
return SDEargument; |
735 |
|
/* binary search to find index */ |
736 |
|
ilower = 0; iupper = mcd->calen; |
737 |
|
while ((i = (iupper + ilower) >> 1) != ilower) |