ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_qtree.c
Revision: 3.20
Committed: Tue Mar 4 05:49:21 2003 UTC (21 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 3.19: +7 -6 lines
Log Message:
Moved dircode.c and fixed prototype compiles in hd directory

File Contents

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