--- ray/src/cv/bsdfrep.c 2012/11/07 03:04:23 2.5 +++ ray/src/cv/bsdfrep.c 2013/11/08 05:37:20 2.18 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdfrep.c,v 2.5 2012/11/07 03:04:23 greg Exp $"; +static const char RCSid[] = "$Id: bsdfrep.c,v 2.18 2013/11/08 05:37:20 greg Exp $"; #endif /* * Support BSDF representation as radial basis functions. @@ -26,6 +26,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; @@ -56,6 +62,8 @@ new_input_direction(double new_theta, double new_phi) new_theta = -new_theta; new_phi += 180.; } + if ((theta_in_deg = new_theta) < 1.0) + return(1); /* don't rely on phi near normal */ while (new_phi < 0) new_phi += 360.; while (new_phi >= 360.) @@ -64,7 +72,6 @@ new_input_direction(double new_theta, double new_phi) single_plane_incident = (round(new_phi) == round(phi_in_deg)); else if (single_plane_incident < 0) single_plane_incident = 1; - theta_in_deg = new_theta; /* assume it's OK */ phi_in_deg = new_phi; if ((1. < new_phi) & (new_phi < 89.)) inp_coverage |= INP_QUAD1; @@ -81,7 +88,7 @@ new_input_direction(double new_theta, double new_phi) int use_symmetry(FVECT vec) { - double phi = get_phi360(vec); + const double phi = get_phi360(vec); switch (inp_coverage) { case INP_QUAD1|INP_QUAD2|INP_QUAD3|INP_QUAD4: @@ -177,13 +184,25 @@ rev_rbf_symmetry(RBFNODE *rbf, int sym) rbf->rbfa[n].gy = grid_res-1 - rbf->rbfa[n].gy; } -/* Compute volume associated with Gaussian lobe */ -double -rbf_volume(const RBFVAL *rbfp) +/* Rotate RBF to correspond to given incident vector */ +void +rotate_rbf(RBFNODE *rbf, const FVECT invec) { - double rad = R2ANG(rbfp->crad); + static const FVECT vnorm = {.0, .0, 1.}; + const double phi = atan2(invec[1],invec[0]) - + atan2(rbf->invec[1],rbf->invec[0]); + FVECT outvec; + int pos[2]; + int n; - return((2.*M_PI) * rbfp->peak * rad*rad); + for (n = ((-.01 > phi) | (phi > .01))*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); + rbf->rbfa[n].gx = pos[0]; + rbf->rbfa[n].gy = pos[1]; + } + VCOPY(rbf->invec, invec); } /* Compute outgoing vector from grid position */ @@ -193,7 +212,7 @@ ovec_from_pos(FVECT vec, int xpos, int ypos) double uv[2]; double r2; - SDsquare2disk(uv, (1./grid_res)*(xpos+.5), (1./grid_res)*(ypos+.5)); + SDsquare2disk(uv, (xpos+.5)/grid_res, (ypos+.5)/grid_res); /* uniform hemispherical projection */ r2 = uv[0]*uv[0] + uv[1]*uv[1]; vec[0] = vec[1] = sqrt(2. - r2); @@ -215,26 +234,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) { - double res = .0; + 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); } @@ -368,6 +421,7 @@ save_bsdf_rep(FILE *ofp) 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 */ @@ -433,6 +487,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); @@ -455,21 +513,24 @@ load_bsdf_rep(FILE *ifp) 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; rbfh.invec[0] = getflt(ifp); rbfh.invec[1] = getflt(ifp); rbfh.invec[2] = getflt(ifp); + if (normalize(rbfh.invec) == 0) { + fprintf(stderr, "%s: zero incident vector\n", progname); + return(0); + } rbfh.vtotal = getflt(ifp); rbfh.nrbf = getint(4, ifp); newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(rbfh.nrbf-1)); if (newrbf == NULL) goto memerr; - memcpy(newrbf, &rbfh, sizeof(RBFNODE)); + *newrbf = rbfh; for (i = 0; i < rbfh.nrbf; i++) { newrbf->rbfa[i].peak = getflt(ifp); newrbf->rbfa[i].crad = getint(2, ifp) & 0xffff;