Loading...
Searching...
No Matches
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
17
18namespace CubbyFlow
19{
21template <typename T, size_t K>
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
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
93
96
98 [[nodiscard]] ConstIterator begin() const;
99
101 [[nodiscard]] ConstIterator end() const;
102
105
108
111
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:23
void Build(const ConstArrayView1< Point > &points)
Builds internal acceleration structure for given points list.
Definition KdTree-Impl.hpp:44
void Reserve(size_t numPoints, size_t numNodes)
Reserves memory space for this tree.
Definition KdTree-Impl.hpp:264
typename NodeContainerType::const_iterator ConstNodeIterator
Definition KdTree.hpp:60
size_t GetNearestPoint(const Point &origin) const
Returns index of the nearest point.
Definition KdTree-Impl.hpp:196
void ForEachNearbyPoint(const Point &origin, T radius, const std::function< void(size_t, const Point &)> &callback) const
Definition KdTree-Impl.hpp:64
std::vector< Node > NodeContainerType
Definition KdTree.hpp:58
typename ContainerType::const_iterator ConstIterator
Definition KdTree.hpp:56
NodeIterator EndNode()
Returns the mutable end iterator of the node.
Definition KdTree-Impl.hpp:301
Iterator begin()
Returns the mutable begin iterator of the item.
Definition KdTree-Impl.hpp:271
typename ContainerType::iterator Iterator
Definition KdTree.hpp:55
bool HasNearbyPoint(const Point &origin, T radius) const
Definition KdTree-Impl.hpp:130
std::vector< Point > ContainerType
Definition KdTree.hpp:54
typename NodeContainerType::iterator NodeIterator
Definition KdTree.hpp:59
NodeIterator BeginNode()
Returns the mutable begin iterator of the node.
Definition KdTree-Impl.hpp:295
Iterator end()
Returns the mutable end iterator of the item.
Definition KdTree-Impl.hpp:277
Definition Matrix.hpp:30
Definition pybind11Utils.hpp:21
Matrix< T, Rows, 1 > Vector
Definition Matrix.hpp:738
Simple K-d tree node.
Definition KdTree.hpp:30
Point point
Point stored in the node.
Definition KdTree.hpp:51
size_t item
Item index.
Definition KdTree.hpp:48
void InitLeaf(size_t it, const Point &pt)
Initializes leaf node.
Definition KdTree-Impl.hpp:19
size_t child
Right child index. Note that left child index is this node index + 1.
Definition KdTree.hpp:45
void InitInternal(size_t axis, size_t it, size_t c, const Point &pt)
Initializes internal node.
Definition KdTree-Impl.hpp:28
bool IsLeaf() const
Returns true if leaf.
Definition KdTree-Impl.hpp:38
size_t flags
Split axis if flags < K, leaf indicator if flags == K.
Definition KdTree.hpp:41