ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
voxelgrid.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include <Image.h>
9 #include <Octree.h>
10 #include <VoxelGrid.h>
12 #include <ecvPointCloud.h>
13 
14 #include <sstream>
15 
17 #include "pybind/docstring.h"
20 
21 namespace cloudViewer {
22 namespace geometry {
23 
24 void pybind_voxelgrid(py::module &m) {
25  py::class_<Voxel, std::shared_ptr<Voxel>> voxel(
26  m, "Voxel", "Base Voxel class, containing grid id and color");
27  py::detail::bind_default_constructor<Voxel>(voxel);
28  py::detail::bind_copy_functions<Voxel>(voxel);
29  voxel.def("__repr__",
30  [](const Voxel &voxel) {
31  std::ostringstream repr;
32  repr << "Voxel with grid_index: (" << voxel.grid_index_(0)
33  << ", " << voxel.grid_index_(1) << ", "
34  << voxel.grid_index_(2) << "), color: ("
35  << voxel.color_(0) << ", " << voxel.color_(1) << ", "
36  << voxel.color_(2) << ")";
37  return repr.str();
38  })
39  .def(py::init([](const Eigen::Vector3i &grid_index) {
40  return new Voxel(grid_index);
41  }),
42  "grid_index"_a)
43  .def(py::init([](const Eigen::Vector3i &grid_index,
44  const Eigen::Vector3d &color) {
45  return new Voxel(grid_index, color);
46  }),
47  "grid_index"_a, "color"_a)
48  .def_readwrite("grid_index", &Voxel::grid_index_,
49  "Int numpy array of shape (3,): Grid coordinate "
50  "index of the voxel.")
51  .def_readwrite(
52  "color", &Voxel::color_,
53  "Float64 numpy array of shape (3,): Color of the voxel.");
54 
55  py::class_<geometry::VoxelGrid, PyGeometry<geometry::VoxelGrid>,
56  std::shared_ptr<geometry::VoxelGrid>, ccHObject>
57  voxelgrid(m, "VoxelGrid",
58  "VoxelGrid is a collection of voxels which are aligned "
59  "in grid.");
60 
61  py::native_enum<geometry::VoxelGrid::VoxelPoolingMode>(
62  voxelgrid, "VoxelPoolingMode", "enum.Enum",
63  "Mode of determining color for each voxel.")
68  .finalize();
69  py::detail::bind_default_constructor<geometry::VoxelGrid>(voxelgrid);
70  py::detail::bind_copy_functions<geometry::VoxelGrid>(voxelgrid);
71  voxelgrid
72  .def("__repr__",
73  [](const VoxelGrid &voxelgrid) {
74  return std::string("VoxelGrid with ") +
75  std::to_string(voxelgrid.voxels_.size()) +
76  " voxels.";
77  })
78  .def(py::self + py::self)
79  .def(py::self += py::self)
80  .def("get_voxels", &VoxelGrid::GetVoxels,
81  "Returns List of ``Voxel``: Voxels contained in voxel grid. "
82  "Changes to the voxels returned from this method"
83  "are not reflected in the voxel grid.")
84  .def("has_colors", &VoxelGrid::HasColors,
85  "Returns ``True`` if the voxel grid contains voxel colors.")
86  .def("has_voxels", &VoxelGrid::HasVoxels,
87  "Returns ``True`` if the voxel grid contains voxels.")
88  .def("get_voxel", &VoxelGrid::GetVoxel, "point"_a,
89  "Returns voxel index given query point.")
90  .def("add_voxel", &VoxelGrid::AddVoxel, "voxel"_a,
91  "Add a new voxel into the VoxelGrid.")
92  .def("remove_voxel", &VoxelGrid::RemoveVoxel, "idx"_a,
93  "Remove a voxel given index.")
94  .def("check_if_included", &VoxelGrid::CheckIfIncluded, "queries"_a,
95  "Element-wise check if a query in the list is included in "
96  "the VoxelGrid. Queries are double precision and "
97  "are mapped to the closest voxel.")
98  .def("carve_depth_map", &VoxelGrid::CarveDepthMap, "depth_map"_a,
99  "camera_params"_a, "keep_voxels_outside_image"_a = false,
100  "Remove all voxels from the VoxelGrid where none of the "
101  "boundary points of the voxel projects to depth value that is "
102  "smaller, or equal than the projected depth of the boundary "
103  "point. If keep_voxels_outside_image is true then voxels are "
104  "only carved if all boundary points project to a valid image "
105  "location.")
106  .def("carve_silhouette", &VoxelGrid::CarveSilhouette,
107  "silhouette_mask"_a, "camera_params"_a,
108  "keep_voxels_outside_image"_a = false,
109  "Remove all voxels from the VoxelGrid where none of the "
110  "boundary points of the voxel projects to a valid mask pixel "
111  "(pixel value > 0). If keep_voxels_outside_image is true then "
112  "voxels are only carved if all boundary points project to a "
113  "valid image location.")
114  .def("to_octree", &VoxelGrid::ToOctree, "max_depth"_a,
115  "Convert to Octree.")
116  .def("create_from_octree", &VoxelGrid::CreateFromOctree, "octree"_a,
117  "Convert from Octree.")
118  .def_static("create_dense", &VoxelGrid::CreateDense,
119  "Creates a voxel grid where every voxel is set (hence "
120  "dense). This is a useful starting point for voxel "
121  "carving",
122  "origin"_a, "color"_a, "voxel_size"_a, "width"_a,
123  "height"_a, "depth"_a)
124  .def_static(
125  "create_from_point_cloud", &VoxelGrid::CreateFromPointCloud,
126  "Creates a VoxelGrid from a given PointCloud. The "
127  "color value of a given voxel is determined by the "
128  "VoxelPoolingMode, e.g. by default the average color "
129  "value of the points that fall into it (if the "
130  "PointCloud has colors). The bounds of the created "
131  "VoxelGrid are computed from the PointCloud.",
132  "input"_a, "voxel_size"_a,
133  py::arg_v("pooling_mode", VoxelGrid::VoxelPoolingMode::AVG,
134  "VoxelPoolingMode.AVG"))
135  .def_static(
136  "create_from_point_cloud_within_bounds",
138  "Creates a VoxelGrid from a given PointCloud. The "
139  "color value of a given voxel is determined by the "
140  "VoxelPoolingMode, e.g. by default the average color "
141  "value of the points that fall into it (if the "
142  "PointCloud has colors). The bounds of the created "
143  "VoxelGrid are defined by the given parameters.",
144  "input"_a, "voxel_size"_a, "min_bound"_a, "max_bound"_a,
145  py::arg_v("pooling_mode", VoxelGrid::VoxelPoolingMode::AVG,
146  "VoxelPoolingMode.AVG"))
147  .def_static("create_from_triangle_mesh",
149  "Creates a VoxelGrid from a given TriangleMesh. No "
150  "color information is converted. The bounds of the "
151  "created VoxelGrid are computed from the "
152  "TriangleMesh.",
153  "input"_a, "voxel_size"_a)
154  .def_static(
155  "create_from_triangle_mesh_within_bounds",
157  "Creates a VoxelGrid from a given TriangleMesh. No color "
158  "information is converted. The bounds "
159  "of the created VoxelGrid are defined by the given "
160  "parameters",
161  "input"_a, "voxel_size"_a, "min_bound"_a, "max_bound"_a)
162  .def_readwrite("origin", &VoxelGrid::origin_,
163  "``float64`` vector of length 3: Coorindate of the "
164  "origin point.")
165  .def_readwrite("rotation", &VoxelGrid::rotation_,
166  "``float64`` 3x3 matrix: Rotation matrix of the "
167  "origin point.")
168  .def_readwrite("voxel_size", &VoxelGrid::voxel_size_,
169  "``float64`` Size of the voxel.");
170  docstring::ClassMethodDocInject(m, "VoxelGrid", "has_colors");
171  docstring::ClassMethodDocInject(m, "VoxelGrid", "has_voxels");
172  docstring::ClassMethodDocInject(m, "VoxelGrid", "get_voxel",
173  {{"point", "The query point."}});
174  docstring::ClassMethodDocInject(m, "VoxelGrid", "add_voxel",
175  {{"Voxel", "A new voxel."}});
177  m, "VoxelGrid", "remove_voxel",
178  {{"idx", "The grid index of the target voxel."}});
180  m, "VoxelGrid", "check_if_included",
181  {{"query", "a list of voxel indices to check."}});
183  m, "VoxelGrid", "carve_depth_map",
184  {{"depth_map", "Depth map (Image) used for VoxelGrid carving."},
185  {"camera_parameters",
186  "PinholeCameraParameters used to record the given depth_map."},
187  {"keep_voxels_outside_image",
188  "retain voxels that don't project"
189  " to pixels in the image"}});
191  m, "VoxelGrid", "carve_silhouette",
192  {{"silhouette_mask",
193  "Silhouette mask (Image) used for VoxelGrid carving."},
194  {"camera_parameters",
195  "PinholeCameraParameters used to record the given depth_map."},
196  {"keep_voxels_outside_image",
197  "retain voxels that don't project"
198  " to pixels in the image"}});
200  m, "VoxelGrid", "to_octree",
201  {{"max_depth", "int: Maximum depth of the octree."}});
203  m, "VoxelGrid", "create_from_octree",
204  {{"octree", "geometry.Octree: The source octree."}});
206  m, "VoxelGrid", "create_dense",
207  {{"origin", "Coordinate center of the VoxelGrid"},
208  {"color", "Voxel color for all voxels if the VoxelGrid."},
209  {"voxel_size", "Voxel size of of the VoxelGrid construction."},
210  {"width", "Spatial width extend of the VoxelGrid."},
211  {"height", "Spatial height extend of the VoxelGrid."},
212  {"depth", "Spatial depth extend of the VoxelGrid."}});
214  m, "VoxelGrid", "create_from_point_cloud",
215  {{"input", "The input PointCloud"},
216  {"voxel_size", "Voxel size of the VoxelGrid construction."},
217  {"pooling_mode",
218  "VoxelPoolingMode for determining voxel color."}});
220  m, "VoxelGrid", "create_from_point_cloud_within_bounds",
221  {{"input", "The input PointCloud"},
222  {"voxel_size", "Voxel size of the VoxelGrid construction."},
223  {"min_bound",
224  "Minimum boundary point for the VoxelGrid to create."},
225  {"max_bound",
226  "Maximum boundary point for the VoxelGrid to create."},
227  {"pooling_mode",
228  "VoxelPoolingMode that determines how to compute the voxel "
229  "color."}});
231  m, "VoxelGrid", "create_from_triangle_mesh",
232  {{"input", "The input TriangleMesh"},
233  {"voxel_size", "Voxel size of of the VoxelGrid construction."}});
235  m, "VoxelGrid", "create_from_triangle_mesh_within_bounds",
236  {{"input", "The input TriangleMesh"},
237  {"voxel_size", "Voxel size of of the VoxelGrid construction."},
238  {"min_bound",
239  "Minimum boundary point for the VoxelGrid to create."},
240  {"max_bound",
241  "Maximum boundary point for the VoxelGrid to create."}});
242 }
243 
244 void pybind_voxelgrid_methods(py::module &m) {}
245 } // namespace geometry
246 } // namespace cloudViewer
math::float4 color
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
VoxelGrid is a collection of voxels which are aligned in grid.
Definition: VoxelGrid.h:64
void CreateFromOctree(const Octree &octree)
VoxelGrid & CarveDepthMap(const Image &depth_map, const camera::PinholeCameraParameters &camera_parameter, bool keep_voxels_outside_image)
double voxel_size_
Size of the voxel.
Definition: VoxelGrid.h:264
static std::shared_ptr< VoxelGrid > CreateFromTriangleMesh(const ccMesh &input, double voxel_size)
VoxelGrid & CarveSilhouette(const Image &silhouette_mask, const camera::PinholeCameraParameters &camera_parameter, bool keep_voxels_outside_image)
static std::shared_ptr< VoxelGrid > CreateFromPointCloud(const ccPointCloud &input, double voxel_size, VoxelPoolingMode color_mode=VoxelPoolingMode::AVG)
bool HasColors() const
Returns true if the voxel grid contains voxel colors.
Definition: VoxelGrid.h:108
bool HasVoxels() const
Returns true if the voxel grid contains voxels.
Definition: VoxelGrid.h:106
std::shared_ptr< geometry::Octree > ToOctree(const size_t &max_depth) const
static std::shared_ptr< VoxelGrid > CreateFromPointCloudWithinBounds(const ccPointCloud &input, double voxel_size, const Eigen::Vector3d &min_bound, const Eigen::Vector3d &max_bound, VoxelPoolingMode color_mode=VoxelPoolingMode::AVG)
std::vector< bool > CheckIfIncluded(const std::vector< Eigen::Vector3d > &queries)
Eigen::Vector3d origin_
Coorindate of the origin point.
Definition: VoxelGrid.h:266
Eigen::Vector3i GetVoxel(const Eigen::Vector3d &point) const
Returns voxel index given query point.
static std::shared_ptr< VoxelGrid > CreateDense(const Eigen::Vector3d &origin, const Eigen::Vector3d &color, double voxel_size, double width, double height, double depth)
void RemoveVoxel(const Eigen::Vector3i &idx)
Remove a voxel with specified grid index.
void AddVoxel(const Voxel &voxel)
Add a voxel with specified grid index and color.
std::vector< Voxel > GetVoxels() const
static std::shared_ptr< VoxelGrid > CreateFromTriangleMeshWithinBounds(const ccMesh &input, double voxel_size, const Eigen::Vector3d &min_bound, const Eigen::Vector3d &max_bound)
Base Voxel class, containing grid id and color.
Definition: VoxelGrid.h:38
Eigen::Vector3i grid_index_
Grid coordinate index of the voxel.
Definition: VoxelGrid.h:56
Eigen::Vector3d color_
Color of the voxel.
Definition: VoxelGrid.h:58
void ClassMethodDocInject(py::module &pybind_module, const std::string &class_name, const std::string &function_name, const std::unordered_map< std::string, std::string > &map_parameter_body_docs)
Definition: docstring.cpp:27
void pybind_voxelgrid(py::module &m)
Definition: voxelgrid.cpp:24
void pybind_voxelgrid_methods(py::module &m)
Definition: voxelgrid.cpp:244
Generic file read and write utility for python interface.
Eigen::Matrix< Index, 3, 1 > Vector3i
Definition: knncpp.h:30
std::string to_string(const T &n)
Definition: Common.h:20