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.12 by greg, Sun May 1 16:34:37 2011 UTC

# Line 61 | Line 61 | SDnewNode(int nd, int lg)
61          if (lg < 0) {
62                  st = (SDNode *)malloc(sizeof(SDNode) +
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 <                                sizeof(st->u.v[0])*((1 << nd*lg) - 1));
69 <                
70 <        if (st == NULL) {
71 <                if (lg < 0)
64 >                if (st == NULL) {
65                          sprintf(SDerrorDetail,
66                                  "Cannot allocate %d branch BSDF tree", 1<<nd);
67 <                else
67 >                        return NULL;
68 >                }
69 >                memset(st->u.t, 0, sizeof(st->u.t[0])<<nd);
70 >        } else {
71 >                st = (SDNode *)malloc(sizeof(SDNode) +
72 >                                sizeof(st->u.v[0])*((1 << nd*lg) - 1));        
73 >                if (st == NULL) {
74                          sprintf(SDerrorDetail,
75                                  "Cannot allocate %d BSDF leaves", 1 << nd*lg);
76 <                return NULL;
76 >                        return NULL;
77 >                }
78          }
79          st->ndim = nd;
80          st->log2GR = lg;
# Line 85 | Line 85 | SDnewNode(int nd, int lg)
85   static void
86   SDfreeTre(SDNode *st)
87   {
88 <        int     i;
88 >        int     n;
89  
90          if (st == NULL)
91                  return;
92 <        for (i = (st->log2GR < 0) << st->ndim; i--; )
93 <                SDfreeTre(st->u.t[i]);
92 >        for (n = (st->log2GR < 0) << st->ndim; n--; )
93 >                SDfreeTre(st->u.t[n]);
94          free((void *)st);
95   }
96  
# Line 129 | Line 129 | grid_branch_start(SDNode *st, int n)
129          float           *vptr = st->u.v;
130          int             i;
131  
132 <        for (i = st->ndim; i--; skipsiz <<= st->log2GR)
133 <                if (1<<i & n)
132 >        for (i = 0; i < st->ndim; skipsiz <<= st->log2GR)
133 >                if (1<<i++ & n)
134                          vptr += skipsiz >> 1;
135          return vptr;
136   }
# Line 187 | Line 187 | SDsmallestLeaf(const SDNode *st)
187   static double
188   SDiterSum(const float *va, int nd, int shft, const int *imin, const int *imax)
189   {
190 <        const unsigned  skipsiz = 1 << nd*shft;
190 >        const unsigned  skipsiz = 1 << --nd*shft;
191          double          sum = .0;
192          int             i;
193          
# Line 196 | Line 196 | SDiterSum(const float *va, int nd, int shft, const int
196                          sum += va[i];
197          else
198                  for (i = *imin; i < *imax; i++)
199 <                        sum += SDiterSum(va + i*skipsiz,
200 <                                        nd-1, shft, imin+1, imax+1);
199 >                        sum += SDiterSum(va + i*skipsiz, nd, shft, imin+1, imax+1);
200          return sum;
201   }
202  
# Line 474 | Line 473 | build_scaffold(float val, const double *cmin, double c
473                  sp->wmax = wid;
474          if (sp->alen >= sp->nall) {     /* need more space? */
475                  struct outdir_s *ndarr;
476 <                sp->nall += 8192;
476 >                sp->nall += 1024;
477                  ndarr = (struct outdir_s *)realloc(sp->darr,
478                                          sizeof(struct outdir_s)*sp->nall);
479 <                if (ndarr == NULL)
479 >                if (ndarr == NULL) {
480 >                        sprintf(SDerrorDetail,
481 >                                "Cannot grow scaffold to %u entries", sp->nall);
482                          return -1;      /* abort build */
483 +                }
484                  sp->darr = ndarr;
485          }
486                                          /* find Hilbert entry index */
487          bmin[0] = cmin[0]*(double)iwmax + .5;
488          bmin[1] = cmin[1]*(double)iwmax + .5;
489 <        bmax[0] = bmin[0] + wid;
490 <        bmax[1] = bmin[1] + wid;
489 >        bmax[0] = bmin[0] + wid-1;
490 >        bmax[1] = bmin[1] + wid-1;
491          hilbert_box_vtx(2, sizeof(bitmask_t), iwbits, 1, bmin, bmax);
492          sp->darr[sp->alen].hent = hilbert_c2i(2, iwbits, bmin);
493          sp->darr[sp->alen].wid = wid;
# Line 498 | Line 500 | build_scaffold(float val, const double *cmin, double c
500   static int
501   sscmp(const void *p1, const void *p2)
502   {
503 <        return (int)((*(const struct outdir_s *)p1).hent -
504 <                        (*(const struct outdir_s *)p2).hent);
503 >        unsigned        h1 = (*(const struct outdir_s *)p1).hent;
504 >        unsigned        h2 = (*(const struct outdir_s *)p2).hent;
505 >
506 >        if (h1 > h2)
507 >                return 1;
508 >        if (h1 < h2)
509 >                return -1;
510 >        return 0;
511   }
512  
513   /* Create a new cumulative distribution for the given input direction */
# Line 516 | Line 524 | make_cdist(const SDTre *sdt, const double *pos)
524          myScaffold.wmax = 0;
525          myScaffold.nic = sdt->st->ndim - 2;
526          myScaffold.alen = 0;
527 <        myScaffold.nall = 8192;
527 >        myScaffold.nall = 512;
528          myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) *
529                                                          myScaffold.nall);
530          if (myScaffold.darr == NULL)
# Line 531 | Line 539 | make_cdist(const SDTre *sdt, const double *pos)
539          cd = (SDTreCDst *)malloc(sizeof(SDTreCDst) +
540                                  sizeof(cd->carr[0])*myScaffold.alen);
541          if (cd == NULL) {
542 +                sprintf(SDerrorDetail,
543 +                        "Cannot allocate %u entry cumulative distribution",
544 +                                myScaffold.alen);
545                  free(myScaffold.darr);
546                  return NULL;
547          }
# Line 712 | Line 723 | next_token(char **spp)
723          return **spp;
724   }
725  
726 < #define eat_token(spp,c)        (next_token(spp)==(c) ? *(*(spp))++ : 0)
726 > /* Advance pointer past matching token (or any token if c==0) */
727 > #define eat_token(spp,c)        (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0)
728  
729   /* Count words from this point in string to '}' */
730   static int
# Line 721 | Line 733 | count_values(char *cp)
733          int     n = 0;
734  
735          while (next_token(&cp) != '}' && *cp) {
736 <                if (*cp == '{')
737 <                        return -1;
738 <                while (*cp && (*cp != ',') & (*cp != '}') & !isspace(*cp))
727 <                        ++cp;
736 >                while (!isspace(*cp) & (*cp != ',') & (*cp != '}'))
737 >                        if (!*++cp)
738 >                                break;
739                  ++n;
740                  eat_token(&cp, ',');
741          }
# Line 769 | Line 780 | load_tree_data(char **spp, int nd)
780          } else {                        /* else load value grid */
781                  int     bsiz;
782                  n = count_values(*spp); /* see how big the grid is */
772                if (n <= 0) {
773                        strcpy(SDerrorDetail, "Bad tensor tree data");
774                        return NULL;
775                }
783                  for (bsiz = 0; bsiz < 8*sizeof(size_t)-1; bsiz += nd)
784                          if (1<<bsiz == n)
785                                  break;
# Line 922 | Line 929 | load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim)
929   static float
930   SDgetTreMin(const SDNode *st)
931   {
932 <        float   vmin = 1./M_PI;
932 >        float   vmin = FHUGE;
933          int     n;
934  
935          if (st->log2GR < 0) {
# Line 960 | Line 967 | subtract_min(SDNode *st)
967   {
968          float   vmin;
969                                          /* be sure to skip unused portion */
970 <        if ((st->ndim == 3) & (st->log2GR < 0)) {
971 <                float   v;
965 <                int     i;
970 >        if (st->ndim == 3) {
971 >                int     n;
972                  vmin = 1./M_PI;
973 <                for (i = 0; i < 4; i++) {
974 <                        v = SDgetTreMin(st->u.t[i]);
975 <                        if (v < vmin)
976 <                                vmin = v;
977 <                }
973 >                if (st->log2GR < 0) {
974 >                        for (n = 0; n < 4; n++) {
975 >                                float   v = SDgetTreMin(st->u.t[n]);
976 >                                if (v < vmin)
977 >                                        vmin = v;
978 >                        }
979 >                } else if (st->log2GR) {
980 >                        for (n = 1 << (3*st->log2GR - 1); n--; )
981 >                                if (st->u.v[n] < vmin)
982 >                                        vmin = st->u.v[n];
983 >                } else
984 >                        vmin = st->u.v[0];
985          } else                          /* anisotropic covers entire tree */
986                  vmin = SDgetTreMin(st);
987  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines