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.1 by gregl, Wed Nov 19 18:01:03 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  
14 static RLEAF    *leafpile;      /* our collection of leaf values */
15 static int      nleaves;        /* count of leaves in our pile */
16 static int      bleaf, tleaf;   /* bottom and top (next) leaf index (ring) */
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  
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 58 | Line 55 | memerr:
55   }
56  
57  
58 < static
62 < freetwigs(really)               /* free allocated twigs */
58 > qtFreeTree(really)              /* free allocated twigs */
59   int     really;
60   {
61          register int    i;
62  
63 <        if (tmTop != NULL)
68 <                tmClearHisto();
69 <        bzero((char *)&qtrunk, sizeof(RTREE));
70 <        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 83 | Line 78 | int    really;
78   }
79  
80  
81 < static RLEAF *
81 > static int
82   newleaf()                       /* allocate a leaf from our pile */
83   {
84 <        if (tleaf++ >= nleaves)         /* get next leaf in ring */
85 <                tleaf = 0;
86 <        if (tleaf == bleaf)             /* need to shake some free */
84 >        int     li;
85 >        
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(leafpile + tleaf);
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;
103  
104 <        freetwigs(0);           /* make sure tree is empty */
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 <        freetwigs(1);           /* free tree also */
135 <        if (nleaves <= 0)
134 >        qtFreeTree(1);          /* free tree also */
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 148 | register RTREE *tp;
148  
149          for (i = 0; i < 4; i++)
150                  if (tp->flgs & BRF(i)) {
151 <                        if (shaketree(tp->k[i].b))
152 <                                tp->flgs |= CHF(i);
153 <                } else if (tp->k[i].l != NULL) {
154 <                        li = tp->k[i].l - leafpile;
155 <                        if (bleaf < tleaf ? (li < bleaf || li >= tleaf) :
156 <                                        (li < bleaf && li >= tleaf)) {
157 <                                tmAddHisto(&tp->k[i].l->brt, 1, -1);
158 <                                tp->k[i].l = NULL;
159 <                                tp->flgs |= CHF(i);
153 <                        }
151 >                        shaketree(tp->k[i].b);
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                  }
155        return(tp->flgs & CH_ANY);
161   }
162  
163  
# Line 160 | Line 165 | int
165   qtCompost(pct)                  /* free up some leaves */
166   int     pct;
167   {
168 <        int     nused, nclear;
168 >        int     nused, nclear, nmapped;
169 >
170                                  /* figure out how many leaves to clear */
171 <        nclear = nleaves * pct / 100;
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);
168        nused = tleaf > bleaf ? tleaf-bleaf : tleaf+nleaves-bleaf;
177          if (nclear >= nused) {  /* clear them all */
178 <                freetwigs(0);
179 <                bleaf = tleaf = 0;
178 >                qtFreeTree(0);
179 >                qtL.tml = qtL.bl = qtL.tl = 0;
180                  return(nused);
181          }
182                                  /* else clear leaves from bottom */
183 <        bleaf = (bleaf + nclear) % 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 + int
194 + qtFindLeaf(x, y)                /* find closest leaf to (x,y) */
195 + int     x, y;
196 + {
197 +        register RTREE  *tp = &qtrunk;
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(-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 & LFF(q)) {
209 +                                li = tp->k[q].li;
210 +                                break;
211 +                        }
212 +                q = 0;                          /* which quadrant are we? */
213 +                mx = (x0 + x1) >> 1;
214 +                my = (y0 + y1) >> 1;
215 +                if (x < mx) x1 = mx;
216 +                else {x0 = mx; q |= 01;}
217 +                if (y < my) y1 = my;
218 +                else {y0 = my; q |= 02;}
219 +                if (tp->flgs & BRF(q)) {        /* branch down if not a leaf */
220 +                        tp = tp->k[q].b;
221 +                        continue;
222 +                }
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 212 | 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);
232                        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();
237                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          }
245        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 <        freetwigs(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          }
274        tmComputeMapping(0., 0., 0.);   /* update the display */
275        qtUpdate();
322   }
323  
324  
325 < static
326 < redraw(ca, tp, x0, y0, x1, y1, l)       /* redraw portion of a tree */
281 < BYTE    ca[3];          /* returned average color */
282 < register RTREE  *tp;
283 < int     x0, y0, x1, y1;
284 < 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)
298 <                quads &= ~(CHF(3)|CHF(1));
299 <        if (l[1][0] >= my)
300 <                quads &= ~(CHF(1)|CHF(0));
301 <        else if (l[1][1] <= my)
302 <                quads &= ~(CHF(3)|CHF(2));
303 <        tp->flgs &= ~quads;             /* mark them done */
304 <        csm[0] = csm[1] = csm[2] = nc = 0;
305 <                                        /* do leaves first */
306 <        for (i = 0; i < 4; i++)
307 <                if (quads & CHF(i) && !(tp->flgs & BRF(i)) &&
308 <                                tp->k[i].l != NULL) {
309 <                        tmMapPixels(rgb, &tp->k[i].l->brt, tp->k[i].l->chr, 1);
310 <                        dev_paintr(rgb, i&01 ? mx : x0, i&02 ? my : y0,
311 <                                        i&01 ? x1 : mx, i&02 ? y1 : my);
312 <                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
313 <                        nc++;
314 <                        quads &= ~CHF(i);
315 <                }
316 <                                        /* now do branches */
317 <        for (i = 0; i < 4; i++)
318 <                if (quads & CHF(i) && tp->flgs & BRF(i)) {
319 <                        redraw(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0,
320 <                                        i&01 ? x1 : mx, i&02 ? y1 : my, l);
321 <                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
322 <                        nc++;
323 <                        quads &= ~CHF(i);
324 <                }
325 <        if (nc > 1) {
326 <                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);
351 < }
352 <
338 <
339 < static
340 < update(ca, tp, x0, y0, x1, y1)  /* update tree display as needed */
341 < BYTE    ca[3];          /* returned average color */
342 < register RTREE  *tp;
343 < int     x0, y0, x1, y1;
344 < {
345 <        int     csm[3], nc;
346 <        BYTE    rgb[3];
347 <        int     gaps = 0;
348 <        int     mx, my;
349 <        register int    i;
350 <                                        /* compute midpoint */
351 <        mx = (x0 + x1) >> 1;
352 <        my = (y0 + y1) >> 1;
353 <        csm[0] = csm[1] = csm[2] = nc = 0;
354 <                                        /* do leaves first */
355 <        for (i = 0; i < 4; i++)
356 <                if ((tp->flgs & CHBRF(i)) == CHF(i)) {
357 <                        if (tp->k[i].l == NULL) {
358 <                                gaps |= 1<<i;   /* empty stem */
359 <                                continue;
360 <                        }
361 <                        tmMapPixels(rgb, &tp->k[i].l->brt, tp->k[i].l->chr, 1);
362 <                        dev_paintr(rgb, i&01 ? mx : x0, i&02 ? my : y0,
363 <                                        i&01 ? x1 : mx, i&02 ? y1 : my);
364 <                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
365 <                        nc++;
366 <                }
367 <                                        /* now do branches */
368 <        for (i = 0; i < 4; i++)
369 <                if ((tp->flgs & CHBRF(i)) == CHBRF(i)) {
370 <                        update(rgb, tp->k[i].b, i&01 ? mx : x0, i&02 ? my : y0,
371 <                                        i&01 ? x1 : mx, i&02 ? y1 : my);
372 <                        csm[0] += rgb[0]; csm[1] += rgb[1]; csm[2] += rgb[2];
373 <                        nc++;
374 <                }
375 <        if (nc > 1) {
376 <                ca[0] = csm[0]/nc; ca[1] = csm[1]/nc; ca[2] = csm[2]/nc;
377 <        } else {
378 <                ca[0] = csm[0]; ca[1] = csm[1]; ca[2] = csm[2];
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(0);
353          }
354 <                                        /* fill in gaps with average */
355 <        for (i = 0; gaps && i < 4; gaps >>= 1, i++)
356 <                if (gaps & 01)
357 <                        dev_paintr(ca, i&01 ? mx : x0, i&02 ? my : y0,
358 <                                        i&01 ? x1 : mx, i&02 ? y1 : my);
359 <        tp->flgs &= ~CH_ANY;            /* all done */
360 < }
361 <
388 <
389 < qtRedraw(x0, y0, x1, y1)        /* redraw part of our screen */
390 < int     x0, y0, x1, y1;
391 < {
392 <        int     lim[2][2];
393 <        BYTE    ca[3];
394 <
395 <        if (is_stump(&qtrunk))
396 <                return;
397 <        if ((lim[0][0]=x0) == 0 & (lim[1][0]=y0) == 0 &
398 <                (lim[0][1]=x1) == odev.hres & (lim[1][1]=y1) == odev.vres ||
399 <                        tmTop->lumap == NULL)
400 <                tmComputeMapping(0., 0., 0.);
401 <        redraw(ca, &qtrunk, 0, 0, odev.hres, odev.vres, lim);
402 < }
403 <
404 <
405 < qtUpdate()                      /* update our tree display */
406 < {
407 <        BYTE    ca[3];
408 <
409 <        if (is_stump(&qtrunk))
410 <                return;
411 <        if (tmTop->lumap == NULL)
412 <                tmComputeMapping(0., 0., 0.);
413 <        update(ca, &qtrunk, 0, 0, odev.hres, odev.vres);
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