BVH2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: BVH2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Bounding Volume Hierarchy (BVH) in 2D.
6 > Created Time: 2017/10/17
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_BVH2_H
10 #define CUBBYFLOW_BVH2_H
11 
14 
15 namespace CubbyFlow
16 {
25  template <typename T>
26  class BVH2 final : public IntersectionQueryEngine2<T>, public NearestNeighborQueryEngine2<T>
27  {
28  public:
29  using ContainerType = std::vector<T>;
30  using Iterator = typename ContainerType::iterator;
31  using ConstIterator = typename ContainerType::const_iterator;
32 
34  BVH2();
35 
37  void Build(const std::vector<T>& items,
38  const std::vector<BoundingBox2D>& itemsBounds);
39 
41  void Clear();
42 
46  const Vector2D& pt,
47  const NearestNeighborDistanceFunc2<T>& distanceFunc) const override;
48 
50  bool IsIntersects(const BoundingBox2D& box,
51  const BoxIntersectionTestFunc2<T>& testFunc) const override;
52 
54  bool IsIntersects(const Ray2D& ray,
55  const RayIntersectionTestFunc2<T>& testFunc) const override;
56 
59  const BoundingBox2D& box, const BoxIntersectionTestFunc2<T>& testFunc,
60  const IntersectionVisitorFunc2<T>& visitorFunc) const override;
61 
64  const Ray2D& ray, const RayIntersectionTestFunc2<T>& testFunc,
65  const IntersectionVisitorFunc2<T>& visitorFunc) const override;
66 
69  const Ray2D& ray,
70  const GetRayIntersectionFunc2<T>& testFunc) const override;
71 
73  const BoundingBox2D& GetBoundingBox() const;
74 
76  Iterator begin();
77 
79  Iterator end();
80 
82  ConstIterator begin() const;
83 
85  ConstIterator end() const;
86 
88  size_t GetNumberOfItems() const;
89 
91  const T& GetItem(size_t i) const;
92 
93  private:
94  struct Node
95  {
96  char flags;
97  union
98  {
99  size_t child;
100  size_t item;
101  };
102  BoundingBox2D bound;
103 
104  Node();
105  void InitLeaf(size_t it, const BoundingBox2D& b);
106  void InitInternal(uint8_t axis, size_t c, const BoundingBox2D& b);
107  bool IsLeaf() const;
108  };
109 
110  BoundingBox2D m_bound;
111  ContainerType m_items;
112  std::vector<BoundingBox2D> m_itemBounds;
113  std::vector<Node> m_nodes;
114 
115  size_t Build(size_t nodeIndex, size_t* itemIndices, size_t nItems, size_t currentDepth);
116 
117  size_t QSplit(size_t* itemIndices, size_t numItems, double pivot, uint8_t axis);
118  };
119 }
120 
121 #include <Core/Geometry/BVH2-Impl.h>
122 
123 #endif
Bounding Volume Hierarchy (BVH) in 2D.
Definition: BVH2.h:26
Iterator end()
Returns the end iterator of the item.
Definition: BVH2-Impl.h:571
Abstract base class for 2-D intersection test query engine.
Definition: IntersectionQueryEngine2.h:47
Closest intersection query result.
Definition: IntersectionQueryEngine2.h:19
Class for 2-D ray.
Definition: Ray2.h:23
void Clear()
Clears all the contents of this instance.
Definition: BVH2-Impl.h:76
BoundingBox2< double > BoundingBox2D
Double-type 2-D BoundingBox.
Definition: BoundingBox2.h:126
std::function< bool(const T &, const BoundingBox2D &)> BoxIntersectionTestFunc2
Box-item intersection test function.
Definition: IntersectionQueryEngine2.h:31
Nearest neighbor query result.
Definition: NearestNeighborQueryEngine2.h:18
typename ContainerType::const_iterator ConstIterator
Definition: BVH2.h:31
const T & GetItem(size_t i) const
Returns the item at i.
Definition: BVH2-Impl.h:595
void ForEachIntersectingItem(const BoundingBox2D &box, const BoxIntersectionTestFunc2< T > &testFunc, const IntersectionVisitorFunc2< T > &visitorFunc) const override
Invokes visitorFunc for every intersecting items.
Definition: BVH2-Impl.h:335
2-D axis-aligned bounding box class.
Definition: BoundingBox2.h:44
Definition: pybind11Utils.h:24
std::function< void(const T &)> IntersectionVisitorFunc2
Visitor function which is invoked for each intersecting item.
Definition: IntersectionQueryEngine2.h:43
BVH2()
Default constructor.
Definition: BVH2-Impl.h:45
NearestNeighborQueryResult2< T > GetNearestNeighbor(const Vector2D &pt, const NearestNeighborDistanceFunc2< T > &distanceFunc) const override
Definition: BVH2-Impl.h:85
std::vector< Surface2Ptr > ContainerType
Definition: BVH2.h:29
bool IsIntersects(const BoundingBox2D &box, const BoxIntersectionTestFunc2< T > &testFunc) const override
Returns true if given box intersects with any of the stored items.
Definition: BVH2-Impl.h:192
Iterator begin()
Returns the begin iterator of the item.
Definition: BVH2-Impl.h:565
void Build(const std::vector< T > &items, const std::vector< BoundingBox2D > &itemsBounds)
Builds bounding volume hierarchy.
Definition: BVH2-Impl.h:51
2-D vector class.
Definition: Vector2.h:26
Abstract base class for 2-D nearest neighbor query engine.
Definition: NearestNeighborQueryEngine2.h:30
ClosestIntersectionQueryResult2< T > GetClosestIntersection(const Ray2D &ray, const GetRayIntersectionFunc2< T > &testFunc) const override
Returns the closest intersection for given ray.
Definition: BVH2-Impl.h:476
const BoundingBox2D & GetBoundingBox() const
Returns bounding box of every items.
Definition: BVH2-Impl.h:559
std::function< double(const T &, const Ray2D &)> GetRayIntersectionFunc2
Ray-item closest intersection evaluation function.
Definition: IntersectionQueryEngine2.h:39
typename ContainerType::iterator Iterator
Definition: BVH2.h:30
size_t GetNumberOfItems() const
Returns the number of items.
Definition: BVH2-Impl.h:589
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