| 45 |
|
SDError |
| 46 |
|
SDreportEnglish(SDError ec, FILE *fp) |
| 47 |
|
{ |
| 48 |
– |
if (fp == NULL) |
| 49 |
– |
return ec; |
| 48 |
|
if (!ec) |
| 49 |
|
return SDEnone; |
| 50 |
+ |
if ((ec < SDEnone) | (ec > SDEunknown)) { |
| 51 |
+ |
SDerrorDetail[0] = '\0'; |
| 52 |
+ |
ec = SDEunknown; |
| 53 |
+ |
} |
| 54 |
+ |
if (fp == NULL) |
| 55 |
+ |
return ec; |
| 56 |
|
fputs(SDerrorEnglish[ec], fp); |
| 57 |
|
if (SDerrorDetail[0]) { |
| 58 |
|
fputs(": ", fp); |
| 99 |
|
if ((geom = ezxml_child(wdb, "Thickness")) != NULL) |
| 100 |
|
sd->dim[2] = atof(ezxml_txt(geom)) * |
| 101 |
|
to_meters(ezxml_attr(geom, "unit")); |
| 102 |
< |
if ((sd->dim[0] < .0) | (sd->dim[1] < .0) | (sd->dim[2] < .0)) { |
| 102 |
> |
if ((sd->dim[0] < 0) | (sd->dim[1] < 0) | (sd->dim[2] < 0)) { |
| 103 |
|
sprintf(SDerrorDetail, "Negative size in \"%s\"", sd->name); |
| 104 |
|
return SDEdata; |
| 105 |
|
} |
| 273 |
|
|
| 274 |
|
/* Initialize an unused BSDF struct (simply clears to zeroes) */ |
| 275 |
|
void |
| 276 |
< |
SDclearBSDF(SDData *sd) |
| 276 |
> |
SDclearBSDF(SDData *sd, const char *fname) |
| 277 |
|
{ |
| 278 |
< |
if (sd != NULL) |
| 279 |
< |
memset(sd, 0, sizeof(SDData)); |
| 278 |
> |
if (sd == NULL) |
| 279 |
> |
return; |
| 280 |
> |
memset(sd, 0, sizeof(SDData)); |
| 281 |
> |
if (fname == NULL) |
| 282 |
> |
return; |
| 283 |
> |
SDclipName(sd->name, fname); |
| 284 |
|
} |
| 285 |
|
|
| 286 |
|
/* Free data associated with BSDF struct */ |
| 338 |
|
sdl->next = SDcacheList; |
| 339 |
|
SDcacheList = sdl; |
| 340 |
|
|
| 341 |
< |
sdl->refcnt++; |
| 341 |
> |
sdl->refcnt = 1; |
| 342 |
|
return &sdl->bsdf; |
| 343 |
|
} |
| 344 |
|
|
| 382 |
|
for (sdl = SDcacheList; sdl != NULL; sdl = (sdLast=sdl)->next) |
| 383 |
|
if (&sdl->bsdf == sd) |
| 384 |
|
break; |
| 385 |
< |
if (sdl == NULL || --sdl->refcnt) |
| 385 |
> |
if (sdl == NULL || (sdl->refcnt -= (sdl->refcnt > 0))) |
| 386 |
|
return; /* missing or still in use */ |
| 387 |
|
/* keep unreferenced data? */ |
| 388 |
|
if (SDisLoaded(sd) && SDretainSet) { |
| 405 |
|
|
| 406 |
|
/* Sample an individual BSDF component */ |
| 407 |
|
SDError |
| 408 |
< |
SDsampComponent(SDValue *sv, FVECT outVec, const FVECT inVec, |
| 401 |
< |
double randX, SDComponent *sdc) |
| 408 |
> |
SDsampComponent(SDValue *sv, FVECT ioVec, double randX, SDComponent *sdc) |
| 409 |
|
{ |
| 410 |
|
float coef[SDmaxCh]; |
| 411 |
|
SDError ec; |
| 412 |
+ |
FVECT inVec; |
| 413 |
|
const SDCDst *cd; |
| 414 |
|
double d; |
| 415 |
|
int n; |
| 416 |
|
/* check arguments */ |
| 417 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sdc == NULL)) |
| 417 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sdc == NULL)) |
| 418 |
|
return SDEargument; |
| 419 |
|
/* get cumulative distribution */ |
| 420 |
+ |
VCOPY(inVec, ioVec); |
| 421 |
|
cd = (*sdc->func->getCDist)(inVec, sdc); |
| 422 |
|
if (cd == NULL) |
| 423 |
|
return SDEmemory; |
| 424 |
|
if (cd->cTotal <= 1e-7) { /* anything to sample? */ |
| 425 |
|
sv->spec = c_dfcolor; |
| 426 |
|
sv->cieY = .0; |
| 427 |
< |
memset(outVec, 0, 3*sizeof(double)); |
| 427 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
| 428 |
|
return SDEnone; |
| 429 |
|
} |
| 430 |
|
sv->cieY = cd->cTotal; |
| 431 |
|
/* compute sample direction */ |
| 432 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX, cd); |
| 432 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX, cd); |
| 433 |
|
if (ec) |
| 434 |
|
return ec; |
| 435 |
|
/* get BSDF color */ |
| 436 |
< |
n = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
| 436 |
> |
n = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc->dist); |
| 437 |
|
if (n <= 0) { |
| 438 |
|
strcpy(SDerrorDetail, "BSDF sample value error"); |
| 439 |
|
return SDEinternal; |
| 460 |
|
bitmask_t ndx, coord[MS_MAXDIM]; |
| 461 |
|
|
| 462 |
|
while (n > MS_MAXDIM) /* punt for higher dimensions */ |
| 463 |
< |
t[--n] = rand()*(1./RAND_MAX); |
| 463 |
> |
t[--n] = rand()*(1./(RAND_MAX+.5)); |
| 464 |
|
nBits = (8*sizeof(bitmask_t) - 1) / n; |
| 465 |
|
ndx = randX * (double)((bitmask_t)1 << (nBits*n)); |
| 466 |
|
/* get coordinate on Hilbert curve */ |
| 468 |
|
/* convert back to [0,1) range */ |
| 469 |
|
scale = 1. / (double)((bitmask_t)1 << nBits); |
| 470 |
|
while (n--) |
| 471 |
< |
t[n] = scale * ((double)coord[n] + rand()*(1./RAND_MAX)); |
| 471 |
> |
t[n] = scale * ((double)coord[n] + rand()*(1./(RAND_MAX+.5))); |
| 472 |
|
} |
| 473 |
|
|
| 474 |
|
#undef MS_MAXDIM |
| 481 |
|
SDmultiSamp(outVec, 2, randX); |
| 482 |
|
SDsquare2disk(outVec, outVec[0], outVec[1]); |
| 483 |
|
outVec[2] = 1. - outVec[0]*outVec[0] - outVec[1]*outVec[1]; |
| 484 |
< |
if (outVec[2] > .0) /* a bit of paranoia */ |
| 484 |
> |
if (outVec[2] > 0) /* a bit of paranoia */ |
| 485 |
|
outVec[2] = sqrt(outVec[2]); |
| 486 |
|
if (!outFront) /* going out back? */ |
| 487 |
|
outVec[2] = -outVec[2]; |
| 489 |
|
|
| 490 |
|
/* Query projected solid angle coverage for non-diffuse BSDF direction */ |
| 491 |
|
SDError |
| 492 |
< |
SDsizeBSDF(double *projSA, const FVECT vec, int qflags, const SDData *sd) |
| 492 |
> |
SDsizeBSDF(double *projSA, const FVECT v1, const RREAL *v2, |
| 493 |
> |
int qflags, const SDData *sd) |
| 494 |
|
{ |
| 495 |
< |
SDSpectralDF *rdf; |
| 495 |
> |
SDSpectralDF *rdf, *tdf; |
| 496 |
|
SDError ec; |
| 497 |
|
int i; |
| 498 |
|
/* check arguments */ |
| 499 |
< |
if ((projSA == NULL) | (vec == NULL) | (sd == NULL)) |
| 499 |
> |
if ((projSA == NULL) | (v1 == NULL)) |
| 500 |
|
return SDEargument; |
| 501 |
|
/* initialize extrema */ |
| 502 |
|
switch (qflags) { |
| 512 |
|
case 0: |
| 513 |
|
return SDEargument; |
| 514 |
|
} |
| 515 |
< |
if (vec[2] > .0) /* front surface query? */ |
| 515 |
> |
if (v1[2] > 0) /* front surface query? */ |
| 516 |
|
rdf = sd->rf; |
| 517 |
|
else |
| 518 |
|
rdf = sd->rb; |
| 519 |
+ |
tdf = NULL; /* transmitted component? */ |
| 520 |
+ |
if (v2 != NULL && v1[2] > 0 ^ v2[2] > 0) { |
| 521 |
+ |
rdf = NULL; |
| 522 |
+ |
tdf = sd->tf; |
| 523 |
+ |
} |
| 524 |
|
ec = SDEdata; /* run through components */ |
| 525 |
|
for (i = (rdf==NULL) ? 0 : rdf->ncomp; i--; ) { |
| 526 |
< |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
| 527 |
< |
rdf->comp[i].dist); |
| 526 |
> |
ec = (*rdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
| 527 |
> |
qflags, rdf->comp[i].dist); |
| 528 |
|
if (ec) |
| 529 |
|
return ec; |
| 530 |
|
} |
| 531 |
< |
for (i = (sd->tf==NULL) ? 0 : sd->tf->ncomp; i--; ) { |
| 532 |
< |
ec = (*sd->tf->comp[i].func->queryProjSA)(projSA, vec, qflags, |
| 533 |
< |
sd->tf->comp[i].dist); |
| 531 |
> |
for (i = (tdf==NULL) ? 0 : tdf->ncomp; i--; ) { |
| 532 |
> |
ec = (*tdf->comp[i].func->queryProjSA)(projSA, v1, v2, |
| 533 |
> |
qflags, tdf->comp[i].dist); |
| 534 |
|
if (ec) |
| 535 |
|
return ec; |
| 536 |
|
} |
| 554 |
|
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL)) |
| 555 |
|
return SDEargument; |
| 556 |
|
/* whose side are we on? */ |
| 557 |
< |
inFront = (inVec[2] > .0); |
| 558 |
< |
outFront = (outVec[2] > .0); |
| 557 |
> |
inFront = (inVec[2] > 0); |
| 558 |
> |
outFront = (outVec[2] > 0); |
| 559 |
|
/* start with diffuse portion */ |
| 560 |
|
if (inFront & outFront) { |
| 561 |
|
*sv = sd->rLambFront; |
| 596 |
|
if ((inVec == NULL) | (sd == NULL)) |
| 597 |
|
return .0; |
| 598 |
|
/* gather diffuse components */ |
| 599 |
< |
if (inVec[2] > .0) { |
| 599 |
> |
if (inVec[2] > 0) { |
| 600 |
|
hsum = sd->rLambFront.cieY; |
| 601 |
|
rdf = sd->rf; |
| 602 |
|
} else /* !inFront */ { |
| 627 |
|
|
| 628 |
|
/* Sample BSDF direction based on the given random variable */ |
| 629 |
|
SDError |
| 630 |
< |
SDsampBSDF(SDValue *sv, FVECT outVec, const FVECT inVec, |
| 616 |
< |
double randX, int sflags, const SDData *sd) |
| 630 |
> |
SDsampBSDF(SDValue *sv, FVECT ioVec, double randX, int sflags, const SDData *sd) |
| 631 |
|
{ |
| 632 |
|
SDError ec; |
| 633 |
+ |
FVECT inVec; |
| 634 |
|
int inFront; |
| 635 |
|
SDSpectralDF *rdf; |
| 636 |
|
double rdiff; |
| 639 |
|
SDComponent *sdc; |
| 640 |
|
const SDCDst **cdarr = NULL; |
| 641 |
|
/* check arguments */ |
| 642 |
< |
if ((sv == NULL) | (outVec == NULL) | (inVec == NULL) | (sd == NULL) | |
| 643 |
< |
(randX < .0) | (randX >= 1.)) |
| 642 |
> |
if ((sv == NULL) | (ioVec == NULL) | (sd == NULL) | |
| 643 |
> |
(randX < 0) | (randX >= 1.)) |
| 644 |
|
return SDEargument; |
| 645 |
|
/* whose side are we on? */ |
| 646 |
< |
inFront = (inVec[2] > .0); |
| 646 |
> |
VCOPY(inVec, ioVec); |
| 647 |
> |
inFront = (inVec[2] > 0); |
| 648 |
|
/* remember diffuse portions */ |
| 649 |
|
if (inFront) { |
| 650 |
|
*sv = sd->rLambFront; |
| 684 |
|
} |
| 685 |
|
if (sv->cieY <= 1e-7) { /* anything to sample? */ |
| 686 |
|
sv->cieY = .0; |
| 687 |
< |
memset(outVec, 0, 3*sizeof(double)); |
| 687 |
> |
memset(ioVec, 0, 3*sizeof(double)); |
| 688 |
|
return SDEnone; |
| 689 |
|
} |
| 690 |
|
/* scale random variable */ |
| 691 |
|
randX *= sv->cieY; |
| 692 |
|
/* diffuse reflection? */ |
| 693 |
|
if (randX < rdiff) { |
| 694 |
< |
SDdiffuseSamp(outVec, inFront, randX/rdiff); |
| 694 |
> |
SDdiffuseSamp(ioVec, inFront, randX/rdiff); |
| 695 |
|
goto done; |
| 696 |
|
} |
| 697 |
|
randX -= rdiff; |
| 699 |
|
if ((sflags & SDsampDf+SDsampT) == SDsampDf+SDsampT) { |
| 700 |
|
if (randX < sd->tLamb.cieY) { |
| 701 |
|
sv->spec = sd->tLamb.spec; |
| 702 |
< |
SDdiffuseSamp(outVec, !inFront, randX/sd->tLamb.cieY); |
| 702 |
> |
SDdiffuseSamp(ioVec, !inFront, randX/sd->tLamb.cieY); |
| 703 |
|
goto done; |
| 704 |
|
} |
| 705 |
|
randX -= sd->tLamb.cieY; |
| 711 |
|
return SDEinternal; |
| 712 |
|
/* compute sample direction */ |
| 713 |
|
sdc = (i < nr) ? &rdf->comp[i] : &sd->tf->comp[i-nr]; |
| 714 |
< |
ec = (*sdc->func->sampCDist)(outVec, randX/cdarr[i]->cTotal, cdarr[i]); |
| 714 |
> |
ec = (*sdc->func->sampCDist)(ioVec, randX/cdarr[i]->cTotal, cdarr[i]); |
| 715 |
|
if (ec) |
| 716 |
|
return ec; |
| 717 |
|
/* compute color */ |
| 718 |
< |
j = (*sdc->func->getBSDFs)(coef, outVec, inVec, sdc->dist); |
| 718 |
> |
j = (*sdc->func->getBSDFs)(coef, ioVec, inVec, sdc->dist); |
| 719 |
|
if (j <= 0) { |
| 720 |
|
sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error", |
| 721 |
|
sd->name); |
| 742 |
|
if ((vMtx == NULL) | (sNrm == NULL) | (uVec == NULL)) |
| 743 |
|
return SDEargument; |
| 744 |
|
VCOPY(vMtx[2], sNrm); |
| 745 |
< |
if (normalize(vMtx[2]) == .0) |
| 745 |
> |
if (normalize(vMtx[2]) == 0) |
| 746 |
|
return SDEargument; |
| 747 |
|
fcross(vMtx[0], uVec, vMtx[2]); |
| 748 |
< |
if (normalize(vMtx[0]) == .0) |
| 748 |
> |
if (normalize(vMtx[0]) == 0) |
| 749 |
|
return SDEargument; |
| 750 |
|
fcross(vMtx[1], vMtx[2], vMtx[0]); |
| 751 |
|
return SDEnone; |
| 765 |
|
mTmp[0][1] = vMtx[2][1]*vMtx[0][2] - vMtx[2][2]*vMtx[0][1]; |
| 766 |
|
mTmp[0][2] = vMtx[1][2]*vMtx[0][1] - vMtx[1][1]*vMtx[0][2]; |
| 767 |
|
d = vMtx[0][0]*mTmp[0][0] + vMtx[1][0]*mTmp[0][1] + vMtx[2][0]*mTmp[0][2]; |
| 768 |
< |
if (d == .0) { |
| 768 |
> |
if (d == 0) { |
| 769 |
|
strcpy(SDerrorDetail, "Zero determinant in matrix inversion"); |
| 770 |
|
return SDEargument; |
| 771 |
|
} |
| 792 |
|
if (vMtx == NULL) { /* assume they just want to normalize */ |
| 793 |
|
if (resVec != inpVec) |
| 794 |
|
VCOPY(resVec, inpVec); |
| 795 |
< |
return (normalize(resVec) > .0) ? SDEnone : SDEargument; |
| 795 |
> |
return (normalize(resVec) > 0) ? SDEnone : SDEargument; |
| 796 |
|
} |
| 797 |
|
vTmp[0] = DOT(vMtx[0], inpVec); |
| 798 |
|
vTmp[1] = DOT(vMtx[1], inpVec); |
| 799 |
|
vTmp[2] = DOT(vMtx[2], inpVec); |
| 800 |
< |
if (normalize(vTmp) == .0) |
| 800 |
> |
if (normalize(vTmp) == 0) |
| 801 |
|
return SDEargument; |
| 802 |
|
VCOPY(resVec, vTmp); |
| 803 |
|
return SDEnone; |
| 869 |
|
static int |
| 870 |
|
fequal(double a, double b) |
| 871 |
|
{ |
| 872 |
< |
if (b != .0) |
| 872 |
> |
if (b != 0) |
| 873 |
|
a = a/b - 1.; |
| 874 |
|
return((a <= 1e-6) & (a >= -1e-6)); |
| 875 |
|
} |
| 1187 |
|
hemi_total = .0; |
| 1188 |
|
for (i = dp->ninc; i--; ) { |
| 1189 |
|
dom = getBSDF_incohm(dp,i); |
| 1190 |
< |
if (dom <= .0) { |
| 1190 |
> |
if (dom <= 0) { |
| 1191 |
|
error(WARNING, "zero/negative incoming solid angle"); |
| 1192 |
|
continue; |
| 1193 |
|
} |
| 1210 |
|
hemi_total = .0; |
| 1211 |
|
for (o = dp->nout; o--; ) { |
| 1212 |
|
dom = getBSDF_outohm(dp,o); |
| 1213 |
< |
if (dom <= .0) { |
| 1213 |
> |
if (dom <= 0) { |
| 1214 |
|
error(WARNING, "zero/negative outgoing solid angle"); |
| 1215 |
|
continue; |
| 1216 |
|
} |
| 1234 |
|
hemi_total = .0; |
| 1235 |
|
for (o = dp->nout; o--; ) { |
| 1236 |
|
double f = BSDF_value(dp,i,o); |
| 1237 |
< |
if (f >= .0) |
| 1237 |
> |
if (f >= 0) |
| 1238 |
|
hemi_total += f*omega_oarr[o]; |
| 1239 |
|
else { |
| 1240 |
|
nneg += (f < -FTINY); |