26 |
|
|
27 |
|
/* reference width maximum (1.0) */ |
28 |
|
static const unsigned iwbits = sizeof(unsigned)*4; |
29 |
< |
static const unsigned iwmax = (1<<(sizeof(unsigned)*4))-1; |
29 |
> |
static const unsigned iwmax = 1<<(sizeof(unsigned)*4); |
30 |
|
/* maximum cumulative value */ |
31 |
|
static const unsigned cumlmax = ~0; |
32 |
|
/* constant z-vector */ |
33 |
|
static const FVECT zvec = {.0, .0, 1.}; |
34 |
+ |
/* quantization value */ |
35 |
+ |
static double quantum = 1./256.; |
36 |
|
|
37 |
|
/* Struct used for our distribution-building callback */ |
38 |
|
typedef struct { |
39 |
< |
int nic; /* number of input coordinates */ |
39 |
> |
short nic; /* number of input coordinates */ |
40 |
> |
short rev; /* reversing query */ |
41 |
|
unsigned alen; /* current array length */ |
42 |
|
unsigned nall; /* number of allocated entries */ |
43 |
|
unsigned wmin; /* minimum square size so far */ |
433 |
|
static float |
434 |
|
SDqueryTre(const SDTre *sdt, const FVECT outVec, const FVECT inVec, double *hc) |
435 |
|
{ |
436 |
< |
FVECT rOutVec; |
437 |
< |
double gridPos[4]; |
436 |
> |
const RREAL *vtmp; |
437 |
> |
FVECT rOutVec; |
438 |
> |
double gridPos[4]; |
439 |
|
|
440 |
|
switch (sdt->sidef) { /* whose side are you on? */ |
441 |
< |
case SD_UFRONT: |
441 |
> |
case SD_FREFL: |
442 |
|
if ((outVec[2] < 0) | (inVec[2] < 0)) |
443 |
|
return -1.; |
444 |
|
break; |
445 |
< |
case SD_UBACK: |
445 |
> |
case SD_BREFL: |
446 |
|
if ((outVec[2] > 0) | (inVec[2] > 0)) |
447 |
|
return -1.; |
448 |
|
break; |
449 |
< |
case SD_XMIT: |
450 |
< |
if ((outVec[2] > 0) == (inVec[2] > 0)) |
449 |
> |
case SD_FXMIT: |
450 |
> |
if (outVec[2] > 0) { |
451 |
> |
if (inVec[2] > 0) |
452 |
> |
return -1.; |
453 |
> |
vtmp = outVec; outVec = inVec; inVec = vtmp; |
454 |
> |
} else if (inVec[2] < 0) |
455 |
|
return -1.; |
456 |
|
break; |
457 |
+ |
case SD_BXMIT: |
458 |
+ |
if (inVec[2] > 0) { |
459 |
+ |
if (outVec[2] > 0) |
460 |
+ |
return -1.; |
461 |
+ |
vtmp = outVec; outVec = inVec; inVec = vtmp; |
462 |
+ |
} else if (outVec[2] < 0) |
463 |
+ |
return -1.; |
464 |
+ |
break; |
465 |
|
default: |
466 |
|
return -1.; |
467 |
|
} |
468 |
|
/* convert vector coordinates */ |
469 |
|
if (sdt->st->ndim == 3) { |
470 |
|
spinvector(rOutVec, outVec, zvec, -atan2(-inVec[1],-inVec[0])); |
471 |
< |
gridPos[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); |
471 |
> |
gridPos[0] = (.5-FTINY) - |
472 |
> |
.5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); |
473 |
|
SDdisk2square(gridPos+1, rOutVec[0], rOutVec[1]); |
474 |
|
} else if (sdt->st->ndim == 4) { |
475 |
|
SDdisk2square(gridPos, -inVec[0], -inVec[1]); |
500 |
|
{ |
501 |
|
SDdistScaffold *sp = (SDdistScaffold *)cptr; |
502 |
|
int wid = csiz*(double)iwmax + .5; |
503 |
+ |
double revcmin[2]; |
504 |
|
bitmask_t bmin[2], bmax[2]; |
505 |
|
|
506 |
< |
cmin += sp->nic; /* skip to output coords */ |
506 |
> |
if (sp->rev) { /* need to reverse sense? */ |
507 |
> |
revcmin[0] = 1. - cmin[0] - csiz; |
508 |
> |
revcmin[1] = 1. - cmin[1] - csiz; |
509 |
> |
cmin = revcmin; |
510 |
> |
} else { |
511 |
> |
cmin += sp->nic; /* else skip to output coords */ |
512 |
> |
} |
513 |
|
if (wid < sp->wmin) /* new minimum width? */ |
514 |
|
sp->wmin = wid; |
515 |
|
if (wid > sp->wmax) /* new maximum? */ |
555 |
|
|
556 |
|
/* Create a new cumulative distribution for the given input direction */ |
557 |
|
static SDTreCDst * |
558 |
< |
make_cdist(const SDTre *sdt, const double *pos) |
558 |
> |
make_cdist(const SDTre *sdt, const double *invec, int rev) |
559 |
|
{ |
560 |
|
SDdistScaffold myScaffold; |
561 |
+ |
double pos[4]; |
562 |
+ |
int cmask; |
563 |
|
SDTreCDst *cd; |
564 |
|
struct outdir_s *sp; |
565 |
|
double scale, cursum; |
568 |
|
myScaffold.wmin = iwmax; |
569 |
|
myScaffold.wmax = 0; |
570 |
|
myScaffold.nic = sdt->st->ndim - 2; |
571 |
+ |
myScaffold.rev = rev; |
572 |
|
myScaffold.alen = 0; |
573 |
|
myScaffold.nall = 512; |
574 |
|
myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) * |
575 |
|
myScaffold.nall); |
576 |
|
if (myScaffold.darr == NULL) |
577 |
|
return NULL; |
578 |
+ |
/* set up traversal */ |
579 |
+ |
cmask = (1<<myScaffold.nic) - 1; |
580 |
+ |
for (i = myScaffold.nic; i--; ) |
581 |
+ |
pos[i+2*rev] = invec[i]; |
582 |
+ |
cmask <<= 2*rev; |
583 |
|
/* grow the distribution */ |
584 |
< |
if (SDtraverseTre(sdt->st, pos, (1<<myScaffold.nic)-1, |
584 |
> |
if (SDtraverseTre(sdt->st, pos, cmask, |
585 |
|
&build_scaffold, &myScaffold) < 0) { |
586 |
|
free(myScaffold.darr); |
587 |
|
return NULL; |
604 |
|
/* record input range */ |
605 |
|
scale = myScaffold.wmin / (double)iwmax; |
606 |
|
for (i = myScaffold.nic; i--; ) { |
607 |
< |
cd->clim[i][0] = floor(pos[i]/scale) * scale; |
607 |
> |
cd->clim[i][0] = floor(pos[i+2*rev]/scale) * scale; |
608 |
|
cd->clim[i][1] = cd->clim[i][0] + scale; |
609 |
|
} |
610 |
|
if (cd->isodist) { /* avoid issue in SDqueryTreProjSA() */ |
613 |
|
} |
614 |
|
cd->max_psa = myScaffold.wmax / (double)iwmax; |
615 |
|
cd->max_psa *= cd->max_psa * M_PI; |
616 |
< |
cd->sidef = sdt->sidef; |
616 |
> |
if (rev) |
617 |
> |
cd->sidef = (sdt->sidef==SD_BXMIT) ? SD_FXMIT : SD_BXMIT; |
618 |
> |
else |
619 |
> |
cd->sidef = sdt->sidef; |
620 |
|
cd->cTotal = 1e-20; /* compute directional total */ |
621 |
|
sp = myScaffold.darr; |
622 |
|
for (i = myScaffold.alen; i--; sp++) |
644 |
|
const SDTre *sdt; |
645 |
|
double inCoord[2]; |
646 |
|
int i; |
647 |
+ |
int mode; |
648 |
|
SDTreCDst *cd, *cdlast; |
649 |
|
/* check arguments */ |
650 |
|
if ((inVec == NULL) | (sdc == NULL) || |
651 |
|
(sdt = (SDTre *)sdc->dist) == NULL) |
652 |
|
return NULL; |
653 |
< |
if (sdt->st->ndim == 3) /* isotropic BSDF? */ |
654 |
< |
inCoord[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); |
655 |
< |
else if (sdt->st->ndim == 4) |
656 |
< |
SDdisk2square(inCoord, -inVec[0], -inVec[1]); |
657 |
< |
else |
653 |
> |
switch (mode = sdt->sidef) { /* check direction */ |
654 |
> |
case SD_FREFL: |
655 |
> |
if (inVec[2] < 0) |
656 |
> |
return NULL; |
657 |
> |
break; |
658 |
> |
case SD_BREFL: |
659 |
> |
if (inVec[2] > 0) |
660 |
> |
return NULL; |
661 |
> |
break; |
662 |
> |
case SD_FXMIT: |
663 |
> |
if (inVec[2] < 0) |
664 |
> |
mode = SD_BXMIT; |
665 |
> |
break; |
666 |
> |
case SD_BXMIT: |
667 |
> |
if (inVec[2] > 0) |
668 |
> |
mode = SD_FXMIT; |
669 |
> |
break; |
670 |
> |
default: |
671 |
> |
return NULL; |
672 |
> |
} |
673 |
> |
if (sdt->st->ndim == 3) { /* isotropic BSDF? */ |
674 |
> |
if (mode != sdt->sidef) /* XXX unhandled reciprocity */ |
675 |
> |
return &SDemptyCD; |
676 |
> |
inCoord[0] = (.5-FTINY) - |
677 |
> |
.5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); |
678 |
> |
} else if (sdt->st->ndim == 4) { |
679 |
> |
if (mode != sdt->sidef) /* use reciprocity? */ |
680 |
> |
SDdisk2square(inCoord, inVec[0], inVec[1]); |
681 |
> |
else |
682 |
> |
SDdisk2square(inCoord, -inVec[0], -inVec[1]); |
683 |
> |
} else |
684 |
|
return NULL; /* should be internal error */ |
685 |
+ |
/* quantize to avoid f.p. errors */ |
686 |
+ |
for (i = sdt->st->ndim - 2; i--; ) |
687 |
+ |
inCoord[i] = floor(inCoord[i]/quantum)*quantum + .5*quantum; |
688 |
|
cdlast = NULL; /* check for direction in cache list */ |
689 |
|
for (cd = (SDTreCDst *)sdc->cdList; cd != NULL; |
690 |
< |
cdlast = cd, cd = (SDTreCDst *)cd->next) { |
690 |
> |
cdlast = cd, cd = cd->next) { |
691 |
> |
if (cd->sidef != mode) |
692 |
> |
continue; |
693 |
|
for (i = sdt->st->ndim - 2; i--; ) |
694 |
|
if ((cd->clim[i][0] > inCoord[i]) | |
695 |
|
(inCoord[i] >= cd->clim[i][1])) |
698 |
|
break; /* means we have a match */ |
699 |
|
} |
700 |
|
if (cd == NULL) /* need to create new entry? */ |
701 |
< |
cdlast = cd = make_cdist(sdt, inCoord); |
701 |
> |
cdlast = cd = make_cdist(sdt, inCoord, mode != sdt->sidef); |
702 |
|
if (cdlast != NULL) { /* move entry to head of cache list */ |
703 |
|
cdlast->next = cd->next; |
704 |
< |
cd->next = sdc->cdList; |
704 |
> |
cd->next = (SDTreCDst *)sdc->cdList; |
705 |
|
sdc->cdList = (SDCDst *)cd; |
706 |
|
} |
707 |
|
return (SDCDst *)cd; /* ready to go */ |
730 |
|
} else { |
731 |
|
const SDTreCDst *cd = (const SDTreCDst *)SDgetTreCDist(v1, sdc); |
732 |
|
if (cd == NULL) |
733 |
< |
return SDEmemory; |
734 |
< |
myPSA[0] = M_PI * (cd->clim[0][1] - cd->clim[0][0]) * |
735 |
< |
(cd->clim[1][1] - cd->clim[1][0]); |
736 |
< |
myPSA[1] = cd->max_psa; |
733 |
> |
myPSA[0] = myPSA[1] = 0; |
734 |
> |
else { |
735 |
> |
myPSA[0] = M_PI * (cd->clim[0][1] - cd->clim[0][0]) * |
736 |
> |
(cd->clim[1][1] - cd->clim[1][0]); |
737 |
> |
myPSA[1] = cd->max_psa; |
738 |
> |
} |
739 |
|
} |
740 |
|
switch (qflags) { /* record based on flag settings */ |
741 |
|
case SDqueryVal: |
750 |
|
psa[1] = myPSA[1]; |
751 |
|
/* fall through */ |
752 |
|
case SDqueryMin: |
753 |
< |
if (myPSA[0] < psa[0]) |
753 |
> |
if ((myPSA[0] > 0) & (myPSA[0] < psa[0])) |
754 |
|
psa[0] = myPSA[0]; |
755 |
|
break; |
756 |
|
} |
771 |
|
/* check arguments */ |
772 |
|
if ((ioVec == NULL) | (cd == NULL)) |
773 |
|
return SDEargument; |
774 |
+ |
if (!cd->sidef) |
775 |
+ |
return SDEnone; /* XXX should never happen */ |
776 |
|
if (ioVec[2] > 0) { |
777 |
< |
if (!(cd->sidef & SD_UFRONT)) |
777 |
> |
if ((cd->sidef != SD_FREFL) & (cd->sidef != SD_FXMIT)) |
778 |
|
return SDEargument; |
779 |
< |
} else if (!(cd->sidef & SD_UBACK)) |
779 |
> |
} else if ((cd->sidef != SD_BREFL) & (cd->sidef != SD_BXMIT)) |
780 |
|
return SDEargument; |
781 |
|
/* binary search to find position */ |
782 |
|
ilower = 0; iupper = cd->calen; |
783 |
|
while ((i = (iupper + ilower) >> 1) != ilower) |
784 |
< |
if ((long)target >= (long)cd->carr[i].cuml) |
784 |
> |
if (target >= cd->carr[i].cuml) |
785 |
|
ilower = i; |
786 |
|
else |
787 |
|
iupper = i; |
802 |
|
if (gpos[2] > 0) /* paranoia, I hope */ |
803 |
|
gpos[2] = sqrt(gpos[2]); |
804 |
|
/* emit from back? */ |
805 |
< |
if (ioVec[2] > 0 ^ cd->sidef != SD_XMIT) |
805 |
> |
if ((cd->sidef == SD_BREFL) | (cd->sidef == SD_FXMIT)) |
806 |
|
gpos[2] = -gpos[2]; |
807 |
|
if (cd->isodist) { /* rotate isotropic result */ |
808 |
|
rotangle = atan2(-ioVec[1],-ioVec[0]); |
912 |
|
double stepWidth, dhemi, bmin[4], bmax[4]; |
913 |
|
|
914 |
|
stepWidth = SDsmallestLeaf(st); |
915 |
+ |
if (quantum > stepWidth) /* adjust quantization factor */ |
916 |
+ |
quantum = stepWidth; |
917 |
|
df->minProjSA = M_PI*stepWidth*stepWidth; |
918 |
|
if (stepWidth < .03125) |
919 |
|
stepWidth = .03125; /* 1/32 resolution good enough */ |
960 |
|
/* |
961 |
|
* Remember that front and back are reversed from WINDOW 6 orientations |
962 |
|
*/ |
963 |
< |
if (!strcasecmp(sdata, "Transmission")) { |
963 |
> |
if (!strcasecmp(sdata, "Transmission Front")) { |
964 |
> |
if (sd->tb != NULL) |
965 |
> |
SDfreeSpectralDF(sd->tb); |
966 |
> |
if ((sd->tb = SDnewSpectralDF(1)) == NULL) |
967 |
> |
return SDEmemory; |
968 |
> |
df = sd->tb; |
969 |
> |
} else if (!strcasecmp(sdata, "Transmission Back")) { |
970 |
|
if (sd->tf != NULL) |
971 |
|
SDfreeSpectralDF(sd->tf); |
972 |
|
if ((sd->tf = SDnewSpectralDF(1)) == NULL) |
973 |
|
return SDEmemory; |
974 |
|
df = sd->tf; |
975 |
|
} else if (!strcasecmp(sdata, "Reflection Front")) { |
976 |
< |
if (sd->rb != NULL) /* note back-front reversal */ |
976 |
> |
if (sd->rb != NULL) |
977 |
|
SDfreeSpectralDF(sd->rb); |
978 |
|
if ((sd->rb = SDnewSpectralDF(1)) == NULL) |
979 |
|
return SDEmemory; |
980 |
|
df = sd->rb; |
981 |
|
} else if (!strcasecmp(sdata, "Reflection Back")) { |
982 |
< |
if (sd->rf != NULL) /* note front-back reversal */ |
982 |
> |
if (sd->rf != NULL) |
983 |
|
SDfreeSpectralDF(sd->rf); |
984 |
|
if ((sd->rf = SDnewSpectralDF(1)) == NULL) |
985 |
|
return SDEmemory; |
999 |
|
if (sdt == NULL) |
1000 |
|
return SDEmemory; |
1001 |
|
if (df == sd->rf) |
1002 |
< |
sdt->sidef = SD_UFRONT; |
1002 |
> |
sdt->sidef = SD_FREFL; |
1003 |
|
else if (df == sd->rb) |
1004 |
< |
sdt->sidef = SD_UBACK; |
1005 |
< |
else |
1006 |
< |
sdt->sidef = SD_XMIT; |
1004 |
> |
sdt->sidef = SD_BREFL; |
1005 |
> |
else if (df == sd->tf) |
1006 |
> |
sdt->sidef = SD_FXMIT; |
1007 |
> |
else /* df == sd->tb */ |
1008 |
> |
sdt->sidef = SD_BXMIT; |
1009 |
|
sdt->st = NULL; |
1010 |
|
df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */ |
1011 |
|
df->comp[0].dist = sdt; |
1167 |
|
/* separate diffuse components */ |
1168 |
|
extract_diffuse(&sd->rLambFront, sd->rf); |
1169 |
|
extract_diffuse(&sd->rLambBack, sd->rb); |
1170 |
< |
extract_diffuse(&sd->tLamb, sd->tf); |
1170 |
> |
if (sd->tf != NULL) |
1171 |
> |
extract_diffuse(&sd->tLamb, sd->tf); |
1172 |
> |
if (sd->tb != NULL) |
1173 |
> |
extract_diffuse(&sd->tLamb, sd->tb); |
1174 |
|
/* return success */ |
1175 |
|
return SDEnone; |
1176 |
|
} |