--- ray/src/cv/bsdfinterp.c 2013/09/26 17:05:00 2.12 +++ ray/src/cv/bsdfinterp.c 2013/10/23 03:41:39 2.15 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdfinterp.c,v 2.12 2013/09/26 17:05:00 greg Exp $"; +static const char RCSid[] = "$Id: bsdfinterp.c,v 2.15 2013/10/23 03:41:39 greg Exp $"; #endif /* * Interpolate BSDF data from radial basis functions in advection mesh. @@ -94,10 +94,14 @@ on_edge(const MIGRATION *ej, const FVECT ivec) cos_a = DOT(ej->rbfv[0]->invec, ivec); if (cos_a <= 0) return(0); + if (cos_a >= 1.) /* handles rounding error */ + return(1); cos_b = DOT(ej->rbfv[1]->invec, ivec); if (cos_b <= 0) return(0); + if (cos_b >= 1.) + return(1); cos_aplusb = cos_a*cos_b - sqrt((1.-cos_a*cos_a)*(1.-cos_b*cos_b)); if (cos_aplusb <= 0) @@ -260,8 +264,9 @@ get_interp(MIGRATION *miga[3], FVECT invec) /* Advect and allocate new RBF along edge */ static RBFNODE * -e_advect_rbf(const MIGRATION *mig, const FVECT invec) +e_advect_rbf(const MIGRATION *mig, const FVECT invec, int lobe_lim) { + double cthresh = FTINY; RBFNODE *rbf; int n, i, j; double t, full_dist; @@ -286,11 +291,17 @@ e_advect_rbf(const MIGRATION *mig, const FVECT invec) rbf->next = NULL; rbf->ejl = NULL; return(rbf); } - t /= full_dist; + t /= full_dist; +tryagain: n = 0; /* count migrating particles */ for (i = 0; i < mtx_nrows(mig); i++) for (j = 0; j < mtx_ncols(mig); j++) - n += (mtx_coef(mig,i,j) > FTINY); + n += (mtx_coef(mig,i,j) > cthresh); + /* are we over our limit? */ + if ((lobe_lim > 0) & (n > lobe_lim)) { + cthresh = cthresh*2. + 10.*FTINY; + goto tryagain; + } #ifdef DEBUG fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n", mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n); @@ -311,14 +322,16 @@ e_advect_rbf(const MIGRATION *mig, const FVECT invec) float mv; ovec_from_pos(v0, rbf0i->gx, rbf0i->gy); for (j = 0; j < mtx_ncols(mig); j++) - if ((mv = mtx_coef(mig,i,j)) > FTINY) { + if ((mv = mtx_coef(mig,i,j)) > cthresh) { const RBFVAL *rbf1j = &mig->rbfv[1]->rbfa[j]; - double rad1 = R2ANG(rbf1j->crad); + double rad2; FVECT v; int pos[2]; - rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal; - rbf->rbfa[n].crad = ANG2R(sqrt(rad0*rad0*(1.-t) + - rad1*rad1*t)); + rad2 = R2ANG(rbf1j->crad); + rad2 = rad0*rad0*(1.-t) + rad2*rad2*t; + rbf->rbfa[n].peak = peak0 * mv * rbf->vtotal * + rad0*rad0/rad2; + rbf->rbfa[n].crad = ANG2R(sqrt(rad2)); ovec_from_pos(v, rbf1j->gx, rbf1j->gy); geodesic(v, v0, v, t, GEOD_REL); pos_from_vec(pos, v); @@ -354,7 +367,7 @@ advect_rbf(const FVECT invec, int lobe_lim) if (sym < 0) /* can't interpolate? */ return(NULL); if (miga[1] == NULL) { /* advect along edge? */ - rbf = e_advect_rbf(miga[0], sivec); + rbf = e_advect_rbf(miga[0], sivec, lobe_lim); if (single_plane_incident) rotate_rbf(rbf, invec); else @@ -388,6 +401,7 @@ tryagain: mtx_ncols(miga[2]); k--; ) n += (mtx_coef(miga[2],i,k) > cthresh || mtx_coef(miga[1],j,k) > cthresh); + /* are we over our limit? */ if ((lobe_lim > 0) & (n > lobe_lim)) { cthresh = cthresh*2. + 10.*FTINY; goto tryagain; @@ -418,26 +432,28 @@ tryagain: for (j = 0; j < mtx_ncols(miga[0]); j++) { const float ma = mtx_coef(miga[0],i,j); const RBFVAL *rbf1j; - double rad1j, srad2; + double srad2; if (ma <= cthresh) continue; rbf1j = &miga[0]->rbfv[1]->rbfa[j]; - rad1j = R2ANG(rbf1j->crad); - srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*rad1j*rad1j; + srad2 = R2ANG(rbf1j->crad); + srad2 = (1.-s)*(1.-t)*rad0i*rad0i + s*(1.-t)*srad2*srad2; ovec_from_pos(v1, rbf1j->gx, rbf1j->gy); geodesic(v1, v0, v1, s, GEOD_REL); for (k = 0; k < mtx_ncols(miga[2]); k++) { float mb = mtx_coef(miga[1],j,k); float mc = mtx_coef(miga[2],i,k); const RBFVAL *rbf2k; - double rad2k; + double rad2; int pos[2]; if ((mb <= cthresh) & (mc <= cthresh)) continue; rbf2k = &miga[2]->rbfv[1]->rbfa[k]; - rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact); - rad2k = R2ANG(rbf2k->crad); - rbf->rbfa[n].crad = ANG2R(sqrt(srad2 + t*rad2k*rad2k)); + rad2 = R2ANG(rbf2k->crad); + rad2 = srad2 + t*rad2*rad2; + rbf->rbfa[n].peak = w0i * ma * (mb*mbfact + mc*mcfact) * + rad0i*rad0i/rad2; + rbf->rbfa[n].crad = ANG2R(sqrt(rad2)); ovec_from_pos(v2, rbf2k->gx, rbf2k->gy); geodesic(v2, v1, v2, t, GEOD_REL); pos_from_vec(pos, v2);