-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosmquadtree.cpp
More file actions
135 lines (117 loc) · 3.59 KB
/
Copy pathosmquadtree.cpp
File metadata and controls
135 lines (117 loc) · 3.59 KB
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
#include "osmquadtree.h"
#include <QtMath>
OsmQuadTree::OsmQuadTree(const QRectF &bounds, int maxDepth, int maxElements)
: m_maxDepth(maxDepth), m_maxElements(maxElements)
{
m_root.bounds = bounds;
}
void OsmQuadTree::clear()
{
m_root.elements.clear();
m_root.elementBBoxes.clear();
m_root.hasChildren = false;
for (int i = 0; i < 4; i++) {
m_root.children[i].reset();
}
m_pendingInsert.clear();
}
QRectF OsmQuadTree::childBounds(const Node &node, int quadrant) const
{
double x = node.bounds.x();
double y = node.bounds.y();
double w = node.bounds.width() / 2.0;
double h = node.bounds.height() / 2.0;
switch (quadrant) {
case 0: return QRectF(x + w, y, w, h);
case 1: return QRectF(x, y, w, h);
case 2: return QRectF(x, y + h, w, h);
case 3: return QRectF(x + w, y + h, w, h);
default: return node.bounds;
}
}
int OsmQuadTree::getQuadrant(const Node &node, const QRectF &bbox) const
{
QPointF center = node.bounds.center();
bool top = bbox.bottom() < center.y();
bool bottom = bbox.y() > center.y();
bool left = bbox.right() < center.x();
bool right = bbox.x() > center.x();
if (top) {
if (right) return 0;
if (left) return 1;
}
if (bottom) {
if (left) return 2;
if (right) return 3;
}
return -1;
}
void OsmQuadTree::split(Node &node)
{
for (int i = 0; i < 4; i++) {
node.children[i] = std::make_unique<Node>();
node.children[i]->bounds = childBounds(node, i);
}
node.hasChildren = true;
}
void OsmQuadTree::insert(int elementIndex, const QRectF &bbox)
{
insertRecursive(m_root, 0, elementIndex, bbox);
}
void OsmQuadTree::insertRecursive(Node &node, int depth, int elementIndex, const QRectF &bbox)
{
if (node.hasChildren) {
int quadrant = getQuadrant(node, bbox);
if (quadrant != -1) {
insertRecursive(*node.children[quadrant], depth + 1, elementIndex, bbox);
return;
}
}
node.elements.append(elementIndex);
node.elementBBoxes.append(bbox);
if (!node.hasChildren && depth < m_maxDepth && node.elements.size() > m_maxElements) {
split(node);
QVector<int> oldElements = node.elements;
QVector<QRectF> oldBBoxes = node.elementBBoxes;
node.elements.clear();
node.elementBBoxes.clear();
for (int i = 0; i < oldElements.size(); i++) {
int q = getQuadrant(node, oldBBoxes[i]);
if (q != -1) {
insertRecursive(*node.children[q], depth + 1, oldElements[i], oldBBoxes[i]);
} else {
node.elements.append(oldElements[i]);
node.elementBBoxes.append(oldBBoxes[i]);
}
}
}
}
QList<int> OsmQuadTree::query(const QRectF &bbox) const
{
QList<int> result;
queryRecursive(m_root, bbox, result);
return result;
}
void OsmQuadTree::queryRecursive(const Node &node, const QRectF &bbox, QList<int> &result) const
{
for (int i = 0; i < node.elements.size(); i++) {
if (node.elementBBoxes[i].intersects(bbox)) {
result.append(node.elements[i]);
}
}
if (node.hasChildren) {
for (int i = 0; i < 4; i++) {
if (node.children[i]->bounds.intersects(bbox)) {
queryRecursive(*node.children[i], bbox, result);
}
}
}
}
QList<int> OsmQuadTree::queryPoint(const QPointF &point, double tolerance) const
{
QRectF searchRect(point.x() - tolerance, point.y() - tolerance, tolerance * 2, tolerance * 2);
return query(searchRect);
}
void OsmQuadTree::rebuild()
{
}