--- ray/src/cv/bsdfinterp.c 2012/11/26 07:02:20 2.7 +++ ray/src/cv/bsdfinterp.c 2014/08/21 10:33:48 2.19 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdfinterp.c,v 2.7 2012/11/26 07:02:20 greg Exp $"; +static const char RCSid[] = "$Id: bsdfinterp.c,v 2.19 2014/08/21 10:33:48 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) @@ -151,8 +155,9 @@ static int in_mesh(MIGRATION *miga[3], unsigned char *emap, int nedges, const FVECT ivec, MIGRATION *mig) { - MIGRATION *ej1, *ej2; - RBFNODE *tv; + RBFNODE *tv[2]; + MIGRATION *sej[2], *dej[2]; + int i; /* check visitation record */ if (!check_edge(emap, nedges, mig, 1)) return(0); @@ -160,30 +165,38 @@ in_mesh(MIGRATION *miga[3], unsigned char *emap, int n miga[0] = mig; /* close enough to edge */ return(1); } - /* do triangles either side */ - for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL; - ej1 = nextedge(mig->rbfv[0],ej1)) { - if (ej1 == mig) - continue; - tv = opp_rbf(mig->rbfv[0],ej1); - for (ej2 = tv->ejl; ej2 != NULL; ej2 = nextedge(tv,ej2)) - if (opp_rbf(tv,ej2) == mig->rbfv[1]) { - int do_ej1 = check_edge(emap, nedges, ej1, 0); - int do_ej2 = check_edge(emap, nedges, ej2, 0); - if (do_ej1 && in_mesh(miga, emap, nedges, ivec, ej1)) - return(1); - if (do_ej2 && in_mesh(miga, emap, nedges, ivec, ej2)) - return(1); - /* check just once */ - if (do_ej1 & do_ej2 && in_tri(mig->rbfv[0], - mig->rbfv[1], tv, ivec)) { - miga[0] = mig; - miga[1] = ej1; - miga[2] = ej2; - return(1); + if (!get_triangles(tv, mig)) /* do triangles either side? */ + return(0); + for (i = 2; i--; ) { /* identify edges to check */ + MIGRATION *ej; + sej[i] = dej[i] = NULL; + if (tv[i] == NULL) + continue; + for (ej = tv[i]->ejl; ej != NULL; ej = nextedge(tv[i],ej)) { + RBFNODE *rbfop = opp_rbf(tv[i],ej); + if (rbfop == mig->rbfv[0]) { + if (check_edge(emap, nedges, ej, 0)) + sej[i] = ej; + } else if (rbfop == mig->rbfv[1]) { + if (check_edge(emap, nedges, ej, 0)) + dej[i] = ej; } } } + for (i = 2; i--; ) { /* check triangles just once */ + if (sej[i] != NULL && in_mesh(miga, emap, nedges, ivec, sej[i])) + return(1); + if (dej[i] != NULL && in_mesh(miga, emap, nedges, ivec, dej[i])) + return(1); + if ((sej[i] == NULL) | (dej[i] == NULL)) + continue; + if (in_tri(mig->rbfv[0], mig->rbfv[1], tv[i], ivec)) { + miga[0] = mig; + miga[1] = sej[i]; + miga[2] = dej[i]; + return(1); + } + } return(0); /* not near this edge */ } @@ -193,28 +206,28 @@ get_interp(MIGRATION *miga[3], FVECT invec) { miga[0] = miga[1] = miga[2] = NULL; if (single_plane_incident) { /* isotropic BSDF? */ - RBFNODE *rbf; /* find edge we're on */ - for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) { - if (input_orient*rbf->invec[2] < input_orient*invec[2]) - break; - if (rbf->next != NULL && - input_orient*rbf->next->invec[2] < - input_orient*invec[2]) { - for (miga[0] = rbf->ejl; miga[0] != NULL; - miga[0] = nextedge(rbf,miga[0])) - if (opp_rbf(rbf,miga[0]) == rbf->next) { - double nf = 1.-rbf->invec[2]*rbf->invec[2]; - if (nf > FTINY) { - nf = sqrt((1.-invec[2]*invec[2])/nf); - invec[0] = nf*rbf->invec[0]; - invec[1] = nf*rbf->invec[1]; - } - return(0); - } - break; + RBFNODE *rbf; /* find edge we're on */ + for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) { + if (input_orient*rbf->invec[2] < input_orient*invec[2]-FTINY) + break; + if (rbf->next != NULL && input_orient*rbf->next->invec[2] < + input_orient*invec[2]+FTINY) { + for (miga[0] = rbf->ejl; miga[0] != NULL; + miga[0] = nextedge(rbf,miga[0])) + if (opp_rbf(rbf,miga[0]) == rbf->next) { + double nf = 1. - + rbf->next->invec[2]*rbf->next->invec[2]; + if (nf > FTINY) { /* rotate to match */ + nf = sqrt((1.-invec[2]*invec[2])/nf); + invec[0] = nf*rbf->next->invec[0]; + invec[1] = nf*rbf->next->invec[1]; + } + return(0); /* rotational symmetry */ } + break; } - return(-1); /* outside range! */ + } + return(-1); /* outside range! */ } { /* else use triangle mesh */ int sym = use_symmetry(invec); @@ -231,9 +244,14 @@ get_interp(MIGRATION *miga[3], FVECT invec) exit(1); } /* identify intersection */ - if (!in_mesh(miga, emap, nedges, invec, mig_list)) + if (!in_mesh(miga, emap, nedges, invec, mig_list)) { +#ifdef DEBUG + fprintf(stderr, + "Incident angle (%.1f,%.1f) deg. outside mesh\n", + get_theta180(invec), get_phi360(invec)); +#endif sym = -1; /* outside mesh */ - else if (miga[1] != NULL && + } else if (miga[1] != NULL && (miga[2] == NULL || !order_triangle(miga))) { #ifdef DEBUG fputs("Munged triangle in get_interp()\n", stderr); @@ -245,85 +263,13 @@ 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) -{ - RBFNODE *rbf; - int n, i, j; - double t, full_dist; - /* get relative position */ - t = acos(DOT(invec, mig->rbfv[0]->invec)); - if (t < M_PI/grid_res) { /* near first DSF */ - n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[0]->nrbf-1); - rbf = (RBFNODE *)malloc(n); - if (rbf == NULL) - goto memerr; - memcpy(rbf, mig->rbfv[0], n); /* just duplicate */ - return(rbf); - } - full_dist = acos(DOT(mig->rbfv[0]->invec, mig->rbfv[1]->invec)); - if (t > full_dist-M_PI/grid_res) { /* near second DSF */ - n = sizeof(RBFNODE) + sizeof(RBFVAL)*(mig->rbfv[1]->nrbf-1); - rbf = (RBFNODE *)malloc(n); - if (rbf == NULL) - goto memerr; - memcpy(rbf, mig->rbfv[1], n); /* just duplicate */ - return(rbf); - } - t /= full_dist; - 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); -#ifdef DEBUG - fprintf(stderr, "Input RBFs have %d, %d nodes -> output has %d\n", - mig->rbfv[0]->nrbf, mig->rbfv[1]->nrbf, n); -#endif - rbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(n-1)); - if (rbf == NULL) - goto memerr; - rbf->next = NULL; rbf->ejl = NULL; - VCOPY(rbf->invec, invec); - rbf->nrbf = n; - rbf->vtotal = 1.-t + t*mig->rbfv[1]->vtotal/mig->rbfv[0]->vtotal; - n = 0; /* advect RBF lobes */ - for (i = 0; i < mtx_nrows(mig); i++) { - const RBFVAL *rbf0i = &mig->rbfv[0]->rbfa[i]; - const float peak0 = rbf0i->peak; - const double rad0 = R2ANG(rbf0i->crad); - FVECT v0; - 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) { - const RBFVAL *rbf1j = &mig->rbfv[1]->rbfa[j]; - double rad1 = R2ANG(rbf1j->crad); - 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)); - ovec_from_pos(v, rbf1j->gx, rbf1j->gy); - geodesic(v, v0, v, t*full_dist, GEOD_RAD); - pos_from_vec(pos, v); - rbf->rbfa[n].gx = pos[0]; - rbf->rbfa[n].gy = pos[1]; - ++n; - } - } - rbf->vtotal *= mig->rbfv[0]->vtotal; /* turn ratio into actual */ - return(rbf); -memerr: - fprintf(stderr, "%s: Out of memory in e_advect_rbf()\n", progname); - exit(1); - return(NULL); /* pro forma return */ -} +/* -/* Partially advect between recorded incident angles and allocate new RBF */ +/* Advect between recorded incident angles and allocate new RBF */ RBFNODE * -advect_rbf(const FVECT invec) +advect_rbf(const FVECT invec, int lobe_lim) { + double cthresh = FTINY; FVECT sivec; MIGRATION *miga[3]; RBFNODE *rbf; @@ -331,14 +277,14 @@ advect_rbf(const FVECT invec) float mbfact, mcfact; int n, i, j, k; FVECT v0, v1, v2; - double s, t, t_full; + double s, t; VCOPY(sivec, invec); /* find triangle/edge */ sym = get_interp(miga, sivec); if (sym < 0) /* can't interpolate? */ - return(NULL); + return(def_rbf_spec(invec)); 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 @@ -346,9 +292,9 @@ advect_rbf(const FVECT invec) return(rbf); } #ifdef DEBUG - if (miga[0]->rbfv[0] != miga[2]->rbfv[0] | - miga[0]->rbfv[1] != miga[1]->rbfv[0] | - miga[1]->rbfv[1] != miga[2]->rbfv[1]) { + if ((miga[0]->rbfv[0] != miga[2]->rbfv[0]) | + (miga[0]->rbfv[1] != miga[1]->rbfv[0]) | + (miga[1]->rbfv[1] != miga[2]->rbfv[1])) { fprintf(stderr, "%s: Triangle vertex screw-up!\n", progname); exit(1); } @@ -363,15 +309,20 @@ advect_rbf(const FVECT invec) s = acos(DOT(v0,v1)) / acos(DOT(v0,v2)); geodesic(v1, miga[0]->rbfv[0]->invec, miga[0]->rbfv[1]->invec, s, GEOD_REL); - t = acos(DOT(v1,sivec)) / - (t_full = acos(DOT(v1,miga[1]->rbfv[1]->invec))); + t = acos(DOT(v1,sivec)) / acos(DOT(v1,miga[1]->rbfv[1]->invec)); +tryagain: n = 0; /* count migrating particles */ for (i = 0; i < mtx_nrows(miga[0]); i++) for (j = 0; j < mtx_ncols(miga[0]); j++) - for (k = (mtx_coef(miga[0],i,j) > FTINY) * + for (k = (mtx_coef(miga[0],i,j) > cthresh) * mtx_ncols(miga[2]); k--; ) - n += (mtx_coef(miga[2],i,k) > FTINY || - mtx_coef(miga[1],j,k) > FTINY); + 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; + } #ifdef DEBUG fprintf(stderr, "Input RBFs have %d, %d, %d nodes -> output has %d\n", miga[0]->rbfv[0]->nrbf, miga[0]->rbfv[1]->nrbf, @@ -398,30 +349,31 @@ advect_rbf(const FVECT invec) for (j = 0; j < mtx_ncols(miga[0]); j++) { const float ma = mtx_coef(miga[0],i,j); const RBFVAL *rbf1j; - double rad1j, srad2; - if (ma <= FTINY) + 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; - FVECT vout; + double rad2; int pos[2]; - if ((mb <= FTINY) & (mc <= FTINY)) + 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(vout, v1, v2, t*t_full, GEOD_RAD); - pos_from_vec(pos, vout); + geodesic(v2, v1, v2, t, GEOD_REL); + pos_from_vec(pos, v2); rbf->rbfa[n].gx = pos[0]; rbf->rbfa[n].gy = pos[1]; ++n;