Open3D (C++ API)
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 
48 public:
52  OctreeNodeInfo() : origin_(0, 0, 0), size_(0), depth_(0), child_index_(0) {}
53 
60  OctreeNodeInfo(const Eigen::Vector3d& origin,
61  const double& size,
62  const size_t& depth,
63  const size_t& child_index)
64  : origin_(origin),
65  size_(size),
66  depth_(depth),
67  child_index_(child_index) {}
69 
70 public:
72  Eigen::Vector3d origin_;
74  double size_;
76  size_t depth_;
79  size_t child_index_;
80 };
81 
90 public:
94  virtual ~OctreeNode() {}
95 
97  static std::shared_ptr<OctreeNode> ConstructFromJsonValue(
98  const Json::Value& value);
99 };
100 
120 public:
123  OctreeInternalNode() : children_(8) {}
124  static std::shared_ptr<OctreeNodeInfo> GetInsertionNodeInfo(
125  const std::shared_ptr<OctreeNodeInfo>& node_info,
126  const Eigen::Vector3d& point);
127 
128  bool ConvertToJsonValue(Json::Value& value) const override;
129  bool ConvertFromJsonValue(const Json::Value& value) override;
130 
131 public:
132  // Use vector instead of C-array for Pybind11, otherwise, need to define
133  // more helper functions
134  // https://github.com/pybind/pybind11/issues/546#issuecomment-265707318
136  std::vector<std::shared_ptr<OctreeNode>> children_;
137 };
138 
142 class OctreeLeafNode : public OctreeNode {
143 public:
144  virtual bool operator==(const OctreeLeafNode& other) const = 0;
146  virtual std::shared_ptr<OctreeLeafNode> Clone() const = 0;
147 };
148 
153 public:
154  bool operator==(const OctreeLeafNode& other) const override;
156  std::shared_ptr<OctreeLeafNode> Clone() const override;
161  static std::function<std::shared_ptr<OctreeLeafNode>()> GetInitFunction();
162  static std::function<void(std::shared_ptr<OctreeLeafNode>)>
169  GetUpdateFunction(const Eigen::Vector3d& color);
170 
171  bool ConvertToJsonValue(Json::Value& value) const override;
172  bool ConvertFromJsonValue(const Json::Value& value) override;
173  // TODO: flexible data, with lambda function for handling node
175  Eigen::Vector3d color_ = Eigen::Vector3d(0, 0, 0);
176 };
177 
182 public:
186  origin_(0, 0, 0),
187  size_(0),
188  max_depth_(0) {}
192  Octree(const size_t& max_depth)
194  origin_(0, 0, 0),
195  size_(0),
196  max_depth_(max_depth) {}
202  Octree(const size_t& max_depth,
203  const Eigen::Vector3d& origin,
204  const double& size)
206  origin_(origin),
207  size_(size),
208  max_depth_(max_depth) {}
209  Octree(const Octree& src_octree);
210  ~Octree() override {}
211 
212 public:
213  Octree& Clear() override;
214  bool IsEmpty() const override;
215  Eigen::Vector3d GetMinBound() const override;
216  Eigen::Vector3d GetMaxBound() const override;
217  Eigen::Vector3d GetCenter() const override;
218  AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const override;
219  OrientedBoundingBox GetOrientedBoundingBox() const override;
220  Octree& Transform(const Eigen::Matrix4d& transformation) override;
221  Octree& Translate(const Eigen::Vector3d& translation,
222  bool relative = true) override;
223  Octree& Scale(const double scale, const Eigen::Vector3d& center) override;
224  Octree& Rotate(const Eigen::Matrix3d& R,
225  const Eigen::Vector3d& center) override;
226  bool ConvertToJsonValue(Json::Value& value) const override;
227  bool ConvertFromJsonValue(const Json::Value& value) override;
228 
229 public:
236  void ConvertFromPointCloud(const geometry::PointCloud& point_cloud,
237  double size_expand = 0.01);
238 
240  std::shared_ptr<OctreeNode> root_node_ = nullptr;
241 
244  Eigen::Vector3d origin_;
245 
248  double size_;
249 
252  size_t max_depth_;
253 
257  void InsertPoint(
258  const Eigen::Vector3d& point,
259  const std::function<std::shared_ptr<OctreeLeafNode>()>& f_init,
260  const std::function<void(std::shared_ptr<OctreeLeafNode>)>&
261  f_update);
262 
265  void Traverse(
266  const std::function<void(const std::shared_ptr<OctreeNode>&,
267  const std::shared_ptr<OctreeNodeInfo>&)>&
268  f);
269 
272  void Traverse(
273  const std::function<void(const std::shared_ptr<OctreeNode>&,
274  const std::shared_ptr<OctreeNodeInfo>&)>&
275  f) const;
276 
277  std::pair<std::shared_ptr<OctreeLeafNode>, std::shared_ptr<OctreeNodeInfo>>
278 
283  LocateLeafNode(const Eigen::Vector3d& point) const;
284 
291  static bool IsPointInBound(const Eigen::Vector3d& point,
292  const Eigen::Vector3d& origin,
293  const double& size);
294 
296  bool operator==(const Octree& other) const;
297 
299  std::shared_ptr<geometry::VoxelGrid> ToVoxelGrid() const;
300 
302  void CreateFromVoxelGrid(const geometry::VoxelGrid& voxel_grid);
303 
304 private:
305  static void TraverseRecurse(
306  const std::shared_ptr<OctreeNode>& node,
307  const std::shared_ptr<OctreeNodeInfo>& node_info,
308  const std::function<void(const std::shared_ptr<OctreeNode>&,
309  const std::shared_ptr<OctreeNodeInfo>&)>&
310  f);
311 
312  void InsertPointRecurse(
313  const std::shared_ptr<OctreeNode>& node,
314  const std::shared_ptr<OctreeNodeInfo>& node_info,
315  const Eigen::Vector3d& point,
316  const std::function<std::shared_ptr<OctreeLeafNode>()>& f_init,
317  const std::function<void(std::shared_ptr<OctreeLeafNode>)>&
318  f_update);
319 };
320 
321 } // namespace geometry
322 } // namespace open3d
OctreeNode()
Default Constructor.
Definition: Octree.h:93
OctreeNodeInfo()
Default Constructor.
Definition: Octree.h:52
OctreeInternalNode class, containing OctreeNode children.
Definition: Octree.h:119
The base geometry class.
Definition: Geometry.h:37
OctreeLeafNode base class.
Definition: Octree.h:142
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:164
~OctreeNodeInfo()
Definition: Octree.h:68
OctreeInternalNode()
Default Constructor.
Definition: Octree.h:123
A bounding box oriented along an arbitrary frame of reference.
Definition: BoundingVolume.h:44
A point cloud consists of point coordinates, and optionally point colors and point normals...
Definition: PointCloud.h:54
Octree(const size_t &max_depth, const Eigen::Vector3d &origin, const double &size)
Parameterized Constructor.
Definition: Octree.h:202
int size
Definition: FilePCD.cpp:57
Eigen::Vector3d origin_
Definition: Octree.h:244
math::float4 color
Definition: LineSetBuffers.cpp:46
OctreeColorLeafNode class is an OctreeLeafNode containing color.
Definition: Octree.h:152
Octree()
Default Constructor.
Definition: Octree.h:184
OctreeNodeInfo(const Eigen::Vector3d &origin, const double &size, const size_t &depth, const size_t &child_index)
Parameterized Constructor.
Definition: Octree.h:60
Octree(const size_t &max_depth)
Parameterized Constructor.
Definition: Octree.h:192
The base geometry class for 3D geometries.
Definition: Geometry3D.h:46
size_t depth_
Depth of the node to the root. The root is of depth 0.
Definition: Octree.h:76
size_t max_depth_
Definition: Octree.h:252
size_t child_index_
Definition: Octree.h:79
std::vector< std::shared_ptr< OctreeNode > > children_
List of children Nodes.
Definition: Octree.h:136
double size_
Definition: Octree.h:248
Eigen::Vector3d origin
Definition: FilePLY.cpp:285
The base class for octree node.
Definition: Octree.h:89
Definition: Open3DViewer.h:29
virtual ~OctreeNode()
Definition: Octree.h:94
VoxelGrid is a collection of voxels which are aligned in grid.
Definition: VoxelGrid.h:81
GeometryType
Specifies possible geometry types.
Definition: Geometry.h:42
Octree datastructure.
Definition: Octree.h:181
Eigen::Vector3d color_
Definition: PointCloud.cpp:240
double size_
Size of the node.
Definition: Octree.h:74
~Octree() override
Definition: Octree.h:210
OctreeNode&#39;s information.
Definition: Octree.h:47
Eigen::Vector3d origin_
Origin coordinate of the node.
Definition: Octree.h:72
Definition: IJsonConvertible.h:42