-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtree.cpp
460 lines (402 loc) · 11 KB
/
rtree.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
#include "rtree.h"
#include "views.h"
Rect2D to2D(const QRectF & rect)
{
return Rect2D(rect.topLeft().x(),rect.topLeft().y(),
rect.bottomRight().x(),rect.bottomRight().y());
}
QRectF toRectF(const Rect2D & rect)
{
return QRectF(rect.a.x,rect.a.y,rect.b.x-rect.a.x,rect.b.y-rect.a.y);
}
namespace misc
{
inline bool sameSign(real x1, real x2)
{
return (((x1 >= 0) && (x2 >=0)) || ((x1<=0) && (x2<=0)));
}
inline bool overlap(const interval& i1, const interval& i2)
{
return !((min(i1.first,i1.second) > max(i2.first,i2.second))
|| (max(i1.first,i1.second) < min(i2.first,i2.second)));
}
inline bool containedIn(const interval& i1, const interval& i2)
{
real a1 = min(i1.first,i1.second);
real a2 = max(i1.first,i1.second);
real b1 = min(i2.first,i2.second);
real b2 = max(i2.first,i2.second);
return ( (a1 < b1) == (b2<a2) );
}
inline void putMinMax(real a, real b, real& min, real& max)
{
if (a<b) { min = a; max =b;}
else {min = b; max = a; }
}
//for rtree only
void updateMinMax(real a, real b, real& min, real& max,int& rmin, int& rmax, int target)
{
if (a<b)
{
if (a>min) { min=a; rmin = target;}
if (b<max) { max =b; rmax=target;}
}
else
{
if (b>min) { min=b; rmin = target;}
if (a<max) { max =a; rmax=target;}
}
}
}
inline Rect2D Rect2D::unite(const Rect2D& other) const
{
return sum(*this,other);
}
inline Rect2D& Rect2D::united(const Rect2D& other)
{
*this = sum(*this,other);
return (*this);
}
inline Rect2D Rect2D::unite(const vector< Rect2D >& others) const
{
return sum(*this, sum(others));
}
inline Rect2D& Rect2D::united(const vector< Rect2D >& others)
{
*this = sum(*this, sum(others));
return * this;
}
Rect2D sum(const Rect2D &rect, const Rect2D &other)
{
real minx,miny,maxx,maxy;
minx=maxx=rect.a.x; miny=maxy=rect.a.y;
Point2D p[] = {rect.b,other.a,other.b};
for (int i = 0; i<3; ++i )
{
if (p[i].x > maxx) maxx=p[i].x;
if (p[i].y > maxy) maxy=p[i].y;
if (p[i].x < minx) minx=p[i].x;
if (p[i].y < miny) miny=p[i].y;
}
return Rect2D(minx,miny,maxx,maxy);
}
Rect2D sum(const vector<Rect2D> &rects)
{
if (!rects.size()) return Rect2D();
Rect2D result = rects[0];
int n=rects.size();
for (int i = 1; i<n; ++i) result.united(rects[i]);
return result;
}
inline bool Rect2D::overlap(const Rect2D& other) const
{
return misc::overlap(interval(a.x,b.x),interval(other.a.x,other.b.x))
&& misc::overlap(interval(a.y,b.y),interval(other.a.y,other.b.y));
}
//linear cost algo
TreeNode* RTree::splitNode(TreeNode* node)
{
TreeNode* newnode = new TreeNode(node->isLeaf,maxEnt);
vector<Entry*> temp;
temp.swap(node->entries);
int i;
int n = temp.size();
real xhigh,xlow,yhigh,ylow;
int rxhigh,rxlow,ryhigh,rylow; // the index of the respective rects
Rect2D* rect = & (temp[0]->rect);
rxhigh=rxlow=ryhigh=rylow=0;
misc::putMinMax(rect->a.x,rect->b.x,xlow,xhigh);
misc::putMinMax(rect->a.y,rect->b.y,ylow,yhigh);
for (i=1; i< n; ++i)
{
rect = & (temp[i]->rect);
misc::updateMinMax(rect->a.x,rect->b.x,xlow,xhigh,rxlow,rxhigh,i);
misc::updateMinMax(rect->a.y,rect->b.y,ylow,yhigh,rylow,ryhigh,i);
}
Rect2D totalRect = root->totalArea();
real dxNormal = (xlow-xhigh)/abs(totalRect.dx());
real dyNormal = (ylow-yhigh)/abs(totalRect.dy());
vector<Entry*>::const_iterator r1,r2;
if (dxNormal < dyNormal)
{
r1 = temp.cbegin() + rxlow;
r2 = temp.cbegin() + rxhigh;
}
else
{
r1 = temp.cbegin() + rylow;
r2 = temp.cbegin() + ryhigh;
}
node->entries.push_back(*r1);
if (*r2 != *r1)
{
newnode->entries.push_back(*r2);
}
auto g = [=](Entry * e) {return (e == *r1) || (e== *r2) ;};
auto end_valid = remove_if(temp.begin(),temp.end(),g);
temp.erase(end_valid,temp.end());
while (true)
{
node->entries.push_back(temp.back()); temp.pop_back();
if (!temp.size()) break;
newnode->entries.push_back(temp.back()); temp.pop_back();
if (!temp.size()) break;
}
int ns = node->entries.size();
if (node->isLeaf)
{
for (int i =0; i< ns; ++i)
{
((LeafEntry*)node->entries[i])->leaf = node;
}
ns=newnode->entries.size();
for (int i =0; i< ns; ++i)
{
((LeafEntry*)newnode->entries[i])->leaf = newnode;
}
}
else
{
ns=newnode->entries.size();
for (int i =0; i< ns; ++i)
{
((NonLeafEntry*)newnode->entries[i])->childNode->parentNode = newnode;
}
}
return newnode;
}
TreeNode::TreeNode(bool IsLeaf, int MaxEnt)
{
parentNode = 0; parentEntry = 0; isLeaf = IsLeaf;
entries.reserve(MaxEnt);
}
inline void TreeNode::adoptLeafEntry(LeafEntry *entry)
{
entries.push_back(entry); entry->leaf = this;
if ( !isLeaf) cerr << "error adoptleafen" << endl;
}
void TreeNode::adoptNonLeafEntry(NonLeafEntry *entry)
{
entries.push_back(entry);
}
Rect2D TreeNode::totalArea() const
{
if (!entries.size()) return Rect2D();
Rect2D result=entries[0]->rect;
int n = entries.size();
for (int i = 1; i<n; ++i) result.united(entries[i]->rect);
return result;
}
inline Rect2D TreeNode::quickTotalArea() const
{
return (parentEntry) ? parentEntry->rect : totalArea();
}
deque<LeafEntry*> RTree::searchOverlap(const Rect2D& rect)
{
deque<LeafEntry*> result;
deque<TreeNode*> nodes;
int n,i; TreeNode* node; bool isLeaf;
nodes.push_back(root);
while (nodes.size())
{
node = nodes.back();
nodes.pop_back();
n = node->entries.size();
isLeaf = node->isLeaf;
for (i=0; i<n; ++i)
{
if (rect.overlap(node->entries[i]->rect))
{
if (isLeaf)
{
LeafEntry * en = (LeafEntry*) node->entries[i];
result.push_back(en);
}
else nodes.push_back(((NonLeafEntry*) node->entries[i])->childNode);
}
}
}
return result;
}
void RTree::insert(LeafEntry* entry)
{
TreeNode* node = chooseLeaf(entry);
TreeNode* newnode = 0;
node->adoptLeafEntry(entry);
if (node->entries.size() > maxEnt)
{
newnode = splitNode(node);
}
adjustTree(node,newnode);
}
TreeNode* RTree::chooseLeaf(LeafEntry* entry)
{
if (root->isLeaf) return root;
TreeNode* node = root;
int i,imin,n;
Rect2D* rect;
real waste,wastemin;
while (true)
{
if (node->isLeaf) return node;
n = node->entries.size();
if (!n) return 0;
imin = 0;
rect = &(node->entries[0]->rect);
wastemin = entry->rect.unite(*rect).area()-rect->area();
for (i=1; i<n; ++i)
{
rect = &(node->entries[i]->rect);
waste = entry->rect.unite(*rect).area() -rect->area();
if (waste<wastemin) { wastemin = waste; imin = i; }
}
node = ((NonLeafEntry*) node->entries[imin])->childNode;
}
}
void RTree::adjustTree(TreeNode* node, TreeNode* newnode)
{
if (newnode)
{
NonLeafEntry* entry2 = new NonLeafEntry(newnode->totalArea(),newnode);
if (node == root)
{
root = new TreeNode(false,maxEnt);
node->parentNode = root;
TreeNode* parent = node->parentNode;
newnode->parentNode = parent;
NonLeafEntry* entry1 = new NonLeafEntry(node->totalArea(),node);
parent->adoptNonLeafEntry(entry1);
parent->adoptNonLeafEntry(entry2);
}
else
{
TreeNode* parent = node->parentNode;
newnode->parentNode = parent;
NonLeafEntry* entry1 = node->parentEntry;
entry1->rect.united(node->totalArea());
parent->adoptNonLeafEntry(entry2);
if (parent->entries.size() > maxEnt)
adjustTree(parent,splitNode(parent));
else adjustTree(parent);
}
}
else
{
if (node == root) return;
node->parentEntry->rect.united(node->totalArea());
adjustTree(node->parentNode);
}
}
inline void NonLeafEntry::adoptChildNode(TreeNode* child)
{
childNode = child;
if (child) child->parentEntry = this;
}
RTree::~RTree()
{
deallocNode(root);
}
void RTree::deallocNode(TreeNode* node)
{
if (node->isLeaf)
{
int n = node->entries.size();
for (int i =0; i<n; ++i)
{
LeafEntry* en = (LeafEntry*) node->entries[i];
deallocLeafEntry(en);
}
}
else
{
int n = node->entries.size();
for (int i =0; i<n; ++i)
{
NonLeafEntry* en = (NonLeafEntry*) node->entries[i];
deallocNode(en->childNode);
delete en;
}
}
delete node;
}
inline void RTree::deallocLeafEntry(LeafEntry* entry)
{
dealloc(entry->data);
delete entry;
}
void RTree::deleteEntry(LeafEntry* entry)
{
TreeNode* leaf = entry->leaf;
vector<Entry*>& ent = leaf->entries;
auto end2 = remove(ent.begin(),ent.end(),entry);
if (end2 == ent.end())
{
cout << "wat" << endl;
}
ent.erase(end2,ent.end());
condenseTree(leaf);
deallocLeafEntry(entry);
}
void RTree::condenseTree(TreeNode* leaf)
{
TreeNode* node = leaf;
deque<TreeNode*> hobo;
vector<Entry*>* ent;
NonLeafEntry* en;
int n;
while (node != root)
{
if (node->entries.size() < minEnt)
{
hobo.push_back(node);
ent = &(node->parentNode->entries);
en = node->parentEntry;
auto end2 = remove(ent->begin(),ent->end(),en);
ent->erase(end2,ent->end());
delete en;
}
else
{
node->parentEntry->rect.united(node->totalArea());
}
node = node->parentNode;
}
if (!root->entries.size()) root->isLeaf = true;
while (hobo.size())
{
node = hobo.back();
hobo.pop_back();
n = node->entries.size();
if (node->isLeaf)
{
for (int i = 0; i<n ; ++i) insert((LeafEntry*) node->entries[i]);
}
else
{
for (int i = 0; i<n ; ++i)
{
en = (NonLeafEntry*) node->entries[i];
hobo.push_back(en->childNode);
delete en;
}
}
delete node;
}
if (!root->isLeaf && (root->entries.size() == 1))
{
en = (NonLeafEntry*) root->entries[0];
TreeNode* newroot = en->childNode;
newroot->parentNode = 0;
newroot->parentEntry = 0;
delete root;
delete en;
root = newroot;
}
}
RTree::RTree(int MaxEntriesPerNode, void (*Dealloc)(void* data))
{
maxEnt = MaxEntriesPerNode ;
minEnt = maxEnt/2;
dealloc = Dealloc;
root = new TreeNode(true,maxEnt);
}