16 |
|
#endif |
17 |
|
/* maximum allowed angle difference (deg.) */ |
18 |
|
#ifndef MAXANG |
19 |
< |
#define MAXANG 20. |
19 |
> |
#define MAXANG 20 |
20 |
|
#endif |
21 |
+ |
#if MAXANG>0 |
22 |
+ |
#define MAXDIFF2 ( MAXANG*MAXANG * (PI*PI/180./180.)) |
23 |
+ |
#endif |
24 |
|
|
22 |
– |
#define MAXDIFF2 (long)( MAXANG*MAXANG /90./90.*(1L<<15)*(1L<<15)) |
23 |
– |
|
25 |
|
#define abs(i) ((i) < 0 ? -(i) : (i)) |
26 |
|
|
27 |
|
RTREE qtrunk; /* our quadtree trunk */ |
34 |
|
static RTREE **twigbundle; /* free twig blocks (NULL term.) */ |
35 |
|
static int nexttwig; /* next free twig */ |
36 |
|
|
36 |
– |
#define ungetleaf(li) (qtL.tl=(li)) /* dangerous if used improperly */ |
37 |
|
|
38 |
– |
|
38 |
|
static RTREE * |
39 |
|
newtwig() /* allocate a twig */ |
40 |
|
{ |
87 |
|
} |
88 |
|
|
89 |
|
|
90 |
< |
static int |
92 |
< |
newleaf() /* allocate a leaf from our pile */ |
93 |
< |
{ |
94 |
< |
int li; |
95 |
< |
|
96 |
< |
li = qtL.tl++; |
97 |
< |
if (qtL.tl >= qtL.nl) /* get next leaf in ring */ |
98 |
< |
qtL.tl = 0; |
99 |
< |
if (qtL.tl == qtL.bl) /* need to shake some free */ |
100 |
< |
qtCompost(LFREEPCT); |
101 |
< |
return(li); |
102 |
< |
} |
103 |
< |
|
104 |
< |
|
105 |
< |
#define LEAFSIZ (3*sizeof(float)+2*sizeof(short)+\ |
90 |
> |
#define LEAFSIZ (3*sizeof(float)+sizeof(int4)+\ |
91 |
|
sizeof(TMbright)+6*sizeof(BYTE)) |
92 |
|
|
93 |
|
int |
114 |
|
return(0); |
115 |
|
/* assign larger alignment types earlier */ |
116 |
|
qtL.wp = (float (*)[3])qtL.base; |
117 |
< |
qtL.wd = (short (*)[2])(qtL.wp + n); |
117 |
> |
qtL.wd = (int4 *)(qtL.wp + n); |
118 |
|
qtL.brt = (TMbright *)(qtL.wd + n); |
119 |
|
qtL.chr = (BYTE (*)[3])(qtL.brt + n); |
120 |
|
qtL.rgb = (BYTE (*)[3])(qtL.chr + n); |
187 |
|
} |
188 |
|
|
189 |
|
|
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 |
– |
|
190 |
|
int |
191 |
|
qtFindLeaf(x, y) /* find closest leaf to (x,y) */ |
192 |
|
int x, y; |
231 |
|
register RTREE *tp = &qtrunk; |
232 |
|
int x0=0, y0=0, x1=odev.hres, y1=odev.vres; |
233 |
|
int lo = -1; |
234 |
< |
long d2; |
310 |
< |
short dc[2]; |
234 |
> |
double d2; |
235 |
|
int x, y, mx, my; |
236 |
|
double z; |
237 |
< |
FVECT ip, wp; |
237 |
> |
FVECT ip, wp, vd; |
238 |
|
register int q; |
239 |
|
/* compute leaf location in view */ |
240 |
|
VCOPY(wp, qtL.wp[li]); |
241 |
|
viewloc(ip, &odev.v, wp); |
242 |
|
if (ip[2] <= 0. || ip[0] < 0. || ip[0] >= 1. |
243 |
|
|| ip[1] < 0. || ip[1] >= 1.) |
244 |
< |
return(0); /* behind or outside view */ |
244 |
> |
return(-1); /* behind or outside view */ |
245 |
|
#ifdef DEBUG |
246 |
|
if (odev.v.type == VT_PAR | odev.v.vfore > FTINY) |
247 |
|
error(INTERNAL, "bad view assumption in addleaf"); |
248 |
|
#endif |
249 |
|
for (q = 0; q < 3; q++) |
250 |
< |
wp[q] = (wp[q] - odev.v.vp[q])/ip[2]; |
251 |
< |
encodedir(dc, wp); /* compute pixel direction */ |
252 |
< |
d2 = dir2diff(dc, qtL.wd[li]); |
250 |
> |
vd[q] = (wp[q] - odev.v.vp[q])/ip[2]; |
251 |
> |
d2 = fdir2diff(qtL.wd[li], vd); |
252 |
> |
#ifdef MAXDIFF2 |
253 |
|
if (d2 > MAXDIFF2) |
254 |
|
return(0); /* leaf dir. too far off */ |
255 |
+ |
#endif |
256 |
|
x = ip[0] * odev.hres; |
257 |
|
y = ip[1] * odev.vres; |
258 |
|
z = ip[2]; |
285 |
|
if (z > (1.+qtDepthEps)*ip[2]) |
286 |
|
return(0); /* old one closer */ |
287 |
|
if (z >= (1.-qtDepthEps)*ip[2] && |
288 |
< |
dir2diff(dc, qtL.wd[lo]) < d2) |
288 |
> |
fdir2diff(qtL.wd[lo], vd) < d2) |
289 |
|
return(0); /* old one better */ |
290 |
|
tp->k[q].li = li; /* else new one is */ |
291 |
|
tp->flgs |= CHF(q); |
299 |
|
my = ip[1] * odev.vres; |
300 |
|
if (mx >= (x0 + x1) >> 1) q |= 01; |
301 |
|
if (my >= (y0 + y1) >> 1) q |= 02; |
302 |
+ |
tp->flgs = CH_ANY|LFF(q); /* all new */ |
303 |
|
tp->k[q].li = lo; |
378 |
– |
tp->flgs |= LFF(q)|CH_ANY; /* all new */ |
304 |
|
} |
305 |
|
return(1); /* done */ |
306 |
|
} |
312 |
|
{ |
313 |
|
register int li; |
314 |
|
|
315 |
< |
li = newleaf(); |
315 |
> |
li = qtL.tl++; |
316 |
> |
if (qtL.tl >= qtL.nl) /* advance to next leaf in ring */ |
317 |
> |
qtL.tl = 0; |
318 |
> |
if (qtL.tl == qtL.bl) /* need to shake some free */ |
319 |
> |
qtCompost(LFREEPCT); |
320 |
|
VCOPY(qtL.wp[li], p); |
321 |
< |
encodedir(qtL.wd[li], v); |
321 |
> |
qtL.wd[li] = encodedir(v); |
322 |
|
tmCvColrs(&qtL.brt[li], qtL.chr[li], c, 1); |
323 |
|
if (!addleaf(li)) |
324 |
< |
ungetleaf(li); |
324 |
> |
qtL.tl = li; /* unget this leaf */ |
325 |
|
} |
326 |
|
|
327 |
|
|