--- ray/src/common/bsdf_t.c 2011/04/27 23:05:51 3.9 +++ ray/src/common/bsdf_t.c 2011/06/28 21:13:46 3.18 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdf_t.c,v 3.9 2011/04/27 23:05:51 greg Exp $"; +static const char RCSid[] = "$Id: bsdf_t.c,v 3.18 2011/06/28 21:13:46 greg Exp $"; #endif /* * bsdf_t.c @@ -10,6 +10,7 @@ static const char RCSid[] = "$Id: bsdf_t.c,v 3.9 2011/ * */ +#define _USE_MATH_DEFINES #include "rtio.h" #include #include @@ -28,6 +29,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 +64,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 +88,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 +128,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; } @@ -187,17 +190,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 +209,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 +218,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 +226,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 +237,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 +249,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 +279,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<log2GR; } - /* fill in unused dimensions */ - for (i = SD_MAXDIM; i-- > st->ndim; ) { - clim[i][0] = 0; clim[i][1] = 1; - } #if (SD_MAXDIM == 4) bmin[0] = cmin[0] + csiz*clim[0][0]; for (cpos[0] = clim[0][0]; cpos[0] < clim[0][1]; cpos[0]++) { bmin[1] = cmin[1] + csiz*clim[1][0]; for (cpos[1] = clim[1][0]; cpos[1] < clim[1][1]; cpos[1]++) { bmin[2] = cmin[2] + csiz*clim[2][0]; - for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) { - bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]); + if (st->ndim == 3) { + cpos[2] = clim[2][0]; n = cpos[0]; - for (i = 1; i < st->ndim; i++) + for (i = 1; i < 3; i++) n = (n << st->log2GR) + cpos[i]; - for ( ; cpos[3] < clim[3][1]; cpos[3]++) { + for ( ; cpos[2] < clim[2][1]; cpos[2]++) { rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr); if (rv < 0) return rv; - bmin[3] += csiz; + bmin[2] += csiz; } - bmin[2] += csiz; + } else { + for (cpos[2] = clim[2][0]; cpos[2] < clim[2][1]; cpos[2]++) { + bmin[3] = cmin[3] + csiz*(cpos[3] = clim[3][0]); + n = cpos[0]; + for (i = 1; i < 4; i++) + n = (n << st->log2GR) + cpos[i]; + for ( ; cpos[3] < clim[3][1]; cpos[3]++) { + rval += rv = (*cf)(st->u.v[n++], bmin, csiz, cptr); + if (rv < 0) + return rv; + bmin[3] += csiz; + } + bmin[2] += csiz; + } } bmin[1] += csiz; } @@ -411,7 +430,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 +451,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 +492,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 +519,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 +543,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 +558,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 +575,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; @@ -573,7 +608,6 @@ SDgetTreCDist(const FVECT inVec, SDComponent *sdc) { const SDTre *sdt; double inCoord[2]; - int vflags; int i; SDTreCDst *cd, *cdlast; /* check arguments */ @@ -663,7 +697,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 +733,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,7 +751,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 @@ -721,10 +761,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, ','); } @@ -769,11 +808,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)) { @@ -845,7 +880,6 @@ load_bsdf_data(SDData *sd, ezxml_t wdb, int ndim) SDSpectralDF *df; SDTre *sdt; char *sdata; - int i; /* allocate BSDF component */ sdata = ezxml_txt(ezxml_child(wdb, "WavelengthDataDirection")); if (!sdata) @@ -922,7 +956,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 +984,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 +995,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);