Open3D (C++ API)  0.12.0
VoxelGrid.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <Eigen/Core>
30 #include <memory>
31 #include <unordered_map>
32 #include <vector>
33 
35 #include "open3d/utility/Console.h"
36 #include "open3d/utility/Helper.h"
37 
38 namespace open3d {
39 
40 namespace camera {
41 class PinholeCameraParameters;
42 }
43 
44 namespace geometry {
45 
46 class PointCloud;
47 class TriangleMesh;
48 class Octree;
49 class Image;
50 
54 class Voxel {
55 public:
57  Voxel() {}
61  Voxel(const Eigen::Vector3i &grid_index) : grid_index_(grid_index) {}
66  Voxel(const Eigen::Vector3i &grid_index, const Eigen::Vector3d &color)
67  : grid_index_(grid_index), color_(color) {}
68  ~Voxel() {}
69 
70 public:
72  Eigen::Vector3i grid_index_ = Eigen::Vector3i(0, 0, 0);
74  Eigen::Vector3d color_ = Eigen::Vector3d(0, 0, 0);
75 };
76 
80 class VoxelGrid : public Geometry3D {
81 public:
85  VoxelGrid(const VoxelGrid &src_voxel_grid);
86  ~VoxelGrid() override {}
87 
88  VoxelGrid &Clear() override;
89  bool IsEmpty() const override;
90  Eigen::Vector3d GetMinBound() const override;
91  Eigen::Vector3d GetMaxBound() const override;
92  Eigen::Vector3d GetCenter() const override;
93  AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const override;
94  OrientedBoundingBox GetOrientedBoundingBox() const override;
95  VoxelGrid &Transform(const Eigen::Matrix4d &transformation) override;
96  VoxelGrid &Translate(const Eigen::Vector3d &translation,
97  bool relative = true) override;
98  VoxelGrid &Scale(const double scale,
99  const Eigen::Vector3d &center) override;
100  VoxelGrid &Rotate(const Eigen::Matrix3d &R,
101  const Eigen::Vector3d &center) override;
102 
103  VoxelGrid &operator+=(const VoxelGrid &voxelgrid);
104  VoxelGrid operator+(const VoxelGrid &voxelgrid) const;
105 
107  bool HasVoxels() const { return voxels_.size() > 0; }
109  bool HasColors() const {
110  return true; // By default, the colors are (0, 0, 0)
111  }
113  Eigen::Vector3i GetVoxel(const Eigen::Vector3d &point) const;
114 
116  Eigen::Vector3d GetVoxelCenterCoordinate(const Eigen::Vector3i &idx) const {
117  auto it = voxels_.find(idx);
118  if (it != voxels_.end()) {
119  auto voxel = it->second;
120  return ((voxel.grid_index_.cast<double>() +
121  Eigen::Vector3d(0.5, 0.5, 0.5)) *
122  voxel_size_) +
123  origin_;
124  } else {
125  return Eigen::Vector3d::Zero();
126  }
127  }
128 
130  void AddVoxel(const Voxel &voxel);
131 
133  std::vector<Eigen::Vector3d> GetVoxelBoundingPoints(
134  const Eigen::Vector3i &index) const;
135 
138  std::vector<bool> CheckIfIncluded(
139  const std::vector<Eigen::Vector3d> &queries);
140 
150  VoxelGrid &CarveDepthMap(
151  const Image &depth_map,
152  const camera::PinholeCameraParameters &camera_parameter,
153  bool keep_voxels_outside_image);
154 
164  VoxelGrid &CarveSilhouette(
165  const Image &silhouette_mask,
166  const camera::PinholeCameraParameters &camera_parameter,
167  bool keep_voxels_outside_image);
168 
172  void CreateFromOctree(const Octree &octree);
173 
177  std::shared_ptr<geometry::Octree> ToOctree(const size_t &max_depth) const;
178 
188  static std::shared_ptr<VoxelGrid> CreateDense(const Eigen::Vector3d &origin,
189  const Eigen::Vector3d &color,
190  double voxel_size,
191  double width,
192  double height,
193  double depth);
194 
202  static std::shared_ptr<VoxelGrid> CreateFromPointCloud(
203  const PointCloud &input, double voxel_size);
204 
214  static std::shared_ptr<VoxelGrid> CreateFromPointCloudWithinBounds(
215  const PointCloud &input,
216  double voxel_size,
217  const Eigen::Vector3d &min_bound,
218  const Eigen::Vector3d &max_bound);
219 
226  static std::shared_ptr<VoxelGrid> CreateFromTriangleMesh(
227  const TriangleMesh &input, double voxel_size);
228 
237  static std::shared_ptr<VoxelGrid> CreateFromTriangleMeshWithinBounds(
238  const TriangleMesh &input,
239  double voxel_size,
240  const Eigen::Vector3d &min_bound,
241  const Eigen::Vector3d &max_bound);
242 
246  std::vector<Voxel> GetVoxels() const;
247 
248 public:
250  double voxel_size_ = 0.0;
252  Eigen::Vector3d origin_ = Eigen::Vector3d::Zero();
254  std::unordered_map<Eigen::Vector3i,
255  Voxel,
258 };
259 
265 public:
266  AvgColorVoxel() : num_of_points_(0), color_(0.0, 0.0, 0.0) {}
267 
268 public:
269  void Add(const Eigen::Vector3i &voxel_index) {
270  if (num_of_points_ > 0 && voxel_index != voxel_index_) {
272  "Tried to aggregate ColorVoxel with different "
273  "voxel_index");
274  }
275  voxel_index_ = voxel_index;
276  }
277 
278  void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color) {
279  Add(voxel_index);
280  color_ += color;
281  num_of_points_++;
282  }
283 
284  Eigen::Vector3i GetVoxelIndex() const { return voxel_index_; }
285 
286  Eigen::Vector3d GetAverageColor() const {
287  if (num_of_points_ > 0) {
288  return color_ / double(num_of_points_);
289  } else {
290  return color_;
291  }
292  }
293 
294 public:
296  Eigen::Vector3i voxel_index_;
297  Eigen::Vector3d color_;
298 };
299 
300 } // namespace geometry
301 } // namespace open3d
Eigen::Vector3d color_
Definition: PointCloud.cpp:242
The base geometry class.
Definition: Geometry.h:37
void Add(const Eigen::Vector3i &voxel_index)
Definition: VoxelGrid.h:269
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:149
void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color)
Definition: VoxelGrid.h:278
Voxel(const Eigen::Vector3i &grid_index)
Parameterized Constructor.
Definition: VoxelGrid.h:61
void LogWarning(const char *format, const Args &... args)
Definition: Console.h:181
Eigen::Vector3d color_
Definition: VoxelGrid.h:297
A bounding box oriented along an arbitrary frame of reference.
Definition: BoundingVolume.h:44
Eigen::Vector3i voxel_index_
Definition: VoxelGrid.h:296
A point cloud consists of point coordinates, and optionally point colors and point normals...
Definition: PointCloud.h:54
Eigen::Vector3d GetVoxelCenterCoordinate(const Eigen::Vector3i &idx) const
Function that returns the 3d coordinates of the queried voxel center.
Definition: VoxelGrid.h:116
bool HasVoxels() const
Returns true if the voxel grid contains voxels.
Definition: VoxelGrid.h:107
VoxelGrid()
Default Constructor.
Definition: VoxelGrid.h:83
int num_of_points_
Definition: PointCloud.cpp:239
Definition: Helper.h:86
~VoxelGrid() override
Definition: VoxelGrid.h:86
math::float4 color
Definition: LineSetBuffers.cpp:64
AvgColorVoxel()
Definition: VoxelGrid.h:266
The base geometry class for 3D geometries.
Definition: Geometry3D.h:46
Class to aggregate color values from different votes in one voxel Computes the average color value in...
Definition: VoxelGrid.h:264
~Voxel()
Definition: VoxelGrid.h:68
bool HasColors() const
Returns true if the voxel grid contains voxel colors.
Definition: VoxelGrid.h:109
Contains both intrinsic and extrinsic pinhole camera parameters.
Definition: PinholeCameraParameters.h:40
std::unordered_map< Eigen::Vector3i, Voxel, utility::hash_eigen< Eigen::Vector3i > > voxels_
Voxels contained in voxel grid.
Definition: VoxelGrid.h:257
Definition: PinholeCameraIntrinsic.cpp:35
VoxelGrid is a collection of voxels which are aligned in grid.
Definition: VoxelGrid.h:80
GeometryType
Specifies possible geometry types.
Definition: Geometry.h:42
Tensor operator+(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1137
int height
Definition: FilePCD.cpp:72
Eigen::Vector3d GetAverageColor() const
Definition: VoxelGrid.h:286
Octree datastructure.
Definition: Octree.h:181
int num_of_points_
Definition: VoxelGrid.h:295
Base Voxel class, containing grid id and color.
Definition: VoxelGrid.h:54
Eigen::Vector3i GetVoxelIndex() const
Definition: VoxelGrid.h:284
Voxel()
Default Constructor.
Definition: VoxelGrid.h:57
Triangle mesh contains vertices and triangles represented by the indices to the vertices.
Definition: TriangleMesh.h:54
Voxel(const Eigen::Vector3i &grid_index, const Eigen::Vector3d &color)
Parameterized Constructor.
Definition: VoxelGrid.h:66
The Image class stores image with customizable width, height, num of channels and bytes per channel...
Definition: Image.h:53
int width
Definition: FilePCD.cpp:71