Loading...
Searching...
No Matches
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>
18
19namespace CubbyFlow
20{
29template <typename T, size_t N>
30class BVH final : public IntersectionQueryEngine<T, N>,
32{
33 public:
37
39 void Build(const ConstArrayView1<T>& items,
41
43 void Clear();
44
48 const Vector<double, N>& pt,
50
52 [[nodiscard]] bool Intersects(
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
65 const IntersectionVisitorFunc<T>& visitorFunc) const override;
66
69 const Ray<double, N>& ray,
71 const IntersectionVisitorFunc<T>& visitorFunc) const override;
72
75 const Ray<double, N>& ray,
76 const GetRayIntersectionFunc<T, N>& testFunc) const override;
77
80
83
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 };
134 BoundingBox<double, N> bound;
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
150template <typename T>
152
154template <typename T>
156} // namespace CubbyFlow
157
159
160#endif
Generic N-dimensional array class interface.
Definition ArrayView.hpp:26
Bounding Volume Hierarchy (BVH) in N-D.
Definition BVH.hpp:32
const BoundingBox< double, N > & GetBoundingBox() const
Returns bounding box of every items.
Definition BVH-Impl.hpp:560
size_t NumberOfItems() const
Returns the number of items.
Definition BVH-Impl.hpp:590
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
void Clear()
Clears all the contents of this instance.
Definition BVH-Impl.hpp:75
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
size_t NumberOfNodes() const
Returns the number of nodes.
Definition BVH-Impl.hpp:602
std::pair< size_t, size_t > Children(size_t i) const
Returns the children indices of i-th node.
Definition BVH-Impl.hpp:608
bool IsLeaf(size_t i) const
Returns true if i-th node is a leaf node.
Definition BVH-Impl.hpp:620
const T & Item(size_t i) const
Returns the item at i.
Definition BVH-Impl.hpp:596
Iterator ItemOfNode(size_t i)
Returns item of i-th node.
Definition BVH-Impl.hpp:632
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
Iterator begin()
Returns the begin Iterator of the item.
Definition BVH-Impl.hpp:566
Iterator end()
Returns the end Iterator of the item.
Definition BVH-Impl.hpp:572
void Build(const ConstArrayView1< T > &items, const ConstArrayView1< BoundingBox< double, N > > &itemsBounds)
Builds bounding volume hierarchy.
Definition BVH-Impl.hpp:48
NearestNeighborQueryResult< T, N > Nearest(const Vector< double, N > &pt, const NearestNeighborDistanceFunc< T, N > &distanceFunc) const override
Definition BVH-Impl.hpp:84
Array1< T > ContainerType
Definition BVH.hpp:34
const BoundingBox< double, N > & NodeBound(size_t i) const
Returns bounding box of i-th node.
Definition BVH-Impl.hpp:626
Abstract base class for N-D intersection test query engine.
Definition IntersectionQueryEngine.hpp:98
Definition Matrix.hpp:30
ConstPointer ConstIterator
Definition Matrix.hpp:45
Pointer Iterator
Definition Matrix.hpp:44
Abstract base class for N-D nearest neighbor query engine.
Definition NearestNeighborQueryEngine.hpp:53
Definition pybind11Utils.hpp:21
Matrix< T, Rows, 1 > Vector
Definition Matrix.hpp:738