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.11 by greg, Thu Apr 28 17:46:25 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]));
63 >                                sizeof(st->u.t[0])*((1<<nd) - 1));
64                  if (st != NULL)
65                          memset(st->u.t, 0, sizeof(st->u.t[0])<<nd);
66          } else
67                  st = (SDNode *)malloc(sizeof(SDNode) +
68 <                                ((1 << nd*lg) - 1)*sizeof(st->u.v[0]));
68 >                                sizeof(st->u.v[0])*((1 << nd*lg) - 1));
69                  
70          if (st == NULL) {
71                  if (lg < 0)
# 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 393 | Line 483 | build_scaffold(float val, const double *cmin, double c
483                                          /* find Hilbert entry index */
484          bmin[0] = cmin[0]*(double)iwmax + .5;
485          bmin[1] = cmin[1]*(double)iwmax + .5;
486 <        bmax[0] = bmin[0] + wid;
487 <        bmax[1] = bmin[1] + wid;
488 <        hilbert_box_vtx(2, sizeof(bitmask_t), sizeof(unsigned)*4,
489 <                                                        1, bmin, bmax);
400 <        sp->darr[sp->alen].hent = hilbert_c2i(2, sizeof(unsigned)*4, bmin);
486 >        bmax[0] = bmin[0] + wid-1;
487 >        bmax[1] = bmin[1] + wid-1;
488 >        hilbert_box_vtx(2, sizeof(bitmask_t), iwbits, 1, bmin, bmax);
489 >        sp->darr[sp->alen].hent = hilbert_c2i(2, iwbits, bmin);
490          sp->darr[sp->alen].wid = wid;
491          sp->darr[sp->alen].bsdf = val;
492          sp->alen++;                     /* on to the next entry */
# Line 408 | Line 497 | build_scaffold(float val, const double *cmin, double c
497   static int
498   sscmp(const void *p1, const void *p2)
499   {
500 <        return (int)((*(const struct outdir_s *)p1).hent -
501 <                        (*(const struct outdir_s *)p2).hent);
500 >        unsigned        h1 = (*(const struct outdir_s *)p1).hent;
501 >        unsigned        h2 = (*(const struct outdir_s *)p2).hent;
502 >
503 >        if (h1 > h2)
504 >                return 1;
505 >        if (h1 < h2)
506 >                return -1;
507 >        return 0;
508   }
509  
510   /* Create a new cumulative distribution for the given input direction */
511   static SDTreCDst *
512   make_cdist(const SDTre *sdt, const double *pos)
513   {
419        const unsigned  cumlmax = ~0;
514          SDdistScaffold  myScaffold;
515          SDTreCDst       *cd;
516          struct outdir_s *sp;
# Line 450 | Line 544 | make_cdist(const SDTre *sdt, const double *pos)
544                                  sizeof(struct outdir_s), &sscmp);
545  
546                                          /* record input range */
547 <        scale = (double)myScaffold.wmin / iwmax;
547 >        scale = myScaffold.wmin / (double)iwmax;
548          for (i = myScaffold.nic; i--; ) {
549 <                cd->clim[i][0] = floor(pos[i]/scale + .5) * scale;
549 >                cd->clim[i][0] = floor(pos[i]/scale) * scale;
550                  cd->clim[i][1] = cd->clim[i][0] + scale;
551          }
552          cd->max_psa = myScaffold.wmax / (double)iwmax;
553          cd->max_psa *= cd->max_psa * M_PI;
554 <        cd->isxmit = sdt->isxmit;
554 >        cd->sidef = sdt->sidef;
555          cd->cTotal = 1e-20;             /* compute directional total */
556          sp = myScaffold.darr;
557          for (i = myScaffold.alen; i--; sp++)
# Line 466 | Line 560 | make_cdist(const SDTre *sdt, const double *pos)
560          scale = (double)cumlmax / cd->cTotal;
561          sp = myScaffold.darr;
562          for (i = 0; i < cd->calen; i++, sp++) {
563 +                cd->carr[i].hndx = sp->hent;
564                  cd->carr[i].cuml = scale*cursum + .5;
565                  cursum += sp->bsdf * (double)sp->wid * sp->wid;
566          }
# Line 531 | Line 626 | SDqueryTreProjSA(double *psa, const FVECT v1, const RR
626                  const SDTre     *sdt = (SDTre *)sdc->dist;
627                  double          hcube[SD_MAXDIM];
628                  if (SDqueryTre(sdt, v1, v2, hcube) < 0) {
629 <                        if (qflags == SDqueryVal)
630 <                                *psa = M_PI;
536 <                        return SDEnone;
629 >                        strcpy(SDerrorDetail, "Bad call to SDqueryTreProjSA");
630 >                        return SDEinternal;
631                  }
632                  myPSA[0] = hcube[sdt->st->ndim];
633                  myPSA[1] = myPSA[0] *= myPSA[0] * M_PI;
# Line 571 | Line 665 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
665   {
666          const unsigned  nBitsC = 4*sizeof(bitmask_t);
667          const unsigned  nExtraBits = 8*(sizeof(bitmask_t)-sizeof(unsigned));
574        const unsigned  maxval = ~0;
668          const SDTreCDst *cd = (const SDTreCDst *)cdp;
669 <        const unsigned  target = randX*maxval;
669 >        const unsigned  target = randX*cumlmax;
670          bitmask_t       hndx, hcoord[2];
671          double          gpos[3];
672          int             i, iupper, ilower;
673                                          /* check arguments */
674          if ((ioVec == NULL) | (cd == NULL))
675                  return SDEargument;
676 +        if (ioVec[2] > 0) {
677 +                if (!(cd->sidef & SD_UFRONT))
678 +                        return SDEargument;
679 +        } else if (!(cd->sidef & SD_UBACK))
680 +                return SDEargument;
681                                          /* binary search to find position */
682          ilower = 0; iupper = cd->calen;
683          while ((i = (iupper + ilower) >> 1) != ilower)
# Line 588 | Line 686 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
686                  else
687                          iupper = i;
688                                          /* localize random position */
689 <        randX = (randX*maxval - cd->carr[ilower].cuml) /
689 >        randX = (randX*cumlmax - cd->carr[ilower].cuml) /
690                      (double)(cd->carr[iupper].cuml - cd->carr[ilower].cuml);
691                                          /* index in longer Hilbert curve */
692          hndx = (randX*cd->carr[iupper].hndx + (1.-randX)*cd->carr[ilower].hndx)
# Line 599 | Line 697 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
697                  gpos[i] = ((double)hcoord[i] + rand()*(1./(RAND_MAX+.5))) /
698                                  (double)((bitmask_t)1 << nBitsC);
699          SDsquare2disk(gpos, gpos[0], gpos[1]);
700 +                                        /* compute Z-coordinate */
701          gpos[2] = 1. - gpos[0]*gpos[0] - gpos[1]*gpos[1];
702          if (gpos[2] > 0)                /* paranoia, I hope */
703                  gpos[2] = sqrt(gpos[2]);
704 <        if (ioVec[2] > 0 ^ !cd->isxmit)
704 >                                        /* emit from back? */
705 >        if (ioVec[2] > 0 ^ cd->sidef != SD_XMIT)
706                  gpos[2] = -gpos[2];
707          VCOPY(ioVec, gpos);
708          return SDEnone;
709   }
710  
711 + /* Advance pointer to the next non-white character in the string (or nul) */
712 + static int
713 + next_token(char **spp)
714 + {
715 +        while (isspace(**spp))
716 +                ++*spp;
717 +        return **spp;
718 + }
719 +
720 + #define eat_token(spp,c)        (next_token(spp)==(c) ? *(*(spp))++ : 0)
721 +
722 + /* Count words from this point in string to '}' */
723 + static int
724 + count_values(char *cp)
725 + {
726 +        int     n = 0;
727 +
728 +        while (next_token(&cp) != '}' && *cp) {
729 +                while (!isspace(*cp) & (*cp != ',') & (*cp != '}'))
730 +                        if (!*++cp)
731 +                                break;
732 +                ++n;
733 +                eat_token(&cp, ',');
734 +        }
735 +        return n;
736 + }
737 +
738 + /* Load an array of real numbers, returning total */
739 + static int
740 + load_values(char **spp, float *va, int n)
741 + {
742 +        float   *v = va;
743 +        char    *svnext;
744 +
745 +        while (n-- > 0 && (svnext = fskip(*spp)) != NULL) {
746 +                *v++ = atof(*spp);
747 +                *spp = svnext;
748 +                eat_token(spp, ',');
749 +        }
750 +        return v - va;
751 + }
752 +
753 + /* Load BSDF tree data */
754 + static SDNode *
755 + load_tree_data(char **spp, int nd)
756 + {
757 +        SDNode  *st;
758 +        int     n;
759 +
760 +        if (!eat_token(spp, '{')) {
761 +                strcpy(SDerrorDetail, "Missing '{' in tensor tree");
762 +                return NULL;
763 +        }
764 +        if (next_token(spp) == '{') {   /* tree branches */
765 +                st = SDnewNode(nd, -1);
766 +                if (st == NULL)
767 +                        return NULL;
768 +                for (n = 0; n < 1<<nd; n++)
769 +                        if ((st->u.t[n] = load_tree_data(spp, nd)) == NULL) {
770 +                                SDfreeTre(st);
771 +                                return NULL;
772 +                        }
773 +        } else {                        /* else load value grid */
774 +                int     bsiz;
775 +                n = count_values(*spp); /* see how big the grid is */
776 +                for (bsiz = 0; bsiz < 8*sizeof(size_t)-1; bsiz += nd)
777 +                        if (1<<bsiz == n)
778 +                                break;
779 +                if (bsiz >= 8*sizeof(size_t)) {
780 +                        strcpy(SDerrorDetail, "Illegal value count in tensor tree");
781 +                        return NULL;
782 +                }
783 +                st = SDnewNode(nd, bsiz/nd);
784 +                if (st == NULL)
785 +                        return NULL;
786 +                if (load_values(spp, st->u.v, n) != n) {
787 +                        strcpy(SDerrorDetail, "Real format error in tensor tree");
788 +                        SDfreeTre(st);
789 +                        return NULL;
790 +                }
791 +        }
792 +        if (!eat_token(spp, '}')) {
793 +                strcpy(SDerrorDetail, "Missing '}' in tensor tree");
794 +                SDfreeTre(st);
795 +                return NULL;
796 +        }
797 +        eat_token(spp, ',');
798 +        return st;
799 + }
800 +
801 + /* Compute min. proj. solid angle and max. direct hemispherical scattering */
802 + static SDError
803 + get_extrema(SDSpectralDF *df)
804 + {
805 +        SDNode  *st = (*(SDTre *)df->comp[0].dist).st;
806 +        double  stepWidth, dhemi, bmin[4], bmax[4];
807 +
808 +        stepWidth = SDsmallestLeaf(st);
809 +        df->minProjSA = M_PI*stepWidth*stepWidth;
810 +        if (stepWidth < .03125)
811 +                stepWidth = .03125;     /* 1/32 resolution good enough */
812 +        df->maxHemi = .0;
813 +        if (st->ndim == 3) {            /* isotropic BSDF */
814 +                bmin[1] = bmin[2] = .0;
815 +                bmax[1] = bmax[2] = 1.;
816 +                for (bmin[0] = .0; bmin[0] < .5-FTINY; bmin[0] += stepWidth) {
817 +                        bmax[0] = bmin[0] + stepWidth;
818 +                        dhemi = SDavgTreBox(st, bmin, bmax);
819 +                        if (dhemi > df->maxHemi)
820 +                                df->maxHemi = dhemi;
821 +                }
822 +        } else if (st->ndim == 4) {     /* anisotropic BSDF */
823 +                bmin[2] = bmin[3] = .0;
824 +                bmax[2] = bmax[3] = 1.;
825 +                for (bmin[0] = .0; bmin[0] < 1.-FTINY; bmin[0] += stepWidth) {
826 +                        bmax[0] = bmin[0] + stepWidth;
827 +                        for (bmin[1] = .0; bmin[1] < 1.-FTINY; bmin[1] += stepWidth) {
828 +                                bmax[1] = bmin[1] + stepWidth;
829 +                                dhemi = SDavgTreBox(st, bmin, bmax);
830 +                                if (dhemi > df->maxHemi)
831 +                                        df->maxHemi = dhemi;
832 +                        }
833 +                }
834 +        } else
835 +                return SDEinternal;
836 +                                        /* correct hemispherical value */
837 +        df->maxHemi *= M_PI;
838 +        return SDEnone;
839 + }
840 +
841 + /* Load BSDF distribution for this wavelength */
842 + static SDError
843 + load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
844 + {
845 +        SDSpectralDF    *df;
846 +        SDTre           *sdt;
847 +        char            *sdata;
848 +        int             i;
849 +                                        /* allocate BSDF component */
850 +        sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection"));
851 +        if (!sdata)
852 +                return SDEnone;
853 +        /*
854 +         * Remember that front and back are reversed from WINDOW 6 orientations
855 +         */
856 +        if (!strcasecmp(sdata, "Transmission")) {
857 +                if (sd->tf != NULL)
858 +                        SDfreeSpectralDF(sd->tf);
859 +                if ((sd->tf = SDnewSpectralDF(1)) == NULL)
860 +                        return SDEmemory;
861 +                df = sd->tf;
862 +        } else if (!strcasecmp(sdata, "Reflection Front")) {
863 +                if (sd->rb != NULL)     /* note back-front reversal */
864 +                        SDfreeSpectralDF(sd->rb);
865 +                if ((sd->rb = SDnewSpectralDF(1)) == NULL)
866 +                        return SDEmemory;
867 +                df = sd->rb;
868 +        } else if (!strcasecmp(sdata, "Reflection Back")) {
869 +                if (sd->rf != NULL)     /* note front-back reversal */
870 +                        SDfreeSpectralDF(sd->rf);
871 +                if ((sd->rf = SDnewSpectralDF(1)) == NULL)
872 +                        return SDEmemory;
873 +                df = sd->rf;
874 +        } else
875 +                return SDEnone;
876 +        /* XXX should also check "ScatteringDataType" for consistency? */
877 +                                        /* get angle bases */
878 +        sdata = ezxml_txt(ezxml_child(wdb,"AngleBasis"));
879 +        if (!sdata || strcasecmp(sdata, "LBNL/Shirley-Chiu")) {
880 +                sprintf(SDerrorDetail, "%s angle basis for BSDF '%s'",
881 +                                !sdata ? "Missing" : "Unsupported", sd->name);
882 +                return !sdata ? SDEformat : SDEsupport;
883 +        }
884 +                                        /* allocate BSDF tree */
885 +        sdt = (SDTre *)malloc(sizeof(SDTre));
886 +        if (sdt == NULL)
887 +                return SDEmemory;
888 +        if (df == sd->rf)
889 +                sdt->sidef = SD_UFRONT;
890 +        else if (df == sd->rb)
891 +                sdt->sidef = SD_UBACK;
892 +        else
893 +                sdt->sidef = SD_XMIT;
894 +        sdt->st = NULL;
895 +        df->comp[0].cspec[0] = c_dfcolor; /* XXX monochrome for now */
896 +        df->comp[0].dist = sdt;
897 +        df->comp[0].func = &SDhandleTre;
898 +                                        /* read BSDF data */
899 +        sdata = ezxml_txt(ezxml_child(wdb, "ScatteringData"));
900 +        if (!sdata || !next_token(&sdata)) {
901 +                sprintf(SDerrorDetail, "Missing BSDF ScatteringData in '%s'",
902 +                                sd->name);
903 +                return SDEformat;
904 +        }
905 +        sdt->st = load_tree_data(&sdata, ndim);
906 +        if (sdt->st == NULL)
907 +                return SDEformat;
908 +        if (next_token(&sdata)) {       /* check for unconsumed characters */
909 +                sprintf(SDerrorDetail,
910 +                        "Extra characters at end of ScatteringData in '%s'",
911 +                                sd->name);
912 +                return SDEformat;
913 +        }
914 +                                        /* flatten branches where possible */
915 +        sdt->st = SDsimplifyTre(sdt->st);
916 +        if (sdt->st == NULL)
917 +                return SDEinternal;
918 +        return get_extrema(df);         /* compute global quantities */
919 + }
920 +
921 + /* Find minimum value in tree */
922 + static float
923 + SDgetTreMin(const SDNode *st)
924 + {
925 +        float   vmin = FHUGE;
926 +        int     n;
927 +
928 +        if (st->log2GR < 0) {
929 +                for (n = 1<<st->ndim; n--; ) {
930 +                        float   v = SDgetTreMin(st->u.t[n]);
931 +                        if (v < vmin)
932 +                                vmin = v;
933 +                }
934 +        } else {
935 +                for (n = 1<<(st->ndim*st->log2GR); n--; )
936 +                        if (st->u.v[n] < vmin)
937 +                                vmin = st->u.v[n];
938 +        }
939 +        return vmin;
940 + }
941 +
942 + /* Subtract the given value from all tree nodes */
943 + static void
944 + SDsubtractTreVal(SDNode *st, float val)
945 + {
946 +        int     n;
947 +
948 +        if (st->log2GR < 0) {
949 +                for (n = 1<<st->ndim; n--; )
950 +                        SDsubtractTreVal(st->u.t[n], val);
951 +        } else {
952 +                for (n = 1<<(st->ndim*st->log2GR); n--; )
953 +                        st->u.v[n] -= val;
954 +        }
955 + }
956 +
957 + /* Subtract minimum value from BSDF */
958 + static double
959 + subtract_min(SDNode *st)
960 + {
961 +        float   vmin;
962 +                                        /* be sure to skip unused portion */
963 +        if (st->ndim == 3) {
964 +                int     n;
965 +                vmin = 1./M_PI;
966 +                if (st->log2GR < 0) {
967 +                        for (n = 0; n < 4; n++) {
968 +                                float   v = SDgetTreMin(st->u.t[n]);
969 +                                if (v < vmin)
970 +                                        vmin = v;
971 +                        }
972 +                } else if (st->log2GR) {
973 +                        for (n = 1 << (3*st->log2GR - 1); n--; )
974 +                                if (st->u.v[n] < vmin)
975 +                                        vmin = st->u.v[n];
976 +                } else
977 +                        vmin = st->u.v[0];
978 +        } else                          /* anisotropic covers entire tree */
979 +                vmin = SDgetTreMin(st);
980 +
981 +        if (vmin <= FTINY)
982 +                return .0;
983 +
984 +        SDsubtractTreVal(st, vmin);
985 +
986 +        return M_PI * vmin;             /* return hemispherical value */
987 + }
988 +
989 + /* Extract and separate diffuse portion of BSDF */
990 + static void
991 + extract_diffuse(SDValue *dv, SDSpectralDF *df)
992 + {
993 +        int     n;
994 +
995 +        if (df == NULL || df->ncomp <= 0) {
996 +                dv->spec = c_dfcolor;
997 +                dv->cieY = .0;
998 +                return;
999 +        }
1000 +        dv->spec = df->comp[0].cspec[0];
1001 +        dv->cieY = subtract_min((*(SDTre *)df->comp[0].dist).st);
1002 +                                        /* in case of multiple components */
1003 +        for (n = df->ncomp; --n; ) {
1004 +                double  ymin = subtract_min((*(SDTre *)df->comp[n].dist).st);
1005 +                c_cmix(&dv->spec, dv->cieY, &dv->spec, ymin, &df->comp[n].cspec[0]);
1006 +                dv->cieY += ymin;
1007 +        }
1008 +        df->maxHemi -= dv->cieY;        /* adjust maximum hemispherical */
1009 +                                        /* make sure everything is set */
1010 +        c_ccvt(&dv->spec, C_CSXY+C_CSSPEC);
1011 + }
1012 +
1013   /* Load a variable-resolution BSDF tree from an open XML file */
1014   SDError
1015   SDloadTre(SDData *sd, ezxml_t wtl)
1016   {
1017 <        return SDEsupport;
1017 >        SDError         ec;
1018 >        ezxml_t         wld, wdb;
1019 >        int             rank;
1020 >        char            *txt;
1021 >                                        /* basic checks and tensor rank */
1022 >        txt = ezxml_txt(ezxml_child(ezxml_child(wtl,
1023 >                        "DataDefinition"), "IncidentDataStructure"));
1024 >        if (txt == NULL || !*txt) {
1025 >                sprintf(SDerrorDetail,
1026 >                        "BSDF \"%s\": missing IncidentDataStructure",
1027 >                                sd->name);
1028 >                return SDEformat;
1029 >        }
1030 >        if (!strcasecmp(txt, "TensorTree3"))
1031 >                rank = 3;
1032 >        else if (!strcasecmp(txt, "TensorTree4"))
1033 >                rank = 4;
1034 >        else {
1035 >                sprintf(SDerrorDetail,
1036 >                        "BSDF \"%s\": unsupported IncidentDataStructure",
1037 >                                sd->name);
1038 >                return SDEsupport;
1039 >        }
1040 >                                        /* load BSDF components */
1041 >        for (wld = ezxml_child(wtl, "WavelengthData");
1042 >                                wld != NULL; wld = wld->next) {
1043 >                if (strcasecmp(ezxml_txt(ezxml_child(wld,"Wavelength")),
1044 >                                "Visible"))
1045 >                        continue;       /* just visible for now */
1046 >                for (wdb = ezxml_child(wld, "WavelengthDataBlock");
1047 >                                        wdb != NULL; wdb = wdb->next)
1048 >                        if ((ec = load_bsdf_data(sd, wdb, rank)) != SDEnone)
1049 >                                return ec;
1050 >        }
1051 >                                        /* separate diffuse components */
1052 >        extract_diffuse(&sd->rLambFront, sd->rf);
1053 >        extract_diffuse(&sd->rLambBack, sd->rb);
1054 >        extract_diffuse(&sd->tLamb, sd->tf);
1055 >                                        /* return success */
1056 >        return SDEnone;
1057   }
1058  
1059   /* Variable resolution BSDF methods */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines