ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhpict2.c
Revision: 3.12
Committed: Mon Jun 30 14:59:12 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.11: +4 -2 lines
Log Message:
Replaced most outdated BSD function calls with their posix equivalents, and cleaned up a few other platform dependencies.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rhpict2.c,v 3.11 2003/06/20 00:25:49 greg Exp $";
3 #endif
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"
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 5 /* 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) memset((char *)(f),'\0',FL4NELS(n)*sizeof(int32))
41 #endif
42
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) (isqrttab[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 int32 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, 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 int i;
123 register int32 p;
124
125 if (n <= 0) {
126 #ifdef DEBUG
127 error(WARNING, "neighborless sample in kill_occl");
128 #endif
129 return(1);
130 }
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 = 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 }
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 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 int maxr2;
156 double d;
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 = nd[n-1];
165 DCHECK(maxr2>=MAXRAD2, CONSISTENCY, "out of range neighbor");
166 maxr = isqrt(maxr2);
167 for (v2 = 1; v2 <= maxr; v2++)
168 for (h2 = 0; h2 <= v2; h2++) {
169 r2 = h2*h2 + v2*v2;
170 if (r2 > maxr2) break;
171 copycolor(mykern[r2], mypixel[p]);
172 scalecolor(mykern[r2], pixWeight[r2]);
173 }
174 ndis = 0; /* find discontinuities */
175 for (i = n; i--; ) {
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],
180 mypixel[nl[i][1]*hres+nl[i][0]], r*PEPS))
181 dis[ndis++] = i;
182 }
183 /* stamp out that kernel */
184 for (v2 = v-maxr; v2 <= v+maxr; v2++) {
185 if (v2 < 0) v2 = 0;
186 else if (v2 >= vres) break;
187 for (h2 = h-maxr; h2 <= h+maxr; h2++) {
188 if (h2 < 0) h2 = 0;
189 else if (h2 >= hres) break;
190 r2 = (h2-h)*(h2-h) + (v2-v)*(v2-v);
191 if (r2 > maxr2) continue;
192 if (CHK4(pixFlags, v2*hres+h2))
193 continue; /* occupied */
194 for (i = ndis; i--; ) {
195 r = (h2-nl[dis[i]][0])*(h2-nl[dis[i]][0]) +
196 (v2-nl[dis[i]][1])*(v2-nl[dis[i]][1]);
197 if (r < r2) break;
198 }
199 if (i >= 0) continue; /* outside edge */
200 addcolor(mypixel[v2*hres+h2], mykern[r2]);
201 myweight[v2*hres+h2] += pixWeight[r2] * myweight[p];
202 }
203 }
204 return(1);
205 }
206
207
208 int
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 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;
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
241
242 pixFinish(ransamp) /* done with beams -- compute pixel values */
243 double ransamp;
244 {
245 if (pixWeight[0] <= FTINY)
246 init_wfunc(); /* initialize weighting function */
247 reset_flags(); /* set occupancy flags */
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(0,random_samp,&ransamp);
252 else
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 int32 p;
262
263 if (pixFlags == NULL) {
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);
268 for (p = hres*vres; p--; )
269 if (myweight[p] > FTINY)
270 SET4(pixFlags, p);
271 }
272
273
274 init_wfunc() /* initialize weighting function */
275 {
276 register int r2;
277 register double d;
278
279 for (r2 = MAXRAD2; --r2; ) {
280 d = sqrt((double)r2);
281 pixWeight[r2] = G0NORM/d;
282 isqrttab[r2] = d + 0.99;
283 }
284 pixWeight[0] = 1.;
285 isqrttab[0] = 0;
286 }
287
288
289 int
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 int d, n, hoff;
298 register int h2, n2;
299
300 nd[NNEIGH-1] = MAXRAD2;
301 for (hoff = 0; hoff < hres; hoff = (hoff<=0) - hoff) {
302 h2 = h + hoff;
303 if (h2 < 0 | h2 >= hres)
304 continue;
305 if ((h2-h)*(h2-h) >= nd[NNEIGH-1])
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 == 0 | d >= nd[NNEIGH-1])
310 continue;
311 if (nn < NNEIGH) /* insert neighbor */
312 nn++;
313 for (n2 = nn; n2--; ) {
314 if (!n2 || d >= nd[n2-1]) {
315 nd[n2] = d;
316 nl[n2][0] = h2;
317 nl[n2][1] = rnl[h2][n];
318 break;
319 }
320 nd[n2] = nd[n2-1];
321 nl[n2][0] = nl[n2-1][0];
322 nl[n2][1] = nl[n2-1][1];
323 }
324 }
325 }
326 return(nn);
327 }
328
329
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 */
340 rnl = (short (*)[NNEIGH])malloc(NNEIGH*sizeof(short)*hres);
341 CHECK(rnl==NULL, SYSTEM, "out of memory in meet_neighbors");
342 for (h = 0; h < hres; h++) {
343 for (n = v = 0; v < vres; v++)
344 if (CHK4(pixFlags, v*hres+h)) {
345 rnl[h][n++] = v;
346 if (n >= NNEIGH)
347 break;
348 }
349 while (n < NNEIGH)
350 rnl[h][n++] = NINF;
351 }
352 v = 0; /* do each row */
353 for ( ; ; ) {
354 for (h = 0; h < hres; h++) {
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;
364 for (h = 0; h < hres; h++)
365 for (v2 = rnl[h][NNEIGH-1]+1; v2 < vres; v2++) {
366 if (v2 - v > v - rnl[h][0])
367 break; /* not close enough */
368 if (CHK4(pixFlags, v2*hres+h)) {
369 for (n = 0; n < NNEIGH-1; n++)
370 rnl[h][n] = rnl[h][n+1];
371 rnl[h][NNEIGH-1] = v2;
372 }
373 }
374 }
375 free((void *)rnl); /* free row list */
376 }