This repository has been archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquadtree.cpp
501 lines (436 loc) · 18.5 KB
/
quadtree.cpp
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#include "quadtree.h"
namespace qt {
// Constructor
template<typename T, typename PairT, typename ContainerT>
QuadTree<T, PairT, ContainerT>::QuadTree(Vertex center, Vertex range, unsigned int bucket_size,
unsigned int depth, bool sort) {
m_root = new Node{center, range};
max_depth = depth > 0 ? depth : 16;
max_bucket_size = bucket_size > 0 ? bucket_size : 1;
m_sort = sort;
m_pair_comp = PairComp();
m_size = 0;
}
// Destructor
template<typename T, typename PairT, typename ContainerT>
QuadTree<T, PairT, ContainerT>::~QuadTree() {
delete_children(m_root);
}
// Class member functions
template<typename T, typename PairT, typename ContainerT>
std::pair<typename QuadTree<T, PairT, ContainerT>::viterator, bool> QuadTree<T, PairT, ContainerT>::insert(
const Vertex &point, const T &data) {
// Bound check
if (!in_region(point, m_root->bottom_left(), m_root->top_right())) return {};
// Duplicates check
if (contains(point)) return {};
auto pit = insert(point, data, m_root, nullptr, 0);
if (pit.second) {
++m_size;
return pit;
}
return {};
}
template<typename T, typename PairT, typename ContainerT>
bool QuadTree<T, PairT, ContainerT>::update(const Vertex &point, const T &data) {
// Bound check
if (!in_region(point, m_root->bottom_left(), m_root->top_right())) return false;
if (!contains(point)) {
// Return true if there is no point yet and successful insertion.
if (insert(point, data, m_root, m_root, 0)) {
++m_size;
return true;
}
}
std::stack<Node *> nodes;
nodes.push(m_root);
Node *top = nodes.top();
unsigned dir;
while (!top->m_leaf) {
dir = direction(point, top);
if (top->m_children[dir]) {
nodes.push(top->m_children[dir]);
top = nodes.top();
} else {
return false;
}
}
for (int i = 0; i < top->m_bucket.size(); ++i)
if (top->m_bucket[i].first == point) {
top->m_bucket[i].second = data;
break;
}
return false;
}
template<typename T, typename PairT, typename ContainerT>
bool QuadTree<T, PairT, ContainerT>::contains(const Vertex &point) {
std::stack<Node *> nodes;
nodes.push(m_root);
Node *top = nodes.top();
unsigned dir;
// Find appropriate node (non-recursive loop)
while (!top->m_leaf) {
dir = direction(point, top);
if (top->m_children[dir] != nullptr) {
nodes.push(top->m_children[dir]);
top = nodes.top();
} else {
return false;
}
}
// Find that point.
for (int i = 0; i < top->m_bucket.size(); ++i)
if (top->m_bucket[i].first == point)
return true;
return false;
}
template<typename T, typename PairT, typename ContainerT>
bool QuadTree<T, PairT, ContainerT>::remove(const Vertex &point) {
std::stack<Node *> nodes;
nodes.push(m_root);
Node *top = nodes.top();
unsigned dir;
// Find appropriate node (non-recursive loop)
while (!top->m_leaf) {
dir = direction(point, top);
if (top->m_children[dir] != nullptr) {
nodes.push(top->m_children[dir]);
top = nodes.top();
} else {
return false;
}
}
// Find that point and delete nodes.
for (int i = 0; i < top->m_bucket.size(); ++i) {
if (top->m_bucket[i].first == point) {
top->m_bucket.erase(top->m_bucket.begin() + i);
reduce(nodes);
--m_size;
return true;
}
}
return false;
}
template<typename T, typename PairT, typename ContainerT>
std::vector<std::pair<Vertex, T>>
QuadTree<T, PairT, ContainerT>::data_in_region(const Vertex &bottom_left, const Vertex &top_right) {
std::vector<std::pair<Vertex, T>> results{};
std::queue<Node *> nodes;
nodes.push(m_root);
while (!nodes.empty()) {
Node *top = nodes.front();
// Leaf node
if (top->m_leaf) {
enclosure status = this->status(top->m_center, top->m_range, bottom_left, top_right);
switch (status) {
case IN_BOUND:
results.insert(results.end(), top->m_bucket.begin(), top->m_bucket.end());
break;
case PARTIAL_BOUND:
for (int i = 0; i < top->m_bucket.size(); ++i)
if (in_region(top->m_bucket[i].first, bottom_left, top_right))
results.insert(results.end(), top->m_bucket[i]);
break;
default:
break;
}
nodes.pop();
continue;
}
// Stem node
for (int i = 0; i < 4; ++i) {
if (top->m_children[i] == nullptr) continue;
enclosure status = this->status(top->m_children[i]->m_center, top->m_children[i]->m_range,
bottom_left, top_right);
switch (status) {
case IN_BOUND:
add_points_to_result(top->m_children[i], results);
break;
case PARTIAL_BOUND:
nodes.push(top->m_children[i]);
break;
default:
break;
}
}
nodes.pop();
}
return results;
}
template<typename T, typename PairT, typename ContainerT>
Vertex QuadTree<T, PairT, ContainerT>::new_center(int direction, QuadTree::Node *node) {
Vertex v(node->m_center.x, node->m_center.y);
switch (direction) {
case BOT_LEFT:
v -= node->m_range / 2.0;
break;
case TOP_LEFT:
v.x -= node->m_range.x / 2.0;
v.y += node->m_range.y / 2.0;
break;
case BOT_RIGHT:
v.x += node->m_range.x / 2.0;
v.y -= node->m_range.y / 2.0;
break;
case TOP_RIGHT:
v += node->m_range / 2.0;
break;
default:
break;
}
return v;
}
template<typename T, typename PairT, typename ContainerT>
int QuadTree<T, PairT, ContainerT>::direction(const Vertex &point, QuadTree::Node *node) {
unsigned X = 0;
unsigned Y = 0;
X |= ((point.x >= node->m_center.x) << 1);
Y |= ((point.y >= node->m_center.y) << 0);
return (int) (X | Y);
}
template<typename T, typename PairT, typename ContainerT>
QuadTreeNode<T, PairT, ContainerT> *&QuadTree<T, PairT, ContainerT>::child_node(const Vertex &v,
QuadTree::Node *&node) {
unsigned dir = direction(v, node);
if (node->m_children[dir] != nullptr) {
// Child node already exists, return that child node.
return node->m_children[dir];
} else {
// Child node doesn't exist, create new one and return it.
node->m_children[dir] = new Node(new_center(dir, node), node->m_range / 2.0);
return node->m_children[dir];
}
}
template<typename T, typename PairT, typename ContainerT>
std::pair<typename QuadTree<T, PairT, ContainerT>::viterator, bool> QuadTree<T, PairT, ContainerT>::insert(
const Vertex &v, const T &data,
QuadTree::Node *&node, QuadTree::Node *parent_node,
unsigned depth) {
std::pair<QuadTree<T, PairT, ContainerT>::viterator, bool> pit;
// Insertion will not happen if insertion point's depth limit has been reached.
// Insert only when the node is a leaf node
if (node->m_leaf) {
if (node->m_bucket.size() < max_bucket_size) {
// Bucket in that node is not full yet, add data to the m_bucket.
pit = {viterator(node), true};
typename std::vector<PairT>::iterator insert_it;
if (m_sort) {
insert_it = std::lower_bound(node->m_bucket.begin(), node->m_bucket.end(),
PairT{v, data}, m_pair_comp);
} else {
insert_it = node->m_bucket.end();
}
node->set_parent(parent_node);
node->m_bucket.insert(insert_it, PairT{v, data});
return pit;
} else if (depth < max_depth) {
// Change this m_leaf node to stem node first
node->m_leaf = false;
pit = insert(v, data, child_node(v, node), node, 1 + depth);
// Pull out data from this node and put it in corresponding child
for (int i = 0; i < node->m_bucket.size(); ++i) {
pit = insert(node->m_bucket[i].first,
node->m_bucket[i].second,
child_node(node->m_bucket[i].first, node),
node,
1 + depth);
}
node->m_bucket.clear();
return pit;
}
} else {
pit = insert(v, data, child_node(v, node), node, 1 + depth);
return pit;
}
return {};
}
template<typename T, typename PairT, typename ContainerT>
void QuadTree<T, PairT, ContainerT>::reduce(std::stack<Node *> &nodes) {
bool canReduce = true;
typename std::vector<PairT>::iterator insert_it;
nodes.pop();
while (canReduce && !nodes.empty()) {
canReduce = true;
Node *top = nodes.top();
int numKeys = 0;
for (int i = 0; i < 4; ++i) {
if (top->m_children[i] && !top->m_children[i]->m_leaf) {
return;
} else if (top->m_children[i] && top->m_children[i]->m_leaf) {
numKeys += top->m_children[i]->m_bucket.size();
}
}
canReduce &= (numKeys <= max_bucket_size);
if (canReduce) {
for (int i = 0; i < 4; ++i) {
if (top->m_children[i]) {
top->m_children[i]->set_parent(top);
for (int j = 0; j < top->m_children[i]->m_bucket.size(); ++j) {
if (m_sort)
insert_it = std::lower_bound(top->m_bucket.begin(),
top->m_bucket.end(),
top->m_children[i]->m_bucket[j],
m_pair_comp);
else
insert_it = top->m_bucket.end();
top->m_bucket.insert(insert_it, top->m_children[i]->m_bucket[j]);
}
delete top->m_children[i];
top->m_children[i] = nullptr;
}
}
top->m_leaf = true;
}
nodes.pop();
}
}
template<typename T, typename PairT, typename ContainerT>
void QuadTree<T, PairT, ContainerT>::add_points_to_result(QuadTree::Node *node,
std::vector<std::pair<Vertex, T>> &results) {
if (node->m_leaf) {
results.insert(results.end(), node->m_bucket.begin(), node->m_bucket.end());
return;
}
for (int i = 0; i < 4; ++i) {
if (node->m_children[i] != nullptr) {
add_points_to_result(node->m_children[i], results);
}
}
}
template<typename T, typename PairT, typename ContainerT>
bool QuadTree<T, PairT, ContainerT>::in_region(const Vertex &point,
const Vertex &bottom_left,
const Vertex &top_right) {
return (point.x >= bottom_left.x) &&
(point.x < top_right.x) &&
(point.y >= bottom_left.y) &&
(point.y < top_right.y);
}
template<typename T, typename PairT, typename ContainerT>
enclosure QuadTree<T, PairT, ContainerT>::status(const Vertex ¢er, const Vertex &range,
const Vertex &bottom_left, const Vertex &top_right) {
int enclosedPts = 0;
enclosedPts += in_region({center.x - range.x, center.y - range.y}, bottom_left, top_right);
enclosedPts += in_region({center.x - range.x, center.y + range.y}, bottom_left, top_right);
enclosedPts += in_region({center.x + range.x, center.y - range.y}, bottom_left, top_right);
enclosedPts += in_region({center.x + range.x, center.y + range.y}, bottom_left, top_right);
if (enclosedPts == 4)
return IN_BOUND;
if (enclosedPts > 0)
return PARTIAL_BOUND;
Vertex nodeMin{center - range};
Vertex nodeMax(center + range);
enclosedPts += in_region(bottom_left, nodeMin, nodeMax);
enclosedPts += in_region({bottom_left.x, top_right.y}, nodeMin, nodeMax);
enclosedPts += in_region(bottom_left, nodeMin, nodeMax);
enclosedPts += in_region({bottom_left.x, top_right.y}, nodeMin, nodeMax);
if (enclosedPts > 0)
return PARTIAL_BOUND;
return OUT_OF_BOUND;
}
template<typename T, typename PairT, typename ContainerT>
std::vector<std::pair<Vertex, T>> QuadTree<T, PairT, ContainerT>::extract_all() {
return data_in_region(m_root->m_center - m_root->m_range, m_root->m_center + m_root->m_range);
}
// Printing data
template<typename T, typename PairT, typename ContainerT>
void QuadTree<T, PairT, ContainerT>::print_nodes(Node *&node, unsigned int depth) {
// Print this node's address
for (unsigned int i = 0; i < depth; ++i) std::cout << "| ";
printf("| At depth = %d, Node at address %p has m_parent %p", depth, node, node->m_parent);
if (node->m_leaf) printf(" (Leaf node)\n");
else printf(" (Stem node)\n");
// Print data in the m_bucket
for (auto const &data: node->m_bucket) {
for (unsigned int i = 0; i < depth; ++i) std::cout << "| ";
std::cout << "[ <*> Point " << data.first << " has data = " << data.second << '\n';
}
// Print m_children addresses
if (!node->m_leaf) {
for (unsigned int i = 0; i < depth; ++i) std::cout << "| ";
printf("| This node has valid m_children: \n");
}
int c = 0;
for (Node *&child: node->m_children) {
if (child != nullptr) {
for (unsigned int i = 0; i < depth; ++i) std::cout << "| ";
printf("| -> Child #%d", c);
// Recursively print nodes and corresponding data if child is not null.
printf("\n");
print_nodes(child, 1 + depth);
}
++c;
}
}
template<typename T, typename PairT, typename ContainerT>
void QuadTree<T, PairT, ContainerT>::print_data(Node *&node) {
if (node == nullptr) return;
for (auto const &data: node->m_bucket)
std::cout << "<*> Point " << data.first << " has data = " << data.second << '\n';
for (Node *&child: node->m_children)
if (child != nullptr)
print_data(child);
}
template<typename T, typename PairT, typename ContainerT>
void QuadTree<T, PairT, ContainerT>::traverse(QuadTree::Node *node, std::queue<Node *> &nodes) {
if (node == nullptr) return;
nodes.push(node);
for (Node *&child: node->m_children)
if (child != nullptr)
traverse(child, nodes);
}
template<typename T, typename PairT, typename ContainerT>
void QuadTree<T, PairT, ContainerT>::data_in_subtrees(QuadTree::Node *node) {
std::queue<Node *> nodes;
Node *top;
traverse(node, nodes);
while (!nodes.empty()) {
top = nodes.front();
nodes.pop();
for (auto const &data: top->m_bucket)
std::cout << "<*> Point " << data.first << " has data = " << data.second << '\n';
}
}
// Element access
template<typename T, typename PairT, typename ContainerT>
T *QuadTree<T, PairT, ContainerT>::at(long double x, long double y) {
return at(Vertex(x, y));
}
template<typename T, typename PairT, typename ContainerT>
T *QuadTree<T, PairT, ContainerT>::at(const Vertex &point) {
std::stack<Node *> nodes;
nodes.push(m_root);
Node *top = nodes.top();
unsigned dir;
while (!top->m_leaf) {
dir = direction(point, top);
if (top->m_children[dir] != nullptr) {
nodes.push(top->m_children[dir]);
top = nodes.top();
} else {
return nullptr;
}
}
for (int i = 0; i < top->m_bucket.size(); ++i)
if (top->m_bucket[i].first == point)
return &top->m_bucket[i].second;
return nullptr;
}
// Iterator
template<typename T, typename PairT, typename ContainerT>
typename QuadTree<T, PairT, ContainerT>::viterator QuadTree<T, PairT, ContainerT>::vbegin() {
return viterator(m_root);
}
template<typename T, typename PairT, typename ContainerT>
typename QuadTree<T, PairT, ContainerT>::viterator QuadTree<T, PairT, ContainerT>::vend() {
return viterator(m_root);
}
template<typename T, typename PairT, typename ContainerT>
typename QuadTree<T, PairT, ContainerT>::iterator QuadTree<T, PairT, ContainerT>::begin() {
return iterator(m_root);
}
template<typename T, typename PairT, typename ContainerT>
typename QuadTree<T, PairT, ContainerT>::iterator QuadTree<T, PairT, ContainerT>::end() {
return iterator(m_root);
}
}