ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
VoxelGrid.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 <Helper.h>
11 #include <Logging.h>
12 
13 #include <Eigen/Core>
14 #include <memory>
15 #include <unordered_map>
16 #include <vector>
17 
18 #include "ecvHObject.h"
19 
20 class ccPointCloud;
21 class ccMesh;
22 class ccBBox;
23 class ecvOrientedBBox;
24 
25 namespace cloudViewer {
26 namespace camera {
27 class PinholeCameraParameters;
28 }
29 
30 namespace geometry {
31 
32 class Octree;
33 class Image;
34 
39 public:
41  Voxel() {}
45  Voxel(const Eigen::Vector3i &grid_index) : grid_index_(grid_index) {}
50  Voxel(const Eigen::Vector3i &grid_index, const Eigen::Vector3d &color)
51  : grid_index_(grid_index), color_(color) {}
52  ~Voxel() {}
53 
54 public:
56  Eigen::Vector3i grid_index_ = Eigen::Vector3i(0, 0, 0);
58  Eigen::Vector3d color_ = Eigen::Vector3d(0, 0, 0);
59 };
60 
65 public:
67  VoxelGrid(const char *name = "VoxelGrid") : ccHObject(name) {}
69  VoxelGrid(const VoxelGrid &src_voxel_grid, const char *name = "VoxelGrid");
70  ~VoxelGrid() override {}
71 
72  // inherited methods (ccHObject)
73  virtual bool isSerializable() const override { return true; }
74 
76  virtual CV_CLASS_ENUM getClassID() const override {
77  return CV_TYPES::VOXEL_GRID;
78  }
79  virtual ccBBox getOwnBB(bool withGLFeatures = false) override;
80 
82  inline virtual bool IsEmpty() const override { return !HasVoxels(); }
83  virtual Eigen::Vector3d GetMinBound() const override;
84  virtual Eigen::Vector3d GetMaxBound() const override;
85  virtual Eigen::Vector3d GetCenter() const override;
90  std::vector<Eigen::Vector3d> GetAllVoxelCorners() const;
91  virtual ccBBox GetAxisAlignedBoundingBox() const override;
92  virtual ecvOrientedBBox GetOrientedBoundingBox() const override;
93  virtual VoxelGrid &Transform(
94  const Eigen::Matrix4d &transformation) override;
95  virtual VoxelGrid &Translate(const Eigen::Vector3d &translation,
96  bool relative = true) override;
97  virtual VoxelGrid &Scale(const double s,
98  const Eigen::Vector3d &center) override;
99  virtual VoxelGrid &Rotate(const Eigen::Matrix3d &R,
100  const Eigen::Vector3d &center) override;
101 
102  VoxelGrid &operator+=(const VoxelGrid &voxelgrid);
103  VoxelGrid operator+(const VoxelGrid &voxelgrid) const;
104 
106  bool HasVoxels() const { return voxels_.size() > 0; }
108  bool HasColors() const {
109  return true; // By default, the colors are (0, 0, 0)
110  }
112  Eigen::Vector3i GetVoxel(const Eigen::Vector3d &point) const;
113 
115  Eigen::Vector3d GetVoxelCenterCoordinate(const Eigen::Vector3i &idx) const {
116  auto it = voxels_.find(idx);
117  if (it != voxels_.end()) {
118  auto voxel = it->second;
119  Eigen::Vector3d local = (voxel.grid_index_.cast<double>() +
120  Eigen::Vector3d(0.5, 0.5, 0.5)) *
121  voxel_size_;
122  return origin_ + rotation_ * local;
123  } else {
124  return Eigen::Vector3d::Zero();
125  }
126  }
127 
129  void AddVoxel(const Voxel &voxel);
130 
132  void RemoveVoxel(const Eigen::Vector3i &idx);
133 
135  std::vector<Eigen::Vector3d> GetVoxelBoundingPoints(
136  const Eigen::Vector3i &index) const;
137 
140  std::vector<bool> CheckIfIncluded(
141  const std::vector<Eigen::Vector3d> &queries);
142 
153  const Image &depth_map,
154  const camera::PinholeCameraParameters &camera_parameter,
155  bool keep_voxels_outside_image);
156 
167  const Image &silhouette_mask,
168  const camera::PinholeCameraParameters &camera_parameter,
169  bool keep_voxels_outside_image);
170 
175 
179  std::shared_ptr<geometry::Octree> ToOctree(const size_t &max_depth) const;
180 
190  static std::shared_ptr<VoxelGrid> CreateDense(const Eigen::Vector3d &origin,
191  const Eigen::Vector3d &color,
192  double voxel_size,
193  double width,
194  double height,
195  double depth);
196 
200  enum class VoxelPoolingMode { AVG, MIN, MAX, SUM };
201 
211  static std::shared_ptr<VoxelGrid> CreateFromPointCloud(
212  const ccPointCloud &input,
213  double voxel_size,
214  VoxelPoolingMode color_mode = VoxelPoolingMode::AVG);
215 
227  static std::shared_ptr<VoxelGrid> CreateFromPointCloudWithinBounds(
228  const ccPointCloud &input,
229  double voxel_size,
230  const Eigen::Vector3d &min_bound,
231  const Eigen::Vector3d &max_bound,
232  VoxelPoolingMode color_mode = VoxelPoolingMode::AVG);
233 
240  static std::shared_ptr<VoxelGrid> CreateFromTriangleMesh(
241  const ccMesh &input, double voxel_size);
242 
251  static std::shared_ptr<VoxelGrid> CreateFromTriangleMeshWithinBounds(
252  const ccMesh &input,
253  double voxel_size,
254  const Eigen::Vector3d &min_bound,
255  const Eigen::Vector3d &max_bound);
256 
260  std::vector<Voxel> GetVoxels() const;
261 
262 public:
264  double voxel_size_ = 0.0;
266  Eigen::Vector3d origin_ = Eigen::Vector3d::Zero();
269  Eigen::Matrix3d rotation_ = Eigen::Matrix3d::Identity();
271  std::unordered_map<Eigen::Vector3i,
272  Voxel,
275 };
276 
282 public:
284  : num_of_points_(0),
285  color_(0.0, 0.0, 0.0),
286  min_color_(Eigen::Vector3d::Constant(
287  std::numeric_limits<double>::max())),
288  max_color_(Eigen::Vector3d::Constant(
289  std::numeric_limits<double>::lowest())) {}
290 
291 public:
292  void Add(const Eigen::Vector3i &voxel_index) {
293  if (num_of_points_ > 0 && voxel_index != voxel_index_) {
295  "Tried to aggregate ColorVoxel with different "
296  "voxel_index");
297  }
298  voxel_index_ = voxel_index;
299  }
300 
301  void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color) {
302  Add(voxel_index);
303  color_ += color;
304  num_of_points_++;
305  }
306 
307  Eigen::Vector3i GetVoxelIndex() const { return voxel_index_; }
308 
309  Eigen::Vector3d GetAverageColor() const {
310  if (num_of_points_ > 0) {
311  return color_ / double(num_of_points_);
312  } else {
313  return color_;
314  }
315  }
316 
317  Eigen::Vector3d GetMinColor() const { return min_color_; }
318 
319  Eigen::Vector3d GetMaxColor() const { return max_color_; }
320 
321  Eigen::Vector3d GetSumColor() const { return color_; }
322 
323 public:
326  Eigen::Vector3d color_;
327 
328 private:
329  Eigen::Vector3d min_color_;
330  Eigen::Vector3d max_color_;
331 };
332 
333 } // namespace geometry
334 } // 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
void Add(const double in1[2], const double in2[2], double out[2])
Definition: Factor.cpp:130
int width
std::string name
int height
math::float4 color
Bounding box structure.
Definition: ecvBBox.h:25
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
Triangular mesh.
Definition: ecvMesh.h:35
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
Contains both intrinsic and extrinsic pinhole camera parameters.
Class to aggregate color values from different votes in one voxel Computes the average color value in...
Definition: VoxelGrid.h:281
Eigen::Vector3d GetSumColor() const
Definition: VoxelGrid.h:321
void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color)
Definition: VoxelGrid.h:301
Eigen::Vector3d GetAverageColor() const
Definition: VoxelGrid.h:309
void Add(const Eigen::Vector3i &voxel_index)
Definition: VoxelGrid.h:292
Eigen::Vector3d GetMaxColor() const
Definition: VoxelGrid.h:319
Eigen::Vector3d GetMinColor() const
Definition: VoxelGrid.h:317
Eigen::Vector3i GetVoxelIndex() const
Definition: VoxelGrid.h:307
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:33
Octree datastructure.
Definition: Octree.h:250
VoxelGrid is a collection of voxels which are aligned in grid.
Definition: VoxelGrid.h:64
void CreateFromOctree(const Octree &octree)
virtual ccBBox GetAxisAlignedBoundingBox() const override
Returns an axis-aligned bounding box of the geometry.
virtual ccBBox getOwnBB(bool withGLFeatures=false) override
Returns the entity's own bounding-box.
std::unordered_map< Eigen::Vector3i, Voxel, cloudViewer::utility::hash_eigen< Eigen::Vector3i > > voxels_
Voxels contained in voxel grid.
Definition: VoxelGrid.h:274
VoxelGrid & CarveDepthMap(const Image &depth_map, const camera::PinholeCameraParameters &camera_parameter, bool keep_voxels_outside_image)
std::vector< Eigen::Vector3d > GetAllVoxelCorners() const
Returns the 3D coordinates of all corners of every voxel in the grid.
static std::shared_ptr< VoxelGrid > CreateFromTriangleMesh(const ccMesh &input, double voxel_size)
Eigen::Vector3d GetVoxelCenterCoordinate(const Eigen::Vector3i &idx) const
Function that returns the 3d coordinates of the queried voxel center.
Definition: VoxelGrid.h:115
VoxelGrid & CarveSilhouette(const Image &silhouette_mask, const camera::PinholeCameraParameters &camera_parameter, bool keep_voxels_outside_image)
static std::shared_ptr< VoxelGrid > CreateFromPointCloud(const ccPointCloud &input, double voxel_size, VoxelPoolingMode color_mode=VoxelPoolingMode::AVG)
virtual VoxelGrid & Rotate(const Eigen::Matrix3d &R, const Eigen::Vector3d &center) override
Apply rotation to the geometry coordinates and normals. Given a rotation matrix , and center ,...
virtual bool IsEmpty() const override
Definition: VoxelGrid.h:82
bool HasColors() const
Returns true if the voxel grid contains voxel colors.
Definition: VoxelGrid.h:108
bool HasVoxels() const
Returns true if the voxel grid contains voxels.
Definition: VoxelGrid.h:106
std::shared_ptr< geometry::Octree > ToOctree(const size_t &max_depth) const
static std::shared_ptr< VoxelGrid > CreateFromPointCloudWithinBounds(const ccPointCloud &input, double voxel_size, const Eigen::Vector3d &min_bound, const Eigen::Vector3d &max_bound, VoxelPoolingMode color_mode=VoxelPoolingMode::AVG)
VoxelGrid operator+(const VoxelGrid &voxelgrid) const
std::vector< bool > CheckIfIncluded(const std::vector< Eigen::Vector3d > &queries)
VoxelPoolingMode
Possible ways of determining voxel color from PointCloud.
Definition: VoxelGrid.h:200
VoxelGrid(const VoxelGrid &src_voxel_grid, const char *name="VoxelGrid")
Copy Constructor.
virtual VoxelGrid & 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 Eigen::Vector3d GetMinBound() const override
Returns min bounds for geometry coordinates.
VoxelGrid(const char *name="VoxelGrid")
Default Constructor.
Definition: VoxelGrid.h:67
std::vector< Eigen::Vector3d > GetVoxelBoundingPoints(const Eigen::Vector3i &index) const
Return a vector of 3D coordinates that define the indexed voxel cube.
virtual VoxelGrid & Translate(const Eigen::Vector3d &translation, bool relative=true) override
Apply translation to the geometry coordinates.
Eigen::Vector3i GetVoxel(const Eigen::Vector3d &point) const
Returns voxel index given query point.
VoxelGrid & operator+=(const VoxelGrid &voxelgrid)
virtual VoxelGrid & Transform(const Eigen::Matrix4d &transformation) override
Apply transformation (4x4 matrix) to the geometry coordinates.
static std::shared_ptr< VoxelGrid > CreateDense(const Eigen::Vector3d &origin, const Eigen::Vector3d &color, double voxel_size, double width, double height, double depth)
void RemoveVoxel(const Eigen::Vector3i &idx)
Remove a voxel with specified grid index.
virtual bool isSerializable() const override
Returns whether object is serializable of not.
Definition: VoxelGrid.h:73
void AddVoxel(const Voxel &voxel)
Add a voxel with specified grid index and color.
virtual ecvOrientedBBox GetOrientedBoundingBox() const override
virtual Eigen::Vector3d GetMaxBound() const override
Returns max bounds for geometry coordinates.
std::vector< Voxel > GetVoxels() const
static std::shared_ptr< VoxelGrid > CreateFromTriangleMeshWithinBounds(const ccMesh &input, double voxel_size, const Eigen::Vector3d &min_bound, const Eigen::Vector3d &max_bound)
virtual CV_CLASS_ENUM getClassID() const override
Returns unique class ID.
Definition: VoxelGrid.h:76
Base Voxel class, containing grid id and color.
Definition: VoxelGrid.h:38
Voxel()
Default Constructor.
Definition: VoxelGrid.h:41
Voxel(const Eigen::Vector3i &grid_index)
Parameterized Constructor.
Definition: VoxelGrid.h:45
Voxel(const Eigen::Vector3i &grid_index, const Eigen::Vector3d &color)
Parameterized Constructor.
Definition: VoxelGrid.h:50
#define LogWarning(...)
Definition: Logging.h:72
int max(int a, int b)
Definition: cutil_math.h:48
Helper functions for the ml ops.
@ VOXEL_GRID
Definition: CVTypes.h:151
Definition: Eigen.h:103
Generic file read and write utility for python interface.
Eigen::Matrix< Index, 3, 1 > Vector3i
Definition: knncpp.h:30
Definition: Eigen.h:85
cloudViewer::DgmOctree * octree
#define MAX(A, B)
Definition: sqlite3.c:14251
#define MIN(A, B)
Definition: sqlite3.c:14248
Definition: lsd.c:149
#define local
Definition: unzip.c:93