--- ray/src/common/bsdf_t.c 2011/04/27 20:05:15 3.8 +++ ray/src/common/bsdf_t.c 2011/05/01 16:34:37 3.12 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdf_t.c,v 3.8 2011/04/27 20:05:15 greg Exp $"; +static const char RCSid[] = "$Id: bsdf_t.c,v 3.12 2011/05/01 16:34:37 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,12 +85,12 @@ 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]); + for (n = (st->log2GR < 0) << st->ndim; n--; ) + SDfreeTre(st->u.t[n]); free((void *)st); } @@ -129,8 +129,8 @@ grid_branch_start(SDNode *st, int n) float *vptr = st->u.v; int i; - for (i = st->ndim; i--; skipsiz <<= st->log2GR) - if (1<ndim; skipsiz <<= st->log2GR) + if (1<> 1; return vptr; } @@ -151,14 +151,14 @@ SDsimplifyTre(SDNode *st) return NULL; /* propogate error up call stack */ match &= (st->u.t[n]->log2GR == st->u.t[0]->log2GR); } - if (match && st->u.t[0]->log2GR >= 0) { - SDNode *stn = SDnewNode(st->ndim, st->u.t[0]->log2GR + 1); + if (match && (match = st->u.t[0]->log2GR) >= 0) { + SDNode *stn = SDnewNode(st->ndim, match + 1); if (stn == NULL) /* out of memory? */ return st; /* transfer values to new grid */ for (n = 1 << st->ndim; n--; ) fill_grid_branch(grid_branch_start(stn, n), - st->u.t[n]->u.v, st->ndim, st->log2GR); + st->u.t[n]->u.v, stn->ndim, stn->log2GR); SDfreeTre(st); /* free old tree */ st = stn; /* return new one */ } @@ -187,7 +187,7 @@ SDsmallestLeaf(const SDNode *st) static double SDiterSum(const float *va, int nd, int shft, const int *imin, const int *imax) { - const unsigned skipsiz = 1 << nd*shft; + const unsigned skipsiz = 1 << --nd*shft; double sum = .0; int i; @@ -196,8 +196,7 @@ SDiterSum(const float *va, int nd, int shft, const int sum += va[i]; else for (i = *imin; i < *imax; i++) - sum += SDiterSum(va + i*skipsiz, - nd-1, shft, imin+1, imax+1); + sum += SDiterSum(va + i*skipsiz, nd, shft, imin+1, imax+1); return sum; } @@ -474,18 +473,21 @@ build_scaffold(float val, const double *cmin, double c sp->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 */ bmin[0] = cmin[0]*(double)iwmax + .5; bmin[1] = cmin[1]*(double)iwmax + .5; - bmax[0] = bmin[0] + wid; - bmax[1] = bmin[1] + wid; + bmax[0] = bmin[0] + wid-1; + bmax[1] = bmin[1] + wid-1; hilbert_box_vtx(2, sizeof(bitmask_t), iwbits, 1, bmin, bmax); sp->darr[sp->alen].hent = hilbert_c2i(2, iwbits, bmin); sp->darr[sp->alen].wid = wid; @@ -498,8 +500,14 @@ build_scaffold(float val, const double *cmin, double c static int sscmp(const void *p1, const void *p2) { - return (int)((*(const struct outdir_s *)p1).hent - - (*(const struct outdir_s *)p2).hent); + unsigned h1 = (*(const struct outdir_s *)p1).hent; + unsigned h2 = (*(const struct outdir_s *)p2).hent; + + if (h1 > h2) + return 1; + if (h1 < h2) + return -1; + return 0; } /* Create a new cumulative distribution for the given input direction */ @@ -516,7 +524,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) @@ -531,6 +539,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; } @@ -712,19 +723,21 @@ next_token(char **spp) return **spp; } +/* 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 count_values(char *cp) { int n = 0; - while (next_token(&cp) != '}') { - if (*cp == '{') - return -1; - while (*cp && !isspace(*cp)) - ++cp; + while (next_token(&cp) != '}' && *cp) { + while (!isspace(*cp) & (*cp != ',') & (*cp != '}')) + if (!*++cp) + break; ++n; - cp += (next_token(&cp) == ','); + eat_token(&cp, ','); } return n; } @@ -739,7 +752,7 @@ load_values(char **spp, float *va, int n) while (n-- > 0 && (svnext = fskip(*spp)) != NULL) { *v++ = atof(*spp); *spp = svnext; - *spp += (next_token(spp) == ','); + eat_token(spp, ','); } return v - va; } @@ -751,11 +764,10 @@ load_tree_data(char **spp, int nd) SDNode *st; int n; - if (next_token(spp) != '{') { + if (!eat_token(spp, '{')) { strcpy(SDerrorDetail, "Missing '{' in tensor tree"); return NULL; } - ++*spp; /* in tree, now */ if (next_token(spp) == '{') { /* tree branches */ st = SDnewNode(nd, -1); if (st == NULL) @@ -768,10 +780,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<log2GR < 0) { @@ -960,15 +967,21 @@ subtract_min(SDNode *st) { float vmin; /* be sure to skip unused portion */ - if ((st->ndim == 3) & (st->log2GR < 0)) { - float v; - int i; + if (st->ndim == 3) { + int n; vmin = 1./M_PI; - for (i = 0; i < 4; i++) { - v = SDgetTreMin(st->u.t[i]); - if (v < vmin) - vmin = v; - } + if (st->log2GR < 0) { + for (n = 0; n < 4; n++) { + float v = SDgetTreMin(st->u.t[n]); + if (v < vmin) + vmin = v; + } + } else if (st->log2GR) { + for (n = 1 << (3*st->log2GR - 1); n--; ) + if (st->u.v[n] < vmin) + vmin = st->u.v[n]; + } else + vmin = st->u.v[0]; } else /* anisotropic covers entire tree */ vmin = SDgetTreMin(st); @@ -992,7 +1005,7 @@ extract_diffuse(SDValue *dv, SDSpectralDF *df) return; } dv->spec = df->comp[0].cspec[0]; - dv->cieY = subtract_min((*(SDTre *)df->comp[n].dist).st); + dv->cieY = subtract_min((*(SDTre *)df->comp[0].dist).st); /* in case of multiple components */ for (n = df->ncomp; --n; ) { double ymin = subtract_min((*(SDTre *)df->comp[n].dist).st);