ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_qtree.c
Revision: 3.24
Committed: Mon Jul 21 22:30:18 2003 UTC (21 years, 2 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.23: +3 -2 lines
Log Message:
Eliminated copystruct() macro, which is unnecessary in ANSI.
Reduced ambiguity warnings for nested if/if/else clauses.

File Contents

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