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 
17 namespace CubbyFlow
18 {
28 template <typename T>
29 class Octree final : public IntersectionQueryEngine3<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 
44  Octree(Octree&&) noexcept = default;
45 
47  virtual ~Octree() = default;
48 
50  Octree& operator=(const Octree&) = default;
51 
53  Octree& operator=(Octree&&) noexcept = default;
54 
57  void Build(const std::vector<T>& items, const BoundingBox3D& bound,
58  const BoxIntersectionTestFunc3<T>& testFunc, size_t maxDepth);
59 
61  void Clear();
62 
65  [[nodiscard]] NearestNeighborQueryResult3<T> Nearest(
66  const Vector3D& pt,
67  const NearestNeighborDistanceFunc3<T>& distanceFunc) const override;
68 
70  [[nodiscard]] bool Intersects(
71  const BoundingBox3D& box,
72  const BoxIntersectionTestFunc3<T>& testFunc) const override;
73 
75  [[nodiscard]] bool Intersects(
76  const Ray3D& ray,
77  const RayIntersectionTestFunc3<T>& testFunc) const override;
78 
81  const BoundingBox3D& box, const BoxIntersectionTestFunc3<T>& testFunc,
82  const IntersectionVisitorFunc<T>& visitorFunc) const override;
83 
86  const Ray3D& ray, const RayIntersectionTestFunc3<T>& testFunc,
87  const IntersectionVisitorFunc<T>& visitorFunc) const override;
88 
91  const Ray3D& ray,
92  const GetRayIntersectionFunc3<T>& testFunc) const override;
93 
95  [[nodiscard]] Iterator begin();
96 
98  [[nodiscard]] Iterator end();
99 
101  [[nodiscard]] ConstIterator begin() const;
102 
104  [[nodiscard]] ConstIterator end() const;
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 
116  [[nodiscard]] const std::vector<size_t>& GetItemsAtNode(
117  size_t nodeIdx) const;
118 
131  [[nodiscard]] size_t GetChildIndex(size_t nodeIdx, size_t childIdx) const;
132 
134  [[nodiscard]] const BoundingBox3D& GetBoundingBox() const;
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,
149  const BoxIntersectionTestFunc3<T>& testFunc);
150 
151  bool Intersects(const BoundingBox3D& box,
152  const BoxIntersectionTestFunc3<T>& testFunc,
153  size_t nodeIdx, const BoundingBox3D& Bound) const;
154 
155  [[nodiscard]] bool Intersects(const Ray3D& ray,
156  const RayIntersectionTestFunc3<T>& testFunc,
157  size_t nodeIdx,
158  const BoundingBox3D& Bound) const;
159 
160  void ForEachIntersectingItem(const BoundingBox3D& box,
161  const BoxIntersectionTestFunc3<T>& testFunc,
162  const IntersectionVisitorFunc<T>& visitorFunc,
163  size_t nodeIdx,
164  const BoundingBox3D& Bound) const;
165 
166  void ForEachIntersectingItem(const Ray3D& ray,
167  const RayIntersectionTestFunc3<T>& testFunc,
168  const IntersectionVisitorFunc<T>& visitorFunc,
169  size_t nodeIdx,
170  const BoundingBox3D& Bound) const;
171 
173  const Ray3D& ray, const GetRayIntersectionFunc3<T>& testFunc,
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
void Clear()
Clears all the contents of this instance.
Definition: Octree-Impl.hpp:51
Generic octree data structure.
Definition: Octree.hpp:29
const BoundingBox3D & GetBoundingBox() const
Returns the bounding box of this octree.
Definition: Octree-Impl.hpp:245
NearestNeighborQueryResult3< T > Nearest(const Vector3D &pt, const NearestNeighborDistanceFunc3< T > &distanceFunc) const override
Definition: Octree-Impl.hpp:60
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
size_t GetMaxDepth() const
Returns the maximum depth of the tree.
Definition: Octree-Impl.hpp:251
N-D nearest neighbor query result.
Definition: NearestNeighborQueryEngine.hpp:23
typename ContainerType::iterator Iterator
Definition: Octree.hpp:34
Class for N-D ray.
Definition: Ray.hpp:25
ClosestIntersectionQueryResult3< T > ClosestIntersection(const Ray3D &ray, const GetRayIntersectionFunc3< T > &testFunc) const override
Returns the closest intersection for given ray.
Definition: Octree-Impl.hpp:180
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
N-D axis-aligned bounding box class.
Definition: BoundingBox.hpp:46
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()=default
Default constructor.
const T & GetItem(size_t i) const
Returns the item at i.
Definition: Octree-Impl.hpp:221
typename ContainerType::const_iterator ConstIterator
Definition: Octree.hpp:35
Definition: Matrix.hpp:27
N-D closest intersection query result.
Definition: IntersectionQueryEngine.hpp:25
Definition: pybind11Utils.hpp:20
std::vector< T > ContainerType
Definition: Octree.hpp:33
RayIntersectionTestFunc< T, 3 > RayIntersectionTestFunc3
3-D ray-item intersection test function.
Definition: IntersectionQueryEngine.hpp:76
GetRayIntersectionFunc< T, 3 > GetRayIntersectionFunc3
3-D ray-item closest intersection evaluation function.
Definition: IntersectionQueryEngine.hpp:89
Abstract base class for N-D intersection test query engine.
Definition: IntersectionQueryEngine.hpp:97
size_t GetNumberOfNodes() const
Returns the number of octree nodes.
Definition: Octree-Impl.hpp:227
BoxIntersectionTestFunc< T, 3 > BoxIntersectionTestFunc3
3-D box-item intersection test function.
Definition: IntersectionQueryEngine.hpp:63
Abstract base class for N-D nearest neighbor query engine.
Definition: NearestNeighborQueryEngine.hpp:52
void Build(const std::vector< T > &items, const BoundingBox3D &bound, const BoxIntersectionTestFunc3< T > &testFunc, size_t maxDepth)
Definition: Octree-Impl.hpp:26
NearestNeighborDistanceFunc< T, 3 > NearestNeighborDistanceFunc3
3-D nearest neighbor distance measure function.
Definition: NearestNeighborQueryEngine.hpp:48
size_t GetChildIndex(size_t nodeIdx, size_t childIdx) const
Returns a child&#39;s index for given node.
Definition: Octree-Impl.hpp:239
Iterator begin()
Returns the begin iterator of the item.
Definition: Octree-Impl.hpp:191
std::function< void(const T &)> IntersectionVisitorFunc
Visitor function which is invoked for each intersecting item.
Definition: IntersectionQueryEngine.hpp:93
Iterator end()
Returns the end iterator of the item.
Definition: Octree-Impl.hpp:197