GEOS 3.15.0dev
KdTree.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2020 Paul Ramsey <pramsey@cleverelephant.ca>
7 *
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Public Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
12 *
13 **********************************************************************/
14
15#pragma once
16
17#include <geos/export.h>
18#include <geos/geom/Envelope.h>
19#include <geos/index/kdtree/KdNodeVisitor.h>
20#include <geos/index/kdtree/KdNode.h>
21
22#include <vector>
23#include <deque>
24
25#ifdef _MSC_VER
26#pragma warning(push)
27#pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
28#endif
29
30
31namespace geos {
32namespace index { // geos::index
33namespace kdtree { // geos::index::kdtree
34
53class GEOS_DLL KdTree {
54
55private:
56
57 std::deque<KdNode> nodeQue;
58 KdNode *root;
59 std::size_t numberOfNodes;
60 double tolerance;
61
62 KdNode* findBestMatchNode(const geom::Coordinate& p);
63 KdNode* insertExact(const geom::Coordinate& p, void* data);
64
65 static void queryNode(KdNode* currentNode, const geom::Envelope& queryEnv, bool odd, KdNodeVisitor& visitor);
66 static KdNode* queryNodePoint(KdNode* currentNode, const geom::Coordinate& queryPt, bool odd);
67
72 KdNode* createNode(const geom::Coordinate& p, void* data);
73
74
79 class BestMatchVisitor : public KdNodeVisitor {
80 public:
81 BestMatchVisitor(const geom::Coordinate& p_p, double p_tolerance);
82 geom::Envelope queryEnvelope();
83 KdNode* getNode();
84 void visit(KdNode* node) override;
85
86 // Declare type as noncopyable
87 BestMatchVisitor(const BestMatchVisitor& other) = delete;
88 BestMatchVisitor& operator=(const BestMatchVisitor& rhs) = delete;
89
90 private:
91 // Members
92 double tolerance;
93 KdNode* matchNode;
94 double matchDist;
95 const geom::Coordinate& p;
96 };
97
102 class AccumulatingVisitor : public KdNodeVisitor {
103 public:
104 explicit AccumulatingVisitor(std::vector<KdNode*>& p_nodeList) :
105 nodeList(p_nodeList) {};
106 void visit(KdNode* node) override { nodeList.push_back(node); }
107
108 // Declare type as noncopyable
109 AccumulatingVisitor(const AccumulatingVisitor& other) = delete;
110 AccumulatingVisitor& operator=(const AccumulatingVisitor& rhs) = delete;
111
112 private:
113 // Members
114 std::vector<KdNode*>& nodeList;
115 };
116
117
118
119public:
120
127 static std::vector<geom::Coordinate> toCoordinates(const std::vector<KdNode*>& kdNodes);
128
140 static std::vector<geom::Coordinate> toCoordinates(const std::vector<KdNode*>& kdNodes, bool includeRepeated);
141
142 KdTree() :
143 root(nullptr),
144 numberOfNodes(0),
145 tolerance(0.0)
146 {};
147
148 explicit KdTree(double p_tolerance) :
149 root(nullptr),
150 numberOfNodes(0),
151 tolerance(p_tolerance)
152 {};
153
154 bool isEmpty() { return root == nullptr; }
155
160 KdNode* insert(const geom::Coordinate& p, void* data);
161
165 void query(const geom::Envelope& queryEnv, KdNodeVisitor& visitor);
166
170 std::vector<KdNode*> query(const geom::Envelope& queryEnv);
171
175 void query(const geom::Envelope& queryEnv, std::vector<KdNode*>& result);
176
180 KdNode* query(const geom::Coordinate& queryPt);
181
182};
183
184} // namespace geos::index::kdtree
185} // namespace geos::index
186} // namespace geos
187
188#ifdef _MSC_VER
189#pragma warning(pop)
190#endif
191
Coordinate is the lightweight class used to store coordinates.
Definition Coordinate.h:220
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition Envelope.h:59
Definition KdNode.h:30
Definition KdTree.h:53
KdNode * query(const geom::Coordinate &queryPt)
static std::vector< geom::Coordinate > toCoordinates(const std::vector< KdNode * > &kdNodes)
KdNode * insert(const geom::Coordinate &p)
void query(const geom::Envelope &queryEnv, KdNodeVisitor &visitor)
std::vector< KdNode * > query(const geom::Envelope &queryEnv)
void query(const geom::Envelope &queryEnv, std::vector< KdNode * > &result)
static std::vector< geom::Coordinate > toCoordinates(const std::vector< KdNode * > &kdNodes, bool includeRepeated)
Basic namespace for all GEOS functionalities.
Definition geos.h:38