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

Comparing ray/src/hd/rhd_qtree.c (file contents):
Revision 3.10 by gregl, Fri Dec 5 09:40:05 1997 UTC vs.
Revision 3.18 by gwlarson, Mon Aug 10 18:39:46 1998 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1997 Silicon Graphics, Inc. */
1 > /* Copyright (c) 1998 Silicon Graphics, Inc. */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ SGI";
# Line 16 | Line 16 | static char SCCSid[] = "$SunId$ SGI";
16   #endif
17                                  /* maximum allowed angle difference (deg.) */
18   #ifndef MAXANG
19 < #define MAXANG          20.
19 > #define MAXANG          20
20   #endif
21 + #if MAXANG>0
22 + #define MAXDIFF2        ( MAXANG*MAXANG * (PI*PI/180./180.))
23 + #endif
24  
22 #define MAXDIFF2        (long)( MAXANG*MAXANG /90./90.*(1L<<15)*(1L<<15))
23
25   #define abs(i)          ((i) < 0 ? -(i) : (i))
26  
27   RTREE   qtrunk;                 /* our quadtree trunk */
# Line 28 | Line 29 | double qtDepthEps = .05;       /* epsilon to compare depths
29   int     qtMinNodesiz = 2;       /* minimum node dimension (pixels) */
30   struct rleaves  qtL;            /* our pile of leaves */
31  
32 + int     rayqleft = 0;           /* rays left to queue before flush */
33 +
34 + static int4     falleaves;      /* our list of fallen leaves */
35 +
36 + #define composted(li)   (qtL.bl <= qtL.tl ? \
37 +                                        ((li) < qtL.bl || (li) >= qtL.tl) : \
38 +                                        ((li) < qtL.bl && (li) >= qtL.tl))
39 +
40   #define TBUNDLESIZ      409     /* number of twigs in a bundle */
41  
42   static RTREE    **twigbundle;   /* free twig blocks (NULL term.) */
43   static int      nexttwig;       /* next free twig */
44  
36 #define ungetleaf(li)   (qtL.tl=(li))   /* dangerous if used improperly */
45  
38
46   static RTREE *
47   newtwig()                       /* allocate a twig */
48   {
# Line 88 | Line 95 | int    really;
95   }
96  
97  
98 < static int
92 < newleaf()                       /* allocate a leaf from our pile */
93 < {
94 <        int     li;
95 <        
96 <        li = qtL.tl++;
97 <        if (qtL.tl >= qtL.nl)   /* get next leaf in ring */
98 <                qtL.tl = 0;
99 <        if (qtL.tl == qtL.bl)   /* need to shake some free */
100 <                qtCompost(LFREEPCT);
101 <        return(li);
102 < }
103 <
104 <
105 < #define LEAFSIZ         (3*sizeof(float)+2*sizeof(short)+\
98 > #define LEAFSIZ         (3*sizeof(float)+sizeof(int4)+\
99                          sizeof(TMbright)+6*sizeof(BYTE))
100  
101   int
# Line 129 | Line 122 | register int   n;
122                  return(0);
123                                  /* assign larger alignment types earlier */
124          qtL.wp = (float (*)[3])qtL.base;
125 <        qtL.wd = (short (*)[2])(qtL.wp + n);
125 >        qtL.wd = (int4 *)(qtL.wp + n);
126          qtL.brt = (TMbright *)(qtL.wd + n);
127          qtL.chr = (BYTE (*)[3])(qtL.brt + n);
128          qtL.rgb = (BYTE (*)[3])(qtL.chr + n);
129          qtL.nl = n;
130          qtL.tml = qtL.bl = qtL.tl = 0;
131 +        falleaves = -1;
132          return(n);
133   }
134  
# Line 165 | Line 159 | register RTREE *tp;
159                                  tp->flgs &= ~BRF(i);
160                  } else if (tp->flgs & LFF(i)) {
161                          li = tp->k[i].li;
162 <                        if (qtL.bl < qtL.tl ?
169 <                                (li < qtL.bl || li >= qtL.tl) :
170 <                                (li < qtL.bl && li >= qtL.tl))
162 >                        if (composted(li))
163                                  tp->flgs &= ~LFF(i);
164                  }
165   }
# Line 177 | Line 169 | int
169   qtCompost(pct)                  /* free up some leaves */
170   int     pct;
171   {
172 +        register int4   *fl;
173          int     nused, nclear, nmapped;
181
174                                  /* figure out how many leaves to clear */
175          nclear = qtL.nl * pct / 100;
176          nused = qtL.tl - qtL.bl;
# Line 189 | Line 181 | int    pct;
181          if (nclear >= nused) {  /* clear them all */
182                  qtFreeTree(0);
183                  qtL.tml = qtL.bl = qtL.tl = 0;
184 +                falleaves = -1;
185                  return(nused);
186          }
187                                  /* else clear leaves from bottom */
# Line 197 | Line 190 | int    pct;
190          qtL.bl += nclear;
191          if (qtL.bl >= qtL.nl) qtL.bl -= qtL.nl;
192          if (nmapped <= nclear) qtL.tml = qtL.bl;
193 <        shaketree(&qtrunk);
193 >        shaketree(&qtrunk);     /* dereference composted leaves */
194 >        for (fl = &falleaves; *fl >= 0; fl = qtL.wd + *fl)
195 >                while (composted(*fl))
196 >                        if ((*fl = qtL.wd[*fl]) < 0)
197 >                                return(nclear);
198          return(nclear);
199   }
200  
201  
205 static
206 encodedir(pa, dv)               /* encode a normalized direction vector */
207 short   pa[2];
208 FVECT   dv;
209 {
210        pa[1] = 0;
211        if (dv[2] >= 1.)
212                pa[0] = (1L<<15)-1;
213        else if (dv[2] <= -1.)
214                pa[0] = -((1L<<15)-1);
215        else {
216                pa[0] = ((1L<<15)-1)/(PI/2.) * asin(dv[2]);
217                pa[1] = ((1L<<15)-1)/PI * atan2(dv[1], dv[0]);
218        }
219 }
220
221
222 #define ALTSHFT         5
223 #define NALT            (1<<ALTSHFT)
224 #define azisft(alt)     azisftab[abs(alt)>>(15-ALTSHFT)]
225
226 static unsigned short   azisftab[NALT];
227
228 static
229 azisftinit(alt)         /* initialize azimuth scale factor table */
230 int     alt;
231 {
232        register int    i;
233
234        for (i = NALT; i--; )
235                azisftab[i] = 2.*(1L<<15) * cos(PI/2.*(i+.5)/NALT);
236        return(azisft(alt));
237 }
238
239 #define azisf(alt)      (azisftab[0] ? azisft(alt) : azisftinit(alt)) >> 15
240
241 static long
242 dir2diff(pa1, pa2)              /* relative distance^2 between directions */
243 short   pa1[2], pa2[2];
244 {
245        long    altd2, azid2;
246        int     alt;
247
248        altd2 = pa1[0] - pa2[0];        /* get altitude difference^2 */
249        altd2 *= altd2;
250        if (altd2 > MAXDIFF2)
251                return(altd2);          /* too large already */
252        azid2 = pa1[1] - pa2[1];        /* get adjusted azimuth difference^2 */
253        if (azid2 < 0) azid2 = -azid2;
254        if (azid2 >= 1L<<15) {          /* wrap sphere */
255                azid2 -= 1L<<16;
256                if (azid2 < 0) azid2 = -azid2;
257        }
258        alt = (pa1[0]+pa2[0])/2;
259        azid2 = azid2*azisf(alt);       /* evaluation order is important */
260        azid2 *= azid2;
261        return(altd2 + azid2);
262 }
263
264
202   int
203   qtFindLeaf(x, y)                /* find closest leaf to (x,y) */
204   int     x, y;
# Line 300 | Line 237 | int    x, y;
237  
238  
239   static
240 < addleaf(li)                     /* add a leaf to our tree */
241 < int     li;
240 > putleaf(li, drop)               /* put a leaf in our tree */
241 > register int    li;
242 > int     drop;
243   {
244          register RTREE  *tp = &qtrunk;
245          int     x0=0, y0=0, x1=odev.hres, y1=odev.vres;
246 <        int     lo = -1;
247 <        long    d2;
310 <        short   dc[2];
246 >        register int    lo = -1;
247 >        double  d2;
248          int     x, y, mx, my;
249          double  z;
250 <        FVECT   ip, wp;
250 >        FVECT   ip, wp, vd;
251          register int    q;
252 +                                        /* check for dead leaf */
253 +        if (!qtL.chr[li][1] && !(qtL.chr[li][0] | qtL.chr[li][2]))
254 +                return(0);
255                                          /* compute leaf location in view */
256          VCOPY(wp, qtL.wp[li]);
257          viewloc(ip, &odev.v, wp);
258          if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1.
259                          || ip[1] < 0. || ip[1] >= 1.)
260 <                return(0);                      /* behind or outside view */
260 >                goto dropit;                    /* behind or outside view */
261   #ifdef DEBUG
262          if (odev.v.type == VT_PAR | odev.v.vfore > FTINY)
263 <                error(INTERNAL, "bad view assumption in addleaf");
263 >                error(INTERNAL, "bad view assumption in putleaf");
264   #endif
265          for (q = 0; q < 3; q++)
266 <                wp[q] = (wp[q] - odev.v.vp[q])/ip[2];
267 <        encodedir(dc, wp);              /* compute pixel direction */
268 <        d2 = dir2diff(dc, qtL.wd[li]);
266 >                vd[q] = (wp[q] - odev.v.vp[q])/ip[2];
267 >        d2 = fdir2diff(qtL.wd[li], vd);
268 > #ifdef MAXDIFF2
269          if (d2 > MAXDIFF2)
270 <                return(0);                      /* leaf dir. too far off */
270 >                goto dropit;                    /* leaf dir. too far off */
271 > #endif
272          x = ip[0] * odev.hres;
273          y = ip[1] * odev.vres;
274          z = ip[2];
# Line 348 | Line 289 | int    li;
289                  if (!(tp->flgs & LFF(q))) {     /* found stem for leaf */
290                          tp->k[q].li = li;
291                          tp->flgs |= CHLFF(q);
292 <                        break;
292 >                        return(1);
293                  }      
294                  if (lo != tp->k[q].li) {        /* check old leaf */
295                          lo = tp->k[q].li;
# Line 358 | Line 299 | int    li;
299                                                  /* is node minimum size? */
300                  if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) {
301                          if (z > (1.+qtDepthEps)*ip[2])
302 <                                return(0);              /* old one closer */
302 >                                break;                  /* old one closer */
303                          if (z >= (1.-qtDepthEps)*ip[2] &&
304 <                                        dir2diff(dc, qtL.wd[lo]) < d2)
305 <                                return(0);              /* old one better */
306 <                        tp->k[q].li = li;               /* else new one is */
304 >                                        fdir2diff(qtL.wd[lo], vd) < d2)
305 >                                break;                  /* old one better */
306 >                        tp->k[q].li = li;               /* attach new */
307                          tp->flgs |= CHF(q);
308 +                        li = lo;                        /* drop old... */
309                          break;
310                  }
311                  tp->flgs &= ~LFF(q);            /* else grow tree */
# Line 374 | Line 316 | int    li;
316                  my = ip[1] * odev.vres;
317                  if (mx >= (x0 + x1) >> 1) q |= 01;
318                  if (my >= (y0 + y1) >> 1) q |= 02;
319 +                tp->flgs = CH_ANY|LFF(q);       /* all new */
320                  tp->k[q].li = lo;
378                tp->flgs |= LFF(q)|CH_ANY;      /* all new */
321          }
322 <        return(1);              /* done */
322 > dropit:
323 >        if (drop)
324 >                if (li+1 == (qtL.tl ? qtL.tl : qtL.nl))
325 >                        qtL.tl = li;            /* special case */
326 >                else {
327 >                        qtL.chr[li][0] = qtL.chr[li][1] = qtL.chr[li][2] = 0;
328 >                        qtL.wd[li] = falleaves;
329 >                        falleaves = li;
330 >                }
331 >        return(li == lo);
332   }
333  
334  
335 < dev_value(c, p, v)              /* add a pixel value to our quadtree */
335 > dev_value(c, d, p)              /* add a pixel value to our quadtree */
336   COLR    c;
337 < FVECT   p, v;
337 > FVECT   d, p;
338   {
339          register int    li;
340 <
341 <        li = newleaf();
342 <        VCOPY(qtL.wp[li], p);
343 <        encodedir(qtL.wd[li], v);
340 >        int     mapit;
341 >                                /* grab a leaf */
342 >        if (!imm_mode && falleaves >= 0) {      /* check for fallen leaves */
343 >                li = falleaves;
344 >                falleaves = qtL.wd[li];
345 >                mapit = qtL.tml <= qtL.tl ?
346 >                                (li < qtL.tml || li >= qtL.tl) :
347 >                                (li < qtL.tml && li >= qtL.tl) ;
348 >        } else {                                /* else allocate new one */
349 >                li = qtL.tl++;
350 >                if (qtL.tl >= qtL.nl)           /* next leaf in ring */
351 >                        qtL.tl = 0;
352 >                if (qtL.tl == qtL.bl)           /* need to shake some free */
353 >                        qtCompost(LFREEPCT);
354 >                mapit = 0;                      /* we'll map it later */
355 >        }
356 >        if (p == NULL)
357 >                VSUM(qtL.wp[li], odev.v.vp, d, FHUGE);
358 >        else
359 >                VCOPY(qtL.wp[li], p);
360 >        qtL.wd[li] = encodedir(d);
361          tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1);
362 <        if (!addleaf(li))
363 <                ungetleaf(li);
362 >        if (putleaf(li, 1)) {
363 >                if (mapit)
364 >                        tmMapPixels(qtL.rgb+li, qtL.brt+li, qtL.chr+li, 1);
365 >                if (--rayqleft == 0)
366 >                        dev_flush();            /* flush output */
367 >        }
368   }
369  
370  
# Line 405 | Line 377 | qtReplant()                    /* replant our tree using new view */
377          qtFreeTree(0);                  /* blow the old tree away */
378                                          /* regrow it in new place */
379          for (i = qtL.bl; i != qtL.tl; ) {
380 <                addleaf(i);
380 >                putleaf(i, 0);
381                  if (++i >= qtL.nl) i = 0;
382          }
383   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines