29 |
|
int qtMinNodesiz = 2; /* minimum node dimension (pixels) */ |
30 |
|
struct rleaves qtL; /* our pile of leaves */ |
31 |
|
|
32 |
+ |
static int4 falleaves; /* our list of fallen leaves */ |
33 |
+ |
|
34 |
+ |
#define composted(li) (qtL.bl <= qtL.tl ? \ |
35 |
+ |
((li) < qtL.bl || (li) >= qtL.tl) : \ |
36 |
+ |
((li) < qtL.bl && (li) >= qtL.tl)) |
37 |
+ |
|
38 |
|
#define TBUNDLESIZ 409 /* number of twigs in a bundle */ |
39 |
|
|
40 |
|
static RTREE **twigbundle; /* free twig blocks (NULL term.) */ |
126 |
|
qtL.rgb = (BYTE (*)[3])(qtL.chr + n); |
127 |
|
qtL.nl = n; |
128 |
|
qtL.tml = qtL.bl = qtL.tl = 0; |
129 |
+ |
falleaves = -1; |
130 |
|
return(n); |
131 |
|
} |
132 |
|
|
157 |
|
tp->flgs &= ~BRF(i); |
158 |
|
} else if (tp->flgs & LFF(i)) { |
159 |
|
li = tp->k[i].li; |
160 |
< |
if (qtL.bl < qtL.tl ? |
154 |
< |
(li < qtL.bl || li >= qtL.tl) : |
155 |
< |
(li < qtL.bl && li >= qtL.tl)) |
160 |
> |
if (composted(li)) |
161 |
|
tp->flgs &= ~LFF(i); |
162 |
|
} |
163 |
|
} |
167 |
|
qtCompost(pct) /* free up some leaves */ |
168 |
|
int pct; |
169 |
|
{ |
170 |
+ |
register int4 *fl; |
171 |
|
int nused, nclear, nmapped; |
166 |
– |
|
172 |
|
/* figure out how many leaves to clear */ |
173 |
|
nclear = qtL.nl * pct / 100; |
174 |
|
nused = qtL.tl - qtL.bl; |
179 |
|
if (nclear >= nused) { /* clear them all */ |
180 |
|
qtFreeTree(0); |
181 |
|
qtL.tml = qtL.bl = qtL.tl = 0; |
182 |
+ |
falleaves = -1; |
183 |
|
return(nused); |
184 |
|
} |
185 |
|
/* else clear leaves from bottom */ |
188 |
|
qtL.bl += nclear; |
189 |
|
if (qtL.bl >= qtL.nl) qtL.bl -= qtL.nl; |
190 |
|
if (nmapped <= nclear) qtL.tml = qtL.bl; |
191 |
< |
shaketree(&qtrunk); |
191 |
> |
shaketree(&qtrunk); /* dereference composted leaves */ |
192 |
> |
for (fl = &falleaves; *fl >= 0; fl = qtL.wd + *fl) |
193 |
> |
while (composted(*fl)) |
194 |
> |
if ((*fl = qtL.wd[*fl]) < 0) |
195 |
> |
return(nclear); |
196 |
|
return(nclear); |
197 |
|
} |
198 |
|
|
235 |
|
|
236 |
|
|
237 |
|
static |
238 |
< |
addleaf(li) /* add a leaf to our tree */ |
239 |
< |
int li; |
238 |
> |
putleaf(li, drop) /* put a leaf in our tree */ |
239 |
> |
register int li; |
240 |
> |
int drop; |
241 |
|
{ |
242 |
|
register RTREE *tp = &qtrunk; |
243 |
|
int x0=0, y0=0, x1=odev.hres, y1=odev.vres; |
244 |
< |
int lo = -1; |
244 |
> |
register int lo = -1; |
245 |
|
double d2; |
246 |
|
int x, y, mx, my; |
247 |
|
double z; |
248 |
|
FVECT ip, wp, vd; |
249 |
|
register int q; |
250 |
+ |
/* check for dead leaf */ |
251 |
+ |
if (!qtL.chr[li][1] && !(qtL.chr[li][0] | qtL.chr[li][2])) |
252 |
+ |
return(0); |
253 |
|
/* compute leaf location in view */ |
254 |
|
VCOPY(wp, qtL.wp[li]); |
255 |
|
viewloc(ip, &odev.v, wp); |
256 |
|
if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1. |
257 |
|
|| ip[1] < 0. || ip[1] >= 1.) |
258 |
< |
return(-1); /* behind or outside view */ |
258 |
> |
goto dropit; /* behind or outside view */ |
259 |
|
#ifdef DEBUG |
260 |
|
if (odev.v.type == VT_PAR | odev.v.vfore > FTINY) |
261 |
< |
error(INTERNAL, "bad view assumption in addleaf"); |
261 |
> |
error(INTERNAL, "bad view assumption in putleaf"); |
262 |
|
#endif |
263 |
|
for (q = 0; q < 3; q++) |
264 |
|
vd[q] = (wp[q] - odev.v.vp[q])/ip[2]; |
265 |
|
d2 = fdir2diff(qtL.wd[li], vd); |
266 |
|
#ifdef MAXDIFF2 |
267 |
|
if (d2 > MAXDIFF2) |
268 |
< |
return(0); /* leaf dir. too far off */ |
268 |
> |
goto dropit; /* leaf dir. too far off */ |
269 |
|
#endif |
270 |
|
x = ip[0] * odev.hres; |
271 |
|
y = ip[1] * odev.vres; |
287 |
|
if (!(tp->flgs & LFF(q))) { /* found stem for leaf */ |
288 |
|
tp->k[q].li = li; |
289 |
|
tp->flgs |= CHLFF(q); |
290 |
< |
break; |
290 |
> |
return(1); |
291 |
|
} |
292 |
|
if (lo != tp->k[q].li) { /* check old leaf */ |
293 |
|
lo = tp->k[q].li; |
297 |
|
/* is node minimum size? */ |
298 |
|
if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) { |
299 |
|
if (z > (1.+qtDepthEps)*ip[2]) |
300 |
< |
return(0); /* old one closer */ |
300 |
> |
break; /* old one closer */ |
301 |
|
if (z >= (1.-qtDepthEps)*ip[2] && |
302 |
|
fdir2diff(qtL.wd[lo], vd) < d2) |
303 |
< |
return(0); /* old one better */ |
304 |
< |
tp->k[q].li = li; /* else new one is */ |
303 |
> |
break; /* old one better */ |
304 |
> |
tp->k[q].li = li; /* attach new */ |
305 |
|
tp->flgs |= CHF(q); |
306 |
+ |
li = lo; /* drop old... */ |
307 |
|
break; |
308 |
|
} |
309 |
|
tp->flgs &= ~LFF(q); /* else grow tree */ |
317 |
|
tp->flgs = CH_ANY|LFF(q); /* all new */ |
318 |
|
tp->k[q].li = lo; |
319 |
|
} |
320 |
< |
return(1); /* done */ |
320 |
> |
dropit: |
321 |
> |
if (drop) { |
322 |
> |
qtL.chr[li][0] = qtL.chr[li][1] = qtL.chr[li][2] = 0; |
323 |
> |
qtL.wd[li] = falleaves; |
324 |
> |
falleaves = li; |
325 |
> |
} |
326 |
> |
return(li == lo); |
327 |
|
} |
328 |
|
|
329 |
|
|
332 |
|
FVECT p, v; |
333 |
|
{ |
334 |
|
register int li; |
335 |
< |
|
336 |
< |
li = qtL.tl++; |
337 |
< |
if (qtL.tl >= qtL.nl) /* advance to next leaf in ring */ |
338 |
< |
qtL.tl = 0; |
339 |
< |
if (qtL.tl == qtL.bl) /* need to shake some free */ |
340 |
< |
qtCompost(LFREEPCT); |
335 |
> |
int mapit; |
336 |
> |
/* grab a leaf */ |
337 |
> |
if (falleaves >= 0) { /* check for fallen leaves */ |
338 |
> |
li = falleaves; |
339 |
> |
falleaves = qtL.wd[li]; |
340 |
> |
mapit = qtL.tml <= qtL.tl ? |
341 |
> |
(li < qtL.tml || li >= qtL.tl) : |
342 |
> |
(li < qtL.tml && li >= qtL.tl) ; |
343 |
> |
} else { /* else allocate new one */ |
344 |
> |
li = qtL.tl++; |
345 |
> |
if (qtL.tl >= qtL.nl) /* advance to next leaf in ring */ |
346 |
> |
qtL.tl = 0; |
347 |
> |
if (qtL.tl == qtL.bl) /* need to shake some free */ |
348 |
> |
qtCompost(LFREEPCT); |
349 |
> |
mapit = 0; /* we'll map it later */ |
350 |
> |
} |
351 |
|
VCOPY(qtL.wp[li], p); |
352 |
|
qtL.wd[li] = encodedir(v); |
353 |
|
tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1); |
354 |
< |
if (!addleaf(li)) |
355 |
< |
qtL.tl = li; /* unget this leaf */ |
354 |
> |
if (putleaf(li, 1) && mapit) |
355 |
> |
tmMapPixels(qtL.rgb+li, qtL.brt+li, qtL.chr+li, 1); |
356 |
|
} |
357 |
|
|
358 |
|
|
365 |
|
qtFreeTree(0); /* blow the old tree away */ |
366 |
|
/* regrow it in new place */ |
367 |
|
for (i = qtL.bl; i != qtL.tl; ) { |
368 |
< |
addleaf(i); |
368 |
> |
putleaf(i, 0); |
369 |
|
if (++i >= qtL.nl) i = 0; |
370 |
|
} |
371 |
|
} |