-
Notifications
You must be signed in to change notification settings - Fork 2
/
quadtree.hpp
253 lines (188 loc) · 6.24 KB
/
quadtree.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <vector>
#include "geometry.hpp"
////////////////////////////////////////////////////////////////////////////
template< unsigned int DIM, unsigned int CAPACITY = 4 > //
class QuadTreeCell { //
////////////////////////////////////////////////////////////////////////////
protected:
// Rectangle which bounds the entire cell
Rectangle<DIM> R;
/* Child cells of this cell. In dimension d, there are 2^d children of
each cell. */
QuadTreeCell<DIM>* child[ 1 << DIM ];
// Points contained in this cell
unsigned int point_count;
unsigned int indices[CAPACITY];
public:
///////////////
// Constructors
// Construct a quadtree cell with a given bounding box
QuadTreeCell( Rectangle<DIM>& boundingBox) {
R = boundingBox;
for (int index = 0; index < (1<<DIM); index++) {
child[index] = NULL;
}
point_count = 0;
}
QuadTreeCell( Point<DIM>& p, double L ) {
Rectangle<DIM> boundingBox( p, L );
R = boundingBox;
for (int index=0; index < (1<<DIM); index++) {
child[index] = NULL;
}
point_count = 0;
}
//////////////
// Destructors
~QuadTreeCell() {
point_count = 0;
for (int index = 0; index < (1<<DIM); index++) {
delete child[index];
}
}
////////////
// Accessors
// Get the center point of the cell
Point<DIM> getCenter() {
return R.getCenter();
}
// Return the width of the cell
double getHalfWidth(int i) {
return R.getHalfWidth(i);
}
double getHalfWidth() {
return R.getHalfWidth(0);
}
Rectangle<DIM> getBoundingBox() {
return R;
}
void rangeSearch(std::vector<int>& I,
std::vector< Point<DIM> >& P, Rectangle<DIM>& T) {
int i, k;
// Check all the children
Rectangle<DIM> S;
for (k = 0; k < (1<<DIM); k++) {
if (child[k]) {
/* Find the intersection of the search rectangle with the
child cell's bounding box */
S = child[k]->getBoundingBox();
S = S & T;
/* If the intersection isn't empty, recurse the range
search to the child cell */
if ( ! S.empty() ) {
child[k]->rangeSearch(I, P, S);
}
}
}
Point<DIM> x;
// Check each of the points stored locally
for (k = 0; k < point_count; k++) {
i = indices[k];
x = P[i];
if ( T.inRectangle(x) ) {
I.push_back(i);
}
}
}
///////////
// Mutators
// Set one of the cell's children
void setChild(int index, QuadTreeCell<DIM>& cell) {
child[index] = &cell;
}
/* Insert a new point `p` into this cell if it has room or into a child
if it does not. Note that we do not store the actual point at the
cell, just an integer `index` that can be used to identify it in a
list of points later. */
bool insertPoint(int index, Point<DIM>& p) {
if ( ! R.inRectangle(p) ) {
return false;
}
/* If there's enough space in this cell's internal storage, store
it here */
if ( point_count < CAPACITY ) {
indices[point_count++] = index;
return true;
}
// Otherwise, find the quadrant to which the point belongs
Point<DIM> q = getCenter();
int k = q.quadrant(p);
// If the corresponding child cell is not empty, recurse to it
if ( child[k] ) {
return child[k]->insertPoint(index, p);
// Otherwise, make a new cell
} else {
int t;
double L = getHalfWidth()/2;
for (int i = 0; i < DIM; i++) {
// t == -1 if the i-th bit of index is 1, 0 otherwise
t = 1 - 2*((k & (1<<i)) != 0);
/* Choose q[i] = this.center[i] +/- this.width/2 according
to which quadrant it belongs to. */
q[i] = q[i] + t*L;
}
// Initialize the appropriate child of this cell
child[k] = new QuadTreeCell<DIM>(q, L);
// Insert X[index] into the child cell
return child[k]->insertPoint(index, p);
}
return false;
}
};
////////////////////////////////////////////////////////////////////////////
template< unsigned int DIM, unsigned int CAPACITY = 4 > //
class QuadTree { //
////////////////////////////////////////////////////////////////////////////
protected:
// bounding box for the entire QuadTree
Rectangle<DIM> R;
// root cell of the QuadTree
QuadTreeCell<DIM>* root;
// number of points contained in the QuadTree
unsigned int num_points;
// pointer to the list of Points in the QuadTree
std::vector< Point<DIM> >* X;
public:
///////////////
// Constructors
QuadTree(Rectangle<DIM>& boundingBox, std::vector< Point<DIM> >& P) {
R = boundingBox;
root = new QuadTreeCell<DIM>(boundingBox);
X = &P;
num_points = 0;
bool success = false;
for (int index = 0; index < P.size(); index++) {
success = root->insertPoint(index, P[index]);
if (success) num_points++;
}
}
//////////////
// Destructors
~QuadTree() {
delete root;
X = NULL;
}
////////////
// Accessors
int getNumPoints() {
return num_points;
}
Point<DIM> getPoint(int k) {
return (*X)[k];
}
void rangeSearch(std::vector<int>& I, Rectangle<DIM>& T) {
root->rangeSearch(I, *X, T);
}
///////////
// Mutators
void rebuild(std::vector< Point<DIM> >& P) {
delete root;
root = new QuadTreeCell<DIM>(R);
num_points = 0;
bool success = false;
for (int index = 0; index < P.size(); index++) {
success = root->insertPoint(index, P[index]);
if (success) num_points++;
}
}
};