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.6 by gregl, Mon Nov 24 15:16:10 1997 UTC vs.
Revision 3.22 by greg, Fri Jun 20 00:25:49 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 14 | Line 11 | static char SCCSid[] = "$SunId$ SGI";
11   #ifndef LFREEPCT
12   #define LFREEPCT        25
13   #endif
14 +                                /* maximum allowed angle difference (deg.) */
15 + #ifndef MAXANG
16 + #define MAXANG          20
17 + #endif
18 + #if MAXANG>0
19 + #define MAXDIFF2        ( MAXANG*MAXANG * (PI*PI/180./180.))
20 + #endif
21  
22 + #define abs(i)          ((i) < 0 ? -(i) : (i))
23 +
24   RTREE   qtrunk;                 /* our quadtree trunk */
25 < double  qtDepthEps = .02;       /* epsilon to compare depths (z fraction) */
25 > double  qtDepthEps = .05;       /* epsilon to compare depths (z fraction) */
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 int32    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  
28 #define is_stump(t)     (!((t)->flgs & (BR_ANY|LF_ANY)))
42  
30
43   static RTREE *
44   newtwig()                       /* allocate a twig */
45   {
# Line 41 | Line 53 | newtwig()                      /* allocate a twig */
53          }
54          bi = nexttwig / TBUNDLESIZ;
55          if (twigbundle[bi] == NULL) {   /* new block */
56 <                twigbundle = (RTREE **)realloc((char *)twigbundle,
56 >                twigbundle = (RTREE **)realloc((void *)twigbundle,
57                                          (bi+2)*sizeof(RTREE *));
58                  if (twigbundle == NULL)
59                          goto memerr;
# Line 62 | Line 74 | int    really;
74   {
75          register int    i;
76  
77 <        qtrunk.flgs = CH_ANY;
66 <        nexttwig = 0;
77 >        qtrunk.flgs = CH_ANY;   /* chop down tree */
78          if (twigbundle == NULL)
79                  return;
80 +        i = (TBUNDLESIZ-1+nexttwig)/TBUNDLESIZ;
81 +        nexttwig = 0;
82          if (!really) {          /* just clear allocated blocks */
83 <                for (i = 0; twigbundle[i] != NULL; i++)
83 >                while (i--)
84                          bzero((char *)twigbundle[i], TBUNDLESIZ*sizeof(RTREE));
85                  return;
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
96 < newleaf()                       /* allocate a leaf from our pile */
84 < {
85 <        int     li;
86 <        
87 <        li = qtL.tl++;
88 <        if (qtL.tl >= qtL.nl)   /* get next leaf in ring */
89 <                qtL.tl = 0;
90 <        if (qtL.tl == qtL.bl)   /* need to shake some free */
91 <                qtCompost(LFREEPCT);
92 <        return(li);
93 < }
95 > #define LEAFSIZ         (3*sizeof(float)+sizeof(int32)+\
96 >                        sizeof(TMbright)+6*sizeof(BYTE))
97  
95
96 #define LEAFSIZ         (3*sizeof(float)+sizeof(TMbright)+6*sizeof(BYTE))
97
98   int
99   qtAllocLeaves(n)                /* allocate space for n leaves */
100   register int    n;
# Line 119 | Line 119 | register int   n;
119                  return(0);
120                                  /* assign larger alignment types earlier */
121          qtL.wp = (float (*)[3])qtL.base;
122 <        qtL.brt = (TMbright *)(qtL.wp + n);
122 >        qtL.wd = (int32 *)(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 154 | 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 ?
158 <                                (li < qtL.bl || li >= qtL.tl) :
159 <                                (li < qtL.bl && li >= qtL.tl))
159 >                        if (composted(li))
160                                  tp->flgs &= ~LFF(i);
161                  }
162   }
# Line 166 | Line 166 | int
166   qtCompost(pct)                  /* free up some leaves */
167   int     pct;
168   {
169 +        register int32  *fl;
170          int     nused, nclear, nmapped;
170
171                                  /* figure out how many leaves to clear */
172          nclear = qtL.nl * pct / 100;
173          nused = qtL.tl - qtL.bl;
# Line 178 | 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 186 | 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  
# Line 229 | 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;
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 <                                        /* compute leaf location */
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;
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 putleaf");
261 > #endif
262 >        for (q = 0; q < 3; q++)
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 >                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 265 | 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 <                                                /* check existing leaf */
271 <                if (lo != tp->k[q].li) {
291 >                if (lo != tp->k[q].li) {        /* check old leaf */
292                          lo = tp->k[q].li;
293                          VCOPY(wp, qtL.wp[lo]);
294                          viewloc(ip, &odev.v, wp);
295                  }
296                                                  /* is node minimum size? */
297 <                if (x1-x0 <= qtMinNodesiz || y1-y0 <= qtMinNodesiz) {
298 <                        if (z > (1.-qtDepthEps)*ip[2])  /* who is closer? */
299 <                                return;                 /* old one is */
300 <                        tp->k[q].li = li;               /* new one is */
297 >                if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) {
298 >                        if (z > (1.+qtDepthEps)*ip[2])
299 >                                break;                  /* old one closer */
300 >                        if (z >= (1.-qtDepthEps)*ip[2] &&
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 289 | 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;
293                tp->flgs |= LFF(q)|CH_ANY;      /* all new */
318          }
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)                 /* add a pixel value to our output queue */
332 > dev_value(c, d, p)              /* add a pixel value to our quadtree */
333   COLR    c;
334 < FVECT   p;
334 > FVECT   d, p;
335   {
336          register int    li;
337 <
338 <        li = newleaf();
339 <        VCOPY(qtL.wp[li], p);
340 <        tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1);
341 <        addleaf(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 317 | 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 352 | 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);
363 }
364
365
366 static
367 redraw(ca, tp, x0, y0, x1, y1, l)       /* redraw portion of a tree */
368 BYTE    ca[3];          /* returned average color */
369 register RTREE  *tp;
370 int     x0, y0, x1, y1;
371 int     l[2][2];
372 {
373        int     csm[3], nc;
374        register BYTE   *cp;
375        BYTE    rgb[3];
376        int     quads = CH_ANY;
377        int     mx, my;
378        register int    i;
379                                        /* compute midpoint */
380        mx = (x0 + x1) >> 1;
381        my = (y0 + y1) >> 1;
382                                        /* see what to do */
383        if (l[0][0] >= mx)
384                quads &= ~(CHF(2)|CHF(0));
385        else if (l[0][1] <= mx)
386                quads &= ~(CHF(3)|CHF(1));
387        if (l[1][0] >= my)
388                quads &= ~(CHF(1)|CHF(0));
389        else if (l[1][1] <= my)
390                quads &= ~(CHF(3)|CHF(2));
391        tp->flgs &= ~quads;             /* mark them done */
392        csm[0] = csm[1] = csm[2] = nc = 0;
393                                        /* do leaves first */
394        for (i = 0; i < 4; i++)
395                if (quads & CHF(i) && tp->flgs & LFF(i)) {
396                        dev_paintr(cp=qtL.rgb[tp->k[i].li],
397                                        i&01 ? mx : x0, i&02 ? my : y0,
398                                        i&01 ? x1 : mx, i&02 ? y1 : my);
399                        csm[0] += cp[0]; csm[1] += cp[1]; csm[2] += cp[2];
400                        nc++;
401                        quads &= ~CHF(i);
402                }
403                                        /* now do branches */
404        for (i = 0; i < 4; i++)
405                if (quads & CHF(i) && tp->flgs & BRF(i)) {
406                        redraw(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0,
407                                        i&01 ? x1 : mx, i&02 ? y1 : my, l);
408                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
409                        nc++;
410                        quads &= ~CHF(i);
411                }
412        if (nc > 1) {
413                ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc;
414        } else {
415                ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2];
416        }
417        if (!quads) return;
418                                        /* fill in gaps with average */
419        for (i = 0; i < 4; i++)
420                if (quads & CHF(i))
421                        dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0,
422                                        i&01 ? x1 : mx, i&02 ? y1 : my);
423 }
424
425
426 static
427 update(ca, tp, x0, y0, x1, y1)  /* update tree display as needed */
428 BYTE    ca[3];          /* returned average color */
429 register RTREE  *tp;
430 int     x0, y0, x1, y1;
431 {
432        int     csm[3], nc;
433        register BYTE   *cp;
434        BYTE    rgb[3];
435        int     gaps = 0;
436        int     mx, my;
437        register int    i;
438                                        /* compute midpoint */
439        mx = (x0 + x1) >> 1;
440        my = (y0 + y1) >> 1;
441        csm[0] = csm[1] = csm[2] = nc = 0;
442                                        /* do leaves first */
443        for (i = 0; i < 4; i++) {
444                if (!(tp->flgs & CHF(i)))
445                        continue;
446                if (tp->flgs & LFF(i)) {
447                        dev_paintr(cp=qtL.rgb[tp->k[i].li],
448                                        i&01 ? mx : x0, i&02 ? my : y0,
449                                        i&01 ? x1 : mx, i&02 ? y1 : my);
450                        csm[0] += cp[0]; csm[1] += cp[1]; csm[2] += cp[2];
451                        nc++;
452                } else if (!(tp->flgs & BRF(i)))
453                        gaps |= 1<<i;   /* empty stem */
454        }
455                                        /* now do branches */
456        for (i = 0; i < 4; i++)
457                if ((tp->flgs & CHBRF(i)) == CHBRF(i)) {
458                        update(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0,
459                                        i&01 ? x1 : mx, i&02 ? y1 : my);
460                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
461                        nc++;
462                }
463        if (nc > 1) {
464                ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc;
465        } else {
466                ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2];
467        }
468                                        /* fill in gaps with average */
469        for (i = 0; gaps && i < 4; gaps >>= 1, i++)
470                if (gaps & 01)
471                        dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0,
472                                        i&01 ? x1 : mx, i&02 ? y1 : my);
473        tp->flgs &= ~CH_ANY;            /* all done */
474 }
475
476
477 qtRedraw(x0, y0, x1, y1)        /* redraw part or all of our screen */
478 int     x0, y0, x1, y1;
479 {
480        int     lim[2][2];
481        BYTE    ca[3];
482
483        if (is_stump(&qtrunk))
484                return;
485        if (!qtMapLeaves((lim[0][0]=x0) <= 0 & (lim[1][0]=y0) <= 0 &
486                (lim[0][1]=x1) >= odev.hres-1 & (lim[1][1]=y1) >= odev.vres-1))
487                return;
488        redraw(ca, &qtrunk, 0, 0, odev.hres, odev.vres, lim);
489 }
490
491
492 qtUpdate()                      /* update our tree display */
493 {
494        BYTE    ca[3];
495
496        if (is_stump(&qtrunk))
497                return;
498        if (!qtMapLeaves(0))
499                return;
500        update(ca, &qtrunk, 0, 0, odev.hres, odev.vres);
421   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines