Open3D (C++ API)
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Octree.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <cstddef>
30 #include <memory>
31 #include <vector>
32 
35 
36 namespace open3d {
37 namespace geometry {
38 
39 class PointCloud;
40 class VoxelGrid;
41 
45 public:
46  OctreeNodeInfo() : origin_(0, 0, 0), size_(0), depth_(0), child_index_(0) {}
47  OctreeNodeInfo(const Eigen::Vector3d& origin,
48  const double& size,
49  const size_t& depth,
50  const size_t& child_index)
51  : origin_(origin),
52  size_(size),
53  depth_(depth),
54  child_index_(child_index) {}
56 
57 public:
58  Eigen::Vector3d origin_;
59  double size_;
60  size_t depth_;
61  size_t child_index_;
62 };
63 
69 public:
71  virtual ~OctreeNode() {}
72 
74  static std::shared_ptr<OctreeNode> ConstructFromJsonValue(
75  const Json::Value& value);
76 };
77 
93 public:
94  OctreeInternalNode() : children_(8) {}
95  static std::shared_ptr<OctreeNodeInfo> GetInsertionNodeInfo(
96  const std::shared_ptr<OctreeNodeInfo>& node_info,
97  const Eigen::Vector3d& point);
98 
99  bool ConvertToJsonValue(Json::Value& value) const override;
100  bool ConvertFromJsonValue(const Json::Value& value) override;
101 
102 public:
103  // Use vector instead of C-array for Pybind11, otherwise, need to define
104  // more helper functions
105  // https://github.com/pybind/pybind11/issues/546#issuecomment-265707318
106  std::vector<std::shared_ptr<OctreeNode>> children_;
107 };
108 
109 class OctreeLeafNode : public OctreeNode {
110 public:
111  virtual bool operator==(const OctreeLeafNode& other) const = 0;
112  virtual std::shared_ptr<OctreeLeafNode> Clone() const = 0;
113 };
114 
116 public:
117  bool operator==(const OctreeLeafNode& other) const override;
118  std::shared_ptr<OctreeLeafNode> Clone() const override;
119  static std::function<std::shared_ptr<OctreeLeafNode>()> GetInitFunction();
120  static std::function<void(std::shared_ptr<OctreeLeafNode>)>
121  GetUpdateFunction(const Eigen::Vector3d& color);
122 
123  bool ConvertToJsonValue(Json::Value& value) const override;
124  bool ConvertFromJsonValue(const Json::Value& value) override;
125  // TODO: flexible data, with lambda function for handling node
126  Eigen::Vector3d color_ = Eigen::Vector3d(0, 0, 0);
127 };
128 
130 public:
133  origin_(0, 0, 0),
134  size_(0),
135  max_depth_(0) {}
136  Octree(const size_t& max_depth)
138  origin_(0, 0, 0),
139  size_(0),
140  max_depth_(max_depth) {}
141  Octree(const size_t& max_depth,
142  const Eigen::Vector3d& origin,
143  const double& size)
145  origin_(origin),
146  size_(size),
147  max_depth_(max_depth) {}
148  Octree(const Octree& src_octree);
149  ~Octree() override {}
150 
151 public:
152  Octree& Clear() override;
153  bool IsEmpty() const override;
154  Eigen::Vector3d GetMinBound() const override;
155  Eigen::Vector3d GetMaxBound() const override;
156  Eigen::Vector3d GetCenter() const override;
157  AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const override;
158  OrientedBoundingBox GetOrientedBoundingBox() const override;
159  Octree& Transform(const Eigen::Matrix4d& transformation) override;
160  Octree& Translate(const Eigen::Vector3d& translation,
161  bool relative = true) override;
162  Octree& Scale(const double scale, bool center = true) override;
163  Octree& Rotate(const Eigen::Matrix3d& R, bool center = true) override;
164  bool ConvertToJsonValue(Json::Value& value) const override;
165  bool ConvertFromJsonValue(const Json::Value& value) override;
166 
167 public:
168  void ConvertFromPointCloud(const geometry::PointCloud& point_cloud,
169  double size_expand = 0.01);
170 
172  std::shared_ptr<OctreeNode> root_node_ = nullptr;
173 
176  Eigen::Vector3d origin_;
177 
180  double size_;
181 
184  size_t max_depth_;
185 
187  void InsertPoint(
188  const Eigen::Vector3d& point,
189  const std::function<std::shared_ptr<OctreeLeafNode>()>& f_init,
190  const std::function<void(std::shared_ptr<OctreeLeafNode>)>&
191  f_update);
192 
195  void Traverse(
196  const std::function<void(const std::shared_ptr<OctreeNode>&,
197  const std::shared_ptr<OctreeNodeInfo>&)>&
198  f);
199 
202  void Traverse(
203  const std::function<void(const std::shared_ptr<OctreeNode>&,
204  const std::shared_ptr<OctreeNodeInfo>&)>&
205  f) const;
206 
207  std::pair<std::shared_ptr<OctreeLeafNode>, std::shared_ptr<OctreeNodeInfo>>
208  LocateLeafNode(const Eigen::Vector3d& point) const;
209 
212  static bool IsPointInBound(const Eigen::Vector3d& point,
213  const Eigen::Vector3d& origin,
214  const double& size);
215 
217  bool operator==(const Octree& other) const;
218 
220  std::shared_ptr<geometry::VoxelGrid> ToVoxelGrid() const;
221 
223  void CreateFromVoxelGrid(const geometry::VoxelGrid& voxel_grid);
224 
225 private:
226  static void TraverseRecurse(
227  const std::shared_ptr<OctreeNode>& node,
228  const std::shared_ptr<OctreeNodeInfo>& node_info,
229  const std::function<void(const std::shared_ptr<OctreeNode>&,
230  const std::shared_ptr<OctreeNodeInfo>&)>&
231  f);
232 
233  void InsertPointRecurse(
234  const std::shared_ptr<OctreeNode>& node,
235  const std::shared_ptr<OctreeNodeInfo>& node_info,
236  const Eigen::Vector3d& point,
237  const std::function<std::shared_ptr<OctreeLeafNode>()>& f_init,
238  const std::function<void(std::shared_ptr<OctreeLeafNode>)>&
239  f_update);
240 };
241 
242 } // namespace geometry
243 } // namespace open3d
OctreeNode()
Definition: Octree.h:70
OctreeNodeInfo()
Definition: Octree.h:46
The base geometry class.
Definition: Geometry.h:35
Definition: Octree.h:109
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:130
~OctreeNodeInfo()
Definition: Octree.h:55
OctreeInternalNode()
Definition: Octree.h:94
A bounding box oriented along an arbitrary frame of reference.
Definition: BoundingVolume.h:44
Definition: PointCloud.h:50
Octree(const size_t &max_depth, const Eigen::Vector3d &origin, const double &size)
Definition: Octree.h:141
int size
Definition: FilePCD.cpp:56
Eigen::Vector3d origin_
Definition: Octree.h:176
Octree()
Definition: Octree.h:131
OctreeNodeInfo(const Eigen::Vector3d &origin, const double &size, const size_t &depth, const size_t &child_index)
Definition: Octree.h:47
Octree(const size_t &max_depth)
Definition: Octree.h:136
The base geometry class for 3D geometries.
Definition: Geometry3D.h:46
size_t depth_
Definition: Octree.h:60
size_t max_depth_
Definition: Octree.h:184
size_t child_index_
Definition: Octree.h:61
Eigen::Vector3d color_
Definition: DownSample.cpp:81
std::vector< std::shared_ptr< OctreeNode > > children_
Definition: Octree.h:106
double size_
Definition: Octree.h:180
Eigen::Vector3d origin
Definition: FilePLY.cpp:284
Definition: Octree.h:68
Definition: PinholeCameraIntrinsic.cpp:34
virtual ~OctreeNode()
Definition: Octree.h:71
Definition: VoxelGrid.h:65
GeometryType
Specifies possible geometry types.
Definition: Geometry.h:40
Definition: Octree.h:129
double size_
Definition: Octree.h:59
~Octree() override
Definition: Octree.h:149
Definition: Octree.h:44
Eigen::Vector3d origin_
Definition: Octree.h:58
Definition: IJsonConvertible.h:42