| 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 |
| 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) ((int)isqrttab[(int)(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) */ |
| 114 |
|
int |
| 115 |
|
kill_occl(h, v, nl, n) /* check for occlusion errors */ |
| 116 |
|
int h, v; |
| 117 |
< |
short nl[NNEIGH][2]; |
| 117 |
> |
register short nl[NNEIGH][2]; |
| 118 |
|
int n; |
| 119 |
|
{ |
| 120 |
|
short forequad[2][2]; |
| 121 |
|
int d; |
| 122 |
< |
register int4 i; |
| 122 |
> |
register int4 i, p; |
| 123 |
|
|
| 124 |
< |
if (n <= 0) |
| 124 |
> |
if (n <= 0) { |
| 125 |
> |
#ifdef DEBUG |
| 126 |
> |
error(WARNING, "neighborless sample in kill_occl"); |
| 127 |
> |
#endif |
| 128 |
|
return(1); |
| 129 |
+ |
} |
| 130 |
+ |
p = v*hres + h; |
| 131 |
|
forequad[0][0] = forequad[0][1] = forequad[1][0] = forequad[1][1] = 0; |
| 132 |
|
for (i = n; i--; ) { |
| 133 |
|
d = (h-nl[i][0])*(h-nl[i][0]) + (v-nl[i][1])*(v-nl[i][1]); |
| 134 |
< |
if (mydepth[nl[i][1]*hres+nl[i][0]] < |
| 135 |
< |
mydepth[v*hres+h]*(1.-DEPS*d)) |
| 134 |
> |
d = isqrt(d); |
| 135 |
> |
if (mydepth[nl[i][1]*hres+nl[i][0]]*(1.+DEPS*d) < mydepth[p]) |
| 136 |
|
forequad[nl[i][0]<h][nl[i][1]<v] = 1; |
| 137 |
|
} |
| 138 |
< |
if (forequad[0][0]+forequad[0][1]+forequad[1][0]+forequad[1][1] > 1) { |
| 139 |
< |
i = v*hres + h; |
| 140 |
< |
setcolor(mypixel[i], 0., 0., 0.); |
| 129 |
< |
myweight[i] = 0.; /* occupancy reset afterwards */ |
| 138 |
> |
if (forequad[0][0]+forequad[0][1]+forequad[1][0]+forequad[1][1] > 2) { |
| 139 |
> |
setcolor(mypixel[p], 0., 0., 0.); |
| 140 |
> |
myweight[p] = 0.; /* occupancy reset afterwards */ |
| 141 |
|
} |
| 142 |
|
return(1); |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
|
| 146 |
|
int |
| 147 |
< |
grow_samp(h, v, nl, n) /* grow sample point appropriately */ |
| 147 |
> |
smooth_samp(h, v, nl, n) /* grow sample point smoothly */ |
| 148 |
|
int h, v; |
| 149 |
|
register short nl[NNEIGH][2]; |
| 150 |
|
int n; |
| 151 |
|
{ |
| 152 |
+ |
int dis[NNEIGH], ndis; |
| 153 |
|
COLOR mykern[MAXRAD2]; |
| 142 |
– |
float mykw[MAXRAD2]; |
| 154 |
|
int4 maxr2; |
| 155 |
< |
double w; |
| 155 |
> |
double d; |
| 156 |
|
register int4 p, r2; |
| 157 |
< |
int maxr, h2, v2; |
| 157 |
> |
int i, r, maxr, h2, v2; |
| 158 |
|
|
| 159 |
|
if (n <= 0) |
| 160 |
|
return(1); |
| 161 |
|
p = v*hres + h; /* build kernel values */ |
| 162 |
|
maxr2 = (h-nl[n-1][0])*(h-nl[n-1][0]) + (v-nl[n-1][1])*(v-nl[n-1][1]); |
| 163 |
|
DCHECK(maxr2>=MAXRAD2, CONSISTENCY, "out of range neighbor"); |
| 164 |
< |
for (r2 = maxr2+1; --r2; ) { |
| 165 |
< |
copycolor(mykern[r2], mypixel[p]); |
| 166 |
< |
mykw[r2] = pixWeight[r2]; |
| 167 |
< |
if (2*r2 >= maxr2) /* soften skirt */ |
| 168 |
< |
mykw[r2] *= (2*(maxr2-r2)+1.0)/maxr2; |
| 169 |
< |
scalecolor(mykern[r2], mykw[r2]); |
| 164 |
> |
maxr = isqrt(maxr2); |
| 165 |
> |
for (v2 = 1; v2 <= maxr; v2++) |
| 166 |
> |
for (h2 = 0; h2 <= v2; h2++) { |
| 167 |
> |
r2 = h2*h2 + v2*v2; |
| 168 |
> |
if (r2 > maxr2) break; |
| 169 |
> |
copycolor(mykern[r2], mypixel[p]); |
| 170 |
> |
scalecolor(mykern[r2], pixWeight[r2]); |
| 171 |
> |
} |
| 172 |
> |
ndis = 0; /* find discontinuities */ |
| 173 |
> |
for (i = n; i--; ) { |
| 174 |
> |
r2 = (h-nl[i][0])*(h-nl[i][0]) + (v-nl[i][1])*(v-nl[i][1]); |
| 175 |
> |
r = isqrt(r2); |
| 176 |
> |
d = mydepth[nl[i][1]*hres+nl[i][0]] / mydepth[p]; |
| 177 |
> |
d = d>=1. ? d-1. : 1.-d; |
| 178 |
> |
if (d > r*DEPS || bigdiff(mypixel[p], |
| 179 |
> |
mypixel[nl[i][1]*hres+nl[i][0]], r*PEPS)) |
| 180 |
> |
dis[ndis++] = i; |
| 181 |
|
} |
| 182 |
< |
maxr = sqrt((double)maxr2) + .99; /* stamp out that kernel */ |
| 182 |
> |
/* stamp out that kernel */ |
| 183 |
|
for (v2 = v-maxr; v2 <= v+maxr; v2++) { |
| 184 |
< |
if (v2 < 0) continue; |
| 185 |
< |
if (v2 >= vres) break; |
| 184 |
> |
if (v2 < 0) v2 = 0; |
| 185 |
> |
else if (v2 >= vres) break; |
| 186 |
|
for (h2 = h-maxr; h2 <= h+maxr; h2++) { |
| 187 |
< |
if (h2 < 0) continue; |
| 188 |
< |
if (h2 >= hres) break; |
| 189 |
< |
r2 = (v2-v)*(v2-v) + (h2-h)*(h2-h); |
| 187 |
> |
if (h2 < 0) h2 = 0; |
| 188 |
> |
else if (h2 >= hres) break; |
| 189 |
> |
r2 = (h2-h)*(h2-h) + (v2-v)*(v2-v); |
| 190 |
|
if (r2 > maxr2) continue; |
| 191 |
|
if (CHK4(pixFlags, v2*hres+h2)) |
| 192 |
|
continue; /* occupied */ |
| 193 |
+ |
for (i = ndis; i--; ) { |
| 194 |
+ |
r = (h2-nl[dis[i]][0])*(h2-nl[dis[i]][0]) + |
| 195 |
+ |
(v2-nl[dis[i]][1])*(v2-nl[dis[i]][1]); |
| 196 |
+ |
if (r < r2) break; |
| 197 |
+ |
} |
| 198 |
+ |
if (i >= 0) continue; /* outside edge */ |
| 199 |
|
addcolor(mypixel[v2*hres+h2], mykern[r2]); |
| 200 |
< |
myweight[v2*hres+h2] += mykw[r2]*myweight[v*hres+h]; |
| 200 |
> |
myweight[v2*hres+h2] += pixWeight[r2] * myweight[p]; |
| 201 |
|
} |
| 202 |
|
} |
| 203 |
|
return(1); |
| 204 |
|
} |
| 205 |
|
|
| 206 |
|
|
| 207 |
< |
pixFlush() /* done with beams -- flush pixel values */ |
| 207 |
> |
int |
| 208 |
> |
random_samp(h, v, nl, n) /* grow sample point noisily */ |
| 209 |
> |
int h, v; |
| 210 |
> |
register short nl[NNEIGH][2]; |
| 211 |
> |
int n; |
| 212 |
|
{ |
| 213 |
+ |
float nwt[NNEIGH]; |
| 214 |
+ |
int4 maxr2; |
| 215 |
+ |
register int4 p, r2; |
| 216 |
+ |
register int i; |
| 217 |
+ |
int maxr, h2, v2; |
| 218 |
+ |
COLOR ctmp; |
| 219 |
+ |
|
| 220 |
+ |
if (n <= 0) |
| 221 |
+ |
return(1); |
| 222 |
+ |
p = v*hres + h; /* compute kernel radius */ |
| 223 |
+ |
maxr2 = (h-nl[n-1][0])*(h-nl[n-1][0]) + (v-nl[n-1][1])*(v-nl[n-1][1]); |
| 224 |
+ |
DCHECK(maxr2>=MAXRAD2, CONSISTENCY, "out of range neighbor"); |
| 225 |
+ |
maxr = isqrt(maxr2); |
| 226 |
+ |
/* compute neighbor weights */ |
| 227 |
+ |
for (i = n; i--; ) { |
| 228 |
+ |
r2 = (nl[i][0]-h)*(nl[i][0]-h) + (nl[i][1]-v)*(nl[i][1]-v); |
| 229 |
+ |
nwt[i] = pixWeight[r2]; |
| 230 |
+ |
} |
| 231 |
+ |
/* sample kernel */ |
| 232 |
+ |
for (v2 = v-maxr; v2 <= v+maxr; v2++) { |
| 233 |
+ |
if (v2 < 0) v2 = 0; |
| 234 |
+ |
else if (v2 >= vres) break; |
| 235 |
+ |
for (h2 = h-maxr; h2 <= h+maxr; h2++) { |
| 236 |
+ |
if (h2 < 0) h2 = 0; |
| 237 |
+ |
else if (h2 >= hres) break; |
| 238 |
+ |
r2 = (h2-h)*(h2-h) + (v2-v)*(v2-v); |
| 239 |
+ |
if (r2 > maxr2) continue; |
| 240 |
+ |
if (CHK4(pixFlags, v2*hres+h2)) |
| 241 |
+ |
continue; /* occupied */ |
| 242 |
+ |
if (frandom() > pixWeight[r2]) { |
| 243 |
+ |
/* pick neighbor instead */ |
| 244 |
+ |
i = random() % n; |
| 245 |
+ |
r2 = nl[i][1]*hres + nl[i][0]; |
| 246 |
+ |
copycolor(ctmp, mypixel[r2]); |
| 247 |
+ |
scalecolor(ctmp, nwt[i]); |
| 248 |
+ |
addcolor(mypixel[v2*hres+h2], ctmp); |
| 249 |
+ |
myweight[v2*hres+h2] += nwt[i] * myweight[r2]; |
| 250 |
+ |
continue; |
| 251 |
+ |
} |
| 252 |
+ |
addcolor(mypixel[v2*hres+h2], mypixel[p]); |
| 253 |
+ |
myweight[v2*hres+h2] += myweight[p]; |
| 254 |
+ |
} |
| 255 |
+ |
} |
| 256 |
+ |
return(1); |
| 257 |
+ |
} |
| 258 |
+ |
|
| 259 |
+ |
|
| 260 |
+ |
pixFinish(ransamp) /* done with beams -- compute pixel values */ |
| 261 |
+ |
int ransamp; |
| 262 |
+ |
{ |
| 263 |
+ |
if (pixWeight[0] <= FTINY) |
| 264 |
+ |
init_wfunc(); /* initialize weighting function */ |
| 265 |
|
reset_flags(); /* set occupancy flags */ |
| 266 |
|
meet_neighbors(kill_occl); /* eliminate occlusion errors */ |
| 267 |
|
reset_flags(); /* reset occupancy flags */ |
| 268 |
< |
if (pixWeight[0] <= FTINY) { /* initialize weighting function */ |
| 269 |
< |
register int r; |
| 270 |
< |
for (r = MAXRAD2; --r; ) |
| 271 |
< |
pixWeight[r] = G0NORM/sqrt((double)r); |
| 188 |
< |
pixWeight[0] = 1.; |
| 189 |
< |
} |
| 190 |
< |
meet_neighbors(grow_samp); /* grow valid samples over image */ |
| 268 |
> |
if (ransamp) /* spread samples over image */ |
| 269 |
> |
meet_neighbors(random_samp); |
| 270 |
> |
else |
| 271 |
> |
meet_neighbors(smooth_samp); |
| 272 |
|
free((char *)pixFlags); /* free pixel flags */ |
| 273 |
|
pixFlags = NULL; |
| 274 |
|
} |
| 276 |
|
|
| 277 |
|
reset_flags() /* allocate/set/reset occupancy flags */ |
| 278 |
|
{ |
| 279 |
< |
register int p; |
| 279 |
> |
register int4 p; |
| 280 |
|
|
| 281 |
|
if (pixFlags == NULL) { |
| 282 |
|
pixFlags = (int4 *)calloc(FL4NELS(hres*vres), sizeof(int4)); |
| 289 |
|
} |
| 290 |
|
|
| 291 |
|
|
| 292 |
+ |
init_wfunc() /* initialize weighting function */ |
| 293 |
+ |
{ |
| 294 |
+ |
register int4 r2; |
| 295 |
+ |
register double d; |
| 296 |
+ |
|
| 297 |
+ |
for (r2 = MAXRAD2; --r2; ) { |
| 298 |
+ |
d = sqrt((double)r2); |
| 299 |
+ |
pixWeight[r2] = G0NORM/d; |
| 300 |
+ |
isqrttab[r2] = d + 0.99; |
| 301 |
+ |
} |
| 302 |
+ |
pixWeight[0] = 1.; |
| 303 |
+ |
isqrttab[0] = 0; |
| 304 |
+ |
} |
| 305 |
+ |
|
| 306 |
+ |
|
| 307 |
|
int |
| 308 |
|
findneigh(nl, h, v, rnl) /* find NNEIGH neighbors for pixel */ |
| 309 |
|
short nl[NNEIGH][2]; |
| 311 |
|
register short (*rnl)[NNEIGH]; |
| 312 |
|
{ |
| 313 |
|
int nn = 0; |
| 314 |
< |
int4 d, ld, nd[NNEIGH+1]; |
| 314 |
> |
int4 d, nd[NNEIGH]; |
| 315 |
|
int n, hoff; |
| 316 |
|
register int h2, n2; |
| 317 |
|
|
| 318 |
< |
ld = MAXRAD2; |
| 318 |
> |
nd[NNEIGH-1] = MAXRAD2; |
| 319 |
|
for (hoff = 1; hoff < hres; hoff = (hoff<0) - hoff) { |
| 320 |
|
h2 = h + hoff; |
| 321 |
|
if (h2 < 0 | h2 >= hres) |
| 322 |
|
continue; |
| 323 |
< |
if ((h2-h)*(h2-h) >= ld) |
| 323 |
> |
if ((h2-h)*(h2-h) >= nd[NNEIGH-1]) |
| 324 |
|
break; |
| 325 |
|
for (n = 0; n < NNEIGH && rnl[h2][n] < NINF; n++) { |
| 326 |
|
d = (h2-h)*(h2-h) + (v-rnl[h2][n])*(v-rnl[h2][n]); |
| 327 |
< |
if (d >= ld) |
| 327 |
> |
if (d >= nd[NNEIGH-1]) |
| 328 |
|
continue; |
| 329 |
< |
for (n2 = nn; ; n2--) { /* insert neighbor */ |
| 329 |
> |
if (nn < NNEIGH) /* insert neighbor */ |
| 330 |
> |
nn++; |
| 331 |
> |
for (n2 = nn; n2--; ) { |
| 332 |
|
if (!n2 || d >= nd[n2-1]) { |
| 333 |
|
nd[n2] = d; |
| 334 |
|
nl[n2][0] = h2; |
| 339 |
|
nl[n2][0] = nl[n2-1][0]; |
| 340 |
|
nl[n2][1] = nl[n2-1][1]; |
| 341 |
|
} |
| 244 |
– |
if (nn < NNEIGH) |
| 245 |
– |
nn++; |
| 246 |
– |
else |
| 247 |
– |
ld = nd[NNEIGH-1]; |
| 342 |
|
} |
| 343 |
|
} |
| 344 |
|
return(nn); |