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.11 by gregl, Fri Dec 5 15:40:54 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  
22 + #define MAXDIFF2        (PI*PI/180./180.* MAXANG*MAXANG )
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 25 | 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 62 | Line 70 | int    really;
70   {
71          register int    i;
72  
73 <        qtrunk.flgs = CH_ANY;
66 <        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 93 | 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)+sizeof(int4)+\
106 >                        sizeof(TMbright)+6*sizeof(BYTE))
107  
108   int
109   qtAllocLeaves(n)                /* allocate space for n leaves */
# Line 119 | 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 = (int4 *)(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 191 | Line 202 | int    pct;
202   }
203  
204  
205 + #define DCSCALE         11585.2         /* (1<<13)*sqrt(2) */
206 + #define FXNEG           01
207 + #define FYNEG           02
208 + #define FZNEG           04
209 + #define FXACT           010
210 + #define FZACT           020
211 + #define F1SFT           5
212 + #define F2SFT           18
213 + #define FMASK           0x1fff
214 +
215 + static int4
216 + encodedir(dv)           /* encode a normalized direction vector */
217 + FVECT   dv;
218 + {
219 +        register int4   dc = 0;
220 +        int     cd[3], cm;
221 +        register int    i;
222 +
223 +        for (i = 0; i < 3; i++)
224 +                if (dv[i] < 0.) {
225 +                        cd[i] = dv[i] * -DCSCALE;
226 +                        dc |= 1<<i;
227 +                } else
228 +                        cd[i] = dv[i] * DCSCALE;
229 +        if (cd[0] <= cd[1]) {
230 +                dc |= FXACT | cd[0] << F1SFT;
231 +                cm = cd[1];
232 +        } else {
233 +                dc |= cd[1] << F1SFT;
234 +                cm = cd[0];
235 +        }
236 +        if (cd[2] <= cm)
237 +                dc |= FZACT | cd[2] << F2SFT;
238 +        else
239 +                dc |= cm << F2SFT;
240 +        return(dc);
241 + }
242 +
243 +
244 + static
245 + decodedir(dv, dc)       /* decode a normalized direction vector */
246 + FVECT   dv;     /* returned */
247 + register int4   dc;
248 + {
249 +        double  d1, d2, der;
250 +
251 +        d1 = ((dc>>F1SFT & FMASK)+.5)/DCSCALE;
252 +        d2 = ((dc>>F2SFT & FMASK)+.5)/DCSCALE;
253 +        der = sqrt(1. - d1*d1 - d2*d2);
254 +        if (dc & FXACT) {
255 +                dv[0] = d1;
256 +                if (dc & FZACT) { dv[1] = der; dv[2] = d2; }
257 +                else { dv[1] = d2; dv[2] = der; }
258 +        } else {
259 +                dv[1] = d1;
260 +                if (dc & FZACT) { dv[0] = der; dv[2] = d2; }
261 +                else { dv[0] = d2; dv[2] = der; }
262 +        }
263 +        if (dc & FXNEG) dv[0] = -dv[0];
264 +        if (dc & FYNEG) dv[1] = -dv[1];
265 +        if (dc & FZNEG) dv[2] = -dv[2];
266 + }
267 +
268 +
269 + static double
270 + dir2diff(dc1, dc2)              /* relative radians^2 between directions */
271 + int4    dc1, dc2;
272 + {
273 +        FVECT   v1, v2;
274 +
275 +        decodedir(v1, dc1);
276 +        decodedir(v2, dc2);
277 +
278 +        return(2. - 2.*DOT(v1,v2));
279 + }
280 +
281 +
282 + static double
283 + fdir2diff(dc1, v2)              /* relative radians^2 between directions */
284 + int4    dc1;
285 + register FVECT  v2;
286 + {
287 +        FVECT   v1;
288 +
289 +        decodedir(v1, dc1);
290 +
291 +        return(2. - 2.*DOT(v1,v2));
292 + }
293 +
294 +
295   int
296   qtFindLeaf(x, y)                /* find closest leaf to (x,y) */
297   int     x, y;
# Line 235 | Line 336 | int    li;
336          register RTREE  *tp = &qtrunk;
337          int     x0=0, y0=0, x1=odev.hres, y1=odev.vres;
338          int     lo = -1;
339 +        double  d2;
340          int     x, y, mx, my;
341          double  z;
342 <        FVECT   ip, wp;
342 >        FVECT   ip, wp, vd;
343          register int    q;
344 <                                        /* compute leaf location */
344 >                                        /* compute leaf location in view */
345          VCOPY(wp, qtL.wp[li]);
346          viewloc(ip, &odev.v, wp);
347          if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1.
348                          || ip[1] < 0. || ip[1] >= 1.)
349 <                return;
349 >                return(0);                      /* behind or outside view */
350 > #ifdef DEBUG
351 >        if (odev.v.type == VT_PAR | odev.v.vfore > FTINY)
352 >                error(INTERNAL, "bad view assumption in addleaf");
353 > #endif
354 >        for (q = 0; q < 3; q++)
355 >                vd[q] = (wp[q] - odev.v.vp[q])/ip[2];
356 >        d2 = fdir2diff(qtL.wd[li], vd);
357 >        if (d2 > MAXDIFF2)
358 >                return(0);                      /* leaf dir. too far off */
359          x = ip[0] * odev.hres;
360          y = ip[1] * odev.vres;
361          z = ip[2];
# Line 267 | Line 378 | int    li;
378                          tp->flgs |= CHLFF(q);
379                          break;
380                  }      
381 <                                                /* check existing leaf */
271 <                if (lo != tp->k[q].li) {
381 >                if (lo != tp->k[q].li) {        /* check old leaf */
382                          lo = tp->k[q].li;
383                          VCOPY(wp, qtL.wp[lo]);
384                          viewloc(ip, &odev.v, wp);
385                  }
386                                                  /* is node minimum size? */
387 <                if (x1-x0 <= qtMinNodesiz || y1-y0 <= qtMinNodesiz) {
388 <                        if (z > (1.-qtDepthEps)*ip[2])  /* who is closer? */
389 <                                return;                 /* old one is */
390 <                        tp->k[q].li = li;               /* new one is */
387 >                if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) {
388 >                        if (z > (1.+qtDepthEps)*ip[2])
389 >                                return(0);              /* old one closer */
390 >                        if (z >= (1.-qtDepthEps)*ip[2] &&
391 >                                        fdir2diff(qtL.wd[lo], vd) < d2)
392 >                                return(0);              /* old one better */
393 >                        tp->k[q].li = li;               /* else new one is */
394                          tp->flgs |= CHF(q);
395                          break;
396                  }
# Line 289 | Line 402 | int    li;
402                  my = ip[1] * odev.vres;
403                  if (mx >= (x0 + x1) >> 1) q |= 01;
404                  if (my >= (y0 + y1) >> 1) q |= 02;
405 +                tp->flgs = CH_ANY|LFF(q);       /* all new */
406                  tp->k[q].li = lo;
293                tp->flgs |= LFF(q)|CH_ANY;      /* all new */
407          }
408 +        return(1);              /* done */
409   }
410  
411  
412 < dev_value(c, p)                 /* add a pixel value to our output queue */
412 > dev_value(c, p, v)              /* add a pixel value to our quadtree */
413   COLR    c;
414 < FVECT   p;
414 > FVECT   p, v;
415   {
416          register int    li;
417  
418          li = newleaf();
419          VCOPY(qtL.wp[li], p);
420 +        qtL.wd[li] = encodedir(v);
421          tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1);
422 <        addleaf(li);
422 >        if (!addleaf(li))
423 >                ungetleaf(li);
424   }
425  
426  
# Line 360 | Line 476 | int    redo;
476                                  qtL.chr+borg, blen);
477          qtL.tml = qtL.tl;
478          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);
479   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines