--- ray/src/cv/bsdfrep.c 2013/11/09 05:47:49 2.19 +++ ray/src/cv/bsdfrep.c 2014/03/25 14:55:35 2.24 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdfrep.c,v 2.19 2013/11/09 05:47:49 greg Exp $"; +static const char RCSid[] = "$Id: bsdfrep.c,v 2.24 2014/03/25 14:55:35 greg Exp $"; #endif /* * Support BSDF representation as radial basis functions. @@ -198,7 +198,7 @@ rotate_rbf(RBFNODE *rbf, const FVECT invec) int pos[2]; int n; - for (n = ((-.01 > phi) | (phi > .01))*rbf->nrbf; n-- > 0; ) { + for (n = (cos(phi) < 1.-FTINY)*rbf->nrbf; n-- > 0; ) { ovec_from_pos(outvec, rbf->rbfa[n].gx, rbf->rbfa[n].gy); spinvector(outvec, outvec, vnorm, phi); pos_from_vec(pos, outvec); @@ -304,8 +304,9 @@ insert_dsf(RBFNODE *newrbf) for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) if (DOT(rbf->invec, newrbf->invec) >= 1.-FTINY) { fprintf(stderr, - "%s: Duplicate incident measurement (ignored)\n", - progname); + "%s: Duplicate incident measurement ignored at (%.1f,%.1f)\n", + progname, get_theta180(newrbf->invec), + get_phi360(newrbf->invec)); free(newrbf); return(-1); } @@ -393,6 +394,92 @@ get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig) return((rbfv[0] != NULL) + (rbfv[1] != NULL)); } +/* Advect and allocate new RBF along edge (internal call) */ +RBFNODE * +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; + /* 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 */ + rbf->next = NULL; rbf->ejl = NULL; + 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 */ + rbf->next = NULL; rbf->ejl = NULL; + return(rbf); + } + 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) > 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); +#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)) > cthresh) { + const RBFVAL *rbf1j = &mig->rbfv[1]->rbfa[j]; + double rad2; + FVECT v; + int pos[2]; + 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); + 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 */ +} + /* Clear our BSDF representation and free memory */ void clear_bsdf_rep(void) @@ -524,8 +611,9 @@ load_bsdf_rep(FILE *ifp) clear_bsdf_rep(); if (ifp == NULL) return(0); - if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 | - !input_orient | !output_orient) { + if (getheader(ifp, headline, NULL) < 0 || (single_plane_incident < 0) | + !input_orient | !output_orient | + (grid_res < 16) | (grid_res > 256)) { fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n", progname); return(0);