--- ray/src/cv/bsdfrep.c 2012/11/22 06:07:17 2.11 +++ ray/src/cv/bsdfrep.c 2014/03/07 21:21:02 2.21 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdfrep.c,v 2.11 2012/11/22 06:07:17 greg Exp $"; +static const char RCSid[] = "$Id: bsdfrep.c,v 2.21 2014/03/07 21:21:02 greg Exp $"; #endif /* * Support BSDF representation as radial basis functions. @@ -14,6 +14,9 @@ static const char RCSid[] = "$Id: bsdfrep.c,v 2.11 201 #include "rtio.h" #include "resolu.h" #include "bsdfrep.h" + /* name and manufacturer if known */ +char bsdf_name[256]; +char bsdf_manuf[256]; /* active grid resolution */ int grid_res = GRIDRES; @@ -26,6 +29,12 @@ int single_plane_incident = -1; int input_orient = 0; int output_orient = 0; + /* BSDF histogram */ +unsigned long bsdf_hist[HISTLEN]; + + /* BSDF value for boundary regions */ +double bsdf_min = 0; + /* processed incident DSF measurements */ RBFNODE *dsf_list = NULL; @@ -199,15 +208,6 @@ rotate_rbf(RBFNODE *rbf, const FVECT invec) VCOPY(rbf->invec, invec); } -/* Compute volume associated with Gaussian lobe */ -double -rbf_volume(const RBFVAL *rbfp) -{ - double rad = R2ANG(rbfp->crad); - - return((2.*M_PI) * rbfp->peak * rad*rad); -} - /* Compute outgoing vector from grid position */ void ovec_from_pos(FVECT vec, int xpos, int ypos) @@ -237,26 +237,60 @@ pos_from_vec(int pos[2], const FVECT vec) pos[1] = (int)(sq[1]*grid_res); } +/* Compute volume associated with Gaussian lobe */ +double +rbf_volume(const RBFVAL *rbfp) +{ + double rad = R2ANG(rbfp->crad); + FVECT odir; + double elev, integ; + /* infinite integral approximation */ + integ = (2.*M_PI) * rbfp->peak * rad*rad; + /* check if we're near horizon */ + ovec_from_pos(odir, rbfp->gx, rbfp->gy); + elev = output_orient*odir[2]; + /* apply cut-off correction if > 1% */ + if (elev < 2.8*rad) { + /* elev = asin(elev); /* this is so crude, anyway... */ + integ *= 1. - .5*exp(-.5*elev*elev/(rad*rad)); + } + return(integ); +} + /* Evaluate RBF for DSF at the given normalized outgoing direction */ double eval_rbfrep(const RBFNODE *rp, const FVECT outvec) { + const double rfact2 = (38./M_PI/M_PI)*(grid_res*grid_res); + double minval = bsdf_min*output_orient*outvec[2]; + int pos[2]; double res = 0; const RBFVAL *rbfp; FVECT odir; - double sig2; + double rad2; int n; - - if (rp == NULL) + /* check for wrong side */ + if (outvec[2] > 0 ^ output_orient > 0) return(.0); + /* use minimum if no information avail. */ + if (rp == NULL) + return(minval); + /* optimization for fast lobe culling */ + pos_from_vec(pos, outvec); + /* sum radial basis function */ rbfp = rp->rbfa; for (n = rp->nrbf; n--; rbfp++) { + int d2 = (pos[0]-rbfp->gx)*(pos[0]-rbfp->gx) + + (pos[1]-rbfp->gy)*(pos[1]-rbfp->gy); + rad2 = R2ANG(rbfp->crad); + rad2 *= rad2; + if (d2 > rad2*rfact2) + continue; ovec_from_pos(odir, rbfp->gx, rbfp->gy); - sig2 = R2ANG(rbfp->crad); - sig2 = (DOT(odir,outvec) - 1.) / (sig2*sig2); - if (sig2 > -19.) - res += rbfp->peak * exp(sig2); + res += rbfp->peak * exp((DOT(odir,outvec) - 1.) / rad2); } + if (res < minval) /* never return less than minval */ + return(minval); return(res); } @@ -359,6 +393,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) @@ -373,6 +493,8 @@ clear_bsdf_rep(void) dsf_list = rbf->next; free(rbf); } + bsdf_name[0] = '\0'; + bsdf_manuf[0] = '\0'; inp_coverage = 0; single_plane_incident = -1; input_orient = output_orient = 0; @@ -387,9 +509,14 @@ save_bsdf_rep(FILE *ofp) MIGRATION *mig; int i, n; /* finish header */ + if (bsdf_name[0]) + fprintf(ofp, "NAME=%s\n", bsdf_name); + if (bsdf_manuf[0]) + fprintf(ofp, "MANUFACT=%s\n", bsdf_manuf); fprintf(ofp, "SYMMETRY=%d\n", !single_plane_incident * inp_coverage); fprintf(ofp, "IO_SIDES= %d %d\n", input_orient, output_orient); fprintf(ofp, "GRIDRES=%d\n", grid_res); + fprintf(ofp, "BSDFMIN=%g\n", bsdf_min); fputformat(BSDFREP_FMT, ofp); fputc('\n', ofp); /* write each DSF */ @@ -442,6 +569,14 @@ headline(char *s, void *p) { char fmt[32]; + if (!strncmp(s, "NAME=", 5)) { + strcpy(bsdf_name, s+5); + bsdf_name[strlen(bsdf_name)-1] = '\0'; + } + if (!strncmp(s, "MANUFACT=", 9)) { + strcpy(bsdf_manuf, s+9); + bsdf_manuf[strlen(bsdf_manuf)-1] = '\0'; + } if (!strncmp(s, "SYMMETRY=", 9)) { inp_coverage = atoi(s+9); single_plane_incident = !inp_coverage; @@ -455,6 +590,10 @@ headline(char *s, void *p) sscanf(s+8, "%d", &grid_res); return(0); } + if (!strncmp(s, "BSDFMIN=", 8)) { + sscanf(s+8, "%lf", &bsdf_min); + return(0); + } if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT)) return(-1); return(0); @@ -471,14 +610,13 @@ load_bsdf_rep(FILE *ifp) clear_bsdf_rep(); if (ifp == NULL) return(0); - if (getheader(ifp, headline, NULL) < 0 || single_plane_incident < 0 | + if (getheader(ifp, headline, NULL) < 0 || (single_plane_incident < 0) | !input_orient | !output_orient) { fprintf(stderr, "%s: missing/bad format for BSDF interpolant\n", progname); return(0); } - rbfh.next = NULL; /* read each DSF */ - rbfh.ejl = NULL; + memset(&rbfh, 0, sizeof(rbfh)); /* read each DSF */ while ((rbfh.ord = getint(4, ifp)) >= 0) { RBFNODE *newrbf; @@ -495,7 +633,7 @@ load_bsdf_rep(FILE *ifp) sizeof(RBFVAL)*(rbfh.nrbf-1)); if (newrbf == NULL) goto memerr; - memcpy(newrbf, &rbfh, sizeof(RBFNODE)-sizeof(RBFVAL)); + *newrbf = rbfh; for (i = 0; i < rbfh.nrbf; i++) { newrbf->rbfa[i].peak = getflt(ifp); newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff;