1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: octree.c,v 2.6 2003/02/25 02:47:21 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* octree.c - routines dealing with octrees and cubes. |
6 |
*/ |
7 |
|
8 |
#include "copyright.h" |
9 |
|
10 |
#include "standard.h" |
11 |
|
12 |
#include "octree.h" |
13 |
|
14 |
OCTREE *octblock[MAXOBLK]; /* our octree */ |
15 |
static OCTREE ofreelist = EMPTY; /* freed octree nodes */ |
16 |
static OCTREE treetop = 0; /* next free node */ |
17 |
|
18 |
|
19 |
OCTREE |
20 |
octalloc() /* allocate an octree */ |
21 |
{ |
22 |
register OCTREE freet; |
23 |
|
24 |
if ((freet = ofreelist) != EMPTY) { |
25 |
ofreelist = octkid(freet, 0); |
26 |
return(freet); |
27 |
} |
28 |
freet = treetop; |
29 |
if (octti(freet) == 0) { |
30 |
errno = 0; |
31 |
if (octbi(freet) >= MAXOBLK) |
32 |
return(EMPTY); |
33 |
if ((octblock[octbi(freet)] = (OCTREE *)malloc( |
34 |
(unsigned)OCTBLKSIZ*8*sizeof(OCTREE))) == NULL) |
35 |
return(EMPTY); |
36 |
} |
37 |
treetop++; |
38 |
return(freet); |
39 |
} |
40 |
|
41 |
|
42 |
void |
43 |
octfree(ot) /* free an octree */ |
44 |
register OCTREE ot; |
45 |
{ |
46 |
register int i; |
47 |
|
48 |
if (!istree(ot)) |
49 |
return; |
50 |
for (i = 0; i < 8; i++) |
51 |
octfree(octkid(ot, i)); |
52 |
octkid(ot, 0) = ofreelist; |
53 |
ofreelist = ot; |
54 |
} |
55 |
|
56 |
|
57 |
void |
58 |
octdone() /* free EVERYTHING */ |
59 |
{ |
60 |
register int i; |
61 |
|
62 |
for (i = 0; i < MAXOBLK; i++) { |
63 |
if (octblock[i] == NULL) |
64 |
break; |
65 |
free((void *)octblock[i]); |
66 |
octblock[i] = NULL; |
67 |
} |
68 |
ofreelist = EMPTY; |
69 |
treetop = 0; |
70 |
} |
71 |
|
72 |
|
73 |
OCTREE |
74 |
combine(ot) /* recursively combine nodes */ |
75 |
register OCTREE ot; |
76 |
{ |
77 |
register int i; |
78 |
register OCTREE ores; |
79 |
|
80 |
if (!istree(ot)) /* not a tree */ |
81 |
return(ot); |
82 |
ores = octkid(ot, 0) = combine(octkid(ot, 0)); |
83 |
for (i = 1; i < 8; i++) |
84 |
if ((octkid(ot, i) = combine(octkid(ot, i))) != ores) |
85 |
ores = ot; |
86 |
if (!istree(ores)) { /* all were identical leaves */ |
87 |
octkid(ot, 0) = ofreelist; |
88 |
ofreelist = ot; |
89 |
} |
90 |
return(ores); |
91 |
} |
92 |
|
93 |
|
94 |
void |
95 |
culocate(cu, pt) /* locate point within cube */ |
96 |
register CUBE *cu; |
97 |
register FVECT pt; |
98 |
{ |
99 |
register int i; |
100 |
int branch; |
101 |
|
102 |
while (istree(cu->cutree)) { |
103 |
cu->cusize *= 0.5; |
104 |
branch = 0; |
105 |
for (i = 0; i < 3; i++) |
106 |
if (cu->cuorg[i] + cu->cusize <= pt[i]) { |
107 |
cu->cuorg[i] += cu->cusize; |
108 |
branch |= 1 << i; |
109 |
} |
110 |
cu->cutree = octkid(cu->cutree, branch); |
111 |
} |
112 |
} |
113 |
|
114 |
|
115 |
void |
116 |
cucopy(cu1, cu2) /* copy cu2 into cu1 */ |
117 |
register CUBE *cu1, *cu2; |
118 |
{ |
119 |
cu1->cutree = cu2->cutree; |
120 |
cu1->cusize = cu2->cusize; |
121 |
VCOPY(cu1->cuorg, cu2->cuorg); |
122 |
} |
123 |
|
124 |
|
125 |
int |
126 |
incube(cu, pt) /* determine if a point is inside a cube */ |
127 |
register CUBE *cu; |
128 |
register FVECT pt; |
129 |
{ |
130 |
if (cu->cuorg[0] > pt[0] || pt[0] >= cu->cuorg[0] + cu->cusize) |
131 |
return(0); |
132 |
if (cu->cuorg[1] > pt[1] || pt[1] >= cu->cuorg[1] + cu->cusize) |
133 |
return(0); |
134 |
if (cu->cuorg[2] > pt[2] || pt[2] >= cu->cuorg[2] + cu->cusize) |
135 |
return(0); |
136 |
return(1); |
137 |
} |