GEOS  3.13.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 <memory>
23 #include <vector>
24 #include <string>
25 #include <deque>
26 
27 #ifdef _MSC_VER
28 #pragma warning(push)
29 #pragma warning(disable: 4251) // warning C4251: needs to have dll-interface to be used by clients of class
30 #endif
31 
32 
33 namespace geos {
34 namespace index { // geos::index
35 namespace kdtree { // geos::index::kdtree
36 
55 class GEOS_DLL KdTree {
56 
57 private:
58 
59  std::deque<KdNode> nodeQue;
60  KdNode *root;
61  std::size_t numberOfNodes;
62  double tolerance;
63 
64  KdNode* findBestMatchNode(const geom::Coordinate& p);
65  KdNode* insertExact(const geom::Coordinate& p, void* data);
66 
67  void queryNode(KdNode* currentNode, const geom::Envelope& queryEnv, bool odd, KdNodeVisitor& visitor);
68  KdNode* queryNodePoint(KdNode* currentNode, const geom::Coordinate& queryPt, bool odd);
69 
74  KdNode* createNode(const geom::Coordinate& p, void* data);
75 
76 
81  class BestMatchVisitor : public KdNodeVisitor {
82  public:
83  BestMatchVisitor(const geom::Coordinate& p_p, double p_tolerance);
84  geom::Envelope queryEnvelope();
85  KdNode* getNode();
86  void visit(KdNode* node) override;
87 
88  private:
89  // Members
90  double tolerance;
91  KdNode* matchNode;
92  double matchDist;
93  const geom::Coordinate& p;
94  // Declare type as noncopyable
95  BestMatchVisitor(const BestMatchVisitor& other);
96  BestMatchVisitor& operator=(const BestMatchVisitor& rhs);
97  };
98 
103  class AccumulatingVisitor : public KdNodeVisitor {
104  public:
105  AccumulatingVisitor(std::vector<KdNode*>& p_nodeList) :
106  nodeList(p_nodeList) {};
107  void visit(KdNode* node) override { nodeList.push_back(node); }
108 
109  private:
110  // Members
111  std::vector<KdNode*>& nodeList;
112  // Declare type as noncopyable
113  AccumulatingVisitor(const AccumulatingVisitor& other);
114  AccumulatingVisitor& operator=(const AccumulatingVisitor& rhs);
115  };
116 
117 
118 
119 public:
120 
127  static std::unique_ptr<std::vector<geom::Coordinate>> toCoordinates(std::vector<KdNode*>& kdnodes);
128 
140  static std::unique_ptr<std::vector<geom::Coordinate>> toCoordinates(std::vector<KdNode*>& kdnodes, bool includeRepeated);
141 
142  KdTree() :
143  root(nullptr),
144  numberOfNodes(0),
145  tolerance(0.0)
146  {};
147 
148  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::unique_ptr<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:216
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition: Envelope.h:58
Definition: KdNode.h:30
Definition: KdTree.h:55
KdNode * query(const geom::Coordinate &queryPt)
KdNode * insert(const geom::Coordinate &p)
static std::unique_ptr< std::vector< geom::Coordinate > > toCoordinates(std::vector< KdNode * > &kdnodes)
static std::unique_ptr< std::vector< geom::Coordinate > > toCoordinates(std::vector< KdNode * > &kdnodes, bool includeRepeated)
void query(const geom::Envelope &queryEnv, KdNodeVisitor &visitor)
void query(const geom::Envelope &queryEnv, std::vector< KdNode * > &result)
std::unique_ptr< std::vector< KdNode * > > query(const geom::Envelope &queryEnv)
Basic namespace for all GEOS functionalities.
Definition: Angle.h:25