Open3D (C++ API)  0.19.0
VoxelGrid.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <Eigen/Core>
11 #include <memory>
12 #include <unordered_map>
13 #include <vector>
14 
16 #include "open3d/utility/Helper.h"
17 #include "open3d/utility/Logging.h"
18 
19 namespace open3d {
20 
21 namespace camera {
22 class PinholeCameraParameters;
23 }
24 
25 namespace geometry {
26 
27 class PointCloud;
28 class TriangleMesh;
29 class Octree;
30 class Image;
31 
35 class Voxel {
36 public:
38  Voxel() {}
42  Voxel(const Eigen::Vector3i &grid_index) : grid_index_(grid_index) {}
47  Voxel(const Eigen::Vector3i &grid_index, const Eigen::Vector3d &color)
48  : grid_index_(grid_index), color_(color) {}
49  ~Voxel() {}
50 
51 public:
53  Eigen::Vector3i grid_index_ = Eigen::Vector3i(0, 0, 0);
55  Eigen::Vector3d color_ = Eigen::Vector3d(0, 0, 0);
56 };
57 
61 class VoxelGrid : public Geometry3D {
62 public:
66  VoxelGrid(const VoxelGrid &src_voxel_grid);
67  ~VoxelGrid() override {}
68 
69  VoxelGrid &Clear() override;
70  bool IsEmpty() const override;
71  Eigen::Vector3d GetMinBound() const override;
72  Eigen::Vector3d GetMaxBound() const override;
73  Eigen::Vector3d GetCenter() const override;
74 
79  std::vector<Eigen::Vector3d> GetAllVoxelCorners() const;
83 
87  bool robust = false) const override;
88 
92  bool robust = false) const override;
93 
94  VoxelGrid &Transform(const Eigen::Matrix4d &transformation) override;
95  VoxelGrid &Translate(const Eigen::Vector3d &translation,
96  bool relative = true) override;
97  VoxelGrid &Scale(const double scale,
98  const Eigen::Vector3d &center) override;
99  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 
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 PointCloud &input,
213  double voxel_size,
215 
227  static std::shared_ptr<VoxelGrid> CreateFromPointCloudWithinBounds(
228  const PointCloud &input,
229  double voxel_size,
230  const Eigen::Vector3d &min_bound,
231  const Eigen::Vector3d &max_bound,
233 
240  static std::shared_ptr<VoxelGrid> CreateFromTriangleMesh(
241  const TriangleMesh &input, double voxel_size);
242 
251  static std::shared_ptr<VoxelGrid> CreateFromTriangleMeshWithinBounds(
252  const TriangleMesh &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:
283  AvgColorVoxel() : num_of_points_(0), color_(0.0, 0.0, 0.0) {}
284 
285 public:
286  void Add(const Eigen::Vector3i &voxel_index) {
287  if (num_of_points_ > 0 && voxel_index != voxel_index_) {
289  "Tried to aggregate ColorVoxel with different "
290  "voxel_index");
291  }
292  voxel_index_ = voxel_index;
293  }
294 
295  void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color) {
296  Add(voxel_index);
297  color_ += color;
298  num_of_points_++;
299  }
300 
301  Eigen::Vector3i GetVoxelIndex() const { return voxel_index_; }
302 
303  Eigen::Vector3d GetAverageColor() const {
304  if (num_of_points_ > 0) {
305  return color_ / double(num_of_points_);
306  } else {
307  return color_;
308  }
309  }
310 
311 public:
313  Eigen::Vector3i voxel_index_;
314  Eigen::Vector3d color_;
315 };
316 
322 public:
324  : num_of_points_(0),
325  color_(0.0, 0.0, 0.0),
326  min_color_(Eigen::Vector3d::Constant(
327  std::numeric_limits<double>::max())),
328  max_color_(Eigen::Vector3d::Constant(
329  std::numeric_limits<double>::lowest())) {}
330 
331 public:
332  void Add(const Eigen::Vector3i &voxel_index) {
333  if (num_of_points_ > 0 && voxel_index != voxel_index_) {
335  "Tried to aggregate ColorVoxel with different "
336  "voxel_index");
337  }
338  voxel_index_ = voxel_index;
339  }
340 
341  void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color) {
342  Add(voxel_index);
343  color_ += color;
344  num_of_points_++;
345  min_color_ = min_color_.cwiseMin(color);
346  max_color_ = max_color_.cwiseMax(color);
347  }
348 
349  Eigen::Vector3i GetVoxelIndex() const { return voxel_index_; }
350 
351  Eigen::Vector3d GetAverageColor() const {
352  if (num_of_points_ > 0) {
353  return color_ / double(num_of_points_);
354  } else {
355  return color_;
356  }
357  }
358 
359  Eigen::Vector3d GetMinColor() const { return min_color_; }
360 
361  Eigen::Vector3d GetMaxColor() const { return max_color_; }
362 
363  Eigen::Vector3d GetSumColor() const { return color_; }
364 
365 public:
367  Eigen::Vector3i voxel_index_;
368  Eigen::Vector3d color_;
369 
370 private:
371  Eigen::Vector3d min_color_;
372  Eigen::Vector3d max_color_;
373 };
374 
375 } // namespace geometry
376 } // namespace open3d
math::float4 color
Definition: LineSetBuffers.cpp:45
#define LogWarning(...)
Definition: Logging.h:63
Point< Real, 3 > point
Definition: SurfaceReconstructionPoisson.cpp:163
Contains both intrinsic and extrinsic pinhole camera parameters.
Definition: PinholeCameraParameters.h:21
Class to aggregate color values from different votes in one voxel Can be used to compute min,...
Definition: VoxelGrid.h:321
Eigen::Vector3d color_
Definition: VoxelGrid.h:368
void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color)
Definition: VoxelGrid.h:341
AggColorVoxel()
Definition: VoxelGrid.h:323
Eigen::Vector3d GetMaxColor() const
Definition: VoxelGrid.h:361
Eigen::Vector3i GetVoxelIndex() const
Definition: VoxelGrid.h:349
Eigen::Vector3i voxel_index_
Definition: VoxelGrid.h:367
Eigen::Vector3d GetSumColor() const
Definition: VoxelGrid.h:363
void Add(const Eigen::Vector3i &voxel_index)
Definition: VoxelGrid.h:332
Eigen::Vector3d GetAverageColor() const
Definition: VoxelGrid.h:351
int num_of_points_
Definition: VoxelGrid.h:366
Eigen::Vector3d GetMinColor() const
Definition: VoxelGrid.h:359
Class to aggregate color values from different votes in one voxel Computes the average color value in...
Definition: VoxelGrid.h:281
AvgColorVoxel()
Definition: VoxelGrid.h:283
void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color)
Definition: VoxelGrid.h:295
Eigen::Vector3d color_
Definition: VoxelGrid.h:314
void Add(const Eigen::Vector3i &voxel_index)
Definition: VoxelGrid.h:286
Eigen::Vector3i GetVoxelIndex() const
Definition: VoxelGrid.h:301
int num_of_points_
Definition: VoxelGrid.h:312
Eigen::Vector3i voxel_index_
Definition: VoxelGrid.h:313
Eigen::Vector3d GetAverageColor() const
Definition: VoxelGrid.h:303
A bounding box that is aligned along the coordinate axes and defined by the min_bound and max_bound.
Definition: BoundingVolume.h:172
The base geometry class for 3D geometries.
Definition: Geometry3D.h:28
The base geometry class.
Definition: Geometry.h:18
GeometryType
Specifies possible geometry types.
Definition: Geometry.h:23
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:34
Octree datastructure.
Definition: Octree.h:244
A bounding box oriented along an arbitrary frame of reference.
Definition: BoundingVolume.h:25
A point cloud consists of point coordinates, and optionally point colors and point normals.
Definition: PointCloud.h:36
Triangle mesh contains vertices and triangles represented by the indices to the vertices.
Definition: TriangleMesh.h:35
VoxelGrid is a collection of voxels which are aligned in grid.
Definition: VoxelGrid.h:61
static std::shared_ptr< VoxelGrid > CreateFromTriangleMesh(const TriangleMesh &input, double voxel_size)
Definition: VoxelGridFactory.cpp:166
AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const override
Definition: VoxelGrid.cpp:108
void CreateFromOctree(const Octree &octree)
Definition: VoxelGrid.cpp:250
VoxelGrid & CarveSilhouette(const Image &silhouette_mask, const camera::PinholeCameraParameters &camera_parameter, bool keep_voxels_outside_image)
Definition: VoxelGrid.cpp:340
VoxelPoolingMode
Possible ways of determining voxel color from PointCloud.
Definition: VoxelGrid.h:200
VoxelGrid operator+(const VoxelGrid &voxelgrid) const
Definition: VoxelGrid.cpp:204
std::vector< Eigen::Vector3d > GetVoxelBoundingPoints(const Eigen::Vector3i &index) const
Return a vector of 3D coordinates that define the indexed voxel cube.
Definition: VoxelGrid.cpp:215
VoxelGrid()
Default Constructor.
Definition: VoxelGrid.h:64
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 ,...
Definition: VoxelGrid.cpp:145
OrientedBoundingBox GetOrientedBoundingBox(bool robust=false) const override
Definition: VoxelGrid.cpp:115
std::unordered_map< Eigen::Vector3i, Voxel, utility::hash_eigen< Eigen::Vector3i > > voxels_
Voxels contained in voxel grid.
Definition: VoxelGrid.h:274
bool IsEmpty() const override
Returns true iff the geometry is empty.
Definition: VoxelGrid.cpp:37
Eigen::Vector3d GetVoxelCenterCoordinate(const Eigen::Vector3i &idx) const
Function that returns the 3d coordinates of the queried voxel center.
Definition: VoxelGrid.h:115
Eigen::Vector3d origin_
Coordinate of the origin point.
Definition: VoxelGrid.h:266
VoxelGrid & Clear() override
Clear all elements in the geometry.
Definition: VoxelGrid.cpp:30
VoxelGrid & CarveDepthMap(const Image &depth_map, const camera::PinholeCameraParameters &camera_parameter, bool keep_voxels_outside_image)
Definition: VoxelGrid.cpp:296
VoxelGrid & Scale(const double scale, const Eigen::Vector3d &center) override
Apply scaling to the geometry coordinates. Given a scaling factor , and center , a given point is tr...
Definition: VoxelGrid.cpp:139
Eigen::Vector3d GetMinBound() const override
Returns min bounds for geometry coordinates.
Definition: VoxelGrid.cpp:39
bool HasVoxels() const
Returns true if the voxel grid contains voxels.
Definition: VoxelGrid.h:106
std::vector< bool > CheckIfIncluded(const std::vector< Eigen::Vector3d > &queries)
Definition: VoxelGrid.cpp:237
VoxelGrid & Transform(const Eigen::Matrix4d &transformation) override
Apply transformation (4x4 matrix) to the geometry coordinates.
Definition: VoxelGrid.cpp:125
bool HasColors() const
Returns true if the voxel grid contains voxel colors.
Definition: VoxelGrid.h:108
Eigen::Vector3d GetMaxBound() const override
Returns max bounds for geometry coordinates.
Definition: VoxelGrid.cpp:54
std::vector< Eigen::Vector3d > GetAllVoxelCorners() const
Returns the 3D coordinates of all corners of every voxel in the grid.
Definition: VoxelGrid.cpp:69
static std::shared_ptr< VoxelGrid > CreateFromTriangleMeshWithinBounds(const TriangleMesh &input, double voxel_size, const Eigen::Vector3d &min_bound, const Eigen::Vector3d &max_bound)
Definition: VoxelGridFactory.cpp:108
Eigen::Vector3d GetCenter() const override
Returns the center of the geometry coordinates.
Definition: VoxelGrid.cpp:92
static std::shared_ptr< VoxelGrid > CreateFromPointCloudWithinBounds(const PointCloud &input, double voxel_size, const Eigen::Vector3d &min_bound, const Eigen::Vector3d &max_bound, VoxelPoolingMode color_mode=VoxelPoolingMode::AVG)
Definition: VoxelGridFactory.cpp:44
void AddVoxel(const Voxel &voxel)
Add a voxel with specified grid index and color.
Definition: VoxelGrid.cpp:231
VoxelGrid & Translate(const Eigen::Vector3d &translation, bool relative=true) override
Apply translation to the geometry coordinates.
Definition: VoxelGrid.cpp:133
std::vector< Voxel > GetVoxels() const
Definition: VoxelGrid.cpp:384
double voxel_size_
Size of the voxel.
Definition: VoxelGrid.h:264
static std::shared_ptr< VoxelGrid > CreateDense(const Eigen::Vector3d &origin, const Eigen::Vector3d &color, double voxel_size, double width, double height, double depth)
Definition: VoxelGridFactory.cpp:21
static std::shared_ptr< VoxelGrid > CreateFromPointCloud(const PointCloud &input, double voxel_size, VoxelPoolingMode color_mode=VoxelPoolingMode::AVG)
Definition: VoxelGridFactory.cpp:97
void RemoveVoxel(const Eigen::Vector3i &idx)
Remove a voxel with specified grid index.
Definition: VoxelGrid.cpp:235
VoxelGrid & operator+=(const VoxelGrid &voxelgrid)
Definition: VoxelGrid.cpp:153
Eigen::Vector3i GetVoxel(const Eigen::Vector3d &point) const
Returns voxel index given query point.
Definition: VoxelGrid.cpp:208
OrientedBoundingBox GetMinimalOrientedBoundingBox(bool robust=false) const override
Definition: VoxelGrid.cpp:119
std::shared_ptr< geometry::Octree > ToOctree(const size_t &max_depth) const
Definition: VoxelGrid.cpp:289
Eigen::Matrix3d rotation_
Definition: VoxelGrid.h:269
~VoxelGrid() override
Definition: VoxelGrid.h:67
Base Voxel class, containing grid id and color.
Definition: VoxelGrid.h:35
~Voxel()
Definition: VoxelGrid.h:49
Eigen::Vector3i grid_index_
Grid coordinate index of the voxel.
Definition: VoxelGrid.h:53
Voxel(const Eigen::Vector3i &grid_index, const Eigen::Vector3d &color)
Parameterized Constructor.
Definition: VoxelGrid.h:47
Eigen::Vector3d color_
Color of the voxel.
Definition: VoxelGrid.h:55
Voxel(const Eigen::Vector3i &grid_index)
Parameterized Constructor.
Definition: VoxelGrid.h:42
Voxel()
Default Constructor.
Definition: VoxelGrid.h:38
int width
Definition: FilePCD.cpp:52
int height
Definition: FilePCD.cpp:53
Definition: NonRigidOptimizer.cpp:22
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Device.h:111
Definition: Helper.h:71