ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_qtree.c
Revision: 3.14
Committed: Mon Dec 29 17:31:45 1997 UTC (26 years, 9 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.13: +6 -18 lines
Log Message:
consolidated newleaf() with dev_value()

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