Loading...
Searching...
No Matches
Quadtree.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_QUADTREE_HPP
12#define CUBBYFLOW_QUADTREE_HPP
13
16
17namespace CubbyFlow
18{
28template <typename T>
31{
32 public:
33 using ContainerType = std::vector<T>;
34 using Iterator = typename ContainerType::iterator;
35 using ConstIterator = typename ContainerType::const_iterator;
36
38 Quadtree() = default;
39
41 Quadtree(const Quadtree&) = default;
42
45
48
51
54
57 void Build(const std::vector<T>& items, const BoundingBox2D& bound,
59
61 void Clear();
62
68
70 [[nodiscard]] bool Intersects(
73
75 [[nodiscard]] bool Intersects(
78
83
88
93
96
99
102
105
107 [[nodiscard]] size_t GetNumberOfItems() const;
108
110 [[nodiscard]] const T& GetItem(size_t i) const;
111
113 [[nodiscard]] size_t GetNumberOfNodes() const;
114
117 size_t nodeIdx) const;
118
131 [[nodiscard]] size_t GetChildIndex(size_t nodeIdx, size_t childIdx) const;
132
135
137 [[nodiscard]] size_t GetMaxDepth() const;
138
139 private:
140 struct Node
141 {
142 [[nodiscard]] bool IsLeaf() const;
143
144 size_t firstChild = std::numeric_limits<size_t>::max();
145 std::vector<size_t> items;
146 };
147
148 void Build(size_t nodeIdx, size_t Depth, const BoundingBox2D& Bound,
150
151 [[nodiscard]] bool Intersects(const BoundingBox2D& box,
153 size_t nodeIdx,
154 const BoundingBox2D& Bound) const;
155
156 [[nodiscard]] bool Intersects(const Ray2D& ray,
158 size_t nodeIdx,
159 const BoundingBox2D& Bound) const;
160
164 size_t nodeIdx,
165 const BoundingBox2D& Bound) const;
166
170 size_t nodeIdx,
171 const BoundingBox2D& Bound) const;
172
175 size_t nodeIdx, const BoundingBox2D& Bound,
177
178 size_t m_maxDepth = 1;
179 BoundingBox2D m_bbox;
180 std::vector<T> m_items;
181 std::vector<Node> m_nodes;
182};
183} // namespace CubbyFlow
184
186
187#endif
N-D axis-aligned bounding box class.
Definition BoundingBox.hpp:47
Abstract base class for N-D intersection test query engine.
Definition IntersectionQueryEngine.hpp:98
Definition Matrix.hpp:30
Abstract base class for N-D nearest neighbor query engine.
Definition NearestNeighborQueryEngine.hpp:53
Generic quadtree data structure.
Definition Quadtree.hpp:31
size_t GetMaxDepth() const
Returns the maximum depth of the tree.
Definition Quadtree-Impl.hpp:246
Quadtree()=default
Default constructor.
void Build(const std::vector< T > &items, const BoundingBox2D &bound, const BoxIntersectionTestFunc2< T > &testFunc, size_t maxDepth)
Definition Quadtree-Impl.hpp:26
Quadtree(Quadtree &&) noexcept=default
Default move constructor.
size_t GetNumberOfItems() const
Returns the number of items.
Definition Quadtree-Impl.hpp:210
Iterator begin()
Returns the begin iterator of the item.
Definition Quadtree-Impl.hpp:186
Quadtree(const Quadtree &)=default
Default copy constructor.
std::vector< T > ContainerType
Definition Quadtree.hpp:33
NearestNeighborQueryResult2< T > Nearest(const Vector2D &pt, const NearestNeighborDistanceFunc2< T > &distanceFunc) const override
Definition Quadtree-Impl.hpp:59
bool Intersects(const BoundingBox2D &box, const BoxIntersectionTestFunc2< T > &testFunc) const override
Returns true if given box intersects with any of the stored items.
Definition Quadtree-Impl.hpp:145
size_t GetNumberOfNodes() const
Returns the number of quadtree nodes.
Definition Quadtree-Impl.hpp:222
size_t GetChildIndex(size_t nodeIdx, size_t childIdx) const
Returns a child's index for given node.
Definition Quadtree-Impl.hpp:234
ClosestIntersectionQueryResult2< T > ClosestIntersection(const Ray2D &ray, const GetRayIntersectionFunc2< T > &testFunc) const override
Returns the closest intersection for given ray.
Definition Quadtree-Impl.hpp:175
const std::vector< size_t > & GetItemsAtNode(size_t nodeIdx) const
Returns the list of the items for given node index.
Definition Quadtree-Impl.hpp:228
const BoundingBox2D & GetBoundingBox() const
Returns the bounding box of this quadtree.
Definition Quadtree-Impl.hpp:240
void ForEachIntersectingItem(const BoundingBox2D &box, const BoxIntersectionTestFunc2< T > &testFunc, const IntersectionVisitorFunc< T > &visitorFunc) const override
Invokes visitorFunc for every intersecting items.
Definition Quadtree-Impl.hpp:159
typename ContainerType::iterator Iterator
Definition Quadtree.hpp:34
void Clear()
Clears all the contents of this instance.
Definition Quadtree-Impl.hpp:50
typename ContainerType::const_iterator ConstIterator
Definition Quadtree.hpp:35
Iterator end()
Returns the end iterator of the item.
Definition Quadtree-Impl.hpp:192
const T & GetItem(size_t i) const
Returns the item at i.
Definition Quadtree-Impl.hpp:216
Class for N-D ray.
Definition Ray.hpp:26
Definition pybind11Utils.hpp:21
std::function< void(const T &)> IntersectionVisitorFunc
Visitor function which is invoked for each intersecting item.
Definition IntersectionQueryEngine.hpp:93
RayIntersectionTestFunc< T, 2 > RayIntersectionTestFunc2
2-D ray-item intersection test function.
Definition IntersectionQueryEngine.hpp:72
Matrix< T, Rows, 1 > Vector
Definition Matrix.hpp:738
NearestNeighborDistanceFunc< T, 2 > NearestNeighborDistanceFunc2
2-D nearest neighbor distance measure function.
Definition NearestNeighborQueryEngine.hpp:44
BoxIntersectionTestFunc< T, 2 > BoxIntersectionTestFunc2
2-D box-item intersection test function.
Definition IntersectionQueryEngine.hpp:59
GetRayIntersectionFunc< T, 2 > GetRayIntersectionFunc2
2-D ray-item closest intersection evaluation function.
Definition IntersectionQueryEngine.hpp:85
N-D closest intersection query result.
Definition IntersectionQueryEngine.hpp:26