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.20 by greg, Tue Mar 4 05:49:21 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Quadtree driver support routines.
6   */
# Line 16 | Line 13 | static char SCCSid[] = "$SunId$ SGI";
13   #endif
14                                  /* maximum allowed angle difference (deg.) */
15   #ifndef MAXANG
16 < #define MAXANG          20.
16 > #define MAXANG          20
17   #endif
18 + #if MAXANG>0
19 + #define MAXDIFF2        ( MAXANG*MAXANG * (PI*PI/180./180.))
20 + #endif
21  
22 #define MAXDIFF2        (long)( MAXANG*MAXANG /90./90.*(1L<<15)*(1L<<15))
23
22   #define abs(i)          ((i) < 0 ? -(i) : (i))
23  
24   RTREE   qtrunk;                 /* our quadtree trunk */
# Line 28 | Line 26 | double qtDepthEps = .05;       /* epsilon to compare depths
26   int     qtMinNodesiz = 2;       /* minimum node dimension (pixels) */
27   struct rleaves  qtL;            /* our pile of leaves */
28  
29 + int     rayqleft = 0;           /* rays left to queue before flush */
30 +
31 + static int4     falleaves;      /* our list of fallen leaves */
32 +
33 + #define composted(li)   (qtL.bl <= qtL.tl ? \
34 +                                        ((li) < qtL.bl || (li) >= qtL.tl) : \
35 +                                        ((li) < qtL.bl && (li) >= qtL.tl))
36 +
37   #define TBUNDLESIZ      409     /* number of twigs in a bundle */
38  
39   static RTREE    **twigbundle;   /* free twig blocks (NULL term.) */
40   static int      nexttwig;       /* next free twig */
41  
36 #define ungetleaf(li)   (qtL.tl=(li))   /* dangerous if used improperly */
42  
38
43   static RTREE *
44   newtwig()                       /* allocate a twig */
45   {
# Line 82 | Line 86 | int    really;
86          }
87                                  /* else "really" means free up memory */
88          for (i = 0; twigbundle[i] != NULL; i++)
89 <                free((char *)twigbundle[i]);
90 <        free((char *)twigbundle);
89 >                free((void *)twigbundle[i]);
90 >        free((void *)twigbundle);
91          twigbundle = NULL;
92   }
93  
94  
95 < 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)+\
95 > #define LEAFSIZ         (3*sizeof(float)+sizeof(int4)+\
96                          sizeof(TMbright)+6*sizeof(BYTE))
97  
98   int
# Line 129 | Line 119 | register int   n;
119                  return(0);
120                                  /* assign larger alignment types earlier */
121          qtL.wp = (float (*)[3])qtL.base;
122 <        qtL.wd = (short (*)[2])(qtL.wp + n);
122 >        qtL.wd = (int4 *)(qtL.wp + n);
123          qtL.brt = (TMbright *)(qtL.wd + n);
124          qtL.chr = (BYTE (*)[3])(qtL.brt + n);
125          qtL.rgb = (BYTE (*)[3])(qtL.chr + n);
126          qtL.nl = n;
127          qtL.tml = qtL.bl = qtL.tl = 0;
128 +        falleaves = -1;
129          return(n);
130   }
131  
# Line 165 | Line 156 | register RTREE *tp;
156                                  tp->flgs &= ~BRF(i);
157                  } else if (tp->flgs & LFF(i)) {
158                          li = tp->k[i].li;
159 <                        if (qtL.bl < qtL.tl ?
169 <                                (li < qtL.bl || li >= qtL.tl) :
170 <                                (li < qtL.bl && li >= qtL.tl))
159 >                        if (composted(li))
160                                  tp->flgs &= ~LFF(i);
161                  }
162   }
# Line 177 | Line 166 | int
166   qtCompost(pct)                  /* free up some leaves */
167   int     pct;
168   {
169 +        register int4   *fl;
170          int     nused, nclear, nmapped;
181
171                                  /* figure out how many leaves to clear */
172          nclear = qtL.nl * pct / 100;
173          nused = qtL.tl - qtL.bl;
# Line 189 | Line 178 | int    pct;
178          if (nclear >= nused) {  /* clear them all */
179                  qtFreeTree(0);
180                  qtL.tml = qtL.bl = qtL.tl = 0;
181 +                falleaves = -1;
182                  return(nused);
183          }
184                                  /* else clear leaves from bottom */
# Line 197 | Line 187 | int    pct;
187          qtL.bl += nclear;
188          if (qtL.bl >= qtL.nl) qtL.bl -= qtL.nl;
189          if (nmapped <= nclear) qtL.tml = qtL.bl;
190 <        shaketree(&qtrunk);
190 >        shaketree(&qtrunk);     /* dereference composted leaves */
191 >        for (fl = &falleaves; *fl >= 0; fl = qtL.wd + *fl)
192 >                while (composted(*fl))
193 >                        if ((*fl = qtL.wd[*fl]) < 0)
194 >                                return(nclear);
195          return(nclear);
196   }
197  
198  
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
199   int
200   qtFindLeaf(x, y)                /* find closest leaf to (x,y) */
201   int     x, y;
# Line 300 | Line 234 | int    x, y;
234  
235  
236   static
237 < addleaf(li)                     /* add a leaf to our tree */
238 < int     li;
237 > putleaf(li, drop)               /* put a leaf in our tree */
238 > register int    li;
239 > int     drop;
240   {
241          register RTREE  *tp = &qtrunk;
242          int     x0=0, y0=0, x1=odev.hres, y1=odev.vres;
243 <        int     lo = -1;
244 <        long    d2;
310 <        short   dc[2];
243 >        register int    lo = -1;
244 >        double  d2;
245          int     x, y, mx, my;
246          double  z;
247 <        FVECT   ip, wp;
247 >        FVECT   ip, wp, vd;
248          register int    q;
249 +                                        /* check for dead leaf */
250 +        if (!qtL.chr[li][1] && !(qtL.chr[li][0] | qtL.chr[li][2]))
251 +                return(0);
252                                          /* compute leaf location in view */
253          VCOPY(wp, qtL.wp[li]);
254          viewloc(ip, &odev.v, wp);
255          if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1.
256                          || ip[1] < 0. || ip[1] >= 1.)
257 <                return(0);                      /* behind or outside view */
257 >                goto dropit;                    /* behind or outside view */
258   #ifdef DEBUG
259          if (odev.v.type == VT_PAR | odev.v.vfore > FTINY)
260 <                error(INTERNAL, "bad view assumption in addleaf");
260 >                error(INTERNAL, "bad view assumption in putleaf");
261   #endif
262          for (q = 0; q < 3; q++)
263 <                wp[q] = (wp[q] - odev.v.vp[q])/ip[2];
264 <        encodedir(dc, wp);              /* compute pixel direction */
265 <        d2 = dir2diff(dc, qtL.wd[li]);
263 >                vd[q] = (wp[q] - odev.v.vp[q])/ip[2];
264 >        d2 = fdir2diff(qtL.wd[li], vd);
265 > #ifdef MAXDIFF2
266          if (d2 > MAXDIFF2)
267 <                return(0);                      /* leaf dir. too far off */
267 >                goto dropit;                    /* leaf dir. too far off */
268 > #endif
269          x = ip[0] * odev.hres;
270          y = ip[1] * odev.vres;
271          z = ip[2];
# Line 348 | Line 286 | int    li;
286                  if (!(tp->flgs & LFF(q))) {     /* found stem for leaf */
287                          tp->k[q].li = li;
288                          tp->flgs |= CHLFF(q);
289 <                        break;
289 >                        return(1);
290                  }      
291                  if (lo != tp->k[q].li) {        /* check old leaf */
292                          lo = tp->k[q].li;
# Line 358 | Line 296 | int    li;
296                                                  /* is node minimum size? */
297                  if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) {
298                          if (z > (1.+qtDepthEps)*ip[2])
299 <                                return(0);              /* old one closer */
299 >                                break;                  /* old one closer */
300                          if (z >= (1.-qtDepthEps)*ip[2] &&
301 <                                        dir2diff(dc, qtL.wd[lo]) < d2)
302 <                                return(0);              /* old one better */
303 <                        tp->k[q].li = li;               /* else new one is */
301 >                                        fdir2diff(qtL.wd[lo], vd) < d2)
302 >                                break;                  /* old one better */
303 >                        tp->k[q].li = li;               /* attach new */
304                          tp->flgs |= CHF(q);
305 +                        li = lo;                        /* drop old... */
306                          break;
307                  }
308                  tp->flgs &= ~LFF(q);            /* else grow tree */
# Line 374 | Line 313 | int    li;
313                  my = ip[1] * odev.vres;
314                  if (mx >= (x0 + x1) >> 1) q |= 01;
315                  if (my >= (y0 + y1) >> 1) q |= 02;
316 +                tp->flgs = CH_ANY|LFF(q);       /* all new */
317                  tp->k[q].li = lo;
378                tp->flgs |= LFF(q)|CH_ANY;      /* all new */
318          }
319 <        return(1);              /* done */
319 > dropit:
320 >        if (drop)
321 >                if (li+1 == (qtL.tl ? qtL.tl : qtL.nl))
322 >                        qtL.tl = li;            /* special case */
323 >                else {
324 >                        qtL.chr[li][0] = qtL.chr[li][1] = qtL.chr[li][2] = 0;
325 >                        qtL.wd[li] = falleaves;
326 >                        falleaves = li;
327 >                }
328 >        return(li == lo);
329   }
330  
331  
332 < dev_value(c, p, v)              /* add a pixel value to our quadtree */
332 > dev_value(c, d, p)              /* add a pixel value to our quadtree */
333   COLR    c;
334 < FVECT   p, v;
334 > FVECT   d, p;
335   {
336          register int    li;
337 <
338 <        li = newleaf();
339 <        VCOPY(qtL.wp[li], p);
340 <        encodedir(qtL.wd[li], v);
341 <        tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1);
342 <        if (!addleaf(li))
343 <                ungetleaf(li);
337 >        int     mapit;
338 >                                /* grab a leaf */
339 >        if (!imm_mode && falleaves >= 0) {      /* check for fallen leaves */
340 >                li = falleaves;
341 >                falleaves = qtL.wd[li];
342 >                mapit = qtL.tml <= qtL.tl ?
343 >                                (li < qtL.tml || li >= qtL.tl) :
344 >                                (li < qtL.tml && li >= qtL.tl) ;
345 >        } else {                                /* else allocate new one */
346 >                li = qtL.tl++;
347 >                if (qtL.tl >= qtL.nl)           /* next leaf in ring */
348 >                        qtL.tl = 0;
349 >                if (qtL.tl == qtL.bl)           /* need to shake some free */
350 >                        qtCompost(LFREEPCT);
351 >                mapit = 0;                      /* we'll map it later */
352 >        }
353 >        if (p == NULL)
354 >                VSUM(qtL.wp[li], odev.v.vp, d, FHUGE);
355 >        else
356 >                VCOPY(qtL.wp[li], p);
357 >        qtL.wd[li] = encodedir(d);
358 >        tmCvColrs(&qtL.brt[li], qtL.chr[li], (COLR *)c, 1);
359 >        if (putleaf(li, 1)) {
360 >                if (mapit)
361 >                        tmMapPixels((BYTE *)(qtL.rgb+li), qtL.brt+li,
362 >                                        (BYTE *)(qtL.chr+li), 1);
363 >                if (--rayqleft == 0)
364 >                        dev_flush();            /* flush output */
365 >        }
366   }
367  
368  
# Line 405 | Line 375 | qtReplant()                    /* replant our tree using new view */
375          qtFreeTree(0);                  /* blow the old tree away */
376                                          /* regrow it in new place */
377          for (i = qtL.bl; i != qtL.tl; ) {
378 <                addleaf(i);
378 >                putleaf(i, 0);
379                  if (++i >= qtL.nl) i = 0;
380          }
381   }
# Line 440 | Line 410 | int    redo;
410                  if (tmComputeMapping(0., 0., 0.) != TM_E_OK)
411                          return(0);
412          }
413 <        if (tmMapPixels(qtL.rgb+aorg, qtL.brt+aorg,
414 <                        qtL.chr+aorg, alen) != TM_E_OK)
413 >        if (tmMapPixels((BYTE *)(qtL.rgb+aorg), qtL.brt+aorg,
414 >                        (BYTE *)(qtL.chr+aorg), alen) != TM_E_OK)
415                  return(0);
416          if (blen > 0)
417 <                tmMapPixels(qtL.rgb+borg, qtL.brt+borg,
418 <                                qtL.chr+borg, blen);
417 >                tmMapPixels((BYTE *)(qtL.rgb+borg), qtL.brt+borg,
418 >                                (BYTE *)(qtL.chr+borg), blen);
419          qtL.tml = qtL.tl;
420          return(1);
421   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines