KdTree.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_KDTREE_HPP
12 #define CUBBYFLOW_KDTREE_HPP
13 
14 #include <Core/Array/ArrayView.hpp>
16 #include <Core/Matrix/Matrix.hpp>
17 
18 namespace CubbyFlow
19 {
21 template <typename T, size_t K>
22 class KdTree final
23 {
24  public:
27 
29  struct Node
30  {
32  void InitLeaf(size_t it, const Point& pt);
33 
35  void InitInternal(size_t axis, size_t it, size_t c, const Point& pt);
36 
38  [[nodiscard]] bool IsLeaf() const;
39 
41  size_t flags = 0;
42 
45  size_t child = std::numeric_limits<size_t>::max();
46 
48  size_t item = std::numeric_limits<size_t>::max();
49 
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  void Build(const ConstArrayView1<Point>& points);
64 
73  void ForEachNearbyPoint(
74  const Point& origin, T radius,
75  const std::function<void(size_t, const Point&)>& callback) const;
76 
86  [[nodiscard]] bool HasNearbyPoint(const Point& origin, T radius) const;
87 
89  [[nodiscard]] size_t GetNearestPoint(const Point& origin) const;
90 
92  [[nodiscard]] Iterator begin();
93 
95  [[nodiscard]] Iterator end();
96 
98  [[nodiscard]] ConstIterator begin() const;
99 
101  [[nodiscard]] ConstIterator end() const;
102 
104  [[nodiscard]] NodeIterator BeginNode();
105 
107  [[nodiscard]] NodeIterator EndNode();
108 
110  [[nodiscard]] ConstNodeIterator BeginNode() const;
111 
113  [[nodiscard]] ConstNodeIterator EndNode() const;
114 
116  void Reserve(size_t numPoints, size_t numNodes);
117 
118  private:
119  [[nodiscard]] size_t Build(size_t nodeIndex, size_t* itemIndices,
120  size_t nItems, size_t currentDepth);
121 
122  std::vector<Point> m_points;
123  std::vector<Node> m_nodes;
124 };
125 } // namespace CubbyFlow
126 
128 
129 #endif
Generic k-d tree structure.
Definition: KdTree.hpp:22
std::vector< Node > NodeContainerType
Definition: KdTree.hpp:58
void Reserve(size_t numPoints, size_t numNodes)
Reserves memory space for this tree.
Definition: KdTree-Impl.hpp:264
NodeIterator EndNode()
Returns the mutable end iterator of the node.
Definition: KdTree-Impl.hpp:301
typename ContainerType::const_iterator ConstIterator
Definition: KdTree.hpp:56
typename NodeContainerType::iterator NodeIterator
Definition: KdTree.hpp:59
N-D axis-aligned bounding box class.
Definition: BoundingBox.hpp:46
Iterator begin()
Returns the mutable begin iterator of the item.
Definition: KdTree-Impl.hpp:271
bool IsLeaf() const
Returns true if leaf.
Definition: KdTree-Impl.hpp:38
bool HasNearbyPoint(const Point &origin, T radius) const
Definition: KdTree-Impl.hpp:130
Point point
Point stored in the node.
Definition: KdTree.hpp:51
NodeIterator BeginNode()
Returns the mutable begin iterator of the node.
Definition: KdTree-Impl.hpp:295
Definition: Matrix.hpp:27
void InitInternal(size_t axis, size_t it, size_t c, const Point &pt)
Initializes internal node.
Definition: KdTree-Impl.hpp:28
size_t GetNearestPoint(const Point &origin) const
Returns index of the nearest point.
Definition: KdTree-Impl.hpp:196
Definition: pybind11Utils.hpp:20
typename NodeContainerType::const_iterator ConstNodeIterator
Definition: KdTree.hpp:60
Simple K-d tree node.
Definition: KdTree.hpp:29
std::vector< Point > ContainerType
Definition: KdTree.hpp:54
void Build(const ConstArrayView1< Point > &points)
Builds internal acceleration structure for given points list.
Definition: KdTree-Impl.hpp:44
size_t flags
Split axis if flags < K, leaf indicator if flags == K.
Definition: KdTree.hpp:41
Generic N-dimensional array class interface.
Definition: Array.hpp:32
size_t child
Right child index. Note that left child index is this node index + 1.
Definition: KdTree.hpp:45
Iterator end()
Returns the mutable end iterator of the item.
Definition: KdTree-Impl.hpp:277
typename ContainerType::iterator Iterator
Definition: KdTree.hpp:55
size_t item
Item index.
Definition: KdTree.hpp:48
void ForEachNearbyPoint(const Point &origin, T radius, const std::function< void(size_t, const Point &)> &callback) const
Definition: KdTree-Impl.hpp:64
void InitLeaf(size_t it, const Point &pt)
Initializes leaf node.
Definition: KdTree-Impl.hpp:19