--- ray/src/cv/bsdfrep.c 2012/10/19 04:14:29 2.1 +++ ray/src/cv/bsdfrep.c 2012/11/07 03:04:23 2.5 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdfrep.c,v 2.1 2012/10/19 04:14:29 greg Exp $"; +static const char RCSid[] = "$Id: bsdfrep.c,v 2.5 2012/11/07 03:04:23 greg Exp $"; #endif /* * Support BSDF representation as radial basis functions. @@ -9,11 +9,15 @@ static const char RCSid[] = "$Id: bsdfrep.c,v 2.1 2012 #define _USE_MATH_DEFINES #include +#include #include #include "rtio.h" #include "resolu.h" #include "bsdfrep.h" - /* which quadrants are represented */ + /* active grid resolution */ +int grid_res = GRIDRES; + + /* coverage/symmetry using INP_QUAD? flags */ int inp_coverage = 0; /* all incident angles in-plane so far? */ int single_plane_incident = -1; @@ -167,10 +171,10 @@ rev_rbf_symmetry(RBFNODE *rbf, int sym) rev_symmetry(rbf->invec, sym); if (sym & MIRROR_X) for (n = rbf->nrbf; n-- > 0; ) - rbf->rbfa[n].gx = GRIDRES-1 - rbf->rbfa[n].gx; + rbf->rbfa[n].gx = grid_res-1 - rbf->rbfa[n].gx; if (sym & MIRROR_Y) for (n = rbf->nrbf; n-- > 0; ) - rbf->rbfa[n].gy = GRIDRES-1 - rbf->rbfa[n].gy; + rbf->rbfa[n].gy = grid_res-1 - rbf->rbfa[n].gy; } /* Compute volume associated with Gaussian lobe */ @@ -189,7 +193,7 @@ ovec_from_pos(FVECT vec, int xpos, int ypos) double uv[2]; double r2; - SDsquare2disk(uv, (1./GRIDRES)*(xpos+.5), (1./GRIDRES)*(ypos+.5)); + SDsquare2disk(uv, (1./grid_res)*(xpos+.5), (1./grid_res)*(ypos+.5)); /* uniform hemispherical projection */ r2 = uv[0]*uv[0] + uv[1]*uv[1]; vec[0] = vec[1] = sqrt(2. - r2); @@ -207,8 +211,8 @@ pos_from_vec(int pos[2], const FVECT vec) SDdisk2square(sq, vec[0]*norm, vec[1]*norm); - pos[0] = (int)(sq[0]*GRIDRES); - pos[1] = (int)(sq[1]*GRIDRES); + pos[0] = (int)(sq[0]*grid_res); + pos[1] = (int)(sq[1]*grid_res); } /* Evaluate RBF for DSF at the given normalized outgoing direction */ @@ -280,7 +284,7 @@ get_dsf(int ord) RBFNODE *rbf; for (rbf = dsf_list; rbf != NULL; rbf = rbf->next) - if (rbf->ord == ord); + if (rbf->ord == ord) return(rbf); return(NULL); } @@ -311,17 +315,17 @@ is_rev_tri(const FVECT v1, const FVECT v2, const FVECT int get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig) { - const MIGRATION *ej, *ej2; + const MIGRATION *ej1, *ej2; RBFNODE *tv; rbfv[0] = rbfv[1] = NULL; if (mig == NULL) return(0); - for (ej = mig->rbfv[0]->ejl; ej != NULL; - ej = nextedge(mig->rbfv[0],ej)) { - if (ej == mig) + for (ej1 = mig->rbfv[0]->ejl; ej1 != NULL; + ej1 = nextedge(mig->rbfv[0],ej1)) { + if (ej1 == mig) continue; - tv = opp_rbf(mig->rbfv[0],ej); + 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]) { rbfv[is_rev_tri(mig->rbfv[0]->invec, @@ -333,6 +337,26 @@ get_triangles(RBFNODE *rbfv[2], const MIGRATION *mig) return((rbfv[0] != NULL) + (rbfv[1] != NULL)); } +/* Clear our BSDF representation and free memory */ +void +clear_bsdf_rep(void) +{ + while (mig_list != NULL) { + MIGRATION *mig = mig_list; + mig_list = mig->next; + free(mig); + } + while (dsf_list != NULL) { + RBFNODE *rbf = dsf_list; + dsf_list = rbf->next; + free(rbf); + } + inp_coverage = 0; + single_plane_incident = -1; + input_orient = output_orient = 0; + grid_res = GRIDRES; +} + /* Write our BSDF mesh interpolant out to the given binary stream */ void save_bsdf_rep(FILE *ofp) @@ -341,6 +365,9 @@ save_bsdf_rep(FILE *ofp) MIGRATION *mig; int i, n; /* finish header */ + 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); fputformat(BSDFREP_FMT, ofp); fputc('\n', ofp); /* write each DSF */ @@ -360,12 +387,23 @@ save_bsdf_rep(FILE *ofp) } putint(-1, 4, ofp); /* terminator */ /* write each migration matrix */ - for (mig = mig_list; mig != NULL; mig = mig_list->next) { + for (mig = mig_list; mig != NULL; mig = mig->next) { + int zerocnt = 0; putint(mig->rbfv[0]->ord, 4, ofp); putint(mig->rbfv[1]->ord, 4, ofp); + /* write out as sparse data */ n = mtx_nrows(mig) * mtx_ncols(mig); - for (i = 0; i < n; i++) - putflt(mig->mtx[i], ofp); + for (i = 0; i < n; i++) { + if (zerocnt == 0xff) { + putint(0xff, 1, ofp); zerocnt = 0; + } + if (mig->mtx[i] != 0) { + putint(zerocnt, 1, ofp); zerocnt = 0; + putflt(mig->mtx[i], ofp); + } else + ++zerocnt; + } + putint(zerocnt, 1, ofp); } putint(-1, 4, ofp); /* terminator */ putint(-1, 4, ofp); @@ -376,6 +414,30 @@ save_bsdf_rep(FILE *ofp) } } +/* Check header line for critical information */ +static int +headline(char *s, void *p) +{ + char fmt[32]; + + if (!strncmp(s, "SYMMETRY=", 9)) { + inp_coverage = atoi(s+9); + single_plane_incident = !inp_coverage; + return(0); + } + if (!strncmp(s, "IO_SIDES=", 9)) { + sscanf(s+9, "%d %d", &input_orient, &output_orient); + return(0); + } + if (!strncmp(s, "GRIDRES=", 8)) { + sscanf(s+8, "%d", &grid_res); + return(0); + } + if (formatval(fmt, s) && strcmp(fmt, BSDFREP_FMT)) + return(-1); + return(0); +} + /* Read a BSDF mesh interpolant from the given binary stream */ int load_bsdf_rep(FILE *ifp) @@ -383,15 +445,12 @@ load_bsdf_rep(FILE *ifp) RBFNODE rbfh; int from_ord, to_ord; int i; -#ifdef DEBUG - if ((dsf_list != NULL) | (mig_list != NULL)) { - fprintf(stderr, - "%s: attempt to load BSDF interpolant over existing\n", - progname); + + clear_bsdf_rep(); + if (ifp == NULL) return(0); - } -#endif - if (checkheader(ifp, BSDFREP_FMT, NULL) <= 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); @@ -404,9 +463,8 @@ load_bsdf_rep(FILE *ifp) rbfh.invec[0] = getflt(ifp); rbfh.invec[1] = getflt(ifp); rbfh.invec[2] = getflt(ifp); + rbfh.vtotal = getflt(ifp); rbfh.nrbf = getint(4, ifp); - if (!new_input_vector(rbfh.invec)) - return(0); newrbf = (RBFNODE *)malloc(sizeof(RBFNODE) + sizeof(RBFVAL)*(rbfh.nrbf-1)); if (newrbf == NULL) @@ -447,9 +505,15 @@ load_bsdf_rep(FILE *ifp) goto memerr; newmig->rbfv[0] = from_rbf; newmig->rbfv[1] = to_rbf; - /* read matrix coefficients */ - for (i = 0; i < n; i++) - newmig->mtx[i] = getflt(ifp); + memset(newmig->mtx, 0, sizeof(float)*n); + for (i = 0; ; ) { /* read sparse data */ + int zc = getint(1, ifp) & 0xff; + if ((i += zc) >= n) + break; + if (zc == 0xff) + continue; + newmig->mtx[i++] = getflt(ifp); + } if (feof(ifp)) goto badEOF; /* insert in edge lists */