Stride Reference Manual  - generated for commit 9643b11
KdTree.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2018, 2019, Jan Broeckhove and Bistromatics group.
14  */
15 
16 #pragma once
17 
18 #include "AABBox.h"
19 #include "KdNode.h"
20 
21 #include <cstddef>
22 #include <functional>
23 #include <memory>
24 #include <utility>
25 #include <vector>
26 
27 namespace geopop {
28 
43 template <typename P>
44 class KdTree
45 {
46 public:
47  static_assert(P::dim > 0, "Cannot have points in 0 dimensions");
48 
52  KdTree();
53 
59  static KdTree Build(const std::vector<P>& points);
60 
65  void Apply(std::function<bool(const P&)> f) const;
66 
72  void Apply(std::function<bool(const P&)> f, const AABBox<P>& box) const;
73 
79  bool Contains(const P& point) const;
80 
82  bool Empty() const;
83 
85  std::size_t GetHeight() const;
86 
91  void Insert(P point);
92 
98  std::vector<P> Query(const AABBox<P>& box) const;
99 
101  std::size_t Size() const;
102 
103 private:
104  template <std::size_t D>
105  static std::unique_ptr<kd::Node<P, D>> Construct(const std::vector<P>& points);
106 
107 private:
108  std::size_t m_size;
109  std::unique_ptr<kd::Node<P, 0>> m_root;
110 };
111 
112 } // namespace geopop
bool Empty() const
Is the tree empty.
Definition: KdTree_def.h:114
static std::unique_ptr< kd::Node< P, D > > Construct(const std::vector< P > &points)
Definition: KdTree_def.h:178
std::size_t Size() const
Get the size of the tree.
Definition: KdTree_def.h:171
std::size_t m_size
The number of points in the tree.
Definition: KdTree.h:108
void Insert(P point)
Insert a new point into the tree, using this often may result in an unbalanced tree.
Definition: KdTree_def.h:139
std::size_t GetHeight() const
Get the height of the tree (mostly for testing purposes).
Definition: KdTree_def.h:120
KdTree()
Constructor: builds an empty tree.
Definition: KdTree_def.h:35
Namespace for the geographic and demograhic classes.
Definition: Coordinate.h:21
std::unique_ptr< kd::Node< P, 0 > > m_root
The root node of the tree.
Definition: KdTree.h:109
std::vector< P > Query(const AABBox< P > &box) const
Get all points in the tree that lie within box.
Definition: KdTree_def.h:158
static KdTree Build(const std::vector< P > &points)
Build a balanced tree from the given set of points efficiently.
Definition: KdTree_def.h:40
bool Contains(const P &point) const
Test wether a point is contained in the tree.
Definition: KdTree_def.h:100
void Apply(std::function< bool(const P &)> f) const
Calls a function with each of the points in the tree.
Definition: KdTree_def.h:49