--- ray/src/common/bsdf_t.c 2011/05/01 16:34:37 3.12 +++ ray/src/common/bsdf_t.c 2011/08/22 05:48:39 3.21 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdf_t.c,v 3.12 2011/05/01 16:34:37 greg Exp $"; +static const char RCSid[] = "$Id: bsdf_t.c,v 3.21 2011/08/22 05:48:39 greg Exp $"; #endif /* * bsdf_t.c @@ -10,6 +10,7 @@ static const char RCSid[] = "$Id: bsdf_t.c,v 3.12 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 { @@ -91,7 +94,7 @@ SDfreeTre(SDNode *st) return; for (n = (st->log2GR < 0) << st->ndim; n--; ) SDfreeTre(st->u.t[n]); - free((void *)st); + 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 = 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; @@ -222,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--; ) { @@ -234,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) { @@ -242,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() */ @@ -269,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; } @@ -410,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]; @@ -432,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) { @@ -545,6 +564,7 @@ make_cdist(const SDTre *sdt, const double *pos) 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); @@ -555,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; @@ -583,23 +607,26 @@ const SDCDst * SDgetTreCDist(const FVECT inVec, SDComponent *sdc) { const SDTre *sdt; - double inCoord[2]; - int vflags; + double inCoord[2], quantum; int i; SDTreCDst *cd, *cdlast; /* check arguments */ if ((inVec == NULL) | (sdc == NULL) || (sdt = (SDTre *)sdc->dist) == NULL) return NULL; - if (sdt->st->ndim == 3) /* isotropic BSDF? */ + if (sdt->st->ndim == 3) { /* isotropic BSDF? */ inCoord[0] = .5 - .5*sqrt(inVec[0]*inVec[0] + inVec[1]*inVec[1]); - else if (sdt->st->ndim == 4) + } else if (sdt->st->ndim == 4) { SDdisk2square(inCoord, -inVec[0], -inVec[1]); - else + } else return NULL; /* should be internal error */ + /* quantize to avoid f.p. errors */ + quantum = SDsmallestLeaf(sdt->st); + for (i = sdt->st->ndim - 2; i--; ) + inCoord[i] = floor(inCoord[i]/quantum)*quantum + .5*quantum; cdlast = NULL; /* check for direction in cache list */ for (cd = (SDTreCDst *)sdc->cdList; cd != NULL; - cdlast = cd, cd = (SDTreCDst *)cd->next) { + cdlast = cd, cd = cd->next) { for (i = sdt->st->ndim - 2; i--; ) if ((cd->clim[i][0] > inCoord[i]) | (inCoord[i] >= cd->clim[i][1])) @@ -611,7 +638,7 @@ SDgetTreCDist(const FVECT inVec, SDComponent *sdc) cdlast = cd = make_cdist(sdt, inCoord); if (cdlast != NULL) { /* move entry to head of cache list */ cdlast->next = cd->next; - cd->next = sdc->cdList; + cd->next = (SDTreCDst *)sdc->cdList; sdc->cdList = (SDCDst *)cd; } return (SDCDst *)cd; /* ready to go */ @@ -674,7 +701,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)) @@ -687,7 +714,7 @@ SDsampTreCDist(FVECT ioVec, double randX, const SDCDst /* binary search to find position */ ilower = 0; iupper = cd->calen; while ((i = (iupper + ilower) >> 1) != ilower) - if ((long)target >= (long)cd->carr[i].cuml) + if (target >= cd->carr[i].cuml) ilower = i; else iupper = i; @@ -710,7 +737,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; } @@ -780,7 +812,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 */ - 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)) { @@ -852,7 +884,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) @@ -957,7 +988,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; } } @@ -971,7 +1003,7 @@ subtract_min(SDNode *st) int n; vmin = 1./M_PI; if (st->log2GR < 0) { - for (n = 0; n < 4; n++) { + for (n = 0; n < 8; n += 2) { float v = SDgetTreMin(st->u.t[n]); if (v < vmin) vmin = v;