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.16 by greg, Sun Jun 5 20:27:14 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines