11 |
|
* |
12 |
|
*/ |
13 |
|
|
14 |
+ |
#define _USE_MATH_DEFINES |
15 |
|
#include "rtio.h" |
16 |
|
#include <stdlib.h> |
17 |
|
#include <math.h> |
82 |
|
static int |
83 |
|
fequal(double a, double b) |
84 |
|
{ |
85 |
< |
if (b != .0) |
85 |
> |
if (b != 0) |
86 |
|
a = a/b - 1.; |
87 |
|
return (a <= 1e-6) & (a >= -1e-6); |
88 |
|
} |
89 |
|
|
90 |
< |
/* returns the name of the given tag */ |
90 |
< |
#ifdef ezxml_name |
91 |
< |
#undef ezxml_name |
92 |
< |
static char * |
93 |
< |
ezxml_name(ezxml_t xml) |
94 |
< |
{ |
95 |
< |
if (xml == NULL) |
96 |
< |
return NULL; |
97 |
< |
return xml->name; |
98 |
< |
} |
99 |
< |
#endif |
100 |
< |
|
101 |
< |
/* returns the given tag's character content or empty string if none */ |
90 |
> |
/* Returns the given tag's character content or empty string if none */ |
91 |
|
#ifdef ezxml_txt |
92 |
|
#undef ezxml_txt |
93 |
|
static char * |
148 |
|
|
149 |
|
/* get vector for this angle basis index (front exiting) */ |
150 |
|
static int |
151 |
< |
fo_getvec(FVECT v, int ndx, double randX, void *p) |
151 |
> |
fo_getvec(FVECT v, double ndxr, void *p) |
152 |
|
{ |
153 |
< |
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
153 |
> |
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
154 |
> |
int ndx = (int)ndxr; |
155 |
> |
double randX = ndxr - ndx; |
156 |
|
double rx[2]; |
157 |
|
int li; |
158 |
|
double pol, azi, d; |
159 |
|
|
160 |
< |
if ((ndx < 0) | (ndx >= ab->nangles)) |
160 |
> |
if ((ndxr < 0) | (ndx >= ab->nangles)) |
161 |
|
return RC_FAIL; |
162 |
|
for (li = 0; ndx >= ab->lat[li].nphis; li++) |
163 |
|
ndx -= ab->lat[li].nphis; |
176 |
|
static int |
177 |
|
fo_getndx(const FVECT v, void *p) |
178 |
|
{ |
179 |
< |
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
179 |
> |
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
180 |
|
int li, ndx; |
181 |
< |
double pol, azi, d; |
181 |
> |
double pol, azi; |
182 |
|
|
183 |
|
if (v == NULL) |
184 |
|
return -1; |
185 |
< |
if ((v[2] < .0) | (v[2] > 1.0)) |
185 |
> |
if ((v[2] < 0) | (v[2] > 1.)) |
186 |
|
return -1; |
187 |
|
pol = 180.0/M_PI*acos(v[2]); |
188 |
|
azi = 180.0/M_PI*atan2(v[1], v[0]); |
218 |
|
if (li == last_li) /* cached latitude? */ |
219 |
|
return last_ohm; |
220 |
|
last_li = li; |
230 |
– |
theta1 = M_PI/180. * ab->lat[li+1].tmin; |
231 |
– |
if (ab->lat[li].nphis == 1) /* special case */ |
232 |
– |
return last_ohm = M_PI*(1. - sq(cos(theta1))); |
221 |
|
theta = M_PI/180. * ab->lat[li].tmin; |
222 |
+ |
theta1 = M_PI/180. * ab->lat[li+1].tmin; |
223 |
|
return last_ohm = M_PI*(sq(cos(theta)) - sq(cos(theta1))) / |
224 |
|
(double)ab->lat[li].nphis; |
225 |
|
} |
226 |
|
|
227 |
|
/* get vector for this angle basis index (back incident) */ |
228 |
|
static int |
229 |
< |
bi_getvec(FVECT v, int ndx, double randX, void *p) |
229 |
> |
bi_getvec(FVECT v, double ndxr, void *p) |
230 |
|
{ |
231 |
< |
if (!fo_getvec(v, ndx, randX, p)) |
231 |
> |
if (!fo_getvec(v, ndxr, p)) |
232 |
|
return RC_FAIL; |
233 |
|
|
234 |
|
v[0] = -v[0]; |
253 |
|
|
254 |
|
/* get vector for this angle basis index (back exiting) */ |
255 |
|
static int |
256 |
< |
bo_getvec(FVECT v, int ndx, double randX, void *p) |
256 |
> |
bo_getvec(FVECT v, double ndxr, void *p) |
257 |
|
{ |
258 |
< |
if (!fo_getvec(v, ndx, randX, p)) |
258 |
> |
if (!fo_getvec(v, ndxr, p)) |
259 |
|
return RC_FAIL; |
260 |
|
|
261 |
|
v[2] = -v[2]; |
278 |
|
|
279 |
|
/* get vector for this angle basis index (front incident) */ |
280 |
|
static int |
281 |
< |
fi_getvec(FVECT v, int ndx, double randX, void *p) |
281 |
> |
fi_getvec(FVECT v, double ndxr, void *p) |
282 |
|
{ |
283 |
< |
if (!fo_getvec(v, ndx, randX, p)) |
283 |
> |
if (!fo_getvec(v, ndxr, p)) |
284 |
|
return RC_FAIL; |
285 |
|
|
286 |
|
v[0] = -v[0]; |
333 |
|
ezxml_child(ezxml_child(wbb, |
334 |
|
"ThetaBounds"), "UpperTheta"))); |
335 |
|
if (!i) |
336 |
< |
abase_list[nabases].lat[i].tmin = |
348 |
< |
-abase_list[nabases].lat[i+1].tmin; |
336 |
> |
abase_list[nabases].lat[0].tmin = 0; |
337 |
|
else if (!fequal(atof(ezxml_txt(ezxml_child(ezxml_child(wbb, |
338 |
|
"ThetaBounds"), "LowerTheta"))), |
339 |
|
abase_list[nabases].lat[i].tmin)) { |
340 |
|
sprintf(SDerrorDetail, "Theta values disagree in '%s'", |
341 |
< |
abname); |
341 |
> |
abname); |
342 |
|
return RC_DATERR; |
343 |
|
} |
344 |
|
abase_list[nabases].nangles += |
348 |
|
(abase_list[nabases].lat[i].nphis == 1 && |
349 |
|
abase_list[nabases].lat[i].tmin > FTINY)) { |
350 |
|
sprintf(SDerrorDetail, "Illegal phi count in '%s'", |
351 |
< |
abname); |
351 |
> |
abname); |
352 |
|
return RC_DATERR; |
353 |
|
} |
354 |
|
} |
404 |
|
int i; |
405 |
|
/* allocate BSDF component */ |
406 |
|
sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection")); |
407 |
+ |
if (!sdata) |
408 |
+ |
return RC_FAIL; |
409 |
|
/* |
410 |
|
* Remember that front and back are reversed from WINDOW 6 orientations |
411 |
+ |
* Favor their "Front" (incoming light) since that's more often valid |
412 |
|
*/ |
413 |
< |
if ((tfront = !strcasecmp(sdata, "Transmission Back")) || |
414 |
< |
(sd->tf == NULL && |
415 |
< |
!strcasecmp(sdata, "Transmission Front"))) { |
413 |
> |
tfront = !strcasecmp(sdata, "Transmission Back"); |
414 |
> |
if (!strcasecmp(sdata, "Transmission Front") || |
415 |
> |
tfront & (sd->tf == NULL)) { |
416 |
|
if (sd->tf != NULL) |
417 |
|
SDfreeSpectralDF(sd->tf); |
418 |
|
if ((sd->tf = SDnewSpectralDF(1)) == NULL) |
495 |
|
df->comp[0].dist = dp; |
496 |
|
df->comp[0].func = &SDhandleMtx; |
497 |
|
/* read BSDF data */ |
498 |
< |
sdata = ezxml_txt(ezxml_child(wdb,"ScatteringData")); |
498 |
> |
sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData")); |
499 |
|
if (!sdata || !*sdata) { |
500 |
|
sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'", |
501 |
|
sd->name); |
509 |
|
sd->name); |
510 |
|
return RC_FORMERR; |
511 |
|
} |
512 |
< |
while (*sdnext && isspace(*sdnext)) |
512 |
> |
while (isspace(*sdnext)) |
513 |
|
sdnext++; |
514 |
|
if (*sdnext == ',') sdnext++; |
515 |
|
if (rowinc) { |
516 |
|
int r = i/dp->nout; |
517 |
< |
int c = i - c*dp->nout; |
517 |
> |
int c = i - r*dp->nout; |
518 |
|
mBSDF_value(dp,r,c) = atof(sdata); |
519 |
|
} else |
520 |
|
dp->bsdf[i] = atof(sdata); |
534 |
|
for (i = n; --i; ) |
535 |
|
if (sm->bsdf[i] < minv) |
536 |
|
minv = sm->bsdf[i]; |
537 |
+ |
|
538 |
+ |
if (minv <= FTINY) |
539 |
+ |
return .0; |
540 |
+ |
|
541 |
|
for (i = n; i--; ) |
542 |
|
sm->bsdf[i] -= minv; |
543 |
|
|
563 |
|
c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]); |
564 |
|
dv->cieY += ymin; |
565 |
|
} |
566 |
< |
df->maxHemi -= dv->cieY; /* adjust minimum hemispherical */ |
566 |
> |
df->maxHemi -= dv->cieY; /* adjust maximum hemispherical */ |
567 |
|
/* make sure everything is set */ |
568 |
|
c_ccvt(&dv->spec, C_CSXY+C_CSSPEC); |
569 |
|
} |
572 |
|
SDError |
573 |
|
SDloadMtx(SDData *sd, ezxml_t wtl) |
574 |
|
{ |
575 |
< |
ezxml_t wld, wdb; |
576 |
< |
int rowIn; |
577 |
< |
struct BSDF_data *dp; |
578 |
< |
char *txt; |
579 |
< |
int rval; |
585 |
< |
|
575 |
> |
ezxml_t wld, wdb; |
576 |
> |
int rowIn; |
577 |
> |
char *txt; |
578 |
> |
int rval; |
579 |
> |
/* basic checks and data ordering */ |
580 |
|
txt = ezxml_txt(ezxml_child(ezxml_child(wtl, |
581 |
|
"DataDefinition"), "IncidentDataStructure")); |
582 |
|
if (txt == NULL || !*txt) { |
595 |
|
sd->name); |
596 |
|
return SDEsupport; |
597 |
|
} |
598 |
< |
/* get angle basis */ |
599 |
< |
rval = load_angle_basis(ezxml_child(ezxml_child(wtl, |
600 |
< |
"DataDefinition"), "AngleBasis")); |
601 |
< |
if (rval < 0) |
602 |
< |
return convert_errcode(rval); |
603 |
< |
/* load BSDF components */ |
598 |
> |
/* get angle bases */ |
599 |
> |
for (wld = ezxml_child(ezxml_child(wtl, "DataDefinition"), "AngleBasis"); |
600 |
> |
wld != NULL; wld = wld->next) { |
601 |
> |
rval = load_angle_basis(wld); |
602 |
> |
if (rval < 0) |
603 |
> |
return convert_errcode(rval); |
604 |
> |
} |
605 |
> |
/* load BSDF components */ |
606 |
|
for (wld = ezxml_child(wtl, "WavelengthData"); |
607 |
|
wld != NULL; wld = wld->next) { |
608 |
|
if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")), |
613 |
|
if ((rval = load_bsdf_data(sd, wdb, rowIn)) < 0) |
614 |
|
return convert_errcode(rval); |
615 |
|
} |
616 |
< |
/* separate diffuse components */ |
616 |
> |
/* separate diffuse components */ |
617 |
|
extract_diffuse(&sd->rLambFront, sd->rf); |
618 |
|
extract_diffuse(&sd->rLambBack, sd->rb); |
619 |
|
extract_diffuse(&sd->tLamb, sd->tf); |
620 |
< |
/* return success */ |
620 |
> |
/* return success */ |
621 |
|
return SDEnone; |
622 |
|
} |
623 |
|
|
624 |
|
/* Get Matrix BSDF value */ |
625 |
|
static int |
626 |
|
SDgetMtxBSDF(float coef[SDmaxCh], const FVECT outVec, |
627 |
< |
const FVECT inVec, const void *dist) |
627 |
> |
const FVECT inVec, SDComponent *sdc) |
628 |
|
{ |
629 |
< |
const SDMat *dp = (const SDMat *)dist; |
629 |
> |
const SDMat *dp; |
630 |
|
int i_ndx, o_ndx; |
631 |
+ |
/* check arguments */ |
632 |
+ |
if ((coef == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL) |
633 |
+ |
|| (dp = (SDMat *)sdc->dist) == NULL) |
634 |
+ |
return 0; |
635 |
|
/* get angle indices */ |
636 |
|
i_ndx = mBSDF_incndx(dp, inVec); |
637 |
|
o_ndx = mBSDF_outndx(dp, outVec); |
646 |
|
return 1; /* XXX monochrome for now */ |
647 |
|
} |
648 |
|
|
649 |
< |
/* Query solid angle for vector */ |
649 |
> |
/* Query solid angle for vector(s) */ |
650 |
|
static SDError |
651 |
< |
SDqueryMtxProjSA(double *psa, const FVECT vec, int qflags, const void *dist) |
651 |
> |
SDqueryMtxProjSA(double *psa, const FVECT v1, const RREAL *v2, |
652 |
> |
int qflags, SDComponent *sdc) |
653 |
|
{ |
654 |
< |
const SDMat *dp = (const SDMat *)dist; |
654 |
> |
const SDMat *dp; |
655 |
|
double inc_psa, out_psa; |
656 |
|
/* check arguments */ |
657 |
< |
if ((psa == NULL) | (vec == NULL) | (dp == NULL)) |
657 |
> |
if ((psa == NULL) | (v1 == NULL) | (sdc == NULL) || |
658 |
> |
(dp = (SDMat *)sdc->dist) == NULL) |
659 |
|
return SDEargument; |
660 |
+ |
if (v2 == NULL) |
661 |
+ |
v2 = v1; |
662 |
|
/* get projected solid angles */ |
663 |
< |
inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, vec)); |
664 |
< |
out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, vec)); |
663 |
> |
out_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v1)); |
664 |
> |
inc_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v2)); |
665 |
> |
if ((v1 != v2) & (out_psa <= 0) & (inc_psa <= 0)) { |
666 |
> |
inc_psa = mBSDF_outohm(dp, mBSDF_outndx(dp, v2)); |
667 |
> |
out_psa = mBSDF_incohm(dp, mBSDF_incndx(dp, v1)); |
668 |
> |
} |
669 |
|
|
670 |
|
switch (qflags) { /* record based on flag settings */ |
663 |
– |
case SDqueryVal: |
664 |
– |
psa[0] = .0; |
665 |
– |
/* fall through */ |
671 |
|
case SDqueryMax: |
672 |
|
if (inc_psa > psa[0]) |
673 |
|
psa[0] = inc_psa; |
675 |
|
psa[0] = out_psa; |
676 |
|
break; |
677 |
|
case SDqueryMin+SDqueryMax: |
678 |
< |
if (inc_psa > psa[0]) |
678 |
> |
if (inc_psa > psa[1]) |
679 |
|
psa[1] = inc_psa; |
680 |
< |
if (out_psa > psa[0]) |
680 |
> |
if (out_psa > psa[1]) |
681 |
|
psa[1] = out_psa; |
682 |
|
/* fall through */ |
683 |
+ |
case SDqueryVal: |
684 |
+ |
if (qflags == SDqueryVal) |
685 |
+ |
psa[0] = M_PI; |
686 |
+ |
/* fall through */ |
687 |
|
case SDqueryMin: |
688 |
< |
if ((inc_psa > .0) & (inc_psa < psa[0])) |
688 |
> |
if ((inc_psa > 0) & (inc_psa < psa[0])) |
689 |
|
psa[0] = inc_psa; |
690 |
< |
if ((out_psa > .0) & (out_psa < psa[0])) |
690 |
> |
if ((out_psa > 0) & (out_psa < psa[0])) |
691 |
|
psa[0] = out_psa; |
692 |
|
break; |
693 |
|
} |
694 |
|
/* make sure it's legal */ |
695 |
< |
return (psa[0] <= .0) ? SDEinternal : SDEnone; |
695 |
> |
return (psa[0] <= 0) ? SDEinternal : SDEnone; |
696 |
|
} |
697 |
|
|
698 |
|
/* Compute new cumulative distribution from BSDF */ |
730 |
|
static const SDCDst * |
731 |
|
SDgetMtxCDist(const FVECT inVec, SDComponent *sdc) |
732 |
|
{ |
733 |
< |
SDMat *dp = (SDMat *)sdc->dist; |
733 |
> |
SDMat *dp; |
734 |
|
int reverse; |
735 |
|
SDMatCDst myCD; |
736 |
|
SDMatCDst *cd, *cdlast; |
737 |
|
/* check arguments */ |
738 |
< |
if ((inVec == NULL) | (dp == NULL)) |
738 |
> |
if ((inVec == NULL) | (sdc == NULL) || |
739 |
> |
(dp = (SDMat *)sdc->dist) == NULL) |
740 |
|
return NULL; |
741 |
|
memset(&myCD, 0, sizeof(myCD)); |
742 |
|
myCD.indx = mBSDF_incndx(dp, inVec); |
755 |
|
reverse = 1; |
756 |
|
} |
757 |
|
cdlast = NULL; /* check for it in cache list */ |
758 |
< |
for (cd = (SDMatCDst *)sdc->cdList; |
759 |
< |
cd != NULL; cd = (SDMatCDst *)cd->next) { |
758 |
> |
for (cd = (SDMatCDst *)sdc->cdList; cd != NULL; |
759 |
> |
cdlast = cd, cd = cd->next) |
760 |
|
if (cd->indx == myCD.indx && (cd->calen == myCD.calen) & |
761 |
|
(cd->ob_priv == myCD.ob_priv) & |
762 |
|
(cd->ob_vec == myCD.ob_vec)) |
763 |
|
break; |
754 |
– |
cdlast = cd; |
755 |
– |
} |
764 |
|
if (cd == NULL) { /* need to allocate new entry */ |
765 |
|
cd = (SDMatCDst *)malloc(sizeof(SDMatCDst) + |
766 |
< |
myCD.calen*sizeof(myCD.carr[0])); |
766 |
> |
sizeof(myCD.carr[0])*myCD.calen); |
767 |
|
if (cd == NULL) |
768 |
|
return NULL; |
769 |
|
*cd = myCD; /* compute cumulative distribution */ |
775 |
|
} |
776 |
|
if (cdlast != NULL) { /* move entry to head of cache list */ |
777 |
|
cdlast->next = cd->next; |
778 |
< |
cd->next = sdc->cdList; |
778 |
> |
cd->next = (SDMatCDst *)sdc->cdList; |
779 |
|
sdc->cdList = (SDCDst *)cd; |
780 |
|
} |
781 |
|
return (SDCDst *)cd; /* ready to go */ |
783 |
|
|
784 |
|
/* Sample cumulative distribution */ |
785 |
|
static SDError |
786 |
< |
SDsampMtxCDist(FVECT outVec, double randX, const SDCDst *cdp) |
786 |
> |
SDsampMtxCDist(FVECT ioVec, double randX, const SDCDst *cdp) |
787 |
|
{ |
788 |
|
const unsigned maxval = ~0; |
789 |
|
const SDMatCDst *mcd = (const SDMatCDst *)cdp; |
790 |
|
const unsigned target = randX*maxval; |
791 |
|
int i, iupper, ilower; |
792 |
|
/* check arguments */ |
793 |
< |
if ((outVec == NULL) | (mcd == NULL)) |
793 |
> |
if ((ioVec == NULL) | (mcd == NULL)) |
794 |
|
return SDEargument; |
795 |
|
/* binary search to find index */ |
796 |
|
ilower = 0; iupper = mcd->calen; |
797 |
|
while ((i = (iupper + ilower) >> 1) != ilower) |
798 |
< |
if ((long)target >= (long)mcd->carr[i]) |
798 |
> |
if (target >= mcd->carr[i]) |
799 |
|
ilower = i; |
800 |
|
else |
801 |
|
iupper = i; |
803 |
|
randX = (randX*maxval - mcd->carr[ilower]) / |
804 |
|
(double)(mcd->carr[iupper] - mcd->carr[ilower]); |
805 |
|
/* convert index to vector */ |
806 |
< |
if ((*mcd->ob_vec)(outVec, i, randX, mcd->ob_priv)) |
806 |
> |
if ((*mcd->ob_vec)(ioVec, i+randX, mcd->ob_priv)) |
807 |
|
return SDEnone; |
808 |
< |
strcpy(SDerrorDetail, "BSDF sampling fault"); |
808 |
> |
strcpy(SDerrorDetail, "Matrix BSDF sampling fault"); |
809 |
|
return SDEinternal; |
810 |
|
} |
811 |
|
|