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 <vector>
32 
34 #include "Open3D/Utility/Console.h"
35 
36 namespace open3d {
37 
38 namespace camera {
39 class PinholeCameraParameters;
40 }
41 
42 namespace geometry {
43 
44 class PointCloud;
45 class TriangleMesh;
46 class Octree;
47 class Image;
48 
49 class Voxel {
50 public:
51  Voxel() {}
52  Voxel(const Eigen::Vector3i &grid_index) : grid_index_(grid_index) {}
53  Voxel(const Eigen::Vector3i &grid_index, const Eigen::Vector3d &color)
54  : grid_index_(grid_index), color_(color) {}
55  ~Voxel() {}
56 
57 public:
58  Eigen::Vector3i grid_index_ = Eigen::Vector3i(0, 0, 0);
59  Eigen::Vector3d color_ = Eigen::Vector3d(0, 0, 0);
60 };
61 
62 class VoxelGrid : public Geometry3D {
63 public:
65  VoxelGrid(const VoxelGrid &src_voxel_grid);
66  ~VoxelGrid() override {}
67 
68  VoxelGrid &Clear() override;
69  bool IsEmpty() const override;
70  Eigen::Vector3d GetMinBound() const override;
71  Eigen::Vector3d GetMaxBound() const override;
72  Eigen::Vector3d GetCenter() const override;
73  AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const override;
74  OrientedBoundingBox GetOrientedBoundingBox() const override;
75  VoxelGrid &Transform(const Eigen::Matrix4d &transformation) override;
76  VoxelGrid &Translate(const Eigen::Vector3d &translation,
77  bool relative = true) override;
78  VoxelGrid &Scale(const double scale, bool center = true) override;
79  VoxelGrid &Rotate(const Eigen::Vector3d &rotation,
80  bool center = true,
81  RotationType type = RotationType::XYZ) override;
82 
83  VoxelGrid &operator+=(const VoxelGrid &voxelgrid);
84  VoxelGrid operator+(const VoxelGrid &voxelgrid) const;
85 
86  bool HasVoxels() const { return voxels_.size() > 0; }
87  bool HasColors() const {
88  return true; // By default, the colors are (0, 0, 0)
89  }
90  Eigen::Vector3i GetVoxel(const Eigen::Vector3d &point) const;
91 
92  // Function that returns the 3d coordinates of the queried voxel center
93  Eigen::Vector3d GetVoxelCenterCoordinate(int idx) const {
94  const Eigen::Vector3i &grid_index = voxels_[idx].grid_index_;
95  return ((grid_index.cast<double>() + Eigen::Vector3d(0.5, 0.5, 0.5)) *
96  voxel_size_) +
97  origin_;
98  }
99 
101  std::vector<Eigen::Vector3d> GetVoxelBoundingPoints(int index) const;
102 
107  VoxelGrid &CarveDepthMap(
108  const Image &depth_map,
109  const camera::PinholeCameraParameters &camera_parameter);
110 
115  VoxelGrid &CarveSilhouette(
116  const Image &silhouette_mask,
117  const camera::PinholeCameraParameters &camera_parameter);
118 
119  void CreateFromOctree(const Octree &octree);
120 
121  std::shared_ptr<geometry::Octree> ToOctree(const size_t &max_depth) const;
122 
123  // Creates a voxel grid where every voxel is set (hence dense). This is a
124  // useful starting point for voxel carving.
125  static std::shared_ptr<VoxelGrid> CreateDense(const Eigen::Vector3d &origin,
126  double voxel_size,
127  double width,
128  double height,
129  double depth);
130 
131  // Creates a VoxelGrid from a given PointCloud. The color value of a given
132  // voxel is the average color value of the points that fall into it (if the
133  // PointCloud has colors).
134  // The bounds of the created VoxelGrid are computed from the PointCloud.
135  static std::shared_ptr<VoxelGrid> CreateFromPointCloud(
136  const PointCloud &input, double voxel_size);
137 
138  // Creates a VoxelGrid from a given PointCloud. The color value of a given
139  // voxel is the average color value of the points that fall into it (if the
140  // PointCloud has colors).
141  // The bounds of the created VoxelGrid are defined by the given parameters.
142  static std::shared_ptr<VoxelGrid> CreateFromPointCloudWithinBounds(
143  const PointCloud &input,
144  double voxel_size,
145  const Eigen::Vector3d &min_bound,
146  const Eigen::Vector3d &max_bound);
147 
148  // Creates a VoxelGrid from a given TriangleMesh. No color information is
149  // converted. The bounds of the created VoxelGrid are computed from the
150  // TriangleMesh..
151  static std::shared_ptr<VoxelGrid> CreateFromTriangleMesh(
152  const TriangleMesh &input, double voxel_size);
153 
154  // Creates a VoxelGrid from a given TriangleMesh. No color information is
155  // converted. The bounds of the created VoxelGrid are defined by the given
156  // parameters..
157  static std::shared_ptr<VoxelGrid> CreateFromTriangleMeshWithinBounds(
158  const TriangleMesh &input,
159  double voxel_size,
160  const Eigen::Vector3d &min_bound,
161  const Eigen::Vector3d &max_bound);
162 
163 public:
164  double voxel_size_;
165  Eigen::Vector3d origin_;
166  std::vector<Voxel> voxels_;
167 };
168 
172 public:
173  AvgColorVoxel() : num_of_points_(0), color_(0.0, 0.0, 0.0) {}
174 
175 public:
176  void Add(const Eigen::Vector3i &voxel_index) {
177  if (num_of_points_ > 0 && voxel_index != voxel_index_) {
179  "Tried to aggregate ColorVoxel with different "
180  "voxel_index\n");
181  }
182  voxel_index_ = voxel_index;
183  }
184 
185  void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color) {
186  Add(voxel_index);
187  color_ += color;
188  num_of_points_++;
189  }
190 
191  Eigen::Vector3i GetVoxelIndex() const { return voxel_index_; }
192 
193  Eigen::Vector3d GetAverageColor() const {
194  if (num_of_points_ > 0) {
195  return color_ / double(num_of_points_);
196  } else {
197  return color_;
198  }
199  }
200 
201 public:
203  Eigen::Vector3i voxel_index_;
204  Eigen::Vector3d color_;
205 };
206 
207 } // namespace geometry
208 } // namespace open3d
int num_of_points_
Definition: DownSample.cpp:77
double voxel_size_
Definition: VoxelGrid.h:164
Definition: Geometry.h:32
void Add(const Eigen::Vector3i &voxel_index)
Definition: VoxelGrid.h:176
Definition: BoundingVolume.h:90
void Add(const Eigen::Vector3i &voxel_index, const Eigen::Vector3d &color)
Definition: VoxelGrid.h:185
Voxel(const Eigen::Vector3i &grid_index)
Definition: VoxelGrid.h:52
void LogWarning(const char *format, const Args &... args)
Definition: Console.h:219
Eigen::Vector3d color_
Definition: VoxelGrid.h:204
Definition: BoundingVolume.h:38
Eigen::Vector3i voxel_index_
Definition: VoxelGrid.h:203
Definition: PointCloud.h:50
long voxel_index
Definition: FilePLY.cpp:284
bool HasVoxels() const
Definition: VoxelGrid.h:86
VoxelGrid()
Definition: VoxelGrid.h:64
RotationType
Definition: Geometry3D.h:43
~VoxelGrid() override
Definition: VoxelGrid.h:66
AvgColorVoxel()
Definition: VoxelGrid.h:173
Definition: Geometry3D.h:41
Definition: VoxelGrid.h:171
~Voxel()
Definition: VoxelGrid.h:55
Eigen::Vector3d origin_
Definition: VoxelGrid.h:165
Eigen::Vector3d color_
Definition: DownSample.cpp:80
bool HasColors() const
Definition: VoxelGrid.h:87
char type
Definition: FilePCD.cpp:56
Definition: PinholeCameraParameters.h:37
Definition: PinholeCameraIntrinsic.cpp:34
Definition: VoxelGrid.h:62
GeometryType
Definition: Geometry.h:34
int height
Definition: FilePCD.cpp:68
Eigen::Vector3d GetAverageColor() const
Definition: VoxelGrid.h:193
Definition: Octree.h:129
std::vector< Voxel > voxels_
Definition: VoxelGrid.h:166
int num_of_points_
Definition: VoxelGrid.h:202
Definition: VoxelGrid.h:49
Eigen::Vector3i GetVoxelIndex() const
Definition: VoxelGrid.h:191
Voxel()
Definition: VoxelGrid.h:51
Definition: TriangleMesh.h:43
Voxel(const Eigen::Vector3i &grid_index, const Eigen::Vector3d &color)
Definition: VoxelGrid.h:53
Definition: Image.h:49
Eigen::Vector3d GetVoxelCenterCoordinate(int idx) const
Definition: VoxelGrid.h:93
int width
Definition: FilePCD.cpp:67