Open3D (C++ API)  0.13.0
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 
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 
132  static std::function<std::shared_ptr<OctreeInternalNode>()>
133  GetInitFunction();
134 
138  static std::function<void(std::shared_ptr<OctreeInternalNode>)>
139  GetUpdateFunction();
140 
141  bool ConvertToJsonValue(Json::Value& value) const override;
142  bool ConvertFromJsonValue(const Json::Value& value) override;
143 
144 public:
149  std::vector<std::shared_ptr<OctreeNode>> children_;
150 };
151 
157 public:
161 
166  static std::function<std::shared_ptr<OctreeInternalNode>()>
167  GetInitFunction();
168 
173  static std::function<void(std::shared_ptr<OctreeInternalNode>)>
174  GetUpdateFunction(size_t idx);
175 
176  bool ConvertToJsonValue(Json::Value& value) const override;
177  bool ConvertFromJsonValue(const Json::Value& value) override;
178 
179 public:
181  std::vector<size_t> indices_;
182 };
183 
187 class OctreeLeafNode : public OctreeNode {
188 public:
189  virtual bool operator==(const OctreeLeafNode& other) const = 0;
191  virtual std::shared_ptr<OctreeLeafNode> Clone() const = 0;
192 };
193 
198 public:
199  bool operator==(const OctreeLeafNode& other) const override;
200 
202  std::shared_ptr<OctreeLeafNode> Clone() const override;
203 
208  static std::function<std::shared_ptr<OctreeLeafNode>()> GetInitFunction();
209 
216  static std::function<void(std::shared_ptr<OctreeLeafNode>)>
217  GetUpdateFunction(const Eigen::Vector3d& color);
218 
219  bool ConvertToJsonValue(Json::Value& value) const override;
220  bool ConvertFromJsonValue(const Json::Value& value) override;
223  Eigen::Vector3d color_ = Eigen::Vector3d(0, 0, 0);
224 };
225 
232 public:
233  bool operator==(const OctreeLeafNode& other) const override;
235  std::shared_ptr<OctreeLeafNode> Clone() const override;
236 
241  static std::function<std::shared_ptr<OctreeLeafNode>()> GetInitFunction();
242 
250  static std::function<void(std::shared_ptr<OctreeLeafNode>)>
251  GetUpdateFunction(size_t index, const Eigen::Vector3d& color);
252 
253  bool ConvertToJsonValue(Json::Value& value) const override;
254  bool ConvertFromJsonValue(const Json::Value& value) override;
255 
257  std::vector<size_t> indices_;
258 };
259 
264 public:
268  origin_(0, 0, 0),
269  size_(0),
270  max_depth_(0) {}
274  Octree(const size_t& max_depth)
276  origin_(0, 0, 0),
277  size_(0),
278  max_depth_(max_depth) {}
284  Octree(const size_t& max_depth,
285  const Eigen::Vector3d& origin,
286  const double& size)
288  origin_(origin),
289  size_(size),
290  max_depth_(max_depth) {}
291  Octree(const Octree& src_octree);
292  ~Octree() override {}
293 
294 public:
295  Octree& Clear() override;
296  bool IsEmpty() const override;
297  Eigen::Vector3d GetMinBound() const override;
298  Eigen::Vector3d GetMaxBound() const override;
299  Eigen::Vector3d GetCenter() const override;
300  AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const override;
301  OrientedBoundingBox GetOrientedBoundingBox() const override;
302  Octree& Transform(const Eigen::Matrix4d& transformation) override;
303  Octree& Translate(const Eigen::Vector3d& translation,
304  bool relative = true) override;
305  Octree& Scale(const double scale, const Eigen::Vector3d& center) override;
306  Octree& Rotate(const Eigen::Matrix3d& R,
307  const Eigen::Vector3d& center) override;
308  bool ConvertToJsonValue(Json::Value& value) const override;
309  bool ConvertFromJsonValue(const Json::Value& value) override;
310 
311 public:
318  void ConvertFromPointCloud(const geometry::PointCloud& point_cloud,
319  double size_expand = 0.01);
320 
322  std::shared_ptr<OctreeNode> root_node_ = nullptr;
323 
326  Eigen::Vector3d origin_;
327 
330  double size_;
331 
334  size_t max_depth_;
335 
349  void InsertPoint(
350  const Eigen::Vector3d& point,
351  const std::function<std::shared_ptr<OctreeLeafNode>()>& fl_init,
352  const std::function<void(std::shared_ptr<OctreeLeafNode>)>&
353  fl_update,
354  const std::function<std::shared_ptr<OctreeInternalNode>()>&
355  fi_init = nullptr,
356  const std::function<void(std::shared_ptr<OctreeInternalNode>)>&
357  fi_update = nullptr);
358 
366  void Traverse(
367  const std::function<bool(const std::shared_ptr<OctreeNode>&,
368  const std::shared_ptr<OctreeNodeInfo>&)>&
369  f);
370 
378  void Traverse(
379  const std::function<bool(const std::shared_ptr<OctreeNode>&,
380  const std::shared_ptr<OctreeNodeInfo>&)>&
381  f) const;
382 
383  std::pair<std::shared_ptr<OctreeLeafNode>, std::shared_ptr<OctreeNodeInfo>>
384 
389  LocateLeafNode(const Eigen::Vector3d& point) const;
390 
397  static bool IsPointInBound(const Eigen::Vector3d& point,
398  const Eigen::Vector3d& origin,
399  const double& size);
400 
402  bool operator==(const Octree& other) const;
403 
405  std::shared_ptr<geometry::VoxelGrid> ToVoxelGrid() const;
406 
408  void CreateFromVoxelGrid(const geometry::VoxelGrid& voxel_grid);
409 
410 private:
411  static void TraverseRecurse(
412  const std::shared_ptr<OctreeNode>& node,
413  const std::shared_ptr<OctreeNodeInfo>& node_info,
414  const std::function<bool(const std::shared_ptr<OctreeNode>&,
415  const std::shared_ptr<OctreeNodeInfo>&)>&
416  f);
417 
418  void InsertPointRecurse(
419  const std::shared_ptr<OctreeNode>& node,
420  const std::shared_ptr<OctreeNodeInfo>& node_info,
421  const Eigen::Vector3d& point,
422  const std::function<std::shared_ptr<OctreeLeafNode>()>& f_l_init,
423  const std::function<void(std::shared_ptr<OctreeLeafNode>)>&
424  f_l_update,
425  const std::function<std::shared_ptr<OctreeInternalNode>()>&
426  f_i_init,
427  const std::function<void(std::shared_ptr<OctreeInternalNode>)>&
428  f_i_update);
429 };
430 
431 } // namespace geometry
432 } // namespace open3d
Eigen::Vector3d color_
Definition: PointCloud.cpp:244
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:187
std::vector< size_t > indices_
Indices of points associated with any children of this node.
Definition: Octree.h:181
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:150
OctreeInternalPointNode()
Default Constructor.
Definition: Octree.h:160
~OctreeNodeInfo()
Definition: Octree.h:68
std::vector< size_t > indices_
Associated point indices with this node.
Definition: Octree.h:257
bool operator==(const PointXYZ A, const PointXYZ B)
Definition: Cloud.h:151
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
OctreeInternalPointNode class is an OctreeInternalNode containing a list of indices which is the unio...
Definition: Octree.h:156
Octree(const size_t &max_depth, const Eigen::Vector3d &origin, const double &size)
Parameterized Constructor.
Definition: Octree.h:284
int size
Definition: FilePCD.cpp:59
Eigen::Vector3d origin_
Definition: Octree.h:326
math::float4 color
Definition: LineSetBuffers.cpp:64
OctreeColorLeafNode class is an OctreeLeafNode containing color.
Definition: Octree.h:197
Octree()
Default Constructor.
Definition: Octree.h:266
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:274
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:334
size_t child_index_
Definition: Octree.h:79
std::vector< std::shared_ptr< OctreeNode > > children_
Definition: Octree.h:149
double size_
Definition: Octree.h:330
OctreePointColorLeafNode class is an OctreeColorLeafNode containing a list of indices corresponding t...
Definition: Octree.h:231
The base class for octree node.
Definition: Octree.h:89
Definition: PinholeCameraIntrinsic.cpp:35
virtual ~OctreeNode()
Definition: Octree.h:94
VoxelGrid is a collection of voxels which are aligned in grid.
Definition: VoxelGrid.h:80
GeometryType
Specifies possible geometry types.
Definition: Geometry.h:42
Octree datastructure.
Definition: Octree.h:263
double size_
Size of the node.
Definition: Octree.h:74
~Octree() override
Definition: Octree.h:292
OctreeNode&#39;s information.
Definition: Octree.h:47
Eigen::Vector3d origin_
Origin coordinate of the node.
Definition: Octree.h:72
Definition: IJsonConvertible.h:57