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 
45 public:
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_ = Eigen::Vector3d(0, 0, 0);
59  double size_ = 0;
60  size_t depth_ = 0;
61  size_t child_index_ = 0;
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:
132  Octree(const size_t& max_depth)
133  : Geometry3D(Geometry::GeometryType::Octree), max_depth_(max_depth) {}
134  Octree(const size_t& max_depth,
135  const Eigen::Vector3d& origin,
136  const double& size)
138  max_depth_(max_depth),
139  origin_(origin),
140  size_(size) {}
141  Octree(const Octree& src_octree);
142  ~Octree() override {}
143 
144 public:
145  void Clear() override;
146  bool IsEmpty() const override;
147  Eigen::Vector3d GetMinBound() const override;
148  Eigen::Vector3d GetMaxBound() const override;
149  Octree& Transform(const Eigen::Matrix4d& transformation) override;
150  Octree& Translate(const Eigen::Vector3d& translation) override;
151  Octree& Scale(const double scale, bool center = true) override;
152  Octree& Rotate(const Eigen::Vector3d& rotation,
153  bool center = true,
154  RotationType type = RotationType::XYZ) override;
155  bool ConvertToJsonValue(Json::Value& value) const override;
156  bool ConvertFromJsonValue(const Json::Value& value) override;
157 
158 public:
159  void ConvertFromPointCloud(const geometry::PointCloud& point_cloud,
160  double size_expand = 0.01);
161 
163  std::shared_ptr<OctreeNode> root_node_ = nullptr;
164 
167  Eigen::Vector3d origin_ = Eigen::Vector3d(0, 0, 0);
168 
171  double size_ = 0;
172 
175  size_t max_depth_ = 0;
176 
178  void InsertPoint(
179  const Eigen::Vector3d& point,
180  const std::function<std::shared_ptr<OctreeLeafNode>()>& f_init,
181  const std::function<void(std::shared_ptr<OctreeLeafNode>)>&
182  f_update);
183 
186  void Traverse(
187  const std::function<void(const std::shared_ptr<OctreeNode>&,
188  const std::shared_ptr<OctreeNodeInfo>&)>&
189  f);
190 
193  void Traverse(
194  const std::function<void(const std::shared_ptr<OctreeNode>&,
195  const std::shared_ptr<OctreeNodeInfo>&)>&
196  f) const;
197 
198  std::pair<std::shared_ptr<OctreeLeafNode>, std::shared_ptr<OctreeNodeInfo>>
199  LocateLeafNode(const Eigen::Vector3d& point) const;
200 
203  static bool IsPointInBound(const Eigen::Vector3d& point,
204  const Eigen::Vector3d& origin,
205  const double& size);
206 
208  bool operator==(const Octree& other) const;
209 
211  std::shared_ptr<geometry::VoxelGrid> ToVoxelGrid() const;
212 
214  void FromVoxelGrid(const geometry::VoxelGrid& voxel_grid);
215 
216 private:
217  static void TraverseRecurse(
218  const std::shared_ptr<OctreeNode>& node,
219  const std::shared_ptr<OctreeNodeInfo>& node_info,
220  const std::function<void(const std::shared_ptr<OctreeNode>&,
221  const std::shared_ptr<OctreeNodeInfo>&)>&
222  f);
223 
224  void InsertPointRecurse(
225  const std::shared_ptr<OctreeNode>& node,
226  const std::shared_ptr<OctreeNodeInfo>& node_info,
227  const Eigen::Vector3d& point,
228  const std::function<std::shared_ptr<OctreeLeafNode>()>& f_init,
229  const std::function<void(std::shared_ptr<OctreeLeafNode>)>&
230  f_update);
231 };
232 
233 } // namespace geometry
234 } // namespace open3d
OctreeNode()
Definition: Octree.h:70
OctreeNodeInfo()
Definition: Octree.h:46
Definition: Geometry.h:32
Definition: Octree.h:109
~OctreeNodeInfo()
Definition: Octree.h:55
OctreeInternalNode()
Definition: Octree.h:94
Definition: PointCloud.h:49
Octree(const size_t &max_depth, const Eigen::Vector3d &origin, const double &size)
Definition: Octree.h:134
RotationType
Definition: Geometry3D.h:40
int size
Definition: FilePCD.cpp:55
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:132
Definition: Geometry3D.h:38
size_t depth_
Definition: Octree.h:60
size_t child_index_
Definition: Octree.h:61
Eigen::Vector3d color_
Definition: DownSample.cpp:80
std::vector< std::shared_ptr< OctreeNode > > children_
Definition: Octree.h:106
char type
Definition: FilePCD.cpp:56
Definition: Octree.h:68
Definition: PinholeCameraIntrinsic.cpp:34
virtual ~OctreeNode()
Definition: Octree.h:71
Definition: VoxelGrid.h:55
GeometryType
Definition: Geometry.h:34
Definition: Octree.h:129
double size_
Definition: Octree.h:59
~Octree() override
Definition: Octree.h:142
Definition: Octree.h:44
Eigen::Vector3d origin_
Definition: Octree.h:58
Definition: IJsonConvertible.h:42