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.6 by greg, Sun Apr 24 19:39:21 2011 UTC vs.
Revision 3.12 by greg, Sun May 1 16:34:37 2011 UTC

# Line 24 | Line 24 | typedef int    SDtreCallback(float val, const double *cmi
24                                          double csiz, void *cptr);
25  
26                                          /* reference width maximum (1.0) */
27 + static const unsigned   iwbits = sizeof(unsigned)*4;
28   static const unsigned   iwmax = (1<<(sizeof(unsigned)*4))-1;
29 +                                        /* maximum cumulative value */
30 + static const unsigned   cumlmax = ~0;
31  
32   /* Struct used for our distribution-building callback */
33   typedef struct {
31        int             wmin;           /* minimum square size so far */
32        int             wmax;           /* maximum square size */
34          int             nic;            /* number of input coordinates */
35 <        int             alen;           /* current array length */
36 <        int             nall;           /* number of allocated entries */
35 >        unsigned        alen;           /* current array length */
36 >        unsigned        nall;           /* number of allocated entries */
37 >        unsigned        wmin;           /* minimum square size so far */
38 >        unsigned        wmax;           /* maximum square size */
39          struct outdir_s {
40                  unsigned        hent;           /* entering Hilbert index */
41                  int             wid;            /* this square size */
# Line 57 | Line 60 | SDnewNode(int nd, int lg)
60          }
61          if (lg < 0) {
62                  st = (SDNode *)malloc(sizeof(SDNode) +
63 <                                ((1<<nd) - 1)*sizeof(st->u.t[0]));
64 <                if (st != NULL)
62 <                        memset(st->u.t, 0, sizeof(st->u.t[0])<<nd);
63 <        } else
64 <                st = (SDNode *)malloc(sizeof(SDNode) +
65 <                                ((1 << nd*lg) - 1)*sizeof(st->u.v[0]));
66 <                
67 <        if (st == NULL) {
68 <                if (lg < 0)
63 >                                sizeof(st->u.t[0])*((1<<nd) - 1));
64 >                if (st == NULL) {
65                          sprintf(SDerrorDetail,
66                                  "Cannot allocate %d branch BSDF tree", 1<<nd);
67 <                else
67 >                        return NULL;
68 >                }
69 >                memset(st->u.t, 0, sizeof(st->u.t[0])<<nd);
70 >        } else {
71 >                st = (SDNode *)malloc(sizeof(SDNode) +
72 >                                sizeof(st->u.v[0])*((1 << nd*lg) - 1));        
73 >                if (st == NULL) {
74                          sprintf(SDerrorDetail,
75                                  "Cannot allocate %d BSDF leaves", 1 << nd*lg);
76 <                return NULL;
76 >                        return NULL;
77 >                }
78          }
79          st->ndim = nd;
80          st->log2GR = lg;
# Line 82 | Line 85 | SDnewNode(int nd, int lg)
85   static void
86   SDfreeTre(SDNode *st)
87   {
88 <        int     i;
88 >        int     n;
89  
90          if (st == NULL)
91                  return;
92 <        for (i = (st->log2GR < 0) << st->ndim; i--; )
93 <                SDfreeTre(st->u.t[i]);
92 >        for (n = (st->log2GR < 0) << st->ndim; n--; )
93 >                SDfreeTre(st->u.t[n]);
94          free((void *)st);
95   }
96  
# Line 103 | Line 106 | SDFreeBTre(void *p)
106          free(sdt);
107   }
108  
109 + /* Fill branch's worth of grid values from subtree */
110 + static void
111 + fill_grid_branch(float *dptr, const float *sptr, int nd, int shft)
112 + {
113 +        unsigned        n = 1 << (shft-1);
114 +
115 +        if (!--nd) {                    /* end on the line */
116 +                memcpy(dptr, sptr, sizeof(*dptr)*n);
117 +                return;
118 +        }
119 +        while (n--)                     /* recurse on each slice */
120 +                fill_grid_branch(dptr + (n << shft*nd),
121 +                                sptr + (n << (shft-1)*nd), nd, shft);
122 + }
123 +
124 + /* Get pointer at appropriate offset for the given branch */
125 + static float *
126 + grid_branch_start(SDNode *st, int n)
127 + {
128 +        unsigned        skipsiz = 1 << st->log2GR;
129 +        float           *vptr = st->u.v;
130 +        int             i;
131 +
132 +        for (i = 0; i < st->ndim; skipsiz <<= st->log2GR)
133 +                if (1<<i++ & n)
134 +                        vptr += skipsiz >> 1;
135 +        return vptr;
136 + }
137 +
138 + /* Simplify (consolidate) a tree by flattening uniform depth regions */
139 + static SDNode *
140 + SDsimplifyTre(SDNode *st)
141 + {
142 +        int             match, n;
143 +
144 +        if (st == NULL)                 /* check for invalid tree */
145 +                return NULL;
146 +        if (st->log2GR >= 0)            /* grid just returns unaltered */
147 +                return st;
148 +        match = 1;                      /* check if grids below match */
149 +        for (n = 0; n < 1<<st->ndim; n++) {
150 +                if ((st->u.t[n] = SDsimplifyTre(st->u.t[n])) == NULL)
151 +                        return NULL;    /* propogate error up call stack */
152 +                match &= (st->u.t[n]->log2GR == st->u.t[0]->log2GR);
153 +        }
154 +        if (match && (match = st->u.t[0]->log2GR) >= 0) {
155 +                SDNode  *stn = SDnewNode(st->ndim, match + 1);
156 +                if (stn == NULL)        /* out of memory? */
157 +                        return st;
158 +                                        /* transfer values to new grid */
159 +                for (n = 1 << st->ndim; n--; )
160 +                        fill_grid_branch(grid_branch_start(stn, n),
161 +                                        st->u.t[n]->u.v, stn->ndim, stn->log2GR);
162 +                SDfreeTre(st);          /* free old tree */
163 +                st = stn;               /* return new one */
164 +        }
165 +        return st;
166 + }
167 +
168 + /* Find smallest leaf in tree */
169 + static double
170 + SDsmallestLeaf(const SDNode *st)
171 + {
172 +        if (st->log2GR < 0) {           /* tree branches */
173 +                double  lmin = 1.;
174 +                int     n;
175 +                for (n = 1<<st->ndim; n--; ) {
176 +                        double  lsiz = SDsmallestLeaf(st->u.t[n]);
177 +                        if (lsiz < lmin)
178 +                                lmin = lsiz;
179 +                }
180 +                return .5*lmin;
181 +        }
182 +                                        /* leaf grid width */
183 +        return 1. / (double)(1 << st->log2GR);
184 + }
185 +
186   /* Add up N-dimensional hypercube array values over the given box */
187   static double
188 < SDiterSum(const float *va, int nd, int siz, const int *imin, const int *imax)
188 > SDiterSum(const float *va, int nd, int shft, const int *imin, const int *imax)
189   {
190 +        const unsigned  skipsiz = 1 << --nd*shft;
191          double          sum = .0;
111        unsigned        skipsiz = 1;
192          int             i;
193          
114        for (i = nd; --i > 0; )
115                skipsiz *= siz;
194          if (skipsiz == 1)
195                  for (i = *imin; i < *imax; i++)
196                          sum += va[i];
197          else
198                  for (i = *imin; i < *imax; i++)
199 <                        sum += SDiterSum(va + i*skipsiz,
122 <                                        nd-1, siz, imin+1, imax+1);
199 >                        sum += SDiterSum(va + i*skipsiz, nd, shft, imin+1, imax+1);
200          return sum;
201   }
202  
# Line 168 | Line 245 | SDavgTreBox(const SDNode *st, const double *bmin, cons
245          }
246          n = 1;                          /* iterate over leaves */
247          for (i = st->ndim; i--; ) {
248 <                imin[i] = (bmin[i] <= .0) ? 0
248 >                imin[i] = (bmin[i] <= 0) ? 0
249                                  : (int)((1 << st->log2GR)*bmin[i]);
250                  imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR)
251                                  : (int)((1 << st->log2GR)*bmax[i] + .999999);
# Line 177 | Line 254 | SDavgTreBox(const SDNode *st, const double *bmin, cons
254          if (!n)
255                  return .0;
256          
257 <        return SDiterSum(st->u.v, st->ndim, 1 << st->log2GR, imin, imax) /
181 <                        (double)n;
257 >        return SDiterSum(st->u.v, st->ndim, st->log2GR, imin, imax) / (double)n;
258   }
259  
260   /* Recursive call for SDtraverseTre() */
# Line 337 | Line 413 | SDqueryTre(const SDTre *sdt, const FVECT outVec, const
413          static const FVECT      zvec = {.0, .0, 1.};
414          FVECT                   rOutVec;
415          double                  gridPos[4];
416 <                                        /* check transmission */
417 <        if (!sdt->isxmit ^ outVec[2] > 0 ^ inVec[2] > 0)
416 >
417 >        switch (sdt->sidef) {           /* whose side are you on? */
418 >        case SD_UFRONT:
419 >                if ((outVec[2] < 0) | (inVec[2] < 0))
420 >                        return -1.;
421 >                break;
422 >        case SD_UBACK:
423 >                if ((outVec[2] > 0) | (inVec[2] > 0))
424 >                        return -1.;
425 >                break;
426 >        case SD_XMIT:
427 >                if ((outVec[2] > 0) == (inVec[2] > 0))
428 >                        return -1.;
429 >                break;
430 >        default:
431                  return -1.;
432 +        }
433                                          /* convert vector coordinates */
434          if (sdt->st->ndim == 3) {
435                  spinvector(rOutVec, outVec, zvec, -atan2(inVec[1],inVec[0]));
# Line 383 | Line 473 | build_scaffold(float val, const double *cmin, double c
473                  sp->wmax = wid;
474          if (sp->alen >= sp->nall) {     /* need more space? */
475                  struct outdir_s *ndarr;
476 <                sp->nall += 8192;
476 >                sp->nall += 1024;
477                  ndarr = (struct outdir_s *)realloc(sp->darr,
478                                          sizeof(struct outdir_s)*sp->nall);
479 <                if (ndarr == NULL)
479 >                if (ndarr == NULL) {
480 >                        sprintf(SDerrorDetail,
481 >                                "Cannot grow scaffold to %u entries", sp->nall);
482                          return -1;      /* abort build */
483 +                }
484                  sp->darr = ndarr;
485          }
486                                          /* find Hilbert entry index */
487          bmin[0] = cmin[0]*(double)iwmax + .5;
488          bmin[1] = cmin[1]*(double)iwmax + .5;
489 <        bmax[0] = bmin[0] + wid;
490 <        bmax[1] = bmin[1] + wid;
491 <        hilbert_box_vtx(2, sizeof(bitmask_t), sizeof(unsigned)*4,
492 <                                                        1, bmin, bmax);
400 <        sp->darr[sp->alen].hent = hilbert_c2i(2, sizeof(unsigned)*4, bmin);
489 >        bmax[0] = bmin[0] + wid-1;
490 >        bmax[1] = bmin[1] + wid-1;
491 >        hilbert_box_vtx(2, sizeof(bitmask_t), iwbits, 1, bmin, bmax);
492 >        sp->darr[sp->alen].hent = hilbert_c2i(2, iwbits, bmin);
493          sp->darr[sp->alen].wid = wid;
494          sp->darr[sp->alen].bsdf = val;
495          sp->alen++;                     /* on to the next entry */
# Line 408 | Line 500 | build_scaffold(float val, const double *cmin, double c
500   static int
501   sscmp(const void *p1, const void *p2)
502   {
503 <        return (int)((*(const struct outdir_s *)p1).hent -
504 <                        (*(const struct outdir_s *)p2).hent);
503 >        unsigned        h1 = (*(const struct outdir_s *)p1).hent;
504 >        unsigned        h2 = (*(const struct outdir_s *)p2).hent;
505 >
506 >        if (h1 > h2)
507 >                return 1;
508 >        if (h1 < h2)
509 >                return -1;
510 >        return 0;
511   }
512  
513   /* Create a new cumulative distribution for the given input direction */
514   static SDTreCDst *
515   make_cdist(const SDTre *sdt, const double *pos)
516   {
419        const unsigned  cumlmax = ~0;
517          SDdistScaffold  myScaffold;
518          SDTreCDst       *cd;
519          struct outdir_s *sp;
# Line 427 | Line 524 | make_cdist(const SDTre *sdt, const double *pos)
524          myScaffold.wmax = 0;
525          myScaffold.nic = sdt->st->ndim - 2;
526          myScaffold.alen = 0;
527 <        myScaffold.nall = 8192;
527 >        myScaffold.nall = 512;
528          myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) *
529                                                          myScaffold.nall);
530          if (myScaffold.darr == NULL)
# Line 442 | Line 539 | make_cdist(const SDTre *sdt, const double *pos)
539          cd = (SDTreCDst *)malloc(sizeof(SDTreCDst) +
540                                  sizeof(cd->carr[0])*myScaffold.alen);
541          if (cd == NULL) {
542 +                sprintf(SDerrorDetail,
543 +                        "Cannot allocate %u entry cumulative distribution",
544 +                                myScaffold.alen);
545                  free(myScaffold.darr);
546                  return NULL;
547          }
# Line 450 | Line 550 | make_cdist(const SDTre *sdt, const double *pos)
550                                  sizeof(struct outdir_s), &sscmp);
551  
552                                          /* record input range */
553 <        scale = (double)myScaffold.wmin / iwmax;
553 >        scale = myScaffold.wmin / (double)iwmax;
554          for (i = myScaffold.nic; i--; ) {
555 <                cd->clim[i][0] = floor(pos[i]/scale + .5) * scale;
555 >                cd->clim[i][0] = floor(pos[i]/scale) * scale;
556                  cd->clim[i][1] = cd->clim[i][0] + scale;
557          }
558          cd->max_psa = myScaffold.wmax / (double)iwmax;
559          cd->max_psa *= cd->max_psa * M_PI;
560 <        cd->isxmit = sdt->isxmit;
560 >        cd->sidef = sdt->sidef;
561          cd->cTotal = 1e-20;             /* compute directional total */
562          sp = myScaffold.darr;
563          for (i = myScaffold.alen; i--; sp++)
# Line 466 | Line 566 | make_cdist(const SDTre *sdt, const double *pos)
566          scale = (double)cumlmax / cd->cTotal;
567          sp = myScaffold.darr;
568          for (i = 0; i < cd->calen; i++, sp++) {
569 +                cd->carr[i].hndx = sp->hent;
570                  cd->carr[i].cuml = scale*cursum + .5;
571                  cursum += sp->bsdf * (double)sp->wid * sp->wid;
572          }
# Line 531 | Line 632 | SDqueryTreProjSA(double *psa, const FVECT v1, const RR
632                  const SDTre     *sdt = (SDTre *)sdc->dist;
633                  double          hcube[SD_MAXDIM];
634                  if (SDqueryTre(sdt, v1, v2, hcube) < 0) {
635 <                        if (qflags == SDqueryVal)
636 <                                *psa = M_PI;
536 <                        return SDEnone;
635 >                        strcpy(SDerrorDetail, "Bad call to SDqueryTreProjSA");
636 >                        return SDEinternal;
637                  }
638                  myPSA[0] = hcube[sdt->st->ndim];
639                  myPSA[1] = myPSA[0] *= myPSA[0] * M_PI;
# Line 571 | Line 671 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
671   {
672          const unsigned  nBitsC = 4*sizeof(bitmask_t);
673          const unsigned  nExtraBits = 8*(sizeof(bitmask_t)-sizeof(unsigned));
574        const unsigned  maxval = ~0;
674          const SDTreCDst *cd = (const SDTreCDst *)cdp;
675 <        const unsigned  target = randX*maxval;
675 >        const unsigned  target = randX*cumlmax;
676          bitmask_t       hndx, hcoord[2];
677          double          gpos[3];
678          int             i, iupper, ilower;
679                                          /* check arguments */
680          if ((ioVec == NULL) | (cd == NULL))
681                  return SDEargument;
682 +        if (ioVec[2] > 0) {
683 +                if (!(cd->sidef & SD_UFRONT))
684 +                        return SDEargument;
685 +        } else if (!(cd->sidef & SD_UBACK))
686 +                return SDEargument;
687                                          /* binary search to find position */
688          ilower = 0; iupper = cd->calen;
689          while ((i = (iupper + ilower) >> 1) != ilower)
# Line 588 | Line 692 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
692                  else
693                          iupper = i;
694                                          /* localize random position */
695 <        randX = (randX*maxval - cd->carr[ilower].cuml) /
695 >        randX = (randX*cumlmax - cd->carr[ilower].cuml) /
696                      (double)(cd->carr[iupper].cuml - cd->carr[ilower].cuml);
697                                          /* index in longer Hilbert curve */
698          hndx = (randX*cd->carr[iupper].hndx + (1.-randX)*cd->carr[ilower].hndx)
# Line 599 | Line 703 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
703                  gpos[i] = ((double)hcoord[i] + rand()*(1./(RAND_MAX+.5))) /
704                                  (double)((bitmask_t)1 << nBitsC);
705          SDsquare2disk(gpos, gpos[0], gpos[1]);
706 +                                        /* compute Z-coordinate */
707          gpos[2] = 1. - gpos[0]*gpos[0] - gpos[1]*gpos[1];
708          if (gpos[2] > 0)                /* paranoia, I hope */
709                  gpos[2] = sqrt(gpos[2]);
710 <        if (ioVec[2] > 0 ^ !cd->isxmit)
710 >                                        /* emit from back? */
711 >        if (ioVec[2] > 0 ^ cd->sidef != SD_XMIT)
712                  gpos[2] = -gpos[2];
713          VCOPY(ioVec, gpos);
714          return SDEnone;
715   }
716  
717 + /* Advance pointer to the next non-white character in the string (or nul) */
718 + static int
719 + next_token(char **spp)
720 + {
721 +        while (isspace(**spp))
722 +                ++*spp;
723 +        return **spp;
724 + }
725 +
726 + /* Advance pointer past matching token (or any token if c==0) */
727 + #define eat_token(spp,c)        (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0)
728 +
729 + /* Count words from this point in string to '}' */
730 + static int
731 + count_values(char *cp)
732 + {
733 +        int     n = 0;
734 +
735 +        while (next_token(&cp) != '}' && *cp) {
736 +                while (!isspace(*cp) & (*cp != ',') & (*cp != '}'))
737 +                        if (!*++cp)
738 +                                break;
739 +                ++n;
740 +                eat_token(&cp, ',');
741 +        }
742 +        return n;
743 + }
744 +
745 + /* Load an array of real numbers, returning total */
746 + static int
747 + load_values(char **spp, float *va, int n)
748 + {
749 +        float   *v = va;
750 +        char    *svnext;
751 +
752 +        while (n-- > 0 && (svnext = fskip(*spp)) != NULL) {
753 +                *v++ = atof(*spp);
754 +                *spp = svnext;
755 +                eat_token(spp, ',');
756 +        }
757 +        return v - va;
758 + }
759 +
760 + /* Load BSDF tree data */
761 + static SDNode *
762 + load_tree_data(char **spp, int nd)
763 + {
764 +        SDNode  *st;
765 +        int     n;
766 +
767 +        if (!eat_token(spp, '{')) {
768 +                strcpy(SDerrorDetail, "Missing '{' in tensor tree");
769 +                return NULL;
770 +        }
771 +        if (next_token(spp) == '{') {   /* tree branches */
772 +                st = SDnewNode(nd, -1);
773 +                if (st == NULL)
774 +                        return NULL;
775 +                for (n = 0; n < 1<<nd; n++)
776 +                        if ((st->u.t[n] = load_tree_data(spp, nd)) == NULL) {
777 +                                SDfreeTre(st);
778 +                                return NULL;
779 +                        }
780 +        } else {                        /* else load value grid */
781 +                int     bsiz;
782 +                n = count_values(*spp); /* see how big the grid is */
783 +                for (bsiz = 0; bsiz < 8*sizeof(size_t)-1; bsiz += nd)
784 +                        if (1<<bsiz == n)
785 +                                break;
786 +                if (bsiz >= 8*sizeof(size_t)) {
787 +                        strcpy(SDerrorDetail, "Illegal value count in tensor tree");
788 +                        return NULL;
789 +                }
790 +                st = SDnewNode(nd, bsiz/nd);
791 +                if (st == NULL)
792 +                        return NULL;
793 +                if (load_values(spp, st->u.v, n) != n) {
794 +                        strcpy(SDerrorDetail, "Real format error in tensor tree");
795 +                        SDfreeTre(st);
796 +                        return NULL;
797 +                }
798 +        }
799 +        if (!eat_token(spp, '}')) {
800 +                strcpy(SDerrorDetail, "Missing '}' in tensor tree");
801 +                SDfreeTre(st);
802 +                return NULL;
803 +        }
804 +        eat_token(spp, ',');
805 +        return st;
806 + }
807 +
808 + /* Compute min. proj. solid angle and max. direct hemispherical scattering */
809 + static SDError
810 + get_extrema(SDSpectralDF *df)
811 + {
812 +        SDNode  *st = (*(SDTre *)df->comp[0].dist).st;
813 +        double  stepWidth, dhemi, bmin[4], bmax[4];
814 +
815 +        stepWidth = SDsmallestLeaf(st);
816 +        df->minProjSA = M_PI*stepWidth*stepWidth;
817 +        if (stepWidth < .03125)
818 +                stepWidth = .03125;     /* 1/32 resolution good enough */
819 +        df->maxHemi = .0;
820 +        if (st->ndim == 3) {            /* isotropic BSDF */
821 +                bmin[1] = bmin[2] = .0;
822 +                bmax[1] = bmax[2] = 1.;
823 +                for (bmin[0] = .0; bmin[0] < .5-FTINY; bmin[0] += stepWidth) {
824 +                        bmax[0] = bmin[0] + stepWidth;
825 +                        dhemi = SDavgTreBox(st, bmin, bmax);
826 +                        if (dhemi > df->maxHemi)
827 +                                df->maxHemi = dhemi;
828 +                }
829 +        } else if (st->ndim == 4) {     /* anisotropic BSDF */
830 +                bmin[2] = bmin[3] = .0;
831 +                bmax[2] = bmax[3] = 1.;
832 +                for (bmin[0] = .0; bmin[0] < 1.-FTINY; bmin[0] += stepWidth) {
833 +                        bmax[0] = bmin[0] + stepWidth;
834 +                        for (bmin[1] = .0; bmin[1] < 1.-FTINY; bmin[1] += stepWidth) {
835 +                                bmax[1] = bmin[1] + stepWidth;
836 +                                dhemi = SDavgTreBox(st, bmin, bmax);
837 +                                if (dhemi > df->maxHemi)
838 +                                        df->maxHemi = dhemi;
839 +                        }
840 +                }
841 +        } else
842 +                return SDEinternal;
843 +                                        /* correct hemispherical value */
844 +        df->maxHemi *= M_PI;
845 +        return SDEnone;
846 + }
847 +
848 + /* Load BSDF distribution for this wavelength */
849 + static SDError
850 + load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
851 + {
852 +        SDSpectralDF    *df;
853 +        SDTre           *sdt;
854 +        char            *sdata;
855 +        int             i;
856 +                                        /* allocate BSDF component */
857 +        sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
858 +        if (!sdata)
859 +                return SDEnone;
860 +        /*
861 +         * Remember that front and back are reversed from WINDOW 6 orientations
862 +         */
863 +        if (!strcasecmp(sdata, "Transmission")) {
864 +                if (sd->tf != NULL)
865 +                        SDfreeSpectralDF(sd->tf);
866 +                if ((sd->tf = SDnewSpectralDF(1)) == NULL)
867 +                        return SDEmemory;
868 +                df = sd->tf;
869 +        } else if (!strcasecmp(sdata, "Reflection Front")) {
870 +                if (sd->rb != NULL)     /* note back-front reversal */
871 +                        SDfreeSpectralDF(sd->rb);
872 +                if ((sd->rb = SDnewSpectralDF(1)) == NULL)
873 +                        return SDEmemory;
874 +                df = sd->rb;
875 +        } else if (!strcasecmp(sdata, "Reflection Back")) {
876 +                if (sd->rf != NULL)     /* note front-back reversal */
877 +                        SDfreeSpectralDF(sd->rf);
878 +                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
879 +                        return SDEmemory;
880 +                df = sd->rf;
881 +        } else
882 +                return SDEnone;
883 +        /* XXX should also check "ScatteringDataType" for consistency? */
884 +                                        /* get angle bases */
885 +        sdata = ezxml_txt(ezxml_child(wdb,"AngleBasis"));
886 +        if (!sdata || strcasecmp(sdata, "LBNL/Shirley-Chiu")) {
887 +                sprintf(SDerrorDetail, "%s angle basis for BSDF '%s'",
888 +                                !sdata ? "Missing" : "Unsupported", sd->name);
889 +                return !sdata ? SDEformat : SDEsupport;
890 +        }
891 +                                        /* allocate BSDF tree */
892 +        sdt = (SDTre *)malloc(sizeof(SDTre));
893 +        if (sdt == NULL)
894 +                return SDEmemory;
895 +        if (df == sd->rf)
896 +                sdt->sidef = SD_UFRONT;
897 +        else if (df == sd->rb)
898 +                sdt->sidef = SD_UBACK;
899 +        else
900 +                sdt->sidef = SD_XMIT;
901 +        sdt->st = NULL;
902 +        df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
903 +        df->comp[0].dist = sdt;
904 +        df->comp[0].func = &SDhandleTre;
905 +                                        /* read BSDF data */
906 +        sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData"));
907 +        if (!sdata || !next_token(&sdata)) {
908 +                sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'",
909 +                                sd->name);
910 +                return SDEformat;
911 +        }
912 +        sdt->st = load_tree_data(&sdata, ndim);
913 +        if (sdt->st == NULL)
914 +                return SDEformat;
915 +        if (next_token(&sdata)) {       /* check for unconsumed characters */
916 +                sprintf(SDerrorDetail,
917 +                        "Extra characters at end of ScatteringData in '%s'",
918 +                                sd->name);
919 +                return SDEformat;
920 +        }
921 +                                        /* flatten branches where possible */
922 +        sdt->st = SDsimplifyTre(sdt->st);
923 +        if (sdt->st == NULL)
924 +                return SDEinternal;
925 +        return get_extrema(df);         /* compute global quantities */
926 + }
927 +
928 + /* Find minimum value in tree */
929 + static float
930 + SDgetTreMin(const SDNode *st)
931 + {
932 +        float   vmin = FHUGE;
933 +        int     n;
934 +
935 +        if (st->log2GR < 0) {
936 +                for (n = 1<<st->ndim; n--; ) {
937 +                        float   v = SDgetTreMin(st->u.t[n]);
938 +                        if (v < vmin)
939 +                                vmin = v;
940 +                }
941 +        } else {
942 +                for (n = 1<<(st->ndim*st->log2GR); n--; )
943 +                        if (st->u.v[n] < vmin)
944 +                                vmin = st->u.v[n];
945 +        }
946 +        return vmin;
947 + }
948 +
949 + /* Subtract the given value from all tree nodes */
950 + static void
951 + SDsubtractTreVal(SDNode *st, float val)
952 + {
953 +        int     n;
954 +
955 +        if (st->log2GR < 0) {
956 +                for (n = 1<<st->ndim; n--; )
957 +                        SDsubtractTreVal(st->u.t[n], val);
958 +        } else {
959 +                for (n = 1<<(st->ndim*st->log2GR); n--; )
960 +                        st->u.v[n] -= val;
961 +        }
962 + }
963 +
964 + /* Subtract minimum value from BSDF */
965 + static double
966 + subtract_min(SDNode *st)
967 + {
968 +        float   vmin;
969 +                                        /* be sure to skip unused portion */
970 +        if (st->ndim == 3) {
971 +                int     n;
972 +                vmin = 1./M_PI;
973 +                if (st->log2GR < 0) {
974 +                        for (n = 0; n < 4; n++) {
975 +                                float   v = SDgetTreMin(st->u.t[n]);
976 +                                if (v < vmin)
977 +                                        vmin = v;
978 +                        }
979 +                } else if (st->log2GR) {
980 +                        for (n = 1 << (3*st->log2GR - 1); n--; )
981 +                                if (st->u.v[n] < vmin)
982 +                                        vmin = st->u.v[n];
983 +                } else
984 +                        vmin = st->u.v[0];
985 +        } else                          /* anisotropic covers entire tree */
986 +                vmin = SDgetTreMin(st);
987 +
988 +        if (vmin <= FTINY)
989 +                return .0;
990 +
991 +        SDsubtractTreVal(st, vmin);
992 +
993 +        return M_PI * vmin;             /* return hemispherical value */
994 + }
995 +
996 + /* Extract and separate diffuse portion of BSDF */
997 + static void
998 + extract_diffuse(SDValue *dv, SDSpectralDF *df)
999 + {
1000 +        int     n;
1001 +
1002 +        if (df == NULL || df->ncomp <= 0) {
1003 +                dv->spec = c_dfcolor;
1004 +                dv->cieY = .0;
1005 +                return;
1006 +        }
1007 +        dv->spec = df->comp[0].cspec[0];
1008 +        dv->cieY = subtract_min((*(SDTre *)df->comp[0].dist).st);
1009 +                                        /* in case of multiple components */
1010 +        for (n = df->ncomp; --n; ) {
1011 +                double  ymin = subtract_min((*(SDTre *)df->comp[n].dist).st);
1012 +                c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]);
1013 +                dv->cieY += ymin;
1014 +        }
1015 +        df->maxHemi -= dv->cieY;        /* adjust maximum hemispherical */
1016 +                                        /* make sure everything is set */
1017 +        c_ccvt(&dv->spec, C_CSXY+C_CSSPEC);
1018 + }
1019 +
1020   /* Load a variable-resolution BSDF tree from an open XML file */
1021   SDError
1022   SDloadTre(SDData *sd, ezxml_t wtl)
1023   {
1024 <        return SDEsupport;
1024 >        SDError         ec;
1025 >        ezxml_t         wld, wdb;
1026 >        int             rank;
1027 >        char            *txt;
1028 >                                        /* basic checks and tensor rank */
1029 >        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
1030 >                        "DataDefinition"), "IncidentDataStructure"));
1031 >        if (txt == NULL || !*txt) {
1032 >                sprintf(SDerrorDetail,
1033 >                        "BSDF \"%s\": missing IncidentDataStructure",
1034 >                                sd->name);
1035 >                return SDEformat;
1036 >        }
1037 >        if (!strcasecmp(txt, "TensorTree3"))
1038 >                rank = 3;
1039 >        else if (!strcasecmp(txt, "TensorTree4"))
1040 >                rank = 4;
1041 >        else {
1042 >                sprintf(SDerrorDetail,
1043 >                        "BSDF \"%s\": unsupported IncidentDataStructure",
1044 >                                sd->name);
1045 >                return SDEsupport;
1046 >        }
1047 >                                        /* load BSDF components */
1048 >        for (wld = ezxml_child(wtl, "WavelengthData");
1049 >                                wld != NULL; wld = wld->next) {
1050 >                if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
1051 >                                "Visible"))
1052 >                        continue;       /* just visible for now */
1053 >                for (wdb = ezxml_child(wld, "WavelengthDataBlock");
1054 >                                        wdb != NULL; wdb = wdb->next)
1055 >                        if ((ec = load_bsdf_data(sd, wdb, rank)) != SDEnone)
1056 >                                return ec;
1057 >        }
1058 >                                        /* separate diffuse components */
1059 >        extract_diffuse(&sd->rLambFront, sd->rf);
1060 >        extract_diffuse(&sd->rLambBack, sd->rb);
1061 >        extract_diffuse(&sd->tLamb, sd->tf);
1062 >                                        /* return success */
1063 >        return SDEnone;
1064   }
1065  
1066   /* Variable resolution BSDF methods */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines