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" |
93 |
|
if (wdb == NULL) /* no geometry section? */ |
94 |
|
return SDEnone; |
95 |
|
sd->dim[0] = sd->dim[1] = sd->dim[2] = .0; |
96 |
+ |
SDerrorDetail[0] = '\0'; |
97 |
|
if ((geom = ezxml_child(wdb, "Width")) != NULL) |
98 |
|
sd->dim[0] = atof(ezxml_txt(geom)) * |
99 |
|
to_meters(ezxml_attr(geom, "unit")); |
104 |
|
sd->dim[2] = atof(ezxml_txt(geom)) * |
105 |
|
to_meters(ezxml_attr(geom, "unit")); |
106 |
|
if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) { |
107 |
< |
sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name); |
107 |
> |
if (!SDerrorDetail[0]) |
108 |
> |
sprintf(SDerrorDetail, "Negative dimension in \"%s\"", |
109 |
> |
sd->name); |
110 |
|
return SDEdata; |
111 |
|
} |
112 |
|
if ((geom = ezxml_child(wdb, "Geometry")) == NULL || |
113 |
|
(mgfstr = ezxml_txt(geom)) == NULL) |
114 |
|
return SDEnone; |
115 |
+ |
while (isspace(*mgfstr)) |
116 |
+ |
++mgfstr; |
117 |
+ |
if (!*mgfstr) |
118 |
+ |
return SDEnone; |
119 |
|
if ((fmt = ezxml_attr(geom, "format")) != NULL && |
120 |
|
strcasecmp(fmt, "MGF")) { |
121 |
|
sprintf(SDerrorDetail, |
124 |
|
return SDEsupport; |
125 |
|
} |
126 |
|
cfact = to_meters(ezxml_attr(geom, "unit")); |
127 |
+ |
if (cfact <= 0) |
128 |
+ |
return SDEformat; |
129 |
|
sd->mgf = (char *)malloc(strlen(mgfstr)+32); |
130 |
|
if (sd->mgf == NULL) { |
131 |
|
strcpy(SDerrorDetail, "Out of memory in SDloadGeometry"); |
169 |
|
} |
170 |
|
wtl = ezxml_child(ezxml_child(fl, "Optical"), "Layer"); |
171 |
|
if (wtl == NULL) { |
172 |
< |
sprintf(SDerrorDetail, "BSDF \"%s\": no optical layer'", |
172 |
> |
sprintf(SDerrorDetail, "BSDF \"%s\": no optical layers'", |
173 |
|
sd->name); |
174 |
|
ezxml_free(fl); |
175 |
|
return SDEformat; |
176 |
|
} |
177 |
|
/* load geometry if present */ |
178 |
|
lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material")); |
179 |
< |
if (lastErr) |
179 |
> |
if (lastErr) { |
180 |
> |
ezxml_free(fl); |
181 |
|
return lastErr; |
182 |
+ |
} |
183 |
|
/* try loading variable resolution data */ |
184 |
|
lastErr = SDloadTre(sd, wtl); |
185 |
|
/* check our result */ |
186 |
< |
switch (lastErr) { |
173 |
< |
case SDEformat: |
174 |
< |
case SDEdata: |
175 |
< |
case SDEsupport: /* possibly we just tried the wrong format */ |
186 |
> |
if (lastErr == SDEsupport) /* try matrix BSDF if not tree data */ |
187 |
|
lastErr = SDloadMtx(sd, wtl); |
188 |
< |
break; |
178 |
< |
default: /* variable res. OK else serious error */ |
179 |
< |
break; |
180 |
< |
} |
188 |
> |
|
189 |
|
/* done with XML file */ |
190 |
|
ezxml_free(fl); |
191 |
|
|
231 |
|
return df; |
232 |
|
} |
233 |
|
|
234 |
+ |
/* Add component(s) to spectral distribution function */ |
235 |
+ |
SDSpectralDF * |
236 |
+ |
SDaddComponent(SDSpectralDF *odf, int nadd) |
237 |
+ |
{ |
238 |
+ |
SDSpectralDF *df; |
239 |
+ |
|
240 |
+ |
if (odf == NULL) |
241 |
+ |
return SDnewSpectralDF(nadd); |
242 |
+ |
if (nadd <= 0) |
243 |
+ |
return odf; |
244 |
+ |
df = (SDSpectralDF *)realloc(odf, sizeof(SDSpectralDF) + |
245 |
+ |
(odf->ncomp+nadd-1)*sizeof(SDComponent)); |
246 |
+ |
if (df == NULL) { |
247 |
+ |
sprintf(SDerrorDetail, |
248 |
+ |
"Cannot add %d component(s) to spectral DF", nadd); |
249 |
+ |
SDfreeSpectralDF(odf); |
250 |
+ |
return NULL; |
251 |
+ |
} |
252 |
+ |
memset(df->comp+df->ncomp, 0, nadd*sizeof(SDComponent)); |
253 |
+ |
df->ncomp += nadd; |
254 |
+ |
return df; |
255 |
+ |
} |
256 |
+ |
|
257 |
|
/* Free cached cumulative distributions for BSDF component */ |
258 |
|
void |
259 |
|
SDfreeCumulativeCache(SDSpectralDF *df) |
280 |
|
return; |
281 |
|
SDfreeCumulativeCache(df); |
282 |
|
for (n = df->ncomp; n-- > 0; ) |
283 |
< |
(*df->comp[n].func->freeSC)(df->comp[n].dist); |
283 |
> |
if (df->comp[n].dist != NULL) |
284 |
> |
(*df->comp[n].func->freeSC)(df->comp[n].dist); |
285 |
|
free(df); |
286 |
|
} |
287 |
|
|
437 |
|
|
438 |
|
/* Sample an individual BSDF component */ |
439 |
|
SDError |
440 |
< |
SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec, |
409 |
< |
double randX, SDComponent *sdc) |
440 |
> |
SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc) |
441 |
|
{ |
442 |
|
float coef[SDmaxCh]; |
443 |
|
SDError ec; |
444 |
+ |
FVECT inVec; |
445 |
|
const SDCDst *cd; |
446 |
|
double d; |
447 |
|
int n; |
448 |
|
/* check arguments */ |
449 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)) |
449 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL)) |
450 |
|
return SDEargument; |
451 |
|
/* get cumulative distribution */ |
452 |
+ |
VCOPY(inVec, ioVec); |
453 |
|
cd = (*sdc->func->getCDist)(inVec, sdc); |
454 |
|
if (cd == NULL) |
455 |
|
return SDEmemory; |
456 |
< |
if (cd->cTotal <= 1e-7) { /* anything to sample? */ |
456 |
> |
if (cd->cTotal <= 1e-6) { /* anything to sample? */ |
457 |
|
sv->spec = c_dfcolor; |
458 |
|
sv->cieY = .0; |
459 |
< |
memset(outVec, 0, 3*sizeof(double)); |
459 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
460 |
|
return SDEnone; |
461 |
|
} |
462 |
|
sv->cieY = cd->cTotal; |
463 |
|
/* compute sample direction */ |
464 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX, cd); |
464 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX, cd); |
465 |
|
if (ec) |
466 |
|
return ec; |
467 |
|
/* get BSDF color */ |
468 |
< |
n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
468 |
> |
n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
469 |
|
if (n <= 0) { |
470 |
|
strcpy(SDerrorDetail, "BSDF sample value error"); |
471 |
|
return SDEinternal; |
528 |
|
SDError ec; |
529 |
|
int i; |
530 |
|
/* check arguments */ |
531 |
< |
if ((projSA == NULL) | (v1 == NULL)) |
531 |
> |
if ((projSA == NULL) | (v1 == NULL) | (sd == NULL)) |
532 |
|
return SDEargument; |
533 |
|
/* initialize extrema */ |
534 |
|
switch (qflags) { |
548 |
|
rdf = sd->rf; |
549 |
|
else |
550 |
|
rdf = sd->rb; |
551 |
< |
tdf = NULL; /* transmitted component? */ |
552 |
< |
if (v2 != NULL && v1[2] > 0 ^ v2[2] > 0) { |
553 |
< |
rdf = NULL; |
554 |
< |
tdf = sd->tf; |
555 |
< |
} |
551 |
> |
tdf = sd->tf; |
552 |
> |
if (v2 != NULL) /* bidirectional? */ |
553 |
> |
if (v1[2] > 0 ^ v2[2] > 0) |
554 |
> |
rdf = NULL; |
555 |
> |
else |
556 |
> |
tdf = NULL; |
557 |
|
ec = SDEdata; /* run through components */ |
558 |
|
for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) { |
559 |
|
ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
560 |
< |
qflags, rdf->comp[i].dist); |
560 |
> |
qflags, &rdf->comp[i]); |
561 |
|
if (ec) |
562 |
|
return ec; |
563 |
|
} |
564 |
|
for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) { |
565 |
|
ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
566 |
< |
qflags, tdf->comp[i].dist); |
566 |
> |
qflags, &tdf->comp[i]); |
567 |
|
if (ec) |
568 |
|
return ec; |
569 |
|
} |
605 |
|
i = (sdf != NULL) ? sdf->ncomp : 0; |
606 |
|
while (i-- > 0) { |
607 |
|
nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec, |
608 |
< |
sdf->comp[i].dist); |
608 |
> |
&sdf->comp[i]); |
609 |
|
while (nch-- > 0) { |
610 |
|
c_cmix(&sv->spec, sv->cieY, &sv->spec, |
611 |
|
coef[nch], &sdf->comp[i].cspec[nch]); |
660 |
|
|
661 |
|
/* Sample BSDF direction based on the given random variable */ |
662 |
|
SDError |
663 |
< |
SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec, |
630 |
< |
double randX, int sflags, const SDData *sd) |
663 |
> |
SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd) |
664 |
|
{ |
665 |
|
SDError ec; |
666 |
+ |
FVECT inVec; |
667 |
|
int inFront; |
668 |
|
SDSpectralDF *rdf; |
669 |
|
double rdiff; |
672 |
|
SDComponent *sdc; |
673 |
|
const SDCDst **cdarr = NULL; |
674 |
|
/* check arguments */ |
675 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) | |
675 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) | |
676 |
|
(randX < 0) | (randX >= 1.)) |
677 |
|
return SDEargument; |
678 |
|
/* whose side are we on? */ |
679 |
+ |
VCOPY(inVec, ioVec); |
680 |
|
inFront = (inVec[2] > 0); |
681 |
|
/* remember diffuse portions */ |
682 |
|
if (inFront) { |
715 |
|
} |
716 |
|
sv->cieY += cdarr[i]->cTotal; |
717 |
|
} |
718 |
< |
if (sv->cieY <= 1e-7) { /* anything to sample? */ |
718 |
> |
if (sv->cieY <= 1e-6) { /* anything to sample? */ |
719 |
|
sv->cieY = .0; |
720 |
< |
memset(outVec, 0, 3*sizeof(double)); |
720 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
721 |
|
return SDEnone; |
722 |
|
} |
723 |
|
/* scale random variable */ |
724 |
|
randX *= sv->cieY; |
725 |
|
/* diffuse reflection? */ |
726 |
|
if (randX < rdiff) { |
727 |
< |
SDdiffuseSamp(outVec, inFront, randX/rdiff); |
727 |
> |
SDdiffuseSamp(ioVec, inFront, randX/rdiff); |
728 |
|
goto done; |
729 |
|
} |
730 |
|
randX -= rdiff; |
732 |
|
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) { |
733 |
|
if (randX < sd->tLamb.cieY) { |
734 |
|
sv->spec = sd->tLamb.spec; |
735 |
< |
SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY); |
735 |
> |
SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY); |
736 |
|
goto done; |
737 |
|
} |
738 |
|
randX -= sd->tLamb.cieY; |
744 |
|
return SDEinternal; |
745 |
|
/* compute sample direction */ |
746 |
|
sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr]; |
747 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]); |
747 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); |
748 |
|
if (ec) |
749 |
|
return ec; |
750 |
|
/* compute color */ |
751 |
< |
j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
751 |
> |
j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
752 |
|
if (j <= 0) { |
753 |
|
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
754 |
|
sd->name); |
845 |
|
|
846 |
|
#include "standard.h" |
847 |
|
#include "paths.h" |
813 |
– |
#include <ctype.h> |
848 |
|
|
849 |
|
#define MAXLATS 46 /* maximum number of latitudes */ |
850 |
|
|
964 |
|
{ |
965 |
|
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
966 |
|
int li, ndx; |
967 |
< |
double pol, azi, d; |
967 |
> |
double pol, azi; |
968 |
|
|
969 |
|
if ((v[2] < -1.0) | (v[2] > 1.0)) |
970 |
|
return(-1); |
1204 |
|
) |
1205 |
|
{ |
1206 |
|
double *omega_iarr, *omega_oarr; |
1207 |
< |
double dom, contrib, hemi_total, full_total; |
1207 |
> |
double dom, hemi_total, full_total; |
1208 |
|
int nneg; |
1209 |
|
FVECT v; |
1210 |
|
int i, o; |
1354 |
|
error(WARNING, errmsg); |
1355 |
|
ezxml_free(fl); |
1356 |
|
return(NULL); |
1357 |
< |
} |
1358 |
< |
load_angle_basis(ezxml_child(ezxml_child(wtl, |
1359 |
< |
"DataDefinition"), "AngleBasis")); |
1357 |
> |
} |
1358 |
> |
for (wld = ezxml_child(ezxml_child(wtl, |
1359 |
> |
"DataDefinition"), "AngleBasis"); |
1360 |
> |
wld != NULL; wld = wld->next) |
1361 |
> |
load_angle_basis(wld); |
1362 |
|
dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data)); |
1363 |
|
load_geometry(dp, ezxml_child(wtl, "Material")); |
1364 |
|
for (wld = ezxml_child(wtl, "WavelengthData"); |