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.8 by gregl, Tue Nov 25 14:24:39 1997 UTC vs.
Revision 3.14 by gregl, Mon Dec 29 17:31:45 1997 UTC

# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ SGI";
14   #ifndef LFREEPCT
15   #define LFREEPCT        25
16   #endif
17 +                                /* maximum allowed angle difference (deg.) */
18 + #ifndef MAXANG
19 + #define MAXANG          20
20 + #endif
21 + #if MAXANG>0
22 + #define MAXDIFF2        ( MAXANG*MAXANG * (PI*PI/180./180.))
23 + #endif
24  
25 + #define abs(i)          ((i) < 0 ? -(i) : (i))
26 +
27   RTREE   qtrunk;                 /* our quadtree trunk */
28 < double  qtDepthEps = .02;       /* epsilon to compare depths (z fraction) */
28 > double  qtDepthEps = .05;       /* epsilon to compare depths (z fraction) */
29   int     qtMinNodesiz = 2;       /* minimum node dimension (pixels) */
30   struct rleaves  qtL;            /* our pile of leaves */
31  
# Line 25 | Line 34 | struct rleaves qtL;            /* our pile of leaves */
34   static RTREE    **twigbundle;   /* free twig blocks (NULL term.) */
35   static int      nexttwig;       /* next free twig */
36  
28 #define is_stump(t)     (!((t)->flgs & (BR_ANY|LF_ANY)))
37  
30
38   static RTREE *
39   newtwig()                       /* allocate a twig */
40   {
# Line 80 | Line 87 | int    really;
87   }
88  
89  
90 < static int
91 < newleaf()                       /* allocate a leaf from our pile */
85 < {
86 <        int     li;
87 <        
88 <        li = qtL.tl++;
89 <        if (qtL.tl >= qtL.nl)   /* get next leaf in ring */
90 <                qtL.tl = 0;
91 <        if (qtL.tl == qtL.bl)   /* need to shake some free */
92 <                qtCompost(LFREEPCT);
93 <        return(li);
94 < }
90 > #define LEAFSIZ         (3*sizeof(float)+sizeof(int4)+\
91 >                        sizeof(TMbright)+6*sizeof(BYTE))
92  
96
97 #define LEAFSIZ         (3*sizeof(float)+sizeof(TMbright)+6*sizeof(BYTE))
98
93   int
94   qtAllocLeaves(n)                /* allocate space for n leaves */
95   register int    n;
# Line 120 | Line 114 | register int   n;
114                  return(0);
115                                  /* assign larger alignment types earlier */
116          qtL.wp = (float (*)[3])qtL.base;
117 <        qtL.brt = (TMbright *)(qtL.wp + n);
117 >        qtL.wd = (int4 *)(qtL.wp + n);
118 >        qtL.brt = (TMbright *)(qtL.wd + n);
119          qtL.chr = (BYTE (*)[3])(qtL.brt + n);
120          qtL.rgb = (BYTE (*)[3])(qtL.chr + n);
121          qtL.nl = n;
# Line 236 | Line 231 | int    li;
231          register RTREE  *tp = &qtrunk;
232          int     x0=0, y0=0, x1=odev.hres, y1=odev.vres;
233          int     lo = -1;
234 +        double  d2;
235          int     x, y, mx, my;
236          double  z;
237 <        FVECT   ip, wp;
237 >        FVECT   ip, wp, vd;
238          register int    q;
239 <                                        /* compute leaf location */
239 >                                        /* compute leaf location in view */
240          VCOPY(wp, qtL.wp[li]);
241          viewloc(ip, &odev.v, wp);
242          if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1.
243                          || ip[1] < 0. || ip[1] >= 1.)
244 <                return;
244 >                return(-1);                     /* behind or outside view */
245 > #ifdef DEBUG
246 >        if (odev.v.type == VT_PAR | odev.v.vfore > FTINY)
247 >                error(INTERNAL, "bad view assumption in addleaf");
248 > #endif
249 >        for (q = 0; q < 3; q++)
250 >                vd[q] = (wp[q] - odev.v.vp[q])/ip[2];
251 >        d2 = fdir2diff(qtL.wd[li], vd);
252 > #ifdef MAXDIFF2
253 >        if (d2 > MAXDIFF2)
254 >                return(0);                      /* leaf dir. too far off */
255 > #endif
256          x = ip[0] * odev.hres;
257          y = ip[1] * odev.vres;
258          z = ip[2];
# Line 268 | Line 275 | int    li;
275                          tp->flgs |= CHLFF(q);
276                          break;
277                  }      
278 <                                                /* check existing leaf */
272 <                if (lo != tp->k[q].li) {
278 >                if (lo != tp->k[q].li) {        /* check old leaf */
279                          lo = tp->k[q].li;
280                          VCOPY(wp, qtL.wp[lo]);
281                          viewloc(ip, &odev.v, wp);
282                  }
283                                                  /* is node minimum size? */
284 <                if (x1-x0 <= qtMinNodesiz || y1-y0 <= qtMinNodesiz) {
285 <                        if (z > (1.-qtDepthEps)*ip[2])  /* who is closer? */
286 <                                return;                 /* old one is */
287 <                        tp->k[q].li = li;               /* new one is */
284 >                if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) {
285 >                        if (z > (1.+qtDepthEps)*ip[2])
286 >                                return(0);              /* old one closer */
287 >                        if (z >= (1.-qtDepthEps)*ip[2] &&
288 >                                        fdir2diff(qtL.wd[lo], vd) < d2)
289 >                                return(0);              /* old one better */
290 >                        tp->k[q].li = li;               /* else new one is */
291                          tp->flgs |= CHF(q);
292                          break;
293                  }
# Line 290 | Line 299 | int    li;
299                  my = ip[1] * odev.vres;
300                  if (mx >= (x0 + x1) >> 1) q |= 01;
301                  if (my >= (y0 + y1) >> 1) q |= 02;
302 +                tp->flgs = CH_ANY|LFF(q);       /* all new */
303                  tp->k[q].li = lo;
294                tp->flgs |= LFF(q)|CH_ANY;      /* all new */
304          }
305 +        return(1);              /* done */
306   }
307  
308  
309 < dev_value(c, p)                 /* add a pixel value to our output queue */
309 > dev_value(c, p, v)              /* add a pixel value to our quadtree */
310   COLR    c;
311 < FVECT   p;
311 > FVECT   p, v;
312   {
313          register int    li;
314  
315 <        li = newleaf();
315 >        li = qtL.tl++;
316 >        if (qtL.tl >= qtL.nl)   /* advance to next leaf in ring */
317 >                qtL.tl = 0;
318 >        if (qtL.tl == qtL.bl)   /* need to shake some free */
319 >                qtCompost(LFREEPCT);
320          VCOPY(qtL.wp[li], p);
321 +        qtL.wd[li] = encodedir(v);
322          tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1);
323 <        addleaf(li);
323 >        if (!addleaf(li))
324 >                qtL.tl = li;    /* unget this leaf */
325   }
326  
327  
# Line 361 | Line 377 | int    redo;
377                                  qtL.chr+borg, blen);
378          qtL.tml = qtL.tl;
379          return(1);
364 }
365
366
367 static
368 redraw(tp, x0, y0, x1, y1, l)   /* mark portion of a tree for redraw */
369 register RTREE  *tp;
370 int     x0, y0, x1, y1;
371 int     l[2][2];
372 {
373        int     quads = CH_ANY;
374        int     mx, my;
375        register int    i;
376                                        /* compute midpoint */
377        mx = (x0 + x1) >> 1;
378        my = (y0 + y1) >> 1;
379                                        /* see what to do */
380        if (l[0][0] >= mx)
381                quads &= ~(CHF(2)|CHF(0));
382        else if (l[0][1] < mx)
383                quads &= ~(CHF(3)|CHF(1));
384        if (l[1][0] >= my)
385                quads &= ~(CHF(1)|CHF(0));
386        else if (l[1][1] < my)
387                quads &= ~(CHF(3)|CHF(2));
388        tp->flgs |= quads;              /* mark quadrants for update */
389                                        /* climb the branches */
390        for (i = 0; i < 4; i++)
391                if (tp->flgs & BRF(i) && quads & CHF(i))
392                        redraw(tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0,
393                                        i&01 ? x1 : mx, i&02 ? y1 : my, l);
394 }
395
396
397 static
398 update(ca, tp, x0, y0, x1, y1)  /* update tree display as needed */
399 BYTE    ca[3];          /* returned average color */
400 register RTREE  *tp;
401 int     x0, y0, x1, y1;
402 {
403        int     csm[3], nc;
404        register BYTE   *cp;
405        BYTE    rgb[3];
406        int     gaps = 0;
407        int     mx, my;
408        register int    i;
409                                        /* compute midpoint */
410        mx = (x0 + x1) >> 1;
411        my = (y0 + y1) >> 1;
412        csm[0] = csm[1] = csm[2] = nc = 0;
413                                        /* do leaves first */
414        for (i = 0; i < 4; i++) {
415                if (tp->flgs & LFF(i)) {
416                        cp = qtL.rgb[tp->k[i].li];
417                        csm[0] += cp[0]; csm[1] += cp[1]; csm[2] += cp[2];
418                        nc++;
419                        if (tp->flgs & CHF(i))
420                                dev_paintr(cp, i&01 ? mx : x0, i&02 ? my : y0,
421                                               i&01 ? x1 : mx, i&02 ? y1 : my);
422                } else if ((tp->flgs & CHBRF(i)) == CHF(i))
423                        gaps |= 1<<i;   /* empty stem */
424        }
425                                        /* now do branches */
426        for (i = 0; i < 4; i++)
427                if ((tp->flgs & CHBRF(i)) == CHBRF(i)) {
428                        update(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0,
429                                        i&01 ? x1 : mx, i&02 ? y1 : my);
430                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
431                        nc++;
432                }
433        if (nc > 1) {
434                ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc;
435        } else {
436                ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2];
437        }
438                                        /* fill in gaps with average */
439        for (i = 0; gaps && i < 4; gaps >>= 1, i++)
440                if (gaps & 01)
441                        dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0,
442                                        i&01 ? x1 : mx, i&02 ? y1 : my);
443        tp->flgs &= ~CH_ANY;            /* all done */
444 }
445
446
447 qtRedraw(x0, y0, x1, y1)        /* redraw part or all of our screen */
448 int     x0, y0, x1, y1;
449 {
450        int     lim[2][2];
451
452        if (is_stump(&qtrunk))
453                return;
454        if (!qtMapLeaves((lim[0][0]=x0) <= 0 & (lim[1][0]=y0) <= 0 &
455                (lim[0][1]=x1) >= odev.hres-1 & (lim[1][1]=y1) >= odev.vres-1))
456                return;
457        redraw(&qtrunk, 0, 0, odev.hres, odev.vres, lim);
458 }
459
460
461 qtUpdate()                      /* update our tree display */
462 {
463        BYTE    ca[3];
464
465        if (is_stump(&qtrunk))
466                return;
467        if (!qtMapLeaves(0))
468                return;
469        update(ca, &qtrunk, 0, 0, odev.hres, odev.vres);
380   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines