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.9 by greg, Wed Apr 27 23:05:51 2011 UTC vs.
Revision 3.15 by greg, Fri Jun 3 18:12:58 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 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 290 | Line 299 | SDdotravTre(const SDNode *st, const double *pos, int c
299                                          bmin[i] = cmin[i] + csiz;
300                                  else
301                                          bmin[i] = cmin[i];
302 +                        for (i = SD_MAXDIM; i-- > st->ndim; )
303 +                                bmin[i] = .0;
304  
305                          rval += rv = SDdotravTre(st->u.t[n], pos, cmask,
306                                                          cf, cptr, bmin, csiz);
# Line 411 | Line 422 | SDlookupTre(const SDNode *st, const double *pos, doubl
422   static float
423   SDqueryTre(const SDTre *sdt, const FVECT outVec, const FVECT inVec, double *hc)
424   {
414        static const FVECT      zvec = {.0, .0, 1.};
425          FVECT                   rOutVec;
426          double                  gridPos[4];
427  
# Line 433 | Line 443 | SDqueryTre(const SDTre *sdt, const FVECT outVec, const
443          }
444                                          /* convert vector coordinates */
445          if (sdt->st->ndim == 3) {
446 <                spinvector(rOutVec, outVec, zvec, -atan2(inVec[1],inVec[0]));
446 >                spinvector(rOutVec, outVec, zvec, -atan2(-inVec[1],-inVec[0]));
447                  gridPos[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]);
448                  SDdisk2square(gridPos+1, rOutVec[0], rOutVec[1]);
449          } else if (sdt->st->ndim == 4) {
# Line 474 | Line 484 | build_scaffold(float val, const double *cmin, double c
484                  sp->wmax = wid;
485          if (sp->alen >= sp->nall) {     /* need more space? */
486                  struct outdir_s *ndarr;
487 <                sp->nall += 8192;
487 >                sp->nall += 1024;
488                  ndarr = (struct outdir_s *)realloc(sp->darr,
489                                          sizeof(struct outdir_s)*sp->nall);
490 <                if (ndarr == NULL)
490 >                if (ndarr == NULL) {
491 >                        sprintf(SDerrorDetail,
492 >                                "Cannot grow scaffold to %u entries", sp->nall);
493                          return -1;      /* abort build */
494 +                }
495                  sp->darr = ndarr;
496          }
497                                          /* find Hilbert entry index */
498          bmin[0] = cmin[0]*(double)iwmax + .5;
499          bmin[1] = cmin[1]*(double)iwmax + .5;
500 <        bmax[0] = bmin[0] + wid;
501 <        bmax[1] = bmin[1] + wid;
500 >        bmax[0] = bmin[0] + wid-1;
501 >        bmax[1] = bmin[1] + wid-1;
502          hilbert_box_vtx(2, sizeof(bitmask_t), iwbits, 1, bmin, bmax);
503          sp->darr[sp->alen].hent = hilbert_c2i(2, iwbits, bmin);
504          sp->darr[sp->alen].wid = wid;
# Line 498 | Line 511 | build_scaffold(float val, const double *cmin, double c
511   static int
512   sscmp(const void *p1, const void *p2)
513   {
514 <        return (int)((*(const struct outdir_s *)p1).hent -
515 <                        (*(const struct outdir_s *)p2).hent);
514 >        unsigned        h1 = (*(const struct outdir_s *)p1).hent;
515 >        unsigned        h2 = (*(const struct outdir_s *)p2).hent;
516 >
517 >        if (h1 > h2)
518 >                return 1;
519 >        if (h1 < h2)
520 >                return -1;
521 >        return 0;
522   }
523  
524   /* Create a new cumulative distribution for the given input direction */
# Line 516 | Line 535 | make_cdist(const SDTre *sdt, const double *pos)
535          myScaffold.wmax = 0;
536          myScaffold.nic = sdt->st->ndim - 2;
537          myScaffold.alen = 0;
538 <        myScaffold.nall = 8192;
538 >        myScaffold.nall = 512;
539          myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) *
540                                                          myScaffold.nall);
541          if (myScaffold.darr == NULL)
# Line 531 | Line 550 | make_cdist(const SDTre *sdt, const double *pos)
550          cd = (SDTreCDst *)malloc(sizeof(SDTreCDst) +
551                                  sizeof(cd->carr[0])*myScaffold.alen);
552          if (cd == NULL) {
553 +                sprintf(SDerrorDetail,
554 +                        "Cannot allocate %u entry cumulative distribution",
555 +                                myScaffold.alen);
556                  free(myScaffold.darr);
557                  return NULL;
558          }
559 +        cd->isodist = (myScaffold.nic == 1);
560                                          /* sort the distribution */
561          qsort(myScaffold.darr, cd->calen = myScaffold.alen,
562                                  sizeof(struct outdir_s), &sscmp);
# Line 544 | Line 567 | make_cdist(const SDTre *sdt, const double *pos)
567                  cd->clim[i][0] = floor(pos[i]/scale) * scale;
568                  cd->clim[i][1] = cd->clim[i][0] + scale;
569          }
570 +        if (cd->isodist) {              /* avoid issue in SDqueryTreProjSA() */
571 +                cd->clim[1][0] = cd->clim[0][0];
572 +                cd->clim[1][1] = cd->clim[0][1];
573 +        }
574          cd->max_psa = myScaffold.wmax / (double)iwmax;
575          cd->max_psa *= cd->max_psa * M_PI;
576          cd->sidef = sdt->sidef;
# Line 663 | Line 690 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
690          const SDTreCDst *cd = (const SDTreCDst *)cdp;
691          const unsigned  target = randX*cumlmax;
692          bitmask_t       hndx, hcoord[2];
693 <        double          gpos[3];
693 >        double          gpos[3], rotangle;
694          int             i, iupper, ilower;
695                                          /* check arguments */
696          if ((ioVec == NULL) | (cd == NULL))
# Line 699 | Line 726 | SDsampTreCDist(FVECT ioVec, double randX, const SDCDst
726                                          /* emit from back? */
727          if (ioVec[2] > 0 ^ cd->sidef != SD_XMIT)
728                  gpos[2] = -gpos[2];
729 <        VCOPY(ioVec, gpos);
729 >        if (cd->isodist) {              /* rotate isotropic result */
730 >                rotangle = atan2(-ioVec[1],-ioVec[0]);
731 >                VCOPY(ioVec, gpos);
732 >                spinvector(ioVec, ioVec, zvec, rotangle);
733 >        } else
734 >                VCOPY(ioVec, gpos);
735          return SDEnone;
736   }
737  
# Line 712 | Line 744 | next_token(char **spp)
744          return **spp;
745   }
746  
747 < #define eat_token(spp,c)        (next_token(spp)==(c) ? *(*(spp))++ : 0)
747 > /* Advance pointer past matching token (or any token if c==0) */
748 > #define eat_token(spp,c)        (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0)
749  
750   /* Count words from this point in string to '}' */
751   static int
# Line 721 | Line 754 | count_values(char *cp)
754          int     n = 0;
755  
756          while (next_token(&cp) != '}' && *cp) {
757 <                if (*cp == '{')
758 <                        return -1;
759 <                while (*cp && (*cp != ',') & (*cp != '}') & !isspace(*cp))
727 <                        ++cp;
757 >                while (!isspace(*cp) & (*cp != ',') & (*cp != '}'))
758 >                        if (!*++cp)
759 >                                break;
760                  ++n;
761                  eat_token(&cp, ',');
762          }
# Line 769 | Line 801 | load_tree_data(char **spp, int nd)
801          } else {                        /* else load value grid */
802                  int     bsiz;
803                  n = count_values(*spp); /* see how big the grid is */
804 <                if (n <= 0) {
773 <                        strcpy(SDerrorDetail, "Bad tensor tree data");
774 <                        return NULL;
775 <                }
776 <                for (bsiz = 0; bsiz < 8*sizeof(size_t)-1; bsiz += nd)
804 >                for (bsiz = 0; bsiz < 8*sizeof(size_t); bsiz += nd)
805                          if (1<<bsiz == n)
806                                  break;
807                  if (bsiz >= 8*sizeof(size_t)) {
# Line 922 | Line 950 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
950   static float
951   SDgetTreMin(const SDNode *st)
952   {
953 <        float   vmin = 1./M_PI;
953 >        float   vmin = FHUGE;
954          int     n;
955  
956          if (st->log2GR < 0) {
# Line 950 | Line 978 | SDsubtractTreVal(SDNode *st, float val)
978                          SDsubtractTreVal(st->u.t[n], val);
979          } else {
980                  for (n = 1<<(st->ndim*st->log2GR); n--; )
981 <                        st->u.v[n] -= val;
981 >                        if ((st->u.v[n] -= val) < 0)
982 >                                st->u.v[n] = .0f;
983          }
984   }
985  
# Line 960 | Line 989 | subtract_min(SDNode *st)
989   {
990          float   vmin;
991                                          /* be sure to skip unused portion */
992 <        if ((st->ndim == 3) & (st->log2GR < 0)) {
993 <                float   v;
965 <                int     i;
992 >        if (st->ndim == 3) {
993 >                int     n;
994                  vmin = 1./M_PI;
995 <                for (i = 0; i < 4; i++) {
996 <                        v = SDgetTreMin(st->u.t[i]);
997 <                        if (v < vmin)
998 <                                vmin = v;
999 <                }
995 >                if (st->log2GR < 0) {
996 >                        for (n = 0; n < 8; n += 2) {
997 >                                float   v = SDgetTreMin(st->u.t[n]);
998 >                                if (v < vmin)
999 >                                        vmin = v;
1000 >                        }
1001 >                } else if (st->log2GR) {
1002 >                        for (n = 1 << (3*st->log2GR - 1); n--; )
1003 >                                if (st->u.v[n] < vmin)
1004 >                                        vmin = st->u.v[n];
1005 >                } else
1006 >                        vmin = st->u.v[0];
1007          } else                          /* anisotropic covers entire tree */
1008                  vmin = SDgetTreMin(st);
1009  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines