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 
17 namespace CubbyFlow
18 {
28 template <typename T>
29 class Quadtree final : public IntersectionQueryEngine2<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 
44  Quadtree(Quadtree&&) noexcept = default;
45 
47  virtual ~Quadtree() = default;
48 
50  Quadtree& operator=(const Quadtree&) = default;
51 
53  Quadtree& operator=(Quadtree&&) noexcept = default;
54 
57  void Build(const std::vector<T>& items, const BoundingBox2D& bound,
58  const BoxIntersectionTestFunc2<T>& testFunc, size_t maxDepth);
59 
61  void Clear();
62 
65  [[nodiscard]] NearestNeighborQueryResult2<T> Nearest(
66  const Vector2D& pt,
67  const NearestNeighborDistanceFunc2<T>& distanceFunc) const override;
68 
70  [[nodiscard]] bool Intersects(
71  const BoundingBox2D& box,
72  const BoxIntersectionTestFunc2<T>& testFunc) const override;
73 
75  [[nodiscard]] bool Intersects(
76  const Ray2D& ray,
77  const RayIntersectionTestFunc2<T>& testFunc) const override;
78 
81  const BoundingBox2D& box, const BoxIntersectionTestFunc2<T>& testFunc,
82  const IntersectionVisitorFunc<T>& visitorFunc) const override;
83 
86  const Ray2D& ray, const RayIntersectionTestFunc2<T>& testFunc,
87  const IntersectionVisitorFunc<T>& visitorFunc) const override;
88 
91  const Ray2D& ray,
92  const GetRayIntersectionFunc2<T>& testFunc) const override;
93 
95  [[nodiscard]] Iterator begin();
96 
98  [[nodiscard]] Iterator end();
99 
101  [[nodiscard]] ConstIterator begin() const;
102 
104  [[nodiscard]] ConstIterator end() const;
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 
116  [[nodiscard]] const std::vector<size_t>& GetItemsAtNode(
117  size_t nodeIdx) const;
118 
131  [[nodiscard]] size_t GetChildIndex(size_t nodeIdx, size_t childIdx) const;
132 
134  [[nodiscard]] const BoundingBox2D& GetBoundingBox() const;
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,
149  const BoxIntersectionTestFunc2<T>& testFunc);
150 
151  [[nodiscard]] bool Intersects(const BoundingBox2D& box,
152  const BoxIntersectionTestFunc2<T>& testFunc,
153  size_t nodeIdx,
154  const BoundingBox2D& Bound) const;
155 
156  [[nodiscard]] bool Intersects(const Ray2D& ray,
157  const RayIntersectionTestFunc2<T>& testFunc,
158  size_t nodeIdx,
159  const BoundingBox2D& Bound) const;
160 
161  void ForEachIntersectingItem(const BoundingBox2D& box,
162  const BoxIntersectionTestFunc2<T>& testFunc,
163  const IntersectionVisitorFunc<T>& visitorFunc,
164  size_t nodeIdx,
165  const BoundingBox2D& Bound) const;
166 
167  void ForEachIntersectingItem(const Ray2D& ray,
168  const RayIntersectionTestFunc2<T>& testFunc,
169  const IntersectionVisitorFunc<T>& visitorFunc,
170  size_t nodeIdx,
171  const BoundingBox2D& Bound) const;
172 
174  const Ray2D& ray, const GetRayIntersectionFunc2<T>& testFunc,
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 nearest neighbor query result.
Definition: NearestNeighborQueryEngine.hpp:23
Class for N-D ray.
Definition: Ray.hpp:25
std::vector< T > ContainerType
Definition: Quadtree.hpp:33
N-D axis-aligned bounding box class.
Definition: BoundingBox.hpp:46
size_t GetNumberOfNodes() const
Returns the number of quadtree nodes.
Definition: Quadtree-Impl.hpp:222
size_t GetMaxDepth() const
Returns the maximum depth of the tree.
Definition: Quadtree-Impl.hpp:246
size_t GetChildIndex(size_t nodeIdx, size_t childIdx) const
Returns a child&#39;s index for given node.
Definition: Quadtree-Impl.hpp:234
NearestNeighborQueryResult2< T > Nearest(const Vector2D &pt, const NearestNeighborDistanceFunc2< T > &distanceFunc) const override
Definition: Quadtree-Impl.hpp:59
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
void Build(const std::vector< T > &items, const BoundingBox2D &bound, const BoxIntersectionTestFunc2< T > &testFunc, size_t maxDepth)
Definition: Quadtree-Impl.hpp:26
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
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
Definition: Matrix.hpp:27
void Clear()
Clears all the contents of this instance.
Definition: Quadtree-Impl.hpp:50
N-D closest intersection query result.
Definition: IntersectionQueryEngine.hpp:25
Definition: pybind11Utils.hpp:20
Generic quadtree data structure.
Definition: Quadtree.hpp:29
GetRayIntersectionFunc< T, 2 > GetRayIntersectionFunc2
2-D ray-item closest intersection evaluation function.
Definition: IntersectionQueryEngine.hpp:85
Abstract base class for N-D intersection test query engine.
Definition: IntersectionQueryEngine.hpp:97
Iterator begin()
Returns the begin iterator of the item.
Definition: Quadtree-Impl.hpp:186
size_t GetNumberOfItems() const
Returns the number of items.
Definition: Quadtree-Impl.hpp:210
BoxIntersectionTestFunc< T, 2 > BoxIntersectionTestFunc2
2-D box-item intersection test function.
Definition: IntersectionQueryEngine.hpp:59
Iterator end()
Returns the end iterator of the item.
Definition: Quadtree-Impl.hpp:192
const BoundingBox2D & GetBoundingBox() const
Returns the bounding box of this quadtree.
Definition: Quadtree-Impl.hpp:240
RayIntersectionTestFunc< T, 2 > RayIntersectionTestFunc2
2-D ray-item intersection test function.
Definition: IntersectionQueryEngine.hpp:72
Quadtree()=default
Default constructor.
Abstract base class for N-D nearest neighbor query engine.
Definition: NearestNeighborQueryEngine.hpp:52
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 T & GetItem(size_t i) const
Returns the item at i.
Definition: Quadtree-Impl.hpp:216
typename ContainerType::const_iterator ConstIterator
Definition: Quadtree.hpp:35
std::function< void(const T &)> IntersectionVisitorFunc
Visitor function which is invoked for each intersecting item.
Definition: IntersectionQueryEngine.hpp:93
typename ContainerType::iterator Iterator
Definition: Quadtree.hpp:34
NearestNeighborDistanceFunc< T, 2 > NearestNeighborDistanceFunc2
2-D nearest neighbor distance measure function.
Definition: NearestNeighborQueryEngine.hpp:44