Open3D (C++ API)  0.16.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-2021 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
37
38namespace open3d {
39
40namespace camera {
41class PinholeCameraParameters;
42}
43
44namespace geometry {
45
46class PointCloud;
47class TriangleMesh;
48class Octree;
49class Image;
50
54class Voxel {
55public:
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
70public:
72 Eigen::Vector3i grid_index_ = Eigen::Vector3i(0, 0, 0);
74 Eigen::Vector3d color_ = Eigen::Vector3d(0, 0, 0);
75};
76
80class VoxelGrid : public Geometry3D {
81public:
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;
95 bool robust = false) const override;
96 VoxelGrid &Transform(const Eigen::Matrix4d &transformation) override;
97 VoxelGrid &Translate(const Eigen::Vector3d &translation,
98 bool relative = true) override;
99 VoxelGrid &Scale(const double scale,
100 const Eigen::Vector3d &center) override;
101 VoxelGrid &Rotate(const Eigen::Matrix3d &R,
102 const Eigen::Vector3d &center) override;
103
104 VoxelGrid &operator+=(const VoxelGrid &voxelgrid);
105 VoxelGrid operator+(const VoxelGrid &voxelgrid) const;
106
108 bool HasVoxels() const { return voxels_.size() > 0; }
110 bool HasColors() const {
111 return true; // By default, the colors are (0, 0, 0)
112 }
114 Eigen::Vector3i GetVoxel(const Eigen::Vector3d &point) const;
115
117 Eigen::Vector3d GetVoxelCenterCoordinate(const Eigen::Vector3i &idx) const {
118 auto it = voxels_.find(idx);
119 if (it != voxels_.end()) {
120 auto voxel = it->second;
121 return ((voxel.grid_index_.cast<double>() +
122 Eigen::Vector3d(0.5, 0.5, 0.5)) *
123 voxel_size_) +
124 origin_;
125 } else {
126 return Eigen::Vector3d::Zero();
127 }
128 }
129
131 void AddVoxel(const Voxel &voxel);
132
134 std::vector<Eigen::Vector3d> GetVoxelBoundingPoints(
135 const Eigen::Vector3i &index) const;
136
139 std::vector<bool> CheckIfIncluded(
140 const std::vector<Eigen::Vector3d> &queries);
141
152 const Image &depth_map,
153 const camera::PinholeCameraParameters &camera_parameter,
154 bool keep_voxels_outside_image);
155
166 const Image &silhouette_mask,
167 const camera::PinholeCameraParameters &camera_parameter,
168 bool keep_voxels_outside_image);
169
173 void CreateFromOctree(const Octree &octree);
174
178 std::shared_ptr<geometry::Octree> ToOctree(const size_t &max_depth) const;
179
189 static std::shared_ptr<VoxelGrid> CreateDense(const Eigen::Vector3d &origin,
190 const Eigen::Vector3d &color,
191 double voxel_size,
192 double width,
193 double height,
194 double depth);
195
203 static std::shared_ptr<VoxelGrid> CreateFromPointCloud(
204 const PointCloud &input, double voxel_size);
205
215 static std::shared_ptr<VoxelGrid> CreateFromPointCloudWithinBounds(
216 const PointCloud &input,
217 double voxel_size,
218 const Eigen::Vector3d &min_bound,
219 const Eigen::Vector3d &max_bound);
220
227 static std::shared_ptr<VoxelGrid> CreateFromTriangleMesh(
228 const TriangleMesh &input, double voxel_size);
229
238 static std::shared_ptr<VoxelGrid> CreateFromTriangleMeshWithinBounds(
239 const TriangleMesh &input,
240 double voxel_size,
241 const Eigen::Vector3d &min_bound,
242 const Eigen::Vector3d &max_bound);
243
247 std::vector<Voxel> GetVoxels() const;
248
249public:
251 double voxel_size_ = 0.0;
253 Eigen::Vector3d origin_ = Eigen::Vector3d::Zero();
255 std::unordered_map<Eigen::Vector3i,
256 Voxel,
259};
260
266public:
267 AvgColorVoxel() : num_of_points_(0), color_(0.0, 0.0, 0.0) {}
268
269public:
270 void Add(const Eigen::Vector3i &voxel_index) {
271 if (num_of_points_ > 0 && voxel_index != voxel_index_) {
273 "Tried to aggregate ColorVoxel with different "
274 "voxel_index");
275 }
276 voxel_index_ = voxel_index;
277 }
278
279 void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color) {
280 Add(voxel_index);
281 color_ += color;
283 }
284
285 Eigen::Vector3i GetVoxelIndex() const { return voxel_index_; }
286
287 Eigen::Vector3d GetAverageColor() const {
288 if (num_of_points_ > 0) {
289 return color_ / double(num_of_points_);
290 } else {
291 return color_;
292 }
293 }
294
295public:
297 Eigen::Vector3i voxel_index_;
298 Eigen::Vector3d color_;
299};
300
301} // namespace geometry
302} // namespace open3d
math::float4 color
Definition: LineSetBuffers.cpp:64
#define LogWarning(...)
Definition: Logging.h:79
Contains both intrinsic and extrinsic pinhole camera parameters.
Definition: PinholeCameraParameters.h:40
Class to aggregate color values from different votes in one voxel Computes the average color value in...
Definition: VoxelGrid.h:265
AvgColorVoxel()
Definition: VoxelGrid.h:267
void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color)
Definition: VoxelGrid.h:279
Eigen::Vector3d color_
Definition: VoxelGrid.h:298
void Add(const Eigen::Vector3i &voxel_index)
Definition: VoxelGrid.h:270
Eigen::Vector3i GetVoxelIndex() const
Definition: VoxelGrid.h:285
int num_of_points_
Definition: VoxelGrid.h:296
Eigen::Vector3i voxel_index_
Definition: VoxelGrid.h:297
Eigen::Vector3d GetAverageColor() const
Definition: VoxelGrid.h:287
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:155
The base geometry class for 3D geometries.
Definition: Geometry3D.h:47
The base geometry class.
Definition: Geometry.h:37
GeometryType
Specifies possible geometry types.
Definition: Geometry.h:42
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:53
Octree datastructure.
Definition: Octree.h:263
A bounding box oriented along an arbitrary frame of reference.
Definition: BoundingVolume.h:44
A point cloud consists of point coordinates, and optionally point colors and point normals.
Definition: PointCloud.h:55
Triangle mesh contains vertices and triangles represented by the indices to the vertices.
Definition: TriangleMesh.h:54
VoxelGrid is a collection of voxels which are aligned in grid.
Definition: VoxelGrid.h:80
static std::shared_ptr< VoxelGrid > CreateFromTriangleMesh(const TriangleMesh &input, double voxel_size)
Definition: VoxelGridFactory.cpp:165
AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const override
Returns an axis-aligned bounding box of the geometry.
Definition: VoxelGrid.cpp:100
void CreateFromOctree(const Octree &octree)
Definition: VoxelGrid.cpp:227
VoxelGrid & CarveSilhouette(const Image &silhouette_mask, const camera::PinholeCameraParameters &camera_parameter, bool keep_voxels_outside_image)
Definition: VoxelGrid.cpp:317
VoxelGrid operator+(const VoxelGrid &voxelgrid) const
Definition: VoxelGrid.cpp:185
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:194
VoxelGrid()
Default Constructor.
Definition: VoxelGrid.h:83
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:128
OrientedBoundingBox GetOrientedBoundingBox(bool robust=false) const override
Definition: VoxelGrid.cpp:107
std::unordered_map< Eigen::Vector3i, Voxel, utility::hash_eigen< Eigen::Vector3i > > voxels_
Voxels contained in voxel grid.
Definition: VoxelGrid.h:258
bool IsEmpty() const override
Returns true iff the geometry is empty.
Definition: VoxelGrid.cpp:55
Eigen::Vector3d GetVoxelCenterCoordinate(const Eigen::Vector3i &idx) const
Function that returns the 3d coordinates of the queried voxel center.
Definition: VoxelGrid.h:117
Eigen::Vector3d origin_
Coorindate of the origin point.
Definition: VoxelGrid.h:253
VoxelGrid & Clear() override
Clear all elements in the geometry.
Definition: VoxelGrid.cpp:48
static std::shared_ptr< VoxelGrid > CreateFromPointCloudWithinBounds(const PointCloud &input, double voxel_size, const Eigen::Vector3d &min_bound, const Eigen::Vector3d &max_bound)
Definition: VoxelGridFactory.cpp:63
VoxelGrid & CarveDepthMap(const Image &depth_map, const camera::PinholeCameraParameters &camera_parameter, bool keep_voxels_outside_image)
Definition: VoxelGrid.cpp:273
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:123
Eigen::Vector3d GetMinBound() const override
Returns min bounds for geometry coordinates.
Definition: VoxelGrid.cpp:57
bool HasVoxels() const
Returns true if the voxel grid contains voxels.
Definition: VoxelGrid.h:108
std::vector< bool > CheckIfIncluded(const std::vector< Eigen::Vector3d > &queries)
Definition: VoxelGrid.cpp:214
VoxelGrid & Transform(const Eigen::Matrix4d &transformation) override
Apply transformation (4x4 matrix) to the geometry coordinates.
Definition: VoxelGrid.cpp:112
bool HasColors() const
Returns true if the voxel grid contains voxel colors.
Definition: VoxelGrid.h:110
Eigen::Vector3d GetMaxBound() const override
Returns max bounds for geometry coordinates.
Definition: VoxelGrid.cpp:70
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:118
Eigen::Vector3d GetCenter() const override
Returns the center of the geometry coordinates.
Definition: VoxelGrid.cpp:84
void AddVoxel(const Voxel &voxel)
Add a voxel with specified grid index and color.
Definition: VoxelGrid.cpp:210
VoxelGrid & Translate(const Eigen::Vector3d &translation, bool relative=true) override
Apply translation to the geometry coordinates.
Definition: VoxelGrid.cpp:117
std::vector< Voxel > GetVoxels() const
Definition: VoxelGrid.cpp:361
double voxel_size_
Size of the voxel.
Definition: VoxelGrid.h:251
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:40
VoxelGrid & operator+=(const VoxelGrid &voxelgrid)
Definition: VoxelGrid.cpp:134
static std::shared_ptr< VoxelGrid > CreateFromPointCloud(const PointCloud &input, double voxel_size)
Definition: VoxelGridFactory.cpp:109
Eigen::Vector3i GetVoxel(const Eigen::Vector3d &point) const
Returns voxel index given query point.
Definition: VoxelGrid.cpp:189
std::shared_ptr< geometry::Octree > ToOctree(const size_t &max_depth) const
Definition: VoxelGrid.cpp:266
~VoxelGrid() override
Definition: VoxelGrid.h:86
Base Voxel class, containing grid id and color.
Definition: VoxelGrid.h:54
~Voxel()
Definition: VoxelGrid.h:68
Eigen::Vector3i grid_index_
Grid coordinate index of the voxel.
Definition: VoxelGrid.h:72
Voxel(const Eigen::Vector3i &grid_index, const Eigen::Vector3d &color)
Parameterized Constructor.
Definition: VoxelGrid.h:66
Eigen::Vector3d color_
Color of the voxel.
Definition: VoxelGrid.h:74
Voxel(const Eigen::Vector3i &grid_index)
Parameterized Constructor.
Definition: VoxelGrid.h:61
Voxel()
Default Constructor.
Definition: VoxelGrid.h:57
int width
Definition: FilePCD.cpp:71
int height
Definition: FilePCD.cpp:72
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Helper.h:90