15 |
|
#include <stdlib.h> |
16 |
|
#include <string.h> |
17 |
|
#include <math.h> |
18 |
+ |
#include <ctype.h> |
19 |
|
#include "ezxml.h" |
20 |
|
#include "hilbert.h" |
21 |
|
#include "bsdf.h" |
38 |
|
/* Additional information on last error (ASCII English) */ |
39 |
|
char SDerrorDetail[256]; |
40 |
|
|
41 |
+ |
/* Empty distribution for getCDist() calls that fail for some reason */ |
42 |
+ |
const SDCDst SDemptyCD; |
43 |
+ |
|
44 |
|
/* Cache of loaded BSDFs */ |
45 |
|
struct SDCache_s *SDcacheList = NULL; |
46 |
|
|
95 |
|
|
96 |
|
if (wdb == NULL) /* no geometry section? */ |
97 |
|
return SDEnone; |
98 |
+ |
if ((geom = ezxml_child(wdb, "Name")) != NULL) { |
99 |
+ |
strncpy(sd->matn, ezxml_txt(geom), SDnameLn); |
100 |
+ |
if (sd->matn[SDnameLn-1]) |
101 |
+ |
strcpy(sd->matn+(SDnameLn-4), "..."); |
102 |
+ |
} |
103 |
+ |
if ((geom = ezxml_child(wdb, "Manufacturer")) != NULL) { |
104 |
+ |
strncpy(sd->makr, ezxml_txt(geom), SDnameLn); |
105 |
+ |
if (sd->makr[SDnameLn-1]) |
106 |
+ |
strcpy(sd->makr+(SDnameLn-4), "..."); |
107 |
+ |
} |
108 |
|
sd->dim[0] = sd->dim[1] = sd->dim[2] = .0; |
109 |
+ |
SDerrorDetail[0] = '\0'; |
110 |
|
if ((geom = ezxml_child(wdb, "Width")) != NULL) |
111 |
|
sd->dim[0] = atof(ezxml_txt(geom)) * |
112 |
|
to_meters(ezxml_attr(geom, "unit")); |
117 |
|
sd->dim[2] = atof(ezxml_txt(geom)) * |
118 |
|
to_meters(ezxml_attr(geom, "unit")); |
119 |
|
if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) { |
120 |
< |
sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name); |
120 |
> |
if (!SDerrorDetail[0]) |
121 |
> |
sprintf(SDerrorDetail, "Negative dimension in \"%s\"", |
122 |
> |
sd->name); |
123 |
|
return SDEdata; |
124 |
|
} |
125 |
|
if ((geom = ezxml_child(wdb, "Geometry")) == NULL || |
126 |
|
(mgfstr = ezxml_txt(geom)) == NULL) |
127 |
|
return SDEnone; |
128 |
+ |
while (isspace(*mgfstr)) |
129 |
+ |
++mgfstr; |
130 |
+ |
if (!*mgfstr) |
131 |
+ |
return SDEnone; |
132 |
|
if ((fmt = ezxml_attr(geom, "format")) != NULL && |
133 |
|
strcasecmp(fmt, "MGF")) { |
134 |
|
sprintf(SDerrorDetail, |
137 |
|
return SDEsupport; |
138 |
|
} |
139 |
|
cfact = to_meters(ezxml_attr(geom, "unit")); |
140 |
+ |
if (cfact <= 0) |
141 |
+ |
return SDEformat; |
142 |
|
sd->mgf = (char *)malloc(strlen(mgfstr)+32); |
143 |
|
if (sd->mgf == NULL) { |
144 |
|
strcpy(SDerrorDetail, "Out of memory in SDloadGeometry"); |
182 |
|
} |
183 |
|
wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); |
184 |
|
if (wtl == NULL) { |
185 |
< |
sprintf(SDerrorDetail, "BSDF \"%s\": no optical layer'", |
185 |
> |
sprintf(SDerrorDetail, "BSDF \"%s\": no optical layers'", |
186 |
|
sd->name); |
187 |
|
ezxml_free(fl); |
188 |
|
return SDEformat; |
189 |
|
} |
190 |
|
/* load geometry if present */ |
191 |
|
lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material")); |
192 |
< |
if (lastErr) |
192 |
> |
if (lastErr) { |
193 |
> |
ezxml_free(fl); |
194 |
|
return lastErr; |
195 |
+ |
} |
196 |
|
/* try loading variable resolution data */ |
197 |
|
lastErr = SDloadTre(sd, wtl); |
198 |
|
/* check our result */ |
216 |
|
if (sd->tf != NULL && sd->tf->maxHemi <= .001) { |
217 |
|
SDfreeSpectralDF(sd->tf); sd->tf = NULL; |
218 |
|
} |
219 |
+ |
if (sd->tb != NULL && sd->tb->maxHemi <= .001) { |
220 |
+ |
SDfreeSpectralDF(sd->tb); sd->tb = NULL; |
221 |
+ |
} |
222 |
|
/* return success */ |
223 |
|
return SDEnone; |
224 |
|
} |
247 |
|
return df; |
248 |
|
} |
249 |
|
|
250 |
+ |
/* Add component(s) to spectral distribution function */ |
251 |
+ |
SDSpectralDF * |
252 |
+ |
SDaddComponent(SDSpectralDF *odf, int nadd) |
253 |
+ |
{ |
254 |
+ |
SDSpectralDF *df; |
255 |
+ |
|
256 |
+ |
if (odf == NULL) |
257 |
+ |
return SDnewSpectralDF(nadd); |
258 |
+ |
if (nadd <= 0) |
259 |
+ |
return odf; |
260 |
+ |
df = (SDSpectralDF *)realloc(odf, sizeof(SDSpectralDF) + |
261 |
+ |
(odf->ncomp+nadd-1)*sizeof(SDComponent)); |
262 |
+ |
if (df == NULL) { |
263 |
+ |
sprintf(SDerrorDetail, |
264 |
+ |
"Cannot add %d component(s) to spectral DF", nadd); |
265 |
+ |
SDfreeSpectralDF(odf); |
266 |
+ |
return NULL; |
267 |
+ |
} |
268 |
+ |
memset(df->comp+df->ncomp, 0, nadd*sizeof(SDComponent)); |
269 |
+ |
df->ncomp += nadd; |
270 |
+ |
return df; |
271 |
+ |
} |
272 |
+ |
|
273 |
|
/* Free cached cumulative distributions for BSDF component */ |
274 |
|
void |
275 |
|
SDfreeCumulativeCache(SDSpectralDF *df) |
296 |
|
return; |
297 |
|
SDfreeCumulativeCache(df); |
298 |
|
for (n = df->ncomp; n-- > 0; ) |
299 |
< |
(*df->comp[n].func->freeSC)(df->comp[n].dist); |
299 |
> |
if (df->comp[n].dist != NULL) |
300 |
> |
(*df->comp[n].func->freeSC)(df->comp[n].dist); |
301 |
|
free(df); |
302 |
|
} |
303 |
|
|
353 |
|
SDfreeSpectralDF(sd->tf); |
354 |
|
sd->tf = NULL; |
355 |
|
} |
356 |
+ |
if (sd->tb != NULL) { |
357 |
+ |
SDfreeSpectralDF(sd->tb); |
358 |
+ |
sd->tb = NULL; |
359 |
+ |
} |
360 |
|
sd->rLambFront.cieY = .0; |
361 |
|
sd->rLambFront.spec.flags = 0; |
362 |
|
sd->rLambBack.cieY = .0; |
444 |
|
SDfreeCumulativeCache(sd->rf); |
445 |
|
SDfreeCumulativeCache(sd->rb); |
446 |
|
SDfreeCumulativeCache(sd->tf); |
447 |
+ |
SDfreeCumulativeCache(sd->tb); |
448 |
|
return; |
449 |
|
} |
450 |
|
/* remove from list and free */ |
474 |
|
cd = (*sdc->func->getCDist)(inVec, sdc); |
475 |
|
if (cd == NULL) |
476 |
|
return SDEmemory; |
477 |
< |
if (cd->cTotal <= 1e-7) { /* anything to sample? */ |
477 |
> |
if (cd->cTotal <= 1e-6) { /* anything to sample? */ |
478 |
|
sv->spec = c_dfcolor; |
479 |
|
sv->cieY = .0; |
480 |
|
memset(ioVec, 0, 3*sizeof(double)); |
511 |
|
unsigned nBits; |
512 |
|
double scale; |
513 |
|
bitmask_t ndx, coord[MS_MAXDIM]; |
514 |
< |
|
514 |
> |
|
515 |
> |
if (n <= 0) /* check corner cases */ |
516 |
> |
return; |
517 |
> |
if (randX < 0) randX = 0; |
518 |
> |
else if (randX >= 1.) randX = 0.999999999999999; |
519 |
> |
if (n == 1) { |
520 |
> |
t[0] = randX; |
521 |
> |
return; |
522 |
> |
} |
523 |
|
while (n > MS_MAXDIM) /* punt for higher dimensions */ |
524 |
|
t[--n] = rand()*(1./(RAND_MAX+.5)); |
525 |
|
nBits = (8*sizeof(bitmask_t) - 1) / n; |
573 |
|
case 0: |
574 |
|
return SDEargument; |
575 |
|
} |
576 |
< |
if (v1[2] > 0) /* front surface query? */ |
576 |
> |
if (v1[2] > 0) { /* front surface query? */ |
577 |
|
rdf = sd->rf; |
578 |
< |
else |
578 |
> |
tdf = (sd->tf != NULL) ? sd->tf : sd->tb; |
579 |
> |
} else { |
580 |
|
rdf = sd->rb; |
581 |
< |
tdf = sd->tf; |
581 |
> |
tdf = (sd->tb != NULL) ? sd->tb : sd->tf; |
582 |
> |
} |
583 |
|
if (v2 != NULL) /* bidirectional? */ |
584 |
|
if (v1[2] > 0 ^ v2[2] > 0) |
585 |
|
rdf = NULL; |
627 |
|
} else if (!(inFront | outFront)) { |
628 |
|
*sv = sd->rLambBack; |
629 |
|
sdf = sd->rb; |
630 |
< |
} else /* inFront ^ outFront */ { |
630 |
> |
} else if (inFront) { |
631 |
|
*sv = sd->tLamb; |
632 |
< |
sdf = sd->tf; |
632 |
> |
sdf = (sd->tf != NULL) ? sd->tf : sd->tb; |
633 |
> |
} else /* inBack */ { |
634 |
> |
*sv = sd->tLamb; |
635 |
> |
sdf = (sd->tb != NULL) ? sd->tb : sd->tf; |
636 |
|
} |
637 |
|
sv->cieY *= 1./M_PI; |
638 |
|
/* add non-diffuse components */ |
656 |
|
SDdirectHemi(const FVECT inVec, int sflags, const SDData *sd) |
657 |
|
{ |
658 |
|
double hsum; |
659 |
< |
SDSpectralDF *rdf; |
659 |
> |
SDSpectralDF *rdf, *tdf; |
660 |
|
const SDCDst *cd; |
661 |
|
int i; |
662 |
|
/* check arguments */ |
666 |
|
if (inVec[2] > 0) { |
667 |
|
hsum = sd->rLambFront.cieY; |
668 |
|
rdf = sd->rf; |
669 |
+ |
tdf = (sd->tf != NULL) ? sd->tf : sd->tb; |
670 |
|
} else /* !inFront */ { |
671 |
|
hsum = sd->rLambBack.cieY; |
672 |
|
rdf = sd->rb; |
673 |
+ |
tdf = (sd->tb != NULL) ? sd->tb : sd->tf; |
674 |
|
} |
675 |
|
if ((sflags & SDsampDf+SDsampR) != SDsampDf+SDsampR) |
676 |
|
hsum = .0; |
677 |
|
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) |
678 |
|
hsum += sd->tLamb.cieY; |
679 |
|
/* gather non-diffuse components */ |
680 |
< |
i = ((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR && |
681 |
< |
rdf != NULL) ? rdf->ncomp : 0; |
680 |
> |
i = (((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR) & |
681 |
> |
(rdf != NULL)) ? rdf->ncomp : 0; |
682 |
|
while (i-- > 0) { /* non-diffuse reflection */ |
683 |
|
cd = (*rdf->comp[i].func->getCDist)(inVec, &rdf->comp[i]); |
684 |
|
if (cd != NULL) |
685 |
|
hsum += cd->cTotal; |
686 |
|
} |
687 |
< |
i = ((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT && |
688 |
< |
sd->tf != NULL) ? sd->tf->ncomp : 0; |
687 |
> |
i = (((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT) & |
688 |
> |
(tdf != NULL)) ? tdf->ncomp : 0; |
689 |
|
while (i-- > 0) { /* non-diffuse transmission */ |
690 |
< |
cd = (*sd->tf->comp[i].func->getCDist)(inVec, &sd->tf->comp[i]); |
690 |
> |
cd = (*tdf->comp[i].func->getCDist)(inVec, &tdf->comp[i]); |
691 |
|
if (cd != NULL) |
692 |
|
hsum += cd->cTotal; |
693 |
|
} |
701 |
|
SDError ec; |
702 |
|
FVECT inVec; |
703 |
|
int inFront; |
704 |
< |
SDSpectralDF *rdf; |
704 |
> |
SDSpectralDF *rdf, *tdf; |
705 |
|
double rdiff; |
706 |
|
float coef[SDmaxCh]; |
707 |
|
int i, j, n, nr; |
718 |
|
if (inFront) { |
719 |
|
*sv = sd->rLambFront; |
720 |
|
rdf = sd->rf; |
721 |
+ |
tdf = (sd->tf != NULL) ? sd->tf : sd->tb; |
722 |
|
} else /* !inFront */ { |
723 |
|
*sv = sd->rLambBack; |
724 |
|
rdf = sd->rb; |
725 |
+ |
tdf = (sd->tb != NULL) ? sd->tb : sd->tf; |
726 |
|
} |
727 |
|
if ((sflags & SDsampDf+SDsampR) != SDsampDf+SDsampR) |
728 |
|
sv->cieY = .0; |
730 |
|
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) |
731 |
|
sv->cieY += sd->tLamb.cieY; |
732 |
|
/* gather non-diffuse components */ |
733 |
< |
i = nr = ((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR && |
734 |
< |
rdf != NULL) ? rdf->ncomp : 0; |
735 |
< |
j = ((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT && |
736 |
< |
sd->tf != NULL) ? sd->tf->ncomp : 0; |
733 |
> |
i = nr = (((sflags & SDsampSp+SDsampR) == SDsampSp+SDsampR) & |
734 |
> |
(rdf != NULL)) ? rdf->ncomp : 0; |
735 |
> |
j = (((sflags & SDsampSp+SDsampT) == SDsampSp+SDsampT) & |
736 |
> |
(tdf != NULL)) ? tdf->ncomp : 0; |
737 |
|
n = i + j; |
738 |
|
if (n > 0 && (cdarr = (const SDCDst **)malloc(n*sizeof(SDCDst *))) == NULL) |
739 |
|
return SDEmemory; |
740 |
|
while (j-- > 0) { /* non-diffuse transmission */ |
741 |
< |
cdarr[i+j] = (*sd->tf->comp[j].func->getCDist)(inVec, &sd->tf->comp[j]); |
741 |
> |
cdarr[i+j] = (*tdf->comp[j].func->getCDist)(inVec, &tdf->comp[j]); |
742 |
|
if (cdarr[i+j] == NULL) { |
743 |
|
free(cdarr); |
744 |
|
return SDEmemory; |
753 |
|
} |
754 |
|
sv->cieY += cdarr[i]->cTotal; |
755 |
|
} |
756 |
< |
if (sv->cieY <= 1e-7) { /* anything to sample? */ |
756 |
> |
if (sv->cieY <= 1e-6) { /* anything to sample? */ |
757 |
|
sv->cieY = .0; |
758 |
|
memset(ioVec, 0, 3*sizeof(double)); |
759 |
|
return SDEnone; |
781 |
|
if (i >= n) |
782 |
|
return SDEinternal; |
783 |
|
/* compute sample direction */ |
784 |
< |
sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr]; |
784 |
> |
sdc = (i < nr) ? &rdf->comp[i] : &tdf->comp[i-nr]; |
785 |
|
ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); |
786 |
|
if (ec) |
787 |
|
return ec; |
883 |
|
|
884 |
|
#include "standard.h" |
885 |
|
#include "paths.h" |
812 |
– |
#include <ctype.h> |
886 |
|
|
887 |
|
#define MAXLATS 46 /* maximum number of latitudes */ |
888 |
|
|
1002 |
|
{ |
1003 |
|
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
1004 |
|
int li, ndx; |
1005 |
< |
double pol, azi, d; |
1005 |
> |
double pol, azi; |
1006 |
|
|
1007 |
|
if ((v[2] < -1.0) | (v[2] > 1.0)) |
1008 |
|
return(-1); |
1242 |
|
) |
1243 |
|
{ |
1244 |
|
double *omega_iarr, *omega_oarr; |
1245 |
< |
double dom, contrib, hemi_total, full_total; |
1245 |
> |
double dom, hemi_total, full_total; |
1246 |
|
int nneg; |
1247 |
|
FVECT v; |
1248 |
|
int i, o; |
1392 |
|
error(WARNING, errmsg); |
1393 |
|
ezxml_free(fl); |
1394 |
|
return(NULL); |
1395 |
< |
} |
1396 |
< |
load_angle_basis(ezxml_child(ezxml_child(wtl, |
1397 |
< |
"DataDefinition"), "AngleBasis")); |
1395 |
> |
} |
1396 |
> |
for (wld = ezxml_child(ezxml_child(wtl, |
1397 |
> |
"DataDefinition"), "AngleBasis"); |
1398 |
> |
wld != NULL; wld = wld->next) |
1399 |
> |
load_angle_basis(wld); |
1400 |
|
dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data)); |
1401 |
|
load_geometry(dp, ezxml_child(wtl, "Material")); |
1402 |
|
for (wld = ezxml_child(wtl, "WavelengthData"); |
1457 |
|
if (!getBSDF_incvec(v, b, i)) |
1458 |
|
return(0); |
1459 |
|
rad = sqrt(getBSDF_incohm(b, i) / PI); |
1460 |
< |
multisamp(pert, 3, rv); |
1460 |
> |
SDmultiSamp(pert, 3, rv); |
1461 |
|
for (j = 0; j < 3; j++) |
1462 |
|
v[j] += rad*(2.*pert[j] - 1.); |
1463 |
|
if (xm != NULL) |
1482 |
|
if (!getBSDF_outvec(v, b, o)) |
1483 |
|
return(0); |
1484 |
|
rad = sqrt(getBSDF_outohm(b, o) / PI); |
1485 |
< |
multisamp(pert, 3, rv); |
1485 |
> |
SDmultiSamp(pert, 3, rv); |
1486 |
|
for (j = 0; j < 3; j++) |
1487 |
|
v[j] += rad*(2.*pert[j] - 1.); |
1488 |
|
if (xm != NULL) |