BVH.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_BVH_HPP
12 #define CUBBYFLOW_BVH_HPP
13 
14 #include <Core/Array/Array.hpp>
15 #include <Core/Array/ArrayView.hpp>
18 
19 namespace CubbyFlow
20 {
29 template <typename T, size_t N>
30 class BVH final : public IntersectionQueryEngine<T, N>,
31  public NearestNeighborQueryEngine<T, N>
32 {
33  public:
37 
39  void Build(const ConstArrayView1<T>& items,
40  const ConstArrayView1<BoundingBox<double, N>>& itemsBounds);
41 
43  void Clear();
44 
48  const Vector<double, N>& pt,
49  const NearestNeighborDistanceFunc<T, N>& distanceFunc) const override;
50 
52  [[nodiscard]] bool Intersects(
53  const BoundingBox<double, N>& box,
54  const BoxIntersectionTestFunc<T, N>& testFunc) const override;
55 
57  [[nodiscard]] bool Intersects(
58  const Ray<double, N>& ray,
59  const RayIntersectionTestFunc<T, N>& testFunc) const override;
60 
63  const BoundingBox<double, N>& box,
64  const BoxIntersectionTestFunc<T, N>& testFunc,
65  const IntersectionVisitorFunc<T>& visitorFunc) const override;
66 
69  const Ray<double, N>& ray,
70  const RayIntersectionTestFunc<T, N>& testFunc,
71  const IntersectionVisitorFunc<T>& visitorFunc) const override;
72 
75  const Ray<double, N>& ray,
76  const GetRayIntersectionFunc<T, N>& testFunc) const override;
77 
79  [[nodiscard]] const BoundingBox<double, N>& GetBoundingBox() const;
80 
82  [[nodiscard]] Iterator begin();
83 
85  [[nodiscard]] Iterator end();
86 
88  [[nodiscard]] ConstIterator begin() const;
89 
91  [[nodiscard]] ConstIterator end() const;
92 
94  [[nodiscard]] size_t NumberOfItems() const;
95 
97  [[nodiscard]] const T& Item(size_t i) const;
98 
100  [[nodiscard]] size_t NumberOfNodes() const;
101 
103  [[nodiscard]] std::pair<size_t, size_t> Children(size_t i) const;
104 
106  [[nodiscard]] bool IsLeaf(size_t i) const;
107 
109  [[nodiscard]] const BoundingBox<double, N>& NodeBound(size_t i) const;
110 
112  [[nodiscard]] Iterator ItemOfNode(size_t i);
113 
115  [[nodiscard]] ConstIterator ItemOfNode(size_t i) const;
116 
117  private:
118  struct Node
119  {
120  Node();
121 
122  void InitLeaf(size_t it, const BoundingBox<double, N>& b);
123  void InitInternal(uint8_t axis, size_t c,
124  const BoundingBox<double, N>& b);
125 
126  [[nodiscard]] bool IsLeaf() const;
127 
128  char flags;
129  union
130  {
131  size_t child;
132  size_t item{};
133  };
135  };
136 
137  size_t Build(size_t nodeIndex, size_t* itemIndices, size_t nItems,
138  size_t currentDepth);
139 
140  [[nodiscard]] size_t QSplit(size_t* itemIndices, size_t numItems,
141  double pivot, uint8_t axis);
142 
143  BoundingBox<double, N> m_bound;
144  ContainerType m_items;
145  Array1<BoundingBox<double, N>> m_itemBounds;
146  Array1<Node> m_nodes;
147 };
148 
150 template <typename T>
151 using BVH2 = BVH<T, 2>;
152 
154 template <typename T>
155 using BVH3 = BVH<T, 3>;
156 } // namespace CubbyFlow
157 
159 
160 #endif
N-D nearest neighbor query result.
Definition: NearestNeighborQueryEngine.hpp:23
Class for N-D ray.
Definition: Ray.hpp:25
bool Intersects(const BoundingBox< double, N > &box, const BoxIntersectionTestFunc< T, N > &testFunc) const override
Returns true if given box intersects with any of the stored items.
Definition: BVH-Impl.hpp:191
const BoundingBox< double, N > & GetBoundingBox() const
Returns bounding box of every items.
Definition: BVH-Impl.hpp:560
Iterator ItemOfNode(size_t i)
Returns item of i-th node.
Definition: BVH-Impl.hpp:632
size_t NumberOfItems() const
Returns the number of items.
Definition: BVH-Impl.hpp:590
void ForEachIntersectingItem(const BoundingBox< double, N > &box, const BoxIntersectionTestFunc< T, N > &testFunc, const IntersectionVisitorFunc< T > &visitorFunc) const override
Invokes visitorFunc for every intersecting items.
Definition: BVH-Impl.hpp:334
Definition: Matrix.hpp:27
N-D closest intersection query result.
Definition: IntersectionQueryEngine.hpp:25
std::pair< size_t, size_t > Children(size_t i) const
Returns the children indices of i-th node.
Definition: BVH-Impl.hpp:608
Definition: pybind11Utils.hpp:20
Definition: Array-Impl.hpp:19
const T & Item(size_t i) const
Returns the item at i.
Definition: BVH-Impl.hpp:596
Iterator begin()
Returns the begin Iterator of the item.
Definition: BVH-Impl.hpp:566
std::function< bool(const T &, const BoundingBox< double, N > &)> BoxIntersectionTestFunc
N-D box-item intersection test function.
Definition: IntersectionQueryEngine.hpp:55
Iterator end()
Returns the end Iterator of the item.
Definition: BVH-Impl.hpp:572
ClosestIntersectionQueryResult< T, N > ClosestIntersection(const Ray< double, N > &ray, const GetRayIntersectionFunc< T, N > &testFunc) const override
Returns the closest intersection for given ray.
Definition: BVH-Impl.hpp:476
typename ContainerType::Iterator Iterator
Definition: BVH.hpp:35
Abstract base class for N-D intersection test query engine.
Definition: IntersectionQueryEngine.hpp:97
Generic N-dimensional array class interface.
Definition: Array.hpp:32
Abstract base class for N-D nearest neighbor query engine.
Definition: NearestNeighborQueryEngine.hpp:52
size_t NumberOfNodes() const
Returns the number of nodes.
Definition: BVH-Impl.hpp:602
typename ContainerType::ConstIterator ConstIterator
Definition: BVH.hpp:36
const T * ConstIterator
Definition: ArrayBase.hpp:29
std::function< double(const T &, const Vector< double, N > &)> NearestNeighborDistanceFunc
N-D nearest neighbor distance measure function.
Definition: NearestNeighborQueryEngine.hpp:40
std::function< bool(const T &, const Ray< double, N > &)> RayIntersectionTestFunc
N-D ray-item intersection test function.
Definition: IntersectionQueryEngine.hpp:68
Bounding Volume Hierarchy (BVH) in N-D.
Definition: BVH.hpp:30
std::function< double(const T &, const Ray< double, N > &)> GetRayIntersectionFunc
N-D ray-item closest intersection evaluation function.
Definition: IntersectionQueryEngine.hpp:81
void Build(const ConstArrayView1< T > &items, const ConstArrayView1< BoundingBox< double, N >> &itemsBounds)
Builds bounding volume hierarchy.
Definition: BVH-Impl.hpp:48
void Clear()
Clears all the contents of this instance.
Definition: BVH-Impl.hpp:75
std::function< void(const T &)> IntersectionVisitorFunc
Visitor function which is invoked for each intersecting item.
Definition: IntersectionQueryEngine.hpp:93
const BoundingBox< double, N > & NodeBound(size_t i) const
Returns bounding box of i-th node.
Definition: BVH-Impl.hpp:626
T * Iterator
Definition: ArrayBase.hpp:28
bool IsLeaf(size_t i) const
Returns true if i-th node is a leaf node.
Definition: BVH-Impl.hpp:620
NearestNeighborQueryResult< T, N > Nearest(const Vector< double, N > &pt, const NearestNeighborDistanceFunc< T, N > &distanceFunc) const override
Definition: BVH-Impl.hpp:84