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.5 by gregl, Fri Nov 21 13:35:57 1997 UTC vs.
Revision 3.10 by gregl, Fri Dec 5 09:40:05 1997 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ SGI";
10  
11   #include "standard.h"
12   #include "rhd_qtree.h"
13 +                                /* quantity of leaves to free at a time */
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  
22 + #define MAXDIFF2        (long)( MAXANG*MAXANG /90./90.*(1L<<15)*(1L<<15))
23 +
24 + #define abs(i)          ((i) < 0 ? -(i) : (i))
25 +
26   RTREE   qtrunk;                 /* our quadtree trunk */
27 < double  qtDepthEps = .02;       /* epsilon to compare depths (z fraction) */
27 > double  qtDepthEps = .05;       /* epsilon to compare depths (z fraction) */
28   int     qtMinNodesiz = 2;       /* minimum node dimension (pixels) */
29   struct rleaves  qtL;            /* our pile of leaves */
30  
# Line 21 | Line 33 | struct rleaves qtL;            /* our pile of leaves */
33   static RTREE    **twigbundle;   /* free twig blocks (NULL term.) */
34   static int      nexttwig;       /* next free twig */
35  
36 < #define is_stump(t)     (!((t)->flgs & (BR_ANY|LF_ANY)))
36 > #define ungetleaf(li)   (qtL.tl=(li))   /* dangerous if used improperly */
37  
38  
39   static RTREE *
# Line 58 | Line 70 | int    really;
70   {
71          register int    i;
72  
73 <        qtrunk.flgs = 0;
62 <        nexttwig = 0;
73 >        qtrunk.flgs = CH_ANY;   /* chop down tree */
74          if (twigbundle == NULL)
75                  return;
76 +        i = (TBUNDLESIZ-1+nexttwig)/TBUNDLESIZ;
77 +        nexttwig = 0;
78          if (!really) {          /* just clear allocated blocks */
79 <                for (i = 0; twigbundle[i] != NULL; i++)
79 >                while (i--)
80                          bzero((char *)twigbundle[i], TBUNDLESIZ*sizeof(RTREE));
81                  return;
82          }
# Line 89 | Line 102 | newleaf()                      /* allocate a leaf from our pile */
102   }
103  
104  
105 < #define LEAFSIZ         (3*sizeof(float)+sizeof(TMbright)+6*sizeof(BYTE))
105 > #define LEAFSIZ         (3*sizeof(float)+2*sizeof(short)+\
106 >                        sizeof(TMbright)+6*sizeof(BYTE))
107  
108   int
109   qtAllocLeaves(n)                /* allocate space for n leaves */
# Line 115 | Line 129 | register int   n;
129                  return(0);
130                                  /* assign larger alignment types earlier */
131          qtL.wp = (float (*)[3])qtL.base;
132 <        qtL.brt = (TMbright *)(qtL.wp + n);
132 >        qtL.wd = (short (*)[2])(qtL.wp + n);
133 >        qtL.brt = (TMbright *)(qtL.wd + n);
134          qtL.chr = (BYTE (*)[3])(qtL.brt + n);
135          qtL.rgb = (BYTE (*)[3])(qtL.chr + n);
136          qtL.nl = n;
# Line 187 | Line 202 | int    pct;
202   }
203  
204  
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 +
265   int
266   qtFindLeaf(x, y)                /* find closest leaf to (x,y) */
267   int     x, y;
# Line 231 | Line 306 | int    li;
306          register RTREE  *tp = &qtrunk;
307          int     x0=0, y0=0, x1=odev.hres, y1=odev.vres;
308          int     lo = -1;
309 +        long    d2;
310 +        short   dc[2];
311          int     x, y, mx, my;
312          double  z;
313          FVECT   ip, wp;
314          register int    q;
315 <                                        /* compute leaf location */
315 >                                        /* compute leaf location in view */
316          VCOPY(wp, qtL.wp[li]);
317          viewloc(ip, &odev.v, wp);
318          if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1.
319                          || ip[1] < 0. || ip[1] >= 1.)
320 <                return;
320 >                return(0);                      /* behind or outside view */
321 > #ifdef DEBUG
322 >        if (odev.v.type == VT_PAR | odev.v.vfore > FTINY)
323 >                error(INTERNAL, "bad view assumption in addleaf");
324 > #endif
325 >        for (q = 0; q < 3; q++)
326 >                wp[q] = (wp[q] - odev.v.vp[q])/ip[2];
327 >        encodedir(dc, wp);              /* compute pixel direction */
328 >        d2 = dir2diff(dc, qtL.wd[li]);
329 >        if (d2 > MAXDIFF2)
330 >                return(0);                      /* leaf dir. too far off */
331          x = ip[0] * odev.hres;
332          y = ip[1] * odev.vres;
333          z = ip[2];
# Line 263 | Line 350 | int    li;
350                          tp->flgs |= CHLFF(q);
351                          break;
352                  }      
353 <                                                /* check existing leaf */
267 <                if (lo != tp->k[q].li) {
353 >                if (lo != tp->k[q].li) {        /* check old leaf */
354                          lo = tp->k[q].li;
355                          VCOPY(wp, qtL.wp[lo]);
356                          viewloc(ip, &odev.v, wp);
357                  }
358                                                  /* is node minimum size? */
359 <                if (x1-x0 <= qtMinNodesiz || y1-y0 <= qtMinNodesiz) {
360 <                        if (z > (1.-qtDepthEps)*ip[2])  /* who is closer? */
361 <                                return;                 /* old one is */
362 <                        tp->k[q].li = li;               /* new one is */
359 >                if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) {
360 >                        if (z > (1.+qtDepthEps)*ip[2])
361 >                                return(0);              /* old one closer */
362 >                        if (z >= (1.-qtDepthEps)*ip[2] &&
363 >                                        dir2diff(dc, qtL.wd[lo]) < d2)
364 >                                return(0);              /* old one better */
365 >                        tp->k[q].li = li;               /* else new one is */
366                          tp->flgs |= CHF(q);
367                          break;
368                  }
# Line 288 | Line 377 | int    li;
377                  tp->k[q].li = lo;
378                  tp->flgs |= LFF(q)|CH_ANY;      /* all new */
379          }
380 +        return(1);              /* done */
381   }
382  
383  
384 < dev_value(c, p)                 /* add a pixel value to our output queue */
384 > dev_value(c, p, v)              /* add a pixel value to our quadtree */
385   COLR    c;
386 < FVECT   p;
386 > FVECT   p, v;
387   {
388          register int    li;
389  
390          li = newleaf();
391          VCOPY(qtL.wp[li], p);
392 +        encodedir(qtL.wd[li], v);
393          tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1);
394 <        addleaf(li);
394 >        if (!addleaf(li))
395 >                ungetleaf(li);
396   }
397  
398  
# Line 323 | Line 415 | qtMapLeaves(redo)              /* map our leaves to RGB */
415   int     redo;
416   {
417          int     aorg, alen, borg, blen;
418 +                                        /* recompute mapping? */
419 +        if (redo)
420 +                qtL.tml = qtL.bl;
421                                          /* already done? */
422          if (qtL.tml == qtL.tl)
423                  return(1);
329        if (redo)
330                qtL.tml = qtL.bl;
424                                          /* compute segments */
425          aorg = qtL.tml;
426          if (qtL.tl >= aorg) {
# Line 355 | Line 448 | int    redo;
448                                  qtL.chr+borg, blen);
449          qtL.tml = qtL.tl;
450          return(1);
358 }
359
360
361 static
362 redraw(ca, tp, x0, y0, x1, y1, l)       /* redraw portion of a tree */
363 BYTE    ca[3];          /* returned average color */
364 register RTREE  *tp;
365 int     x0, y0, x1, y1;
366 int     l[2][2];
367 {
368        int     csm[3], nc;
369        register BYTE   *cp;
370        BYTE    rgb[3];
371        int     quads = CH_ANY;
372        int     mx, my;
373        register int    i;
374                                        /* compute midpoint */
375        mx = (x0 + x1) >> 1;
376        my = (y0 + y1) >> 1;
377                                        /* see what to do */
378        if (l[0][0] >= mx)
379                quads &= ~(CHF(2)|CHF(0));
380        else if (l[0][1] <= mx)
381                quads &= ~(CHF(3)|CHF(1));
382        if (l[1][0] >= my)
383                quads &= ~(CHF(1)|CHF(0));
384        else if (l[1][1] <= my)
385                quads &= ~(CHF(3)|CHF(2));
386        tp->flgs &= ~quads;             /* mark them done */
387        csm[0] = csm[1] = csm[2] = nc = 0;
388                                        /* do leaves first */
389        for (i = 0; i < 4; i++)
390                if (quads & CHF(i) && tp->flgs & LFF(i)) {
391                        dev_paintr(cp=qtL.rgb[tp->k[i].li],
392                                        i&01 ? mx : x0, i&02 ? my : y0,
393                                        i&01 ? x1 : mx, i&02 ? y1 : my);
394                        csm[0] += cp[0]; csm[1] += cp[1]; csm[2] += cp[2];
395                        nc++;
396                        quads &= ~CHF(i);
397                }
398                                        /* now do branches */
399        for (i = 0; i < 4; i++)
400                if (quads & CHF(i) && tp->flgs & BRF(i)) {
401                        redraw(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0,
402                                        i&01 ? x1 : mx, i&02 ? y1 : my, l);
403                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
404                        nc++;
405                        quads &= ~CHF(i);
406                }
407        if (nc > 1) {
408                ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc;
409        } else {
410                ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2];
411        }
412        if (!quads) return;
413                                        /* fill in gaps with average */
414        for (i = 0; i < 4; i++)
415                if (quads & CHF(i))
416                        dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0,
417                                        i&01 ? x1 : mx, i&02 ? y1 : my);
418 }
419
420
421 static
422 update(ca, tp, x0, y0, x1, y1)  /* update tree display as needed */
423 BYTE    ca[3];          /* returned average color */
424 register RTREE  *tp;
425 int     x0, y0, x1, y1;
426 {
427        int     csm[3], nc;
428        register BYTE   *cp;
429        BYTE    rgb[3];
430        int     gaps = 0;
431        int     mx, my;
432        register int    i;
433                                        /* compute midpoint */
434        mx = (x0 + x1) >> 1;
435        my = (y0 + y1) >> 1;
436        csm[0] = csm[1] = csm[2] = nc = 0;
437                                        /* do leaves first */
438        for (i = 0; i < 4; i++) {
439                if (!(tp->flgs & CHF(i)))
440                        continue;
441                if (tp->flgs & LFF(i)) {
442                        dev_paintr(cp=qtL.rgb[tp->k[i].li],
443                                        i&01 ? mx : x0, i&02 ? my : y0,
444                                        i&01 ? x1 : mx, i&02 ? y1 : my);
445                        csm[0] += cp[0]; csm[1] += cp[1]; csm[2] += cp[2];
446                        nc++;
447                } else if (!(tp->flgs & BRF(i)))
448                        gaps |= 1<<i;   /* empty stem */
449        }
450                                        /* now do branches */
451        for (i = 0; i < 4; i++)
452                if ((tp->flgs & CHBRF(i)) == CHBRF(i)) {
453                        update(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0,
454                                        i&01 ? x1 : mx, i&02 ? y1 : my);
455                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
456                        nc++;
457                }
458        if (nc > 1) {
459                ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc;
460        } else {
461                ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2];
462        }
463                                        /* fill in gaps with average */
464        for (i = 0; gaps && i < 4; gaps >>= 1, i++)
465                if (gaps & 01)
466                        dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0,
467                                        i&01 ? x1 : mx, i&02 ? y1 : my);
468        tp->flgs &= ~CH_ANY;            /* all done */
469 }
470
471
472 qtRedraw(x0, y0, x1, y1)        /* redraw part or all of our screen */
473 int     x0, y0, x1, y1;
474 {
475        int     lim[2][2];
476        BYTE    ca[3];
477
478        if (is_stump(&qtrunk))
479                return;
480        if (!qtMapLeaves((lim[0][0]=x0) <= 0 & (lim[1][0]=y0) <= 0 &
481                (lim[0][1]=x1) >= odev.hres-1 & (lim[1][1]=y1) >= odev.vres-1))
482                return;
483        redraw(ca, &qtrunk, 0, 0, odev.hres, odev.vres, lim);
484 }
485
486
487 qtUpdate()                      /* update our tree display */
488 {
489        BYTE    ca[3];
490
491        if (is_stump(&qtrunk))
492                return;
493        if (!qtMapLeaves(0))
494                return;
495        update(ca, &qtrunk, 0, 0, odev.hres, odev.vres);
451   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines