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" |
48 |
|
SDError |
49 |
|
SDreportEnglish(SDError ec, FILE *fp) |
50 |
|
{ |
48 |
– |
if (fp == NULL) |
49 |
– |
return ec; |
51 |
|
if (!ec) |
52 |
|
return SDEnone; |
53 |
+ |
if ((ec < SDEnone) | (ec > SDEunknown)) { |
54 |
+ |
SDerrorDetail[0] = '\0'; |
55 |
+ |
ec = SDEunknown; |
56 |
+ |
} |
57 |
+ |
if (fp == NULL) |
58 |
+ |
return ec; |
59 |
|
fputs(SDerrorEnglish[ec], fp); |
60 |
|
if (SDerrorDetail[0]) { |
61 |
|
fputs(": ", fp); |
102 |
|
if ((geom = ezxml_child(wdb, "Thickness")) != NULL) |
103 |
|
sd->dim[2] = atof(ezxml_txt(geom)) * |
104 |
|
to_meters(ezxml_attr(geom, "unit")); |
105 |
< |
if ((sd->dim[0] < .0) | (sd->dim[1] < .0) | (sd->dim[2] < .0)) { |
105 |
> |
if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) { |
106 |
|
sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name); |
107 |
|
return SDEdata; |
108 |
|
} |
109 |
|
if ((geom = ezxml_child(wdb, "Geometry")) == NULL || |
110 |
|
(mgfstr = ezxml_txt(geom)) == NULL) |
111 |
|
return SDEnone; |
112 |
+ |
while (isspace(*mgfstr)) |
113 |
+ |
++mgfstr; |
114 |
+ |
if (!*mgfstr) |
115 |
+ |
return SDEnone; |
116 |
|
if ((fmt = ezxml_attr(geom, "format")) != NULL && |
117 |
|
strcasecmp(fmt, "MGF")) { |
118 |
|
sprintf(SDerrorDetail, |
171 |
|
} |
172 |
|
/* load geometry if present */ |
173 |
|
lastErr = SDloadGeometry(sd, ezxml_child(wtl, "Material")); |
174 |
< |
if (lastErr) |
174 |
> |
if (lastErr) { |
175 |
> |
ezxml_free(fl); |
176 |
|
return lastErr; |
177 |
+ |
} |
178 |
|
/* try loading variable resolution data */ |
179 |
|
lastErr = SDloadTre(sd, wtl); |
180 |
|
/* check our result */ |
181 |
< |
switch (lastErr) { |
169 |
< |
case SDEformat: |
170 |
< |
case SDEdata: |
171 |
< |
case SDEsupport: /* possibly we just tried the wrong format */ |
181 |
> |
if (lastErr == SDEsupport) /* try matrix BSDF if not tree data */ |
182 |
|
lastErr = SDloadMtx(sd, wtl); |
183 |
< |
break; |
174 |
< |
default: /* variable res. OK else serious error */ |
175 |
< |
break; |
176 |
< |
} |
183 |
> |
|
184 |
|
/* done with XML file */ |
185 |
|
ezxml_free(fl); |
186 |
|
|
252 |
|
return; |
253 |
|
SDfreeCumulativeCache(df); |
254 |
|
for (n = df->ncomp; n-- > 0; ) |
255 |
< |
(*df->comp[n].func->freeSC)(df->comp[n].dist); |
255 |
> |
if (df->comp[n].dist != NULL) |
256 |
> |
(*df->comp[n].func->freeSC)(df->comp[n].dist); |
257 |
|
free(df); |
258 |
|
} |
259 |
|
|
342 |
|
sdl->next = SDcacheList; |
343 |
|
SDcacheList = sdl; |
344 |
|
|
345 |
< |
sdl->refcnt++; |
345 |
> |
sdl->refcnt = 1; |
346 |
|
return &sdl->bsdf; |
347 |
|
} |
348 |
|
|
386 |
|
for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next) |
387 |
|
if (&sdl->bsdf == sd) |
388 |
|
break; |
389 |
< |
if (sdl == NULL || --sdl->refcnt) |
389 |
> |
if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0))) |
390 |
|
return; /* missing or still in use */ |
391 |
|
/* keep unreferenced data? */ |
392 |
|
if (SDisLoaded(sd) && SDretainSet) { |
409 |
|
|
410 |
|
/* Sample an individual BSDF component */ |
411 |
|
SDError |
412 |
< |
SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec, |
405 |
< |
double randX, SDComponent *sdc) |
412 |
> |
SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc) |
413 |
|
{ |
414 |
|
float coef[SDmaxCh]; |
415 |
|
SDError ec; |
416 |
+ |
FVECT inVec; |
417 |
|
const SDCDst *cd; |
418 |
|
double d; |
419 |
|
int n; |
420 |
|
/* check arguments */ |
421 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)) |
421 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL)) |
422 |
|
return SDEargument; |
423 |
|
/* get cumulative distribution */ |
424 |
+ |
VCOPY(inVec, ioVec); |
425 |
|
cd = (*sdc->func->getCDist)(inVec, sdc); |
426 |
|
if (cd == NULL) |
427 |
|
return SDEmemory; |
428 |
< |
if (cd->cTotal <= 1e-7) { /* anything to sample? */ |
428 |
> |
if (cd->cTotal <= 1e-6) { /* anything to sample? */ |
429 |
|
sv->spec = c_dfcolor; |
430 |
|
sv->cieY = .0; |
431 |
< |
memset(outVec, 0, 3*sizeof(double)); |
431 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
432 |
|
return SDEnone; |
433 |
|
} |
434 |
|
sv->cieY = cd->cTotal; |
435 |
|
/* compute sample direction */ |
436 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX, cd); |
436 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX, cd); |
437 |
|
if (ec) |
438 |
|
return ec; |
439 |
|
/* get BSDF color */ |
440 |
< |
n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
440 |
> |
n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
441 |
|
if (n <= 0) { |
442 |
|
strcpy(SDerrorDetail, "BSDF sample value error"); |
443 |
|
return SDEinternal; |
485 |
|
SDmultiSamp(outVec, 2, randX); |
486 |
|
SDsquare2disk(outVec, outVec[0], outVec[1]); |
487 |
|
outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1]; |
488 |
< |
if (outVec[2] > .0) /* a bit of paranoia */ |
488 |
> |
if (outVec[2] > 0) /* a bit of paranoia */ |
489 |
|
outVec[2] = sqrt(outVec[2]); |
490 |
|
if (!outFront) /* going out back? */ |
491 |
|
outVec[2] = -outVec[2]; |
493 |
|
|
494 |
|
/* Query projected solid angle coverage for non-diffuse BSDF direction */ |
495 |
|
SDError |
496 |
< |
SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd) |
496 |
> |
SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2, |
497 |
> |
int qflags, const SDData *sd) |
498 |
|
{ |
499 |
< |
SDSpectralDF *rdf; |
499 |
> |
SDSpectralDF *rdf, *tdf; |
500 |
|
SDError ec; |
501 |
|
int i; |
502 |
|
/* check arguments */ |
503 |
< |
if ((projSA == NULL) | (vec == NULL) | (sd == NULL)) |
503 |
> |
if ((projSA == NULL) | (v1 == NULL) | (sd == NULL)) |
504 |
|
return SDEargument; |
505 |
|
/* initialize extrema */ |
506 |
|
switch (qflags) { |
516 |
|
case 0: |
517 |
|
return SDEargument; |
518 |
|
} |
519 |
< |
if (vec[2] > .0) /* front surface query? */ |
519 |
> |
if (v1[2] > 0) /* front surface query? */ |
520 |
|
rdf = sd->rf; |
521 |
|
else |
522 |
|
rdf = sd->rb; |
523 |
+ |
tdf = sd->tf; |
524 |
+ |
if (v2 != NULL) /* bidirectional? */ |
525 |
+ |
if (v1[2] > 0 ^ v2[2] > 0) |
526 |
+ |
rdf = NULL; |
527 |
+ |
else |
528 |
+ |
tdf = NULL; |
529 |
|
ec = SDEdata; /* run through components */ |
530 |
|
for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) { |
531 |
< |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
532 |
< |
rdf->comp[i].dist); |
531 |
> |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
532 |
> |
qflags, &rdf->comp[i]); |
533 |
|
if (ec) |
534 |
|
return ec; |
535 |
|
} |
536 |
< |
for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) { |
537 |
< |
ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
538 |
< |
sd->tf->comp[i].dist); |
536 |
> |
for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) { |
537 |
> |
ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
538 |
> |
qflags, &tdf->comp[i]); |
539 |
|
if (ec) |
540 |
|
return ec; |
541 |
|
} |
559 |
|
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL)) |
560 |
|
return SDEargument; |
561 |
|
/* whose side are we on? */ |
562 |
< |
inFront = (inVec[2] > .0); |
563 |
< |
outFront = (outVec[2] > .0); |
562 |
> |
inFront = (inVec[2] > 0); |
563 |
> |
outFront = (outVec[2] > 0); |
564 |
|
/* start with diffuse portion */ |
565 |
|
if (inFront & outFront) { |
566 |
|
*sv = sd->rLambFront; |
577 |
|
i = (sdf != NULL) ? sdf->ncomp : 0; |
578 |
|
while (i-- > 0) { |
579 |
|
nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec, |
580 |
< |
sdf->comp[i].dist); |
580 |
> |
&sdf->comp[i]); |
581 |
|
while (nch-- > 0) { |
582 |
|
c_cmix(&sv->spec, sv->cieY, &sv->spec, |
583 |
|
coef[nch], &sdf->comp[i].cspec[nch]); |
601 |
|
if ((inVec == NULL) | (sd == NULL)) |
602 |
|
return .0; |
603 |
|
/* gather diffuse components */ |
604 |
< |
if (inVec[2] > .0) { |
604 |
> |
if (inVec[2] > 0) { |
605 |
|
hsum = sd->rLambFront.cieY; |
606 |
|
rdf = sd->rf; |
607 |
|
} else /* !inFront */ { |
632 |
|
|
633 |
|
/* Sample BSDF direction based on the given random variable */ |
634 |
|
SDError |
635 |
< |
SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec, |
620 |
< |
double randX, int sflags, const SDData *sd) |
635 |
> |
SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd) |
636 |
|
{ |
637 |
|
SDError ec; |
638 |
+ |
FVECT inVec; |
639 |
|
int inFront; |
640 |
|
SDSpectralDF *rdf; |
641 |
|
double rdiff; |
644 |
|
SDComponent *sdc; |
645 |
|
const SDCDst **cdarr = NULL; |
646 |
|
/* check arguments */ |
647 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) | |
648 |
< |
(randX < .0) | (randX >= 1.)) |
647 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) | |
648 |
> |
(randX < 0) | (randX >= 1.)) |
649 |
|
return SDEargument; |
650 |
|
/* whose side are we on? */ |
651 |
< |
inFront = (inVec[2] > .0); |
651 |
> |
VCOPY(inVec, ioVec); |
652 |
> |
inFront = (inVec[2] > 0); |
653 |
|
/* remember diffuse portions */ |
654 |
|
if (inFront) { |
655 |
|
*sv = sd->rLambFront; |
687 |
|
} |
688 |
|
sv->cieY += cdarr[i]->cTotal; |
689 |
|
} |
690 |
< |
if (sv->cieY <= 1e-7) { /* anything to sample? */ |
690 |
> |
if (sv->cieY <= 1e-6) { /* anything to sample? */ |
691 |
|
sv->cieY = .0; |
692 |
< |
memset(outVec, 0, 3*sizeof(double)); |
692 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
693 |
|
return SDEnone; |
694 |
|
} |
695 |
|
/* scale random variable */ |
696 |
|
randX *= sv->cieY; |
697 |
|
/* diffuse reflection? */ |
698 |
|
if (randX < rdiff) { |
699 |
< |
SDdiffuseSamp(outVec, inFront, randX/rdiff); |
699 |
> |
SDdiffuseSamp(ioVec, inFront, randX/rdiff); |
700 |
|
goto done; |
701 |
|
} |
702 |
|
randX -= rdiff; |
704 |
|
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) { |
705 |
|
if (randX < sd->tLamb.cieY) { |
706 |
|
sv->spec = sd->tLamb.spec; |
707 |
< |
SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY); |
707 |
> |
SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY); |
708 |
|
goto done; |
709 |
|
} |
710 |
|
randX -= sd->tLamb.cieY; |
716 |
|
return SDEinternal; |
717 |
|
/* compute sample direction */ |
718 |
|
sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr]; |
719 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]); |
719 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); |
720 |
|
if (ec) |
721 |
|
return ec; |
722 |
|
/* compute color */ |
723 |
< |
j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
723 |
> |
j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
724 |
|
if (j <= 0) { |
725 |
|
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
726 |
|
sd->name); |
747 |
|
if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL)) |
748 |
|
return SDEargument; |
749 |
|
VCOPY(vMtx[2], sNrm); |
750 |
< |
if (normalize(vMtx[2]) == .0) |
750 |
> |
if (normalize(vMtx[2]) == 0) |
751 |
|
return SDEargument; |
752 |
|
fcross(vMtx[0], uVec, vMtx[2]); |
753 |
< |
if (normalize(vMtx[0]) == .0) |
753 |
> |
if (normalize(vMtx[0]) == 0) |
754 |
|
return SDEargument; |
755 |
|
fcross(vMtx[1], vMtx[2], vMtx[0]); |
756 |
|
return SDEnone; |
770 |
|
mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1]; |
771 |
|
mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2]; |
772 |
|
d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2]; |
773 |
< |
if (d == .0) { |
773 |
> |
if (d == 0) { |
774 |
|
strcpy(SDerrorDetail, "Zero determinant in matrix inversion"); |
775 |
|
return SDEargument; |
776 |
|
} |
797 |
|
if (vMtx == NULL) { /* assume they just want to normalize */ |
798 |
|
if (resVec != inpVec) |
799 |
|
VCOPY(resVec, inpVec); |
800 |
< |
return (normalize(resVec) > .0) ? SDEnone : SDEargument; |
800 |
> |
return (normalize(resVec) > 0) ? SDEnone : SDEargument; |
801 |
|
} |
802 |
|
vTmp[0] = DOT(vMtx[0], inpVec); |
803 |
|
vTmp[1] = DOT(vMtx[1], inpVec); |
804 |
|
vTmp[2] = DOT(vMtx[2], inpVec); |
805 |
< |
if (normalize(vTmp) == .0) |
805 |
> |
if (normalize(vTmp) == 0) |
806 |
|
return SDEargument; |
807 |
|
VCOPY(resVec, vTmp); |
808 |
|
return SDEnone; |
817 |
|
|
818 |
|
#include "standard.h" |
819 |
|
#include "paths.h" |
803 |
– |
#include <ctype.h> |
820 |
|
|
821 |
|
#define MAXLATS 46 /* maximum number of latitudes */ |
822 |
|
|
873 |
|
static int |
874 |
|
fequal(double a, double b) |
875 |
|
{ |
876 |
< |
if (b != .0) |
876 |
> |
if (b != 0) |
877 |
|
a = a/b - 1.; |
878 |
|
return((a <= 1e-6) & (a >= -1e-6)); |
879 |
|
} |
936 |
|
{ |
937 |
|
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
938 |
|
int li, ndx; |
939 |
< |
double pol, azi, d; |
939 |
> |
double pol, azi; |
940 |
|
|
941 |
|
if ((v[2] < -1.0) | (v[2] > 1.0)) |
942 |
|
return(-1); |
1176 |
|
) |
1177 |
|
{ |
1178 |
|
double *omega_iarr, *omega_oarr; |
1179 |
< |
double dom, contrib, hemi_total, full_total; |
1179 |
> |
double dom, hemi_total, full_total; |
1180 |
|
int nneg; |
1181 |
|
FVECT v; |
1182 |
|
int i, o; |
1191 |
|
hemi_total = .0; |
1192 |
|
for (i = dp->ninc; i--; ) { |
1193 |
|
dom = getBSDF_incohm(dp,i); |
1194 |
< |
if (dom <= .0) { |
1194 |
> |
if (dom <= 0) { |
1195 |
|
error(WARNING, "zero/negative incoming solid angle"); |
1196 |
|
continue; |
1197 |
|
} |
1214 |
|
hemi_total = .0; |
1215 |
|
for (o = dp->nout; o--; ) { |
1216 |
|
dom = getBSDF_outohm(dp,o); |
1217 |
< |
if (dom <= .0) { |
1217 |
> |
if (dom <= 0) { |
1218 |
|
error(WARNING, "zero/negative outgoing solid angle"); |
1219 |
|
continue; |
1220 |
|
} |
1238 |
|
hemi_total = .0; |
1239 |
|
for (o = dp->nout; o--; ) { |
1240 |
|
double f = BSDF_value(dp,i,o); |
1241 |
< |
if (f >= .0) |
1241 |
> |
if (f >= 0) |
1242 |
|
hemi_total += f*omega_oarr[o]; |
1243 |
|
else { |
1244 |
|
nneg += (f < -FTINY); |
1326 |
|
error(WARNING, errmsg); |
1327 |
|
ezxml_free(fl); |
1328 |
|
return(NULL); |
1329 |
< |
} |
1330 |
< |
load_angle_basis(ezxml_child(ezxml_child(wtl, |
1331 |
< |
"DataDefinition"), "AngleBasis")); |
1329 |
> |
} |
1330 |
> |
for (wld = ezxml_child(ezxml_child(wtl, |
1331 |
> |
"DataDefinition"), "AngleBasis"); |
1332 |
> |
wld != NULL; wld = wld->next) |
1333 |
> |
load_angle_basis(wld); |
1334 |
|
dp = (struct BSDF_data *)calloc(1, sizeof(struct BSDF_data)); |
1335 |
|
load_geometry(dp, ezxml_child(wtl, "Material")); |
1336 |
|
for (wld = ezxml_child(wtl, "WavelengthData"); |