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.4 by gregl, Fri Nov 21 09:52:06 1997 UTC vs.
Revision 3.9 by gregl, Tue Nov 25 16:52:04 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  
18   RTREE   qtrunk;                 /* our quadtree trunk */
19   double  qtDepthEps = .02;       /* epsilon to compare depths (z fraction) */
20   int     qtMinNodesiz = 2;       /* minimum node dimension (pixels) */
21 + struct rleaves  qtL;            /* our pile of leaves */
22  
18 static RLEAF    *leafpile;      /* our collection of leaf values */
19 static int      nleaves;        /* count of leaves in our pile */
20 static int      bleaf, tleaf;   /* bottom and top (next) leaf index (ring) */
21
23   #define TBUNDLESIZ      409     /* number of twigs in a bundle */
24  
25   static RTREE    **twigbundle;   /* free twig blocks (NULL term.) */
26   static int      nexttwig;       /* next free twig */
27  
27 static RTREE    emptytree;      /* empty tree for test below */
28  
29 #define is_stump(t)     (!bcmp((char *)(t), (char *)&emptytree, sizeof(RTREE)))
30
31
29   static RTREE *
30   newtwig()                       /* allocate a twig */
31   {
# Line 63 | Line 60 | int    really;
60   {
61          register int    i;
62  
63 <        tmClearHisto();
67 <        bzero((char *)&qtrunk, sizeof(RTREE));
68 <        nexttwig = 0;
63 >        qtrunk.flgs = CH_ANY;   /* chop down tree */
64          if (twigbundle == NULL)
65                  return;
66 +        i = (TBUNDLESIZ-1+nexttwig)/TBUNDLESIZ;
67 +        nexttwig = 0;
68          if (!really) {          /* just clear allocated blocks */
69 <                for (i = 0; twigbundle[i] != NULL; i++)
69 >                while (i--)
70                          bzero((char *)twigbundle[i], TBUNDLESIZ*sizeof(RTREE));
71                  return;
72          }
# Line 81 | Line 78 | int    really;
78   }
79  
80  
81 < static RLEAF *
81 > static int
82   newleaf()                       /* allocate a leaf from our pile */
83   {
84 <        RLEAF   *lp;
84 >        int     li;
85          
86 <        lp = leafpile + tleaf++;
87 <        if (tleaf >= nleaves)           /* get next leaf in ring */
88 <                tleaf = 0;
89 <        if (tleaf == bleaf)             /* need to shake some free */
86 >        li = qtL.tl++;
87 >        if (qtL.tl >= qtL.nl)   /* get next leaf in ring */
88 >                qtL.tl = 0;
89 >        if (qtL.tl == qtL.bl)   /* need to shake some free */
90                  qtCompost(LFREEPCT);
91 <        return(lp);
91 >        return(li);
92   }
93  
94  
95 + #define LEAFSIZ         (3*sizeof(float)+sizeof(TMbright)+6*sizeof(BYTE))
96 +
97   int
98   qtAllocLeaves(n)                /* allocate space for n leaves */
99 < int     n;
99 > register int    n;
100   {
101          unsigned        nbytes;
102          register unsigned       i;
# Line 105 | Line 104 | int    n;
104          qtFreeTree(0);          /* make sure tree is empty */
105          if (n <= 0)
106                  return(0);
107 <        if (nleaves >= n)
108 <                return(nleaves);
109 <        else if (nleaves > 0)
110 <                free((char *)leafpile);
107 >        if (qtL.nl >= n)
108 >                return(qtL.nl);
109 >        else if (qtL.nl > 0)
110 >                free(qtL.base);
111                                  /* round space up to nearest power of 2 */
112 <        nbytes = n*sizeof(RLEAF) + 8;
112 >        nbytes = n*LEAFSIZ + 8;
113          for (i = 1024; nbytes > i; i <<= 1)
114                  ;
115 <        n = (i - 8) / sizeof(RLEAF);
116 <        leafpile = (RLEAF *)malloc(n*sizeof(RLEAF));
117 <        if (leafpile == NULL)
118 <                return(-1);
119 <        nleaves = n;
120 <        bleaf = tleaf = 0;
121 <        return(nleaves);
115 >        n = (i - 8) / LEAFSIZ;  /* should we make sure n is even? */
116 >        qtL.base = (char *)malloc(n*LEAFSIZ);
117 >        if (qtL.base == NULL)
118 >                return(0);
119 >                                /* assign larger alignment types earlier */
120 >        qtL.wp = (float (*)[3])qtL.base;
121 >        qtL.brt = (TMbright *)(qtL.wp + n);
122 >        qtL.chr = (BYTE (*)[3])(qtL.brt + n);
123 >        qtL.rgb = (BYTE (*)[3])(qtL.chr + n);
124 >        qtL.nl = n;
125 >        qtL.tml = qtL.bl = qtL.tl = 0;
126 >        return(n);
127   }
128  
129 + #undef  LEAFSIZ
130  
131 +
132   qtFreeLeaves()                  /* free our allocated leaves and twigs */
133   {
134          qtFreeTree(1);          /* free tree also */
135 <        if (nleaves <= 0)
135 >        if (qtL.nl <= 0)
136                  return;
137 <        free((char *)leafpile);
138 <        leafpile = NULL;
139 <        nleaves = 0;
137 >        free(qtL.base);
138 >        qtL.base = NULL;
139 >        qtL.nl = 0;
140   }
141  
142  
# Line 141 | Line 147 | register RTREE *tp;
147          register int    i, li;
148  
149          for (i = 0; i < 4; i++)
150 <                if (tp->flgs & BRF(i))
150 >                if (tp->flgs & BRF(i)) {
151                          shaketree(tp->k[i].b);
152 <                else if (tp->k[i].l != NULL) {
153 <                        li = tp->k[i].l - leafpile;
154 <                        if (bleaf < tleaf ? (li < bleaf || li >= tleaf) :
155 <                                        (li < bleaf && li >= tleaf)) {
156 <                                tmAddHisto(&tp->k[i].l->brt, 1, -1);
157 <                                tp->k[i].l = NULL;
158 <                        }
152 >                        if (is_stump(tp->k[i].b))
153 >                                tp->flgs &= ~BRF(i);
154 >                } else if (tp->flgs & LFF(i)) {
155 >                        li = tp->k[i].li;
156 >                        if (qtL.bl < qtL.tl ?
157 >                                (li < qtL.bl || li >= qtL.tl) :
158 >                                (li < qtL.bl && li >= qtL.tl))
159 >                                tp->flgs &= ~LFF(i);
160                  }
161   }
162  
# Line 158 | Line 165 | int
165   qtCompost(pct)                  /* free up some leaves */
166   int     pct;
167   {
168 <        int     nused, nclear;
168 >        int     nused, nclear, nmapped;
169  
163        if (is_stump(&qtrunk))
164                return(0);
170                                  /* figure out how many leaves to clear */
171 <        nclear = nleaves * pct / 100;
172 <        nused = tleaf > bleaf ? tleaf-bleaf : tleaf+nleaves-bleaf;
173 <        nclear -= nleaves - nused;      /* less what's already free */
171 >        nclear = qtL.nl * pct / 100;
172 >        nused = qtL.tl - qtL.bl;
173 >        if (nused <= 0) nused += qtL.nl;
174 >        nclear -= qtL.nl - nused;
175          if (nclear <= 0)
176                  return(0);
177          if (nclear >= nused) {  /* clear them all */
178                  qtFreeTree(0);
179 <                bleaf = tleaf = 0;
179 >                qtL.tml = qtL.bl = qtL.tl = 0;
180                  return(nused);
181          }
182                                  /* else clear leaves from bottom */
183 <        bleaf += nclear;
184 <        if (bleaf >= nleaves) bleaf -= nleaves;
183 >        nmapped = qtL.tml - qtL.bl;
184 >        if (nmapped < 0) nmapped += qtL.nl;
185 >        qtL.bl += nclear;
186 >        if (qtL.bl >= qtL.nl) qtL.bl -= qtL.nl;
187 >        if (nmapped <= nclear) qtL.tml = qtL.bl;
188          shaketree(&qtrunk);
189          return(nclear);
190   }
191  
192  
193 < RLEAF *
193 > int
194   qtFindLeaf(x, y)                /* find closest leaf to (x,y) */
195   int     x, y;
196   {
197          register RTREE  *tp = &qtrunk;
198 <        RLEAF   *lp = NULL;
198 >        int     li = -1;
199          int     x0=0, y0=0, x1=odev.hres, y1=odev.vres;
200          int     mx, my;
201          register int    q;
202                                          /* check limits */
203          if (x < 0 || x >= odev.hres || y < 0 || y >= odev.vres)
204 <                return(NULL);
204 >                return(-1);
205                                          /* find nearby leaf in our tree */
206          for ( ; ; ) {
207                  for (q = 0; q < 4; q++)         /* find any leaf this level */
208 <                        if (!(tp->flgs & BRF(q)) && tp->k[q].l != NULL) {
209 <                                lp = tp->k[q].l;
208 >                        if (tp->flgs & LFF(q)) {
209 >                                li = tp->k[q].li;
210                                  break;
211                          }
212                  q = 0;                          /* which quadrant are we? */
# Line 211 | Line 220 | int    x, y;
220                          tp = tp->k[q].b;
221                          continue;
222                  }
223 <                if (tp->k[q].l != NULL)         /* good shot! */
224 <                        return(tp->k[q].l);
225 <                return(lp);                     /* else return what we have */
223 >                if (tp->flgs & LFF(q))          /* good shot! */
224 >                        return(tp->k[q].li);
225 >                return(li);                     /* else return what we have */
226          }
227   }
228  
229  
230   static
231 < addleaf(lp)                     /* add a leaf to our tree */
232 < RLEAF   *lp;
231 > addleaf(li)                     /* add a leaf to our tree */
232 > int     li;
233   {
234          register RTREE  *tp = &qtrunk;
235          int     x0=0, y0=0, x1=odev.hres, y1=odev.vres;
236 <        RLEAF   *lo = NULL;
236 >        int     lo = -1;
237          int     x, y, mx, my;
238          double  z;
239          FVECT   ip, wp;
240          register int    q;
241                                          /* compute leaf location */
242 <        VCOPY(wp, lp->wp);
242 >        VCOPY(wp, qtL.wp[li]);
243          viewloc(ip, &odev.v, wp);
244          if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1.
245                          || ip[1] < 0. || ip[1] >= 1.)
# Line 252 | Line 261 | RLEAF  *lp;
261                          tp = tp->k[q].b;
262                          continue;
263                  }
264 <                if (tp->k[q].l == NULL) {       /* found stem for leaf */
265 <                        tp->k[q].l = lp;
266 <                        tp->flgs |= CHF(q);
264 >                if (!(tp->flgs & LFF(q))) {     /* found stem for leaf */
265 >                        tp->k[q].li = li;
266 >                        tp->flgs |= CHLFF(q);
267                          break;
268                  }      
269                                                  /* check existing leaf */
270 <                if (lo != tp->k[q].l) {
271 <                        lo = tp->k[q].l;
272 <                        VCOPY(wp, lo->wp);
270 >                if (lo != tp->k[q].li) {
271 >                        lo = tp->k[q].li;
272 >                        VCOPY(wp, qtL.wp[lo]);
273                          viewloc(ip, &odev.v, wp);
274                  }
275                                                  /* is node minimum size? */
276                  if (x1-x0 <= qtMinNodesiz || y1-y0 <= qtMinNodesiz) {
277                          if (z > (1.-qtDepthEps)*ip[2])  /* who is closer? */
278                                  return;                 /* old one is */
279 <                        tp->k[q].l = lp;                /* new one is */
279 >                        tp->k[q].li = li;               /* new one is */
280                          tp->flgs |= CHF(q);
272                        tmAddHisto(&lo->brt, 1, -1);    /* drop old one */
281                          break;
282                  }
283 <                tp->flgs |= CHBRF(q);           /* else grow tree */
283 >                tp->flgs &= ~LFF(q);            /* else grow tree */
284 >                tp->flgs |= CHBRF(q);
285                  tp = tp->k[q].b = newtwig();
277                tp->flgs |= CH_ANY;             /* all new */
286                  q = 0;                          /* old leaf -> new branch */
287                  mx = ip[0] * odev.hres;
288                  my = ip[1] * odev.vres;
289                  if (mx >= (x0 + x1) >> 1) q |= 01;
290                  if (my >= (y0 + y1) >> 1) q |= 02;
291 <                tp->k[q].l = lo;
291 >                tp->k[q].li = lo;
292 >                tp->flgs |= LFF(q)|CH_ANY;      /* all new */
293          }
285        tmAddHisto(&lp->brt, 1, 1);     /* add leaf to histogram */
294   }
295  
296  
297 < dev_value(c, p)                 /* add a pixel value to our output queue */
297 > dev_value(c, p)                 /* add a pixel value to our quadtree */
298   COLR    c;
299   FVECT   p;
300   {
301 <        register RLEAF  *lp;
301 >        register int    li;
302  
303 <        lp = newleaf();
304 <        VCOPY(lp->wp, p);
305 <        tmCvColrs(&lp->brt, lp->chr, c, 1);
306 <        addleaf(lp);
303 >        li = newleaf();
304 >        VCOPY(qtL.wp[li], p);
305 >        tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1);
306 >        addleaf(li);
307   }
308  
309  
310   qtReplant()                     /* replant our tree using new view */
311   {
312          register int    i;
313 <
314 <        if (bleaf == tleaf)             /* anything to replant? */
313 >                                        /* anything to replant? */
314 >        if (qtL.bl == qtL.tl)
315                  return;
316 <        qtFreeTree(0);                  /* blow the tree away */
317 <                                        /* now rebuild it */
318 <        for (i = bleaf; i != tleaf; ) {
319 <                addleaf(leafpile+i);
320 <                if (++i >= nleaves) i = 0;
316 >        qtFreeTree(0);                  /* blow the old tree away */
317 >                                        /* regrow it in new place */
318 >        for (i = qtL.bl; i != qtL.tl; ) {
319 >                addleaf(i);
320 >                if (++i >= qtL.nl) i = 0;
321          }
314        tmComputeMapping(0., 0., 0.);   /* update the display */
315        qtUpdate();
322   }
323  
324  
325 < static
326 < redraw(ca, tp, x0, y0, x1, y1, l)       /* redraw portion of a tree */
321 < BYTE    ca[3];          /* returned average color */
322 < register RTREE  *tp;
323 < int     x0, y0, x1, y1;
324 < int     l[2][2];
325 > qtMapLeaves(redo)               /* map our leaves to RGB */
326 > int     redo;
327   {
328 <        int     csm[3], nc;
329 <        BYTE    rgb[3];
330 <        int     quads = CH_ANY;
331 <        int     mx, my;
332 <        register int    i;
333 <                                        /* compute midpoint */
334 <        mx = (x0 + x1) >> 1;
335 <        my = (y0 + y1) >> 1;
336 <                                        /* see what to do */
337 <        if (l[0][0] >= mx)
338 <                quads &= ~(CHF(2)|CHF(0));
339 <        else if (l[0][1] <= mx)
338 <                quads &= ~(CHF(3)|CHF(1));
339 <        if (l[1][0] >= my)
340 <                quads &= ~(CHF(1)|CHF(0));
341 <        else if (l[1][1] <= my)
342 <                quads &= ~(CHF(3)|CHF(2));
343 <        tp->flgs &= ~quads;             /* mark them done */
344 <        csm[0] = csm[1] = csm[2] = nc = 0;
345 <                                        /* do leaves first */
346 <        for (i = 0; i < 4; i++)
347 <                if (quads & CHF(i) && !(tp->flgs & BRF(i)) &&
348 <                                tp->k[i].l != NULL) {
349 <                        tmMapPixels(rgb, &tp->k[i].l->brt, tp->k[i].l->chr, 1);
350 <                        dev_paintr(rgb, i&01 ? mx : x0, i&02 ? my : y0,
351 <                                        i&01 ? x1 : mx, i&02 ? y1 : my);
352 <                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
353 <                        nc++;
354 <                        quads &= ~CHF(i);
355 <                }
356 <                                        /* now do branches */
357 <        for (i = 0; i < 4; i++)
358 <                if (quads & CHF(i) && tp->flgs & BRF(i)) {
359 <                        redraw(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0,
360 <                                        i&01 ? x1 : mx, i&02 ? y1 : my, l);
361 <                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
362 <                        nc++;
363 <                        quads &= ~CHF(i);
364 <                }
365 <        if (nc > 1) {
366 <                ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc;
328 >        int     aorg, alen, borg, blen;
329 >                                        /* recompute mapping? */
330 >        if (redo)
331 >                qtL.tml = qtL.bl;
332 >                                        /* already done? */
333 >        if (qtL.tml == qtL.tl)
334 >                return(1);
335 >                                        /* compute segments */
336 >        aorg = qtL.tml;
337 >        if (qtL.tl >= aorg) {
338 >                alen = qtL.tl - aorg;
339 >                blen = 0;
340          } else {
341 <                ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2];
341 >                alen = qtL.nl - aorg;
342 >                borg = 0;
343 >                blen = qtL.tl;
344          }
345 <        if (!quads) return;
346 <                                        /* fill in gaps with average */
347 <        for (i = 0; i < 4; i++)
348 <                if (quads & CHF(i))
349 <                        dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0,
350 <                                        i&01 ? x1 : mx, i&02 ? y1 : my);
376 < }
377 <
378 <
379 < static
380 < update(ca, tp, x0, y0, x1, y1)  /* update tree display as needed */
381 < BYTE    ca[3];          /* returned average color */
382 < register RTREE  *tp;
383 < int     x0, y0, x1, y1;
384 < {
385 <        int     csm[3], nc;
386 <        BYTE    rgb[3];
387 <        int     gaps = 0;
388 <        int     mx, my;
389 <        register int    i;
390 <                                        /* compute midpoint */
391 <        mx = (x0 + x1) >> 1;
392 <        my = (y0 + y1) >> 1;
393 <        csm[0] = csm[1] = csm[2] = nc = 0;
394 <                                        /* do leaves first */
395 <        for (i = 0; i < 4; i++)
396 <                if ((tp->flgs & CHBRF(i)) == CHF(i)) {
397 <                        if (tp->k[i].l == NULL) {
398 <                                gaps |= 1<<i;   /* empty stem */
399 <                                continue;
400 <                        }
401 <                        tmMapPixels(rgb, &tp->k[i].l->brt, tp->k[i].l->chr, 1);
402 <                        dev_paintr(rgb, i&01 ? mx : x0, i&02 ? my : y0,
403 <                                        i&01 ? x1 : mx, i&02 ? y1 : my);
404 <                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
405 <                        nc++;
406 <                }
407 <                                        /* now do branches */
408 <        for (i = 0; i < 4; i++)
409 <                if ((tp->flgs & CHBRF(i)) == CHBRF(i)) {
410 <                        update(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0,
411 <                                        i&01 ? x1 : mx, i&02 ? y1 : my);
412 <                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
413 <                        nc++;
414 <                }
415 <        if (nc > 1) {
416 <                ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc;
417 <        } else {
418 <                ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2];
419 <        }
420 <                                        /* fill in gaps with average */
421 <        for (i = 0; gaps && i < 4; gaps >>= 1, i++)
422 <                if (gaps & 01)
423 <                        dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0,
424 <                                        i&01 ? x1 : mx, i&02 ? y1 : my);
425 <        tp->flgs &= ~CH_ANY;            /* all done */
426 < }
427 <
428 <
429 < qtRedraw(x0, y0, x1, y1)        /* redraw part of our screen */
430 < int     x0, y0, x1, y1;
431 < {
432 <        int     lim[2][2];
433 <        BYTE    ca[3];
434 <
435 <        if (is_stump(&qtrunk))
436 <                return;
437 <        if ((lim[0][0]=x0) <= 0 & (lim[1][0]=y0) <= 0 &
438 <                (lim[0][1]=x1) >= odev.hres-1 & (lim[1][1]=y1) >= odev.vres-1
439 <                        || tmTop->lumap == NULL)
345 >                                        /* (re)compute tone mapping? */
346 >        if (qtL.tml == qtL.bl) {
347 >                tmClearHisto();
348 >                tmAddHisto(qtL.brt+aorg, alen, 1);
349 >                if (blen > 0)
350 >                        tmAddHisto(qtL.brt+borg, blen, 1);
351                  if (tmComputeMapping(0., 0., 0.) != TM_E_OK)
352 <                        return;
353 <        redraw(ca, &qtrunk, 0, 0, odev.hres, odev.vres, lim);
354 < }
355 <
356 <
357 < qtUpdate()                      /* update our tree display */
358 < {
359 <        BYTE    ca[3];
360 <
361 <        if (is_stump(&qtrunk))
451 <                return;
452 <        if (tmTop->lumap == NULL)
453 <                tmComputeMapping(0., 0., 0.);
454 <        update(ca, &qtrunk, 0, 0, odev.hres, odev.vres);
352 >                        return(0);
353 >        }
354 >        if (tmMapPixels(qtL.rgb+aorg, qtL.brt+aorg,
355 >                        qtL.chr+aorg, alen) != TM_E_OK)
356 >                return(0);
357 >        if (blen > 0)
358 >                tmMapPixels(qtL.rgb+borg, qtL.brt+borg,
359 >                                qtL.chr+borg, blen);
360 >        qtL.tml = qtL.tl;
361 >        return(1);
362   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines