Open3D (C++ API)
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
37 #include "Open3D/Utility/Helper.h"
38 
39 namespace open3d {
40 
41 namespace camera {
42 class PinholeCameraParameters;
43 }
44 
45 namespace geometry {
46 
47 class PointCloud;
48 class TriangleMesh;
49 class Octree;
50 class Image;
51 
52 class Voxel {
53 public:
54  Voxel() {}
55  Voxel(const Eigen::Vector3i &grid_index) : grid_index_(grid_index) {}
56  Voxel(const Eigen::Vector3i &grid_index, const Eigen::Vector3d &color)
57  : grid_index_(grid_index), color_(color) {}
58  ~Voxel() {}
59 
60 public:
61  Eigen::Vector3i grid_index_ = Eigen::Vector3i(0, 0, 0);
62  Eigen::Vector3d color_ = Eigen::Vector3d(0, 0, 0);
63 };
64 
65 class VoxelGrid : public Geometry3D {
66 public:
68  VoxelGrid(const VoxelGrid &src_voxel_grid);
69  ~VoxelGrid() override {}
70 
71  VoxelGrid &Clear() override;
72  bool IsEmpty() const override;
73  Eigen::Vector3d GetMinBound() const override;
74  Eigen::Vector3d GetMaxBound() const override;
75  Eigen::Vector3d GetCenter() const override;
76  AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const override;
77  OrientedBoundingBox GetOrientedBoundingBox() const override;
78  VoxelGrid &Transform(const Eigen::Matrix4d &transformation) override;
79  VoxelGrid &Translate(const Eigen::Vector3d &translation,
80  bool relative = true) override;
81  VoxelGrid &Scale(const double scale, bool center = true) override;
82  VoxelGrid &Rotate(const Eigen::Matrix3d &R, bool center = true) override;
83 
84  VoxelGrid &operator+=(const VoxelGrid &voxelgrid);
85  VoxelGrid operator+(const VoxelGrid &voxelgrid) const;
86 
87  bool HasVoxels() const { return voxels_.size() > 0; }
88  bool HasColors() const {
89  return true; // By default, the colors are (0, 0, 0)
90  }
91  Eigen::Vector3i GetVoxel(const Eigen::Vector3d &point) const;
92 
93  // Function that returns the 3d coordinates of the queried voxel center
94  Eigen::Vector3d GetVoxelCenterCoordinate(const Eigen::Vector3i &idx) const {
95  auto it = voxels_.find(idx);
96  if (it != voxels_.end()) {
97  auto voxel = it->second;
98  return ((voxel.grid_index_.cast<double>() +
99  Eigen::Vector3d(0.5, 0.5, 0.5)) *
100  voxel_size_) +
101  origin_;
102  } else {
103  return Eigen::Vector3d::Zero();
104  }
105  }
106 
108  void AddVoxel(const Voxel &voxel);
109 
111  std::vector<Eigen::Vector3d> GetVoxelBoundingPoints(
112  const Eigen::Vector3i &index) const;
113 
114  // Element-wise check if a query in the list is included in the VoxelGrid
115  // Queries are double precision and are mapped to the closest voxel.
116  std::vector<bool> CheckIfIncluded(
117  const std::vector<Eigen::Vector3d> &queries);
118 
123  VoxelGrid &CarveDepthMap(
124  const Image &depth_map,
125  const camera::PinholeCameraParameters &camera_parameter);
126 
131  VoxelGrid &CarveSilhouette(
132  const Image &silhouette_mask,
133  const camera::PinholeCameraParameters &camera_parameter);
134 
135  void CreateFromOctree(const Octree &octree);
136 
137  std::shared_ptr<geometry::Octree> ToOctree(const size_t &max_depth) const;
138 
139  // Creates a voxel grid where every voxel is set (hence dense). This is a
140  // useful starting point for voxel carving.
141  static std::shared_ptr<VoxelGrid> CreateDense(const Eigen::Vector3d &origin,
142  double voxel_size,
143  double width,
144  double height,
145  double depth);
146 
147  // Creates a VoxelGrid from a given PointCloud. The color value of a given
148  // voxel is the average color value of the points that fall into it (if the
149  // PointCloud has colors).
150  // The bounds of the created VoxelGrid are computed from the PointCloud.
151  static std::shared_ptr<VoxelGrid> CreateFromPointCloud(
152  const PointCloud &input, double voxel_size);
153 
154  // Creates a VoxelGrid from a given PointCloud. The color value of a given
155  // voxel is the average color value of the points that fall into it (if the
156  // PointCloud has colors).
157  // The bounds of the created VoxelGrid are defined by the given parameters.
158  static std::shared_ptr<VoxelGrid> CreateFromPointCloudWithinBounds(
159  const PointCloud &input,
160  double voxel_size,
161  const Eigen::Vector3d &min_bound,
162  const Eigen::Vector3d &max_bound);
163 
164  // Creates a VoxelGrid from a given TriangleMesh. No color information is
165  // converted. The bounds of the created VoxelGrid are computed from the
166  // TriangleMesh..
167  static std::shared_ptr<VoxelGrid> CreateFromTriangleMesh(
168  const TriangleMesh &input, double voxel_size);
169 
170  // Creates a VoxelGrid from a given TriangleMesh. No color information is
171  // converted. The bounds of the created VoxelGrid are defined by the given
172  // parameters..
173  static std::shared_ptr<VoxelGrid> CreateFromTriangleMeshWithinBounds(
174  const TriangleMesh &input,
175  double voxel_size,
176  const Eigen::Vector3d &min_bound,
177  const Eigen::Vector3d &max_bound);
178 
179 public:
180  double voxel_size_ = 0.0;
181  Eigen::Vector3d origin_ = Eigen::Vector3d::Zero();
182  std::unordered_map<Eigen::Vector3i,
183  Voxel,
186 };
187 
191 public:
192  AvgColorVoxel() : num_of_points_(0), color_(0.0, 0.0, 0.0) {}
193 
194 public:
195  void Add(const Eigen::Vector3i &voxel_index) {
196  if (num_of_points_ > 0 && voxel_index != voxel_index_) {
198  "Tried to aggregate ColorVoxel with different "
199  "voxel_index");
200  }
201  voxel_index_ = voxel_index;
202  }
203 
204  void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color) {
205  Add(voxel_index);
206  color_ += color;
207  num_of_points_++;
208  }
209 
210  Eigen::Vector3i GetVoxelIndex() const { return voxel_index_; }
211 
212  Eigen::Vector3d GetAverageColor() const {
213  if (num_of_points_ > 0) {
214  return color_ / double(num_of_points_);
215  } else {
216  return color_;
217  }
218  }
219 
220 public:
222  Eigen::Vector3i voxel_index_;
223  Eigen::Vector3d color_;
224 };
225 
226 } // namespace geometry
227 } // namespace open3d
int num_of_points_
Definition: DownSample.cpp:78
Definition: Helper.h:90
The base geometry class.
Definition: Geometry.h:35
void Add(const Eigen::Vector3i &voxel_index)
Definition: VoxelGrid.h:195
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:130
void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color)
Definition: VoxelGrid.h:204
Voxel(const Eigen::Vector3i &grid_index)
Definition: VoxelGrid.h:55
void LogWarning(const char *format, const Args &... args)
Definition: Console.h:206
Eigen::Vector3d color_
Definition: VoxelGrid.h:223
A bounding box oriented along an arbitrary frame of reference.
Definition: BoundingVolume.h:44
Eigen::Vector3i voxel_index_
Definition: VoxelGrid.h:222
Definition: PointCloud.h:50
Eigen::Vector3d GetVoxelCenterCoordinate(const Eigen::Vector3i &idx) const
Definition: VoxelGrid.h:94
long voxel_index
Definition: FilePLY.cpp:286
bool HasVoxels() const
Definition: VoxelGrid.h:87
VoxelGrid()
Definition: VoxelGrid.h:67
~VoxelGrid() override
Definition: VoxelGrid.h:69
double voxel_size
Definition: FilePLY.cpp:285
AvgColorVoxel()
Definition: VoxelGrid.h:192
The base geometry class for 3D geometries.
Definition: Geometry3D.h:46
Definition: VoxelGrid.h:190
~Voxel()
Definition: VoxelGrid.h:58
Eigen::Vector3d color_
Definition: DownSample.cpp:81
bool HasColors() const
Definition: VoxelGrid.h:88
Contains both intrinsic and extrinsic pinhole camera parameters.
Definition: PinholeCameraParameters.h:40
Eigen::Vector3d origin
Definition: FilePLY.cpp:284
Definition: PinholeCameraIntrinsic.cpp:34
Definition: VoxelGrid.h:65
GeometryType
Specifies possible geometry types.
Definition: Geometry.h:40
int height
Definition: FilePCD.cpp:69
Eigen::Vector3d GetAverageColor() const
Definition: VoxelGrid.h:212
Definition: Octree.h:129
int num_of_points_
Definition: VoxelGrid.h:221
Definition: VoxelGrid.h:52
Eigen::Vector3i GetVoxelIndex() const
Definition: VoxelGrid.h:210
Voxel()
Definition: VoxelGrid.h:54
Definition: TriangleMesh.h:46
Voxel(const Eigen::Vector3i &grid_index, const Eigen::Vector3d &color)
Definition: VoxelGrid.h:56
The Image class stores image with customizable width, height, num of channels and bytes per channel...
Definition: Image.h:53
std::unordered_map< Eigen::Vector3i, Voxel, utility::hash_eigen::hash< Eigen::Vector3i > > voxels_
Definition: VoxelGrid.h:185
int width
Definition: FilePCD.cpp:68