Octree.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Octree.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Generic octree data structure.
6 > Created Time: 2017/10/15
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_OCTREE_H
10 #define CUBBYFLOW_OCTREE_H
11 
14 
15 namespace CubbyFlow
16 {
26  template <typename T>
27  class Octree final : public IntersectionQueryEngine3<T>, public NearestNeighborQueryEngine3<T>
28  {
29  public:
30  using ContainerType = std::vector<T>;
31  using Iterator = typename ContainerType::iterator;
32  using ConstIterator = typename ContainerType::const_iterator;
33 
35  Octree();
36 
39  void Build(
40  const std::vector<T>& items, const BoundingBox3D& bound,
41  const BoxIntersectionTestFunc3<T>& testFunc, size_t maxDepth);
42 
44  void Clear();
45 
49  const Vector3D& pt,
50  const NearestNeighborDistanceFunc3<T>& distanceFunc) const override;
51 
53  bool IsIntersects(const BoundingBox3D& box,
54  const BoxIntersectionTestFunc3<T>& testFunc) const override;
55 
57  bool IsIntersects(const Ray3D& ray,
58  const RayIntersectionTestFunc3<T>& testFunc) const override;
59 
62  const BoundingBox3D& box, const BoxIntersectionTestFunc3<T>& testFunc,
63  const IntersectionVisitorFunc3<T>& visitorFunc) const override;
64 
67  const Ray3D& ray, const RayIntersectionTestFunc3<T>& testFunc,
68  const IntersectionVisitorFunc3<T>& visitorFunc) const override;
69 
72  const Ray3D& ray,
73  const GetRayIntersectionFunc3<T>& testFunc) const override;
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 
94  size_t GetNumberOfNodes() const;
95 
97  const std::vector<size_t>& GetItemsAtNode(size_t nodeIdx) const;
98 
111  size_t GetChildIndex(size_t nodeIdx, size_t childIdx) const;
112 
114  const BoundingBox3D& GetBoundingBox() const;
115 
117  size_t GetMaxDepth() const;
118 
119  private:
120  struct Node
121  {
122  size_t firstChild = std::numeric_limits<size_t>::max();
123  std::vector<size_t> items;
124 
125  bool IsLeaf() const;
126  };
127 
128  size_t m_maxDepth = 1;
129  BoundingBox3D m_bbox;
130  std::vector<T> m_items;
131  std::vector<Node> m_nodes;
132 
133  void Build(size_t nodeIdx, size_t currentDepth,
134  const BoundingBox3D& currentBound,
135  const BoxIntersectionTestFunc3<T>& overlapsFunc);
136 
137  bool IsIntersects(const BoundingBox3D& box,
138  const BoxIntersectionTestFunc3<T>& testFunc, size_t nodeIdx,
139  const BoundingBox3D& currentBound) const;
140 
141  bool IsIntersects(const Ray3D& ray,
142  const RayIntersectionTestFunc3<T>& testFunc, size_t nodeIdx,
143  const BoundingBox3D& currentBound) const;
144 
145  void ForEachIntersectingItem(const BoundingBox3D& box,
146  const BoxIntersectionTestFunc3<T>& testFunc,
147  const IntersectionVisitorFunc3<T>& visitorFunc,
148  size_t nodeIdx,
149  const BoundingBox3D& currentBound) const;
150 
151  void ForEachIntersectingItem(const Ray3D& ray,
152  const RayIntersectionTestFunc3<T>& testFunc,
153  const IntersectionVisitorFunc3<T>& visitorFunc,
154  size_t nodeIdx,
155  const BoundingBox3D& currentBound) const;
156 
158  const Ray3D& ray, const GetRayIntersectionFunc3<T>& testFunc,
159  size_t nodeIdx, const BoundingBox3D& currentBound,
161  };
162 }
163 
165 
166 #endif
void Clear()
Clears all the contents of this instance.
Definition: Octree-Impl.h:52
3-D vector class.
Definition: Vector3.h:26
Generic octree data structure.
Definition: Octree.h:27
const BoundingBox3D & GetBoundingBox() const
Returns the bounding box of this octree.
Definition: Octree-Impl.h:244
Abstract base class for 3-D nearest neighbor query engine.
Definition: NearestNeighborQueryEngine3.h:30
size_t GetMaxDepth() const
Returns the maximum depth of the tree.
Definition: Octree-Impl.h:250
typename ContainerType::iterator Iterator
Definition: Octree.h:31
size_t GetNumberOfItems() const
Returns the number of items.
Definition: Octree-Impl.h:214
ClosestIntersectionQueryResult3< T > GetClosestIntersection(const Ray3D &ray, const GetRayIntersectionFunc3< T > &testFunc) const override
Returns the closest intersection for given ray.
Definition: Octree-Impl.h:179
const std::vector< size_t > & GetItemsAtNode(size_t nodeIdx) const
Returns the list of the items for given node index.
Definition: Octree-Impl.h:232
const T & GetItem(size_t i) const
Returns the item at i.
Definition: Octree-Impl.h:220
typename ContainerType::const_iterator ConstIterator
Definition: Octree.h:32
std::function< bool(const T &, const BoundingBox3D &)> BoxIntersectionTestFunc3
Box-item intersection test function.
Definition: IntersectionQueryEngine3.h:31
NearestNeighborQueryResult3< T > GetNearestNeighbor(const Vector3D &pt, const NearestNeighborDistanceFunc3< T > &distanceFunc) const override
Definition: Octree-Impl.h:61
std::function< double(const T &, const Ray3D &)> GetRayIntersectionFunc3
Ray-item closest intersection evaluation function.
Definition: IntersectionQueryEngine3.h:39
Definition: pybind11Utils.h:24
std::vector< T > ContainerType
Definition: Octree.h:30
Octree()
Default constructor.
Definition: Octree-Impl.h:24
3-D axis-aligned bounding box class.
Definition: BoundingBox3.h:44
Closest intersection query result.
Definition: IntersectionQueryEngine3.h:19
size_t GetNumberOfNodes() const
Returns the number of octree nodes.
Definition: Octree-Impl.h:226
bool IsIntersects(const BoundingBox3D &box, const BoxIntersectionTestFunc3< T > &testFunc) const override
Returns true if given box intersects with any of the stored items.
Definition: Octree-Impl.h:149
void Build(const std::vector< T > &items, const BoundingBox3D &bound, const BoxIntersectionTestFunc3< T > &testFunc, size_t maxDepth)
Definition: Octree-Impl.h:30
size_t GetChildIndex(size_t nodeIdx, size_t childIdx) const
Returns a child&#39;s index for given node.
Definition: Octree-Impl.h:238
std::function< void(const T &)> IntersectionVisitorFunc3
Visitor function which is invoked for each intersecting item.
Definition: IntersectionQueryEngine3.h:43
std::function< bool(const T &, const Ray3D &)> RayIntersectionTestFunc3
Ray-item intersection test function.
Definition: IntersectionQueryEngine3.h:35
Nearest neighbor query result.
Definition: NearestNeighborQueryEngine3.h:18
Iterator begin()
Returns the begin iterator of the item.
Definition: Octree-Impl.h:190
Class for 3-D ray.
Definition: Ray3.h:23
void ForEachIntersectingItem(const BoundingBox3D &box, const BoxIntersectionTestFunc3< T > &testFunc, const IntersectionVisitorFunc3< T > &visitorFunc) const override
Invokes visitorFunc for every intersecting items.
Definition: Octree-Impl.h:163
Iterator end()
Returns the end iterator of the item.
Definition: Octree-Impl.h:196
Abstract base class for 3-D intersection test query engine.
Definition: IntersectionQueryEngine3.h:47
std::function< double(const T &, const Vector3D &)> NearestNeighborDistanceFunc3
Nearest neighbor distance measure function.
Definition: NearestNeighborQueryEngine3.h:26