KdTree.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: KdTree.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Generic k-d tree structure.
6 > Created Time: 2017/12/05
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_KDTREE_H
10 #define CUBBYFLOW_KDTREE_H
11 
13 #include <Core/Vector/Vector.h>
14 
15 namespace CubbyFlow
16 {
18  template <typename T, size_t K>
19  class KdTree final
20  {
21  public:
24 
26  struct Node
27  {
29  size_t flags = 0;
30 
33  size_t child = std::numeric_limits<size_t>::max();
34 
36  size_t item = std::numeric_limits<size_t>::max();
37 
40 
42  Node();
43 
45  void InitLeaf(size_t it, const Point& pt);
46 
48  void InitInternal(size_t axis, size_t it, size_t c, const Point& pt);
49 
51  bool IsLeaf() const;
52  };
53 
54  using ContainerType = std::vector<Point>;
55  using Iterator = typename ContainerType::iterator;
56  using ConstIterator = typename ContainerType::const_iterator;
57 
58  using NodeContainerType = std::vector<Node>;
59  using NodeIterator = typename NodeContainerType::iterator;
60  using ConstNodeIterator = typename NodeContainerType::const_iterator;
61 
63  KdTree();
64 
66  void Build(const ConstArrayAccessor1<Point>& points);
67 
76  void ForEachNearbyPoint(
77  const Point& origin, T radius,
78  const std::function<void(size_t, const Point&)>& callback) const;
79 
89  bool HasNearbyPoint(const Point& origin, T radius) const;
90 
92  size_t GetNearestPoint(const Point& origin) const;
93 
95  Iterator begin();
96 
98  Iterator end();
99 
101  ConstIterator begin() const;
102 
104  ConstIterator end() const;
105 
108 
111 
114 
116  ConstNodeIterator EndNode() const;
117 
119  void Reserve(size_t numPoints, size_t numNodes);
120 
121  private:
122  std::vector<Point> m_points;
123  std::vector<Node> m_nodes;
124 
125  size_t Build(size_t nodeIndex, size_t* itemIndices, size_t nItems, size_t currentDepth);
126  };
127 }
128 
130 
131 #endif
Generic k-d tree structure.
Definition: KdTree.h:19
std::vector< Node > NodeContainerType
Definition: KdTree.h:58
void Reserve(size_t numPoints, size_t numNodes)
Reserves memory space for this tree.
Definition: KdTree-Impl.h:270
NodeIterator EndNode()
Returns the mutable end iterator of the node.
Definition: KdTree-Impl.h:307
typename ContainerType::const_iterator ConstIterator
Definition: KdTree.h:56
typename NodeContainerType::iterator NodeIterator
Definition: KdTree.h:59
Generic N-D axis-aligned bounding box class.
Definition: BoundingBox.h:23
Iterator begin()
Returns the mutable begin iterator of the item.
Definition: KdTree-Impl.h:277
bool IsLeaf() const
Returns true if leaf.
Definition: KdTree-Impl.h:41
Node()
Default contructor.
Definition: KdTree-Impl.h:17
bool HasNearbyPoint(const Point &origin, T radius) const
Definition: KdTree-Impl.h:137
Point point
Point stored in the node.
Definition: KdTree.h:39
Generic statically-sized N-D vector class.
Definition: Vector.h:33
1-D read-only array accessor class.
Definition: ArrayAccessor1.h:185
NodeIterator BeginNode()
Returns the mutable begin iterator of the node.
Definition: KdTree-Impl.h:301
void InitInternal(size_t axis, size_t it, size_t c, const Point &pt)
Initializes internal node.
Definition: KdTree-Impl.h:32
size_t GetNearestPoint(const Point &origin) const
Returns index of the nearest point.
Definition: KdTree-Impl.h:202
Definition: pybind11Utils.h:24
typename NodeContainerType::const_iterator ConstNodeIterator
Definition: KdTree.h:60
Simple K-d tree node.
Definition: KdTree.h:26
std::vector< Point > ContainerType
Definition: KdTree.h:54
size_t flags
Split axis if flags < K, leaf indicator if flags == K.
Definition: KdTree.h:29
void Build(const ConstArrayAccessor1< Point > &points)
Builds internal acceleration structure for given points list.
Definition: KdTree-Impl.h:53
size_t child
Right child index. Note that left child index is this node index + 1.
Definition: KdTree.h:33
Iterator end()
Returns the mutable end iterator of the item.
Definition: KdTree-Impl.h:283
KdTree()
Constructs an empty kD-tree instance.
Definition: KdTree-Impl.h:47
typename ContainerType::iterator Iterator
Definition: KdTree.h:55
size_t item
Item index.
Definition: KdTree.h:36
void ForEachNearbyPoint(const Point &origin, T radius, const std::function< void(size_t, const Point &)> &callback) const
Definition: KdTree-Impl.h:72
void InitLeaf(size_t it, const Point &pt)
Initializes leaf node.
Definition: KdTree-Impl.h:23