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 
81  VoxelGrid &Clear();
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 
152  VoxelGrid &CarveDepthMap(
153  const Image &depth_map,
154  const camera::PinholeCameraParameters &camera_parameter,
155  bool keep_voxels_outside_image);
156 
166  VoxelGrid &CarveSilhouette(
167  const Image &silhouette_mask,
168  const camera::PinholeCameraParameters &camera_parameter,
169  bool keep_voxels_outside_image);
170 
174  void CreateFromOctree(const Octree &octree);
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:
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
cloudViewer
Generic file read and write utility for python interface.
Definition: CloudViewerConfig.cpp:12
cloudViewer::geometry::AvgColorVoxel::Add
void Add(const Eigen::Vector3i &voxel_index)
Definition: VoxelGrid.h:292
cloudViewer::t::geometry::Transform
void Transform(benchmark::State &state, const core::Device &device)
Definition: PointCloud.cpp:127
Eigen
Definition: Eigen.h:103
cloudViewer::geometry::VoxelGrid
VoxelGrid is a collection of voxels which are aligned in grid.
Definition: VoxelGrid.h:64
cloudViewer::geometry::AvgColorVoxel::GetVoxelIndex
Eigen::Vector3i GetVoxelIndex() const
Definition: VoxelGrid.h:307
cloudViewer::geometry::Voxel::Voxel
Voxel(const Eigen::Vector3i &grid_index)
Parameterized Constructor.
Definition: VoxelGrid.h:45
ccBBox
Bounding box structure.
Definition: ecvBBox.h:25
cloudViewer::geometry::AvgColorVoxel::color_
Eigen::Vector3d color_
Definition: VoxelGrid.h:326
cloudViewer::geometry::VoxelGrid::~VoxelGrid
~VoxelGrid() override
Definition: VoxelGrid.h:70
ccPointCloud
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
Definition: ecvPointCloud.h:146
cloudViewer::geometry::AvgColorVoxel::AvgColorVoxel
AvgColorVoxel()
Definition: VoxelGrid.h:283
color
math::float4 color
Definition: LineSetBuffers.cpp:46
cloudViewer::geometry::AvgColorVoxel::GetMinColor
Eigen::Vector3d GetMinColor() const
Definition: VoxelGrid.h:317
voxel_index
long voxel_index
Definition: VoxelGridIO.cpp:29
cloudViewer::geometry::VoxelGrid::VoxelGrid
VoxelGrid(const char *name="VoxelGrid")
Default Constructor.
Definition: VoxelGrid.h:67
CV_TYPES::VOXEL_GRID
@ VOXEL_GRID
Definition: CVTypes.h:151
width
int width
Definition: FileIOFactory.cpp:142
CV_CLASS_ENUM
int64_t CV_CLASS_ENUM
Type of object type flags (64 bits)
Definition: CVTypes.h:97
cloudViewer::geometry::AvgColorVoxel::voxel_index_
Eigen::Vector3i voxel_index_
Definition: VoxelGrid.h:325
cloudViewer::camera::PinholeCameraParameters
Contains both intrinsic and extrinsic pinhole camera parameters.
Definition: PinholeCameraParameters.h:22
height
int height
Definition: FileIOFactory.cpp:143
cloudViewer::geometry::AvgColorVoxel
Class to aggregate color values from different votes in one voxel Computes the average color value in...
Definition: VoxelGrid.h:281
cloudViewer::geometry::Voxel::~Voxel
~Voxel()
Definition: VoxelGrid.h:52
cloudViewer::geometry::VoxelGrid::GetVoxelCenterCoordinate
Eigen::Vector3d GetVoxelCenterCoordinate(const Eigen::Vector3i &idx) const
Function that returns the 3d coordinates of the queried voxel center.
Definition: VoxelGrid.h:115
cloudViewer::ml::impl::MAX
@ MAX
Definition: VoxelPooling.h:20
cloudViewer::geometry::VoxelGrid::isSerializable
virtual bool isSerializable() const override
Returns whether object is serializable of not.
Definition: VoxelGrid.h:73
cloudViewer::geometry::Voxel::Voxel
Voxel()
Default Constructor.
Definition: VoxelGrid.h:41
cloudViewer::geometry::VoxelGrid::voxels_
std::unordered_map< Eigen::Vector3i, Voxel, cloudViewer::utility::hash_eigen< Eigen::Vector3i > > voxels_
Voxels contained in voxel grid.
Definition: VoxelGrid.h:274
cloudViewer::core::operator+
Tensor operator+(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1417
cloudViewer::utility::hash_eigen
Definition: Helper.h:73
cloudViewer::geometry::Voxel
Base Voxel class, containing grid id and color.
Definition: VoxelGrid.h:38
knncpp::Vector3i
Eigen::Matrix< Index, 3, 1 > Vector3i
Definition: knncpp.h:30
cloudViewer::geometry::Octree
Octree datastructure.
Definition: Octree.h:249
origin
Eigen::Vector3d origin
Definition: VoxelGridIO.cpp:26
cloudViewer::geometry::VoxelGrid::HasColors
bool HasColors() const
Returns true if the voxel grid contains voxel colors.
Definition: VoxelGrid.h:108
octree
cloudViewer::DgmOctree * octree
Definition: qCanupoTools.cpp:37
cloudViewer::geometry::AvgColorVoxel::GetSumColor
Eigen::Vector3d GetSumColor() const
Definition: VoxelGrid.h:321
cloudViewer::utility::operator+=
FN_SPECIFIERS void operator+=(MiniVec< T, N > &a, const MiniVec< T, N > &b)
Definition: MiniVec.h:150
cloudViewer::geometry::VoxelGrid::VoxelPoolingMode
VoxelPoolingMode
Possible ways of determining voxel color from PointCloud.
Definition: VoxelGrid.h:200
std
Definition: Eigen.h:85
ccMesh
Triangular mesh.
Definition: ecvMesh.h:35
ecvOrientedBBox
Definition: ecvOrientedBBox.h:29
ccHObject
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
LogWarning
#define LogWarning(...)
Definition: Logging.h:72
CV_DB_LIB_API
#define CV_DB_LIB_API
Definition: CV_db.h:15
cloudViewer::geometry::VoxelGrid::HasVoxels
bool HasVoxels() const
Returns true if the voxel grid contains voxels.
Definition: VoxelGrid.h:106
cloudViewer::geometry::VoxelGrid::getClassID
virtual CV_CLASS_ENUM getClassID() const override
Returns unique class ID.
Definition: VoxelGrid.h:76
ecvHObject.h
cloudViewer::geometry::AvgColorVoxel::Add
void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color)
Definition: VoxelGrid.h:301
cloudViewer::geometry::AvgColorVoxel::GetAverageColor
Eigen::Vector3d GetAverageColor() const
Definition: VoxelGrid.h:309
cloudViewer::geometry::AvgColorVoxel::GetMaxColor
Eigen::Vector3d GetMaxColor() const
Definition: VoxelGrid.h:319
cloudViewer::geometry::VoxelGrid::IsEmpty
virtual bool IsEmpty() const override
Definition: VoxelGrid.h:82
cloudViewer::geometry::Image
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:33
cloudViewer::geometry::AvgColorVoxel::num_of_points_
int num_of_points_
Definition: VoxelGrid.h:324
voxel_size
double voxel_size
Definition: VoxelGridIO.cpp:28
cloudViewer::geometry::Voxel::Voxel
Voxel(const Eigen::Vector3i &grid_index, const Eigen::Vector3d &color)
Parameterized Constructor.
Definition: VoxelGrid.h:50
name
std::string name
Definition: FileIOFactory.cpp:129