10 |
|
* |
11 |
|
*/ |
12 |
|
|
13 |
+ |
#define _USE_MATH_DEFINES |
14 |
|
#include <stdio.h> |
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" |
92 |
|
|
93 |
|
if (wdb == NULL) /* no geometry section? */ |
94 |
|
return SDEnone; |
95 |
+ |
if ((geom = ezxml_child(wdb, "Name")) != NULL) { |
96 |
+ |
strncpy(sd->matn, ezxml_txt(geom), SDnameLn); |
97 |
+ |
sd->matn[SDnameLn-1] = '\0'; |
98 |
+ |
} |
99 |
+ |
if ((geom = ezxml_child(wdb, "Manufacturer")) != NULL) { |
100 |
+ |
strncpy(sd->makr, ezxml_txt(geom), SDnameLn); |
101 |
+ |
sd->makr[SDnameLn-1] = '\0'; |
102 |
+ |
} |
103 |
|
sd->dim[0] = sd->dim[1] = sd->dim[2] = .0; |
104 |
+ |
SDerrorDetail[0] = '\0'; |
105 |
|
if ((geom = ezxml_child(wdb, "Width")) != NULL) |
106 |
|
sd->dim[0] = atof(ezxml_txt(geom)) * |
107 |
|
to_meters(ezxml_attr(geom, "unit")); |
111 |
|
if ((geom = ezxml_child(wdb, "Thickness")) != NULL) |
112 |
|
sd->dim[2] = atof(ezxml_txt(geom)) * |
113 |
|
to_meters(ezxml_attr(geom, "unit")); |
114 |
< |
if ((sd->dim[0] < .0) | (sd->dim[1] < .0) | (sd->dim[2] < .0)) { |
115 |
< |
sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name); |
114 |
> |
if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) { |
115 |
> |
if (!SDerrorDetail[0]) |
116 |
> |
sprintf(SDerrorDetail, "Negative dimension in \"%s\"", |
117 |
> |
sd->name); |
118 |
|
return SDEdata; |
119 |
|
} |
120 |
|
if ((geom = ezxml_child(wdb, "Geometry")) == NULL || |
121 |
|
(mgfstr = ezxml_txt(geom)) == NULL) |
122 |
|
return SDEnone; |
123 |
+ |
while (isspace(*mgfstr)) |
124 |
+ |
++mgfstr; |
125 |
+ |
if (!*mgfstr) |
126 |
+ |
return SDEnone; |
127 |
|
if ((fmt = ezxml_attr(geom, "format")) != NULL && |
128 |
|
strcasecmp(fmt, "MGF")) { |
129 |
|
sprintf(SDerrorDetail, |
132 |
|
return SDEsupport; |
133 |
|
} |
134 |
|
cfact = to_meters(ezxml_attr(geom, "unit")); |
135 |
+ |
if (cfact <= 0) |
136 |
+ |
return SDEformat; |
137 |
|
sd->mgf = (char *)malloc(strlen(mgfstr)+32); |
138 |
|
if (sd->mgf == NULL) { |
139 |
|
strcpy(SDerrorDetail, "Out of memory in SDloadGeometry"); |
177 |
|
} |
178 |
|
wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); |
179 |
|
if (wtl == NULL) { |
180 |
< |
sprintf(SDerrorDetail, "BSDF \"%s\": no optical layer'", |
180 |
> |
sprintf(SDerrorDetail, "BSDF \"%s\": no optical layers'", |
181 |
|
sd->name); |
182 |
|
ezxml_free(fl); |
183 |
|
return SDEformat; |
184 |
|
} |
185 |
|
/* load geometry if present */ |
186 |
|
lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material")); |
187 |
< |
if (lastErr) |
187 |
> |
if (lastErr) { |
188 |
> |
ezxml_free(fl); |
189 |
|
return lastErr; |
190 |
+ |
} |
191 |
|
/* try loading variable resolution data */ |
192 |
|
lastErr = SDloadTre(sd, wtl); |
193 |
|
/* check our result */ |
194 |
< |
switch (lastErr) { |
173 |
< |
case SDEformat: |
174 |
< |
case SDEdata: |
175 |
< |
case SDEsupport: /* possibly we just tried the wrong format */ |
194 |
> |
if (lastErr == SDEsupport) /* try matrix BSDF if not tree data */ |
195 |
|
lastErr = SDloadMtx(sd, wtl); |
196 |
< |
break; |
178 |
< |
default: /* variable res. OK else serious error */ |
179 |
< |
break; |
180 |
< |
} |
196 |
> |
|
197 |
|
/* done with XML file */ |
198 |
|
ezxml_free(fl); |
199 |
|
|
239 |
|
return df; |
240 |
|
} |
241 |
|
|
242 |
+ |
/* Add component(s) to spectral distribution function */ |
243 |
+ |
SDSpectralDF * |
244 |
+ |
SDaddComponent(SDSpectralDF *odf, int nadd) |
245 |
+ |
{ |
246 |
+ |
SDSpectralDF *df; |
247 |
+ |
|
248 |
+ |
if (odf == NULL) |
249 |
+ |
return SDnewSpectralDF(nadd); |
250 |
+ |
if (nadd <= 0) |
251 |
+ |
return odf; |
252 |
+ |
df = (SDSpectralDF *)realloc(odf, sizeof(SDSpectralDF) + |
253 |
+ |
(odf->ncomp+nadd-1)*sizeof(SDComponent)); |
254 |
+ |
if (df == NULL) { |
255 |
+ |
sprintf(SDerrorDetail, |
256 |
+ |
"Cannot add %d component(s) to spectral DF", nadd); |
257 |
+ |
SDfreeSpectralDF(odf); |
258 |
+ |
return NULL; |
259 |
+ |
} |
260 |
+ |
memset(df->comp+df->ncomp, 0, nadd*sizeof(SDComponent)); |
261 |
+ |
df->ncomp += nadd; |
262 |
+ |
return df; |
263 |
+ |
} |
264 |
+ |
|
265 |
|
/* Free cached cumulative distributions for BSDF component */ |
266 |
|
void |
267 |
|
SDfreeCumulativeCache(SDSpectralDF *df) |
288 |
|
return; |
289 |
|
SDfreeCumulativeCache(df); |
290 |
|
for (n = df->ncomp; n-- > 0; ) |
291 |
< |
(*df->comp[n].func->freeSC)(df->comp[n].dist); |
291 |
> |
if (df->comp[n].dist != NULL) |
292 |
> |
(*df->comp[n].func->freeSC)(df->comp[n].dist); |
293 |
|
free(df); |
294 |
|
} |
295 |
|
|
445 |
|
|
446 |
|
/* Sample an individual BSDF component */ |
447 |
|
SDError |
448 |
< |
SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec, |
409 |
< |
double randX, SDComponent *sdc) |
448 |
> |
SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc) |
449 |
|
{ |
450 |
|
float coef[SDmaxCh]; |
451 |
|
SDError ec; |
452 |
+ |
FVECT inVec; |
453 |
|
const SDCDst *cd; |
454 |
|
double d; |
455 |
|
int n; |
456 |
|
/* check arguments */ |
457 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)) |
457 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL)) |
458 |
|
return SDEargument; |
459 |
|
/* get cumulative distribution */ |
460 |
+ |
VCOPY(inVec, ioVec); |
461 |
|
cd = (*sdc->func->getCDist)(inVec, sdc); |
462 |
|
if (cd == NULL) |
463 |
|
return SDEmemory; |
464 |
< |
if (cd->cTotal <= 1e-7) { /* anything to sample? */ |
464 |
> |
if (cd->cTotal <= 1e-6) { /* anything to sample? */ |
465 |
|
sv->spec = c_dfcolor; |
466 |
|
sv->cieY = .0; |
467 |
< |
memset(outVec, 0, 3*sizeof(double)); |
467 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
468 |
|
return SDEnone; |
469 |
|
} |
470 |
|
sv->cieY = cd->cTotal; |
471 |
|
/* compute sample direction */ |
472 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX, cd); |
472 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX, cd); |
473 |
|
if (ec) |
474 |
|
return ec; |
475 |
|
/* get BSDF color */ |
476 |
< |
n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
476 |
> |
n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
477 |
|
if (n <= 0) { |
478 |
|
strcpy(SDerrorDetail, "BSDF sample value error"); |
479 |
|
return SDEinternal; |
521 |
|
SDmultiSamp(outVec, 2, randX); |
522 |
|
SDsquare2disk(outVec, outVec[0], outVec[1]); |
523 |
|
outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1]; |
524 |
< |
if (outVec[2] > .0) /* a bit of paranoia */ |
524 |
> |
if (outVec[2] > 0) /* a bit of paranoia */ |
525 |
|
outVec[2] = sqrt(outVec[2]); |
526 |
|
if (!outFront) /* going out back? */ |
527 |
|
outVec[2] = -outVec[2]; |
529 |
|
|
530 |
|
/* Query projected solid angle coverage for non-diffuse BSDF direction */ |
531 |
|
SDError |
532 |
< |
SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd) |
532 |
> |
SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2, |
533 |
> |
int qflags, const SDData *sd) |
534 |
|
{ |
535 |
< |
SDSpectralDF *rdf; |
535 |
> |
SDSpectralDF *rdf, *tdf; |
536 |
|
SDError ec; |
537 |
|
int i; |
538 |
|
/* check arguments */ |
539 |
< |
if ((projSA == NULL) | (vec == NULL) | (sd == NULL)) |
539 |
> |
if ((projSA == NULL) | (v1 == NULL) | (sd == NULL)) |
540 |
|
return SDEargument; |
541 |
|
/* initialize extrema */ |
542 |
|
switch (qflags) { |
552 |
|
case 0: |
553 |
|
return SDEargument; |
554 |
|
} |
555 |
< |
if (vec[2] > .0) /* front surface query? */ |
555 |
> |
if (v1[2] > 0) /* front surface query? */ |
556 |
|
rdf = sd->rf; |
557 |
|
else |
558 |
|
rdf = sd->rb; |
559 |
+ |
tdf = sd->tf; |
560 |
+ |
if (v2 != NULL) /* bidirectional? */ |
561 |
+ |
if (v1[2] > 0 ^ v2[2] > 0) |
562 |
+ |
rdf = NULL; |
563 |
+ |
else |
564 |
+ |
tdf = NULL; |
565 |
|
ec = SDEdata; /* run through components */ |
566 |
|
for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) { |
567 |
< |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
568 |
< |
rdf->comp[i].dist); |
567 |
> |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
568 |
> |
qflags, &rdf->comp[i]); |
569 |
|
if (ec) |
570 |
|
return ec; |
571 |
|
} |
572 |
< |
for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) { |
573 |
< |
ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
574 |
< |
sd->tf->comp[i].dist); |
572 |
> |
for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) { |
573 |
> |
ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
574 |
> |
qflags, &tdf->comp[i]); |
575 |
|
if (ec) |
576 |
|
return ec; |
577 |
|
} |
595 |
|
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL)) |
596 |
|
return SDEargument; |
597 |
|
/* whose side are we on? */ |
598 |
< |
inFront = (inVec[2] > .0); |
599 |
< |
outFront = (outVec[2] > .0); |
598 |
> |
inFront = (inVec[2] > 0); |
599 |
> |
outFront = (outVec[2] > 0); |
600 |
|
/* start with diffuse portion */ |
601 |
|
if (inFront & outFront) { |
602 |
|
*sv = sd->rLambFront; |
613 |
|
i = (sdf != NULL) ? sdf->ncomp : 0; |
614 |
|
while (i-- > 0) { |
615 |
|
nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec, |
616 |
< |
sdf->comp[i].dist); |
616 |
> |
&sdf->comp[i]); |
617 |
|
while (nch-- > 0) { |
618 |
|
c_cmix(&sv->spec, sv->cieY, &sv->spec, |
619 |
|
coef[nch], &sdf->comp[i].cspec[nch]); |
637 |
|
if ((inVec == NULL) | (sd == NULL)) |
638 |
|
return .0; |
639 |
|
/* gather diffuse components */ |
640 |
< |
if (inVec[2] > .0) { |
640 |
> |
if (inVec[2] > 0) { |
641 |
|
hsum = sd->rLambFront.cieY; |
642 |
|
rdf = sd->rf; |
643 |
|
} else /* !inFront */ { |
668 |
|
|
669 |
|
/* Sample BSDF direction based on the given random variable */ |
670 |
|
SDError |
671 |
< |
SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec, |
624 |
< |
double randX, int sflags, const SDData *sd) |
671 |
> |
SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd) |
672 |
|
{ |
673 |
|
SDError ec; |
674 |
+ |
FVECT inVec; |
675 |
|
int inFront; |
676 |
|
SDSpectralDF *rdf; |
677 |
|
double rdiff; |
680 |
|
SDComponent *sdc; |
681 |
|
const SDCDst **cdarr = NULL; |
682 |
|
/* check arguments */ |
683 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) | |
684 |
< |
(randX < .0) | (randX >= 1.)) |
683 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) | |
684 |
> |
(randX < 0) | (randX >= 1.)) |
685 |
|
return SDEargument; |
686 |
|
/* whose side are we on? */ |
687 |
< |
inFront = (inVec[2] > .0); |
687 |
> |
VCOPY(inVec, ioVec); |
688 |
> |
inFront = (inVec[2] > 0); |
689 |
|
/* remember diffuse portions */ |
690 |
|
if (inFront) { |
691 |
|
*sv = sd->rLambFront; |
723 |
|
} |
724 |
|
sv->cieY += cdarr[i]->cTotal; |
725 |
|
} |
726 |
< |
if (sv->cieY <= 1e-7) { /* anything to sample? */ |
726 |
> |
if (sv->cieY <= 1e-6) { /* anything to sample? */ |
727 |
|
sv->cieY = .0; |
728 |
< |
memset(outVec, 0, 3*sizeof(double)); |
728 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
729 |
|
return SDEnone; |
730 |
|
} |
731 |
|
/* scale random variable */ |
732 |
|
randX *= sv->cieY; |
733 |
|
/* diffuse reflection? */ |
734 |
|
if (randX < rdiff) { |
735 |
< |
SDdiffuseSamp(outVec, inFront, randX/rdiff); |
735 |
> |
SDdiffuseSamp(ioVec, inFront, randX/rdiff); |
736 |
|
goto done; |
737 |
|
} |
738 |
|
randX -= rdiff; |
740 |
|
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) { |
741 |
|
if (randX < sd->tLamb.cieY) { |
742 |
|
sv->spec = sd->tLamb.spec; |
743 |
< |
SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY); |
743 |
> |
SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY); |
744 |
|
goto done; |
745 |
|
} |
746 |
|
randX -= sd->tLamb.cieY; |
752 |
|
return SDEinternal; |
753 |
|
/* compute sample direction */ |
754 |
|
sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr]; |
755 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]); |
755 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); |
756 |
|
if (ec) |
757 |
|
return ec; |
758 |
|
/* compute color */ |
759 |
< |
j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
759 |
> |
j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
760 |
|
if (j <= 0) { |
761 |
|
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
762 |
|
sd->name); |
783 |
|
if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL)) |
784 |
|
return SDEargument; |
785 |
|
VCOPY(vMtx[2], sNrm); |
786 |
< |
if (normalize(vMtx[2]) == .0) |
786 |
> |
if (normalize(vMtx[2]) == 0) |
787 |
|
return SDEargument; |
788 |
|
fcross(vMtx[0], uVec, vMtx[2]); |
789 |
< |
if (normalize(vMtx[0]) == .0) |
789 |
> |
if (normalize(vMtx[0]) == 0) |
790 |
|
return SDEargument; |
791 |
|
fcross(vMtx[1], vMtx[2], vMtx[0]); |
792 |
|
return SDEnone; |
806 |
|
mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1]; |
807 |
|
mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2]; |
808 |
|
d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2]; |
809 |
< |
if (d == .0) { |
809 |
> |
if (d == 0) { |
810 |
|
strcpy(SDerrorDetail, "Zero determinant in matrix inversion"); |
811 |
|
return SDEargument; |
812 |
|
} |
833 |
|
if (vMtx == NULL) { /* assume they just want to normalize */ |
834 |
|
if (resVec != inpVec) |
835 |
|
VCOPY(resVec, inpVec); |
836 |
< |
return (normalize(resVec) > .0) ? SDEnone : SDEargument; |
836 |
> |
return (normalize(resVec) > 0) ? SDEnone : SDEargument; |
837 |
|
} |
838 |
|
vTmp[0] = DOT(vMtx[0], inpVec); |
839 |
|
vTmp[1] = DOT(vMtx[1], inpVec); |
840 |
|
vTmp[2] = DOT(vMtx[2], inpVec); |
841 |
< |
if (normalize(vTmp) == .0) |
841 |
> |
if (normalize(vTmp) == 0) |
842 |
|
return SDEargument; |
843 |
|
VCOPY(resVec, vTmp); |
844 |
|
return SDEnone; |
853 |
|
|
854 |
|
#include "standard.h" |
855 |
|
#include "paths.h" |
807 |
– |
#include <ctype.h> |
856 |
|
|
857 |
|
#define MAXLATS 46 /* maximum number of latitudes */ |
858 |
|
|
909 |
|
static int |
910 |
|
fequal(double a, double b) |
911 |
|
{ |
912 |
< |
if (b != .0) |
912 |
> |
if (b != 0) |
913 |
|
a = a/b - 1.; |
914 |
|
return((a <= 1e-6) & (a >= -1e-6)); |
915 |
|
} |
972 |
|
{ |
973 |
|
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
974 |
|
int li, ndx; |
975 |
< |
double pol, azi, d; |
975 |
> |
double pol, azi; |
976 |
|
|
977 |
|
if ((v[2] < -1.0) | (v[2] > 1.0)) |
978 |
|
return(-1); |
1212 |
|
) |
1213 |
|
{ |
1214 |
|
double *omega_iarr, *omega_oarr; |
1215 |
< |
double dom, contrib, hemi_total, full_total; |
1215 |
> |
double dom, hemi_total, full_total; |
1216 |
|
int nneg; |
1217 |
|
FVECT v; |
1218 |
|
int i, o; |
1227 |
|
hemi_total = .0; |
1228 |
|
for (i = dp->ninc; i--; ) { |
1229 |
|
dom = getBSDF_incohm(dp,i); |
1230 |
< |
if (dom <= .0) { |
1230 |
> |
if (dom <= 0) { |
1231 |
|
error(WARNING, "zero/negative incoming solid angle"); |
1232 |
|
continue; |
1233 |
|
} |
1250 |
|
hemi_total = .0; |
1251 |
|
for (o = dp->nout; o--; ) { |
1252 |
|
dom = getBSDF_outohm(dp,o); |
1253 |
< |
if (dom <= .0) { |
1253 |
> |
if (dom <= 0) { |
1254 |
|
error(WARNING, "zero/negative outgoing solid angle"); |
1255 |
|
continue; |
1256 |
|
} |
1274 |
|
hemi_total = .0; |
1275 |
|
for (o = dp->nout; o--; ) { |
1276 |
|
double f = BSDF_value(dp,i,o); |
1277 |
< |
if (f >= .0) |
1277 |
> |
if (f >= 0) |
1278 |
|
hemi_total += f*omega_oarr[o]; |
1279 |
|
else { |
1280 |
|
nneg += (f < -FTINY); |
1362 |
|
error(WARNING, errmsg); |
1363 |
|
ezxml_free(fl); |
1364 |
|
return(NULL); |
1365 |
< |
} |
1366 |
< |
load_angle_basis(ezxml_child(ezxml_child(wtl, |
1367 |
< |
"DataDefinition"), "AngleBasis")); |
1365 |
> |
} |
1366 |
> |
for (wld = ezxml_child(ezxml_child(wtl, |
1367 |
> |
"DataDefinition"), "AngleBasis"); |
1368 |
> |
wld != NULL; wld = wld->next) |
1369 |
> |
load_angle_basis(wld); |
1370 |
|
dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data)); |
1371 |
|
load_geometry(dp, ezxml_child(wtl, "Material")); |
1372 |
|
for (wld = ezxml_child(wtl, "WavelengthData"); |