Loading...
Searching...
No Matches
Octree.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_OCTREE_HPP
12#define CUBBYFLOW_OCTREE_HPP
13
16
17namespace CubbyFlow
18{
28template <typename T>
31{
32 public:
33 using ContainerType = std::vector<T>;
34 using Iterator = typename ContainerType::iterator;
35 using ConstIterator = typename ContainerType::const_iterator;
36
38 Octree() = default;
39
41 Octree(const Octree&) = default;
42
45
48
51
54
57 void Build(const std::vector<T>& items, const BoundingBox3D& bound,
59
61 void Clear();
62
68
70 [[nodiscard]] bool Intersects(
73
75 [[nodiscard]] bool Intersects(
78
83
88
93
96
99
102
105
107 [[nodiscard]] size_t GetNumberOfItems() const;
108
110 [[nodiscard]] const T& GetItem(size_t i) const;
111
113 [[nodiscard]] size_t GetNumberOfNodes() const;
114
117 size_t nodeIdx) const;
118
131 [[nodiscard]] size_t GetChildIndex(size_t nodeIdx, size_t childIdx) const;
132
135
137 [[nodiscard]] size_t GetMaxDepth() const;
138
139 private:
140 struct Node
141 {
142 [[nodiscard]] bool IsLeaf() const;
143
144 size_t firstChild = std::numeric_limits<size_t>::max();
145 std::vector<size_t> items;
146 };
147
148 void Build(size_t nodeIdx, size_t Depth, const BoundingBox3D& Bound,
150
151 bool Intersects(const BoundingBox3D& box,
153 size_t nodeIdx, const BoundingBox3D& Bound) const;
154
155 [[nodiscard]] bool Intersects(const Ray3D& ray,
157 size_t nodeIdx,
158 const BoundingBox3D& Bound) const;
159
163 size_t nodeIdx,
164 const BoundingBox3D& Bound) const;
165
169 size_t nodeIdx,
170 const BoundingBox3D& Bound) const;
171
174 size_t nodeIdx, const BoundingBox3D& Bound,
176
177 size_t m_maxDepth = 1;
178 BoundingBox3D m_bbox;
179 std::vector<T> m_items;
180 std::vector<Node> m_nodes;
181};
182} // namespace CubbyFlow
183
185
186#endif
N-D axis-aligned bounding box class.
Definition BoundingBox.hpp:47
Abstract base class for N-D intersection test query engine.
Definition IntersectionQueryEngine.hpp:98
Definition Matrix.hpp:30
Abstract base class for N-D nearest neighbor query engine.
Definition NearestNeighborQueryEngine.hpp:53
Generic octree data structure.
Definition Octree.hpp:31
NearestNeighborQueryResult3< T > Nearest(const Vector3D &pt, const NearestNeighborDistanceFunc3< T > &distanceFunc) const override
Definition Octree-Impl.hpp:60
void Build(const std::vector< T > &items, const BoundingBox3D &bound, const BoxIntersectionTestFunc3< T > &testFunc, size_t maxDepth)
Definition Octree-Impl.hpp:26
const T & GetItem(size_t i) const
Returns the item at i.
Definition Octree-Impl.hpp:221
std::vector< T > ContainerType
Definition Octree.hpp:33
Octree(const Octree &)=default
Default copy constructor.
void Clear()
Clears all the contents of this instance.
Definition Octree-Impl.hpp:51
typename ContainerType::iterator Iterator
Definition Octree.hpp:34
typename ContainerType::const_iterator ConstIterator
Definition Octree.hpp:35
size_t GetMaxDepth() const
Returns the maximum depth of the tree.
Definition Octree-Impl.hpp:251
ClosestIntersectionQueryResult3< T > ClosestIntersection(const Ray3D &ray, const GetRayIntersectionFunc3< T > &testFunc) const override
Returns the closest intersection for given ray.
Definition Octree-Impl.hpp:180
Octree()=default
Default constructor.
size_t GetChildIndex(size_t nodeIdx, size_t childIdx) const
Returns a child's index for given node.
Definition Octree-Impl.hpp:239
Iterator begin()
Returns the begin iterator of the item.
Definition Octree-Impl.hpp:191
const BoundingBox3D & GetBoundingBox() const
Returns the bounding box of this octree.
Definition Octree-Impl.hpp:245
size_t GetNumberOfNodes() const
Returns the number of octree nodes.
Definition Octree-Impl.hpp:227
size_t GetNumberOfItems() const
Returns the number of items.
Definition Octree-Impl.hpp:215
bool Intersects(const BoundingBox3D &box, const BoxIntersectionTestFunc3< T > &testFunc) const override
Returns true if given box intersects with any of the stored items.
Definition Octree-Impl.hpp:150
const std::vector< size_t > & GetItemsAtNode(size_t nodeIdx) const
Returns the list of the items for given node index.
Definition Octree-Impl.hpp:233
Octree(Octree &&) noexcept=default
Default move constructor.
void ForEachIntersectingItem(const BoundingBox3D &box, const BoxIntersectionTestFunc3< T > &testFunc, const IntersectionVisitorFunc< T > &visitorFunc) const override
Invokes visitorFunc for every intersecting items.
Definition Octree-Impl.hpp:164
Iterator end()
Returns the end iterator of the item.
Definition Octree-Impl.hpp:197
Class for N-D ray.
Definition Ray.hpp:26
Definition pybind11Utils.hpp:21
RayIntersectionTestFunc< T, 3 > RayIntersectionTestFunc3
3-D ray-item intersection test function.
Definition IntersectionQueryEngine.hpp:76
NearestNeighborDistanceFunc< T, 3 > NearestNeighborDistanceFunc3
3-D nearest neighbor distance measure function.
Definition NearestNeighborQueryEngine.hpp:48
GetRayIntersectionFunc< T, 3 > GetRayIntersectionFunc3
3-D ray-item closest intersection evaluation function.
Definition IntersectionQueryEngine.hpp:89
std::function< void(const T &)> IntersectionVisitorFunc
Visitor function which is invoked for each intersecting item.
Definition IntersectionQueryEngine.hpp:93
Matrix< T, Rows, 1 > Vector
Definition Matrix.hpp:738
BoxIntersectionTestFunc< T, 3 > BoxIntersectionTestFunc3
3-D box-item intersection test function.
Definition IntersectionQueryEngine.hpp:63
N-D closest intersection query result.
Definition IntersectionQueryEngine.hpp:26