ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhpict2.c
Revision: 3.4
Committed: Mon Mar 8 17:34:24 1999 UTC (25 years, 1 month ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.3: +22 -15 lines
Log Message:
put weighting function initialization into separate routine

File Contents

# Content
1 /* Copyright (c) 1999 Silicon Graphics, Inc. */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ SGI";
5 #endif
6
7 /*
8 * Rendering routines for rhpict.
9 */
10
11 #include "holo.h"
12 #include "view.h"
13
14 #ifndef DEPS
15 #define DEPS 0.02 /* depth epsilon */
16 #endif
17 #ifndef PEPS
18 #define PEPS 0.04 /* pixel value epsilon */
19 #endif
20 #ifndef MAXRAD
21 #define MAXRAD 64 /* maximum kernel radius */
22 #endif
23 #ifndef NNEIGH
24 #define NNEIGH 7 /* find this many neighbors */
25 #endif
26
27 #define NINF 16382
28
29 #define MAXRAD2 (MAXRAD*MAXRAD+1)
30
31 #define G0NORM 0.286 /* ground zero normalization (1/x integral) */
32
33 #ifndef FL4OP
34 #define FL4OP(f,i,op) ((f)[(i)>>5] op (1L<<((i)&0x1f)))
35 #define CHK4(f,i) FL4OP(f,i,&)
36 #define SET4(f,i) FL4OP(f,i,|=)
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))
41 #endif
42
43 static int4 *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)])
48
49 extern VIEW myview; /* current output view */
50 extern COLOR *mypixel; /* pixels being rendered */
51 extern float *myweight; /* weights (used to compute final pixels) */
52 extern float *mydepth; /* depth values (visibility culling) */
53 extern int hres, vres; /* current horizontal and vertical res. */
54
55
56 pixBeam(bp, hb) /* render a particular beam */
57 BEAM *bp;
58 register HDBEAMI *hb;
59 {
60 GCOORD gc[2];
61 register RAYVAL *rv;
62 FVECT rorg, rdir, wp, ip;
63 double d, prox;
64 COLOR col;
65 int n;
66 register int4 p;
67
68 if (!hdbcoord(gc, hb->h, hb->b))
69 error(CONSISTENCY, "bad beam in render_beam");
70 for (n = bp->nrm, rv = hdbray(bp); n--; rv++) {
71 /* reproject each sample */
72 hdray(rorg, rdir, hb->h, gc, rv->r);
73 if (rv->d < DCINF) {
74 d = hddepth(hb->h, rv->d);
75 VSUM(wp, rorg, rdir, d);
76 VSUB(ip, wp, myview.vp);
77 d = DOT(ip,rdir);
78 prox = d*d/DOT(ip,ip); /* cos(diff_angle)^32 */
79 prox *= prox; prox *= prox; prox *= prox; prox *= prox;
80 } else {
81 if (myview.type == VT_PAR || myview.vaft > FTINY)
82 continue; /* inf. off view */
83 VSUM(wp, myview.vp, rdir, FHUGE);
84 prox = 1.;
85 }
86 viewloc(ip, &myview, wp); /* frustum clipping */
87 if (ip[2] < 0.)
88 continue;
89 if (ip[0] < 0. || ip[0] >= 1.)
90 continue;
91 if (ip[1] < 0. || ip[1] >= 1.)
92 continue;
93 if (myview.vaft > FTINY && ip[2] > myview.vaft - myview.vfore)
94 continue; /* not exact for VT_PER */
95 p = (int)(ip[1]*vres)*hres + (int)(ip[0]*hres);
96 if (mydepth[p] > FTINY) { /* check depth */
97 if (ip[2] > mydepth[p]*(1.+DEPS))
98 continue;
99 if (ip[2] < mydepth[p]*(1.-DEPS)) {
100 setcolor(mypixel[p], 0., 0., 0.);
101 myweight[p] = 0.;
102 }
103 }
104 colr_color(col, rv->v);
105 scalecolor(col, prox);
106 addcolor(mypixel[p], col);
107 myweight[p] += prox;
108 mydepth[p] = ip[2];
109 }
110 }
111
112
113 int
114 kill_occl(h, v, nl, n) /* check for occlusion errors */
115 int h, v;
116 register short nl[NNEIGH][2];
117 int n;
118 {
119 short forequad[2][2];
120 int d;
121 register int4 i, p;
122
123 if (n <= 0)
124 return(1);
125 p = v*hres + h;
126 forequad[0][0] = forequad[0][1] = forequad[1][0] = forequad[1][1] = 0;
127 for (i = n; i--; ) {
128 d = (h-nl[i][0])*(h-nl[i][0]) + (v-nl[i][1])*(v-nl[i][1]);
129 d = isqrt(d);
130 if (mydepth[nl[i][1]*hres+nl[i][0]]*(1.+DEPS*d) < mydepth[p])
131 forequad[nl[i][0]<h][nl[i][1]<v] = 1;
132 }
133 if (forequad[0][0]+forequad[0][1]+forequad[1][0]+forequad[1][1] > 2) {
134 setcolor(mypixel[p], 0., 0., 0.);
135 myweight[p] = 0.; /* occupancy reset afterwards */
136 }
137 return(1);
138 }
139
140
141 int
142 grow_samp(h, v, nl, n) /* grow sample point appropriately */
143 int h, v;
144 register short nl[NNEIGH][2];
145 int n;
146 {
147 int dis[NNEIGH], ndis;
148 COLOR mykern[MAXRAD2];
149 int4 maxr2;
150 double w, d;
151 register int4 p, r2;
152 int i, r, maxr, h2, v2;
153
154 if (n <= 0)
155 return(1);
156 p = v*hres + h; /* build kernel values */
157 maxr2 = (h-nl[n-1][0])*(h-nl[n-1][0]) + (v-nl[n-1][1])*(v-nl[n-1][1]);
158 DCHECK(maxr2>=MAXRAD2, CONSISTENCY, "out of range neighbor");
159 maxr = isqrt(maxr2);
160 for (v2 = 1; v2 <= maxr; v2++)
161 for (h2 = 0; h2 <= v2; h2++) {
162 r2 = h2*h2 + v2*v2;
163 if (r2 > maxr2) break;
164 copycolor(mykern[r2], mypixel[p]);
165 scalecolor(mykern[r2], pixWeight[r2]);
166 }
167 ndis = 0; /* find discontinuities */
168 for (i = n; i--; ) {
169 r2 = (h-nl[i][0])*(h-nl[i][0]) + (v-nl[i][1])*(v-nl[i][1]);
170 r = isqrt(r2);
171 d = mydepth[nl[i][1]*hres+nl[i][0]] / mydepth[p];
172 d = d>=1. ? d-1. : 1.-d;
173 if (d > r*DEPS || bigdiff(mypixel[p],
174 mypixel[nl[i][1]*hres+nl[i][0]], r*PEPS))
175 dis[ndis++] = i;
176 }
177 /* stamp out that kernel */
178 for (v2 = v-maxr; v2 <= v+maxr; v2++) {
179 if (v2 < 0) v2 = 0;
180 else if (v2 >= vres) break;
181 for (h2 = h-maxr; h2 <= h+maxr; h2++) {
182 if (h2 < 0) h2 = 0;
183 else if (h2 >= hres) break;
184 r2 = (h2-h)*(h2-h) + (v2-v)*(v2-v);
185 if (r2 > maxr2) continue;
186 if (CHK4(pixFlags, v2*hres+h2))
187 continue; /* occupied */
188 for (i = ndis; i--; ) {
189 r = (h2-nl[dis[i]][0])*(h2-nl[dis[i]][0]) +
190 (v2-nl[dis[i]][1])*(v2-nl[dis[i]][1]);
191 if (r < r2) break;
192 }
193 if (i >= 0) continue; /* outside edge */
194 addcolor(mypixel[v2*hres+h2], mykern[r2]);
195 myweight[v2*hres+h2] += pixWeight[r2] *
196 myweight[v*hres+h];
197 }
198 }
199 return(1);
200 }
201
202
203 pixFlush() /* done with beams -- flush pixel values */
204 {
205 reset_flags(); /* set occupancy flags */
206 meet_neighbors(kill_occl); /* eliminate occlusion errors */
207 reset_flags(); /* reset occupancy flags */
208 if (pixWeight[0] <= FTINY)
209 init_wfunc(); /* initialize weighting function */
210 meet_neighbors(grow_samp); /* grow valid samples over image */
211 free((char *)pixFlags); /* free pixel flags */
212 pixFlags = NULL;
213 }
214
215
216 reset_flags() /* allocate/set/reset occupancy flags */
217 {
218 register int4 p;
219
220 if (pixFlags == NULL) {
221 pixFlags = (int4 *)calloc(FL4NELS(hres*vres), sizeof(int4));
222 CHECK(pixFlags==NULL, SYSTEM, "out of memory in reset_flags");
223 } else
224 CLR4ALL(pixFlags, hres*vres);
225 for (p = hres*vres; p--; )
226 if (myweight[p] > FTINY)
227 SET4(pixFlags, p);
228 }
229
230
231 init_wfunc() /* initialize weighting function */
232 {
233 register int i, j;
234 register int4 r2;
235 register double d;
236
237 for (i = 1; i <= MAXRAD; i++)
238 for (j = 0; j <= i; j++) {
239 r2 = i*i + j*j;
240 if (r2 >= MAXRAD2) break;
241 d = sqrt((double)r2);
242 pixWeight[r2] = G0NORM/d;
243 isqrttab[r2] = d + 0.99;
244 }
245 pixWeight[0] = 1.;
246 isqrttab[0] = 0;
247 }
248
249
250 int
251 findneigh(nl, h, v, rnl) /* find NNEIGH neighbors for pixel */
252 short nl[NNEIGH][2];
253 int h, v;
254 register short (*rnl)[NNEIGH];
255 {
256 int nn = 0;
257 int4 d, nd[NNEIGH];
258 int n, hoff;
259 register int h2, n2;
260
261 nd[NNEIGH-1] = MAXRAD2;
262 for (hoff = 1; hoff < hres; hoff = (hoff<0) - hoff) {
263 h2 = h + hoff;
264 if (h2 < 0 | h2 >= hres)
265 continue;
266 if ((h2-h)*(h2-h) >= nd[NNEIGH-1])
267 break;
268 for (n = 0; n < NNEIGH && rnl[h2][n] < NINF; n++) {
269 d = (h2-h)*(h2-h) + (v-rnl[h2][n])*(v-rnl[h2][n]);
270 if (d >= nd[NNEIGH-1])
271 continue;
272 if (nn < NNEIGH) /* insert neighbor */
273 nn++;
274 for (n2 = nn; n2--; ) {
275 if (!n2 || d >= nd[n2-1]) {
276 nd[n2] = d;
277 nl[n2][0] = h2;
278 nl[n2][1] = rnl[h2][n];
279 break;
280 }
281 nd[n2] = nd[n2-1];
282 nl[n2][0] = nl[n2-1][0];
283 nl[n2][1] = nl[n2-1][1];
284 }
285 }
286 }
287 return(nn);
288 }
289
290
291 meet_neighbors(nf) /* run through samples and their neighbors */
292 int (*nf)();
293 {
294 short ln[NNEIGH][2];
295 int h, v, n, v2;
296 register short (*rnl)[NNEIGH];
297 /* initialize bottom row list */
298 rnl = (short (*)[NNEIGH])malloc(NNEIGH*sizeof(short)*hres);
299 CHECK(rnl==NULL, SYSTEM, "out of memory in meet_neighbors");
300 for (h = 0; h < hres; h++) {
301 for (n = v = 0; v < vres; v++)
302 if (CHK4(pixFlags, v*hres+h)) {
303 rnl[h][n++] = v;
304 if (n >= NNEIGH)
305 break;
306 }
307 while (n < NNEIGH)
308 rnl[h][n++] = NINF;
309 }
310 v = 0; /* do each row */
311 for ( ; ; ) {
312 for (h = 0; h < hres; h++) {
313 if (!CHK4(pixFlags, v*hres+h))
314 continue; /* no one home */
315 n = findneigh(ln, h, v, rnl);
316 (*nf)(h, v, ln, n); /* call on neighbors */
317 }
318 if (++v >= vres) /* reinitialize row list */
319 break;
320 for (h = 0; h < hres; h++)
321 for (v2 = rnl[h][NNEIGH-1]+1; v2 < vres; v2++) {
322 if (v2 - v > v - rnl[h][0])
323 break; /* not close enough */
324 if (CHK4(pixFlags, v2*hres+h)) {
325 for (n = 0; n < NNEIGH-1; n++)
326 rnl[h][n] = rnl[h][n+1];
327 rnl[h][NNEIGH-1] = v2;
328 }
329 }
330 }
331 free((char *)rnl); /* free row list */
332 }