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 |
|
|
22 |
+ |
#define MAXDIFF2 (long)( MAXANG*MAXANG /90./90.*(1L<<15)*(1L<<15)) |
23 |
+ |
|
24 |
+ |
#define abs(i) ((i) < 0 ? -(i) : (i)) |
25 |
+ |
|
26 |
|
RTREE qtrunk; /* our quadtree trunk */ |
27 |
< |
double qtDepthEps = .02; /* epsilon to compare depths (z fraction) */ |
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 |
|
|
33 |
|
static RTREE **twigbundle; /* free twig blocks (NULL term.) */ |
34 |
|
static int nexttwig; /* next free twig */ |
35 |
|
|
36 |
+ |
#define ungetleaf(li) (qtL.tl=(li)) /* dangerous if used improperly */ |
37 |
|
|
38 |
+ |
|
39 |
|
static RTREE * |
40 |
|
newtwig() /* allocate a twig */ |
41 |
|
{ |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
< |
#define LEAFSIZ (3*sizeof(float)+sizeof(TMbright)+6*sizeof(BYTE)) |
105 |
> |
#define LEAFSIZ (3*sizeof(float)+2*sizeof(short)+\ |
106 |
> |
sizeof(TMbright)+6*sizeof(BYTE)) |
107 |
|
|
108 |
|
int |
109 |
|
qtAllocLeaves(n) /* allocate space for n leaves */ |
129 |
|
return(0); |
130 |
|
/* assign larger alignment types earlier */ |
131 |
|
qtL.wp = (float (*)[3])qtL.base; |
132 |
< |
qtL.brt = (TMbright *)(qtL.wp + n); |
132 |
> |
qtL.wd = (short (*)[2])(qtL.wp + n); |
133 |
> |
qtL.brt = (TMbright *)(qtL.wd + n); |
134 |
|
qtL.chr = (BYTE (*)[3])(qtL.brt + n); |
135 |
|
qtL.rgb = (BYTE (*)[3])(qtL.chr + n); |
136 |
|
qtL.nl = n; |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
+ |
static |
206 |
+ |
encodedir(pa, dv) /* encode a normalized direction vector */ |
207 |
+ |
short pa[2]; |
208 |
+ |
FVECT dv; |
209 |
+ |
{ |
210 |
+ |
pa[1] = 0; |
211 |
+ |
if (dv[2] >= 1.) |
212 |
+ |
pa[0] = (1L<<15)-1; |
213 |
+ |
else if (dv[2] <= -1.) |
214 |
+ |
pa[0] = -((1L<<15)-1); |
215 |
+ |
else { |
216 |
+ |
pa[0] = ((1L<<15)-1)/(PI/2.) * asin(dv[2]); |
217 |
+ |
pa[1] = ((1L<<15)-1)/PI * atan2(dv[1], dv[0]); |
218 |
+ |
} |
219 |
+ |
} |
220 |
+ |
|
221 |
+ |
|
222 |
+ |
#define ALTSHFT 5 |
223 |
+ |
#define NALT (1<<ALTSHFT) |
224 |
+ |
#define azisft(alt) azisftab[abs(alt)>>(15-ALTSHFT)] |
225 |
+ |
|
226 |
+ |
static unsigned short azisftab[NALT]; |
227 |
+ |
|
228 |
+ |
static |
229 |
+ |
azisftinit(alt) /* initialize azimuth scale factor table */ |
230 |
+ |
int alt; |
231 |
+ |
{ |
232 |
+ |
register int i; |
233 |
+ |
|
234 |
+ |
for (i = NALT; i--; ) |
235 |
+ |
azisftab[i] = 2.*(1L<<15) * cos(PI/2.*(i+.5)/NALT); |
236 |
+ |
return(azisft(alt)); |
237 |
+ |
} |
238 |
+ |
|
239 |
+ |
#define azisf(alt) (azisftab[0] ? azisft(alt) : azisftinit(alt)) >> 15 |
240 |
+ |
|
241 |
+ |
static long |
242 |
+ |
dir2diff(pa1, pa2) /* relative distance^2 between directions */ |
243 |
+ |
short pa1[2], pa2[2]; |
244 |
+ |
{ |
245 |
+ |
long altd2, azid2; |
246 |
+ |
int alt; |
247 |
+ |
|
248 |
+ |
altd2 = pa1[0] - pa2[0]; /* get altitude difference^2 */ |
249 |
+ |
altd2 *= altd2; |
250 |
+ |
if (altd2 > MAXDIFF2) |
251 |
+ |
return(altd2); /* too large already */ |
252 |
+ |
azid2 = pa1[1] - pa2[1]; /* get adjusted azimuth difference^2 */ |
253 |
+ |
if (azid2 < 0) azid2 = -azid2; |
254 |
+ |
if (azid2 >= 1L<<15) { /* wrap sphere */ |
255 |
+ |
azid2 -= 1L<<16; |
256 |
+ |
if (azid2 < 0) azid2 = -azid2; |
257 |
+ |
} |
258 |
+ |
alt = (pa1[0]+pa2[0])/2; |
259 |
+ |
azid2 = azid2*azisf(alt); /* evaluation order is important */ |
260 |
+ |
azid2 *= azid2; |
261 |
+ |
return(altd2 + azid2); |
262 |
+ |
} |
263 |
+ |
|
264 |
+ |
|
265 |
|
int |
266 |
|
qtFindLeaf(x, y) /* find closest leaf to (x,y) */ |
267 |
|
int x, y; |
306 |
|
register RTREE *tp = &qtrunk; |
307 |
|
int x0=0, y0=0, x1=odev.hres, y1=odev.vres; |
308 |
|
int lo = -1; |
309 |
+ |
long d2; |
310 |
+ |
short dc[2]; |
311 |
|
int x, y, mx, my; |
312 |
|
double z; |
313 |
|
FVECT ip, wp; |
314 |
|
register int q; |
315 |
< |
/* compute leaf location */ |
315 |
> |
/* compute leaf location in view */ |
316 |
|
VCOPY(wp, qtL.wp[li]); |
317 |
|
viewloc(ip, &odev.v, wp); |
318 |
|
if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1. |
319 |
|
|| ip[1] < 0. || ip[1] >= 1.) |
320 |
< |
return; |
320 |
> |
return(0); /* behind or outside view */ |
321 |
> |
#ifdef DEBUG |
322 |
> |
if (odev.v.type == VT_PAR | odev.v.vfore > FTINY) |
323 |
> |
error(INTERNAL, "bad view assumption in addleaf"); |
324 |
> |
#endif |
325 |
> |
for (q = 0; q < 3; q++) |
326 |
> |
wp[q] = (wp[q] - odev.v.vp[q])/ip[2]; |
327 |
> |
encodedir(dc, wp); /* compute pixel direction */ |
328 |
> |
d2 = dir2diff(dc, qtL.wd[li]); |
329 |
> |
if (d2 > MAXDIFF2) |
330 |
> |
return(0); /* leaf dir. too far off */ |
331 |
|
x = ip[0] * odev.hres; |
332 |
|
y = ip[1] * odev.vres; |
333 |
|
z = ip[2]; |
350 |
|
tp->flgs |= CHLFF(q); |
351 |
|
break; |
352 |
|
} |
353 |
< |
/* check existing leaf */ |
270 |
< |
if (lo != tp->k[q].li) { |
353 |
> |
if (lo != tp->k[q].li) { /* check old leaf */ |
354 |
|
lo = tp->k[q].li; |
355 |
|
VCOPY(wp, qtL.wp[lo]); |
356 |
|
viewloc(ip, &odev.v, wp); |
357 |
|
} |
358 |
|
/* is node minimum size? */ |
359 |
< |
if (x1-x0 <= qtMinNodesiz || y1-y0 <= qtMinNodesiz) { |
360 |
< |
if (z > (1.-qtDepthEps)*ip[2]) /* who is closer? */ |
361 |
< |
return; /* old one is */ |
362 |
< |
tp->k[q].li = li; /* new one is */ |
359 |
> |
if (y1-y0 <= qtMinNodesiz || x1-x0 <= qtMinNodesiz) { |
360 |
> |
if (z > (1.+qtDepthEps)*ip[2]) |
361 |
> |
return(0); /* old one closer */ |
362 |
> |
if (z >= (1.-qtDepthEps)*ip[2] && |
363 |
> |
dir2diff(dc, qtL.wd[lo]) < d2) |
364 |
> |
return(0); /* old one better */ |
365 |
> |
tp->k[q].li = li; /* else new one is */ |
366 |
|
tp->flgs |= CHF(q); |
367 |
|
break; |
368 |
|
} |
377 |
|
tp->k[q].li = lo; |
378 |
|
tp->flgs |= LFF(q)|CH_ANY; /* all new */ |
379 |
|
} |
380 |
+ |
return(1); /* done */ |
381 |
|
} |
382 |
|
|
383 |
|
|
384 |
< |
dev_value(c, p) /* add a pixel value to our quadtree */ |
384 |
> |
dev_value(c, p, v) /* add a pixel value to our quadtree */ |
385 |
|
COLR c; |
386 |
< |
FVECT p; |
386 |
> |
FVECT p, v; |
387 |
|
{ |
388 |
|
register int li; |
389 |
|
|
390 |
|
li = newleaf(); |
391 |
|
VCOPY(qtL.wp[li], p); |
392 |
+ |
encodedir(qtL.wd[li], v); |
393 |
|
tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1); |
394 |
< |
addleaf(li); |
394 |
> |
if (!addleaf(li)) |
395 |
> |
ungetleaf(li); |
396 |
|
} |
397 |
|
|
398 |
|
|