--- ray/src/common/bsdf_t.c 2011/04/27 20:05:15 3.8 +++ ray/src/common/bsdf_t.c 2011/06/03 18:12:58 3.15 @@ -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.15 2011/06/03 18:12:58 greg Exp $"; #endif /* * bsdf_t.c @@ -28,6 +28,8 @@ static const unsigned iwbits = sizeof(unsigned)*4; static const unsigned iwmax = (1<<(sizeof(unsigned)*4))-1; /* maximum cumulative value */ static const unsigned cumlmax = ~0; + /* constant z-vector */ +static const FVECT zvec = {.0, .0, 1.}; /* Struct used for our distribution-building callback */ typedef struct { @@ -61,20 +63,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 +87,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 +127,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 = st->ndim; i--; skipsiz <<= st->log2GR) if (1<> 1; + vptr += skipsiz; return vptr; } @@ -151,14 +153,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,17 +189,18 @@ 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; - + + va += *imin * skipsiz; + if (skipsiz == 1) for (i = *imin; i < *imax; i++) - sum += va[i]; + sum += *va++; else - for (i = *imin; i < *imax; i++) - sum += SDiterSum(va + i*skipsiz, - nd-1, shft, imin+1, imax+1); + for (i = *imin; i < *imax; i++, va += skipsiz) + sum += SDiterSum(va, nd, shft, imin+1, imax+1); return sum; } @@ -205,7 +208,6 @@ SDiterSum(const float *va, int nd, int shft, const int static double SDavgTreBox(const SDNode *st, const double *bmin, const double *bmax) { - int imin[SD_MAXDIM], imax[SD_MAXDIM]; unsigned n; int i; @@ -215,7 +217,7 @@ SDavgTreBox(const SDNode *st, const double *bmin, cons for (i = st->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; @@ -223,7 +225,6 @@ SDavgTreBox(const SDNode *st, const double *bmin, cons if (st->log2GR < 0) { /* iterate on subtree */ double sum = .0, wsum = 1e-20; double sbmin[SD_MAXDIM], sbmax[SD_MAXDIM], w; - for (n = 1 << st->ndim; n--; ) { w = 1.; for (i = st->ndim; i--; ) { @@ -235,6 +236,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) { @@ -243,19 +248,22 @@ SDavgTreBox(const SDNode *st, const double *bmin, cons } } return sum / wsum; + } else { /* iterate over leaves */ + int imin[SD_MAXDIM], imax[SD_MAXDIM]; + + n = 1; + for (i = st->ndim; i--; ) { + imin[i] = (bmin[i] <= 0) ? 0 : + (int)((1 << st->log2GR)*bmin[i]); + imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR) : + (int)((1 << st->log2GR)*bmax[i] + .999999); + n *= imax[i] - imin[i]; + } + if (n) + return SDiterSum(st->u.v, st->ndim, + st->log2GR, imin, imax) / (double)n; } - n = 1; /* iterate over leaves */ - for (i = st->ndim; i--; ) { - imin[i] = (bmin[i] <= 0) ? 0 - : (int)((1 << st->log2GR)*bmin[i]); - imax[i] = (bmax[i] >= 1.) ? (1 << st->log2GR) - : (int)((1 << st->log2GR)*bmax[i] + .999999); - n *= imax[i] - imin[i]; - } - if (!n) - return .0; - - return SDiterSum(st->u.v, st->ndim, st->log2GR, imin, imax) / (double)n; + return .0; } /* Recursive call for SDtraverseTre() */ @@ -270,18 +278,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< st->ndim; ) + bmin[i] = .0; rval += rv = SDdotravTre(st->u.t[n], pos, cmask, cf, cptr, bmin, csiz); @@ -411,7 +422,6 @@ SDlookupTre(const SDNode *st, const double *pos, doubl static float SDqueryTre(const SDTre *sdt, const FVECT outVec, const FVECT inVec, double *hc) { - static const FVECT zvec = {.0, .0, 1.}; FVECT rOutVec; double gridPos[4]; @@ -433,7 +443,7 @@ SDqueryTre(const SDTre *sdt, const FVECT outVec, const } /* convert vector coordinates */ if (sdt->st->ndim == 3) { - spinvector(rOutVec, outVec, zvec, -atan2(inVec[1],inVec[0])); + spinvector(rOutVec, outVec, zvec, -atan2(-inVec[1],-inVec[0])); gridPos[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); SDdisk2square(gridPos+1, rOutVec[0], rOutVec[1]); } else if (sdt->st->ndim == 4) { @@ -474,18 +484,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 +511,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 +535,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,9 +550,13 @@ 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; } + cd->isodist = (myScaffold.nic == 1); /* sort the distribution */ qsort(myScaffold.darr, cd->calen = myScaffold.alen, sizeof(struct outdir_s), &sscmp); @@ -544,6 +567,10 @@ make_cdist(const SDTre *sdt, const double *pos) cd->clim[i][0] = floor(pos[i]/scale) * scale; cd->clim[i][1] = cd->clim[i][0] + scale; } + if (cd->isodist) { /* avoid issue in SDqueryTreProjSA() */ + cd->clim[1][0] = cd->clim[0][0]; + cd->clim[1][1] = cd->clim[0][1]; + } cd->max_psa = myScaffold.wmax / (double)iwmax; cd->max_psa *= cd->max_psa * M_PI; cd->sidef = sdt->sidef; @@ -663,7 +690,7 @@ SDsampTreCDist(FVECT ioVec, double randX, const SDCDst const SDTreCDst *cd = (const SDTreCDst *)cdp; const unsigned target = randX*cumlmax; bitmask_t hndx, hcoord[2]; - double gpos[3]; + double gpos[3], rotangle; int i, iupper, ilower; /* check arguments */ if ((ioVec == NULL) | (cd == NULL)) @@ -699,7 +726,12 @@ SDsampTreCDist(FVECT ioVec, double randX, const SDCDst /* emit from back? */ if (ioVec[2] > 0 ^ cd->sidef != SD_XMIT) gpos[2] = -gpos[2]; - VCOPY(ioVec, gpos); + if (cd->isodist) { /* rotate isotropic result */ + rotangle = atan2(-ioVec[1],-ioVec[0]); + VCOPY(ioVec, gpos); + spinvector(ioVec, ioVec, zvec, rotangle); + } else + VCOPY(ioVec, gpos); return SDEnone; } @@ -712,19 +744,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 +773,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 +785,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,11 +801,7 @@ 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) + for (bsiz = 0; bsiz < 8*sizeof(size_t); bsiz += nd) if (1<= 8*sizeof(size_t)) { @@ -788,13 +817,12 @@ load_tree_data(char **spp, int nd) return NULL; } } - if (next_token(spp) != '}') { + if (!eat_token(spp, '}')) { strcpy(SDerrorDetail, "Missing '}' in tensor tree"); SDfreeTre(st); return NULL; } - ++*spp; /* walk past close and return */ - *spp += (next_token(spp) == ','); + eat_token(spp, ','); return st; } @@ -922,7 +950,7 @@ load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim) static float SDgetTreMin(const SDNode *st) { - float vmin = 1./M_PI; + float vmin = FHUGE; int n; if (st->log2GR < 0) { @@ -950,7 +978,8 @@ SDsubtractTreVal(SDNode *st, float val) SDsubtractTreVal(st->u.t[n], val); } else { for (n = 1<<(st->ndim*st->log2GR); n--; ) - st->u.v[n] -= val; + if ((st->u.v[n] -= val) < 0) + st->u.v[n] = .0f; } } @@ -960,15 +989,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 < 8; n += 2) { + 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 +1027,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);