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.8 by greg, Wed Apr 27 20:05:15 2011 UTC vs.
Revision 3.17 by greg, Thu Jun 9 17:09:39 2011 UTC

# Line 10 | Line 10 | static const char RCSid[] = "$Id$";
10   *
11   */
12  
13 + #define _USE_MATH_DEFINES
14   #include "rtio.h"
15   #include <stdlib.h>
16   #include <math.h>
# Line 28 | Line 29 | static const unsigned  iwbits = sizeof(unsigned)*4;
29   static const unsigned   iwmax = (1<<(sizeof(unsigned)*4))-1;
30                                          /* maximum cumulative value */
31   static const unsigned   cumlmax = ~0;
32 +                                        /* constant z-vector */
33 + static const FVECT      zvec = {.0, .0, 1.};
34  
35   /* Struct used for our distribution-building callback */
36   typedef struct {
# Line 61 | Line 64 | SDnewNode(int nd, int lg)
64          if (lg < 0) {
65                  st = (SDNode *)malloc(sizeof(SDNode) +
66                                  sizeof(st->u.t[0])*((1<<nd) - 1));
67 <                if (st != NULL)
65 <                        memset(st->u.t, 0, sizeof(st->u.t[0])<<nd);
66 <        } else
67 <                st = (SDNode *)malloc(sizeof(SDNode) +
68 <                                sizeof(st->u.v[0])*((1 << nd*lg) - 1));
69 <                
70 <        if (st == NULL) {
71 <                if (lg < 0)
67 >                if (st == NULL) {
68                          sprintf(SDerrorDetail,
69                                  "Cannot allocate %d branch BSDF tree", 1<<nd);
70 <                else
70 >                        return NULL;
71 >                }
72 >                memset(st->u.t, 0, sizeof(st->u.t[0])<<nd);
73 >        } else {
74 >                st = (SDNode *)malloc(sizeof(SDNode) +
75 >                                sizeof(st->u.v[0])*((1 << nd*lg) - 1));        
76 >                if (st == NULL) {
77                          sprintf(SDerrorDetail,
78                                  "Cannot allocate %d BSDF leaves", 1 << nd*lg);
79 <                return NULL;
79 >                        return NULL;
80 >                }
81          }
82          st->ndim = nd;
83          st->log2GR = lg;
# Line 85 | Line 88 | SDnewNode(int nd, int lg)
88   static void
89   SDfreeTre(SDNode *st)
90   {
91 <        int     i;
91 >        int     n;
92  
93          if (st == NULL)
94                  return;
95 <        for (i = (st->log2GR < 0) << st->ndim; i--; )
96 <                SDfreeTre(st->u.t[i]);
97 <        free((void *)st);
95 >        for (n = (st->log2GR < 0) << st->ndim; n--; )
96 >                SDfreeTre(st->u.t[n]);
97 >        free(st);
98   }
99  
100   /* Free a variable-resolution BSDF */
# Line 125 | Line 128 | fill_grid_branch(float *dptr, const float *sptr, int n
128   static float *
129   grid_branch_start(SDNode *st, int n)
130   {
131 <        unsigned        skipsiz = 1 << st->log2GR;
131 >        unsigned        skipsiz = 1 << (st->log2GR - 1);
132          float           *vptr = st->u.v;
133          int             i;
134  
135          for (i = st->ndim; i--; skipsiz <<= st->log2GR)
136                  if (1<<i & n)
137 <                        vptr += skipsiz >> 1;
137 >                        vptr += skipsiz;
138          return vptr;
139   }
140  
# Line 151 | Line 154 | SDsimplifyTre(SDNode *st)
154                          return NULL;    /* propogate error up call stack */
155                  match &= (st->u.t[n]->log2GR == st->u.t[0]->log2GR);
156          }
157 <        if (match && st->u.t[0]->log2GR >= 0) {
158 <                SDNode  *stn = SDnewNode(st->ndim, st->u.t[0]->log2GR + 1);
157 >        if (match && (match = st->u.t[0]->log2GR) >= 0) {
158 >                SDNode  *stn = SDnewNode(st->ndim, match + 1);
159                  if (stn == NULL)        /* out of memory? */
160                          return st;
161                                          /* transfer values to new grid */
162                  for (n = 1 << st->ndim; n--; )
163                          fill_grid_branch(grid_branch_start(stn, n),
164 <                                        st->u.t[n]->u.v, st->ndim, st->log2GR);
164 >                                        st->u.t[n]->u.v, stn->ndim, stn->log2GR);
165                  SDfreeTre(st);          /* free old tree */
166                  st = stn;               /* return new one */
167          }
# Line 187 | Line 190 | SDsmallestLeaf(const SDNode *st)
190   static double
191   SDiterSum(const float *va, int nd, int shft, const int *imin, const int *imax)
192   {
193 <        const unsigned  skipsiz = 1 << nd*shft;
193 >        const unsigned  skipsiz = 1 << --nd*shft;
194          double          sum = .0;
195          int             i;
196 <        
196 >
197 >        va += *imin * skipsiz;
198 >
199          if (skipsiz == 1)
200                  for (i = *imin; i < *imax; i++)
201 <                        sum += va[i];
201 >                        sum += *va++;
202          else
203 <                for (i = *imin; i < *imax; i++)
204 <                        sum += SDiterSum(va + i*skipsiz,
200 <                                        nd-1, shft, imin+1, imax+1);
203 >                for (i = *imin; i < *imax; i++, va += skipsiz)
204 >                        sum += SDiterSum(va, nd, shft, imin+1, imax+1);
205          return sum;
206   }
207  
# Line 205 | Line 209 | SDiterSum(const float *va, int nd, int shft, const int
209   static double
210   SDavgTreBox(const SDNode *st, const double *bmin, const double *bmax)
211   {
208        int             imin[SD_MAXDIM], imax[SD_MAXDIM];
212          unsigned        n;
213          int             i;
214  
# Line 215 | Line 218 | SDavgTreBox(const SDNode *st, const double *bmin, cons
218          for (i = st->ndim; i--; ) {
219                  if (bmin[i] >= 1.)
220                          return .0;
221 <                if (bmax[i] <= .0)
221 >                if (bmax[i] <= 0)
222                          return .0;
223                  if (bmin[i] >= bmax[i])
224                          return .0;
# Line 223 | Line 226 | SDavgTreBox(const SDNode *st, const double *bmin, cons
226          if (st->log2GR < 0) {           /* iterate on subtree */
227                  double          sum = .0, wsum = 1e-20;
228                  double          sbmin[SD_MAXDIM], sbmax[SD_MAXDIM], w;
226
229                  for (n = 1 << st->ndim; n--; ) {
230                          w = 1.;
231                          for (i = st->ndim; i--; ) {
# Line 235 | Line 237 | SDavgTreBox(const SDNode *st, const double *bmin, cons
237                                  }
238                                  if (sbmin[i] < .0) sbmin[i] = .0;
239                                  if (sbmax[i] > 1.) sbmax[i] = 1.;
240 +                                if (sbmin[i] >= sbmax[i]) {
241 +                                        w = .0;
242 +                                        break;
243 +                                }
244                                  w *= sbmax[i] - sbmin[i];
245                          }
246                          if (w > 1e-10) {
# Line 243 | Line 249 | SDavgTreBox(const SDNode *st, const double *bmin, cons
249                          }
250                  }
251                  return sum / wsum;
252 +        } else {                        /* iterate over leaves */
253 +                int             imin[SD_MAXDIM], imax[SD_MAXDIM];
254 +
255 +                n = 1;
256 +                for (i = st->ndim; i--; ) {
257 +                        imin[i] = (bmin[i] <= 0) ? 0 :
258 +                                        (int)((1 << st->log2GR)*bmin[i]);
259 +                        imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR) :
260 +                                (int)((1 << st->log2GR)*bmax[i] + .999999);
261 +                        n *= imax[i] - imin[i];
262 +                }
263 +                if (n)
264 +                        return SDiterSum(st->u.v, st->ndim,
265 +                                        st->log2GR, imin, imax) / (double)n;
266          }
267 <        n = 1;                          /* iterate over leaves */
248 <        for (i = st->ndim; i--; ) {
249 <                imin[i] = (bmin[i] <= 0) ? 0
250 <                                : (int)((1 << st->log2GR)*bmin[i]);
251 <                imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR)
252 <                                : (int)((1 << st->log2GR)*bmax[i] + .999999);
253 <                n *= imax[i] - imin[i];
254 <        }
255 <        if (!n)
256 <                return .0;
257 <        
258 <        return SDiterSum(st->u.v, st->ndim, st->log2GR, imin, imax) / (double)n;
267 >        return .0;
268   }
269  
270   /* Recursive call for SDtraverseTre() */
# Line 270 | Line 279 | SDdotravTre(const SDNode *st, const double *pos, int c
279                                          /* in branches? */
280          if (st->log2GR < 0) {
281                  unsigned        skipmask = 0;
273
282                  csiz *= .5;
283                  for (i = st->ndim; i--; )
284                          if (1<<i & cmask)
285                                  if (pos[i] < cmin[i] + csiz)
286 <                                        for (n = 1 << st->ndim; n--; )
286 >                                        for (n = 1 << st->ndim; n--; ) {
287                                                  if (n & 1<<i)
288                                                          skipmask |= 1<<n;
289 +                                        }
290                                  else
291 <                                        for (n = 1 << st->ndim; n--; )
291 >                                        for (n = 1 << st->ndim; n--; ) {
292                                                  if (!(n & 1<<i))
293                                                          skipmask |= 1<<n;
294 +                                        }
295                  for (n = 1 << st->ndim; n--; ) {
296                          if (1<<n & skipmask)
297                                  continue;
# Line 315 | Line 325 | SDdotravTre(const SDNode *st, const double *pos, int c
325                                  clim[i][0] = 0;
326                                  clim[i][1] = 1 << st->log2GR;
327                          }
318                                        /* fill in unused dimensions */
319                for (i = SD_MAXDIM; i-- > st->ndim; ) {
320                        clim[i][0] = 0; clim[i][1] = 1;
321                }
328   #if (SD_MAXDIM == 4)
329                  bmin[0] = cmin[0] + csiz*clim[0][0];
330                  for (cpos[0] = clim[0][0]; cpos[0] < clim[0][1]; cpos[0]++) {
331                      bmin[1] = cmin[1] + csiz*clim[1][0];
332                      for (cpos[1] = clim[1][0]; cpos[1] < clim[1][1]; cpos[1]++) {
333                          bmin[2] = cmin[2] + csiz*clim[2][0];
334 <                        for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) {
335 <                            bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]);
334 >                        if (st->ndim == 3) {
335 >                            cpos[2] = clim[2][0];
336                              n = cpos[0];
337 <                            for (i = 1; i < st->ndim; i++)
337 >                            for (i = 1; i < 3; i++)
338                                  n = (n << st->log2GR) + cpos[i];
339 <                            for ( ; cpos[3] < clim[3][1]; cpos[3]++) {
339 >                            for ( ; cpos[2] < clim[2][1]; cpos[2]++) {
340                                  rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr);
341                                  if (rv < 0)
342                                      return rv;
343 <                                bmin[3] += csiz;
343 >                                bmin[2] += csiz;
344                              }
345 <                            bmin[2] += csiz;
345 >                        } else {
346 >                            for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) {
347 >                                bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]);
348 >                                n = cpos[0];
349 >                                for (i = 1; i < 4; i++)
350 >                                    n = (n << st->log2GR) + cpos[i];
351 >                                for ( ; cpos[3] < clim[3][1]; cpos[3]++) {
352 >                                    rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr);
353 >                                    if (rv < 0)
354 >                                        return rv;
355 >                                    bmin[3] += csiz;
356 >                                }
357 >                                bmin[2] += csiz;
358 >                            }
359                          }
360                          bmin[1] += csiz;
361                      }
# Line 411 | Line 430 | SDlookupTre(const SDNode *st, const double *pos, doubl
430   static float
431   SDqueryTre(const SDTre *sdt, const FVECT outVec, const FVECT inVec, double *hc)
432   {
414        static const FVECT      zvec = {.0, .0, 1.};
433          FVECT                   rOutVec;
434          double                  gridPos[4];
435  
# Line 433 | Line 451 | SDqueryTre(const SDTre *sdt, const FVECT outVec, const
451          }
452                                          /* convert vector coordinates */
453          if (sdt->st->ndim == 3) {
454 <                spinvector(rOutVec, outVec, zvec, -atan2(inVec[1],inVec[0]));
454 >                spinvector(rOutVec, outVec, zvec, -atan2(-inVec[1],-inVec[0]));
455                  gridPos[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
456                  SDdisk2square(gridPos+1, rOutVec[0], rOutVec[1]);
457          } else if (sdt->st->ndim == 4) {
# Line 474 | Line 492 | build_scaffold(float val, const double *cmin, double c
492                  sp->wmax = wid;
493          if (sp->alen >= sp->nall) {     /* need more space? */
494                  struct outdir_s *ndarr;
495 <                sp->nall += 8192;
495 >                sp->nall += 1024;
496                  ndarr = (struct outdir_s *)realloc(sp->darr,
497                                          sizeof(struct outdir_s)*sp->nall);
498 <                if (ndarr == NULL)
498 >                if (ndarr == NULL) {
499 >                        sprintf(SDerrorDetail,
500 >                                "Cannot grow scaffold to %u entries", sp->nall);
501                          return -1;      /* abort build */
502 +                }
503                  sp->darr = ndarr;
504          }
505                                          /* find Hilbert entry index */
506          bmin[0] = cmin[0]*(double)iwmax + .5;
507          bmin[1] = cmin[1]*(double)iwmax + .5;
508 <        bmax[0] = bmin[0] + wid;
509 <        bmax[1] = bmin[1] + wid;
508 >        bmax[0] = bmin[0] + wid-1;
509 >        bmax[1] = bmin[1] + wid-1;
510          hilbert_box_vtx(2, sizeof(bitmask_t), iwbits, 1, bmin, bmax);
511          sp->darr[sp->alen].hent = hilbert_c2i(2, iwbits, bmin);
512          sp->darr[sp->alen].wid = wid;
# Line 498 | Line 519 | build_scaffold(float val, const double *cmin, double c
519   static int
520   sscmp(const void *p1, const void *p2)
521   {
522 <        return (int)((*(const struct outdir_s *)p1).hent -
523 <                        (*(const struct outdir_s *)p2).hent);
522 >        unsigned        h1 = (*(const struct outdir_s *)p1).hent;
523 >        unsigned        h2 = (*(const struct outdir_s *)p2).hent;
524 >
525 >        if (h1 > h2)
526 >                return 1;
527 >        if (h1 < h2)
528 >                return -1;
529 >        return 0;
530   }
531  
532   /* Create a new cumulative distribution for the given input direction */
# Line 516 | Line 543 | make_cdist(const SDTre *sdt, const double *pos)
543          myScaffold.wmax = 0;
544          myScaffold.nic = sdt->st->ndim - 2;
545          myScaffold.alen = 0;
546 <        myScaffold.nall = 8192;
546 >        myScaffold.nall = 512;
547          myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) *
548                                                          myScaffold.nall);
549          if (myScaffold.darr == NULL)
# Line 531 | Line 558 | make_cdist(const SDTre *sdt, const double *pos)
558          cd = (SDTreCDst *)malloc(sizeof(SDTreCDst) +
559                                  sizeof(cd->carr[0])*myScaffold.alen);
560          if (cd == NULL) {
561 +                sprintf(SDerrorDetail,
562 +                        "Cannot allocate %u entry cumulative distribution",
563 +                                myScaffold.alen);
564                  free(myScaffold.darr);
565                  return NULL;
566          }
567 +        cd->isodist = (myScaffold.nic == 1);
568                                          /* sort the distribution */
569          qsort(myScaffold.darr, cd->calen = myScaffold.alen,
570                                  sizeof(struct outdir_s), &sscmp);
# Line 544 | Line 575 | make_cdist(const SDTre *sdt, const double *pos)
575                  cd->clim[i][0] = floor(pos[i]/scale) * scale;
576                  cd->clim[i][1] = cd->clim[i][0] + scale;
577          }
578 +        if (cd->isodist) {              /* avoid issue in SDqueryTreProjSA() */
579 +                cd->clim[1][0] = cd->clim[0][0];
580 +                cd->clim[1][1] = cd->clim[0][1];
581 +        }
582          cd->max_psa = myScaffold.wmax / (double)iwmax;
583          cd->max_psa *= cd->max_psa * M_PI;
584          cd->sidef = sdt->sidef;
# Line 663 | Line 698 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
698          const SDTreCDst *cd = (const SDTreCDst *)cdp;
699          const unsigned  target = randX*cumlmax;
700          bitmask_t       hndx, hcoord[2];
701 <        double          gpos[3];
701 >        double          gpos[3], rotangle;
702          int             i, iupper, ilower;
703                                          /* check arguments */
704          if ((ioVec == NULL) | (cd == NULL))
# Line 699 | Line 734 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
734                                          /* emit from back? */
735          if (ioVec[2] > 0 ^ cd->sidef != SD_XMIT)
736                  gpos[2] = -gpos[2];
737 <        VCOPY(ioVec, gpos);
737 >        if (cd->isodist) {              /* rotate isotropic result */
738 >                rotangle = atan2(-ioVec[1],-ioVec[0]);
739 >                VCOPY(ioVec, gpos);
740 >                spinvector(ioVec, ioVec, zvec, rotangle);
741 >        } else
742 >                VCOPY(ioVec, gpos);
743          return SDEnone;
744   }
745  
# Line 712 | Line 752 | next_token(char **spp)
752          return **spp;
753   }
754  
755 + /* Advance pointer past matching token (or any token if c==0) */
756 + #define eat_token(spp,c)        (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0)
757 +
758   /* Count words from this point in string to '}' */
759   static int
760   count_values(char *cp)
761   {
762          int     n = 0;
763  
764 <        while (next_token(&cp) != '}') {
765 <                if (*cp == '{')
766 <                        return -1;
767 <                while (*cp && !isspace(*cp))
725 <                        ++cp;
764 >        while (next_token(&cp) != '}' && *cp) {
765 >                while (!isspace(*cp) & (*cp != ',') & (*cp != '}'))
766 >                        if (!*++cp)
767 >                                break;
768                  ++n;
769 <                cp += (next_token(&cp) == ',');
769 >                eat_token(&cp, ',');
770          }
771          return n;
772   }
# Line 739 | Line 781 | load_values(char **spp, float *va, int n)
781          while (n-- > 0 && (svnext = fskip(*spp)) != NULL) {
782                  *v++ = atof(*spp);
783                  *spp = svnext;
784 <                *spp += (next_token(spp) == ',');
784 >                eat_token(spp, ',');
785          }
786          return v - va;
787   }
# Line 751 | Line 793 | load_tree_data(char **spp, int nd)
793          SDNode  *st;
794          int     n;
795  
796 <        if (next_token(spp) != '{') {
796 >        if (!eat_token(spp, '{')) {
797                  strcpy(SDerrorDetail, "Missing '{' in tensor tree");
798                  return NULL;
799          }
758        ++*spp;                         /* in tree, now */
800          if (next_token(spp) == '{') {   /* tree branches */
801                  st = SDnewNode(nd, -1);
802                  if (st == NULL)
# Line 768 | Line 809 | load_tree_data(char **spp, int nd)
809          } else {                        /* else load value grid */
810                  int     bsiz;
811                  n = count_values(*spp); /* see how big the grid is */
812 <                if (n <= 0) {
772 <                        strcpy(SDerrorDetail, "Bad tensor tree data");
773 <                        return NULL;
774 <                }
775 <                for (bsiz = 0; bsiz < 8*sizeof(size_t)-1; bsiz += nd)
812 >                for (bsiz = 0; bsiz < 8*sizeof(size_t); bsiz += nd)
813                          if (1<<bsiz == n)
814                                  break;
815                  if (bsiz >= 8*sizeof(size_t)) {
# Line 788 | Line 825 | load_tree_data(char **spp, int nd)
825                          return NULL;
826                  }
827          }
828 <        if (next_token(spp) != '}') {
828 >        if (!eat_token(spp, '}')) {
829                  strcpy(SDerrorDetail, "Missing '}' in tensor tree");
830                  SDfreeTre(st);
831                  return NULL;
832          }
833 <        ++*spp;                         /* walk past close and return */
797 <        *spp += (next_token(spp) == ',');
833 >        eat_token(spp, ',');
834          return st;
835   }
836  
# Line 922 | Line 958 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
958   static float
959   SDgetTreMin(const SDNode *st)
960   {
961 <        float   vmin = 1./M_PI;
961 >        float   vmin = FHUGE;
962          int     n;
963  
964          if (st->log2GR < 0) {
# Line 950 | Line 986 | SDsubtractTreVal(SDNode *st, float val)
986                          SDsubtractTreVal(st->u.t[n], val);
987          } else {
988                  for (n = 1<<(st->ndim*st->log2GR); n--; )
989 <                        st->u.v[n] -= val;
989 >                        if ((st->u.v[n] -= val) < 0)
990 >                                st->u.v[n] = .0f;
991          }
992   }
993  
# Line 960 | Line 997 | subtract_min(SDNode *st)
997   {
998          float   vmin;
999                                          /* be sure to skip unused portion */
1000 <        if ((st->ndim == 3) & (st->log2GR < 0)) {
1001 <                float   v;
965 <                int     i;
1000 >        if (st->ndim == 3) {
1001 >                int     n;
1002                  vmin = 1./M_PI;
1003 <                for (i = 0; i < 4; i++) {
1004 <                        v = SDgetTreMin(st->u.t[i]);
1005 <                        if (v < vmin)
1006 <                                vmin = v;
1007 <                }
1003 >                if (st->log2GR < 0) {
1004 >                        for (n = 0; n < 8; n += 2) {
1005 >                                float   v = SDgetTreMin(st->u.t[n]);
1006 >                                if (v < vmin)
1007 >                                        vmin = v;
1008 >                        }
1009 >                } else if (st->log2GR) {
1010 >                        for (n = 1 << (3*st->log2GR - 1); n--; )
1011 >                                if (st->u.v[n] < vmin)
1012 >                                        vmin = st->u.v[n];
1013 >                } else
1014 >                        vmin = st->u.v[0];
1015          } else                          /* anisotropic covers entire tree */
1016                  vmin = SDgetTreMin(st);
1017  
# Line 992 | Line 1035 | extract_diffuse(SDValue *dv, SDSpectralDF *df)
1035                  return;
1036          }
1037          dv->spec = df->comp[0].cspec[0];
1038 <        dv->cieY = subtract_min((*(SDTre *)df->comp[n].dist).st);
1038 >        dv->cieY = subtract_min((*(SDTre *)df->comp[0].dist).st);
1039                                          /* in case of multiple components */
1040          for (n = df->ncomp; --n; ) {
1041                  double  ymin = subtract_min((*(SDTre *)df->comp[n].dist).st);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines