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, 10 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

# User Rev Content
1 gwlarson 3.1 #ifndef lint
2 schorsch 3.12 static const char RCSid[] = "$Id: rhpict2.c,v 3.11 2003/06/20 00:25:49 greg Exp $";
3 gwlarson 3.1 #endif
4     /*
5     * Rendering routines for rhpict.
6     */
7    
8 schorsch 3.12 #include <string.h>
9    
10 gwlarson 3.1 #include "holo.h"
11     #include "view.h"
12 gwlarson 3.6 #include "random.h"
13 gwlarson 3.1
14 gwlarson 3.2 #ifndef DEPS
15 gwlarson 3.3 #define DEPS 0.02 /* depth epsilon */
16 gwlarson 3.2 #endif
17 gwlarson 3.3 #ifndef PEPS
18     #define PEPS 0.04 /* pixel value epsilon */
19     #endif
20 gwlarson 3.2 #ifndef MAXRAD
21     #define MAXRAD 64 /* maximum kernel radius */
22     #endif
23     #ifndef NNEIGH
24 gwlarson 3.9 #define NNEIGH 5 /* find this many neighbors */
25 gwlarson 3.2 #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 schorsch 3.12 #define CLR4ALL(f,n) memset((char *)(f),'\0',FL4NELS(n)*sizeof(int32))
41 gwlarson 3.2 #endif
42    
43 greg 3.11 static int32 *pixFlags; /* pixel occupancy flags */
44 gwlarson 3.2 static float pixWeight[MAXRAD2]; /* pixel weighting function */
45 gwlarson 3.3 static short isqrttab[MAXRAD2]; /* integer square root table */
46 gwlarson 3.2
47 gwlarson 3.9 #define isqrt(i2) (isqrttab[i2])
48 gwlarson 3.3
49 gwlarson 3.1 extern VIEW myview; /* current output view */
50     extern COLOR *mypixel; /* pixels being rendered */
51     extern float *myweight; /* weights (used to compute final pixels) */
52 gwlarson 3.2 extern float *mydepth; /* depth values (visibility culling) */
53 gwlarson 3.1 extern int hres, vres; /* current horizontal and vertical res. */
54    
55    
56 gwlarson 3.2 pixBeam(bp, hb) /* render a particular beam */
57 gwlarson 3.1 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 gwlarson 3.2 int n;
66 greg 3.11 register int32 p;
67 gwlarson 3.1
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 gwlarson 3.2 prox = d*d/DOT(ip,ip); /* cos(diff_angle)^32 */
79     prox *= prox; prox *= prox; prox *= prox; prox *= prox;
80 gwlarson 3.1 } else {
81     if (myview.type == VT_PAR || myview.vaft > FTINY)
82     continue; /* inf. off view */
83     VSUM(wp, myview.vp, rdir, FHUGE);
84 gwlarson 3.2 prox = 1.;
85 gwlarson 3.1 }
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 gwlarson 3.2 continue; /* not exact for VT_PER */
95 gwlarson 3.1 p = (int)(ip[1]*vres)*hres + (int)(ip[0]*hres);
96 gwlarson 3.2 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 gwlarson 3.1 colr_color(col, rv->v);
105 gwlarson 3.2 scalecolor(col, prox);
106 gwlarson 3.1 addcolor(mypixel[p], col);
107 gwlarson 3.2 myweight[p] += prox;
108     mydepth[p] = ip[2];
109 gwlarson 3.1 }
110 gwlarson 3.2 }
111    
112    
113     int
114 gwlarson 3.9 kill_occl(h, v, nl, nd, n) /* check for occlusion errors */
115 gwlarson 3.2 int h, v;
116 gwlarson 3.3 register short nl[NNEIGH][2];
117 gwlarson 3.9 int nd[NNEIGH];
118 gwlarson 3.2 int n;
119     {
120     short forequad[2][2];
121     int d;
122 gwlarson 3.9 register int i;
123 greg 3.11 register int32 p;
124 gwlarson 3.2
125 gwlarson 3.5 if (n <= 0) {
126     #ifdef DEBUG
127     error(WARNING, "neighborless sample in kill_occl");
128     #endif
129 gwlarson 3.2 return(1);
130 gwlarson 3.5 }
131 gwlarson 3.3 p = v*hres + h;
132 gwlarson 3.2 forequad[0][0] = forequad[0][1] = forequad[1][0] = forequad[1][1] = 0;
133     for (i = n; i--; ) {
134 gwlarson 3.9 d = isqrt(nd[i]);
135 gwlarson 3.3 if (mydepth[nl[i][1]*hres+nl[i][0]]*(1.+DEPS*d) < mydepth[p])
136 gwlarson 3.2 forequad[nl[i][0]<h][nl[i][1]<v] = 1;
137     }
138 gwlarson 3.3 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 gwlarson 3.2 }
142     return(1);
143     }
144    
145    
146     int
147 gwlarson 3.9 smooth_samp(h, v, nl, nd, n) /* grow sample point smoothly */
148 gwlarson 3.2 int h, v;
149     register short nl[NNEIGH][2];
150 gwlarson 3.9 int nd[NNEIGH];
151 gwlarson 3.2 int n;
152     {
153 gwlarson 3.3 int dis[NNEIGH], ndis;
154 gwlarson 3.2 COLOR mykern[MAXRAD2];
155 gwlarson 3.9 int maxr2;
156 gwlarson 3.5 double d;
157 greg 3.11 register int32 p;
158 gwlarson 3.9 register int r2;
159 gwlarson 3.3 int i, r, maxr, h2, v2;
160 gwlarson 3.2
161     if (n <= 0)
162     return(1);
163     p = v*hres + h; /* build kernel values */
164 gwlarson 3.9 maxr2 = nd[n-1];
165 gwlarson 3.2 DCHECK(maxr2>=MAXRAD2, CONSISTENCY, "out of range neighbor");
166 gwlarson 3.3 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 gwlarson 3.9 r = isqrt(nd[i]);
177 gwlarson 3.3 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 gwlarson 3.2 }
183 gwlarson 3.3 /* stamp out that kernel */
184 gwlarson 3.2 for (v2 = v-maxr; v2 <= v+maxr; v2++) {
185 gwlarson 3.3 if (v2 < 0) v2 = 0;
186     else if (v2 >= vres) break;
187 gwlarson 3.2 for (h2 = h-maxr; h2 <= h+maxr; h2++) {
188 gwlarson 3.3 if (h2 < 0) h2 = 0;
189     else if (h2 >= hres) break;
190     r2 = (h2-h)*(h2-h) + (v2-v)*(v2-v);
191 gwlarson 3.2 if (r2 > maxr2) continue;
192     if (CHK4(pixFlags, v2*hres+h2))
193     continue; /* occupied */
194 gwlarson 3.3 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 gwlarson 3.2 addcolor(mypixel[v2*hres+h2], mykern[r2]);
201 gwlarson 3.5 myweight[v2*hres+h2] += pixWeight[r2] * myweight[p];
202 gwlarson 3.2 }
203     }
204     return(1);
205     }
206    
207    
208 gwlarson 3.6 int
209 gwlarson 3.9 random_samp(h, v, nl, nd, n, rf) /* gather samples randomly */
210 gwlarson 3.6 int h, v;
211     register short nl[NNEIGH][2];
212 gwlarson 3.9 int nd[NNEIGH];
213 gwlarson 3.6 int n;
214 gwlarson 3.7 double *rf;
215 gwlarson 3.2 {
216 gwlarson 3.9 float rnt[NNEIGH];
217     double rvar;
218 greg 3.11 register int32 p, pn;
219 gwlarson 3.9 register int ni;
220 gwlarson 3.6
221     if (n <= 0)
222     return(1);
223 gwlarson 3.9 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 gwlarson 3.6 }
235 gwlarson 3.9 pn = nl[ni][1]*hres + nl[ni][0];
236     addcolor(mypixel[p], mypixel[pn]);
237     myweight[p] += myweight[pn];
238 gwlarson 3.6 return(1);
239     }
240    
241    
242     pixFinish(ransamp) /* done with beams -- compute pixel values */
243 gwlarson 3.7 double ransamp;
244 gwlarson 3.6 {
245 gwlarson 3.5 if (pixWeight[0] <= FTINY)
246     init_wfunc(); /* initialize weighting function */
247 gwlarson 3.2 reset_flags(); /* set occupancy flags */
248 gwlarson 3.9 meet_neighbors(1,kill_occl,NULL); /* identify occlusion errors */
249 gwlarson 3.2 reset_flags(); /* reset occupancy flags */
250 gwlarson 3.7 if (ransamp >= 0.) /* spread samples over image */
251 gwlarson 3.9 meet_neighbors(0,random_samp,&ransamp);
252 gwlarson 3.6 else
253 gwlarson 3.9 meet_neighbors(1,smooth_samp,NULL);
254 greg 3.10 free((void *)pixFlags); /* free pixel flags */
255 gwlarson 3.2 pixFlags = NULL;
256     }
257    
258    
259     reset_flags() /* allocate/set/reset occupancy flags */
260     {
261 greg 3.11 register int32 p;
262 gwlarson 3.2
263     if (pixFlags == NULL) {
264 greg 3.11 pixFlags = (int32 *)calloc(FL4NELS(hres*vres), sizeof(int32));
265 gwlarson 3.2 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 gwlarson 3.4 }
272    
273    
274     init_wfunc() /* initialize weighting function */
275     {
276 gwlarson 3.9 register int r2;
277 gwlarson 3.4 register double d;
278    
279 gwlarson 3.6 for (r2 = MAXRAD2; --r2; ) {
280     d = sqrt((double)r2);
281     pixWeight[r2] = G0NORM/d;
282     isqrttab[r2] = d + 0.99;
283     }
284 gwlarson 3.4 pixWeight[0] = 1.;
285     isqrttab[0] = 0;
286 gwlarson 3.2 }
287    
288    
289     int
290 gwlarson 3.9 findneigh(nl, nd, h, v, rnl) /* find NNEIGH neighbors for pixel */
291 gwlarson 3.2 short nl[NNEIGH][2];
292 gwlarson 3.9 int nd[NNEIGH];
293 gwlarson 3.2 int h, v;
294     register short (*rnl)[NNEIGH];
295     {
296     int nn = 0;
297 gwlarson 3.9 int d, n, hoff;
298 gwlarson 3.2 register int h2, n2;
299    
300 gwlarson 3.3 nd[NNEIGH-1] = MAXRAD2;
301 gwlarson 3.9 for (hoff = 0; hoff < hres; hoff = (hoff<=0) - hoff) {
302 gwlarson 3.2 h2 = h + hoff;
303     if (h2 < 0 | h2 >= hres)
304     continue;
305 gwlarson 3.3 if ((h2-h)*(h2-h) >= nd[NNEIGH-1])
306 gwlarson 3.2 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 gwlarson 3.9 if (d == 0 | d >= nd[NNEIGH-1])
310 gwlarson 3.2 continue;
311 gwlarson 3.3 if (nn < NNEIGH) /* insert neighbor */
312     nn++;
313     for (n2 = nn; n2--; ) {
314 gwlarson 3.2 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 gwlarson 3.9 meet_neighbors(occ, nf, dp) /* run through samples and their neighbors */
331     int occ;
332 gwlarson 3.2 int (*nf)();
333 gwlarson 3.7 char *dp;
334 gwlarson 3.2 {
335     short ln[NNEIGH][2];
336 gwlarson 3.9 int nd[NNEIGH];
337 gwlarson 3.2 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 gwlarson 3.9 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 gwlarson 3.2 }
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 greg 3.10 free((void *)rnl); /* free row list */
376 gwlarson 3.1 }