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 */ |
284 |
|
unsigned skipmask = 0; |
285 |
|
csiz *= .5; |
286 |
|
for (i = st->ndim; i--; ) |
287 |
< |
if (1<<i & cmask) |
287 |
> |
if (1<<i & cmask) { |
288 |
|
if (pos[i] < cmin[i] + csiz) |
289 |
|
for (n = 1 << st->ndim; n--; ) { |
290 |
|
if (n & 1<<i) |
295 |
|
if (!(n & 1<<i)) |
296 |
|
skipmask |= 1<<n; |
297 |
|
} |
298 |
+ |
} |
299 |
|
for (n = 1 << st->ndim; n--; ) { |
300 |
|
if (1<<n & skipmask) |
301 |
|
continue; |
434 |
|
static float |
435 |
|
SDqueryTre(const SDTre *sdt, const FVECT outVec, const FVECT inVec, double *hc) |
436 |
|
{ |
437 |
< |
FVECT rOutVec; |
438 |
< |
double gridPos[4]; |
437 |
> |
const RREAL *vtmp; |
438 |
> |
FVECT rOutVec; |
439 |
> |
double gridPos[4]; |
440 |
|
|
441 |
|
switch (sdt->sidef) { /* whose side are you on? */ |
442 |
< |
case SD_UFRONT: |
442 |
> |
case SD_FREFL: |
443 |
|
if ((outVec[2] < 0) | (inVec[2] < 0)) |
444 |
|
return -1.; |
445 |
|
break; |
446 |
< |
case SD_UBACK: |
446 |
> |
case SD_BREFL: |
447 |
|
if ((outVec[2] > 0) | (inVec[2] > 0)) |
448 |
|
return -1.; |
449 |
|
break; |
450 |
< |
case SD_XMIT: |
451 |
< |
if ((outVec[2] > 0) == (inVec[2] > 0)) |
450 |
> |
case SD_FXMIT: |
451 |
> |
if (outVec[2] > 0) { |
452 |
> |
if (inVec[2] > 0) |
453 |
> |
return -1.; |
454 |
> |
vtmp = outVec; outVec = inVec; inVec = vtmp; |
455 |
> |
} else if (inVec[2] < 0) |
456 |
|
return -1.; |
457 |
|
break; |
458 |
+ |
case SD_BXMIT: |
459 |
+ |
if (inVec[2] > 0) { |
460 |
+ |
if (outVec[2] > 0) |
461 |
+ |
return -1.; |
462 |
+ |
vtmp = outVec; outVec = inVec; inVec = vtmp; |
463 |
+ |
} else if (outVec[2] < 0) |
464 |
+ |
return -1.; |
465 |
+ |
break; |
466 |
|
default: |
467 |
|
return -1.; |
468 |
|
} |
469 |
|
/* convert vector coordinates */ |
470 |
|
if (sdt->st->ndim == 3) { |
471 |
|
spinvector(rOutVec, outVec, zvec, -atan2(-inVec[1],-inVec[0])); |
472 |
< |
gridPos[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); |
472 |
> |
gridPos[0] = (.5-FTINY) - |
473 |
> |
.5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); |
474 |
|
SDdisk2square(gridPos+1, rOutVec[0], rOutVec[1]); |
475 |
|
} else if (sdt->st->ndim == 4) { |
476 |
|
SDdisk2square(gridPos, -inVec[0], -inVec[1]); |
501 |
|
{ |
502 |
|
SDdistScaffold *sp = (SDdistScaffold *)cptr; |
503 |
|
int wid = csiz*(double)iwmax + .5; |
504 |
+ |
double revcmin[2]; |
505 |
|
bitmask_t bmin[2], bmax[2]; |
506 |
|
|
507 |
< |
cmin += sp->nic; /* skip to output coords */ |
507 |
> |
if (sp->rev) { /* need to reverse sense? */ |
508 |
> |
revcmin[0] = 1. - cmin[0] - csiz; |
509 |
> |
revcmin[1] = 1. - cmin[1] - csiz; |
510 |
> |
cmin = revcmin; |
511 |
> |
} else { |
512 |
> |
cmin += sp->nic; /* else skip to output coords */ |
513 |
> |
} |
514 |
|
if (wid < sp->wmin) /* new minimum width? */ |
515 |
|
sp->wmin = wid; |
516 |
|
if (wid > sp->wmax) /* new maximum? */ |
556 |
|
|
557 |
|
/* Create a new cumulative distribution for the given input direction */ |
558 |
|
static SDTreCDst * |
559 |
< |
make_cdist(const SDTre *sdt, const double *pos) |
559 |
> |
make_cdist(const SDTre *sdt, const double *invec, int rev) |
560 |
|
{ |
561 |
|
SDdistScaffold myScaffold; |
562 |
+ |
double pos[4]; |
563 |
+ |
int cmask; |
564 |
|
SDTreCDst *cd; |
565 |
|
struct outdir_s *sp; |
566 |
|
double scale, cursum; |
569 |
|
myScaffold.wmin = iwmax; |
570 |
|
myScaffold.wmax = 0; |
571 |
|
myScaffold.nic = sdt->st->ndim - 2; |
572 |
+ |
myScaffold.rev = rev; |
573 |
|
myScaffold.alen = 0; |
574 |
|
myScaffold.nall = 512; |
575 |
|
myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) * |
576 |
|
myScaffold.nall); |
577 |
|
if (myScaffold.darr == NULL) |
578 |
|
return NULL; |
579 |
+ |
/* set up traversal */ |
580 |
+ |
cmask = (1<<myScaffold.nic) - 1; |
581 |
+ |
for (i = myScaffold.nic; i--; ) |
582 |
+ |
pos[i+2*rev] = invec[i]; |
583 |
+ |
cmask <<= 2*rev; |
584 |
|
/* grow the distribution */ |
585 |
< |
if (SDtraverseTre(sdt->st, pos, (1<<myScaffold.nic)-1, |
585 |
> |
if (SDtraverseTre(sdt->st, pos, cmask, |
586 |
|
&build_scaffold, &myScaffold) < 0) { |
587 |
|
free(myScaffold.darr); |
588 |
|
return NULL; |
605 |
|
/* record input range */ |
606 |
|
scale = myScaffold.wmin / (double)iwmax; |
607 |
|
for (i = myScaffold.nic; i--; ) { |
608 |
< |
cd->clim[i][0] = floor(pos[i]/scale) * scale; |
608 |
> |
cd->clim[i][0] = floor(pos[i+2*rev]/scale) * scale; |
609 |
|
cd->clim[i][1] = cd->clim[i][0] + scale; |
610 |
|
} |
611 |
|
if (cd->isodist) { /* avoid issue in SDqueryTreProjSA() */ |
614 |
|
} |
615 |
|
cd->max_psa = myScaffold.wmax / (double)iwmax; |
616 |
|
cd->max_psa *= cd->max_psa * M_PI; |
617 |
< |
cd->sidef = sdt->sidef; |
617 |
> |
if (rev) |
618 |
> |
cd->sidef = (sdt->sidef==SD_BXMIT) ? SD_FXMIT : SD_BXMIT; |
619 |
> |
else |
620 |
> |
cd->sidef = sdt->sidef; |
621 |
|
cd->cTotal = 1e-20; /* compute directional total */ |
622 |
|
sp = myScaffold.darr; |
623 |
|
for (i = myScaffold.alen; i--; sp++) |
645 |
|
const SDTre *sdt; |
646 |
|
double inCoord[2]; |
647 |
|
int i; |
648 |
+ |
int mode; |
649 |
|
SDTreCDst *cd, *cdlast; |
650 |
|
/* check arguments */ |
651 |
|
if ((inVec == NULL) | (sdc == NULL) || |
652 |
|
(sdt = (SDTre *)sdc->dist) == NULL) |
653 |
|
return NULL; |
654 |
+ |
switch (mode = sdt->sidef) { /* check direction */ |
655 |
+ |
case SD_FREFL: |
656 |
+ |
if (inVec[2] < 0) |
657 |
+ |
return NULL; |
658 |
+ |
break; |
659 |
+ |
case SD_BREFL: |
660 |
+ |
if (inVec[2] > 0) |
661 |
+ |
return NULL; |
662 |
+ |
break; |
663 |
+ |
case SD_FXMIT: |
664 |
+ |
if (inVec[2] < 0) |
665 |
+ |
mode = SD_BXMIT; |
666 |
+ |
break; |
667 |
+ |
case SD_BXMIT: |
668 |
+ |
if (inVec[2] > 0) |
669 |
+ |
mode = SD_FXMIT; |
670 |
+ |
break; |
671 |
+ |
default: |
672 |
+ |
return NULL; |
673 |
+ |
} |
674 |
|
if (sdt->st->ndim == 3) { /* isotropic BSDF? */ |
675 |
< |
inCoord[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); |
675 |
> |
if (mode != sdt->sidef) /* XXX unhandled reciprocity */ |
676 |
> |
return &SDemptyCD; |
677 |
> |
inCoord[0] = (.5-FTINY) - |
678 |
> |
.5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); |
679 |
|
} else if (sdt->st->ndim == 4) { |
680 |
< |
SDdisk2square(inCoord, -inVec[0], -inVec[1]); |
680 |
> |
if (mode != sdt->sidef) /* use reciprocity? */ |
681 |
> |
SDdisk2square(inCoord, inVec[0], inVec[1]); |
682 |
> |
else |
683 |
> |
SDdisk2square(inCoord, -inVec[0], -inVec[1]); |
684 |
|
} else |
685 |
|
return NULL; /* should be internal error */ |
686 |
|
/* quantize to avoid f.p. errors */ |
689 |
|
cdlast = NULL; /* check for direction in cache list */ |
690 |
|
for (cd = (SDTreCDst *)sdc->cdList; cd != NULL; |
691 |
|
cdlast = cd, cd = cd->next) { |
692 |
+ |
if (cd->sidef != mode) |
693 |
+ |
continue; |
694 |
|
for (i = sdt->st->ndim - 2; i--; ) |
695 |
|
if ((cd->clim[i][0] > inCoord[i]) | |
696 |
|
(inCoord[i] >= cd->clim[i][1])) |
699 |
|
break; /* means we have a match */ |
700 |
|
} |
701 |
|
if (cd == NULL) /* need to create new entry? */ |
702 |
< |
cdlast = cd = make_cdist(sdt, inCoord); |
702 |
> |
cdlast = cd = make_cdist(sdt, inCoord, mode != sdt->sidef); |
703 |
|
if (cdlast != NULL) { /* move entry to head of cache list */ |
704 |
|
cdlast->next = cd->next; |
705 |
|
cd->next = (SDTreCDst *)sdc->cdList; |
731 |
|
} else { |
732 |
|
const SDTreCDst *cd = (const SDTreCDst *)SDgetTreCDist(v1, sdc); |
733 |
|
if (cd == NULL) |
734 |
< |
return SDEmemory; |
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; |
734 |
> |
myPSA[0] = myPSA[1] = 0; |
735 |
> |
else { |
736 |
> |
myPSA[0] = M_PI * (cd->clim[0][1] - cd->clim[0][0]) * |
737 |
> |
(cd->clim[1][1] - cd->clim[1][0]); |
738 |
> |
myPSA[1] = cd->max_psa; |
739 |
> |
} |
740 |
|
} |
741 |
|
switch (qflags) { /* record based on flag settings */ |
742 |
|
case SDqueryVal: |
751 |
|
psa[1] = myPSA[1]; |
752 |
|
/* fall through */ |
753 |
|
case SDqueryMin: |
754 |
< |
if (myPSA[0] < psa[0]) |
754 |
> |
if ((myPSA[0] > 0) & (myPSA[0] < psa[0])) |
755 |
|
psa[0] = myPSA[0]; |
756 |
|
break; |
757 |
|
} |
772 |
|
/* check arguments */ |
773 |
|
if ((ioVec == NULL) | (cd == NULL)) |
774 |
|
return SDEargument; |
775 |
+ |
if (!cd->sidef) |
776 |
+ |
return SDEnone; /* XXX should never happen */ |
777 |
|
if (ioVec[2] > 0) { |
778 |
< |
if (!(cd->sidef & SD_UFRONT)) |
778 |
> |
if ((cd->sidef != SD_FREFL) & (cd->sidef != SD_FXMIT)) |
779 |
|
return SDEargument; |
780 |
< |
} else if (!(cd->sidef & SD_UBACK)) |
780 |
> |
} else if ((cd->sidef != SD_BREFL) & (cd->sidef != SD_BXMIT)) |
781 |
|
return SDEargument; |
782 |
|
/* binary search to find position */ |
783 |
|
ilower = 0; iupper = cd->calen; |
800 |
|
SDsquare2disk(gpos, gpos[0], gpos[1]); |
801 |
|
/* compute Z-coordinate */ |
802 |
|
gpos[2] = 1. - gpos[0]*gpos[0] - gpos[1]*gpos[1]; |
803 |
< |
if (gpos[2] > 0) /* paranoia, I hope */ |
737 |
< |
gpos[2] = sqrt(gpos[2]); |
803 |
> |
gpos[2] = sqrt(gpos[2]*(gpos[2]>0)); |
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]); |
849 |
|
char *svnext; |
850 |
|
|
851 |
|
while (n-- > 0 && (svnext = fskip(*spp)) != NULL) { |
852 |
< |
*v++ = atof(*spp); |
852 |
> |
if ((*v++ = atof(*spp)) < 0) |
853 |
> |
v[-1] = 0; |
854 |
|
*spp = svnext; |
855 |
|
eat_token(spp, ','); |
856 |
|
} |
961 |
|
/* |
962 |
|
* Remember that front and back are reversed from WINDOW 6 orientations |
963 |
|
*/ |
964 |
< |
if (!strcasecmp(sdata, "Transmission")) { |
964 |
> |
if (!strcasecmp(sdata, "Transmission Front")) { |
965 |
> |
if (sd->tb != NULL) |
966 |
> |
SDfreeSpectralDF(sd->tb); |
967 |
> |
if ((sd->tb = SDnewSpectralDF(1)) == NULL) |
968 |
> |
return SDEmemory; |
969 |
> |
df = sd->tb; |
970 |
> |
} else if (!strcasecmp(sdata, "Transmission Back")) { |
971 |
|
if (sd->tf != NULL) |
972 |
|
SDfreeSpectralDF(sd->tf); |
973 |
|
if ((sd->tf = SDnewSpectralDF(1)) == NULL) |
974 |
|
return SDEmemory; |
975 |
|
df = sd->tf; |
976 |
|
} else if (!strcasecmp(sdata, "Reflection Front")) { |
977 |
< |
if (sd->rb != NULL) /* note back-front reversal */ |
977 |
> |
if (sd->rb != NULL) |
978 |
|
SDfreeSpectralDF(sd->rb); |
979 |
|
if ((sd->rb = SDnewSpectralDF(1)) == NULL) |
980 |
|
return SDEmemory; |
981 |
|
df = sd->rb; |
982 |
|
} else if (!strcasecmp(sdata, "Reflection Back")) { |
983 |
< |
if (sd->rf != NULL) /* note front-back reversal */ |
983 |
> |
if (sd->rf != NULL) |
984 |
|
SDfreeSpectralDF(sd->rf); |
985 |
|
if ((sd->rf = SDnewSpectralDF(1)) == NULL) |
986 |
|
return SDEmemory; |
1000 |
|
if (sdt == NULL) |
1001 |
|
return SDEmemory; |
1002 |
|
if (df == sd->rf) |
1003 |
< |
sdt->sidef = SD_UFRONT; |
1003 |
> |
sdt->sidef = SD_FREFL; |
1004 |
|
else if (df == sd->rb) |
1005 |
< |
sdt->sidef = SD_UBACK; |
1006 |
< |
else |
1007 |
< |
sdt->sidef = SD_XMIT; |
1005 |
> |
sdt->sidef = SD_BREFL; |
1006 |
> |
else if (df == sd->tf) |
1007 |
> |
sdt->sidef = SD_FXMIT; |
1008 |
> |
else /* df == sd->tb */ |
1009 |
> |
sdt->sidef = SD_BXMIT; |
1010 |
|
sdt->st = NULL; |
1011 |
|
df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */ |
1012 |
|
df->comp[0].dist = sdt; |
1168 |
|
/* separate diffuse components */ |
1169 |
|
extract_diffuse(&sd->rLambFront, sd->rf); |
1170 |
|
extract_diffuse(&sd->rLambBack, sd->rb); |
1171 |
< |
extract_diffuse(&sd->tLamb, sd->tf); |
1171 |
> |
if (sd->tf != NULL) |
1172 |
> |
extract_diffuse(&sd->tLamb, sd->tf); |
1173 |
> |
if (sd->tb != NULL) |
1174 |
> |
extract_diffuse(&sd->tLamb, sd->tb); |
1175 |
|
/* return success */ |
1176 |
|
return SDEnone; |
1177 |
|
} |