32 |
|
#define RSCA 2.2 /* radius scaling factor (empirical) */ |
33 |
|
#endif |
34 |
|
#ifndef SMOOTH_MSE |
35 |
< |
#define SMOOTH_MSE 2e-5 /* acceptable mean squared error */ |
35 |
> |
#define SMOOTH_MSE 5e-5 /* acceptable mean squared error */ |
36 |
|
#endif |
37 |
|
#ifndef SMOOTH_MSER |
38 |
|
#define SMOOTH_MSER 0.03 /* acceptable relative MSE */ |
41 |
|
|
42 |
|
#define RBFALLOCB 10 /* RBF allocation block size */ |
43 |
|
|
44 |
< |
/* our loaded grid for this incident angle */ |
44 |
> |
/* our loaded grid or comparison DSFs */ |
45 |
|
GRIDVAL dsf_grid[GRIDRES][GRIDRES]; |
46 |
|
|
47 |
|
/* Start new DSF input grid */ |
72 |
|
ovec[1] = sin((M_PI/180.)*phi_out) * ovec[2]; |
73 |
|
ovec[2] = sqrt(1. - ovec[2]*ovec[2]); |
74 |
|
|
75 |
< |
if (val <= 0) /* truncate to zero */ |
76 |
< |
val = 0; |
77 |
< |
else if (!isDSF) |
75 |
> |
if (!isDSF) |
76 |
|
val *= ovec[2]; /* convert from BSDF to DSF */ |
77 |
|
|
78 |
|
/* update BSDF histogram */ |
81 |
|
|
82 |
|
pos_from_vec(pos, ovec); |
83 |
|
|
84 |
< |
dsf_grid[pos[0]][pos[1]].vsum += val; |
85 |
< |
dsf_grid[pos[0]][pos[1]].nval++; |
84 |
> |
dsf_grid[pos[0]][pos[1]].sum.v += val; |
85 |
> |
dsf_grid[pos[0]][pos[1]].sum.n++; |
86 |
|
} |
87 |
|
|
88 |
|
/* Compute minimum BSDF from histogram (does not clear) */ |
114 |
|
|
115 |
|
for (x = x0; x < x1; x++) |
116 |
|
for (y = y0; y < y1; y++) |
117 |
< |
if (dsf_grid[x][y].nval) |
117 |
> |
if (dsf_grid[x][y].sum.n) |
118 |
|
return(0); |
119 |
|
return(1); |
120 |
|
} |
132 |
|
memset(xvec, 0, sizeof(xvec)); |
133 |
|
for (x = x0; x < x1; x++) |
134 |
|
for (y = y0; y < y1; y++) |
135 |
< |
if ((n = dsf_grid[x][y].nval) > 0) { |
136 |
< |
double z = dsf_grid[x][y].vsum; |
135 |
> |
if ((n = dsf_grid[x][y].sum.n) > 0) { |
136 |
> |
double z = dsf_grid[x][y].sum.v; |
137 |
|
rMtx[0][0] += x*x*(double)n; |
138 |
|
rMtx[0][1] += x*y*(double)n; |
139 |
|
rMtx[0][2] += x*(double)n; |
156 |
|
sqerr = 0.0; /* compute mean squared error */ |
157 |
|
for (x = x0; x < x1; x++) |
158 |
|
for (y = y0; y < y1; y++) |
159 |
< |
if ((n = dsf_grid[x][y].nval) > 0) { |
160 |
< |
double d = A*x + B*y + C - dsf_grid[x][y].vsum/n; |
159 |
> |
if ((n = dsf_grid[x][y].sum.n) > 0) { |
160 |
> |
double d = A*x + B*y + C - dsf_grid[x][y].sum.v/n; |
161 |
|
sqerr += n*d*d; |
162 |
|
} |
163 |
|
if (sqerr <= nvs*SMOOTH_MSE) /* below absolute MSE threshold? */ |
167 |
|
} |
168 |
|
|
169 |
|
/* Create new lobe based on integrated samples in region */ |
170 |
< |
static void |
170 |
> |
static int |
171 |
|
create_lobe(RBFVAL *rvp, int x0, int x1, int y0, int y1) |
172 |
|
{ |
173 |
|
double vtot = 0.0; |
177 |
|
/* compute average for region */ |
178 |
|
for (x = x0; x < x1; x++) |
179 |
|
for (y = y0; y < y1; y++) { |
180 |
< |
vtot += dsf_grid[x][y].vsum; |
181 |
< |
nv += dsf_grid[x][y].nval; |
180 |
> |
vtot += dsf_grid[x][y].sum.v; |
181 |
> |
nv += dsf_grid[x][y].sum.n; |
182 |
|
} |
183 |
|
if (!nv) { |
184 |
|
fprintf(stderr, "%s: internal - missing samples in create_lobe\n", |
185 |
|
progname); |
186 |
|
exit(1); |
187 |
|
} |
188 |
+ |
if (vtot <= 0) /* only create positive lobes */ |
189 |
+ |
return(0); |
190 |
|
/* peak value based on integral */ |
191 |
|
vtot *= (x1-x0)*(y1-y0)*(2.*M_PI/GRIDRES/GRIDRES)/(double)nv; |
192 |
|
rad = (RSCA/(double)GRIDRES)*(x1-x0); |
194 |
|
rvp->crad = ANG2R(rad); |
195 |
|
rvp->gx = (x0+x1)>>1; |
196 |
|
rvp->gy = (y0+y1)>>1; |
197 |
+ |
return(1); |
198 |
|
} |
199 |
|
|
200 |
|
/* Recursive function to build radial basis function representation */ |
236 |
|
return(-1); |
237 |
|
} |
238 |
|
/* create lobes for leaves */ |
239 |
< |
if (!branched[0]) |
240 |
< |
create_lobe(*arp+(*np)++, x0, xmid, y0, ymid); |
241 |
< |
if (!branched[1]) |
242 |
< |
create_lobe(*arp+(*np)++, xmid, x1, y0, ymid); |
243 |
< |
if (!branched[2]) |
244 |
< |
create_lobe(*arp+(*np)++, x0, xmid, ymid, y1); |
245 |
< |
if (!branched[3]) |
246 |
< |
create_lobe(*arp+(*np)++, xmid, x1, ymid, y1); |
247 |
< |
nadded += nleaves; |
239 |
> |
if (!branched[0] && create_lobe(*arp+*np, x0, xmid, y0, ymid)) { |
240 |
> |
++(*np); ++nadded; |
241 |
> |
} |
242 |
> |
if (!branched[1] && create_lobe(*arp+*np, xmid, x1, y0, ymid)) { |
243 |
> |
++(*np); ++nadded; |
244 |
> |
} |
245 |
> |
if (!branched[2] && create_lobe(*arp+*np, x0, xmid, ymid, y1)) { |
246 |
> |
++(*np); ++nadded; |
247 |
> |
} |
248 |
> |
if (!branched[3] && create_lobe(*arp+*np, xmid, x1, ymid, y1)) { |
249 |
> |
++(*np); ++nadded; |
250 |
> |
} |
251 |
|
return(nadded); |
252 |
|
} |
253 |
|
|
262 |
|
comp_bsdf_min(); |
263 |
|
/* create RBF node list */ |
264 |
|
rbfarr = NULL; nn = 0; |
265 |
< |
if (build_rbfrep(&rbfarr, &nn, 0, GRIDRES, 0, GRIDRES) <= 0) |
266 |
< |
goto memerr; |
265 |
> |
if (build_rbfrep(&rbfarr, &nn, 0, GRIDRES, 0, GRIDRES) <= 0) { |
266 |
> |
if (nn) |
267 |
> |
goto memerr; |
268 |
> |
fprintf(stderr, |
269 |
> |
"%s: warning - skipping bad incidence (%.1f,%.1f)\n", |
270 |
> |
progname, theta_in_deg, phi_in_deg); |
271 |
> |
return(NULL); |
272 |
> |
} |
273 |
|
/* (re)allocate RBF array */ |
274 |
|
newnode = (RBFNODE *)realloc(rbfarr, |
275 |
|
sizeof(RBFNODE) + sizeof(RBFVAL)*(nn-1)); |
296 |
|
newnode->vtotal); |
297 |
|
#endif |
298 |
|
insert_dsf(newnode); |
289 |
– |
|
299 |
|
return(newnode); |
300 |
|
memerr: |
301 |
|
fprintf(stderr, "%s: Out of memory in make_rbfrep()\n", progname); |