-
Notifications
You must be signed in to change notification settings - Fork 420
Expand file tree
/
Copy pathKdTree.cpp
More file actions
341 lines (294 loc) · 8.52 KB
/
Copy pathKdTree.cpp
File metadata and controls
341 lines (294 loc) · 8.52 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
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
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca>
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************/
#include <geos/index/kdtree/KdTree.h>
#include <geos/geom/Envelope.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace geos::geom;
namespace geos {
namespace index { // geos.index
namespace kdtree { // geos.index.kdtree
/*public static*/
std::unique_ptr<std::vector<Coordinate>>
KdTree::toCoordinates(std::vector<KdNode*>& kdnodes)
{
return toCoordinates(kdnodes, false);
}
/*public static*/
std::unique_ptr<std::vector<Coordinate>>
KdTree::toCoordinates(std::vector<KdNode*>& kdnodes, bool includeRepeated)
{
std::unique_ptr<std::vector<Coordinate>> coord(new std::vector<Coordinate>);
for (auto node: kdnodes) {
std::size_t count = includeRepeated ? node->getCount() : 1;
for (std::size_t i = 0; i < count; i++) {
coord->emplace_back(node->getCoordinate());
}
}
if (!includeRepeated) {
// Remove duplicate Coordinates from coordList
coord->erase(std::unique(coord->begin(), coord->end()), coord->end());
}
return coord;
}
/*private*/
KdNode*
KdTree::createNode(const Coordinate& p, void* data)
{
auto it = nodeQue.emplace(nodeQue.end(), p, data);
return &(*it);
}
/*public*/
KdNode*
KdTree::insert(const Coordinate& p)
{
return insert(p, nullptr);
}
/*public*/
KdNode*
KdTree::insert(const Coordinate& p, void* data)
{
if (root == nullptr) {
root = createNode(p, data);
return root;
}
/**
* Check if the point is already in the tree, up to tolerance.
* If tolerance is zero, this phase of the insertion can be skipped.
*/
if (tolerance > 0) {
KdNode* matchNode = findBestMatchNode(p);
if (matchNode != nullptr) {
// point already in index - increment counter
matchNode->increment();
return matchNode;
}
}
return insertExact(p, data);
}
/*private*/
KdNode*
KdTree::findBestMatchNode(const Coordinate& p) {
BestMatchVisitor visitor(p, tolerance);
query(visitor.queryEnvelope(), visitor);
return visitor.getNode();
}
KdNode*
KdTree::insertExact(const geom::Coordinate& p, void* data)
{
KdNode* currentNode = root;
KdNode* leafNode = root;
bool isOddLevel = true;
bool isLessThan = true;
/**
* Traverse the tree, first cutting the plane left-right (by X ordinate)
* then top-bottom (by Y ordinate)
*/
while (currentNode != nullptr) {
// test if point is already a node (not strictly necessary)
bool isInTolerance = p.distance(currentNode->getCoordinate()) <= tolerance;
// check if point is already in tree (up to tolerance) and if so simply
// return existing node
if (isInTolerance) {
currentNode->increment();
return currentNode;
}
if (isOddLevel) {
isLessThan = p.x < currentNode->getX();
} else {
isLessThan = p.y < currentNode->getY();
}
leafNode = currentNode;
if (isLessThan) {
currentNode = currentNode->getLeft();
} else {
currentNode = currentNode->getRight();
}
isOddLevel = !isOddLevel;
}
// no node found, add new leaf node to tree
numberOfNodes++;
KdNode* node = createNode(p, data);
if (isLessThan) {
leafNode->setLeft(node);
} else {
leafNode->setRight(node);
}
return node;
}
/*private*/
void
KdTree::queryNode(KdNode* currentNode, const geom::Envelope& queryEnv, bool odd, KdNodeVisitor& visitor)
{
// Non recursive formulation of in-order traversal from
// http://web.cs.wpi.edu/~cs2005/common/iterative.inorder
// Otherwise we may blow up the stack
// See https://github.com/qgis/QGIS/issues/45226
typedef std::pair<KdNode*, bool> Pair;
std::stack<Pair> activeNodes;
while(true)
{
if( currentNode != nullptr )
{
double min;
double discriminant;
if (odd) {
min = queryEnv.getMinX();
discriminant = currentNode->getX();
} else {
min = queryEnv.getMinY();
discriminant = currentNode->getY();
}
bool searchLeft = min < discriminant;
activeNodes.emplace(Pair(currentNode, odd));
// search is computed via in-order traversal
KdNode* leftNode = nullptr;
if (searchLeft ) {
leftNode = currentNode->getLeft();
}
if( leftNode ) {
currentNode = leftNode;
odd = !odd;
} else {
currentNode = nullptr;
}
}
else if( !activeNodes.empty() )
{
currentNode = activeNodes.top().first;
odd = activeNodes.top().second;
activeNodes.pop();
if (queryEnv.contains(currentNode->getCoordinate())) {
visitor.visit(currentNode);
}
double max;
double discriminant;
if (odd) {
max = queryEnv.getMaxX();
discriminant = currentNode->getX();
} else {
max = queryEnv.getMaxY();
discriminant = currentNode->getY();
}
bool searchRight = discriminant <= max;
if (searchRight) {
currentNode = currentNode->getRight();
if( currentNode )
odd = !odd;
} else {
currentNode = nullptr;
}
}
else
{
break;
}
}
}
/*private*/
KdNode*
KdTree::queryNodePoint(KdNode* currentNode, const geom::Coordinate& queryPt, bool odd)
{
while (currentNode != nullptr)
{
if (currentNode->getCoordinate().equals2D(queryPt))
return currentNode;
double ord;
double discriminant;
if (odd) {
ord = queryPt.x;
discriminant = currentNode->getX();
}
else {
ord = queryPt.y;
discriminant = currentNode->getY();
}
bool searchLeft = (ord < discriminant);
odd = !odd;
if (searchLeft) {
currentNode = currentNode->getLeft();
}
else {
currentNode = currentNode->getRight();
}
}
return nullptr;
}
/*public*/
void
KdTree::query(const geom::Envelope& queryEnv, KdNodeVisitor& visitor)
{
queryNode(root, queryEnv, true, visitor);
}
/*public*/
std::unique_ptr<std::vector<KdNode*>>
KdTree::query(const geom::Envelope& queryEnv)
{
std::unique_ptr<std::vector<KdNode*>> result(new std::vector<KdNode*>);
query(queryEnv, *result);
return result;
}
/*public*/
void
KdTree::query(const geom::Envelope& queryEnv, std::vector<KdNode*>& result)
{
AccumulatingVisitor visitor(result);
queryNode(root, queryEnv, true, visitor);
}
/*public*/
KdNode*
KdTree::query(const geom::Coordinate& queryPt) {
return queryNodePoint(root, queryPt, true);
}
/**********************************************************************/
/*private*/
KdTree::BestMatchVisitor::BestMatchVisitor(const geom::Coordinate& p_p, double p_tolerance)
: tolerance(p_tolerance)
, matchNode(nullptr)
, matchDist(0.0)
, p(p_p) {}
/*private*/
Envelope
KdTree::BestMatchVisitor::queryEnvelope() {
geom::Envelope queryEnv(p);
queryEnv.expandBy(tolerance);
return queryEnv;
}
/*private*/
KdNode*
KdTree::BestMatchVisitor::getNode() {
return matchNode;
}
/*private*/
void
KdTree::BestMatchVisitor::visit(KdNode* node) {
double dist = p.distance(node->getCoordinate());
if (! (dist <= tolerance)) return;
bool update = false;
if (matchNode == nullptr || dist < matchDist
// if distances are the same, record the lesser coordinate
|| (matchNode != nullptr && dist == matchDist
&& node->getCoordinate().compareTo(matchNode->getCoordinate()) < 1)) {
update = true;
}
if (update) {
matchNode = node;
matchDist = dist;
}
}
} // namespace geos.index.kdtree
} // namespace geos.index
} // namespace geos