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