--- ray/src/common/bsdf_t.c 2011/04/28 04:05:11 3.10 +++ ray/src/common/bsdf_t.c 2011/06/01 05:21:18 3.14 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdf_t.c,v 3.10 2011/04/28 04:05:11 greg Exp $"; +static const char RCSid[] = "$Id: bsdf_t.c,v 3.14 2011/06/01 05:21:18 greg Exp $"; #endif /* * bsdf_t.c @@ -61,20 +61,20 @@ SDnewNode(int nd, int lg) if (lg < 0) { st = (SDNode *)malloc(sizeof(SDNode) + sizeof(st->u.t[0])*((1<u.t, 0, sizeof(st->u.t[0])<u.v[0])*((1 << nd*lg) - 1)); - - if (st == NULL) { - if (lg < 0) + if (st == NULL) { sprintf(SDerrorDetail, "Cannot allocate %d branch BSDF tree", 1<u.t, 0, sizeof(st->u.t[0])<u.v[0])*((1 << nd*lg) - 1)); + if (st == NULL) { sprintf(SDerrorDetail, "Cannot allocate %d BSDF leaves", 1 << nd*lg); - return NULL; + return NULL; + } } st->ndim = nd; st->log2GR = lg; @@ -85,13 +85,13 @@ SDnewNode(int nd, int lg) static void SDfreeTre(SDNode *st) { - int i; + int n; if (st == NULL) return; - for (i = (st->log2GR < 0) << st->ndim; i--; ) - SDfreeTre(st->u.t[i]); - free((void *)st); + for (n = (st->log2GR < 0) << st->ndim; n--; ) + SDfreeTre(st->u.t[n]); + free(st); } /* Free a variable-resolution BSDF */ @@ -125,13 +125,13 @@ fill_grid_branch(float *dptr, const float *sptr, int n static float * grid_branch_start(SDNode *st, int n) { - unsigned skipsiz = 1 << st->log2GR; + unsigned skipsiz = 1 << (st->log2GR - 1); float *vptr = st->u.v; int i; - for (i = 0; i < st->ndim; skipsiz <<= st->log2GR) - if (1<> 1; + for (i = st->ndim; i--; skipsiz <<= st->log2GR) + if (1<ndim; i--; ) { if (bmin[i] >= 1.) return .0; - if (bmax[i] <= .0) + if (bmax[i] <= 0) return .0; if (bmin[i] >= bmax[i]) return .0; @@ -234,6 +234,10 @@ SDavgTreBox(const SDNode *st, const double *bmin, cons } if (sbmin[i] < .0) sbmin[i] = .0; if (sbmax[i] > 1.) sbmax[i] = 1.; + if (sbmin[i] >= sbmax[i]) { + w = .0; + break; + } w *= sbmax[i] - sbmin[i]; } if (w > 1e-10) { @@ -269,18 +273,19 @@ SDdotravTre(const SDNode *st, const double *pos, int c /* in branches? */ if (st->log2GR < 0) { unsigned skipmask = 0; - csiz *= .5; for (i = st->ndim; i--; ) if (1<ndim; n--; ) + for (n = 1 << st->ndim; n--; ) { if (n & 1<ndim; n--; ) + for (n = 1 << st->ndim; n--; ) { if (!(n & 1<ndim; n--; ) { if (1<wmax = wid; if (sp->alen >= sp->nall) { /* need more space? */ struct outdir_s *ndarr; - sp->nall += 8192; + sp->nall += 1024; ndarr = (struct outdir_s *)realloc(sp->darr, sizeof(struct outdir_s)*sp->nall); - if (ndarr == NULL) + if (ndarr == NULL) { + sprintf(SDerrorDetail, + "Cannot grow scaffold to %u entries", sp->nall); return -1; /* abort build */ + } sp->darr = ndarr; } /* find Hilbert entry index */ @@ -505,10 +513,6 @@ sscmp(const void *p1, const void *p2) if (h1 < h2) return -1; return 0; -/* - return (int)((*(const struct outdir_s *)p1).hent - - (*(const struct outdir_s *)p2).hent); -*/ } /* Create a new cumulative distribution for the given input direction */ @@ -525,7 +529,7 @@ make_cdist(const SDTre *sdt, const double *pos) myScaffold.wmax = 0; myScaffold.nic = sdt->st->ndim - 2; myScaffold.alen = 0; - myScaffold.nall = 8192; + myScaffold.nall = 512; myScaffold.darr = (struct outdir_s *)malloc(sizeof(struct outdir_s) * myScaffold.nall); if (myScaffold.darr == NULL) @@ -540,6 +544,9 @@ make_cdist(const SDTre *sdt, const double *pos) cd = (SDTreCDst *)malloc(sizeof(SDTreCDst) + sizeof(cd->carr[0])*myScaffold.alen); if (cd == NULL) { + sprintf(SDerrorDetail, + "Cannot allocate %u entry cumulative distribution", + myScaffold.alen); free(myScaffold.darr); return NULL; } @@ -721,7 +728,8 @@ next_token(char **spp) return **spp; } -#define eat_token(spp,c) (next_token(spp)==(c) ? *(*(spp))++ : 0) +/* Advance pointer past matching token (or any token if c==0) */ +#define eat_token(spp,c) (next_token(spp)==(c) ^ !(c) ? *(*(spp))++ : 0) /* Count words from this point in string to '}' */ static int @@ -730,10 +738,9 @@ count_values(char *cp) int n = 0; while (next_token(&cp) != '}' && *cp) { - if (*cp == '{') - return -1; - while (*cp && (*cp != ',') & (*cp != '}') & !isspace(*cp)) - ++cp; + while (!isspace(*cp) & (*cp != ',') & (*cp != '}')) + if (!*++cp) + break; ++n; eat_token(&cp, ','); } @@ -778,10 +785,6 @@ load_tree_data(char **spp, int nd) } else { /* else load value grid */ int bsiz; n = count_values(*spp); /* see how big the grid is */ - if (n <= 0) { - strcpy(SDerrorDetail, "Bad tensor tree data"); - return NULL; - } for (bsiz = 0; bsiz < 8*sizeof(size_t)-1; bsiz += nd) if (1<