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.7 by gregl, Tue Nov 25 11:15:20 1997 UTC vs.
Revision 3.13 by gregl, Wed Dec 24 10:50:49 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  
37 < #define is_stump(t)     (!((t)->flgs & (BR_ANY|LF_ANY)))
37 > #define ungetleaf(li)   (qtL.tl=(li))   /* dangerous if used improperly */
38  
39  
40   static RTREE *
# Line 94 | Line 103 | newleaf()                      /* allocate a leaf from our pile */
103   }
104  
105  
106 < #define LEAFSIZ         (3*sizeof(float)+sizeof(TMbright)+6*sizeof(BYTE))
106 > #define LEAFSIZ         (3*sizeof(float)+sizeof(int4)+\
107 >                        sizeof(TMbright)+6*sizeof(BYTE))
108  
109   int
110   qtAllocLeaves(n)                /* allocate space for n leaves */
# Line 120 | Line 130 | register int   n;
130                  return(0);
131                                  /* assign larger alignment types earlier */
132          qtL.wp = (float (*)[3])qtL.base;
133 <        qtL.brt = (TMbright *)(qtL.wp + n);
133 >        qtL.wd = (int4 *)(qtL.wp + n);
134 >        qtL.brt = (TMbright *)(qtL.wd + n);
135          qtL.chr = (BYTE (*)[3])(qtL.brt + n);
136          qtL.rgb = (BYTE (*)[3])(qtL.chr + n);
137          qtL.nl = n;
# Line 236 | Line 247 | int    li;
247          register RTREE  *tp = &qtrunk;
248          int     x0=0, y0=0, x1=odev.hres, y1=odev.vres;
249          int     lo = -1;
250 +        double  d2;
251          int     x, y, mx, my;
252          double  z;
253 <        FVECT   ip, wp;
253 >        FVECT   ip, wp, vd;
254          register int    q;
255 <                                        /* compute leaf location */
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;
260 >                return(-1);                     /* 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");
264 > #endif
265 >        for (q = 0; q < 3; q++)
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 */
271 > #endif
272          x = ip[0] * odev.hres;
273          y = ip[1] * odev.vres;
274          z = ip[2];
# Line 268 | Line 291 | int    li;
291                          tp->flgs |= CHLFF(q);
292                          break;
293                  }      
294 <                                                /* check existing leaf */
272 <                if (lo != tp->k[q].li) {
294 >                if (lo != tp->k[q].li) {        /* check old leaf */
295                          lo = tp->k[q].li;
296                          VCOPY(wp, qtL.wp[lo]);
297                          viewloc(ip, &odev.v, wp);
298                  }
299                                                  /* is node minimum size? */
300 <                if (x1-x0 <= qtMinNodesiz || y1-y0 <= qtMinNodesiz) {
301 <                        if (z > (1.-qtDepthEps)*ip[2])  /* who is closer? */
302 <                                return;                 /* old one is */
303 <                        tp->k[q].li = li;               /* new one is */
300 >                if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) {
301 >                        if (z > (1.+qtDepthEps)*ip[2])
302 >                                return(0);              /* old one closer */
303 >                        if (z >= (1.-qtDepthEps)*ip[2] &&
304 >                                        fdir2diff(qtL.wd[lo], vd) < d2)
305 >                                return(0);              /* old one better */
306 >                        tp->k[q].li = li;               /* else new one is */
307                          tp->flgs |= CHF(q);
308                          break;
309                  }
# Line 290 | Line 315 | int    li;
315                  my = ip[1] * odev.vres;
316                  if (mx >= (x0 + x1) >> 1) q |= 01;
317                  if (my >= (y0 + y1) >> 1) q |= 02;
318 +                tp->flgs = CH_ANY|LFF(q);       /* all new */
319                  tp->k[q].li = lo;
294                tp->flgs |= LFF(q)|CH_ANY;      /* all new */
320          }
321 +        return(1);              /* done */
322   }
323  
324  
325 < dev_value(c, p)                 /* add a pixel value to our output queue */
325 > dev_value(c, p, v)              /* add a pixel value to our quadtree */
326   COLR    c;
327 < FVECT   p;
327 > FVECT   p, v;
328   {
329          register int    li;
330  
331          li = newleaf();
332          VCOPY(qtL.wp[li], p);
333 +        qtL.wd[li] = encodedir(v);
334          tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1);
335 <        addleaf(li);
335 >        if (!addleaf(li))
336 >                ungetleaf(li);
337   }
338  
339  
# Line 361 | Line 389 | int    redo;
389                                  qtL.chr+borg, blen);
390          qtL.tml = qtL.tl;
391          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 & CHF(i)))
416                        continue;
417                if (tp->flgs & LFF(i)) {
418                        dev_paintr(cp=qtL.rgb[tp->k[i].li],
419                                        i&01 ? mx : x0, i&02 ? my : y0,
420                                        i&01 ? x1 : mx, i&02 ? y1 : my);
421                        csm[0] += cp[0]; csm[1] += cp[1]; csm[2] += cp[2];
422                        nc++;
423                } else if (!(tp->flgs & BRF(i)))
424                        gaps |= 1<<i;   /* empty stem */
425        }
426                                        /* now do branches */
427        for (i = 0; i < 4; i++)
428                if ((tp->flgs & CHBRF(i)) == CHBRF(i)) {
429                        update(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0,
430                                        i&01 ? x1 : mx, i&02 ? y1 : my);
431                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
432                        nc++;
433                }
434        if (nc > 1) {
435                ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc;
436        } else {
437                ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2];
438        }
439                                        /* fill in gaps with average */
440        for (i = 0; gaps && i < 4; gaps >>= 1, i++)
441                if (gaps & 01)
442                        dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0,
443                                        i&01 ? x1 : mx, i&02 ? y1 : my);
444        tp->flgs &= ~CH_ANY;            /* all done */
445 }
446
447
448 qtRedraw(x0, y0, x1, y1)        /* redraw part or all of our screen */
449 int     x0, y0, x1, y1;
450 {
451        int     lim[2][2];
452
453        if (is_stump(&qtrunk))
454                return;
455        if (!qtMapLeaves((lim[0][0]=x0) <= 0 & (lim[1][0]=y0) <= 0 &
456                (lim[0][1]=x1) >= odev.hres-1 & (lim[1][1]=y1) >= odev.vres-1))
457                return;
458        redraw(&qtrunk, 0, 0, odev.hres, odev.vres, lim);
459 }
460
461
462 qtUpdate()                      /* update our tree display */
463 {
464        BYTE    ca[3];
465
466        if (is_stump(&qtrunk))
467                return;
468        if (!qtMapLeaves(0))
469                return;
470        update(ca, &qtrunk, 0, 0, odev.hres, odev.vres);
392   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines