10 |
|
|
11 |
|
#include "holo.h" |
12 |
|
#include "view.h" |
13 |
+ |
#include "random.h" |
14 |
|
|
15 |
|
#ifndef DEPS |
16 |
< |
#define DEPS 0.01 /* depth epsilon */ |
16 |
> |
#define DEPS 0.02 /* depth epsilon */ |
17 |
|
#endif |
18 |
+ |
#ifndef PEPS |
19 |
+ |
#define PEPS 0.04 /* pixel value epsilon */ |
20 |
+ |
#endif |
21 |
|
#ifndef MAXRAD |
22 |
|
#define MAXRAD 64 /* maximum kernel radius */ |
23 |
|
#endif |
24 |
|
#ifndef NNEIGH |
25 |
< |
#define NNEIGH 7 /* find this many neighbors */ |
25 |
> |
#define NNEIGH 5 /* find this many neighbors */ |
26 |
|
#endif |
27 |
|
|
28 |
|
#define NINF 16382 |
43 |
|
|
44 |
|
static int4 *pixFlags; /* pixel occupancy flags */ |
45 |
|
static float pixWeight[MAXRAD2]; /* pixel weighting function */ |
46 |
+ |
static short isqrttab[MAXRAD2]; /* integer square root table */ |
47 |
|
|
48 |
+ |
#define isqrt(i2) (isqrttab[i2]) |
49 |
+ |
|
50 |
|
extern VIEW myview; /* current output view */ |
51 |
|
extern COLOR *mypixel; /* pixels being rendered */ |
52 |
|
extern float *myweight; /* weights (used to compute final pixels) */ |
112 |
|
|
113 |
|
|
114 |
|
int |
115 |
< |
kill_occl(h, v, nl, n) /* check for occlusion errors */ |
115 |
> |
kill_occl(h, v, nl, nd, n) /* check for occlusion errors */ |
116 |
|
int h, v; |
117 |
< |
short nl[NNEIGH][2]; |
117 |
> |
register short nl[NNEIGH][2]; |
118 |
> |
int nd[NNEIGH]; |
119 |
|
int n; |
120 |
|
{ |
121 |
|
short forequad[2][2]; |
122 |
|
int d; |
123 |
< |
register int4 i; |
123 |
> |
register int i; |
124 |
> |
register int4 p; |
125 |
|
|
126 |
< |
if (n <= 0) |
126 |
> |
if (n <= 0) { |
127 |
> |
#ifdef DEBUG |
128 |
> |
error(WARNING, "neighborless sample in kill_occl"); |
129 |
> |
#endif |
130 |
|
return(1); |
131 |
+ |
} |
132 |
+ |
p = v*hres + h; |
133 |
|
forequad[0][0] = forequad[0][1] = forequad[1][0] = forequad[1][1] = 0; |
134 |
|
for (i = n; i--; ) { |
135 |
< |
d = (h-nl[i][0])*(h-nl[i][0]) + (v-nl[i][1])*(v-nl[i][1]); |
136 |
< |
if (mydepth[nl[i][1]*hres+nl[i][0]] < |
123 |
< |
mydepth[v*hres+h]*(1.-DEPS*d)) |
135 |
> |
d = isqrt(nd[i]); |
136 |
> |
if (mydepth[nl[i][1]*hres+nl[i][0]]*(1.+DEPS*d) < mydepth[p]) |
137 |
|
forequad[nl[i][0]<h][nl[i][1]<v] = 1; |
138 |
|
} |
139 |
< |
if (forequad[0][0]+forequad[0][1]+forequad[1][0]+forequad[1][1] > 1) { |
140 |
< |
i = v*hres + h; |
141 |
< |
setcolor(mypixel[i], 0., 0., 0.); |
129 |
< |
myweight[i] = 0.; /* occupancy reset afterwards */ |
139 |
> |
if (forequad[0][0]+forequad[0][1]+forequad[1][0]+forequad[1][1] > 2) { |
140 |
> |
setcolor(mypixel[p], 0., 0., 0.); |
141 |
> |
myweight[p] = 0.; /* occupancy reset afterwards */ |
142 |
|
} |
143 |
|
return(1); |
144 |
|
} |
145 |
|
|
146 |
|
|
147 |
|
int |
148 |
< |
grow_samp(h, v, nl, n) /* grow sample point appropriately */ |
148 |
> |
smooth_samp(h, v, nl, nd, n) /* grow sample point smoothly */ |
149 |
|
int h, v; |
150 |
|
register short nl[NNEIGH][2]; |
151 |
+ |
int nd[NNEIGH]; |
152 |
|
int n; |
153 |
|
{ |
154 |
+ |
int dis[NNEIGH], ndis; |
155 |
|
COLOR mykern[MAXRAD2]; |
156 |
< |
float mykw[MAXRAD2]; |
157 |
< |
int4 maxr2; |
158 |
< |
double w; |
159 |
< |
register int4 p, r2; |
160 |
< |
int maxr, h2, v2; |
156 |
> |
int maxr2; |
157 |
> |
double d; |
158 |
> |
register int4 p; |
159 |
> |
register int r2; |
160 |
> |
int i, r, maxr, h2, v2; |
161 |
|
|
162 |
|
if (n <= 0) |
163 |
|
return(1); |
164 |
|
p = v*hres + h; /* build kernel values */ |
165 |
< |
maxr2 = (h-nl[n-1][0])*(h-nl[n-1][0]) + (v-nl[n-1][1])*(v-nl[n-1][1]); |
165 |
> |
maxr2 = nd[n-1]; |
166 |
|
DCHECK(maxr2>=MAXRAD2, CONSISTENCY, "out of range neighbor"); |
167 |
< |
for (r2 = maxr2+1; --r2; ) { |
168 |
< |
copycolor(mykern[r2], mypixel[p]); |
169 |
< |
mykw[r2] = pixWeight[r2]; |
170 |
< |
if (2*r2 >= maxr2) /* soften skirt */ |
171 |
< |
mykw[r2] *= (2*(maxr2-r2)+1.0)/maxr2; |
172 |
< |
scalecolor(mykern[r2], mykw[r2]); |
167 |
> |
maxr = isqrt(maxr2); |
168 |
> |
for (v2 = 1; v2 <= maxr; v2++) |
169 |
> |
for (h2 = 0; h2 <= v2; h2++) { |
170 |
> |
r2 = h2*h2 + v2*v2; |
171 |
> |
if (r2 > maxr2) break; |
172 |
> |
copycolor(mykern[r2], mypixel[p]); |
173 |
> |
scalecolor(mykern[r2], pixWeight[r2]); |
174 |
> |
} |
175 |
> |
ndis = 0; /* find discontinuities */ |
176 |
> |
for (i = n; i--; ) { |
177 |
> |
r = isqrt(nd[i]); |
178 |
> |
d = mydepth[nl[i][1]*hres+nl[i][0]] / mydepth[p]; |
179 |
> |
d = d>=1. ? d-1. : 1.-d; |
180 |
> |
if (d > r*DEPS || bigdiff(mypixel[p], |
181 |
> |
mypixel[nl[i][1]*hres+nl[i][0]], r*PEPS)) |
182 |
> |
dis[ndis++] = i; |
183 |
|
} |
184 |
< |
maxr = sqrt((double)maxr2) + .99; /* stamp out that kernel */ |
184 |
> |
/* stamp out that kernel */ |
185 |
|
for (v2 = v-maxr; v2 <= v+maxr; v2++) { |
186 |
< |
if (v2 < 0) continue; |
187 |
< |
if (v2 >= vres) break; |
186 |
> |
if (v2 < 0) v2 = 0; |
187 |
> |
else if (v2 >= vres) break; |
188 |
|
for (h2 = h-maxr; h2 <= h+maxr; h2++) { |
189 |
< |
if (h2 < 0) continue; |
190 |
< |
if (h2 >= hres) break; |
191 |
< |
r2 = (v2-v)*(v2-v) + (h2-h)*(h2-h); |
189 |
> |
if (h2 < 0) h2 = 0; |
190 |
> |
else if (h2 >= hres) break; |
191 |
> |
r2 = (h2-h)*(h2-h) + (v2-v)*(v2-v); |
192 |
|
if (r2 > maxr2) continue; |
193 |
|
if (CHK4(pixFlags, v2*hres+h2)) |
194 |
|
continue; /* occupied */ |
195 |
+ |
for (i = ndis; i--; ) { |
196 |
+ |
r = (h2-nl[dis[i]][0])*(h2-nl[dis[i]][0]) + |
197 |
+ |
(v2-nl[dis[i]][1])*(v2-nl[dis[i]][1]); |
198 |
+ |
if (r < r2) break; |
199 |
+ |
} |
200 |
+ |
if (i >= 0) continue; /* outside edge */ |
201 |
|
addcolor(mypixel[v2*hres+h2], mykern[r2]); |
202 |
< |
myweight[v2*hres+h2] += mykw[r2]*myweight[v*hres+h]; |
202 |
> |
myweight[v2*hres+h2] += pixWeight[r2] * myweight[p]; |
203 |
|
} |
204 |
|
} |
205 |
|
return(1); |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
< |
pixFlush() /* done with beams -- flush pixel values */ |
209 |
> |
int |
210 |
> |
random_samp(h, v, nl, nd, n, rf) /* gather samples randomly */ |
211 |
> |
int h, v; |
212 |
> |
register short nl[NNEIGH][2]; |
213 |
> |
int nd[NNEIGH]; |
214 |
> |
int n; |
215 |
> |
double *rf; |
216 |
|
{ |
217 |
+ |
float rnt[NNEIGH]; |
218 |
+ |
double rvar; |
219 |
+ |
register int4 p, pn; |
220 |
+ |
register int ni; |
221 |
+ |
|
222 |
+ |
if (n <= 0) |
223 |
+ |
return(1); |
224 |
+ |
p = v*hres + h; |
225 |
+ |
if (*rf <= FTINY) /* straight Voronoi regions */ |
226 |
+ |
ni = 0; |
227 |
+ |
else { /* weighted choice */ |
228 |
+ |
DCHECK(nd[n-1]>=MAXRAD2, CONSISTENCY, "out of range neighbor"); |
229 |
+ |
rnt[0] = pixWeight[nd[0]]; |
230 |
+ |
for (ni = 1; ni < n; ni++) |
231 |
+ |
rnt[ni] = rnt[ni-1] + pixWeight[nd[ni]]; |
232 |
+ |
rvar = rnt[n-1]*pow(frandom(), 1. / *rf); |
233 |
+ |
for (ni = 0; rvar > rnt[ni]+FTINY; ni++) |
234 |
+ |
; |
235 |
+ |
} |
236 |
+ |
pn = nl[ni][1]*hres + nl[ni][0]; |
237 |
+ |
addcolor(mypixel[p], mypixel[pn]); |
238 |
+ |
myweight[p] += myweight[pn]; |
239 |
+ |
return(1); |
240 |
+ |
} |
241 |
+ |
|
242 |
+ |
|
243 |
+ |
pixFinish(ransamp) /* done with beams -- compute pixel values */ |
244 |
+ |
double ransamp; |
245 |
+ |
{ |
246 |
+ |
if (pixWeight[0] <= FTINY) |
247 |
+ |
init_wfunc(); /* initialize weighting function */ |
248 |
|
reset_flags(); /* set occupancy flags */ |
249 |
< |
meet_neighbors(kill_occl); /* eliminate occlusion errors */ |
249 |
> |
meet_neighbors(1,kill_occl,NULL); /* identify occlusion errors */ |
250 |
|
reset_flags(); /* reset occupancy flags */ |
251 |
< |
if (pixWeight[0] <= FTINY) { /* initialize weighting function */ |
252 |
< |
register int r; |
253 |
< |
for (r = MAXRAD2; --r; ) |
254 |
< |
pixWeight[r] = G0NORM/sqrt((double)r); |
188 |
< |
pixWeight[0] = 1.; |
189 |
< |
} |
190 |
< |
meet_neighbors(grow_samp); /* grow valid samples over image */ |
251 |
> |
if (ransamp >= 0.) /* spread samples over image */ |
252 |
> |
meet_neighbors(0,random_samp,&ransamp); |
253 |
> |
else |
254 |
> |
meet_neighbors(1,smooth_samp,NULL); |
255 |
|
free((char *)pixFlags); /* free pixel flags */ |
256 |
|
pixFlags = NULL; |
257 |
|
} |
259 |
|
|
260 |
|
reset_flags() /* allocate/set/reset occupancy flags */ |
261 |
|
{ |
262 |
< |
register int p; |
262 |
> |
register int4 p; |
263 |
|
|
264 |
|
if (pixFlags == NULL) { |
265 |
|
pixFlags = (int4 *)calloc(FL4NELS(hres*vres), sizeof(int4)); |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
+ |
init_wfunc() /* initialize weighting function */ |
276 |
+ |
{ |
277 |
+ |
register int r2; |
278 |
+ |
register double d; |
279 |
+ |
|
280 |
+ |
for (r2 = MAXRAD2; --r2; ) { |
281 |
+ |
d = sqrt((double)r2); |
282 |
+ |
pixWeight[r2] = G0NORM/d; |
283 |
+ |
isqrttab[r2] = d + 0.99; |
284 |
+ |
} |
285 |
+ |
pixWeight[0] = 1.; |
286 |
+ |
isqrttab[0] = 0; |
287 |
+ |
} |
288 |
+ |
|
289 |
+ |
|
290 |
|
int |
291 |
< |
findneigh(nl, h, v, rnl) /* find NNEIGH neighbors for pixel */ |
291 |
> |
findneigh(nl, nd, h, v, rnl) /* find NNEIGH neighbors for pixel */ |
292 |
|
short nl[NNEIGH][2]; |
293 |
+ |
int nd[NNEIGH]; |
294 |
|
int h, v; |
295 |
|
register short (*rnl)[NNEIGH]; |
296 |
|
{ |
297 |
|
int nn = 0; |
298 |
< |
int4 d, ld, nd[NNEIGH+1]; |
219 |
< |
int n, hoff; |
298 |
> |
int d, n, hoff; |
299 |
|
register int h2, n2; |
300 |
|
|
301 |
< |
ld = MAXRAD2; |
302 |
< |
for (hoff = 1; hoff < hres; hoff = (hoff<0) - hoff) { |
301 |
> |
nd[NNEIGH-1] = MAXRAD2; |
302 |
> |
for (hoff = 0; hoff < hres; hoff = (hoff<=0) - hoff) { |
303 |
|
h2 = h + hoff; |
304 |
|
if (h2 < 0 | h2 >= hres) |
305 |
|
continue; |
306 |
< |
if ((h2-h)*(h2-h) >= ld) |
306 |
> |
if ((h2-h)*(h2-h) >= nd[NNEIGH-1]) |
307 |
|
break; |
308 |
|
for (n = 0; n < NNEIGH && rnl[h2][n] < NINF; n++) { |
309 |
|
d = (h2-h)*(h2-h) + (v-rnl[h2][n])*(v-rnl[h2][n]); |
310 |
< |
if (d >= ld) |
310 |
> |
if (d == 0 | d >= nd[NNEIGH-1]) |
311 |
|
continue; |
312 |
< |
for (n2 = nn; ; n2--) { /* insert neighbor */ |
312 |
> |
if (nn < NNEIGH) /* insert neighbor */ |
313 |
> |
nn++; |
314 |
> |
for (n2 = nn; n2--; ) { |
315 |
|
if (!n2 || d >= nd[n2-1]) { |
316 |
|
nd[n2] = d; |
317 |
|
nl[n2][0] = h2; |
322 |
|
nl[n2][0] = nl[n2-1][0]; |
323 |
|
nl[n2][1] = nl[n2-1][1]; |
324 |
|
} |
244 |
– |
if (nn < NNEIGH) |
245 |
– |
nn++; |
246 |
– |
else |
247 |
– |
ld = nd[NNEIGH-1]; |
325 |
|
} |
326 |
|
} |
327 |
|
return(nn); |
328 |
|
} |
329 |
|
|
330 |
|
|
331 |
< |
meet_neighbors(nf) /* run through samples and their neighbors */ |
331 |
> |
meet_neighbors(occ, nf, dp) /* run through samples and their neighbors */ |
332 |
> |
int occ; |
333 |
|
int (*nf)(); |
334 |
+ |
char *dp; |
335 |
|
{ |
336 |
|
short ln[NNEIGH][2]; |
337 |
+ |
int nd[NNEIGH]; |
338 |
|
int h, v, n, v2; |
339 |
|
register short (*rnl)[NNEIGH]; |
340 |
|
/* initialize bottom row list */ |
353 |
|
v = 0; /* do each row */ |
354 |
|
for ( ; ; ) { |
355 |
|
for (h = 0; h < hres; h++) { |
356 |
< |
if (!CHK4(pixFlags, v*hres+h)) |
357 |
< |
continue; /* no one home */ |
358 |
< |
n = findneigh(ln, h, v, rnl); |
359 |
< |
(*nf)(h, v, ln, n); /* call on neighbors */ |
356 |
> |
if (!CHK4(pixFlags, v*hres+h) != !occ) |
357 |
> |
continue; /* occupancy mismatch */ |
358 |
> |
/* find neighbors */ |
359 |
> |
n = findneigh(ln, nd, h, v, rnl); |
360 |
> |
/* call on neighbors */ |
361 |
> |
(*nf)(h, v, ln, nd, n, dp); |
362 |
|
} |
363 |
|
if (++v >= vres) /* reinitialize row list */ |
364 |
|
break; |