-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathquadtree.h
415 lines (342 loc) · 9.17 KB
/
quadtree.h
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
/**
* @author Hauke Strasdat
*
* Copyright (C) 2010 Hauke Strasdat
* Imperial College London
*
* quadtree.h is part of RobotVision.
*
* RobotVision is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* RobotVision is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RV_QUADTREE_H
#define RV_QUADTREE_H
#include <list>
#include <queue>
#include <stack>
#include <cvd/random.h>
#include "maths_utils.h"
#include "rectangle.h"
namespace TooN
{
template <typename T> class QuadTreeElement
{
public:
QuadTreeElement() : dfs_visited(false)
{
}
QuadTreeElement (const Vector<2> & pos,
const T & t): pos(pos), content(t), dfs_visited(false)
{
}
Vector<2> pos;
T content;
bool dfs_visited;
};
template <typename T> class QuadTreeNode
{
public:
class QuadTree;
friend class QuadTree;
QuadTreeNode(double delta,
const RobotVision::Rectangle & bbox)
: bbox(bbox), delta(delta), empty(true)
{
}
bool deleteAtPos(const Vector<2> & pos)
{
if (children.size() == 0)
{
if(empty)
{
return false;
}
else
{
if(norm(elem.pos-pos) < delta)
{
empty = true;
return true;
}
return false;
}
}
else
{
assert(children.size()==4);
typename std::list<QuadTreeNode<T> >::iterator it = children.begin();
if (it->bbox.contains(pos)){
it->deleteAtPos(pos);
}
else
{
++it;
if (it->bbox.contains(pos)){
it->deleteAtPos(pos);
}
else
{
++it;
if (it->bbox.contains(pos)){
it->deleteAtPos(pos);
}
else
{
++it;
if (it->bbox.contains(pos)){
it->deleteAtPos(pos);
}
}
}
}
assert(false);
return false;
}
}
bool insert(const QuadTreeElement<T> & new_elem)
{
assert(bbox.contains(new_elem.pos));
assert(isnan(new_elem.pos)==false);
if (children.size() == 0)
{
if(empty)
{
elem = new_elem;
empty = false;
}
else{
if (norm(elem.pos-new_elem.pos)<delta){
return false;
}
double x_diff = bbox.x2 - bbox.x1;
double y_diff = bbox.y2 - bbox.y1;
double x0 = bbox.x1;
double x1 = bbox.x1 + x_diff*0.5;
double x2 = bbox.x2;
double y0 = bbox.y1;
double y1 = bbox.y1 + y_diff*0.5;
double y2 = bbox.y2;
QuadTreeNode xy(delta,RobotVision::Rectangle(x0,y0,x1,y1));
QuadTreeNode xY(delta,RobotVision::Rectangle(x0,y1,x1,y2));
QuadTreeNode Xy(delta,RobotVision::Rectangle(x1,y0,x2,y1));
QuadTreeNode XY(delta,RobotVision::Rectangle(x1,y1,x2,y2));
insertMacro(xy,xY,Xy,XY,elem,bbox);
insertMacro(xy,xY,Xy,XY,new_elem,bbox);
children.push_back(xy);
children.push_back(xY);
children.push_back(Xy);
children.push_back(XY);
}
}
else{
assert(children.size()==4);
typename std::list<QuadTreeNode<T> >::iterator it = children.begin();
typename std::list<QuadTreeNode<T> >::iterator xy = it;
++it;
typename std::list<QuadTreeNode<T> >::iterator xY = it;
++it;
typename std::list<QuadTreeNode<T> >::iterator Xy = it;
++it;
typename std::list<QuadTreeNode<T> >::iterator XY = it;
insertMacro(*xy,*xY,*Xy,*XY,new_elem,bbox);
}
return true;
}
void query(const RobotVision::Rectangle & win,
std::list<QuadTreeElement<T> > & content_list) const
{
if (children.size() == 0){
if(empty == false){
if (win.contains(elem.pos)){
content_list.push_back(elem);
}
}
}
else{
assert(children.size()==4);
typename std::list<QuadTreeNode<T> >::const_iterator it
= children.begin();
if (it->bbox.intersectsWith(win)){
it->query(win,content_list);
}
++it;
if (it->bbox.intersectsWith(win)){
it->query(win,content_list);
}
++it;
if (it->bbox.intersectsWith(win)){
it->query(win,content_list);
}
++it;
if (it->bbox.intersectsWith(win)){
it->query(win,content_list);
}
}
}
//This method is faster than query, if you only wanna know
//whether there is any element within 'win'.
bool isEmpty(const RobotVision::Rectangle & win) const
{
if (children.size() == 0)
{
if(empty)
{
return true;
}
else
{
if (win.contains(elem.pos))
{
return false;
}
return true;
}
}
else
{
assert(children.size()==4);
typename std::list<QuadTreeNode>::const_iterator it = children.begin();
if (it->bbox.intersectsWith(win))
{
if (!it->isEmpty(win))
return false;
}
++it;
if (it->bbox.intersectsWith(win))
{
if (!it->isEmpty(win))
return false;
}
++it;
if (it->bbox.intersectsWith(win))
{
if (!it->isEmpty(win))
return false;
}
++it;
if (it->bbox.intersectsWith(win))
{
if (!it->isEmpty(win))
return false;
}
}
return true;
}
//Mainly for debugging:
//Shows the whole tree -- all nodes (quads) and leafs (elems)
void traverse(std::list<QuadTreeElement<T> > & elem_list,
std::list<RobotVision::Rectangle> & quad_list) const
{
quad_list.push_back(bbox);
if (children.size() == 0)
{
if(empty == false)
{
elem_list.push_back(elem);
}
}
else
{
assert(children.size()==4);
typename std::list<QuadTreeNode>::const_iterator it = children.begin();
it->traverse(elem_list, quad_list);
++it;
it->traverse(elem_list, quad_list);
++it;
it->traverse(elem_list, quad_list);
++it;
it->traverse(elem_list, quad_list);
}
}
static void insertMacro(QuadTreeNode & xy,
QuadTreeNode & xY,
QuadTreeNode & Xy,
QuadTreeNode & XY,
const QuadTreeElement<T> & elem,
const RobotVision::Rectangle & bbox)
{
double x_diff = bbox.x2 - bbox.x1;
double y_diff = bbox.y2 - bbox.y1;
double rel_x = 1-(bbox.x2 -elem.pos[0])/x_diff;
double rel_y = 1-(bbox.y2 -elem.pos[1])/y_diff;
assert(rel_x >= 0);
assert(rel_y >= 0);
assert(rel_x <= 1);
assert(rel_y <= 1);
if(rel_x < 0.5 && rel_y < 0.5){
xy.insert(elem);
}
else if (rel_x >= 0.5 && rel_y < 0.5){
Xy.insert(elem);
}
else if (rel_x < 0.5 && rel_y >= 0.5){
xY.insert(elem);
}
else if (rel_x >= 0.5 && rel_y >= 0.5){
XY.insert(elem);
}
else{
assert(false);
}
}
RobotVision::Rectangle bbox;
std::list<QuadTreeNode<T> > children;
QuadTreeElement<T> elem;
double delta ;
bool empty;
};
template <typename T> class QuadTree
{
public:
public:
QuadTree() :
bbox(RobotVision::Rectangle(-1,-1,-1,-1)),
root(-1, bbox),
delta(-1)
{
}
QuadTree(const RobotVision::Rectangle & bbox,
double delta) : bbox(bbox) , root(delta, bbox)
{
this->delta = delta;
}
bool insert(const QuadTreeElement<T> & elem)
{
return root.insert(elem);
}
bool deleteAtPos( const Vector<2> & p)
{
return root.deleteAtPos(p);
}
void query(const RobotVision::Rectangle & win,
std::list<QuadTreeElement<T> > & l) const
{
root.query(win,l);
}
bool isEmpty(const RobotVision::Rectangle & win) const
{
return root.isEmpty(win);
}
void traverse(std::list<QuadTreeElement<T> > & elem_list,
std::list<RobotVision::Rectangle> & quad_list) const
{
return root.traverse(elem_list, quad_list);
}
private:
RobotVision::Rectangle bbox;
QuadTreeNode<T> root;
double delta;
};
}
#endif // RV_QUADTREE_H