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