ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Octree.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <IJsonConvertible.h>
11 
12 #include <cstddef>
13 #include <memory>
14 #include <vector>
15 
16 #include "CV_db.h"
17 #include "ecvHObject.h"
18 
19 class ccBBox;
20 class ccPointCloud;
21 class ecvOrientedBBox;
22 
23 namespace cloudViewer {
24 namespace geometry {
25 
26 class VoxelGrid;
27 
34 public:
38  OctreeNodeInfo() : origin_(0, 0, 0), size_(0), depth_(0), child_index_(0) {}
39 
46  OctreeNodeInfo(const Eigen::Vector3d& origin,
47  const double& size,
48  const size_t& depth,
49  const size_t& child_index)
50  : origin_(origin),
51  size_(size),
52  depth_(depth),
53  child_index_(child_index) {}
55 
56 public:
58  Eigen::Vector3d origin_;
60  double size_;
62  size_t depth_;
65  size_t child_index_;
66 };
67 
76 public:
80  virtual ~OctreeNode() {}
81 
83  static std::shared_ptr<OctreeNode> ConstructFromJsonValue(
84  const Json::Value& value);
85 };
86 
105 class CV_DB_LIB_API OctreeInternalNode : public OctreeNode {
106 public:
109  OctreeInternalNode() : children_(8) {}
110  static std::shared_ptr<OctreeNodeInfo> GetInsertionNodeInfo(
111  const std::shared_ptr<OctreeNodeInfo>& node_info,
112  const Eigen::Vector3d& point);
113 
118  static std::function<std::shared_ptr<OctreeInternalNode>()>
119  GetInitFunction();
120 
124  static std::function<void(std::shared_ptr<OctreeInternalNode>)>
125  GetUpdateFunction();
126 
127  bool ConvertToJsonValue(Json::Value& value) const override;
128  bool ConvertFromJsonValue(const Json::Value& value) override;
129 
130 public:
135  std::vector<std::shared_ptr<OctreeNode>> children_;
136 };
137 
142 class CV_DB_LIB_API OctreeInternalPointNode : public OctreeInternalNode {
143 public:
146  OctreeInternalPointNode() : OctreeInternalNode() {}
147 
152  static std::function<std::shared_ptr<OctreeInternalNode>()>
153  GetInitFunction();
154 
159  static std::function<void(std::shared_ptr<OctreeInternalNode>)>
160  GetUpdateFunction(size_t idx);
161 
162  bool ConvertToJsonValue(Json::Value& value) const override;
163  bool ConvertFromJsonValue(const Json::Value& value) override;
164 
165 public:
167  std::vector<size_t> indices_;
168 };
169 
174 public:
175  virtual bool operator==(const OctreeLeafNode& other) const = 0;
177  virtual std::shared_ptr<OctreeLeafNode> Clone() const = 0;
178 };
179 
184 public:
185  bool operator==(const OctreeLeafNode& other) const override;
186 
188  std::shared_ptr<OctreeLeafNode> Clone() const override;
189 
194  static std::function<std::shared_ptr<OctreeLeafNode>()> GetInitFunction();
195 
202  static std::function<void(std::shared_ptr<OctreeLeafNode>)>
203  GetUpdateFunction(const Eigen::Vector3d& color);
204 
205  bool ConvertToJsonValue(Json::Value& value) const override;
206  bool ConvertFromJsonValue(const Json::Value& value) override;
209  Eigen::Vector3d color_ = Eigen::Vector3d(0, 0, 0);
210 };
211 
218 public:
219  bool operator==(const OctreeLeafNode& other) const override;
221  std::shared_ptr<OctreeLeafNode> Clone() const override;
222 
227  static std::function<std::shared_ptr<OctreeLeafNode>()> GetInitFunction();
228 
236  static std::function<void(std::shared_ptr<OctreeLeafNode>)>
237  GetUpdateFunction(size_t index, const Eigen::Vector3d& color);
238 
239  bool ConvertToJsonValue(Json::Value& value) const override;
240  bool ConvertFromJsonValue(const Json::Value& value) override;
241 
243  std::vector<size_t> indices_;
244 };
245 
251 public:
253  Octree(const char* name = "Octree2")
254  : ccHObject(name), origin_(0, 0, 0), size_(0), max_depth_(0) {}
258  Octree(const size_t& max_depth, const char* name = "Octree2")
259  : ccHObject(name), origin_(0, 0, 0), size_(0), max_depth_(max_depth) {}
265  Octree(const size_t& max_depth,
266  const Eigen::Vector3d& origin,
267  const double& size,
268  const char* name = "Octree2")
269  : ccHObject(name),
270  origin_(origin),
271  size_(size),
272  max_depth_(max_depth) {}
273  Octree(const Octree& src_octree, const char* name = "Octree2");
274  ~Octree() override {}
275 
276  // inherited methods (ccHObject)
277  virtual bool isSerializable() const override { return true; }
278 
280  virtual CV_CLASS_ENUM getClassID() const override {
282  }
283 
284  virtual ccBBox getOwnBB(bool withGLFeatures = false) override;
285 
286 public:
287  Octree& Clear();
288  inline virtual bool IsEmpty() const override {
289  return root_node_ == nullptr;
290  }
291  virtual Eigen::Vector3d GetMinBound() const override;
292  virtual Eigen::Vector3d GetMaxBound() const override;
293  virtual Eigen::Vector3d GetCenter() const override;
294  virtual ccBBox GetAxisAlignedBoundingBox() const override;
295  virtual ecvOrientedBBox GetOrientedBoundingBox() const override;
296  virtual Octree& Transform(const Eigen::Matrix4d& transformation) override;
297  virtual Octree& Translate(const Eigen::Vector3d& translation,
298  bool relative = true) override;
299  virtual Octree& Scale(const double s,
300  const Eigen::Vector3d& center) override;
301  virtual Octree& Rotate(const Eigen::Matrix3d& R,
302  const Eigen::Vector3d& center) override;
303  bool ConvertToJsonValue(Json::Value& value) const override;
304  bool ConvertFromJsonValue(const Json::Value& value) override;
305 
306 public:
313  void ConvertFromPointCloud(const ccPointCloud& point_cloud,
314  double size_expand = 0.01);
315 
317  std::shared_ptr<OctreeNode> root_node_ = nullptr;
318 
321  Eigen::Vector3d origin_;
322 
325  double size_;
326 
329  size_t max_depth_;
330 
344  void InsertPoint(
345  const Eigen::Vector3d& point,
346  const std::function<std::shared_ptr<OctreeLeafNode>()>& fl_init,
347  const std::function<void(std::shared_ptr<OctreeLeafNode>)>&
348  fl_update,
349  const std::function<std::shared_ptr<OctreeInternalNode>()>&
350  fi_init = nullptr,
351  const std::function<void(std::shared_ptr<OctreeInternalNode>)>&
352  fi_update = nullptr);
353 
361  void Traverse(
362  const std::function<bool(const std::shared_ptr<OctreeNode>&,
363  const std::shared_ptr<OctreeNodeInfo>&)>&
364  f);
365 
373  void Traverse(
374  const std::function<bool(const std::shared_ptr<OctreeNode>&,
375  const std::shared_ptr<OctreeNodeInfo>&)>&
376  f) const;
377 
378  std::pair<std::shared_ptr<OctreeLeafNode>, std::shared_ptr<OctreeNodeInfo>>
379 
384  LocateLeafNode(const Eigen::Vector3d& point) const;
385 
392  static bool IsPointInBound(const Eigen::Vector3d& point,
393  const Eigen::Vector3d& origin,
394  const double& size);
395 
397  bool operator==(const Octree& other) const;
398 
400  std::shared_ptr<geometry::VoxelGrid> ToVoxelGrid() const;
401 
403  void CreateFromVoxelGrid(const geometry::VoxelGrid& voxel_grid);
404 
405 private:
406  static void TraverseRecurse(
407  const std::shared_ptr<OctreeNode>& node,
408  const std::shared_ptr<OctreeNodeInfo>& node_info,
409  const std::function<bool(const std::shared_ptr<OctreeNode>&,
410  const std::shared_ptr<OctreeNodeInfo>&)>&
411  f);
412 
413  void InsertPointRecurse(
414  const std::shared_ptr<OctreeNode>& node,
415  const std::shared_ptr<OctreeNodeInfo>& node_info,
416  const Eigen::Vector3d& point,
417  const std::function<std::shared_ptr<OctreeLeafNode>()>& f_l_init,
418  const std::function<void(std::shared_ptr<OctreeLeafNode>)>&
419  f_l_update,
420  const std::function<std::shared_ptr<OctreeInternalNode>()>&
421  f_i_init,
422  const std::function<void(std::shared_ptr<OctreeInternalNode>)>&
423  f_i_update);
424 };
425 
426 } // namespace geometry
427 } // namespace cloudViewer
cloudViewer
Generic file read and write utility for python interface.
Definition: CloudViewerConfig.cpp:12
cloudViewer::t::geometry::Transform
void Transform(benchmark::State &state, const core::Device &device)
Definition: PointCloud.cpp:127
cloudViewer::geometry::Octree::getClassID
virtual CV_CLASS_ENUM getClassID() const override
Returns unique class ID.
Definition: Octree.h:280
cloudViewer::geometry::OctreePointColorLeafNode
OctreePointColorLeafNode class is an OctreeColorLeafNode containing a list of indices corresponding t...
Definition: Octree.h:217
cloudViewer::geometry::VoxelGrid
VoxelGrid is a collection of voxels which are aligned in grid.
Definition: VoxelGrid.h:64
cloudViewer::geometry::Octree::Octree
Octree(const char *name="Octree2")
Default Constructor.
Definition: Octree.h:253
cloudViewer::geometry::OctreeNodeInfo::OctreeNodeInfo
OctreeNodeInfo(const Eigen::Vector3d &origin, const double &size, const size_t &depth, const size_t &child_index)
Parameterized Constructor.
Definition: Octree.h:46
CV_TYPES::POINT_OCTREE2
@ POINT_OCTREE2
Definition: CVTypes.h:157
size_
int64_t size_
Definition: FilePLY.cpp:37
cloudViewer::geometry::OctreeColorLeafNode
OctreeColorLeafNode class is an OctreeLeafNode containing color.
Definition: Octree.h:183
ccBBox
Bounding box structure.
Definition: ecvBBox.h:25
cloudViewer::utility::operator==
constexpr bool operator==(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:615
ccPointCloud
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
Definition: ecvPointCloud.h:146
cloudViewer::geometry::OctreeNode
The base class for octree node.
Definition: Octree.h:75
color
math::float4 color
Definition: LineSetBuffers.cpp:46
cloudViewer::geometry::OctreeNode::OctreeNode
OctreeNode()
Default Constructor.
Definition: Octree.h:79
cloudViewer::geometry::Octree::origin_
Eigen::Vector3d origin_
Definition: Octree.h:321
cloudViewer::geometry::OctreeNodeInfo::depth_
size_t depth_
Depth of the node to the root. The root is of depth 0.
Definition: Octree.h:62
cloudViewer::geometry::OctreePointColorLeafNode::indices_
std::vector< size_t > indices_
Associated point indices with this node.
Definition: Octree.h:243
CV_CLASS_ENUM
int64_t CV_CLASS_ENUM
Type of object type flags (64 bits)
Definition: CVTypes.h:97
cloudViewer::geometry::OctreeNodeInfo
OctreeNode's information.
Definition: Octree.h:33
cloudViewer::utility::IJsonConvertible
Definition: IJsonConvertible.h:41
size
int size
Definition: FileIOFactory.cpp:130
cloudViewer::geometry::OctreeNode::~OctreeNode
virtual ~OctreeNode()
Definition: Octree.h:80
cloudViewer::geometry::OctreeNodeInfo::child_index_
size_t child_index_
Definition: Octree.h:65
cloudViewer::geometry::OctreeNodeInfo::OctreeNodeInfo
OctreeNodeInfo()
Default Constructor.
Definition: Octree.h:38
cloudViewer::geometry::Octree::Octree
Octree(const size_t &max_depth, const Eigen::Vector3d &origin, const double &size, const char *name="Octree2")
Parameterized Constructor.
Definition: Octree.h:265
cloudViewer::geometry::Octree::max_depth_
size_t max_depth_
Definition: Octree.h:329
cloudViewer::geometry::OctreeLeafNode
OctreeLeafNode base class.
Definition: Octree.h:173
cloudViewer::geometry::Octree
Octree datastructure.
Definition: Octree.h:249
cloudViewer::geometry::OctreeNodeInfo::size_
double size_
Size of the node.
Definition: Octree.h:60
origin
Eigen::Vector3d origin
Definition: VoxelGridIO.cpp:26
CV_db.h
cloudViewer::geometry::Octree::~Octree
~Octree() override
Definition: Octree.h:274
cloudViewer::geometry::OctreeNodeInfo::~OctreeNodeInfo
~OctreeNodeInfo()
Definition: Octree.h:54
cloudViewer::geometry::Octree::IsEmpty
virtual bool IsEmpty() const override
Definition: Octree.h:288
cloudViewer::geometry::Octree::isSerializable
virtual bool isSerializable() const override
Returns whether object is serializable of not.
Definition: Octree.h:277
cloudViewer::geometry::Octree::size_
double size_
Definition: Octree.h:325
ecvOrientedBBox
Definition: ecvOrientedBBox.h:29
ccHObject
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
CV_DB_LIB_API
#define CV_DB_LIB_API
Definition: CV_db.h:15
Value
static vtkPVTrivialProducerStaticInternal Value
Definition: vtkDistributedTrivialProducer.cxx:65
ecvHObject.h
IJsonConvertible.h
cloudViewer::geometry::OctreeNodeInfo::origin_
Eigen::Vector3d origin_
Origin coordinate of the node.
Definition: Octree.h:58
name
std::string name
Definition: FileIOFactory.cpp:129
cloudViewer::geometry::Octree::Octree
Octree(const size_t &max_depth, const char *name="Octree2")
Parameterized Constructor.
Definition: Octree.h:258