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