| 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, |
| 176 |
|
/* try loading variable resolution data */ |
| 177 |
|
lastErr = SDloadTre(sd, wtl); |
| 178 |
|
/* check our result */ |
| 179 |
< |
switch (lastErr) { |
| 169 |
< |
case SDEformat: |
| 170 |
< |
case SDEdata: |
| 171 |
< |
case SDEsupport: /* possibly we just tried the wrong format */ |
| 179 |
> |
if (lastErr == SDEsupport) /* try matrix BSDF if not tree data */ |
| 180 |
|
lastErr = SDloadMtx(sd, wtl); |
| 181 |
< |
break; |
| 174 |
< |
default: /* variable res. OK else serious error */ |
| 175 |
< |
break; |
| 176 |
< |
} |
| 181 |
> |
|
| 182 |
|
/* done with XML file */ |
| 183 |
|
ezxml_free(fl); |
| 184 |
|
|
| 274 |
|
|
| 275 |
|
/* Initialize an unused BSDF struct (simply clears to zeroes) */ |
| 276 |
|
void |
| 277 |
< |
SDclearBSDF(SDData *sd) |
| 277 |
> |
SDclearBSDF(SDData *sd, const char *fname) |
| 278 |
|
{ |
| 279 |
< |
if (sd != NULL) |
| 280 |
< |
memset(sd, 0, sizeof(SDData)); |
| 279 |
> |
if (sd == NULL) |
| 280 |
> |
return; |
| 281 |
> |
memset(sd, 0, sizeof(SDData)); |
| 282 |
> |
if (fname == NULL) |
| 283 |
> |
return; |
| 284 |
> |
SDclipName(sd->name, fname); |
| 285 |
|
} |
| 286 |
|
|
| 287 |
|
/* Free data associated with BSDF struct */ |
| 339 |
|
sdl->next = SDcacheList; |
| 340 |
|
SDcacheList = sdl; |
| 341 |
|
|
| 342 |
< |
sdl->refcnt++; |
| 342 |
> |
sdl->refcnt = 1; |
| 343 |
|
return &sdl->bsdf; |
| 344 |
|
} |
| 345 |
|
|
| 383 |
|
for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next) |
| 384 |
|
if (&sdl->bsdf == sd) |
| 385 |
|
break; |
| 386 |
< |
if (sdl == NULL || --sdl->refcnt) |
| 386 |
> |
if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0))) |
| 387 |
|
return; /* missing or still in use */ |
| 388 |
|
/* keep unreferenced data? */ |
| 389 |
|
if (SDisLoaded(sd) && SDretainSet) { |
| 406 |
|
|
| 407 |
|
/* Sample an individual BSDF component */ |
| 408 |
|
SDError |
| 409 |
< |
SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec, |
| 401 |
< |
double randX, SDComponent *sdc) |
| 409 |
> |
SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc) |
| 410 |
|
{ |
| 411 |
|
float coef[SDmaxCh]; |
| 412 |
|
SDError ec; |
| 413 |
+ |
FVECT inVec; |
| 414 |
|
const SDCDst *cd; |
| 415 |
|
double d; |
| 416 |
|
int n; |
| 417 |
|
/* check arguments */ |
| 418 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)) |
| 418 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL)) |
| 419 |
|
return SDEargument; |
| 420 |
|
/* get cumulative distribution */ |
| 421 |
+ |
VCOPY(inVec, ioVec); |
| 422 |
|
cd = (*sdc->func->getCDist)(inVec, sdc); |
| 423 |
|
if (cd == NULL) |
| 424 |
|
return SDEmemory; |
| 425 |
|
if (cd->cTotal <= 1e-7) { /* anything to sample? */ |
| 426 |
|
sv->spec = c_dfcolor; |
| 427 |
|
sv->cieY = .0; |
| 428 |
< |
memset(outVec, 0, 3*sizeof(double)); |
| 428 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
| 429 |
|
return SDEnone; |
| 430 |
|
} |
| 431 |
|
sv->cieY = cd->cTotal; |
| 432 |
|
/* compute sample direction */ |
| 433 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX, cd); |
| 433 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX, cd); |
| 434 |
|
if (ec) |
| 435 |
|
return ec; |
| 436 |
|
/* get BSDF color */ |
| 437 |
< |
n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
| 437 |
> |
n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
| 438 |
|
if (n <= 0) { |
| 439 |
|
strcpy(SDerrorDetail, "BSDF sample value error"); |
| 440 |
|
return SDEinternal; |
| 461 |
|
bitmask_t ndx, coord[MS_MAXDIM]; |
| 462 |
|
|
| 463 |
|
while (n > MS_MAXDIM) /* punt for higher dimensions */ |
| 464 |
< |
t[--n] = rand()*(1./RAND_MAX); |
| 464 |
> |
t[--n] = rand()*(1./(RAND_MAX+.5)); |
| 465 |
|
nBits = (8*sizeof(bitmask_t) - 1) / n; |
| 466 |
|
ndx = randX * (double)((bitmask_t)1 << (nBits*n)); |
| 467 |
|
/* get coordinate on Hilbert curve */ |
| 469 |
|
/* convert back to [0,1) range */ |
| 470 |
|
scale = 1. / (double)((bitmask_t)1 << nBits); |
| 471 |
|
while (n--) |
| 472 |
< |
t[n] = scale * ((double)coord[n] + rand()*(1./RAND_MAX)); |
| 472 |
> |
t[n] = scale * ((double)coord[n] + rand()*(1./(RAND_MAX+.5))); |
| 473 |
|
} |
| 474 |
|
|
| 475 |
|
#undef MS_MAXDIM |
| 482 |
|
SDmultiSamp(outVec, 2, randX); |
| 483 |
|
SDsquare2disk(outVec, outVec[0], outVec[1]); |
| 484 |
|
outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1]; |
| 485 |
< |
if (outVec[2] > .0) /* a bit of paranoia */ |
| 485 |
> |
if (outVec[2] > 0) /* a bit of paranoia */ |
| 486 |
|
outVec[2] = sqrt(outVec[2]); |
| 487 |
|
if (!outFront) /* going out back? */ |
| 488 |
|
outVec[2] = -outVec[2]; |
| 490 |
|
|
| 491 |
|
/* Query projected solid angle coverage for non-diffuse BSDF direction */ |
| 492 |
|
SDError |
| 493 |
< |
SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd) |
| 493 |
> |
SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2, |
| 494 |
> |
int qflags, const SDData *sd) |
| 495 |
|
{ |
| 496 |
< |
SDSpectralDF *rdf; |
| 496 |
> |
SDSpectralDF *rdf, *tdf; |
| 497 |
|
SDError ec; |
| 498 |
|
int i; |
| 499 |
|
/* check arguments */ |
| 500 |
< |
if ((projSA == NULL) | (vec == NULL) | (sd == NULL)) |
| 500 |
> |
if ((projSA == NULL) | (v1 == NULL) | (sd == NULL)) |
| 501 |
|
return SDEargument; |
| 502 |
|
/* initialize extrema */ |
| 503 |
|
switch (qflags) { |
| 513 |
|
case 0: |
| 514 |
|
return SDEargument; |
| 515 |
|
} |
| 516 |
< |
if (vec[2] > .0) /* front surface query? */ |
| 516 |
> |
if (v1[2] > 0) /* front surface query? */ |
| 517 |
|
rdf = sd->rf; |
| 518 |
|
else |
| 519 |
|
rdf = sd->rb; |
| 520 |
+ |
tdf = sd->tf; |
| 521 |
+ |
if (v2 != NULL) /* bidirectional? */ |
| 522 |
+ |
if (v1[2] > 0 ^ v2[2] > 0) |
| 523 |
+ |
rdf = NULL; |
| 524 |
+ |
else |
| 525 |
+ |
tdf = NULL; |
| 526 |
|
ec = SDEdata; /* run through components */ |
| 527 |
|
for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) { |
| 528 |
< |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
| 529 |
< |
rdf->comp[i].dist); |
| 528 |
> |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
| 529 |
> |
qflags, &rdf->comp[i]); |
| 530 |
|
if (ec) |
| 531 |
|
return ec; |
| 532 |
|
} |
| 533 |
< |
for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) { |
| 534 |
< |
ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
| 535 |
< |
sd->tf->comp[i].dist); |
| 533 |
> |
for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) { |
| 534 |
> |
ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
| 535 |
> |
qflags, &tdf->comp[i]); |
| 536 |
|
if (ec) |
| 537 |
|
return ec; |
| 538 |
|
} |
| 556 |
|
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL)) |
| 557 |
|
return SDEargument; |
| 558 |
|
/* whose side are we on? */ |
| 559 |
< |
inFront = (inVec[2] > .0); |
| 560 |
< |
outFront = (outVec[2] > .0); |
| 559 |
> |
inFront = (inVec[2] > 0); |
| 560 |
> |
outFront = (outVec[2] > 0); |
| 561 |
|
/* start with diffuse portion */ |
| 562 |
|
if (inFront & outFront) { |
| 563 |
|
*sv = sd->rLambFront; |
| 574 |
|
i = (sdf != NULL) ? sdf->ncomp : 0; |
| 575 |
|
while (i-- > 0) { |
| 576 |
|
nch = (*sdf->comp[i].func->getBSDFs)(coef, outVec, inVec, |
| 577 |
< |
sdf->comp[i].dist); |
| 577 |
> |
&sdf->comp[i]); |
| 578 |
|
while (nch-- > 0) { |
| 579 |
|
c_cmix(&sv->spec, sv->cieY, &sv->spec, |
| 580 |
|
coef[nch], &sdf->comp[i].cspec[nch]); |
| 598 |
|
if ((inVec == NULL) | (sd == NULL)) |
| 599 |
|
return .0; |
| 600 |
|
/* gather diffuse components */ |
| 601 |
< |
if (inVec[2] > .0) { |
| 601 |
> |
if (inVec[2] > 0) { |
| 602 |
|
hsum = sd->rLambFront.cieY; |
| 603 |
|
rdf = sd->rf; |
| 604 |
|
} else /* !inFront */ { |
| 629 |
|
|
| 630 |
|
/* Sample BSDF direction based on the given random variable */ |
| 631 |
|
SDError |
| 632 |
< |
SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec, |
| 616 |
< |
double randX, int sflags, const SDData *sd) |
| 632 |
> |
SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd) |
| 633 |
|
{ |
| 634 |
|
SDError ec; |
| 635 |
+ |
FVECT inVec; |
| 636 |
|
int inFront; |
| 637 |
|
SDSpectralDF *rdf; |
| 638 |
|
double rdiff; |
| 641 |
|
SDComponent *sdc; |
| 642 |
|
const SDCDst **cdarr = NULL; |
| 643 |
|
/* check arguments */ |
| 644 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) | |
| 645 |
< |
(randX < .0) | (randX >= 1.)) |
| 644 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) | |
| 645 |
> |
(randX < 0) | (randX >= 1.)) |
| 646 |
|
return SDEargument; |
| 647 |
|
/* whose side are we on? */ |
| 648 |
< |
inFront = (inVec[2] > .0); |
| 648 |
> |
VCOPY(inVec, ioVec); |
| 649 |
> |
inFront = (inVec[2] > 0); |
| 650 |
|
/* remember diffuse portions */ |
| 651 |
|
if (inFront) { |
| 652 |
|
*sv = sd->rLambFront; |
| 686 |
|
} |
| 687 |
|
if (sv->cieY <= 1e-7) { /* anything to sample? */ |
| 688 |
|
sv->cieY = .0; |
| 689 |
< |
memset(outVec, 0, 3*sizeof(double)); |
| 689 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
| 690 |
|
return SDEnone; |
| 691 |
|
} |
| 692 |
|
/* scale random variable */ |
| 693 |
|
randX *= sv->cieY; |
| 694 |
|
/* diffuse reflection? */ |
| 695 |
|
if (randX < rdiff) { |
| 696 |
< |
SDdiffuseSamp(outVec, inFront, randX/rdiff); |
| 696 |
> |
SDdiffuseSamp(ioVec, inFront, randX/rdiff); |
| 697 |
|
goto done; |
| 698 |
|
} |
| 699 |
|
randX -= rdiff; |
| 701 |
|
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) { |
| 702 |
|
if (randX < sd->tLamb.cieY) { |
| 703 |
|
sv->spec = sd->tLamb.spec; |
| 704 |
< |
SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY); |
| 704 |
> |
SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY); |
| 705 |
|
goto done; |
| 706 |
|
} |
| 707 |
|
randX -= sd->tLamb.cieY; |
| 713 |
|
return SDEinternal; |
| 714 |
|
/* compute sample direction */ |
| 715 |
|
sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr]; |
| 716 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]); |
| 716 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); |
| 717 |
|
if (ec) |
| 718 |
|
return ec; |
| 719 |
|
/* compute color */ |
| 720 |
< |
j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
| 720 |
> |
j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc); |
| 721 |
|
if (j <= 0) { |
| 722 |
|
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
| 723 |
|
sd->name); |
| 744 |
|
if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL)) |
| 745 |
|
return SDEargument; |
| 746 |
|
VCOPY(vMtx[2], sNrm); |
| 747 |
< |
if (normalize(vMtx[2]) == .0) |
| 747 |
> |
if (normalize(vMtx[2]) == 0) |
| 748 |
|
return SDEargument; |
| 749 |
|
fcross(vMtx[0], uVec, vMtx[2]); |
| 750 |
< |
if (normalize(vMtx[0]) == .0) |
| 750 |
> |
if (normalize(vMtx[0]) == 0) |
| 751 |
|
return SDEargument; |
| 752 |
|
fcross(vMtx[1], vMtx[2], vMtx[0]); |
| 753 |
|
return SDEnone; |
| 767 |
|
mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1]; |
| 768 |
|
mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2]; |
| 769 |
|
d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2]; |
| 770 |
< |
if (d == .0) { |
| 770 |
> |
if (d == 0) { |
| 771 |
|
strcpy(SDerrorDetail, "Zero determinant in matrix inversion"); |
| 772 |
|
return SDEargument; |
| 773 |
|
} |
| 794 |
|
if (vMtx == NULL) { /* assume they just want to normalize */ |
| 795 |
|
if (resVec != inpVec) |
| 796 |
|
VCOPY(resVec, inpVec); |
| 797 |
< |
return (normalize(resVec) > .0) ? SDEnone : SDEargument; |
| 797 |
> |
return (normalize(resVec) > 0) ? SDEnone : SDEargument; |
| 798 |
|
} |
| 799 |
|
vTmp[0] = DOT(vMtx[0], inpVec); |
| 800 |
|
vTmp[1] = DOT(vMtx[1], inpVec); |
| 801 |
|
vTmp[2] = DOT(vMtx[2], inpVec); |
| 802 |
< |
if (normalize(vTmp) == .0) |
| 802 |
> |
if (normalize(vTmp) == 0) |
| 803 |
|
return SDEargument; |
| 804 |
|
VCOPY(resVec, vTmp); |
| 805 |
|
return SDEnone; |
| 814 |
|
|
| 815 |
|
#include "standard.h" |
| 816 |
|
#include "paths.h" |
| 799 |
– |
#include <ctype.h> |
| 817 |
|
|
| 818 |
|
#define MAXLATS 46 /* maximum number of latitudes */ |
| 819 |
|
|
| 870 |
|
static int |
| 871 |
|
fequal(double a, double b) |
| 872 |
|
{ |
| 873 |
< |
if (b != .0) |
| 873 |
> |
if (b != 0) |
| 874 |
|
a = a/b - 1.; |
| 875 |
|
return((a <= 1e-6) & (a >= -1e-6)); |
| 876 |
|
} |
| 933 |
|
{ |
| 934 |
|
ANGLE_BASIS *ab = (ANGLE_BASIS *)p; |
| 935 |
|
int li, ndx; |
| 936 |
< |
double pol, azi, d; |
| 936 |
> |
double pol, azi; |
| 937 |
|
|
| 938 |
|
if ((v[2] < -1.0) | (v[2] > 1.0)) |
| 939 |
|
return(-1); |
| 1173 |
|
) |
| 1174 |
|
{ |
| 1175 |
|
double *omega_iarr, *omega_oarr; |
| 1176 |
< |
double dom, contrib, hemi_total, full_total; |
| 1176 |
> |
double dom, hemi_total, full_total; |
| 1177 |
|
int nneg; |
| 1178 |
|
FVECT v; |
| 1179 |
|
int i, o; |
| 1188 |
|
hemi_total = .0; |
| 1189 |
|
for (i = dp->ninc; i--; ) { |
| 1190 |
|
dom = getBSDF_incohm(dp,i); |
| 1191 |
< |
if (dom <= .0) { |
| 1191 |
> |
if (dom <= 0) { |
| 1192 |
|
error(WARNING, "zero/negative incoming solid angle"); |
| 1193 |
|
continue; |
| 1194 |
|
} |
| 1211 |
|
hemi_total = .0; |
| 1212 |
|
for (o = dp->nout; o--; ) { |
| 1213 |
|
dom = getBSDF_outohm(dp,o); |
| 1214 |
< |
if (dom <= .0) { |
| 1214 |
> |
if (dom <= 0) { |
| 1215 |
|
error(WARNING, "zero/negative outgoing solid angle"); |
| 1216 |
|
continue; |
| 1217 |
|
} |
| 1235 |
|
hemi_total = .0; |
| 1236 |
|
for (o = dp->nout; o--; ) { |
| 1237 |
|
double f = BSDF_value(dp,i,o); |
| 1238 |
< |
if (f >= .0) |
| 1238 |
> |
if (f >= 0) |
| 1239 |
|
hemi_total += f*omega_oarr[o]; |
| 1240 |
|
else { |
| 1241 |
|
nneg += (f < -FTINY); |