Quadtree.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Quadtree.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Generic quadtree data structure.
6 > Created Time: 2017/10/15
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_QUADTREE_H
10 #define CUBBYFLOW_QUADTREE_H
11 
14 
15 namespace CubbyFlow
16 {
26  template <typename T>
28  {
29  public:
30  using ContainerType = std::vector<T>;
31  using Iterator = typename ContainerType::iterator;
32  using ConstIterator = typename ContainerType::const_iterator;
33 
35  Quadtree();
36 
39  void Build(const std::vector<T>& items, const BoundingBox2D& bound,
40  const BoxIntersectionTestFunc2<T>& testFunc, size_t maxDepth);
41 
43  void Clear();
44 
48  const Vector2D& pt,
49  const NearestNeighborDistanceFunc2<T>& distanceFunc) const override;
50 
52  bool IsIntersects(const BoundingBox2D& box,
53  const BoxIntersectionTestFunc2<T>& testFunc) const override;
54 
56  bool IsIntersects(const Ray2D& ray,
57  const RayIntersectionTestFunc2<T>& testFunc) const override;
58 
61  const BoundingBox2D& box, const BoxIntersectionTestFunc2<T>& testFunc,
62  const IntersectionVisitorFunc2<T>& visitorFunc) const override;
63 
66  const Ray2D& ray, const RayIntersectionTestFunc2<T>& testFunc,
67  const IntersectionVisitorFunc2<T>& visitorFunc) const override;
68 
71  const Ray2D& ray,
72  const GetRayIntersectionFunc2<T>& testFunc) const override;
73 
75  Iterator begin();
76 
78  Iterator end();
79 
81  ConstIterator begin() const;
82 
84  ConstIterator end() const;
85 
87  size_t GetNumberOfItems() const;
88 
90  const T& GetItem(size_t i) const;
91 
93  size_t GetNumberOfNodes() const;
94 
96  const std::vector<size_t>& GetItemsAtNode(size_t nodeIdx) const;
97 
110  size_t GetChildIndex(size_t nodeIdx, size_t childIdx) const;
111 
113  const BoundingBox2D& GetBoundingBox() const;
114 
116  size_t GetMaxDepth() const;
117 
118  private:
119  struct Node
120  {
121  size_t firstChild = std::numeric_limits<size_t>::max();
122  std::vector<size_t> items;
123 
124  bool IsLeaf() const;
125  };
126 
127  size_t m_maxDepth = 1;
128  BoundingBox2D m_bbox;
129  std::vector<T> m_items;
130  std::vector<Node> m_nodes;
131 
132  void Build(size_t nodeIdx, size_t currentDepth,
133  const BoundingBox2D& currentBound,
134  const BoxIntersectionTestFunc2<T>& overlapsFunc);
135 
136  bool IsIntersects(const BoundingBox2D& box,
137  const BoxIntersectionTestFunc2<T>& testFunc, size_t nodeIdx,
138  const BoundingBox2D& currentBound) const;
139 
140  bool IsIntersects(const Ray2D& ray,
141  const RayIntersectionTestFunc2<T>& testFunc, size_t nodeIdx,
142  const BoundingBox2D& currentBound) const;
143 
144  void ForEachIntersectingItem(const BoundingBox2D& box,
145  const BoxIntersectionTestFunc2<T>& testFunc,
146  const IntersectionVisitorFunc2<T>& visitorFunc,
147  size_t nodeIdx,
148  const BoundingBox2D& currentBound) const;
149 
150  void ForEachIntersectingItem(const Ray2D& ray,
151  const RayIntersectionTestFunc2<T>& testFunc,
152  const IntersectionVisitorFunc2<T>& visitorFunc,
153  size_t nodeIdx,
154  const BoundingBox2D& currentBound) const;
155 
157  const Ray2D& ray, const GetRayIntersectionFunc2<T>& testFunc,
158  size_t nodeIdx, const BoundingBox2D& currentBound,
160  };
161 }
162 
164 
165 #endif
NearestNeighborQueryResult2< T > GetNearestNeighbor(const Vector2D &pt, const NearestNeighborDistanceFunc2< T > &distanceFunc) const override
Definition: Quadtree-Impl.h:61
Quadtree()
Default constructor.
Definition: Quadtree-Impl.h:24
Abstract base class for 2-D intersection test query engine.
Definition: IntersectionQueryEngine2.h:47
Closest intersection query result.
Definition: IntersectionQueryEngine2.h:19
std::vector< T > ContainerType
Definition: Quadtree.h:30
Class for 2-D ray.
Definition: Ray2.h:23
size_t GetNumberOfNodes() const
Returns the number of quadtree nodes.
Definition: Quadtree-Impl.h:223
size_t GetMaxDepth() const
Returns the maximum depth of the tree.
Definition: Quadtree-Impl.h:247
std::function< bool(const T &, const BoundingBox2D &)> BoxIntersectionTestFunc2
Box-item intersection test function.
Definition: IntersectionQueryEngine2.h:31
size_t GetChildIndex(size_t nodeIdx, size_t childIdx) const
Returns a child&#39;s index for given node.
Definition: Quadtree-Impl.h:235
Nearest neighbor query result.
Definition: NearestNeighborQueryEngine2.h:18
void Build(const std::vector< T > &items, const BoundingBox2D &bound, const BoxIntersectionTestFunc2< T > &testFunc, size_t maxDepth)
Definition: Quadtree-Impl.h:30
const std::vector< size_t > & GetItemsAtNode(size_t nodeIdx) const
Returns the list of the items for given noide index.
Definition: Quadtree-Impl.h:229
bool IsIntersects(const BoundingBox2D &box, const BoxIntersectionTestFunc2< T > &testFunc) const override
Returns true if given box intersects with any of the stored items.
Definition: Quadtree-Impl.h:145
void Clear()
Clears all the contents of this instance.
Definition: Quadtree-Impl.h:52
2-D axis-aligned bounding box class.
Definition: BoundingBox2.h:44
Definition: pybind11Utils.h:24
Generic quadtree data structure.
Definition: Quadtree.h:27
void ForEachIntersectingItem(const BoundingBox2D &box, const BoxIntersectionTestFunc2< T > &testFunc, const IntersectionVisitorFunc2< T > &visitorFunc) const override
Invokes visitorFunc for every intersecting items.
Definition: Quadtree-Impl.h:160
std::function< void(const T &)> IntersectionVisitorFunc2
Visitor function which is invoked for each intersecting item.
Definition: IntersectionQueryEngine2.h:43
Iterator begin()
Returns the begin iterator of the item.
Definition: Quadtree-Impl.h:187
size_t GetNumberOfItems() const
Returns the number of items.
Definition: Quadtree-Impl.h:211
ClosestIntersectionQueryResult2< T > GetClosestIntersection(const Ray2D &ray, const GetRayIntersectionFunc2< T > &testFunc) const override
Returns the closest intersection for given ray.
Definition: Quadtree-Impl.h:176
Iterator end()
Returns the end iterator of the item.
Definition: Quadtree-Impl.h:193
const BoundingBox2D & GetBoundingBox() const
Returns the bounding box of this quadtree.
Definition: Quadtree-Impl.h:241
2-D vector class.
Definition: Vector2.h:26
Abstract base class for 2-D nearest neighbor query engine.
Definition: NearestNeighborQueryEngine2.h:30
const T & GetItem(size_t i) const
Returns the item at i.
Definition: Quadtree-Impl.h:217
std::function< double(const T &, const Ray2D &)> GetRayIntersectionFunc2
Ray-item closest intersection evaluation function.
Definition: IntersectionQueryEngine2.h:39
typename ContainerType::const_iterator ConstIterator
Definition: Quadtree.h:32
std::function< bool(const T &, const Ray2D &)> RayIntersectionTestFunc2
Ray-item intersection test function.
Definition: IntersectionQueryEngine2.h:35
std::function< double(const T &, const Vector2D &)> NearestNeighborDistanceFunc2
Nearest neighbor distance measure function.
Definition: NearestNeighborQueryEngine2.h:26
typename ContainerType::iterator Iterator
Definition: Quadtree.h:31