--- ray/src/cv/bsdfrbf.c 2014/02/18 16:42:16 2.18 +++ ray/src/cv/bsdfrbf.c 2014/03/15 19:47:16 2.21 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: bsdfrbf.c,v 2.18 2014/02/18 16:42:16 greg Exp $"; +static const char RCSid[] = "$Id: bsdfrbf.c,v 2.21 2014/03/15 19:47:16 greg Exp $"; #endif /* * Radial basis function representation for BSDF data. @@ -32,7 +32,7 @@ static const char RCSid[] = "$Id: bsdfrbf.c,v 2.18 201 #define RSCA 2.2 /* radius scaling factor (empirical) */ #endif #ifndef SMOOTH_MSE -#define SMOOTH_MSE 2e-5 /* acceptable mean squared error */ +#define SMOOTH_MSE 5e-5 /* acceptable mean squared error */ #endif #ifndef SMOOTH_MSER #define SMOOTH_MSER 0.03 /* acceptable relative MSE */ @@ -41,7 +41,7 @@ static const char RCSid[] = "$Id: bsdfrbf.c,v 2.18 201 #define RBFALLOCB 10 /* RBF allocation block size */ - /* our loaded grid for this incident angle */ + /* our loaded grid or comparison DSFs */ GRIDVAL dsf_grid[GRIDRES][GRIDRES]; /* Start new DSF input grid */ @@ -72,9 +72,7 @@ add_bsdf_data(double theta_out, double phi_out, double ovec[1] = sin((M_PI/180.)*phi_out) * ovec[2]; ovec[2] = sqrt(1. - ovec[2]*ovec[2]); - if (val <= 0) /* truncate to zero */ - val = 0; - else if (!isDSF) + if (!isDSF) val *= ovec[2]; /* convert from BSDF to DSF */ /* update BSDF histogram */ @@ -83,8 +81,8 @@ add_bsdf_data(double theta_out, double phi_out, double pos_from_vec(pos, ovec); - dsf_grid[pos[0]][pos[1]].vsum += val; - dsf_grid[pos[0]][pos[1]].nval++; + dsf_grid[pos[0]][pos[1]].sum.v += val; + dsf_grid[pos[0]][pos[1]].sum.n++; } /* Compute minimum BSDF from histogram (does not clear) */ @@ -116,7 +114,7 @@ empty_region(int x0, int x1, int y0, int y1) for (x = x0; x < x1; x++) for (y = y0; y < y1; y++) - if (dsf_grid[x][y].nval) + if (dsf_grid[x][y].sum.n) return(0); return(1); } @@ -134,8 +132,8 @@ smooth_region(int x0, int x1, int y0, int y1) memset(xvec, 0, sizeof(xvec)); for (x = x0; x < x1; x++) for (y = y0; y < y1; y++) - if ((n = dsf_grid[x][y].nval) > 0) { - double z = dsf_grid[x][y].vsum; + if ((n = dsf_grid[x][y].sum.n) > 0) { + double z = dsf_grid[x][y].sum.v; rMtx[0][0] += x*x*(double)n; rMtx[0][1] += x*y*(double)n; rMtx[0][2] += x*(double)n; @@ -158,8 +156,8 @@ smooth_region(int x0, int x1, int y0, int y1) sqerr = 0.0; /* compute mean squared error */ for (x = x0; x < x1; x++) for (y = y0; y < y1; y++) - if ((n = dsf_grid[x][y].nval) > 0) { - double d = A*x + B*y + C - dsf_grid[x][y].vsum/n; + if ((n = dsf_grid[x][y].sum.n) > 0) { + double d = A*x + B*y + C - dsf_grid[x][y].sum.v/n; sqerr += n*d*d; } if (sqerr <= nvs*SMOOTH_MSE) /* below absolute MSE threshold? */ @@ -169,7 +167,7 @@ smooth_region(int x0, int x1, int y0, int y1) } /* Create new lobe based on integrated samples in region */ -static void +static int create_lobe(RBFVAL *rvp, int x0, int x1, int y0, int y1) { double vtot = 0.0; @@ -179,14 +177,16 @@ create_lobe(RBFVAL *rvp, int x0, int x1, int y0, int y /* compute average for region */ for (x = x0; x < x1; x++) for (y = y0; y < y1; y++) { - vtot += dsf_grid[x][y].vsum; - nv += dsf_grid[x][y].nval; + vtot += dsf_grid[x][y].sum.v; + nv += dsf_grid[x][y].sum.n; } if (!nv) { fprintf(stderr, "%s: internal - missing samples in create_lobe\n", progname); exit(1); } + if (vtot <= 0) /* only create positive lobes */ + return(0); /* peak value based on integral */ vtot *= (x1-x0)*(y1-y0)*(2.*M_PI/GRIDRES/GRIDRES)/(double)nv; rad = (RSCA/(double)GRIDRES)*(x1-x0); @@ -194,6 +194,7 @@ create_lobe(RBFVAL *rvp, int x0, int x1, int y0, int y rvp->crad = ANG2R(rad); rvp->gx = (x0+x1)>>1; rvp->gy = (y0+y1)>>1; + return(1); } /* Recursive function to build radial basis function representation */ @@ -235,15 +236,18 @@ build_rbfrep(RBFVAL **arp, int *np, int x0, int x1, in return(-1); } /* create lobes for leaves */ - if (!branched[0]) - create_lobe(*arp+(*np)++, x0, xmid, y0, ymid); - if (!branched[1]) - create_lobe(*arp+(*np)++, xmid, x1, y0, ymid); - if (!branched[2]) - create_lobe(*arp+(*np)++, x0, xmid, ymid, y1); - if (!branched[3]) - create_lobe(*arp+(*np)++, xmid, x1, ymid, y1); - nadded += nleaves; + if (!branched[0] && create_lobe(*arp+*np, x0, xmid, y0, ymid)) { + ++(*np); ++nadded; + } + if (!branched[1] && create_lobe(*arp+*np, xmid, x1, y0, ymid)) { + ++(*np); ++nadded; + } + if (!branched[2] && create_lobe(*arp+*np, x0, xmid, ymid, y1)) { + ++(*np); ++nadded; + } + if (!branched[3] && create_lobe(*arp+*np, xmid, x1, ymid, y1)) { + ++(*np); ++nadded; + } return(nadded); }