ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/bsdf_t.c
(Generate patch)

Comparing ray/src/common/bsdf_t.c (file contents):
Revision 3.15 by greg, Fri Jun 3 18:12:58 2011 UTC vs.
Revision 3.54 by greg, Tue Jan 25 01:34:20 2022 UTC

# Line 10 | Line 10 | static const char RCSid[] = "$Id$";
10   *
11   */
12  
13 + #define _USE_MATH_DEFINES
14   #include "rtio.h"
15   #include <stdlib.h>
16   #include <math.h>
# Line 20 | Line 21 | static const char RCSid[] = "$Id$";
21   #include "hilbert.h"
22  
23   /* Callback function type for SDtraverseTre() */
24 < typedef int     SDtreCallback(float val, const double *cmin,
25 <                                        double csiz, void *cptr);
25 <
24 > typedef int     SDtreCallback(float val, const double *cmin, double csiz,
25 >                                                void *cptr);
26                                          /* reference width maximum (1.0) */
27   static const unsigned   iwbits = sizeof(unsigned)*4;
28 < static const unsigned   iwmax = (1<<(sizeof(unsigned)*4))-1;
28 > static const unsigned   iwmax = 1<<(sizeof(unsigned)*4);
29                                          /* maximum cumulative value */
30   static const unsigned   cumlmax = ~0;
31                                          /* constant z-vector */
32   static const FVECT      zvec = {.0, .0, 1.};
33 +                                        /* quantization value */
34 + static double           quantum = 1./256.;
35 +                                        /* our RGB primaries */
36 + static C_COLOR          tt_RGB_prim[3];
37 + static float            tt_RGB_coef[3];
38  
39 + static const double     czero[SD_MAXDIM];
40 +
41 + enum {tt_Y, tt_u, tt_v};                /* tree components (tt_Y==0) */
42 +
43   /* Struct used for our distribution-building callback */
44   typedef struct {
45 <        int             nic;            /* number of input coordinates */
45 >        short           nic;            /* number of input coordinates */
46 >        short           rev;            /* reversing query */
47          unsigned        alen;           /* current array length */
48          unsigned        nall;           /* number of allocated entries */
49          unsigned        wmin;           /* minimum square size so far */
# Line 104 | Line 114 | SDFreeBTre(void *p)
114  
115          if (sdt == NULL)
116                  return;
117 <        SDfreeTre(sdt->st);
117 >        SDfreeTre(sdt->stc[tt_Y]);
118 >        SDfreeTre(sdt->stc[tt_u]);
119 >        SDfreeTre(sdt->stc[tt_v]);
120          free(sdt);
121   }
122  
# Line 167 | Line 179 | SDsimplifyTre(SDNode *st)
179          return st;
180   }
181  
182 + /* Assign the given voxel in tree (produces no grid nodes) */
183 + static SDNode *
184 + SDsetVoxel(SDNode *sroot, int nd, const double *tmin, const double tsiz, float val)
185 + {
186 +        double  ctrk[SD_MAXDIM];
187 +        double  csiz = 1.;
188 +        SDNode  *st;
189 +        int     i, n;
190 +                                        /* check arguments */
191 +        for (i = nd; i-- > 0; )
192 +                if ((tmin[i] < .0) | (tmin[i] >= 1.-FTINY))
193 +                        break;
194 +        if ((i >= 0) | (nd <= 0) | (tsiz <= FTINY) | (tsiz > 1.+FTINY) |
195 +                        (sroot != NULL && sroot->ndim != nd)) {
196 +                SDfreeTre(sroot);
197 +                return NULL;
198 +        }
199 +        if (tsiz >= 1.-FTINY) {         /* special case when tree is a leaf */
200 +                SDfreeTre(sroot);
201 +                if ((sroot = SDnewNode(nd, 0)) != NULL)
202 +                        sroot->u.v[0] = val;
203 +                return sroot;
204 +        }
205 +                                        /* make sure we have branching root */
206 +        if (sroot != NULL && sroot->log2GR >= 0) {
207 +                SDfreeTre(sroot); sroot = NULL;
208 +        }
209 +        if (sroot == NULL && (sroot = SDnewNode(nd, -1)) == NULL)
210 +                return NULL;
211 +        st = sroot;                     /* climb/grow tree */
212 +        memset(ctrk, 0, sizeof(ctrk));
213 +        for ( ; ; ) {
214 +                csiz *= .5;             /* find appropriate branch */
215 +                n = 0;
216 +                for (i = nd; i--; )
217 +                        if (ctrk[i]+csiz <= tmin[i]+FTINY) {
218 +                                ctrk[i] += csiz;
219 +                                n |= 1 << i;
220 +                        }
221 +                                        /* reached desired voxel? */
222 +                if (csiz <= tsiz+FTINY) {
223 +                        SDfreeTre(st->u.t[n]);
224 +                        st = st->u.t[n] = SDnewNode(nd, 0);
225 +                        break;
226 +                }
227 +                                        /* else grow tree as needed */
228 +                if (st->u.t[n] != NULL && st->u.t[n]->log2GR >= 0) {
229 +                        SDfreeTre(st->u.t[n]); st->u.t[n] = NULL;
230 +                }
231 +                if (st->u.t[n] == NULL)
232 +                        st->u.t[n] = SDnewNode(nd, -1);
233 +                if ((st = st->u.t[n]) == NULL)
234 +                        break;
235 +        }
236 +        if (st == NULL) {
237 +                SDfreeTre(sroot);
238 +                return NULL;
239 +        }
240 +        st->u.v[0] = val;               /* assign leaf and return root */
241 +        return sroot;
242 + }
243 +
244   /* Find smallest leaf in tree */
245   static double
246   SDsmallestLeaf(const SDNode *st)
# Line 275 | Line 349 | SDdotravTre(const SDNode *st, const double *pos, int c
349          int     rv, rval = 0;
350          double  bmin[SD_MAXDIM];
351          int     i, n;
352 +                                        /* paranoia */
353 +        if (st == NULL)
354 +                return 0;
355                                          /* in branches? */
356          if (st->log2GR < 0) {
357                  unsigned        skipmask = 0;
358                  csiz *= .5;
359                  for (i = st->ndim; i--; )
360 <                        if (1<<i & cmask)
360 >                        if (1<<i & cmask) {
361                                  if (pos[i] < cmin[i] + csiz)
362                                          for (n = 1 << st->ndim; n--; ) {
363                                                  if (n & 1<<i)
# Line 291 | Line 368 | SDdotravTre(const SDNode *st, const double *pos, int c
368                                                  if (!(n & 1<<i))
369                                                          skipmask |= 1<<n;
370                                          }
371 +                        }
372                  for (n = 1 << st->ndim; n--; ) {
373                          if (1<<n & skipmask)
374                                  continue;
# Line 299 | Line 377 | SDdotravTre(const SDNode *st, const double *pos, int c
377                                          bmin[i] = cmin[i] + csiz;
378                                  else
379                                          bmin[i] = cmin[i];
302                        for (i = SD_MAXDIM; i-- > st->ndim; )
303                                bmin[i] = .0;
380  
381                          rval += rv = SDdotravTre(st->u.t[n], pos, cmask,
382                                                          cf, cptr, bmin, csiz);
# Line 326 | Line 402 | SDdotravTre(const SDNode *st, const double *pos, int c
402                                  clim[i][0] = 0;
403                                  clim[i][1] = 1 << st->log2GR;
404                          }
329                                        /* fill in unused dimensions */
330                for (i = SD_MAXDIM; i-- > st->ndim; ) {
331                        clim[i][0] = 0; clim[i][1] = 1;
332                }
405   #if (SD_MAXDIM == 4)
406                  bmin[0] = cmin[0] + csiz*clim[0][0];
407                  for (cpos[0] = clim[0][0]; cpos[0] < clim[0][1]; cpos[0]++) {
408                      bmin[1] = cmin[1] + csiz*clim[1][0];
409                      for (cpos[1] = clim[1][0]; cpos[1] < clim[1][1]; cpos[1]++) {
410                          bmin[2] = cmin[2] + csiz*clim[2][0];
411 <                        for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) {
412 <                            bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]);
411 >                        if (st->ndim == 3) {
412 >                            cpos[2] = clim[2][0];
413                              n = cpos[0];
414 <                            for (i = 1; i < st->ndim; i++)
414 >                            for (i = 1; i < 3; i++)
415                                  n = (n << st->log2GR) + cpos[i];
416 <                            for ( ; cpos[3] < clim[3][1]; cpos[3]++) {
416 >                            for ( ; cpos[2] < clim[2][1]; cpos[2]++) {
417                                  rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr);
418                                  if (rv < 0)
419                                      return rv;
420 <                                bmin[3] += csiz;
420 >                                bmin[2] += csiz;
421                              }
422 <                            bmin[2] += csiz;
422 >                        } else {
423 >                            for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) {
424 >                                bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]);
425 >                                n = cpos[0];
426 >                                for (i = 1; i < 4; i++)
427 >                                    n = (n << st->log2GR) + cpos[i];
428 >                                for ( ; cpos[3] < clim[3][1]; cpos[3]++) {
429 >                                    rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr);
430 >                                    if (rv < 0)
431 >                                        return rv;
432 >                                    bmin[3] += csiz;
433 >                                }
434 >                                bmin[2] += csiz;
435 >                            }
436                          }
437                          bmin[1] += csiz;
438                      }
# Line 365 | Line 450 | static int
450   SDtraverseTre(const SDNode *st, const double *pos, int cmask,
451                                  SDtreCallback *cf, void *cptr)
452   {
368        static double   czero[SD_MAXDIM];
453          int             i;
454                                          /* check arguments */
455          if ((st == NULL) | (cf == NULL))
# Line 390 | Line 474 | SDlookupTre(const SDNode *st, const double *pos, doubl
474                          hcube[i] = .0;
475          }
476                                          /* climb the tree */
477 <        while (st->log2GR < 0) {
477 >        while (st != NULL && st->log2GR < 0) {
478                  n = 0;                  /* move to appropriate branch */
479                  if (hcube) hcube[st->ndim] *= .5;
480                  for (i = st->ndim; i--; ) {
# Line 403 | Line 487 | SDlookupTre(const SDNode *st, const double *pos, doubl
487                  st = st->u.t[n];        /* avoids tail recursion */
488                  pos = spos;
489          }
490 +        if (st == NULL)                 /* should never happen? */
491 +                return .0;
492          if (st->log2GR == 0)            /* short cut */
493                  return st->u.v[0];
494          n = t = 0;                      /* find grid array index */
# Line 418 | Line 504 | SDlookupTre(const SDNode *st, const double *pos, doubl
504          return st->u.v[n];              /* no interpolation */
505   }
506  
507 + /* Convert CIE (Y,u',v') color to our RGB */
508 + static void
509 + SDyuv2rgb(double yval, double uprime, double vprime, float rgb[3])
510 + {
511 +        const double    dfact = 1./(6.*uprime - 16.*vprime + 12.);
512 +        C_COLOR         cxy;
513 +
514 +        c_cset(&cxy, 9.*uprime*dfact, 4.*vprime*dfact);
515 +        c_toSharpRGB(&cxy, yval, rgb);
516 + }
517 +
518 + static double
519 + pfrac(double x)
520 + {
521 +        return( x - (int)x );
522 + }
523 +
524   /* Query BSDF value and sample hypercube for the given vectors */
525 < static float
526 < SDqueryTre(const SDTre *sdt, const FVECT outVec, const FVECT inVec, double *hc)
525 > static int
526 > SDqueryTre(const SDTre *sdt, float *coef,
527 >                const FVECT inVec, const FVECT outVec, double *hc)
528   {
529 <        FVECT                   rOutVec;
530 <        double                  gridPos[4];
529 >        const RREAL     *vtmp;
530 >        double          hcube[SD_MAXDIM+1];
531 >        float           yval;
532 >        FVECT           rOutVec;
533 >        RREAL           gridPos[4];
534 >        double          d;
535 >        int             i;
536  
537 +        if (sdt->stc[tt_Y] == NULL)     /* paranoia, I hope */
538 +                return 0;
539 +
540          switch (sdt->sidef) {           /* whose side are you on? */
541 <        case SD_UFRONT:
541 >        case SD_FREFL:
542                  if ((outVec[2] < 0) | (inVec[2] < 0))
543 <                        return -1.;
543 >                        return 0;
544                  break;
545 <        case SD_UBACK:
545 >        case SD_BREFL:
546                  if ((outVec[2] > 0) | (inVec[2] > 0))
547 <                        return -1.;
547 >                        return 0;
548                  break;
549 <        case SD_XMIT:
550 <                if ((outVec[2] > 0) == (inVec[2] > 0))
551 <                        return -1.;
549 >        case SD_FXMIT:
550 >                if (outVec[2] > 0) {
551 >                        if (inVec[2] > 0)
552 >                                return 0;
553 >                        vtmp = outVec; outVec = inVec; inVec = vtmp;
554 >                } else if (inVec[2] < 0)
555 >                        return 0;
556                  break;
557 +        case SD_BXMIT:
558 +                if (inVec[2] > 0) {
559 +                        if (outVec[2] > 0)
560 +                                return 0;
561 +                        vtmp = outVec; outVec = inVec; inVec = vtmp;
562 +                } else if (outVec[2] < 0)
563 +                        return 0;
564 +                break;
565          default:
566 <                return -1.;
566 >                return 0;
567          }
568                                          /* convert vector coordinates */
569 <        if (sdt->st->ndim == 3) {
569 >        if (sdt->stc[tt_Y]->ndim == 3) {
570                  spinvector(rOutVec, outVec, zvec, -atan2(-inVec[1],-inVec[0]));
571 <                gridPos[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
572 <                SDdisk2square(gridPos+1, rOutVec[0], rOutVec[1]);
573 <        } else if (sdt->st->ndim == 4) {
574 <                SDdisk2square(gridPos, -inVec[0], -inVec[1]);
575 <                SDdisk2square(gridPos+2, outVec[0], outVec[1]);
571 >                gridPos[0] = (.5-FTINY) -
572 >                                .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
573 >                disk2square(gridPos+1, rOutVec[0], rOutVec[1]);
574 >        } else if (sdt->stc[tt_Y]->ndim == 4) {
575 >                disk2square(gridPos, -inVec[0], -inVec[1]);
576 >                disk2square(gridPos+2, outVec[0], outVec[1]);
577          } else
578 <                return -1.;             /* should be internal error */
578 >                return 0;               /* should be internal error */
579  
580 <        return SDlookupTre(sdt->st, gridPos, hc);
580 >        if (hc == NULL) hc = hcube;     /* get BSDF value */
581 >        yval = SDlookupTre(sdt->stc[tt_Y], gridPos, hc);
582 >        if (coef == NULL)               /* just getting hypercube? */
583 >                return 1;
584 >        d = 0;                          /* position-specific perturbation */
585 >        for (i = sdt->stc[tt_Y]->ndim; i--; )
586 >                d += pfrac((2<<i)/(hc[i]+.01687)) - .5;
587 >        yval *= 1. + 1e-4*d;            /* assumes tolerance is > 0.04% */
588 >        if (sdt->stc[tt_u] == NULL || sdt->stc[tt_v] == NULL) {
589 >                *coef = yval;
590 >                return 1;               /* no color */
591 >        }
592 >                                        /* else decode color */
593 >        SDyuv2rgb(yval, SDlookupTre(sdt->stc[tt_u], gridPos, NULL),
594 >                        SDlookupTre(sdt->stc[tt_v], gridPos, NULL), coef);
595 >        coef[0] *= tt_RGB_coef[0];
596 >        coef[1] *= tt_RGB_coef[1];
597 >        coef[2] *= tt_RGB_coef[2];
598 >        return 3;
599   }
600  
601   /* Compute non-diffuse component for variable-resolution BSDF */
# Line 465 | Line 608 | SDgetTreBSDF(float coef[SDmaxCh], const FVECT outVec,
608                                  || sdc->dist == NULL)
609                  return 0;
610                                          /* get nearest BSDF value */
611 <        coef[0] = SDqueryTre((SDTre *)sdc->dist, outVec, inVec, NULL);
469 <        return (coef[0] >= 0);          /* monochromatic for now */
611 >        return SDqueryTre((SDTre *)sdc->dist, coef, outVec, inVec, NULL);
612   }
613  
614   /* Callback to build cumulative distribution using SDtraverseTre() */
# Line 475 | Line 617 | build_scaffold(float val, const double *cmin, double c
617   {
618          SDdistScaffold  *sp = (SDdistScaffold *)cptr;
619          int             wid = csiz*(double)iwmax + .5;
620 +        double          revcmin[2];
621          bitmask_t       bmin[2], bmax[2];
622  
623 <        cmin += sp->nic;                /* skip to output coords */
623 >        if (sp->rev) {                  /* need to reverse sense? */
624 >                revcmin[0] = 1. - cmin[0] - csiz;
625 >                revcmin[1] = 1. - cmin[1] - csiz;
626 >                cmin = revcmin;
627 >        } else {
628 >                cmin += sp->nic;        /* else skip to output coords */
629 >        }
630          if (wid < sp->wmin)             /* new minimum width? */
631                  sp->wmin = wid;
632          if (wid > sp->wmax)             /* new maximum? */
633                  sp->wmax = wid;
634          if (sp->alen >= sp->nall) {     /* need more space? */
635                  struct outdir_s *ndarr;
636 <                sp->nall += 1024;
636 >                sp->nall = (int)(1.5*sp->nall) + 256;
637                  ndarr = (struct outdir_s *)realloc(sp->darr,
638                                          sizeof(struct outdir_s)*sp->nall);
639                  if (ndarr == NULL) {
# Line 523 | Line 672 | sscmp(const void *p1, const void *p2)
672  
673   /* Create a new cumulative distribution for the given input direction */
674   static SDTreCDst *
675 < make_cdist(const SDTre *sdt, const double *pos)
675 > make_cdist(const SDTre *sdt, const double *invec, int rev)
676   {
677          SDdistScaffold  myScaffold;
678 +        double          pos[4];
679 +        int             cmask;
680          SDTreCDst       *cd;
681          struct outdir_s *sp;
682          double          scale, cursum;
# Line 533 | Line 684 | make_cdist(const SDTre *sdt, const double *pos)
684                                          /* initialize scaffold */
685          myScaffold.wmin = iwmax;
686          myScaffold.wmax = 0;
687 <        myScaffold.nic = sdt->st->ndim - 2;
687 >        myScaffold.nic = sdt->stc[tt_Y]->ndim - 2;
688 >        myScaffold.rev = rev;
689          myScaffold.alen = 0;
690          myScaffold.nall = 512;
691          myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) *
692                                                          myScaffold.nall);
693          if (myScaffold.darr == NULL)
694                  return NULL;
695 +                                        /* set up traversal */
696 +        cmask = (1<<myScaffold.nic) - 1;
697 +        for (i = myScaffold.nic; i--; )
698 +                        pos[i+2*rev] = invec[i];
699 +        cmask <<= 2*rev;
700                                          /* grow the distribution */
701 <        if (SDtraverseTre(sdt->st, pos, (1<<myScaffold.nic)-1,
702 <                                &build_scaffold, &myScaffold) < 0) {
701 >        if (SDtraverseTre(sdt->stc[tt_Y], pos, cmask,
702 >                                build_scaffold, &myScaffold) < 0) {
703                  free(myScaffold.darr);
704                  return NULL;
705          }
# Line 559 | Line 716 | make_cdist(const SDTre *sdt, const double *pos)
716          cd->isodist = (myScaffold.nic == 1);
717                                          /* sort the distribution */
718          qsort(myScaffold.darr, cd->calen = myScaffold.alen,
719 <                                sizeof(struct outdir_s), &sscmp);
719 >                                sizeof(struct outdir_s), sscmp);
720  
721                                          /* record input range */
722          scale = myScaffold.wmin / (double)iwmax;
723          for (i = myScaffold.nic; i--; ) {
724 <                cd->clim[i][0] = floor(pos[i]/scale) * scale;
724 >                cd->clim[i][0] = floor(pos[i+2*rev]/scale) * scale;
725                  cd->clim[i][1] = cd->clim[i][0] + scale;
726          }
727          if (cd->isodist) {              /* avoid issue in SDqueryTreProjSA() */
# Line 573 | Line 730 | make_cdist(const SDTre *sdt, const double *pos)
730          }
731          cd->max_psa = myScaffold.wmax / (double)iwmax;
732          cd->max_psa *= cd->max_psa * M_PI;
733 <        cd->sidef = sdt->sidef;
733 >        if (rev)
734 >                cd->sidef = (sdt->sidef==SD_BXMIT) ? SD_FXMIT : SD_BXMIT;
735 >        else
736 >                cd->sidef = sdt->sidef;
737          cd->cTotal = 1e-20;             /* compute directional total */
738          sp = myScaffold.darr;
739          for (i = myScaffold.alen; i--; sp++)
# Line 598 | Line 758 | make_cdist(const SDTre *sdt, const double *pos)
758   const SDCDst *
759   SDgetTreCDist(const FVECT inVec, SDComponent *sdc)
760   {
761 +        unsigned long   cacheLeft = SDmaxCache;
762          const SDTre     *sdt;
763 <        double          inCoord[2];
603 <        int             vflags;
763 >        RREAL           inCoord[2];
764          int             i;
765 <        SDTreCDst       *cd, *cdlast;
765 >        int             mode;
766 >        SDTreCDst       *cd, *cdlast, *cdlimit;
767                                          /* check arguments */
768          if ((inVec == NULL) | (sdc == NULL) ||
769                          (sdt = (SDTre *)sdc->dist) == NULL)
770                  return NULL;
771 <        if (sdt->st->ndim == 3)         /* isotropic BSDF? */
772 <                inCoord[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
773 <        else if (sdt->st->ndim == 4)
774 <                SDdisk2square(inCoord, -inVec[0], -inVec[1]);
775 <        else
771 >        switch (mode = sdt->sidef) {    /* check direction */
772 >        case SD_FREFL:
773 >                if (inVec[2] < 0)
774 >                        return NULL;
775 >                break;
776 >        case SD_BREFL:
777 >                if (inVec[2] > 0)
778 >                        return NULL;
779 >                break;
780 >        case SD_FXMIT:
781 >                if (inVec[2] < 0)
782 >                        mode = SD_BXMIT;
783 >                break;
784 >        case SD_BXMIT:
785 >                if (inVec[2] > 0)
786 >                        mode = SD_FXMIT;
787 >                break;
788 >        default:
789 >                return NULL;
790 >        }
791 >        if (sdt->stc[tt_Y]->ndim == 3) {        /* isotropic BSDF? */
792 >                if (mode != sdt->sidef) /* XXX unhandled reciprocity */
793 >                        return &SDemptyCD;
794 >                inCoord[0] = (.5-FTINY) -
795 >                                .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
796 >        } else if (sdt->stc[tt_Y]->ndim == 4) {
797 >                if (mode != sdt->sidef) /* use reciprocity? */
798 >                        disk2square(inCoord, inVec[0], inVec[1]);
799 >                else
800 >                        disk2square(inCoord, -inVec[0], -inVec[1]);
801 >        } else
802                  return NULL;            /* should be internal error */
803 <        cdlast = NULL;                  /* check for direction in cache list */
803 >                                        /* quantize to avoid f.p. errors */
804 >        for (i = sdt->stc[tt_Y]->ndim - 2; i--; )
805 >                inCoord[i] = floor(inCoord[i]/quantum)*quantum + .5*quantum;
806 >        cdlast = cdlimit = NULL;        /* check for direction in cache list */
807 >        /* PLACE MUTEX LOCK HERE FOR THREAD-SAFE */
808          for (cd = (SDTreCDst *)sdc->cdList; cd != NULL;
809 <                                cdlast = cd, cd = (SDTreCDst *)cd->next) {
810 <                for (i = sdt->st->ndim - 2; i--; )
809 >                                        cdlast = cd, cd = cd->next) {
810 >                if (cacheLeft) {        /* check cache size limit */
811 >                        long    csiz = sizeof(SDTreCDst) +
812 >                                        sizeof(cd->carr[0])*cd->calen;
813 >                        if (cacheLeft > csiz)
814 >                                cacheLeft -= csiz;
815 >                        else {
816 >                                cdlimit = cdlast;
817 >                                cacheLeft = 0;
818 >                        }
819 >                }
820 >                if (cd->sidef != mode)
821 >                        continue;
822 >                for (i = sdt->stc[tt_Y]->ndim - 2; i--; )
823                          if ((cd->clim[i][0] > inCoord[i]) |
824                                          (inCoord[i] >= cd->clim[i][1]))
825                                  break;
826                  if (i < 0)
827                          break;          /* means we have a match */
828          }
829 <        if (cd == NULL)                 /* need to create new entry? */
830 <                cdlast = cd = make_cdist(sdt, inCoord);
829 >        if (cd == NULL) {               /* need to create new entry? */
830 >                if (cdlimit != NULL)    /* exceeded cache size limit? */
831 >                        while ((cd = cdlimit->next) != NULL) {
832 >                                cdlimit->next = cd->next;
833 >                                free(cd);
834 >                        }
835 >                cdlast = cd = make_cdist(sdt, inCoord, mode != sdt->sidef);
836 >        }
837          if (cdlast != NULL) {           /* move entry to head of cache list */
838                  cdlast->next = cd->next;
839 <                cd->next = sdc->cdList;
839 >                cd->next = (SDTreCDst *)sdc->cdList;
840                  sdc->cdList = (SDCDst *)cd;
841          }
842 +        /* END MUTEX LOCK */
843          return (SDCDst *)cd;            /* ready to go */
844   }
845  
# Line 646 | Line 856 | SDqueryTreProjSA(double *psa, const FVECT v1, const RR
856                                          /* get projected solid angle(s) */
857          if (v2 != NULL) {
858                  const SDTre     *sdt = (SDTre *)sdc->dist;
859 <                double          hcube[SD_MAXDIM];
860 <                if (SDqueryTre(sdt, v1, v2, hcube) < 0) {
859 >                double          hcube[SD_MAXDIM+1];
860 >                if (!SDqueryTre(sdt, NULL, v1, v2, hcube)) {
861                          strcpy(SDerrorDetail, "Bad call to SDqueryTreProjSA");
862                          return SDEinternal;
863                  }
864 <                myPSA[0] = hcube[sdt->st->ndim];
864 >                myPSA[0] = hcube[sdt->stc[tt_Y]->ndim];
865                  myPSA[1] = myPSA[0] *= myPSA[0] * M_PI;
866          } else {
867                  const SDTreCDst *cd = (const SDTreCDst *)SDgetTreCDist(v1, sdc);
868                  if (cd == NULL)
869 <                        return SDEmemory;
870 <                myPSA[0] = M_PI * (cd->clim[0][1] - cd->clim[0][0]) *
871 <                                (cd->clim[1][1] - cd->clim[1][0]);
872 <                myPSA[1] = cd->max_psa;
869 >                        myPSA[0] = myPSA[1] = 0;
870 >                else {
871 >                        myPSA[0] = M_PI * (cd->clim[0][1] - cd->clim[0][0]) *
872 >                                        (cd->clim[1][1] - cd->clim[1][0]);
873 >                        myPSA[1] = cd->max_psa;
874 >                }
875          }
876          switch (qflags) {               /* record based on flag settings */
877          case SDqueryVal:
# Line 674 | Line 886 | SDqueryTreProjSA(double *psa, const FVECT v1, const RR
886                          psa[1] = myPSA[1];
887                  /* fall through */
888          case SDqueryMin:
889 <                if (myPSA[0] < psa[0])
889 >                if ((myPSA[0] > 0) & (myPSA[0] < psa[0]))
890                          psa[0] = myPSA[0];
891                  break;
892          }
# Line 690 | Line 902 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
902          const SDTreCDst *cd = (const SDTreCDst *)cdp;
903          const unsigned  target = randX*cumlmax;
904          bitmask_t       hndx, hcoord[2];
905 <        double          gpos[3], rotangle;
905 >        FVECT           gpos;
906 >        double          rotangle;
907          int             i, iupper, ilower;
908                                          /* check arguments */
909          if ((ioVec == NULL) | (cd == NULL))
910                  return SDEargument;
911 +        if (!cd->sidef)
912 +                return SDEnone;         /* XXX should never happen */
913          if (ioVec[2] > 0) {
914 <                if (!(cd->sidef & SD_UFRONT))
914 >                if ((cd->sidef != SD_FREFL) & (cd->sidef != SD_FXMIT))
915                          return SDEargument;
916 <        } else if (!(cd->sidef & SD_UBACK))
916 >        } else if ((cd->sidef != SD_BREFL) & (cd->sidef != SD_BXMIT))
917                  return SDEargument;
918                                          /* binary search to find position */
919          ilower = 0; iupper = cd->calen;
920          while ((i = (iupper + ilower) >> 1) != ilower)
921 <                if ((long)target >= (long)cd->carr[i].cuml)
921 >                if (target >= cd->carr[i].cuml)
922                          ilower = i;
923                  else
924                          iupper = i;
# Line 718 | Line 933 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
933          for (i = 2; i--; )
934                  gpos[i] = ((double)hcoord[i] + rand()*(1./(RAND_MAX+.5))) /
935                                  (double)((bitmask_t)1 << nBitsC);
936 <        SDsquare2disk(gpos, gpos[0], gpos[1]);
936 >        square2disk(gpos, gpos[0], gpos[1]);
937                                          /* compute Z-coordinate */
938          gpos[2] = 1. - gpos[0]*gpos[0] - gpos[1]*gpos[1];
939 <        if (gpos[2] > 0)                /* paranoia, I hope */
725 <                gpos[2] = sqrt(gpos[2]);
939 >        gpos[2] = sqrt(gpos[2]*(gpos[2]>0));
940                                          /* emit from back? */
941 <        if (ioVec[2] > 0 ^ cd->sidef != SD_XMIT)
941 >        if ((cd->sidef == SD_BREFL) | (cd->sidef == SD_FXMIT))
942                  gpos[2] = -gpos[2];
943 <        if (cd->isodist) {              /* rotate isotropic result */
943 >        if (cd->isodist) {              /* rotate isotropic sample */
944                  rotangle = atan2(-ioVec[1],-ioVec[0]);
945 <                VCOPY(ioVec, gpos);
732 <                spinvector(ioVec, ioVec, zvec, rotangle);
945 >                spinvector(ioVec, gpos, zvec, rotangle);
946          } else
947                  VCOPY(ioVec, gpos);
948          return SDEnone;
# Line 745 | Line 958 | next_token(char **spp)
958   }
959  
960   /* Advance pointer past matching token (or any token if c==0) */
961 < #define eat_token(spp,c)        (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0)
961 > #define eat_token(spp,c)        ((next_token(spp)==(c)) ^ !(c) ? *(*(spp))++ : 0)
962  
963   /* Count words from this point in string to '}' */
964   static int
# Line 771 | Line 984 | load_values(char **spp, float *va, int n)
984          char    *svnext;
985  
986          while (n-- > 0 && (svnext = fskip(*spp)) != NULL) {
987 <                *v++ = atof(*spp);
987 >                if ((*v++ = atof(*spp)) < 0)
988 >                        v[-1] = 0;
989                  *spp = svnext;
990                  eat_token(spp, ',');
991          }
# Line 830 | Line 1044 | load_tree_data(char **spp, int nd)
1044   static SDError
1045   get_extrema(SDSpectralDF *df)
1046   {
1047 <        SDNode  *st = (*(SDTre *)df->comp[0].dist).st;
1047 >        SDNode  *st = (*(SDTre *)df->comp[0].dist).stc[tt_Y];
1048          double  stepWidth, dhemi, bmin[4], bmax[4];
1049  
1050          stepWidth = SDsmallestLeaf(st);
1051 +        if (quantum > stepWidth)        /* adjust quantization factor */
1052 +                quantum = stepWidth;
1053          df->minProjSA = M_PI*stepWidth*stepWidth;
1054          if (stepWidth < .03125)
1055                  stepWidth = .03125;     /* 1/32 resolution good enough */
# Line 868 | Line 1084 | get_extrema(SDSpectralDF *df)
1084  
1085   /* Load BSDF distribution for this wavelength */
1086   static SDError
1087 < load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
1087 > load_bsdf_data(SDData *sd, ezxml_t wdb, int ct, int ndim)
1088   {
1089          SDSpectralDF    *df;
1090          SDTre           *sdt;
1091          char            *sdata;
876        int             i;
1092                                          /* allocate BSDF component */
1093          sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
1094          if (!sdata)
# Line 881 | Line 1096 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
1096          /*
1097           * Remember that front and back are reversed from WINDOW 6 orientations
1098           */
1099 <        if (!strcasecmp(sdata, "Transmission")) {
1100 <                if (sd->tf != NULL)
886 <                        SDfreeSpectralDF(sd->tf);
887 <                if ((sd->tf = SDnewSpectralDF(1)) == NULL)
1099 >        if (!strcasecmp(sdata, "Transmission Front")) {
1100 >                if (sd->tb == NULL && (sd->tb = SDnewSpectralDF(1)) == NULL)
1101                          return SDEmemory;
1102 +                df = sd->tb;
1103 +        } else if (!strcasecmp(sdata, "Transmission Back")) {
1104 +                if (sd->tf == NULL && (sd->tf = SDnewSpectralDF(1)) == NULL)
1105 +                        return SDEmemory;
1106                  df = sd->tf;
1107          } else if (!strcasecmp(sdata, "Reflection Front")) {
1108 <                if (sd->rb != NULL)     /* note back-front reversal */
892 <                        SDfreeSpectralDF(sd->rb);
893 <                if ((sd->rb = SDnewSpectralDF(1)) == NULL)
1108 >                if (sd->rb == NULL && (sd->rb = SDnewSpectralDF(1)) == NULL)
1109                          return SDEmemory;
1110                  df = sd->rb;
1111          } else if (!strcasecmp(sdata, "Reflection Back")) {
1112 <                if (sd->rf != NULL)     /* note front-back reversal */
898 <                        SDfreeSpectralDF(sd->rf);
899 <                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
1112 >                if (sd->rf == NULL && (sd->rf = SDnewSpectralDF(1)) == NULL)
1113                          return SDEmemory;
1114                  df = sd->rf;
1115          } else
1116                  return SDEnone;
904        /* XXX should also check "ScatteringDataType" for consistency? */
1117                                          /* get angle bases */
1118          sdata = ezxml_txt(ezxml_child(wdb,"AngleBasis"));
1119          if (!sdata || strcasecmp(sdata, "LBNL/Shirley-Chiu")) {
# Line 909 | Line 1121 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
1121                                  !sdata ? "Missing" : "Unsupported", sd->name);
1122                  return !sdata ? SDEformat : SDEsupport;
1123          }
1124 <                                        /* allocate BSDF tree */
1125 <        sdt = (SDTre *)malloc(sizeof(SDTre));
1126 <        if (sdt == NULL)
1127 <                return SDEmemory;
1128 <        if (df == sd->rf)
1129 <                sdt->sidef = SD_UFRONT;
1130 <        else if (df == sd->rb)
1131 <                sdt->sidef = SD_UBACK;
1132 <        else
1133 <                sdt->sidef = SD_XMIT;
1134 <        sdt->st = NULL;
1135 <        df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
1136 <        df->comp[0].dist = sdt;
1137 <        df->comp[0].func = &SDhandleTre;
1124 >        if (df->comp[0].dist == NULL) { /* need to allocate BSDF tree? */
1125 >                sdt = (SDTre *)malloc(sizeof(SDTre));
1126 >                if (sdt == NULL)
1127 >                        return SDEmemory;
1128 >                if (df == sd->rf)
1129 >                        sdt->sidef = SD_FREFL;
1130 >                else if (df == sd->rb)
1131 >                        sdt->sidef = SD_BREFL;
1132 >                else if (df == sd->tf)
1133 >                        sdt->sidef = SD_FXMIT;
1134 >                else /* df == sd->tb */
1135 >                        sdt->sidef = SD_BXMIT;
1136 >                sdt->stc[tt_Y] = sdt->stc[tt_u] = sdt->stc[tt_v] = NULL;
1137 >                df->comp[0].dist = sdt;
1138 >                df->comp[0].func = &SDhandleTre;
1139 >        } else {
1140 >                sdt = (SDTre *)df->comp[0].dist;
1141 >                if (sdt->stc[ct] != NULL) {
1142 >                        SDfreeTre(sdt->stc[ct]);
1143 >                        sdt->stc[ct] = NULL;
1144 >                }
1145 >        }
1146                                          /* read BSDF data */
1147          sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData"));
1148          if (!sdata || !next_token(&sdata)) {
# Line 930 | Line 1150 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
1150                                  sd->name);
1151                  return SDEformat;
1152          }
1153 <        sdt->st = load_tree_data(&sdata, ndim);
1154 <        if (sdt->st == NULL)
1153 >        sdt->stc[ct] = load_tree_data(&sdata, ndim);
1154 >        if (sdt->stc[ct] == NULL)
1155                  return SDEformat;
1156          if (next_token(&sdata)) {       /* check for unconsumed characters */
1157                  sprintf(SDerrorDetail,
# Line 940 | Line 1160 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
1160                  return SDEformat;
1161          }
1162                                          /* flatten branches where possible */
1163 <        sdt->st = SDsimplifyTre(sdt->st);
1164 <        if (sdt->st == NULL)
1163 >        sdt->stc[ct] = SDsimplifyTre(sdt->stc[ct]);
1164 >        if (sdt->stc[ct] == NULL)
1165                  return SDEinternal;
1166 <        return get_extrema(df);         /* compute global quantities */
1166 >                                        /* compute global quantities for Y */
1167 >        return (ct == tt_Y) ? get_extrema(df) : SDEnone;
1168   }
1169  
1170   /* Find minimum value in tree */
# Line 983 | Line 1204 | SDsubtractTreVal(SDNode *st, float val)
1204          }
1205   }
1206  
1207 < /* Subtract minimum value from BSDF */
1207 > /* Subtract minimum Y value from BSDF */
1208   static double
1209 < subtract_min(SDNode *st)
1209 > subtract_min_Y(SDNode *st)
1210   {
1211 <        float   vmin;
1211 >        const float     vmaxmin = 1.5/M_PI;
1212 >        float           vmin;
1213                                          /* be sure to skip unused portion */
1214          if (st->ndim == 3) {
1215                  int     n;
1216 <                vmin = 1./M_PI;
1216 >                vmin = vmaxmin;
1217                  if (st->log2GR < 0) {
1218                          for (n = 0; n < 8; n += 2) {
1219                                  float   v = SDgetTreMin(st->u.t[n]);
# Line 1007 | Line 1229 | subtract_min(SDNode *st)
1229          } else                          /* anisotropic covers entire tree */
1230                  vmin = SDgetTreMin(st);
1231  
1232 <        if (vmin <= FTINY)
1233 <                return .0;
1232 >        if ((vmin >= vmaxmin) | (vmin <= .01/M_PI))
1233 >                return .0;              /* not worth bothering about */
1234  
1235          SDsubtractTreVal(st, vmin);
1236  
1237          return M_PI * vmin;             /* return hemispherical value */
1238   }
1239  
1240 + /* Struct used in callback to find RGB extrema */
1241 + typedef struct {
1242 +        SDNode  **stc;                  /* original Y, u' & v' trees */
1243 +        float   rgb[3];                 /* RGB value */
1244 +        SDNode  *new_stu, *new_stv;     /* replacement u' & v' trees */
1245 + } SDextRGBs;
1246 +
1247 + /* Callback to find minimum RGB from Y value plus CIE (u',v') trees */
1248 + static int
1249 + get_min_RGB(float yval, const double *cmin, double csiz, void *cptr)
1250 + {
1251 +        SDextRGBs       *mp = (SDextRGBs *)cptr;
1252 +        double          cmax[SD_MAXDIM];
1253 +        float           rgb[3];
1254 +
1255 +        if (mp->stc[tt_Y]->ndim == 3) {
1256 +                if (cmin[0] + .5*csiz >= .5)
1257 +                        return 0;       /* ignore dead half of isotropic */
1258 +        } else
1259 +                cmax[3] = cmin[3] + csiz;
1260 +        cmax[0] = cmin[0] + csiz;
1261 +        cmax[1] = cmin[1] + csiz;
1262 +        cmax[2] = cmin[2] + csiz;
1263 +                                        /* average RGB color over voxel */
1264 +        SDyuv2rgb(yval, SDavgTreBox(mp->stc[tt_u], cmin, cmax),
1265 +                        SDavgTreBox(mp->stc[tt_v], cmin, cmax), rgb);
1266 +                                        /* track smallest components */
1267 +        if (rgb[0] < mp->rgb[0]) mp->rgb[0] = rgb[0];
1268 +        if (rgb[1] < mp->rgb[1]) mp->rgb[1] = rgb[1];
1269 +        if (rgb[2] < mp->rgb[2]) mp->rgb[2] = rgb[2];
1270 +        return 0;
1271 + }
1272 +
1273 + /* Callback to build adjusted u' tree */
1274 + static int
1275 + adjust_utree(float uprime, const double *cmin, double csiz, void *cptr)
1276 + {
1277 +        SDextRGBs       *mp = (SDextRGBs *)cptr;
1278 +        double          cmax[SD_MAXDIM];
1279 +        double          yval;
1280 +        float           rgb[3];
1281 +        C_COLOR         clr;
1282 +
1283 +        if (mp->stc[tt_Y]->ndim == 3) {
1284 +                if (cmin[0] + .5*csiz >= .5)
1285 +                        return 0;       /* ignore dead half of isotropic */
1286 +        } else
1287 +                cmax[3] = cmin[3] + csiz;
1288 +        cmax[0] = cmin[0] + csiz;
1289 +        cmax[1] = cmin[1] + csiz;
1290 +        cmax[2] = cmin[2] + csiz;
1291 +                                        /* average RGB color over voxel */
1292 +        SDyuv2rgb(yval=SDavgTreBox(mp->stc[tt_Y], cmin, cmax), uprime,
1293 +                        SDavgTreBox(mp->stc[tt_v], cmin, cmax), rgb);
1294 +                                        /* subtract minimum (& clamp) */
1295 +        if ((rgb[0] -= mp->rgb[0]) < 1e-5*yval) rgb[0] = 1e-5*yval;
1296 +        if ((rgb[1] -= mp->rgb[1]) < 1e-5*yval) rgb[1] = 1e-5*yval;
1297 +        if ((rgb[2] -= mp->rgb[2]) < 1e-5*yval) rgb[2] = 1e-5*yval;
1298 +        c_fromSharpRGB(rgb, &clr);      /* compute new u' for adj. RGB */
1299 +        uprime = 4.*clr.cx/(-2.*clr.cx + 12.*clr.cy + 3.);
1300 +                                        /* assign in new u' tree */
1301 +        mp->new_stu = SDsetVoxel(mp->new_stu, mp->stc[tt_Y]->ndim,
1302 +                                        cmin, csiz, uprime);
1303 +        return -(mp->new_stu == NULL);
1304 + }
1305 +
1306 + /* Callback to build adjusted v' tree */
1307 + static int
1308 + adjust_vtree(float vprime, const double *cmin, double csiz, void *cptr)
1309 + {
1310 +        SDextRGBs       *mp = (SDextRGBs *)cptr;
1311 +        double          cmax[SD_MAXDIM];
1312 +        double          yval;
1313 +        float           rgb[3];
1314 +        C_COLOR         clr;
1315 +
1316 +        if (mp->stc[tt_Y]->ndim == 3) {
1317 +                if (cmin[0] + .5*csiz >= .5)
1318 +                        return 0;       /* ignore dead half of isotropic */
1319 +        } else
1320 +                cmax[3] = cmin[3] + csiz;
1321 +        cmax[0] = cmin[0] + csiz;
1322 +        cmax[1] = cmin[1] + csiz;
1323 +        cmax[2] = cmin[2] + csiz;
1324 +                                        /* average RGB color over voxel */
1325 +        SDyuv2rgb(yval=SDavgTreBox(mp->stc[tt_Y], cmin, cmax),
1326 +                        SDavgTreBox(mp->stc[tt_u], cmin, cmax),
1327 +                        vprime, rgb);
1328 +                                        /* subtract minimum (& clamp) */
1329 +        if ((rgb[0] -= mp->rgb[0]) < 1e-5*yval) rgb[0] = 1e-5*yval;
1330 +        if ((rgb[1] -= mp->rgb[1]) < 1e-5*yval) rgb[1] = 1e-5*yval;
1331 +        if ((rgb[2] -= mp->rgb[2]) < 1e-5*yval) rgb[2] = 1e-5*yval;
1332 +        c_fromSharpRGB(rgb, &clr);      /* compute new v' for adj. RGB */
1333 +        vprime = 9.*clr.cy/(-2.*clr.cx + 12.*clr.cy + 3.);
1334 +                                        /* assign in new v' tree */
1335 +        mp->new_stv = SDsetVoxel(mp->new_stv, mp->stc[tt_Y]->ndim,
1336 +                                        cmin, csiz, vprime);
1337 +        return -(mp->new_stv == NULL);
1338 + }
1339 +
1340 + /* Subtract minimum (diffuse) color and return luminance & CIE (x,y) */
1341 + static double
1342 + subtract_min_RGB(C_COLOR *cs, SDNode *stc[])
1343 + {
1344 +        SDextRGBs       my_min;
1345 +        double          ymin;
1346 +
1347 +        my_min.stc = stc;
1348 +        my_min.rgb[0] = my_min.rgb[1] = my_min.rgb[2] = FHUGE;
1349 +        my_min.new_stu = my_min.new_stv = NULL;
1350 +                                        /* get minimum RGB value */
1351 +        SDtraverseTre(stc[tt_Y], NULL, 0, get_min_RGB, &my_min);
1352 +                                        /* convert to C_COLOR */
1353 +        ymin =  c_fromSharpRGB(my_min.rgb, cs);
1354 +        if ((ymin >= .5*FHUGE) | (ymin <= .01/M_PI))
1355 +                return .0;              /* close to zero or no tree */
1356 +                                        /* adjust u' & v' trees */
1357 +        SDtraverseTre(stc[tt_u], NULL, 0, adjust_utree, &my_min);
1358 +        SDtraverseTre(stc[tt_v], NULL, 0, adjust_vtree, &my_min);
1359 +        SDfreeTre(stc[tt_u]); SDfreeTre(stc[tt_v]);
1360 +        stc[tt_u] = SDsimplifyTre(my_min.new_stu);
1361 +        stc[tt_v] = SDsimplifyTre(my_min.new_stv);
1362 +                                        /* subtract Y & return hemispherical */
1363 +        SDsubtractTreVal(stc[tt_Y], ymin);
1364 +
1365 +        return M_PI * ymin;
1366 + }
1367 +
1368   /* Extract and separate diffuse portion of BSDF */
1369   static void
1370   extract_diffuse(SDValue *dv, SDSpectralDF *df)
1371   {
1372          int     n;
1373 +        SDTre   *sdt;
1374  
1375          if (df == NULL || df->ncomp <= 0) {
1376                  dv->spec = c_dfcolor;
1377                  dv->cieY = .0;
1378                  return;
1379          }
1380 <        dv->spec = df->comp[0].cspec[0];
1381 <        dv->cieY = subtract_min((*(SDTre *)df->comp[0].dist).st);
1382 <                                        /* in case of multiple components */
1383 <        for (n = df->ncomp; --n; ) {
1384 <                double  ymin = subtract_min((*(SDTre *)df->comp[n].dist).st);
1385 <                c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]);
1386 <                dv->cieY += ymin;
1380 >        sdt = (SDTre *)df->comp[0].dist;
1381 >                                        /* subtract minimum color/grayscale */
1382 >        if (sdt->stc[tt_u] != NULL && sdt->stc[tt_v] != NULL) {
1383 >                int     i = 3*(tt_RGB_coef[1] < .001);
1384 >                while (i--) {           /* initialize on first call */
1385 >                        float   rgb[3];
1386 >                        rgb[0] = rgb[1] = rgb[2] = .0f; rgb[i] = 1.f;
1387 >                        tt_RGB_coef[i] = c_fromSharpRGB(rgb, &tt_RGB_prim[i]);
1388 >                }
1389 >                memcpy(df->comp[0].cspec, tt_RGB_prim, sizeof(tt_RGB_prim));
1390 >                dv->cieY = subtract_min_RGB(&dv->spec, sdt->stc);
1391 >        } else {
1392 >                df->comp[0].cspec[0] = dv->spec = c_dfcolor;
1393 >                dv->cieY = subtract_min_Y(sdt->stc[tt_Y]);
1394          }
1395          df->maxHemi -= dv->cieY;        /* adjust maximum hemispherical */
1396 <                                        /* make sure everything is set */
1397 <        c_ccvt(&dv->spec, C_CSXY+C_CSSPEC);
1396 >                                
1397 >        c_ccvt(&dv->spec, C_CSXY);      /* make sure (x,y) is set */
1398   }
1399  
1400   /* Load a variable-resolution BSDF tree from an open XML file */
# Line 1069 | Line 1427 | SDloadTre(SDData *sd, ezxml_t wtl)
1427                                          /* load BSDF components */
1428          for (wld = ezxml_child(wtl, "WavelengthData");
1429                                  wld != NULL; wld = wld->next) {
1430 <                if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
1431 <                                "Visible"))
1432 <                        continue;       /* just visible for now */
1430 >                const char      *cnm = ezxml_txt(ezxml_child(wld,"Wavelength"));
1431 >                int             ct = -1;
1432 >                if (!strcasecmp(cnm, "Visible"))
1433 >                        ct = tt_Y;
1434 >                else if (!strcasecmp(cnm, "CIE-u"))
1435 >                        ct = tt_u;
1436 >                else if (!strcasecmp(cnm, "CIE-v"))
1437 >                        ct = tt_v;
1438 >                else
1439 >                        continue;
1440                  for (wdb = ezxml_child(wld, "WavelengthDataBlock");
1441                                          wdb != NULL; wdb = wdb->next)
1442 <                        if ((ec = load_bsdf_data(sd, wdb, rank)) != SDEnone)
1442 >                        if ((ec = load_bsdf_data(sd, wdb, ct, rank)) != SDEnone)
1443                                  return ec;
1444          }
1445                                          /* separate diffuse components */
1446          extract_diffuse(&sd->rLambFront, sd->rf);
1447          extract_diffuse(&sd->rLambBack, sd->rb);
1448 <        extract_diffuse(&sd->tLamb, sd->tf);
1448 >        extract_diffuse(&sd->tLambFront, sd->tf);
1449 >        if (sd->tb != NULL) {
1450 >                extract_diffuse(&sd->tLambBack, sd->tb);
1451 >                if (sd->tf == NULL)
1452 >                        sd->tLambFront = sd->tLambBack;
1453 >        } else if (sd->tf != NULL)
1454 >                sd->tLambBack = sd->tLambFront;
1455                                          /* return success */
1456          return SDEnone;
1457   }
1458  
1459   /* Variable resolution BSDF methods */
1460 < SDFunc SDhandleTre = {
1460 > const SDFunc SDhandleTre = {
1461          &SDgetTreBSDF,
1462          &SDqueryTreProjSA,
1463          &SDgetTreCDist,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines