1 |
– |
/* Copyright (c) 1999 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Rendering routines for rhpict. |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include <string.h> |
9 |
+ |
|
10 |
|
#include "holo.h" |
11 |
|
#include "view.h" |
12 |
|
#include "random.h" |
21 |
|
#define MAXRAD 64 /* maximum kernel radius */ |
22 |
|
#endif |
23 |
|
#ifndef NNEIGH |
24 |
< |
#define NNEIGH 7 /* find this many neighbors */ |
24 |
> |
#define NNEIGH 5 /* find this many neighbors */ |
25 |
|
#endif |
26 |
|
|
27 |
|
#define NINF 16382 |
37 |
|
#define CLR4(f,i) FL4OP(f,i,&=~) |
38 |
|
#define TGL4(f,i) FL4OP(f,i,^=) |
39 |
|
#define FL4NELS(n) (((n)+0x1f)>>5) |
40 |
< |
#define CLR4ALL(f,n) bzero((char *)(f),FL4NELS(n)*sizeof(int4)) |
40 |
> |
#define CLR4ALL(f,n) memset((char *)(f),'\0',FL4NELS(n)*sizeof(int32)) |
41 |
|
#endif |
42 |
|
|
43 |
< |
static int4 *pixFlags; /* pixel occupancy flags */ |
43 |
> |
static int32 *pixFlags; /* pixel occupancy flags */ |
44 |
|
static float pixWeight[MAXRAD2]; /* pixel weighting function */ |
45 |
|
static short isqrttab[MAXRAD2]; /* integer square root table */ |
46 |
|
|
47 |
< |
#define isqrt(i2) ((int)isqrttab[(int)(i2)]) |
47 |
> |
#define isqrt(i2) (isqrttab[i2]) |
48 |
|
|
49 |
|
extern VIEW myview; /* current output view */ |
50 |
|
extern COLOR *mypixel; /* pixels being rendered */ |
63 |
|
double d, prox; |
64 |
|
COLOR col; |
65 |
|
int n; |
66 |
< |
register int4 p; |
66 |
> |
register int32 p; |
67 |
|
|
68 |
|
if (!hdbcoord(gc, hb->h, hb->b)) |
69 |
|
error(CONSISTENCY, "bad beam in render_beam"); |
111 |
|
|
112 |
|
|
113 |
|
int |
114 |
< |
kill_occl(h, v, nl, n) /* check for occlusion errors */ |
114 |
> |
kill_occl(h, v, nl, nd, n) /* check for occlusion errors */ |
115 |
|
int h, v; |
116 |
|
register short nl[NNEIGH][2]; |
117 |
+ |
int nd[NNEIGH]; |
118 |
|
int n; |
119 |
|
{ |
120 |
|
short forequad[2][2]; |
121 |
|
int d; |
122 |
< |
register int4 i, p; |
122 |
> |
register int i; |
123 |
> |
register int32 p; |
124 |
|
|
125 |
|
if (n <= 0) { |
126 |
|
#ifdef DEBUG |
131 |
|
p = v*hres + h; |
132 |
|
forequad[0][0] = forequad[0][1] = forequad[1][0] = forequad[1][1] = 0; |
133 |
|
for (i = n; i--; ) { |
134 |
< |
d = (h-nl[i][0])*(h-nl[i][0]) + (v-nl[i][1])*(v-nl[i][1]); |
134 |
< |
d = isqrt(d); |
134 |
> |
d = isqrt(nd[i]); |
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 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
int |
147 |
< |
smooth_samp(h, v, nl, n) /* grow sample point smoothly */ |
147 |
> |
smooth_samp(h, v, nl, nd, n) /* grow sample point smoothly */ |
148 |
|
int h, v; |
149 |
|
register short nl[NNEIGH][2]; |
150 |
+ |
int nd[NNEIGH]; |
151 |
|
int n; |
152 |
|
{ |
153 |
|
int dis[NNEIGH], ndis; |
154 |
|
COLOR mykern[MAXRAD2]; |
155 |
< |
int4 maxr2; |
155 |
> |
int maxr2; |
156 |
|
double d; |
157 |
< |
register int4 p, r2; |
157 |
> |
register int32 p; |
158 |
> |
register int r2; |
159 |
|
int i, r, maxr, h2, v2; |
160 |
|
|
161 |
|
if (n <= 0) |
162 |
|
return(1); |
163 |
|
p = v*hres + h; /* build kernel values */ |
164 |
< |
maxr2 = (h-nl[n-1][0])*(h-nl[n-1][0]) + (v-nl[n-1][1])*(v-nl[n-1][1]); |
164 |
> |
maxr2 = nd[n-1]; |
165 |
|
DCHECK(maxr2>=MAXRAD2, CONSISTENCY, "out of range neighbor"); |
166 |
|
maxr = isqrt(maxr2); |
167 |
|
for (v2 = 1; v2 <= maxr; v2++) |
173 |
|
} |
174 |
|
ndis = 0; /* find discontinuities */ |
175 |
|
for (i = n; i--; ) { |
176 |
< |
r2 = (h-nl[i][0])*(h-nl[i][0]) + (v-nl[i][1])*(v-nl[i][1]); |
175 |
< |
r = isqrt(r2); |
176 |
> |
r = isqrt(nd[i]); |
177 |
|
d = mydepth[nl[i][1]*hres+nl[i][0]] / mydepth[p]; |
178 |
|
d = d>=1. ? d-1. : 1.-d; |
179 |
|
if (d > r*DEPS || bigdiff(mypixel[p], |
206 |
|
|
207 |
|
|
208 |
|
int |
209 |
< |
random_samp(h, v, nl, n, rf) /* grow sample point noisily */ |
209 |
> |
random_samp(h, v, nl, nd, n, rf) /* gather samples randomly */ |
210 |
|
int h, v; |
211 |
|
register short nl[NNEIGH][2]; |
212 |
+ |
int nd[NNEIGH]; |
213 |
|
int n; |
214 |
|
double *rf; |
215 |
|
{ |
216 |
< |
double tv = *rf * (1.-1./NNEIGH); |
217 |
< |
int4 maxr2; |
218 |
< |
register int4 p, r2; |
219 |
< |
register int i; |
218 |
< |
int maxr, h2, v2; |
219 |
< |
COLOR ctmp; |
216 |
> |
float rnt[NNEIGH]; |
217 |
> |
double rvar; |
218 |
> |
register int32 p, pn; |
219 |
> |
register int ni; |
220 |
|
|
221 |
|
if (n <= 0) |
222 |
|
return(1); |
223 |
< |
p = v*hres + h; /* compute kernel radius */ |
224 |
< |
maxr2 = (h-nl[n-1][0])*(h-nl[n-1][0]) + (v-nl[n-1][1])*(v-nl[n-1][1]); |
225 |
< |
DCHECK(maxr2>=MAXRAD2, CONSISTENCY, "out of range neighbor"); |
226 |
< |
maxr = isqrt(maxr2); |
227 |
< |
/* sample kernel */ |
228 |
< |
for (v2 = v-maxr; v2 <= v+maxr; v2++) { |
229 |
< |
if (v2 < 0) v2 = 0; |
230 |
< |
else if (v2 >= vres) break; |
231 |
< |
for (h2 = h-maxr; h2 <= h+maxr; h2++) { |
232 |
< |
if (h2 < 0) h2 = 0; |
233 |
< |
else if (h2 >= hres) break; |
234 |
< |
r2 = (h2-h)*(h2-h) + (v2-v)*(v2-v); |
235 |
< |
if (r2 > maxr2) continue; |
236 |
< |
if (CHK4(pixFlags, v2*hres+h2)) |
237 |
< |
continue; /* occupied */ |
238 |
< |
if (frandom() < tv) { /* use neighbor instead? */ |
239 |
< |
i = random() % n; |
240 |
< |
r2 = nl[i][1]*hres + nl[i][0]; |
241 |
< |
copycolor(ctmp, mypixel[r2]); |
242 |
< |
r2 = (h2-nl[i][0])*(h2-nl[i][0]) + |
243 |
< |
(v2-nl[i][1])*(v2-nl[i][1]); |
244 |
< |
if (r2 < MAXRAD2) { |
245 |
< |
scalecolor(ctmp, pixWeight[r2]); |
246 |
< |
addcolor(mypixel[v2*hres+h2], ctmp); |
247 |
< |
myweight[v2*hres+h2] += pixWeight[r2] * |
248 |
< |
myweight[nl[i][1]*hres+nl[i][0]]; |
249 |
< |
continue; |
250 |
< |
} |
251 |
< |
} |
252 |
< |
/* use central sample */ |
253 |
< |
copycolor(ctmp, mypixel[p]); |
254 |
< |
scalecolor(ctmp, pixWeight[r2]); |
255 |
< |
addcolor(mypixel[v2*hres+h2], ctmp); |
256 |
< |
myweight[v2*hres+h2] += pixWeight[r2] * myweight[p]; |
257 |
< |
} |
223 |
> |
p = v*hres + h; |
224 |
> |
if (*rf <= FTINY) /* straight Voronoi regions */ |
225 |
> |
ni = 0; |
226 |
> |
else { /* weighted choice */ |
227 |
> |
DCHECK(nd[n-1]>=MAXRAD2, CONSISTENCY, "out of range neighbor"); |
228 |
> |
rnt[0] = pixWeight[nd[0]]; |
229 |
> |
for (ni = 1; ni < n; ni++) |
230 |
> |
rnt[ni] = rnt[ni-1] + pixWeight[nd[ni]]; |
231 |
> |
rvar = rnt[n-1]*pow(frandom(), 1. / *rf); |
232 |
> |
for (ni = 0; rvar > rnt[ni]+FTINY; ni++) |
233 |
> |
; |
234 |
|
} |
235 |
+ |
pn = nl[ni][1]*hres + nl[ni][0]; |
236 |
+ |
addcolor(mypixel[p], mypixel[pn]); |
237 |
+ |
myweight[p] += myweight[pn]; |
238 |
|
return(1); |
239 |
|
} |
240 |
|
|
245 |
|
if (pixWeight[0] <= FTINY) |
246 |
|
init_wfunc(); /* initialize weighting function */ |
247 |
|
reset_flags(); /* set occupancy flags */ |
248 |
< |
meet_neighbors(kill_occl,NULL); /* eliminate occlusion errors */ |
248 |
> |
meet_neighbors(1,kill_occl,NULL); /* identify occlusion errors */ |
249 |
|
reset_flags(); /* reset occupancy flags */ |
250 |
|
if (ransamp >= 0.) /* spread samples over image */ |
251 |
< |
meet_neighbors(random_samp,&ransamp); |
251 |
> |
meet_neighbors(0,random_samp,&ransamp); |
252 |
|
else |
253 |
< |
meet_neighbors(smooth_samp,NULL); |
254 |
< |
free((char *)pixFlags); /* free pixel flags */ |
253 |
> |
meet_neighbors(1,smooth_samp,NULL); |
254 |
> |
free((void *)pixFlags); /* free pixel flags */ |
255 |
|
pixFlags = NULL; |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
reset_flags() /* allocate/set/reset occupancy flags */ |
260 |
|
{ |
261 |
< |
register int4 p; |
261 |
> |
register int32 p; |
262 |
|
|
263 |
|
if (pixFlags == NULL) { |
264 |
< |
pixFlags = (int4 *)calloc(FL4NELS(hres*vres), sizeof(int4)); |
264 |
> |
pixFlags = (int32 *)calloc(FL4NELS(hres*vres), sizeof(int32)); |
265 |
|
CHECK(pixFlags==NULL, SYSTEM, "out of memory in reset_flags"); |
266 |
|
} else |
267 |
|
CLR4ALL(pixFlags, hres*vres); |
273 |
|
|
274 |
|
init_wfunc() /* initialize weighting function */ |
275 |
|
{ |
276 |
< |
register int4 r2; |
276 |
> |
register int r2; |
277 |
|
register double d; |
278 |
|
|
279 |
|
for (r2 = MAXRAD2; --r2; ) { |
287 |
|
|
288 |
|
|
289 |
|
int |
290 |
< |
findneigh(nl, h, v, rnl) /* find NNEIGH neighbors for pixel */ |
290 |
> |
findneigh(nl, nd, h, v, rnl) /* find NNEIGH neighbors for pixel */ |
291 |
|
short nl[NNEIGH][2]; |
292 |
+ |
int nd[NNEIGH]; |
293 |
|
int h, v; |
294 |
|
register short (*rnl)[NNEIGH]; |
295 |
|
{ |
296 |
|
int nn = 0; |
297 |
< |
int4 d, nd[NNEIGH]; |
318 |
< |
int n, hoff; |
297 |
> |
int d, n, hoff; |
298 |
|
register int h2, n2; |
299 |
|
|
300 |
|
nd[NNEIGH-1] = MAXRAD2; |
301 |
< |
for (hoff = 1; hoff < hres; hoff = (hoff<0) - hoff) { |
301 |
> |
for (hoff = 0; hoff < hres; hoff = (hoff<=0) - hoff) { |
302 |
|
h2 = h + hoff; |
303 |
|
if (h2 < 0 | h2 >= hres) |
304 |
|
continue; |
306 |
|
break; |
307 |
|
for (n = 0; n < NNEIGH && rnl[h2][n] < NINF; n++) { |
308 |
|
d = (h2-h)*(h2-h) + (v-rnl[h2][n])*(v-rnl[h2][n]); |
309 |
< |
if (d >= nd[NNEIGH-1]) |
309 |
> |
if (d == 0 | d >= nd[NNEIGH-1]) |
310 |
|
continue; |
311 |
|
if (nn < NNEIGH) /* insert neighbor */ |
312 |
|
nn++; |
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
< |
meet_neighbors(nf, dp) /* run through samples and their neighbors */ |
330 |
> |
meet_neighbors(occ, nf, dp) /* run through samples and their neighbors */ |
331 |
> |
int occ; |
332 |
|
int (*nf)(); |
333 |
|
char *dp; |
334 |
|
{ |
335 |
|
short ln[NNEIGH][2]; |
336 |
+ |
int nd[NNEIGH]; |
337 |
|
int h, v, n, v2; |
338 |
|
register short (*rnl)[NNEIGH]; |
339 |
|
/* initialize bottom row list */ |
352 |
|
v = 0; /* do each row */ |
353 |
|
for ( ; ; ) { |
354 |
|
for (h = 0; h < hres; h++) { |
355 |
< |
if (!CHK4(pixFlags, v*hres+h)) |
356 |
< |
continue; /* no one home */ |
357 |
< |
n = findneigh(ln, h, v, rnl); |
358 |
< |
(*nf)(h, v, ln, n, dp); /* call on neighbors */ |
355 |
> |
if (!CHK4(pixFlags, v*hres+h) != !occ) |
356 |
> |
continue; /* occupancy mismatch */ |
357 |
> |
/* find neighbors */ |
358 |
> |
n = findneigh(ln, nd, h, v, rnl); |
359 |
> |
/* call on neighbors */ |
360 |
> |
(*nf)(h, v, ln, nd, n, dp); |
361 |
|
} |
362 |
|
if (++v >= vres) /* reinitialize row list */ |
363 |
|
break; |
372 |
|
} |
373 |
|
} |
374 |
|
} |
375 |
< |
free((char *)rnl); /* free row list */ |
375 |
> |
free((void *)rnl); /* free row list */ |
376 |
|
} |