1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* Quadtree-specific set operations. |
6 |
*/ |
7 |
|
8 |
#include "standard.h" |
9 |
|
10 |
#include "sm_qtree.h" |
11 |
|
12 |
|
13 |
#define QTSETIBLK 2048 /* set index allocation block size */ |
14 |
#define QTELEMBLK 8 /* set element allocation block */ |
15 |
#define QTELEMMOD(ne) ((ne)&7) /* (ne) % QTELEMBLK */ |
16 |
|
17 |
#define QTONTHRESH(ne) (!QTELEMMOD((ne)+1)) |
18 |
#define QTNODESIZ(ne) ((ne) + QTELEMBLK - QTELEMMOD(ne)) |
19 |
|
20 |
OBJECT **qtsettab= NULL; /* quadtree leaf node table */ |
21 |
QUADTREE qtnumsets; /* number of used set indices */ |
22 |
static int qtfreesets = EMPTY; /* free set index list */ |
23 |
|
24 |
|
25 |
QUADTREE |
26 |
qtnewleaf(oset) /* return new leaf node for object set */ |
27 |
OBJECT *oset; |
28 |
{ |
29 |
register QUADTREE osi; |
30 |
|
31 |
if (*oset <= 0) |
32 |
return(EMPTY); /* should be error? */ |
33 |
if (qtfreesets != EMPTY) { |
34 |
osi = qtfreesets; |
35 |
qtfreesets = (int)qtsettab[osi]; |
36 |
} else if ((osi = qtnumsets++) % QTSETIBLK == 0) { |
37 |
qtsettab = (OBJECT **)realloc((void *)qtsettab, |
38 |
(unsigned)(osi+QTSETIBLK)*sizeof(OBJECT *)); |
39 |
if (qtsettab == NULL) |
40 |
goto memerr; |
41 |
} |
42 |
qtsettab[osi] = (OBJECT *)malloc(QTNODESIZ(*oset)*sizeof(OBJECT)); |
43 |
if (qtsettab[osi] == NULL) |
44 |
goto memerr; |
45 |
setcopy(qtsettab[osi], oset); |
46 |
return(QT_INDEX(osi)); |
47 |
memerr: |
48 |
error(SYSTEM, "out of memory in qtnewleaf"); |
49 |
} |
50 |
|
51 |
|
52 |
QUADTREE |
53 |
qtdelelem(qt, id) /* delete element from leaf node */ |
54 |
QUADTREE qt; |
55 |
OBJECT id; |
56 |
{ |
57 |
register QUADTREE lf; |
58 |
|
59 |
#ifdef DEBUG |
60 |
if(id < 0) |
61 |
eputs("qtdelelem():Invalid triangle id\n"); |
62 |
if (!QT_IS_LEAF(qt) || (lf = QT_SET_INDEX(qt)) >= qtnumsets) |
63 |
error(CONSISTENCY, "empty/bad node in qtdelelem"); |
64 |
if (!inset(qtsettab[lf], id)) |
65 |
error(CONSISTENCY, "id not in leaf in qtdelelem"); |
66 |
#else |
67 |
lf = QT_SET_INDEX(qt); |
68 |
#endif |
69 |
if (qtsettab[lf][0] <= 1) { /* blow leaf away */ |
70 |
free((void *)qtsettab[lf]); |
71 |
qtsettab[lf] = (OBJECT *)qtfreesets; |
72 |
qtfreesets = lf; |
73 |
return(EMPTY); |
74 |
} |
75 |
deletelem(qtsettab[lf], id); |
76 |
if (QTONTHRESH(qtsettab[lf][0])) |
77 |
qtsettab[lf] = (OBJECT *)realloc((void *)qtsettab[lf], |
78 |
QTNODESIZ(qtsettab[lf][0])*sizeof(OBJECT)); |
79 |
return(qt); |
80 |
} |
81 |
|
82 |
|
83 |
QUADTREE |
84 |
qtaddelem(qt, id) /* add element to leaf node */ |
85 |
QUADTREE qt; |
86 |
OBJECT id; |
87 |
{ |
88 |
OBJECT newset[2]; |
89 |
register QUADTREE lf; |
90 |
|
91 |
#ifdef DEBUG |
92 |
if(id < 0) |
93 |
eputs("qtaddelem():Invalid triangle id\n"); |
94 |
#endif |
95 |
if (QT_IS_EMPTY(qt)) { /* create new leaf */ |
96 |
newset[0] = 1; newset[1] = id; |
97 |
return(qtnewleaf(newset)); |
98 |
} /* else add element */ |
99 |
#ifdef DEBUG |
100 |
if (!QT_IS_LEAF(qt) || (lf = QT_SET_INDEX(qt)) >= qtnumsets) |
101 |
error(CONSISTENCY, "bad node in qtaddelem"); |
102 |
if (inset(qtsettab[lf], id)) |
103 |
error(CONSISTENCY, "id already in leaf in qtaddelem"); |
104 |
#else |
105 |
lf = QT_SET_INDEX(qt); |
106 |
#endif |
107 |
if (QTONTHRESH(qtsettab[lf][0])) { |
108 |
qtsettab[lf] = (OBJECT *)realloc((void *)qtsettab[lf], |
109 |
QTNODESIZ(qtsettab[lf][0]+1)*sizeof(OBJECT)); |
110 |
if (qtsettab[lf] == NULL) |
111 |
error(SYSTEM, "out of memory in qtaddelem"); |
112 |
} |
113 |
insertelem(qtsettab[lf], id); |
114 |
return(qt); |
115 |
} |
116 |
|
117 |
|
118 |
#ifdef DEBUG |
119 |
OBJECT * |
120 |
qtqueryset(qt) /* return object set for leaf node */ |
121 |
QUADTREE qt; |
122 |
{ |
123 |
register QUADTREE lf; |
124 |
|
125 |
if (!QT_IS_LEAF(qt) || (lf = QT_SET_INDEX(qt)) >= qtnumsets) |
126 |
error(CONSISTENCY, "bad node in qtqueryset"); |
127 |
return(qtsettab[lf]); |
128 |
} |
129 |
#endif |
130 |
|
131 |
|
132 |
qtfreeleaf(qt) /* free set and leaf node */ |
133 |
QUADTREE qt; |
134 |
{ |
135 |
register QUADTREE osi; |
136 |
|
137 |
if (!QT_IS_LEAF(qt)) |
138 |
return; |
139 |
osi = QT_SET_INDEX(qt); |
140 |
if (osi >= qtnumsets) |
141 |
return; |
142 |
free((void *)qtsettab[osi]); |
143 |
qtsettab[osi] = (OBJECT *)qtfreesets; |
144 |
qtfreesets = osi; |
145 |
} |
146 |
|
147 |
|
148 |
|
149 |
qtfreeleaves() /* free ALL sets and leaf nodes */ |
150 |
{ |
151 |
register int i; |
152 |
|
153 |
while ((i = qtfreesets) != EMPTY) { |
154 |
qtfreesets = (int)qtsettab[i]; |
155 |
qtsettab[i] = NULL; |
156 |
} |
157 |
for (i = qtnumsets; i--; ) |
158 |
if (qtsettab[i] != NULL) |
159 |
free((void *)qtsettab[i]); |
160 |
free((void *)qtsettab); |
161 |
qtsettab = NULL; |
162 |
qtnumsets = 0; |
163 |
} |
164 |
|
165 |
|
166 |
check_set(os, cs) /* modify checked set and set to check */ |
167 |
register OBJECT *os; /* os' = os - cs */ |
168 |
register OBJECT *cs; /* cs' = cs + os */ |
169 |
{ |
170 |
OBJECT cset[QT_MAXCSET+QT_MAXSET+1]; |
171 |
register int i, j; |
172 |
int k; |
173 |
/* copy os in place, cset <- cs */ |
174 |
cset[0] = 0; |
175 |
k = 0; |
176 |
for (i = j = 1; i <= os[0]; i++) { |
177 |
while (j <= cs[0] && cs[j] < os[i]) |
178 |
cset[++cset[0]] = cs[j++]; |
179 |
if (j > cs[0] || os[i] != cs[j]) { /* object to check */ |
180 |
os[++k] = os[i]; |
181 |
cset[++cset[0]] = os[i]; |
182 |
} |
183 |
} |
184 |
if (!(os[0] = k)) /* new "to check" set size */ |
185 |
return; /* special case */ |
186 |
while (j <= cs[0]) /* get the rest of cs */ |
187 |
cset[++cset[0]] = cs[j++]; |
188 |
if (cset[0] > QT_MAXCSET) /* truncate "checked" set if nec. */ |
189 |
cset[0] = QT_MAXCSET; |
190 |
/* setcopy(cs, cset); */ /* copy cset back to cs */ |
191 |
os = cset; |
192 |
for (i = os[0]; i-- >= 0; ) |
193 |
*cs++ = *os++; |
194 |
} |
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|