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 "ezxml.h" |
19 |
|
#include "hilbert.h" |
47 |
|
SDError |
48 |
|
SDreportEnglish(SDError ec, FILE *fp) |
49 |
|
{ |
48 |
– |
if (fp == NULL) |
49 |
– |
return ec; |
50 |
|
if (!ec) |
51 |
|
return SDEnone; |
52 |
+ |
if ((ec < SDEnone) | (ec > SDEunknown)) { |
53 |
+ |
SDerrorDetail[0] = '\0'; |
54 |
+ |
ec = SDEunknown; |
55 |
+ |
} |
56 |
+ |
if (fp == NULL) |
57 |
+ |
return ec; |
58 |
|
fputs(SDerrorEnglish[ec], fp); |
59 |
|
if (SDerrorDetail[0]) { |
60 |
|
fputs(": ", fp); |
91 |
|
|
92 |
|
if (wdb == NULL) /* no geometry section? */ |
93 |
|
return SDEnone; |
88 |
– |
sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name); |
94 |
|
sd->dim[0] = sd->dim[1] = sd->dim[2] = .0; |
95 |
|
if ((geom = ezxml_child(wdb, "Width")) != NULL) |
96 |
|
sd->dim[0] = atof(ezxml_txt(geom)) * |
101 |
|
if ((geom = ezxml_child(wdb, "Thickness")) != NULL) |
102 |
|
sd->dim[2] = atof(ezxml_txt(geom)) * |
103 |
|
to_meters(ezxml_attr(geom, "unit")); |
104 |
< |
if ((sd->dim[0] < .0) | (sd->dim[1] < .0) | (sd->dim[2] < .0)) |
104 |
> |
if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) { |
105 |
> |
sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name); |
106 |
|
return SDEdata; |
107 |
+ |
} |
108 |
|
if ((geom = ezxml_child(wdb, "Geometry")) == NULL || |
109 |
|
(mgfstr = ezxml_txt(geom)) == NULL) |
110 |
|
return SDEnone; |
171 |
|
/* try loading variable resolution data */ |
172 |
|
lastErr = SDloadTre(sd, wtl); |
173 |
|
/* check our result */ |
174 |
< |
switch (lastErr) { |
168 |
< |
case SDEformat: |
169 |
< |
case SDEdata: |
170 |
< |
case SDEsupport: /* possibly we just tried the wrong format */ |
174 |
> |
if (lastErr == SDEsupport) /* try matrix BSDF if not tree data */ |
175 |
|
lastErr = SDloadMtx(sd, wtl); |
176 |
< |
break; |
173 |
< |
default: /* variable res. OK else serious error */ |
174 |
< |
break; |
175 |
< |
} |
176 |
> |
|
177 |
|
/* done with XML file */ |
178 |
|
ezxml_free(fl); |
179 |
|
|
269 |
|
|
270 |
|
/* Initialize an unused BSDF struct (simply clears to zeroes) */ |
271 |
|
void |
272 |
< |
SDclearBSDF(SDData *sd) |
272 |
> |
SDclearBSDF(SDData *sd, const char *fname) |
273 |
|
{ |
274 |
< |
if (sd != NULL) |
275 |
< |
memset(sd, 0, sizeof(SDData)); |
274 |
> |
if (sd == NULL) |
275 |
> |
return; |
276 |
> |
memset(sd, 0, sizeof(SDData)); |
277 |
> |
if (fname == NULL) |
278 |
> |
return; |
279 |
> |
SDclipName(sd->name, fname); |
280 |
|
} |
281 |
|
|
282 |
|
/* Free data associated with BSDF struct */ |
334 |
|
sdl->next = SDcacheList; |
335 |
|
SDcacheList = sdl; |
336 |
|
|
337 |
< |
sdl->refcnt++; |
337 |
> |
sdl->refcnt = 1; |
338 |
|
return &sdl->bsdf; |
339 |
|
} |
340 |
|
|
378 |
|
for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next) |
379 |
|
if (&sdl->bsdf == sd) |
380 |
|
break; |
381 |
< |
if (sdl == NULL || --sdl->refcnt) |
381 |
> |
if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0))) |
382 |
|
return; /* missing or still in use */ |
383 |
|
/* keep unreferenced data? */ |
384 |
|
if (SDisLoaded(sd) && SDretainSet) { |
401 |
|
|
402 |
|
/* Sample an individual BSDF component */ |
403 |
|
SDError |
404 |
< |
SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec, |
400 |
< |
double randX, SDComponent *sdc) |
404 |
> |
SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc) |
405 |
|
{ |
406 |
|
float coef[SDmaxCh]; |
407 |
|
SDError ec; |
408 |
+ |
FVECT inVec; |
409 |
|
const SDCDst *cd; |
410 |
|
double d; |
411 |
|
int n; |
412 |
|
/* check arguments */ |
413 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)) |
413 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL)) |
414 |
|
return SDEargument; |
415 |
|
/* get cumulative distribution */ |
416 |
+ |
VCOPY(inVec, ioVec); |
417 |
|
cd = (*sdc->func->getCDist)(inVec, sdc); |
418 |
|
if (cd == NULL) |
419 |
|
return SDEmemory; |
420 |
|
if (cd->cTotal <= 1e-7) { /* anything to sample? */ |
421 |
|
sv->spec = c_dfcolor; |
422 |
|
sv->cieY = .0; |
423 |
< |
memset(outVec, 0, 3*sizeof(double)); |
423 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
424 |
|
return SDEnone; |
425 |
|
} |
426 |
|
sv->cieY = cd->cTotal; |
427 |
|
/* compute sample direction */ |
428 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX, cd); |
428 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX, cd); |
429 |
|
if (ec) |
430 |
|
return ec; |
431 |
|
/* get BSDF color */ |
432 |
< |
n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
432 |
> |
n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
433 |
|
if (n <= 0) { |
434 |
|
strcpy(SDerrorDetail, "BSDF sample value error"); |
435 |
|
return SDEinternal; |
456 |
|
bitmask_t ndx, coord[MS_MAXDIM]; |
457 |
|
|
458 |
|
while (n > MS_MAXDIM) /* punt for higher dimensions */ |
459 |
< |
t[--n] = drand48(); |
459 |
> |
t[--n] = rand()*(1./(RAND_MAX+.5)); |
460 |
|
nBits = (8*sizeof(bitmask_t) - 1) / n; |
461 |
|
ndx = randX * (double)((bitmask_t)1 << (nBits*n)); |
462 |
|
/* get coordinate on Hilbert curve */ |
464 |
|
/* convert back to [0,1) range */ |
465 |
|
scale = 1. / (double)((bitmask_t)1 << nBits); |
466 |
|
while (n--) |
467 |
< |
t[n] = scale * ((double)coord[n] + drand48()); |
467 |
> |
t[n] = scale * ((double)coord[n] + rand()*(1./(RAND_MAX+.5))); |
468 |
|
} |
469 |
|
|
470 |
|
#undef MS_MAXDIM |
477 |
|
SDmultiSamp(outVec, 2, randX); |
478 |
|
SDsquare2disk(outVec, outVec[0], outVec[1]); |
479 |
|
outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1]; |
480 |
< |
if (outVec[2] > .0) /* a bit of paranoia */ |
480 |
> |
if (outVec[2] > 0) /* a bit of paranoia */ |
481 |
|
outVec[2] = sqrt(outVec[2]); |
482 |
|
if (!outFront) /* going out back? */ |
483 |
|
outVec[2] = -outVec[2]; |
485 |
|
|
486 |
|
/* Query projected solid angle coverage for non-diffuse BSDF direction */ |
487 |
|
SDError |
488 |
< |
SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd) |
488 |
> |
SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2, |
489 |
> |
int qflags, const SDData *sd) |
490 |
|
{ |
491 |
< |
SDSpectralDF *rdf; |
491 |
> |
SDSpectralDF *rdf, *tdf; |
492 |
|
SDError ec; |
493 |
|
int i; |
494 |
|
/* check arguments */ |
495 |
< |
if ((projSA == NULL) | (vec == NULL) | (sd == NULL)) |
495 |
> |
if ((projSA == NULL) | (v1 == NULL) | (sd == NULL)) |
496 |
|
return SDEargument; |
497 |
|
/* initialize extrema */ |
498 |
< |
switch (qflags & SDqueryMin+SDqueryMax) { |
498 |
> |
switch (qflags) { |
499 |
|
case SDqueryMax: |
500 |
|
projSA[0] = .0; |
501 |
|
break; |
508 |
|
case 0: |
509 |
|
return SDEargument; |
510 |
|
} |
511 |
< |
if (vec[2] > .0) /* front surface query? */ |
511 |
> |
if (v1[2] > 0) /* front surface query? */ |
512 |
|
rdf = sd->rf; |
513 |
|
else |
514 |
|
rdf = sd->rb; |
515 |
+ |
tdf = sd->tf; |
516 |
+ |
if (v2 != NULL) /* bidirectional? */ |
517 |
+ |
if (v1[2] > 0 ^ v2[2] > 0) |
518 |
+ |
rdf = NULL; |
519 |
+ |
else |
520 |
+ |
tdf = NULL; |
521 |
|
ec = SDEdata; /* run through components */ |
522 |
|
for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) { |
523 |
< |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
524 |
< |
rdf->comp[i].dist); |
523 |
> |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
524 |
> |
qflags, &rdf->comp[i]); |
525 |
|
if (ec) |
526 |
|
return ec; |
527 |
|
} |
528 |
< |
for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) { |
529 |
< |
ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
530 |
< |
sd->tf->comp[i].dist); |
528 |
> |
for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) { |
529 |
> |
ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
530 |
> |
qflags, &tdf->comp[i]); |
531 |
|
if (ec) |
532 |
|
return ec; |
533 |
|
} |
534 |
< |
return ec; |
534 |
> |
if (ec) { /* all diffuse? */ |
535 |
> |
projSA[0] = M_PI; |
536 |
> |
if (qflags == SDqueryMin+SDqueryMax) |
537 |
> |
projSA[1] = M_PI; |
538 |
> |
} |
539 |
> |
return SDEnone; |
540 |
|
} |
541 |
|
|
542 |
|
/* Return BSDF for the given incident and scattered ray vectors */ |
551 |
|
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL)) |
552 |
|
return SDEargument; |
553 |
|
/* whose side are we on? */ |
554 |
< |
inFront = (inVec[2] > .0); |
555 |
< |
outFront = (outVec[2] > .0); |
554 |
> |
inFront = (inVec[2] > 0); |
555 |
> |
outFront = (outVec[2] > 0); |
556 |
|
/* start with diffuse portion */ |
557 |
|
if (inFront & outFront) { |
558 |
|
*sv = sd->rLambFront; |
569 |
|
i = (sdf != NULL) ? sdf->ncomp : 0; |
570 |
|
while (i-- > 0) { |
571 |
|
nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec, |
572 |
< |
sdf->comp[i].dist); |
572 |
> |
&sdf->comp[i]); |
573 |
|
while (nch-- > 0) { |
574 |
|
c_cmix(&sv->spec, sv->cieY, &sv->spec, |
575 |
|
coef[nch], &sdf->comp[i].cspec[nch]); |
593 |
|
if ((inVec == NULL) | (sd == NULL)) |
594 |
|
return .0; |
595 |
|
/* gather diffuse components */ |
596 |
< |
if (inVec[2] > .0) { |
596 |
> |
if (inVec[2] > 0) { |
597 |
|
hsum = sd->rLambFront.cieY; |
598 |
|
rdf = sd->rf; |
599 |
|
} else /* !inFront */ { |
624 |
|
|
625 |
|
/* Sample BSDF direction based on the given random variable */ |
626 |
|
SDError |
627 |
< |
SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec, |
610 |
< |
double randX, int sflags, const SDData *sd) |
627 |
> |
SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd) |
628 |
|
{ |
629 |
|
SDError ec; |
630 |
+ |
FVECT inVec; |
631 |
|
int inFront; |
632 |
|
SDSpectralDF *rdf; |
633 |
|
double rdiff; |
636 |
|
SDComponent *sdc; |
637 |
|
const SDCDst **cdarr = NULL; |
638 |
|
/* check arguments */ |
639 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) | |
640 |
< |
(randX < .0) | (randX >= 1.)) |
639 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) | |
640 |
> |
(randX < 0) | (randX >= 1.)) |
641 |
|
return SDEargument; |
642 |
|
/* whose side are we on? */ |
643 |
< |
inFront = (inVec[2] > .0); |
643 |
> |
VCOPY(inVec, ioVec); |
644 |
> |
inFront = (inVec[2] > 0); |
645 |
|
/* remember diffuse portions */ |
646 |
|
if (inFront) { |
647 |
|
*sv = sd->rLambFront; |
681 |
|
} |
682 |
|
if (sv->cieY <= 1e-7) { /* anything to sample? */ |
683 |
|
sv->cieY = .0; |
684 |
< |
memset(outVec, 0, 3*sizeof(double)); |
684 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
685 |
|
return SDEnone; |
686 |
|
} |
687 |
|
/* scale random variable */ |
688 |
|
randX *= sv->cieY; |
689 |
|
/* diffuse reflection? */ |
690 |
|
if (randX < rdiff) { |
691 |
< |
SDdiffuseSamp(outVec, inFront, randX/rdiff); |
691 |
> |
SDdiffuseSamp(ioVec, inFront, randX/rdiff); |
692 |
|
goto done; |
693 |
|
} |
694 |
|
randX -= rdiff; |
696 |
|
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) { |
697 |
|
if (randX < sd->tLamb.cieY) { |
698 |
|
sv->spec = sd->tLamb.spec; |
699 |
< |
SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY); |
699 |
> |
SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY); |
700 |
|
goto done; |
701 |
|
} |
702 |
|
randX -= sd->tLamb.cieY; |
708 |
|
return SDEinternal; |
709 |
|
/* compute sample direction */ |
710 |
|
sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr]; |
711 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]); |
711 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); |
712 |
|
if (ec) |
713 |
|
return ec; |
714 |
|
/* compute color */ |
715 |
< |
j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
715 |
> |
j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
716 |
|
if (j <= 0) { |
717 |
|
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
718 |
|
sd->name); |
739 |
|
if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL)) |
740 |
|
return SDEargument; |
741 |
|
VCOPY(vMtx[2], sNrm); |
742 |
< |
if (normalize(vMtx[2]) == .0) |
742 |
> |
if (normalize(vMtx[2]) == 0) |
743 |
|
return SDEargument; |
744 |
|
fcross(vMtx[0], uVec, vMtx[2]); |
745 |
< |
if (normalize(vMtx[0]) == .0) |
745 |
> |
if (normalize(vMtx[0]) == 0) |
746 |
|
return SDEargument; |
747 |
|
fcross(vMtx[1], vMtx[2], vMtx[0]); |
748 |
|
return SDEnone; |
762 |
|
mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1]; |
763 |
|
mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2]; |
764 |
|
d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2]; |
765 |
< |
if (d == .0) { |
765 |
> |
if (d == 0) { |
766 |
|
strcpy(SDerrorDetail, "Zero determinant in matrix inversion"); |
767 |
|
return SDEargument; |
768 |
|
} |
789 |
|
if (vMtx == NULL) { /* assume they just want to normalize */ |
790 |
|
if (resVec != inpVec) |
791 |
|
VCOPY(resVec, inpVec); |
792 |
< |
return (normalize(resVec) > .0) ? SDEnone : SDEargument; |
792 |
> |
return (normalize(resVec) > 0) ? SDEnone : SDEargument; |
793 |
|
} |
794 |
|
vTmp[0] = DOT(vMtx[0], inpVec); |
795 |
|
vTmp[1] = DOT(vMtx[1], inpVec); |
796 |
|
vTmp[2] = DOT(vMtx[2], inpVec); |
797 |
< |
if (normalize(vTmp) == .0) |
797 |
> |
if (normalize(vTmp) == 0) |
798 |
|
return SDEargument; |
799 |
|
VCOPY(resVec, vTmp); |
800 |
|
return SDEnone; |
866 |
|
static int |
867 |
|
fequal(double a, double b) |
868 |
|
{ |
869 |
< |
if (b != .0) |
869 |
> |
if (b != 0) |
870 |
|
a = a/b - 1.; |
871 |
|
return((a <= 1e-6) & (a >= -1e-6)); |
872 |
|
} |
1184 |
|
hemi_total = .0; |
1185 |
|
for (i = dp->ninc; i--; ) { |
1186 |
|
dom = getBSDF_incohm(dp,i); |
1187 |
< |
if (dom <= .0) { |
1187 |
> |
if (dom <= 0) { |
1188 |
|
error(WARNING, "zero/negative incoming solid angle"); |
1189 |
|
continue; |
1190 |
|
} |
1207 |
|
hemi_total = .0; |
1208 |
|
for (o = dp->nout; o--; ) { |
1209 |
|
dom = getBSDF_outohm(dp,o); |
1210 |
< |
if (dom <= .0) { |
1210 |
> |
if (dom <= 0) { |
1211 |
|
error(WARNING, "zero/negative outgoing solid angle"); |
1212 |
|
continue; |
1213 |
|
} |
1231 |
|
hemi_total = .0; |
1232 |
|
for (o = dp->nout; o--; ) { |
1233 |
|
double f = BSDF_value(dp,i,o); |
1234 |
< |
if (f >= .0) |
1234 |
> |
if (f >= 0) |
1235 |
|
hemi_total += f*omega_oarr[o]; |
1236 |
|
else { |
1237 |
|
nneg += (f < -FTINY); |