ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_qtree.c
Revision: 3.15
Committed: Tue Dec 30 17:08:37 1997 UTC (26 years, 9 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.14: +56 -25 lines
Log Message:
allow unused leaves to fall into free list for greater efficiency

File Contents

# User Rev Content
1 gregl 3.1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * Quadtree driver support routines.
9     */
10    
11     #include "standard.h"
12     #include "rhd_qtree.h"
13 gregl 3.6 /* quantity of leaves to free at a time */
14     #ifndef LFREEPCT
15     #define LFREEPCT 25
16     #endif
17 gregl 3.10 /* maximum allowed angle difference (deg.) */
18     #ifndef MAXANG
19 gregl 3.12 #define MAXANG 20
20 gregl 3.10 #endif
21 gregl 3.12 #if MAXANG>0
22     #define MAXDIFF2 ( MAXANG*MAXANG * (PI*PI/180./180.))
23     #endif
24 gregl 3.1
25 gregl 3.10 #define abs(i) ((i) < 0 ? -(i) : (i))
26    
27 gregl 3.2 RTREE qtrunk; /* our quadtree trunk */
28 gregl 3.10 double qtDepthEps = .05; /* epsilon to compare depths (z fraction) */
29 gregl 3.2 int qtMinNodesiz = 2; /* minimum node dimension (pixels) */
30 gregl 3.5 struct rleaves qtL; /* our pile of leaves */
31 gregl 3.2
32 gregl 3.15 static int4 falleaves; /* our list of fallen leaves */
33    
34     #define composted(li) (qtL.bl <= qtL.tl ? \
35     ((li) < qtL.bl || (li) >= qtL.tl) : \
36     ((li) < qtL.bl && (li) >= qtL.tl))
37    
38 gregl 3.1 #define TBUNDLESIZ 409 /* number of twigs in a bundle */
39    
40     static RTREE **twigbundle; /* free twig blocks (NULL term.) */
41     static int nexttwig; /* next free twig */
42    
43    
44     static RTREE *
45     newtwig() /* allocate a twig */
46     {
47     register int bi;
48    
49     if (twigbundle == NULL) { /* initialize */
50     twigbundle = (RTREE **)malloc(sizeof(RTREE *));
51     if (twigbundle == NULL)
52     goto memerr;
53     twigbundle[0] = NULL;
54     }
55     bi = nexttwig / TBUNDLESIZ;
56     if (twigbundle[bi] == NULL) { /* new block */
57     twigbundle = (RTREE **)realloc((char *)twigbundle,
58     (bi+2)*sizeof(RTREE *));
59     if (twigbundle == NULL)
60     goto memerr;
61     twigbundle[bi] = (RTREE *)calloc(TBUNDLESIZ, sizeof(RTREE));
62     if (twigbundle[bi] == NULL)
63     goto memerr;
64     twigbundle[bi+1] = NULL;
65     }
66     /* nexttwig++ % TBUNDLESIZ */
67     return(twigbundle[bi] + (nexttwig++ - bi*TBUNDLESIZ));
68     memerr:
69     error(SYSTEM, "out of memory in newtwig");
70     }
71    
72    
73 gregl 3.3 qtFreeTree(really) /* free allocated twigs */
74 gregl 3.1 int really;
75     {
76     register int i;
77    
78 gregl 3.7 qtrunk.flgs = CH_ANY; /* chop down tree */
79 gregl 3.1 if (twigbundle == NULL)
80     return;
81 gregl 3.7 i = (TBUNDLESIZ-1+nexttwig)/TBUNDLESIZ;
82     nexttwig = 0;
83 gregl 3.1 if (!really) { /* just clear allocated blocks */
84 gregl 3.7 while (i--)
85 gregl 3.1 bzero((char *)twigbundle[i], TBUNDLESIZ*sizeof(RTREE));
86     return;
87     }
88     /* else "really" means free up memory */
89     for (i = 0; twigbundle[i] != NULL; i++)
90     free((char *)twigbundle[i]);
91     free((char *)twigbundle);
92     twigbundle = NULL;
93     }
94    
95    
96 gregl 3.11 #define LEAFSIZ (3*sizeof(float)+sizeof(int4)+\
97 gregl 3.10 sizeof(TMbright)+6*sizeof(BYTE))
98 gregl 3.5
99 gregl 3.1 int
100     qtAllocLeaves(n) /* allocate space for n leaves */
101 gregl 3.5 register int n;
102 gregl 3.1 {
103     unsigned nbytes;
104     register unsigned i;
105    
106 gregl 3.3 qtFreeTree(0); /* make sure tree is empty */
107 gregl 3.1 if (n <= 0)
108     return(0);
109 gregl 3.5 if (qtL.nl >= n)
110     return(qtL.nl);
111     else if (qtL.nl > 0)
112     free(qtL.base);
113 gregl 3.1 /* round space up to nearest power of 2 */
114 gregl 3.5 nbytes = n*LEAFSIZ + 8;
115 gregl 3.1 for (i = 1024; nbytes > i; i <<= 1)
116     ;
117 gregl 3.5 n = (i - 8) / LEAFSIZ; /* should we make sure n is even? */
118     qtL.base = (char *)malloc(n*LEAFSIZ);
119     if (qtL.base == NULL)
120     return(0);
121     /* assign larger alignment types earlier */
122     qtL.wp = (float (*)[3])qtL.base;
123 gregl 3.11 qtL.wd = (int4 *)(qtL.wp + n);
124 gregl 3.10 qtL.brt = (TMbright *)(qtL.wd + n);
125 gregl 3.5 qtL.chr = (BYTE (*)[3])(qtL.brt + n);
126     qtL.rgb = (BYTE (*)[3])(qtL.chr + n);
127     qtL.nl = n;
128     qtL.tml = qtL.bl = qtL.tl = 0;
129 gregl 3.15 falleaves = -1;
130 gregl 3.5 return(n);
131 gregl 3.1 }
132    
133 gregl 3.5 #undef LEAFSIZ
134 gregl 3.1
135 gregl 3.5
136 gregl 3.1 qtFreeLeaves() /* free our allocated leaves and twigs */
137     {
138 gregl 3.3 qtFreeTree(1); /* free tree also */
139 gregl 3.5 if (qtL.nl <= 0)
140 gregl 3.1 return;
141 gregl 3.5 free(qtL.base);
142     qtL.base = NULL;
143     qtL.nl = 0;
144 gregl 3.1 }
145    
146    
147     static
148     shaketree(tp) /* shake dead leaves from tree */
149     register RTREE *tp;
150     {
151     register int i, li;
152    
153     for (i = 0; i < 4; i++)
154 gregl 3.5 if (tp->flgs & BRF(i)) {
155 gregl 3.2 shaketree(tp->k[i].b);
156 gregl 3.5 if (is_stump(tp->k[i].b))
157     tp->flgs &= ~BRF(i);
158     } else if (tp->flgs & LFF(i)) {
159     li = tp->k[i].li;
160 gregl 3.15 if (composted(li))
161 gregl 3.5 tp->flgs &= ~LFF(i);
162 gregl 3.1 }
163     }
164    
165    
166     int
167     qtCompost(pct) /* free up some leaves */
168     int pct;
169     {
170 gregl 3.15 register int4 *fl;
171 gregl 3.5 int nused, nclear, nmapped;
172 gregl 3.1 /* figure out how many leaves to clear */
173 gregl 3.5 nclear = qtL.nl * pct / 100;
174     nused = qtL.tl - qtL.bl;
175     if (nused <= 0) nused += qtL.nl;
176     nclear -= qtL.nl - nused;
177 gregl 3.1 if (nclear <= 0)
178     return(0);
179     if (nclear >= nused) { /* clear them all */
180 gregl 3.3 qtFreeTree(0);
181 gregl 3.5 qtL.tml = qtL.bl = qtL.tl = 0;
182 gregl 3.15 falleaves = -1;
183 gregl 3.1 return(nused);
184     }
185     /* else clear leaves from bottom */
186 gregl 3.5 nmapped = qtL.tml - qtL.bl;
187     if (nmapped < 0) nmapped += qtL.nl;
188     qtL.bl += nclear;
189     if (qtL.bl >= qtL.nl) qtL.bl -= qtL.nl;
190     if (nmapped <= nclear) qtL.tml = qtL.bl;
191 gregl 3.15 shaketree(&qtrunk); /* dereference composted leaves */
192     for (fl = &falleaves; *fl >= 0; fl = qtL.wd + *fl)
193     while (composted(*fl))
194     if ((*fl = qtL.wd[*fl]) < 0)
195     return(nclear);
196 gregl 3.1 return(nclear);
197     }
198    
199    
200 gregl 3.5 int
201 gregl 3.3 qtFindLeaf(x, y) /* find closest leaf to (x,y) */
202     int x, y;
203     {
204     register RTREE *tp = &qtrunk;
205 gregl 3.5 int li = -1;
206 gregl 3.3 int x0=0, y0=0, x1=odev.hres, y1=odev.vres;
207     int mx, my;
208     register int q;
209     /* check limits */
210     if (x < 0 || x >= odev.hres || y < 0 || y >= odev.vres)
211 gregl 3.5 return(-1);
212 gregl 3.3 /* find nearby leaf in our tree */
213     for ( ; ; ) {
214     for (q = 0; q < 4; q++) /* find any leaf this level */
215 gregl 3.5 if (tp->flgs & LFF(q)) {
216     li = tp->k[q].li;
217 gregl 3.3 break;
218     }
219     q = 0; /* which quadrant are we? */
220     mx = (x0 + x1) >> 1;
221     my = (y0 + y1) >> 1;
222     if (x < mx) x1 = mx;
223     else {x0 = mx; q |= 01;}
224     if (y < my) y1 = my;
225     else {y0 = my; q |= 02;}
226     if (tp->flgs & BRF(q)) { /* branch down if not a leaf */
227     tp = tp->k[q].b;
228     continue;
229     }
230 gregl 3.5 if (tp->flgs & LFF(q)) /* good shot! */
231     return(tp->k[q].li);
232     return(li); /* else return what we have */
233 gregl 3.3 }
234     }
235    
236    
237 gregl 3.1 static
238 gregl 3.15 putleaf(li, drop) /* put a leaf in our tree */
239     register int li;
240     int drop;
241 gregl 3.1 {
242     register RTREE *tp = &qtrunk;
243     int x0=0, y0=0, x1=odev.hres, y1=odev.vres;
244 gregl 3.15 register int lo = -1;
245 gregl 3.11 double d2;
246 gregl 3.1 int x, y, mx, my;
247     double z;
248 gregl 3.11 FVECT ip, wp, vd;
249 gregl 3.1 register int q;
250 gregl 3.15 /* check for dead leaf */
251     if (!qtL.chr[li][1] && !(qtL.chr[li][0] | qtL.chr[li][2]))
252     return(0);
253 gregl 3.10 /* compute leaf location in view */
254 gregl 3.5 VCOPY(wp, qtL.wp[li]);
255 gregl 3.1 viewloc(ip, &odev.v, wp);
256     if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1.
257     || ip[1] < 0. || ip[1] >= 1.)
258 gregl 3.15 goto dropit; /* behind or outside view */
259 gregl 3.10 #ifdef DEBUG
260     if (odev.v.type == VT_PAR | odev.v.vfore > FTINY)
261 gregl 3.15 error(INTERNAL, "bad view assumption in putleaf");
262 gregl 3.10 #endif
263     for (q = 0; q < 3; q++)
264 gregl 3.11 vd[q] = (wp[q] - odev.v.vp[q])/ip[2];
265     d2 = fdir2diff(qtL.wd[li], vd);
266 gregl 3.12 #ifdef MAXDIFF2
267 gregl 3.10 if (d2 > MAXDIFF2)
268 gregl 3.15 goto dropit; /* leaf dir. too far off */
269 gregl 3.12 #endif
270 gregl 3.1 x = ip[0] * odev.hres;
271     y = ip[1] * odev.vres;
272     z = ip[2];
273     /* find the place for it */
274     for ( ; ; ) {
275     q = 0; /* which quadrant? */
276     mx = (x0 + x1) >> 1;
277     my = (y0 + y1) >> 1;
278     if (x < mx) x1 = mx;
279     else {x0 = mx; q |= 01;}
280     if (y < my) y1 = my;
281     else {y0 = my; q |= 02;}
282     if (tp->flgs & BRF(q)) { /* move to next branch */
283     tp->flgs |= CHF(q); /* not sure; guess */
284     tp = tp->k[q].b;
285     continue;
286     }
287 gregl 3.5 if (!(tp->flgs & LFF(q))) { /* found stem for leaf */
288     tp->k[q].li = li;
289     tp->flgs |= CHLFF(q);
290 gregl 3.15 return(1);
291 gregl 3.1 }
292 gregl 3.10 if (lo != tp->k[q].li) { /* check old leaf */
293 gregl 3.5 lo = tp->k[q].li;
294     VCOPY(wp, qtL.wp[lo]);
295 gregl 3.1 viewloc(ip, &odev.v, wp);
296     }
297     /* is node minimum size? */
298 gregl 3.10 if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) {
299     if (z > (1.+qtDepthEps)*ip[2])
300 gregl 3.15 break; /* old one closer */
301 gregl 3.10 if (z >= (1.-qtDepthEps)*ip[2] &&
302 gregl 3.11 fdir2diff(qtL.wd[lo], vd) < d2)
303 gregl 3.15 break; /* old one better */
304     tp->k[q].li = li; /* attach new */
305 gregl 3.1 tp->flgs |= CHF(q);
306 gregl 3.15 li = lo; /* drop old... */
307 gregl 3.1 break;
308     }
309 gregl 3.5 tp->flgs &= ~LFF(q); /* else grow tree */
310     tp->flgs |= CHBRF(q);
311 gregl 3.1 tp = tp->k[q].b = newtwig();
312     q = 0; /* old leaf -> new branch */
313     mx = ip[0] * odev.hres;
314     my = ip[1] * odev.vres;
315     if (mx >= (x0 + x1) >> 1) q |= 01;
316     if (my >= (y0 + y1) >> 1) q |= 02;
317 gregl 3.11 tp->flgs = CH_ANY|LFF(q); /* all new */
318 gregl 3.5 tp->k[q].li = lo;
319 gregl 3.1 }
320 gregl 3.15 dropit:
321     if (drop) {
322     qtL.chr[li][0] = qtL.chr[li][1] = qtL.chr[li][2] = 0;
323     qtL.wd[li] = falleaves;
324     falleaves = li;
325     }
326     return(li == lo);
327 gregl 3.1 }
328    
329    
330 gregl 3.10 dev_value(c, p, v) /* add a pixel value to our quadtree */
331 gregl 3.1 COLR c;
332 gregl 3.10 FVECT p, v;
333 gregl 3.1 {
334 gregl 3.5 register int li;
335 gregl 3.15 int mapit;
336     /* grab a leaf */
337     if (falleaves >= 0) { /* check for fallen leaves */
338     li = falleaves;
339     falleaves = qtL.wd[li];
340     mapit = qtL.tml <= qtL.tl ?
341     (li < qtL.tml || li >= qtL.tl) :
342     (li < qtL.tml && li >= qtL.tl) ;
343     } else { /* else allocate new one */
344     li = qtL.tl++;
345     if (qtL.tl >= qtL.nl) /* advance to next leaf in ring */
346     qtL.tl = 0;
347     if (qtL.tl == qtL.bl) /* need to shake some free */
348     qtCompost(LFREEPCT);
349     mapit = 0; /* we'll map it later */
350     }
351 gregl 3.5 VCOPY(qtL.wp[li], p);
352 gregl 3.11 qtL.wd[li] = encodedir(v);
353 gregl 3.5 tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1);
354 gregl 3.15 if (putleaf(li, 1) && mapit)
355     tmMapPixels(qtL.rgb+li, qtL.brt+li, qtL.chr+li, 1);
356 gregl 3.1 }
357    
358    
359     qtReplant() /* replant our tree using new view */
360     {
361     register int i;
362 gregl 3.5 /* anything to replant? */
363     if (qtL.bl == qtL.tl)
364 gregl 3.1 return;
365 gregl 3.5 qtFreeTree(0); /* blow the old tree away */
366     /* regrow it in new place */
367     for (i = qtL.bl; i != qtL.tl; ) {
368 gregl 3.15 putleaf(i, 0);
369 gregl 3.5 if (++i >= qtL.nl) i = 0;
370 gregl 3.1 }
371     }
372    
373    
374 gregl 3.5 qtMapLeaves(redo) /* map our leaves to RGB */
375     int redo;
376     {
377     int aorg, alen, borg, blen;
378 gregl 3.6 /* recompute mapping? */
379     if (redo)
380     qtL.tml = qtL.bl;
381 gregl 3.5 /* already done? */
382     if (qtL.tml == qtL.tl)
383     return(1);
384     /* compute segments */
385     aorg = qtL.tml;
386     if (qtL.tl >= aorg) {
387     alen = qtL.tl - aorg;
388     blen = 0;
389     } else {
390     alen = qtL.nl - aorg;
391     borg = 0;
392     blen = qtL.tl;
393     }
394     /* (re)compute tone mapping? */
395     if (qtL.tml == qtL.bl) {
396     tmClearHisto();
397     tmAddHisto(qtL.brt+aorg, alen, 1);
398     if (blen > 0)
399     tmAddHisto(qtL.brt+borg, blen, 1);
400     if (tmComputeMapping(0., 0., 0.) != TM_E_OK)
401     return(0);
402     }
403     if (tmMapPixels(qtL.rgb+aorg, qtL.brt+aorg,
404     qtL.chr+aorg, alen) != TM_E_OK)
405     return(0);
406     if (blen > 0)
407     tmMapPixels(qtL.rgb+borg, qtL.brt+borg,
408     qtL.chr+borg, blen);
409     qtL.tml = qtL.tl;
410     return(1);
411 gregl 3.1 }