ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rhd_qtree.c
Revision: 3.21
Committed: Wed Apr 23 00:52:33 2003 UTC (21 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.20: +1 -1 lines
Log Message:
Added (void *) cast to realloc calls

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 * Quadtree driver support routines.
6 */
7
8 #include "standard.h"
9 #include "rhd_qtree.h"
10 /* quantity of leaves to free at a time */
11 #ifndef LFREEPCT
12 #define LFREEPCT 25
13 #endif
14 /* maximum allowed angle difference (deg.) */
15 #ifndef MAXANG
16 #define MAXANG 20
17 #endif
18 #if MAXANG>0
19 #define MAXDIFF2 ( MAXANG*MAXANG * (PI*PI/180./180.))
20 #endif
21
22 #define abs(i) ((i) < 0 ? -(i) : (i))
23
24 RTREE qtrunk; /* our quadtree trunk */
25 double qtDepthEps = .05; /* epsilon to compare depths (z fraction) */
26 int qtMinNodesiz = 2; /* minimum node dimension (pixels) */
27 struct rleaves qtL; /* our pile of leaves */
28
29 int rayqleft = 0; /* rays left to queue before flush */
30
31 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 #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((void *)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 qtFreeTree(really) /* free allocated twigs */
73 int really;
74 {
75 register int i;
76
77 qtrunk.flgs = CH_ANY; /* chop down tree */
78 if (twigbundle == NULL)
79 return;
80 i = (TBUNDLESIZ-1+nexttwig)/TBUNDLESIZ;
81 nexttwig = 0;
82 if (!really) { /* just clear allocated blocks */
83 while (i--)
84 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 free((void *)twigbundle[i]);
90 free((void *)twigbundle);
91 twigbundle = NULL;
92 }
93
94
95 #define LEAFSIZ (3*sizeof(float)+sizeof(int4)+\
96 sizeof(TMbright)+6*sizeof(BYTE))
97
98 int
99 qtAllocLeaves(n) /* allocate space for n leaves */
100 register int n;
101 {
102 unsigned nbytes;
103 register unsigned i;
104
105 qtFreeTree(0); /* make sure tree is empty */
106 if (n <= 0)
107 return(0);
108 if (qtL.nl >= n)
109 return(qtL.nl);
110 else if (qtL.nl > 0)
111 free(qtL.base);
112 /* round space up to nearest power of 2 */
113 nbytes = n*LEAFSIZ + 8;
114 for (i = 1024; nbytes > i; i <<= 1)
115 ;
116 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 qtL.wd = (int4 *)(qtL.wp + n);
123 qtL.brt = (TMbright *)(qtL.wd + n);
124 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 falleaves = -1;
129 return(n);
130 }
131
132 #undef LEAFSIZ
133
134
135 qtFreeLeaves() /* free our allocated leaves and twigs */
136 {
137 qtFreeTree(1); /* free tree also */
138 if (qtL.nl <= 0)
139 return;
140 free(qtL.base);
141 qtL.base = NULL;
142 qtL.nl = 0;
143 }
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 if (tp->flgs & BRF(i)) {
154 shaketree(tp->k[i].b);
155 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 if (composted(li))
160 tp->flgs &= ~LFF(i);
161 }
162 }
163
164
165 int
166 qtCompost(pct) /* free up some leaves */
167 int pct;
168 {
169 register int4 *fl;
170 int nused, nclear, nmapped;
171 /* figure out how many leaves to clear */
172 nclear = qtL.nl * pct / 100;
173 nused = qtL.tl - qtL.bl;
174 if (nused <= 0) nused += qtL.nl;
175 nclear -= qtL.nl - nused;
176 if (nclear <= 0)
177 return(0);
178 if (nclear >= nused) { /* clear them all */
179 qtFreeTree(0);
180 qtL.tml = qtL.bl = qtL.tl = 0;
181 falleaves = -1;
182 return(nused);
183 }
184 /* else clear leaves from bottom */
185 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 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 return(nclear);
196 }
197
198
199 int
200 qtFindLeaf(x, y) /* find closest leaf to (x,y) */
201 int x, y;
202 {
203 register RTREE *tp = &qtrunk;
204 int li = -1;
205 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 return(-1);
211 /* find nearby leaf in our tree */
212 for ( ; ; ) {
213 for (q = 0; q < 4; q++) /* find any leaf this level */
214 if (tp->flgs & LFF(q)) {
215 li = tp->k[q].li;
216 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 if (tp->flgs & LFF(q)) /* good shot! */
230 return(tp->k[q].li);
231 return(li); /* else return what we have */
232 }
233 }
234
235
236 static
237 putleaf(li, drop) /* put a leaf in our tree */
238 register int li;
239 int drop;
240 {
241 register RTREE *tp = &qtrunk;
242 int x0=0, y0=0, x1=odev.hres, y1=odev.vres;
243 register int lo = -1;
244 double d2;
245 int x, y, mx, my;
246 double z;
247 FVECT ip, wp, vd;
248 register int q;
249 /* check for dead leaf */
250 if (!qtL.chr[li][1] && !(qtL.chr[li][0] | qtL.chr[li][2]))
251 return(0);
252 /* compute leaf location in view */
253 VCOPY(wp, qtL.wp[li]);
254 viewloc(ip, &odev.v, wp);
255 if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1.
256 || ip[1] < 0. || ip[1] >= 1.)
257 goto dropit; /* behind or outside view */
258 #ifdef DEBUG
259 if (odev.v.type == VT_PAR | odev.v.vfore > FTINY)
260 error(INTERNAL, "bad view assumption in putleaf");
261 #endif
262 for (q = 0; q < 3; q++)
263 vd[q] = (wp[q] - odev.v.vp[q])/ip[2];
264 d2 = fdir2diff(qtL.wd[li], vd);
265 #ifdef MAXDIFF2
266 if (d2 > MAXDIFF2)
267 goto dropit; /* leaf dir. too far off */
268 #endif
269 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 if (!(tp->flgs & LFF(q))) { /* found stem for leaf */
287 tp->k[q].li = li;
288 tp->flgs |= CHLFF(q);
289 return(1);
290 }
291 if (lo != tp->k[q].li) { /* check old leaf */
292 lo = tp->k[q].li;
293 VCOPY(wp, qtL.wp[lo]);
294 viewloc(ip, &odev.v, wp);
295 }
296 /* is node minimum size? */
297 if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) {
298 if (z > (1.+qtDepthEps)*ip[2])
299 break; /* old one closer */
300 if (z >= (1.-qtDepthEps)*ip[2] &&
301 fdir2diff(qtL.wd[lo], vd) < d2)
302 break; /* old one better */
303 tp->k[q].li = li; /* attach new */
304 tp->flgs |= CHF(q);
305 li = lo; /* drop old... */
306 break;
307 }
308 tp->flgs &= ~LFF(q); /* else grow tree */
309 tp->flgs |= CHBRF(q);
310 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 tp->flgs = CH_ANY|LFF(q); /* all new */
317 tp->k[q].li = lo;
318 }
319 dropit:
320 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 return(li == lo);
329 }
330
331
332 dev_value(c, d, p) /* add a pixel value to our quadtree */
333 COLR c;
334 FVECT d, p;
335 {
336 register int li;
337 int mapit;
338 /* grab a leaf */
339 if (!imm_mode && falleaves >= 0) { /* check for fallen leaves */
340 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 } else { /* else allocate new one */
346 li = qtL.tl++;
347 if (qtL.tl >= qtL.nl) /* next leaf in ring */
348 qtL.tl = 0;
349 if (qtL.tl == qtL.bl) /* need to shake some free */
350 qtCompost(LFREEPCT);
351 mapit = 0; /* we'll map it later */
352 }
353 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 tmCvColrs(&qtL.brt[li], qtL.chr[li], (COLR *)c, 1);
359 if (putleaf(li, 1)) {
360 if (mapit)
361 tmMapPixels((BYTE *)(qtL.rgb+li), qtL.brt+li,
362 (BYTE *)(qtL.chr+li), 1);
363 if (--rayqleft == 0)
364 dev_flush(); /* flush output */
365 }
366 }
367
368
369 qtReplant() /* replant our tree using new view */
370 {
371 register int i;
372 /* anything to replant? */
373 if (qtL.bl == qtL.tl)
374 return;
375 qtFreeTree(0); /* blow the old tree away */
376 /* regrow it in new place */
377 for (i = qtL.bl; i != qtL.tl; ) {
378 putleaf(i, 0);
379 if (++i >= qtL.nl) i = 0;
380 }
381 }
382
383
384 qtMapLeaves(redo) /* map our leaves to RGB */
385 int redo;
386 {
387 int aorg, alen, borg, blen;
388 /* recompute mapping? */
389 if (redo)
390 qtL.tml = qtL.bl;
391 /* 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 if (tmMapPixels((BYTE *)(qtL.rgb+aorg), qtL.brt+aorg,
414 (BYTE *)(qtL.chr+aorg), alen) != TM_E_OK)
415 return(0);
416 if (blen > 0)
417 tmMapPixels((BYTE *)(qtL.rgb+borg), qtL.brt+borg,
418 (BYTE *)(qtL.chr+borg), blen);
419 qtL.tml = qtL.tl;
420 return(1);
421 }