Jet  v1.3.3
quadtree.h
Go to the documentation of this file.
1 // Copyright (c) 2018 Doyub Kim
2 //
3 // I am making my contributions/submissions to this project solely in my
4 // personal capacity and am not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef INCLUDE_JET_QUADTREE_H_
8 #define INCLUDE_JET_QUADTREE_H_
9 
12 #include <vector>
13 
14 namespace jet {
15 
25 template <typename T>
26 class Quadtree final : public IntersectionQueryEngine2<T>,
27  public NearestNeighborQueryEngine2<T> {
28  public:
29  typedef std::vector<T> ContainerType;
30  typedef typename ContainerType::iterator Iterator;
31  typedef typename ContainerType::const_iterator ConstIterator;
32 
35 
38  void build(const std::vector<T>& items, const BoundingBox2D& bound,
39  const BoxIntersectionTestFunc2<T>& testFunc, size_t maxDepth);
40 
42  void clear();
43 
47  const Vector2D& pt,
48  const NearestNeighborDistanceFunc2<T>& distanceFunc) const override;
49 
51  bool intersects(const BoundingBox2D& box,
52  const BoxIntersectionTestFunc2<T>& testFunc) const override;
53 
55  bool intersects(const Ray2D& ray,
56  const RayIntersectionTestFunc2<T>& testFunc) const override;
57 
60  const BoundingBox2D& box, const BoxIntersectionTestFunc2<T>& testFunc,
61  const IntersectionVisitorFunc2<T>& visitorFunc) const override;
62 
65  const Ray2D& ray, const RayIntersectionTestFunc2<T>& testFunc,
66  const IntersectionVisitorFunc2<T>& visitorFunc) const override;
67 
70  const Ray2D& ray,
71  const GetRayIntersectionFunc2<T>& testFunc) const override;
72 
75 
78 
81 
83  ConstIterator end() const;
84 
86  size_t numberOfItems() const;
87 
89  const T& item(size_t i) const;
90 
92  size_t numberOfNodes() const;
93 
95  const std::vector<size_t>& itemsAtNode(size_t nodeIdx) const;
96 
109  size_t childIndex(size_t nodeIdx, size_t childIdx) const;
110 
112  const BoundingBox2D& boundingBox() const;
113 
115  size_t maxDepth() const;
116 
117  private:
118  struct Node {
119  size_t firstChild = kMaxSize;
120  std::vector<size_t> items;
121 
122  bool isLeaf() const;
123  };
124 
125  size_t _maxDepth = 1;
126  BoundingBox2D _bbox;
127  std::vector<T> _items;
128  std::vector<Node> _nodes;
129 
130  void build(size_t nodeIdx, size_t currentDepth,
131  const BoundingBox2D& currentBound,
132  const BoxIntersectionTestFunc2<T>& overlapsFunc);
133 
134  bool intersects(const BoundingBox2D& box,
135  const BoxIntersectionTestFunc2<T>& testFunc, size_t nodeIdx,
136  const BoundingBox2D& currentBound) const;
137 
138  bool intersects(const Ray2D& ray,
139  const RayIntersectionTestFunc2<T>& testFunc, size_t nodeIdx,
140  const BoundingBox2D& currentBound) const;
141 
142  void forEachIntersectingItem(const BoundingBox2D& box,
143  const BoxIntersectionTestFunc2<T>& testFunc,
144  const IntersectionVisitorFunc2<T>& visitorFunc,
145  size_t nodeIdx,
146  const BoundingBox2D& currentBound) const;
147 
148  void forEachIntersectingItem(const Ray2D& ray,
149  const RayIntersectionTestFunc2<T>& testFunc,
150  const IntersectionVisitorFunc2<T>& visitorFunc,
151  size_t nodeIdx,
152  const BoundingBox2D& currentBound) const;
153 
155  const Ray2D& ray, const GetRayIntersectionFunc2<T>& testFunc,
156  size_t nodeIdx, const BoundingBox2D& currentBound,
158 };
159 
160 } // namespace jet
161 
162 #include "detail/quadtree-inl.h"
163 
164 #endif // INCLUDE_JET_QUADTREE_H_
jet::Quadtree::nearest
NearestNeighborQueryResult2< T > nearest(const Vector2D &pt, const NearestNeighborDistanceFunc2< T > &distanceFunc) const override
jet::IntersectionQueryEngine2
Abstract base class for 2-D intersection test query engine.
Definition: intersection_query_engine2.h:48
nearest_neighbor_query_engine2.h
jet::Quadtree::childIndex
size_t childIndex(size_t nodeIdx, size_t childIdx) const
Returns a child's index for given node.
jet::Ray< T, 2 >
Class for 2-D ray.
Definition: ray2.h:21
jet::NearestNeighborQueryResult2
Nearest neighbor query result.
Definition: nearest_neighbor_query_engine2.h:19
jet::Quadtree::item
const T & item(size_t i) const
Returns the item at i.
jet::Quadtree::intersects
bool intersects(const BoundingBox2D &box, const BoxIntersectionTestFunc2< T > &testFunc) const override
Returns true if given box intersects with any of the stored items.
jet::Quadtree::intersects
bool intersects(const Ray2D &ray, const RayIntersectionTestFunc2< T > &testFunc) const override
Returns true if given ray intersects with any of the stored items.
jet::kMaxSize
constexpr size_t kMaxSize
Max size_t.
Definition: constants.h:79
jet::Quadtree::end
ConstIterator end() const
Returns the immutable end iterator of the item.
jet
Definition: advection_solver2.h:18
jet::BoxIntersectionTestFunc2
std::function< bool(const T &, const BoundingBox2D &)> BoxIntersectionTestFunc2
Box-item intersection test function.
Definition: intersection_query_engine2.h:32
jet::Quadtree::begin
ConstIterator begin() const
Returns the immutable begin iterator of the item.
intersection_query_engine2.h
jet::Quadtree::forEachIntersectingItem
void forEachIntersectingItem(const Ray2D &ray, const RayIntersectionTestFunc2< T > &testFunc, const IntersectionVisitorFunc2< T > &visitorFunc) const override
Invokes visitorFunc for every intersecting items.
jet::Quadtree::forEachIntersectingItem
void forEachIntersectingItem(const BoundingBox2D &box, const BoxIntersectionTestFunc2< T > &testFunc, const IntersectionVisitorFunc2< T > &visitorFunc) const override
Invokes visitorFunc for every intersecting items.
jet::Vector< T, 2 >
2-D vector class.
Definition: vector2.h:24
jet::NearestNeighborQueryEngine2
Abstract base class for 2-D nearest neigbor query engine.
Definition: nearest_neighbor_query_engine2.h:31
jet::Quadtree::ContainerType
std::vector< T > ContainerType
Definition: quadtree.h:29
jet::GetRayIntersectionFunc2
std::function< double(const T &, const Ray2D &)> GetRayIntersectionFunc2
Ray-item closest intersection evaluation function.
Definition: intersection_query_engine2.h:40
jet::Quadtree::clear
void clear()
Clears all the contents of this instance.
jet::Quadtree::ConstIterator
ContainerType::const_iterator ConstIterator
Definition: quadtree.h:31
jet::NearestNeighborDistanceFunc2
std::function< double(const T &, const Vector2D &)> NearestNeighborDistanceFunc2
Nearest neighbor distance measure function.
Definition: nearest_neighbor_query_engine2.h:27
jet::IntersectionVisitorFunc2
std::function< void(const T &)> IntersectionVisitorFunc2
Visitor function which is invoked for each intersecting item.
Definition: intersection_query_engine2.h:44
jet::BoundingBox< T, 2 >
2-D axis-aligned bounding box class.
Definition: bounding_box2.h:41
jet::Quadtree::closestIntersection
ClosestIntersectionQueryResult2< T > closestIntersection(const Ray2D &ray, const GetRayIntersectionFunc2< T > &testFunc) const override
Returns the closest intersection for given ray.
jet::Quadtree::Iterator
ContainerType::iterator Iterator
Definition: quadtree.h:30
jet::Quadtree::maxDepth
size_t maxDepth() const
Returns the maximum depth of the tree.
jet::Quadtree::end
Iterator end()
Returns the end iterator of the item.
jet::Quadtree::build
void build(const std::vector< T > &items, const BoundingBox2D &bound, const BoxIntersectionTestFunc2< T > &testFunc, size_t maxDepth)
jet::Quadtree::numberOfNodes
size_t numberOfNodes() const
Returns the number of quadtree nodes.
jet::Quadtree
Generic quadtree data structure.
Definition: quadtree.h:27
jet::Quadtree::numberOfItems
size_t numberOfItems() const
Returns the number of items.
jet::Quadtree::Quadtree
Quadtree()
Default constructor.
jet::Quadtree::begin
Iterator begin()
Returns the begin iterator of the item.
jet::Quadtree::boundingBox
const BoundingBox2D & boundingBox() const
Returns the bounding box of this quadtree.
jet::ClosestIntersectionQueryResult2
Closest intersection query result.
Definition: intersection_query_engine2.h:19
jet::RayIntersectionTestFunc2
std::function< bool(const T &, const Ray2D &)> RayIntersectionTestFunc2
Ray-item intersection test function.
Definition: intersection_query_engine2.h:36
jet::Quadtree::itemsAtNode
const std::vector< size_t > & itemsAtNode(size_t nodeIdx) const
Returns the list of the items for given noide index.