ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhpict2.c
(Generate patch)

Comparing ray/src/hd/rhpict2.c (file contents):
Revision 3.2 by gwlarson, Fri Mar 5 17:03:38 1999 UTC vs.
Revision 3.12 by schorsch, Mon Jun 30 14:59:12 2003 UTC

# Line 1 | Line 1
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"
13  
14   #ifndef DEPS
15 < #define DEPS            0.01    /* depth epsilon */
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 */
24 > #define NNEIGH          5       /* find this many neighbors */
25   #endif
26  
27   #define NINF            16382
# Line 34 | Line 37 | static char SCCSid[] = "$SunId$ SGI";
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)       (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) */
# Line 57 | Line 63 | register HDBEAMI       *hb;
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");
# Line 105 | Line 111 | register HDBEAMI       *hb;
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 < short   nl[NNEIGH][2];
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;
122 >        register int    i;
123 >        register int32  p;
124  
125 <        if (n <= 0)
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 = (h-nl[i][0])*(h-nl[i][0]) + (v-nl[i][1])*(v-nl[i][1]);
135 <                if (mydepth[nl[i][1]*hres+nl[i][0]] <
123 <                                mydepth[v*hres+h]*(1.-DEPS*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          }
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, 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 <        float   mykw[MAXRAD2];
156 <        int4    maxr2;
157 <        double  w;
158 <        register int4   p, r2;
159 <        int     maxr, h2, v2;
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 = (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 <        for (r2 = maxr2+1; --r2; ) {
167 <                copycolor(mykern[r2], mypixel[p]);
168 <                mykw[r2] = pixWeight[r2];
169 <                if (2*r2 >= maxr2)              /* soften skirt */
170 <                        mykw[r2] *= (2*(maxr2-r2)+1.0)/maxr2;
171 <                scalecolor(mykern[r2], mykw[r2]);
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 <        maxr = sqrt((double)maxr2) + .99;       /* stamp out that kernel */
183 >                                                /* stamp out that kernel */
184          for (v2 = v-maxr; v2 <= v+maxr; v2++) {
185 <                if (v2 < 0) continue;
186 <                if (v2 >= vres) break;
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) continue;
189 <                        if (h2 >= hres) break;
190 <                        r2 = (v2-v)*(v2-v) + (h2-h)*(h2-h);
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] += mykw[r2]*myweight[v*hres+h];
201 >                        myweight[v2*hres+h2] += pixWeight[r2] * myweight[p];
202                  }
203          }
204          return(1);
205   }
206  
207  
208 < pixFlush()                      /* done with beams -- flush pixel values */
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(kill_occl);      /* eliminate occlusion errors */
248 >        meet_neighbors(1,kill_occl,NULL); /* identify occlusion errors */
249          reset_flags();                  /* reset occupancy flags */
250 <        if (pixWeight[0] <= FTINY) {    /* initialize weighting function */
251 <                register int    r;
252 <                for (r = MAXRAD2; --r; )
253 <                        pixWeight[r] = G0NORM/sqrt((double)r);
254 <                pixWeight[0] = 1.;
189 <        }
190 <        meet_neighbors(grow_samp);      /* grow valid samples over image */
191 <        free((char *)pixFlags);         /* free pixel 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 int    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);
# Line 208 | Line 271 | reset_flags()                  /* allocate/set/reset occupancy flags
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, 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, ld, nd[NNEIGH+1];
219 <        int     n, hoff;
297 >        int     d, n, hoff;
298          register int    h2, n2;
299  
300 <        ld = MAXRAD2;
301 <        for (hoff = 1; hoff < hres; hoff = (hoff<0) - hoff) {
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) >= ld)
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 >= ld)
309 >                        if (d == 0 | d >= nd[NNEIGH-1])
310                                  continue;
311 <                        for (n2 = nn; ; n2--)   {       /* insert neighbor */
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;
# Line 241 | Line 321 | register short (*rnl)[NNEIGH];
321                                  nl[n2][0] = nl[n2-1][0];
322                                  nl[n2][1] = nl[n2-1][1];
323                          }
244                        if (nn < NNEIGH)
245                                nn++;
246                        else
247                                ld = nd[NNEIGH-1];
324                  }
325          }
326          return(nn);
327   }
328  
329  
330 < meet_neighbors(nf)              /* 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 */
# Line 273 | Line 352 | int    (*nf)();
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);     /* 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;
# Line 291 | Line 372 | int    (*nf)();
372                                  }
373                          }
374          }
375 <        free((char *)rnl);              /* free row list */
375 >        free((void *)rnl);              /* free row list */
376   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines