7 |
|
* G. Ward |
8 |
|
*/ |
9 |
|
|
10 |
+ |
/**************************************************************** |
11 |
+ |
1) Collect samples into a grid using the Shirley-Chiu |
12 |
+ |
angular mapping from a hemisphere to a square. |
13 |
+ |
|
14 |
+ |
2) Compute an adaptive quadtree by subdividing the grid so that |
15 |
+ |
each leaf node has at least one sample up to as many |
16 |
+ |
samples as fit nicely on a plane to within a certain |
17 |
+ |
MSE tolerance. |
18 |
+ |
|
19 |
+ |
3) Place one Gaussian lobe at each leaf node in the quadtree, |
20 |
+ |
sizing it to have a radius equal to the leaf size and |
21 |
+ |
a volume equal to the energy in that node. |
22 |
+ |
*****************************************************************/ |
23 |
+ |
|
24 |
|
#define _USE_MATH_DEFINES |
25 |
|
#include <stdio.h> |
26 |
|
#include <stdlib.h> |
35 |
|
#define SMOOTH_MSE 5e-5 /* acceptable mean squared error */ |
36 |
|
#endif |
37 |
|
#ifndef SMOOTH_MSER |
38 |
< |
#define SMOOTH_MSER 0.07 /* acceptable relative MSE */ |
38 |
> |
#define SMOOTH_MSER 0.03 /* acceptable relative MSE */ |
39 |
|
#endif |
40 |
|
#define MAX_RAD (GRIDRES/8) /* maximum lobe radius */ |
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 */ |
83 |
|
|
84 |
|
pos_from_vec(pos, ovec); |
85 |
|
|
86 |
< |
dsf_grid[pos[0]][pos[1]].vsum += val; |
87 |
< |
dsf_grid[pos[0]][pos[1]].nval++; |
86 |
> |
dsf_grid[pos[0]][pos[1]].sum.v += val; |
87 |
> |
dsf_grid[pos[0]][pos[1]].sum.n++; |
88 |
|
} |
89 |
|
|
90 |
|
/* Compute minimum BSDF from histogram (does not clear) */ |
91 |
|
static void |
92 |
|
comp_bsdf_min() |
93 |
|
{ |
94 |
< |
int cnt; |
95 |
< |
int i, target; |
94 |
> |
unsigned long cnt, target; |
95 |
> |
int i; |
96 |
|
|
97 |
|
cnt = 0; |
98 |
|
for (i = HISTLEN; i--; ) |
116 |
|
|
117 |
|
for (x = x0; x < x1; x++) |
118 |
|
for (y = y0; y < y1; y++) |
119 |
< |
if (dsf_grid[x][y].nval) |
119 |
> |
if (dsf_grid[x][y].sum.n) |
120 |
|
return(0); |
121 |
|
return(1); |
122 |
|
} |
134 |
|
memset(xvec, 0, sizeof(xvec)); |
135 |
|
for (x = x0; x < x1; x++) |
136 |
|
for (y = y0; y < y1; y++) |
137 |
< |
if ((n = dsf_grid[x][y].nval) > 0) { |
138 |
< |
double z = dsf_grid[x][y].vsum; |
139 |
< |
rMtx[0][0] += n*x*x; |
140 |
< |
rMtx[0][1] += n*x*y; |
141 |
< |
rMtx[0][2] += n*x; |
142 |
< |
rMtx[1][1] += n*y*y; |
143 |
< |
rMtx[1][2] += n*y; |
144 |
< |
rMtx[2][2] += n; |
137 |
> |
if ((n = dsf_grid[x][y].sum.n) > 0) { |
138 |
> |
double z = dsf_grid[x][y].sum.v; |
139 |
> |
rMtx[0][0] += x*x*(double)n; |
140 |
> |
rMtx[0][1] += x*y*(double)n; |
141 |
> |
rMtx[0][2] += x*(double)n; |
142 |
> |
rMtx[1][1] += y*y*(double)n; |
143 |
> |
rMtx[1][2] += y*(double)n; |
144 |
> |
rMtx[2][2] += (double)n; |
145 |
|
xvec[0] += x*z; |
146 |
|
xvec[1] += y*z; |
147 |
|
xvec[2] += z; |
148 |
|
} |
149 |
|
rMtx[1][0] = rMtx[0][1]; |
150 |
+ |
rMtx[2][0] = rMtx[0][2]; |
151 |
|
rMtx[2][1] = rMtx[1][2]; |
152 |
|
nvs = rMtx[2][2]; |
153 |
|
if (SDinvXform(rMtx, rMtx) != SDEnone) |
154 |
< |
return(0); |
154 |
> |
return(1); /* colinear values */ |
155 |
|
A = DOT(rMtx[0], xvec); |
156 |
|
B = DOT(rMtx[1], xvec); |
157 |
|
C = DOT(rMtx[2], xvec); |
158 |
|
sqerr = 0.0; /* compute mean squared error */ |
159 |
|
for (x = x0; x < x1; x++) |
160 |
|
for (y = y0; y < y1; y++) |
161 |
< |
if ((n = dsf_grid[x][y].nval) > 0) { |
162 |
< |
double d = A*x + B*y + C - dsf_grid[x][y].vsum/n; |
161 |
> |
if ((n = dsf_grid[x][y].sum.n) > 0) { |
162 |
> |
double d = A*x + B*y + C - dsf_grid[x][y].sum.v/n; |
163 |
|
sqerr += n*d*d; |
164 |
|
} |
165 |
|
if (sqerr <= nvs*SMOOTH_MSE) /* below absolute MSE threshold? */ |
166 |
|
return(1); |
167 |
< |
/* below relative MSE threshold? */ |
167 |
> |
/* OR below relative MSE threshold? */ |
168 |
|
return(sqerr*nvs <= xvec[2]*xvec[2]*SMOOTH_MSER); |
169 |
|
} |
170 |
|
|
179 |
|
/* compute average for region */ |
180 |
|
for (x = x0; x < x1; x++) |
181 |
|
for (y = y0; y < y1; y++) { |
182 |
< |
vtot += dsf_grid[x][y].vsum; |
183 |
< |
nv += dsf_grid[x][y].nval; |
182 |
> |
vtot += dsf_grid[x][y].sum.v; |
183 |
> |
nv += dsf_grid[x][y].sum.n; |
184 |
|
} |
185 |
|
if (!nv) { |
186 |
|
fprintf(stderr, "%s: internal - missing samples in create_lobe\n", |
224 |
|
if (!nleaves) /* nothing but branches? */ |
225 |
|
return(nadded); |
226 |
|
/* combine 4 leaves into 1? */ |
227 |
< |
if (nleaves == 4 && x1-x0 < MAX_RAD && smooth_region(x0, x1, y0, y1)) |
227 |
> |
if ((nleaves == 4) & (x1-x0 <= MAX_RAD) && |
228 |
> |
smooth_region(x0, x1, y0, y1)) |
229 |
|
return(0); |
230 |
|
/* need more array space? */ |
231 |
|
if ((*np+nleaves-1)>>RBFALLOCB != (*np-1)>>RBFALLOCB) { |