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.14 by greg, Wed Jun 1 05:21:18 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]);
94 <        free((void *)st);
92 >        for (n = (st->log2GR < 0) << st->ndim; n--; )
93 >                SDfreeTre(st->u.t[n]);
94 >        free(st);
95   }
96  
97   /* Free a variable-resolution BSDF */
# 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 - 1);
129 +        float           *vptr = st->u.v;
130 +        int             i;
131 +
132 +        for (i = st->ndim; i--; skipsiz <<= st->log2GR)
133 +                if (1<<i & n)
134 +                        vptr += skipsiz;
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 137 | Line 214 | SDavgTreBox(const SDNode *st, const double *bmin, cons
214          for (i = st->ndim; i--; ) {
215                  if (bmin[i] >= 1.)
216                          return .0;
217 <                if (bmax[i] <= .0)
217 >                if (bmax[i] <= 0)
218                          return .0;
219                  if (bmin[i] >= bmax[i])
220                          return .0;
# Line 157 | Line 234 | SDavgTreBox(const SDNode *st, const double *bmin, cons
234                                  }
235                                  if (sbmin[i] < .0) sbmin[i] = .0;
236                                  if (sbmax[i] > 1.) sbmax[i] = 1.;
237 +                                if (sbmin[i] >= sbmax[i]) {
238 +                                        w = .0;
239 +                                        break;
240 +                                }
241                                  w *= sbmax[i] - sbmin[i];
242                          }
243                          if (w > 1e-10) {
# Line 168 | Line 249 | SDavgTreBox(const SDNode *st, const double *bmin, cons
249          }
250          n = 1;                          /* iterate over leaves */
251          for (i = st->ndim; i--; ) {
252 <                imin[i] = (bmin[i] <= .0) ? 0
252 >                imin[i] = (bmin[i] <= 0) ? 0
253                                  : (int)((1 << st->log2GR)*bmin[i]);
254                  imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR)
255                                  : (int)((1 << st->log2GR)*bmax[i] + .999999);
# Line 177 | Line 258 | SDavgTreBox(const SDNode *st, const double *bmin, cons
258          if (!n)
259                  return .0;
260          
261 <        return SDiterSum(st->u.v, st->ndim, 1 << st->log2GR, imin, imax) /
181 <                        (double)n;
261 >        return SDiterSum(st->u.v, st->ndim, st->log2GR, imin, imax) / (double)n;
262   }
263  
264   /* Recursive call for SDtraverseTre() */
# Line 193 | Line 273 | SDdotravTre(const SDNode *st, const double *pos, int c
273                                          /* in branches? */
274          if (st->log2GR < 0) {
275                  unsigned        skipmask = 0;
196
276                  csiz *= .5;
277                  for (i = st->ndim; i--; )
278                          if (1<<i & cmask)
279                                  if (pos[i] < cmin[i] + csiz)
280 <                                        for (n = 1 << st->ndim; n--; )
280 >                                        for (n = 1 << st->ndim; n--; ) {
281                                                  if (n & 1<<i)
282                                                          skipmask |= 1<<n;
283 +                                        }
284                                  else
285 <                                        for (n = 1 << st->ndim; n--; )
285 >                                        for (n = 1 << st->ndim; n--; ) {
286                                                  if (!(n & 1<<i))
287                                                          skipmask |= 1<<n;
288 +                                        }
289                  for (n = 1 << st->ndim; n--; ) {
290                          if (1<<n & skipmask)
291                                  continue;
# Line 337 | Line 418 | SDqueryTre(const SDTre *sdt, const FVECT outVec, const
418          static const FVECT      zvec = {.0, .0, 1.};
419          FVECT                   rOutVec;
420          double                  gridPos[4];
421 <                                        /* check transmission */
422 <        if (!sdt->isxmit ^ outVec[2] > 0 ^ inVec[2] > 0)
421 >
422 >        switch (sdt->sidef) {           /* whose side are you on? */
423 >        case SD_UFRONT:
424 >                if ((outVec[2] < 0) | (inVec[2] < 0))
425 >                        return -1.;
426 >                break;
427 >        case SD_UBACK:
428 >                if ((outVec[2] > 0) | (inVec[2] > 0))
429 >                        return -1.;
430 >                break;
431 >        case SD_XMIT:
432 >                if ((outVec[2] > 0) == (inVec[2] > 0))
433 >                        return -1.;
434 >                break;
435 >        default:
436                  return -1.;
437 +        }
438                                          /* convert vector coordinates */
439          if (sdt->st->ndim == 3) {
440                  spinvector(rOutVec, outVec, zvec, -atan2(inVec[1],inVec[0]));
# Line 383 | Line 478 | build_scaffold(float val, const double *cmin, double c
478                  sp->wmax = wid;
479          if (sp->alen >= sp->nall) {     /* need more space? */
480                  struct outdir_s *ndarr;
481 <                sp->nall += 8192;
481 >                sp->nall += 1024;
482                  ndarr = (struct outdir_s *)realloc(sp->darr,
483                                          sizeof(struct outdir_s)*sp->nall);
484 <                if (ndarr == NULL)
484 >                if (ndarr == NULL) {
485 >                        sprintf(SDerrorDetail,
486 >                                "Cannot grow scaffold to %u entries", sp->nall);
487                          return -1;      /* abort build */
488 +                }
489                  sp->darr = ndarr;
490          }
491                                          /* find Hilbert entry index */
492          bmin[0] = cmin[0]*(double)iwmax + .5;
493          bmin[1] = cmin[1]*(double)iwmax + .5;
494 <        bmax[0] = bmin[0] + wid;
495 <        bmax[1] = bmin[1] + wid;
496 <        hilbert_box_vtx(2, sizeof(bitmask_t), sizeof(unsigned)*4,
497 <                                                        1, bmin, bmax);
400 <        sp->darr[sp->alen].hent = hilbert_c2i(2, sizeof(unsigned)*4, bmin);
494 >        bmax[0] = bmin[0] + wid-1;
495 >        bmax[1] = bmin[1] + wid-1;
496 >        hilbert_box_vtx(2, sizeof(bitmask_t), iwbits, 1, bmin, bmax);
497 >        sp->darr[sp->alen].hent = hilbert_c2i(2, iwbits, bmin);
498          sp->darr[sp->alen].wid = wid;
499          sp->darr[sp->alen].bsdf = val;
500          sp->alen++;                     /* on to the next entry */
# Line 408 | Line 505 | build_scaffold(float val, const double *cmin, double c
505   static int
506   sscmp(const void *p1, const void *p2)
507   {
508 <        return (int)((*(const struct outdir_s *)p1).hent -
509 <                        (*(const struct outdir_s *)p2).hent);
508 >        unsigned        h1 = (*(const struct outdir_s *)p1).hent;
509 >        unsigned        h2 = (*(const struct outdir_s *)p2).hent;
510 >
511 >        if (h1 > h2)
512 >                return 1;
513 >        if (h1 < h2)
514 >                return -1;
515 >        return 0;
516   }
517  
518   /* Create a new cumulative distribution for the given input direction */
519   static SDTreCDst *
520   make_cdist(const SDTre *sdt, const double *pos)
521   {
419        const unsigned  cumlmax = ~0;
522          SDdistScaffold  myScaffold;
523          SDTreCDst       *cd;
524          struct outdir_s *sp;
# Line 427 | Line 529 | make_cdist(const SDTre *sdt, const double *pos)
529          myScaffold.wmax = 0;
530          myScaffold.nic = sdt->st->ndim - 2;
531          myScaffold.alen = 0;
532 <        myScaffold.nall = 8192;
532 >        myScaffold.nall = 512;
533          myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) *
534                                                          myScaffold.nall);
535          if (myScaffold.darr == NULL)
# Line 442 | Line 544 | make_cdist(const SDTre *sdt, const double *pos)
544          cd = (SDTreCDst *)malloc(sizeof(SDTreCDst) +
545                                  sizeof(cd->carr[0])*myScaffold.alen);
546          if (cd == NULL) {
547 +                sprintf(SDerrorDetail,
548 +                        "Cannot allocate %u entry cumulative distribution",
549 +                                myScaffold.alen);
550                  free(myScaffold.darr);
551                  return NULL;
552          }
# Line 450 | Line 555 | make_cdist(const SDTre *sdt, const double *pos)
555                                  sizeof(struct outdir_s), &sscmp);
556  
557                                          /* record input range */
558 <        scale = (double)myScaffold.wmin / iwmax;
558 >        scale = myScaffold.wmin / (double)iwmax;
559          for (i = myScaffold.nic; i--; ) {
560 <                cd->clim[i][0] = floor(pos[i]/scale + .5) * scale;
560 >                cd->clim[i][0] = floor(pos[i]/scale) * scale;
561                  cd->clim[i][1] = cd->clim[i][0] + scale;
562          }
563          cd->max_psa = myScaffold.wmax / (double)iwmax;
564          cd->max_psa *= cd->max_psa * M_PI;
565 <        cd->isxmit = sdt->isxmit;
565 >        cd->sidef = sdt->sidef;
566          cd->cTotal = 1e-20;             /* compute directional total */
567          sp = myScaffold.darr;
568          for (i = myScaffold.alen; i--; sp++)
# Line 466 | Line 571 | make_cdist(const SDTre *sdt, const double *pos)
571          scale = (double)cumlmax / cd->cTotal;
572          sp = myScaffold.darr;
573          for (i = 0; i < cd->calen; i++, sp++) {
574 +                cd->carr[i].hndx = sp->hent;
575                  cd->carr[i].cuml = scale*cursum + .5;
576                  cursum += sp->bsdf * (double)sp->wid * sp->wid;
577          }
# Line 531 | Line 637 | SDqueryTreProjSA(double *psa, const FVECT v1, const RR
637                  const SDTre     *sdt = (SDTre *)sdc->dist;
638                  double          hcube[SD_MAXDIM];
639                  if (SDqueryTre(sdt, v1, v2, hcube) < 0) {
640 <                        if (qflags == SDqueryVal)
641 <                                *psa = M_PI;
536 <                        return SDEnone;
640 >                        strcpy(SDerrorDetail, "Bad call to SDqueryTreProjSA");
641 >                        return SDEinternal;
642                  }
643                  myPSA[0] = hcube[sdt->st->ndim];
644                  myPSA[1] = myPSA[0] *= myPSA[0] * M_PI;
# Line 571 | Line 676 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
676   {
677          const unsigned  nBitsC = 4*sizeof(bitmask_t);
678          const unsigned  nExtraBits = 8*(sizeof(bitmask_t)-sizeof(unsigned));
574        const unsigned  maxval = ~0;
679          const SDTreCDst *cd = (const SDTreCDst *)cdp;
680 <        const unsigned  target = randX*maxval;
680 >        const unsigned  target = randX*cumlmax;
681          bitmask_t       hndx, hcoord[2];
682          double          gpos[3];
683          int             i, iupper, ilower;
684                                          /* check arguments */
685          if ((ioVec == NULL) | (cd == NULL))
686                  return SDEargument;
687 +        if (ioVec[2] > 0) {
688 +                if (!(cd->sidef & SD_UFRONT))
689 +                        return SDEargument;
690 +        } else if (!(cd->sidef & SD_UBACK))
691 +                return SDEargument;
692                                          /* binary search to find position */
693          ilower = 0; iupper = cd->calen;
694          while ((i = (iupper + ilower) >> 1) != ilower)
# Line 588 | Line 697 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
697                  else
698                          iupper = i;
699                                          /* localize random position */
700 <        randX = (randX*maxval - cd->carr[ilower].cuml) /
700 >        randX = (randX*cumlmax - cd->carr[ilower].cuml) /
701                      (double)(cd->carr[iupper].cuml - cd->carr[ilower].cuml);
702                                          /* index in longer Hilbert curve */
703          hndx = (randX*cd->carr[iupper].hndx + (1.-randX)*cd->carr[ilower].hndx)
# Line 599 | Line 708 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
708                  gpos[i] = ((double)hcoord[i] + rand()*(1./(RAND_MAX+.5))) /
709                                  (double)((bitmask_t)1 << nBitsC);
710          SDsquare2disk(gpos, gpos[0], gpos[1]);
711 +                                        /* compute Z-coordinate */
712          gpos[2] = 1. - gpos[0]*gpos[0] - gpos[1]*gpos[1];
713          if (gpos[2] > 0)                /* paranoia, I hope */
714                  gpos[2] = sqrt(gpos[2]);
715 <        if (ioVec[2] > 0 ^ !cd->isxmit)
715 >                                        /* emit from back? */
716 >        if (ioVec[2] > 0 ^ cd->sidef != SD_XMIT)
717                  gpos[2] = -gpos[2];
718          VCOPY(ioVec, gpos);
719          return SDEnone;
720   }
721  
722 + /* Advance pointer to the next non-white character in the string (or nul) */
723 + static int
724 + next_token(char **spp)
725 + {
726 +        while (isspace(**spp))
727 +                ++*spp;
728 +        return **spp;
729 + }
730 +
731 + /* Advance pointer past matching token (or any token if c==0) */
732 + #define eat_token(spp,c)        (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0)
733 +
734 + /* Count words from this point in string to '}' */
735 + static int
736 + count_values(char *cp)
737 + {
738 +        int     n = 0;
739 +
740 +        while (next_token(&cp) != '}' && *cp) {
741 +                while (!isspace(*cp) & (*cp != ',') & (*cp != '}'))
742 +                        if (!*++cp)
743 +                                break;
744 +                ++n;
745 +                eat_token(&cp, ',');
746 +        }
747 +        return n;
748 + }
749 +
750 + /* Load an array of real numbers, returning total */
751 + static int
752 + load_values(char **spp, float *va, int n)
753 + {
754 +        float   *v = va;
755 +        char    *svnext;
756 +
757 +        while (n-- > 0 && (svnext = fskip(*spp)) != NULL) {
758 +                *v++ = atof(*spp);
759 +                *spp = svnext;
760 +                eat_token(spp, ',');
761 +        }
762 +        return v - va;
763 + }
764 +
765 + /* Load BSDF tree data */
766 + static SDNode *
767 + load_tree_data(char **spp, int nd)
768 + {
769 +        SDNode  *st;
770 +        int     n;
771 +
772 +        if (!eat_token(spp, '{')) {
773 +                strcpy(SDerrorDetail, "Missing '{' in tensor tree");
774 +                return NULL;
775 +        }
776 +        if (next_token(spp) == '{') {   /* tree branches */
777 +                st = SDnewNode(nd, -1);
778 +                if (st == NULL)
779 +                        return NULL;
780 +                for (n = 0; n < 1<<nd; n++)
781 +                        if ((st->u.t[n] = load_tree_data(spp, nd)) == NULL) {
782 +                                SDfreeTre(st);
783 +                                return NULL;
784 +                        }
785 +        } else {                        /* else load value grid */
786 +                int     bsiz;
787 +                n = count_values(*spp); /* see how big the grid is */
788 +                for (bsiz = 0; bsiz < 8*sizeof(size_t)-1; bsiz += nd)
789 +                        if (1<<bsiz == n)
790 +                                break;
791 +                if (bsiz >= 8*sizeof(size_t)) {
792 +                        strcpy(SDerrorDetail, "Illegal value count in tensor tree");
793 +                        return NULL;
794 +                }
795 +                st = SDnewNode(nd, bsiz/nd);
796 +                if (st == NULL)
797 +                        return NULL;
798 +                if (load_values(spp, st->u.v, n) != n) {
799 +                        strcpy(SDerrorDetail, "Real format error in tensor tree");
800 +                        SDfreeTre(st);
801 +                        return NULL;
802 +                }
803 +        }
804 +        if (!eat_token(spp, '}')) {
805 +                strcpy(SDerrorDetail, "Missing '}' in tensor tree");
806 +                SDfreeTre(st);
807 +                return NULL;
808 +        }
809 +        eat_token(spp, ',');
810 +        return st;
811 + }
812 +
813 + /* Compute min. proj. solid angle and max. direct hemispherical scattering */
814 + static SDError
815 + get_extrema(SDSpectralDF *df)
816 + {
817 +        SDNode  *st = (*(SDTre *)df->comp[0].dist).st;
818 +        double  stepWidth, dhemi, bmin[4], bmax[4];
819 +
820 +        stepWidth = SDsmallestLeaf(st);
821 +        df->minProjSA = M_PI*stepWidth*stepWidth;
822 +        if (stepWidth < .03125)
823 +                stepWidth = .03125;     /* 1/32 resolution good enough */
824 +        df->maxHemi = .0;
825 +        if (st->ndim == 3) {            /* isotropic BSDF */
826 +                bmin[1] = bmin[2] = .0;
827 +                bmax[1] = bmax[2] = 1.;
828 +                for (bmin[0] = .0; bmin[0] < .5-FTINY; bmin[0] += stepWidth) {
829 +                        bmax[0] = bmin[0] + stepWidth;
830 +                        dhemi = SDavgTreBox(st, bmin, bmax);
831 +                        if (dhemi > df->maxHemi)
832 +                                df->maxHemi = dhemi;
833 +                }
834 +        } else if (st->ndim == 4) {     /* anisotropic BSDF */
835 +                bmin[2] = bmin[3] = .0;
836 +                bmax[2] = bmax[3] = 1.;
837 +                for (bmin[0] = .0; bmin[0] < 1.-FTINY; bmin[0] += stepWidth) {
838 +                        bmax[0] = bmin[0] + stepWidth;
839 +                        for (bmin[1] = .0; bmin[1] < 1.-FTINY; bmin[1] += stepWidth) {
840 +                                bmax[1] = bmin[1] + stepWidth;
841 +                                dhemi = SDavgTreBox(st, bmin, bmax);
842 +                                if (dhemi > df->maxHemi)
843 +                                        df->maxHemi = dhemi;
844 +                        }
845 +                }
846 +        } else
847 +                return SDEinternal;
848 +                                        /* correct hemispherical value */
849 +        df->maxHemi *= M_PI;
850 +        return SDEnone;
851 + }
852 +
853 + /* Load BSDF distribution for this wavelength */
854 + static SDError
855 + load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
856 + {
857 +        SDSpectralDF    *df;
858 +        SDTre           *sdt;
859 +        char            *sdata;
860 +        int             i;
861 +                                        /* allocate BSDF component */
862 +        sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
863 +        if (!sdata)
864 +                return SDEnone;
865 +        /*
866 +         * Remember that front and back are reversed from WINDOW 6 orientations
867 +         */
868 +        if (!strcasecmp(sdata, "Transmission")) {
869 +                if (sd->tf != NULL)
870 +                        SDfreeSpectralDF(sd->tf);
871 +                if ((sd->tf = SDnewSpectralDF(1)) == NULL)
872 +                        return SDEmemory;
873 +                df = sd->tf;
874 +        } else if (!strcasecmp(sdata, "Reflection Front")) {
875 +                if (sd->rb != NULL)     /* note back-front reversal */
876 +                        SDfreeSpectralDF(sd->rb);
877 +                if ((sd->rb = SDnewSpectralDF(1)) == NULL)
878 +                        return SDEmemory;
879 +                df = sd->rb;
880 +        } else if (!strcasecmp(sdata, "Reflection Back")) {
881 +                if (sd->rf != NULL)     /* note front-back reversal */
882 +                        SDfreeSpectralDF(sd->rf);
883 +                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
884 +                        return SDEmemory;
885 +                df = sd->rf;
886 +        } else
887 +                return SDEnone;
888 +        /* XXX should also check "ScatteringDataType" for consistency? */
889 +                                        /* get angle bases */
890 +        sdata = ezxml_txt(ezxml_child(wdb,"AngleBasis"));
891 +        if (!sdata || strcasecmp(sdata, "LBNL/Shirley-Chiu")) {
892 +                sprintf(SDerrorDetail, "%s angle basis for BSDF '%s'",
893 +                                !sdata ? "Missing" : "Unsupported", sd->name);
894 +                return !sdata ? SDEformat : SDEsupport;
895 +        }
896 +                                        /* allocate BSDF tree */
897 +        sdt = (SDTre *)malloc(sizeof(SDTre));
898 +        if (sdt == NULL)
899 +                return SDEmemory;
900 +        if (df == sd->rf)
901 +                sdt->sidef = SD_UFRONT;
902 +        else if (df == sd->rb)
903 +                sdt->sidef = SD_UBACK;
904 +        else
905 +                sdt->sidef = SD_XMIT;
906 +        sdt->st = NULL;
907 +        df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
908 +        df->comp[0].dist = sdt;
909 +        df->comp[0].func = &SDhandleTre;
910 +                                        /* read BSDF data */
911 +        sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData"));
912 +        if (!sdata || !next_token(&sdata)) {
913 +                sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'",
914 +                                sd->name);
915 +                return SDEformat;
916 +        }
917 +        sdt->st = load_tree_data(&sdata, ndim);
918 +        if (sdt->st == NULL)
919 +                return SDEformat;
920 +        if (next_token(&sdata)) {       /* check for unconsumed characters */
921 +                sprintf(SDerrorDetail,
922 +                        "Extra characters at end of ScatteringData in '%s'",
923 +                                sd->name);
924 +                return SDEformat;
925 +        }
926 +                                        /* flatten branches where possible */
927 +        sdt->st = SDsimplifyTre(sdt->st);
928 +        if (sdt->st == NULL)
929 +                return SDEinternal;
930 +        return get_extrema(df);         /* compute global quantities */
931 + }
932 +
933 + /* Find minimum value in tree */
934 + static float
935 + SDgetTreMin(const SDNode *st)
936 + {
937 +        float   vmin = FHUGE;
938 +        int     n;
939 +
940 +        if (st->log2GR < 0) {
941 +                for (n = 1<<st->ndim; n--; ) {
942 +                        float   v = SDgetTreMin(st->u.t[n]);
943 +                        if (v < vmin)
944 +                                vmin = v;
945 +                }
946 +        } else {
947 +                for (n = 1<<(st->ndim*st->log2GR); n--; )
948 +                        if (st->u.v[n] < vmin)
949 +                                vmin = st->u.v[n];
950 +        }
951 +        return vmin;
952 + }
953 +
954 + /* Subtract the given value from all tree nodes */
955 + static void
956 + SDsubtractTreVal(SDNode *st, float val)
957 + {
958 +        int     n;
959 +
960 +        if (st->log2GR < 0) {
961 +                for (n = 1<<st->ndim; n--; )
962 +                        SDsubtractTreVal(st->u.t[n], val);
963 +        } else {
964 +                for (n = 1<<(st->ndim*st->log2GR); n--; )
965 +                        st->u.v[n] -= val;
966 +        }
967 + }
968 +
969 + /* Subtract minimum value from BSDF */
970 + static double
971 + subtract_min(SDNode *st)
972 + {
973 +        float   vmin;
974 +                                        /* be sure to skip unused portion */
975 +        if (st->ndim == 3) {
976 +                int     n;
977 +                vmin = 1./M_PI;
978 +                if (st->log2GR < 0) {
979 +                        for (n = 0; n < 4; n++) {
980 +                                float   v = SDgetTreMin(st->u.t[n]);
981 +                                if (v < vmin)
982 +                                        vmin = v;
983 +                        }
984 +                } else if (st->log2GR) {
985 +                        for (n = 1 << (3*st->log2GR - 1); n--; )
986 +                                if (st->u.v[n] < vmin)
987 +                                        vmin = st->u.v[n];
988 +                } else
989 +                        vmin = st->u.v[0];
990 +        } else                          /* anisotropic covers entire tree */
991 +                vmin = SDgetTreMin(st);
992 +
993 +        if (vmin <= FTINY)
994 +                return .0;
995 +
996 +        SDsubtractTreVal(st, vmin);
997 +
998 +        return M_PI * vmin;             /* return hemispherical value */
999 + }
1000 +
1001 + /* Extract and separate diffuse portion of BSDF */
1002 + static void
1003 + extract_diffuse(SDValue *dv, SDSpectralDF *df)
1004 + {
1005 +        int     n;
1006 +
1007 +        if (df == NULL || df->ncomp <= 0) {
1008 +                dv->spec = c_dfcolor;
1009 +                dv->cieY = .0;
1010 +                return;
1011 +        }
1012 +        dv->spec = df->comp[0].cspec[0];
1013 +        dv->cieY = subtract_min((*(SDTre *)df->comp[0].dist).st);
1014 +                                        /* in case of multiple components */
1015 +        for (n = df->ncomp; --n; ) {
1016 +                double  ymin = subtract_min((*(SDTre *)df->comp[n].dist).st);
1017 +                c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]);
1018 +                dv->cieY += ymin;
1019 +        }
1020 +        df->maxHemi -= dv->cieY;        /* adjust maximum hemispherical */
1021 +                                        /* make sure everything is set */
1022 +        c_ccvt(&dv->spec, C_CSXY+C_CSSPEC);
1023 + }
1024 +
1025   /* Load a variable-resolution BSDF tree from an open XML file */
1026   SDError
1027   SDloadTre(SDData *sd, ezxml_t wtl)
1028   {
1029 <        return SDEsupport;
1029 >        SDError         ec;
1030 >        ezxml_t         wld, wdb;
1031 >        int             rank;
1032 >        char            *txt;
1033 >                                        /* basic checks and tensor rank */
1034 >        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
1035 >                        "DataDefinition"), "IncidentDataStructure"));
1036 >        if (txt == NULL || !*txt) {
1037 >                sprintf(SDerrorDetail,
1038 >                        "BSDF \"%s\": missing IncidentDataStructure",
1039 >                                sd->name);
1040 >                return SDEformat;
1041 >        }
1042 >        if (!strcasecmp(txt, "TensorTree3"))
1043 >                rank = 3;
1044 >        else if (!strcasecmp(txt, "TensorTree4"))
1045 >                rank = 4;
1046 >        else {
1047 >                sprintf(SDerrorDetail,
1048 >                        "BSDF \"%s\": unsupported IncidentDataStructure",
1049 >                                sd->name);
1050 >                return SDEsupport;
1051 >        }
1052 >                                        /* load BSDF components */
1053 >        for (wld = ezxml_child(wtl, "WavelengthData");
1054 >                                wld != NULL; wld = wld->next) {
1055 >                if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
1056 >                                "Visible"))
1057 >                        continue;       /* just visible for now */
1058 >                for (wdb = ezxml_child(wld, "WavelengthDataBlock");
1059 >                                        wdb != NULL; wdb = wdb->next)
1060 >                        if ((ec = load_bsdf_data(sd, wdb, rank)) != SDEnone)
1061 >                                return ec;
1062 >        }
1063 >                                        /* separate diffuse components */
1064 >        extract_diffuse(&sd->rLambFront, sd->rf);
1065 >        extract_diffuse(&sd->rLambBack, sd->rb);
1066 >        extract_diffuse(&sd->tLamb, sd->tf);
1067 >                                        /* return success */
1068 >        return SDEnone;
1069   }
1070  
1071   /* Variable resolution BSDF methods */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines