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:
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 
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
int64_t CV_CLASS_ENUM
Type of object type flags (64 bits)
Definition: CVTypes.h:97
#define CV_DB_LIB_API
Definition: CV_db.h:15
int size
std::string name
int64_t size_
Definition: FilePLY.cpp:37
math::float4 color
Bounding box structure.
Definition: ecvBBox.h:25
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
OctreeColorLeafNode class is an OctreeLeafNode containing color.
Definition: Octree.h:183
static std::function< void(std::shared_ptr< OctreeLeafNode >)> GetUpdateFunction(const Eigen::Vector3d &color)
Get lambda function for updating OctreeLeafNode.
std::shared_ptr< OctreeLeafNode > Clone() const override
Clone this OctreeLeafNode.
bool operator==(const OctreeLeafNode &other) const override
bool ConvertFromJsonValue(const Json::Value &value) override
bool ConvertToJsonValue(Json::Value &value) const override
static std::function< std::shared_ptr< OctreeLeafNode >)> GetInitFunction()
Get lambda function for initializing OctreeLeafNode.
OctreeLeafNode base class.
Definition: Octree.h:173
virtual std::shared_ptr< OctreeLeafNode > Clone() const =0
Clone this OctreeLeafNode.
virtual bool operator==(const OctreeLeafNode &other) const =0
OctreeNode's information.
Definition: Octree.h:33
OctreeNodeInfo(const Eigen::Vector3d &origin, const double &size, const size_t &depth, const size_t &child_index)
Parameterized Constructor.
Definition: Octree.h:46
OctreeNodeInfo()
Default Constructor.
Definition: Octree.h:38
double size_
Size of the node.
Definition: Octree.h:60
Eigen::Vector3d origin_
Origin coordinate of the node.
Definition: Octree.h:58
size_t depth_
Depth of the node to the root. The root is of depth 0.
Definition: Octree.h:62
The base class for octree node.
Definition: Octree.h:75
OctreeNode()
Default Constructor.
Definition: Octree.h:79
static std::shared_ptr< OctreeNode > ConstructFromJsonValue(const Json::Value &value)
Factory function to construct an OctreeNode by parsing the json value.
OctreePointColorLeafNode class is an OctreeColorLeafNode containing a list of indices corresponding t...
Definition: Octree.h:217
std::vector< size_t > indices_
Associated point indices with this node.
Definition: Octree.h:243
bool ConvertToJsonValue(Json::Value &value) const override
std::shared_ptr< OctreeLeafNode > Clone() const override
Clone this OctreeLeafNode.
bool ConvertFromJsonValue(const Json::Value &value) override
static std::function< void(std::shared_ptr< OctreeLeafNode >)> GetUpdateFunction(size_t index, const Eigen::Vector3d &color)
Get lambda function for updating OctreeLeafNode.
bool operator==(const OctreeLeafNode &other) const override
static std::function< std::shared_ptr< OctreeLeafNode >)> GetInitFunction()
Get lambda function for initializing OctreeLeafNode.
Octree datastructure.
Definition: Octree.h:250
void CreateFromVoxelGrid(const geometry::VoxelGrid &voxel_grid)
Convert from voxel grid.
virtual ccBBox GetAxisAlignedBoundingBox() const override
Returns an axis-aligned bounding box of the geometry.
std::pair< std::shared_ptr< OctreeLeafNode >, std::shared_ptr< OctreeNodeInfo > > LocateLeafNode(const Eigen::Vector3d &point) const
Returns leaf OctreeNode and OctreeNodeInfo where the querypoint should reside.
Octree(const char *name="Octree2")
Default Constructor.
Definition: Octree.h:253
virtual Octree & Scale(const double s, const Eigen::Vector3d &center) override
Apply scaling to the geometry coordinates. Given a scaling factor , and center , a given point is tr...
virtual Eigen::Vector3d GetCenter() const override
Returns the center of the geometry coordinates.
virtual Octree & Translate(const Eigen::Vector3d &translation, bool relative=true) override
Apply translation to the geometry coordinates.
bool ConvertToJsonValue(Json::Value &value) const override
virtual CV_CLASS_ENUM getClassID() const override
Returns unique class ID.
Definition: Octree.h:280
void ConvertFromPointCloud(const ccPointCloud &point_cloud, double size_expand=0.01)
Convert octree from point cloud.
bool operator==(const Octree &other) const
Returns true if the Octree is completely the same, used for testing.
virtual bool isSerializable() const override
Returns whether object is serializable of not.
Definition: Octree.h:277
virtual ecvOrientedBBox GetOrientedBoundingBox() const override
virtual bool IsEmpty() const override
Definition: Octree.h:288
void InsertPoint(const Eigen::Vector3d &point, const std::function< std::shared_ptr< OctreeLeafNode >()> &fl_init, const std::function< void(std::shared_ptr< OctreeLeafNode >)> &fl_update, const std::function< std::shared_ptr< OctreeInternalNode >()> &fi_init=nullptr, const std::function< void(std::shared_ptr< OctreeInternalNode >)> &fi_update=nullptr)
Insert a point to the octree.
void Traverse(const std::function< bool(const std::shared_ptr< OctreeNode > &, const std::shared_ptr< OctreeNodeInfo > &)> &f) const
Const version of Traverse. DFS traversal of Octree from the root, with callback function called for e...
std::shared_ptr< geometry::VoxelGrid > ToVoxelGrid() const
Convert to VoxelGrid.
virtual Octree & Rotate(const Eigen::Matrix3d &R, const Eigen::Vector3d &center) override
Apply rotation to the geometry coordinates and normals. Given a rotation matrix , and center ,...
Octree(const size_t &max_depth, const char *name="Octree2")
Parameterized Constructor.
Definition: Octree.h:258
void Traverse(const std::function< bool(const std::shared_ptr< OctreeNode > &, const std::shared_ptr< OctreeNodeInfo > &)> &f)
DFS traversal of Octree from the root, with callback function called for each node.
Octree(const Octree &src_octree, const char *name="Octree2")
Eigen::Vector3d origin_
Definition: Octree.h:321
virtual ccBBox getOwnBB(bool withGLFeatures=false) override
Returns the entity's own bounding-box.
Octree(const size_t &max_depth, const Eigen::Vector3d &origin, const double &size, const char *name="Octree2")
Parameterized Constructor.
Definition: Octree.h:265
static bool IsPointInBound(const Eigen::Vector3d &point, const Eigen::Vector3d &origin, const double &size)
Return true if point within bound, that is, origin <= point < origin + size.
virtual Eigen::Vector3d GetMinBound() const override
Returns min bounds for geometry coordinates.
virtual Eigen::Vector3d GetMaxBound() const override
Returns max bounds for geometry coordinates.
virtual Octree & Transform(const Eigen::Matrix4d &transformation) override
Apply transformation (4x4 matrix) to the geometry coordinates.
bool ConvertFromJsonValue(const Json::Value &value) override
VoxelGrid is a collection of voxels which are aligned in grid.
Definition: VoxelGrid.h:64
@ POINT_OCTREE2
Definition: CVTypes.h:157
Generic file read and write utility for python interface.
Definition: lsd.c:149