ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
integration.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 
9 
10 #include <VoxelGrid.h>
11 
15 #include "pybind/docstring.h"
16 
17 namespace cloudViewer {
18 namespace pipelines {
19 namespace integration {
20 
21 template <class TSDFVolumeBase = TSDFVolume>
22 class PyTSDFVolume : public TSDFVolumeBase {
23 public:
24  using TSDFVolumeBase::TSDFVolumeBase;
25  void Reset() override { PYBIND11_OVERLOAD_PURE(void, TSDFVolumeBase, ); }
27  const camera::PinholeCameraIntrinsic &intrinsic,
28  const Eigen::Matrix4d &extrinsic) override {
29  PYBIND11_OVERLOAD_PURE(void, TSDFVolumeBase, image, intrinsic,
30  extrinsic);
31  }
32  std::shared_ptr<ccPointCloud> ExtractPointCloud() override {
33  PYBIND11_OVERLOAD_PURE(std::shared_ptr<ccPointCloud>, TSDFVolumeBase, );
34  }
35  std::shared_ptr<ccMesh> ExtractTriangleMesh() override {
36  PYBIND11_OVERLOAD_PURE(std::shared_ptr<ccMesh>, TSDFVolumeBase, );
37  }
38 };
39 
40 void pybind_integration_classes(py::module &m) {
41  // cloudViewer.integration.TSDFVolumeColorType
42  py::native_enum<TSDFVolumeColorType>(m, "TSDFVolumeColorType", "enum.Enum",
43  "Enum class for TSDFVolumeColorType.")
44  .value("NoColor", TSDFVolumeColorType::NoColor)
45  .value("RGB8", TSDFVolumeColorType::RGB8)
46  .value("Gray32", TSDFVolumeColorType::Gray32)
47  .export_values()
48  .finalize();
49 
50  // cloudViewer.integration.TSDFVolume
51  py::class_<TSDFVolume, PyTSDFVolume<TSDFVolume>> tsdfvolume(
52  m, "TSDFVolume", R"(Base class of the Truncated
53 Signed Distance Function (TSDF) volume This volume is usually used to integrate
54 surface data (e.g., a series of RGB-D images) into a Mesh or PointCloud. The
55 basic technique is presented in the following paper:
56 
57 A volumetric method for building complex models from range images
58 
59 B. Curless and M. Levoy
60 
61 In SIGGRAPH, 1996)");
62  tsdfvolume
63  .def("reset", &TSDFVolume::Reset,
64  "Function to reset the TSDFVolume")
65  .def("integrate", &TSDFVolume::Integrate,
66  "Function to integrate an RGB-D image into the volume",
67  "image"_a, "intrinsic"_a, "extrinsic"_a)
68  .def("extract_point_cloud", &TSDFVolume::ExtractPointCloud,
69  "Function to extract a point cloud with normals")
70  .def("extract_triangle_mesh", &TSDFVolume::ExtractTriangleMesh,
71  "Function to extract a triangle mesh")
72  .def_readwrite("voxel_length", &TSDFVolume::voxel_length_,
73  "float: Length of the voxel in meters.")
74  .def_readwrite("sdf_trunc", &TSDFVolume::sdf_trunc_,
75  "float: Truncation value for signed distance "
76  "function (SDF).")
77  .def_readwrite("color_type", &TSDFVolume::color_type_,
78  "integration.TSDFVolumeColorType: Color type of the "
79  "TSDF volume.");
80  docstring::ClassMethodDocInject(m, "TSDFVolume", "extract_point_cloud");
81  docstring::ClassMethodDocInject(m, "TSDFVolume", "extract_triangle_mesh");
83  m, "TSDFVolume", "integrate",
84  {{"image", "RGBD image."},
85  {"intrinsic", "Pinhole camera intrinsic parameters."},
86  {"extrinsic", "Extrinsic parameters."}});
87  docstring::ClassMethodDocInject(m, "TSDFVolume", "reset");
88 
89  // cloudViewer.integration.UniformTSDFVolume:
90  // cloudViewer.integration.TSDFVolume
91  py::class_<UniformTSDFVolume, PyTSDFVolume<UniformTSDFVolume>, TSDFVolume>
92  uniform_tsdfvolume(
93  m, "UniformTSDFVolume",
94  "UniformTSDFVolume implements the classic TSDF "
95  "volume with uniform voxel grid (Curless and Levoy 1996).");
96  py::detail::bind_copy_functions<UniformTSDFVolume>(uniform_tsdfvolume);
97  uniform_tsdfvolume
98  .def(py::init([](double length, int resolution, double sdf_trunc,
99  TSDFVolumeColorType color_type) {
100  return new UniformTSDFVolume(length, resolution, sdf_trunc,
101  color_type);
102  }),
103  "length"_a, "resolution"_a, "sdf_trunc"_a, "color_type"_a)
104  .def(py::init([](double length, int resolution, double sdf_trunc,
105  TSDFVolumeColorType color_type,
106  Eigen::Vector3d origin) {
107  return new UniformTSDFVolume(length, resolution, sdf_trunc,
108  color_type, origin);
109  }),
110  "length"_a, "resolution"_a, "sdf_trunc"_a, "color_type"_a,
111  "origin"_a)
112  .def("__repr__",
113  [](const UniformTSDFVolume &vol) {
114  return std::string(
115  ""
116  "UniformTSDFVolume ") +
118  ? std::string("without color.")
119  : std::string("with color."));
120  }) // todo: extend
121  .def("extract_voxel_point_cloud",
123  "Debug function to extract the voxel data into a point cloud.")
124  .def("extract_voxel_grid", &UniformTSDFVolume::ExtractVoxelGrid,
125  "Debug function to extract the voxel data VoxelGrid.")
126  .def("extract_volume_tsdf", &UniformTSDFVolume::ExtractVolumeTSDF,
127  "Debug function to extract the volume TSDF data.")
128  .def("extract_volume_color", &UniformTSDFVolume::ExtractVolumeColor,
129  "Debug function to extract the volume color data.")
130  .def("inject_volume_tsdf", &UniformTSDFVolume::InjectVolumeTSDF,
131  "Debug function to inject the voxel TSDF data.", "tsdf"_a)
132  .def("inject_volume_color", &UniformTSDFVolume::InjectVolumeColor,
133  "Debug function to inject the voxel Color data.", "color"_a)
134  .def_readwrite("length", &UniformTSDFVolume::length_,
135  "Total length, where ``voxel_length = length / "
136  "resolution``.")
137  .def_readwrite("resolution", &UniformTSDFVolume::resolution_,
138  "Resolution over the total length, where "
139  "``voxel_length = length / resolution``");
140  docstring::ClassMethodDocInject(m, "UniformTSDFVolume",
141  "extract_voxel_point_cloud");
142 
143  // cloudViewer.integration.ScalableTSDFVolume:
144  // cloudViewer.integration.TSDFVolume
145  py::class_<ScalableTSDFVolume, PyTSDFVolume<ScalableTSDFVolume>, TSDFVolume>
146  scalable_tsdfvolume(m, "ScalableTSDFVolume", R"(The
147 ScalableTSDFVolume implements a more memory efficient data structure for
148 volumetric integration.
149 
150 This implementation is based on the following repository:
151 https://github.com/qianyizh/ElasticReconstruction/tree/master/Integrate
152 
153 An observed depth pixel gives two types of information: (a) an approximation
154 of the nearby surface, and (b) empty space from the camera to the surface.
155 They induce two core concepts of volumetric integration: weighted average of
156 a truncated signed distance function (TSDF), and carving. The weighted
157 average of TSDF is great in addressing the Gaussian noise along surface
158 normal and producing a smooth surface output. The carving is great in
159 removing outlier structures like floating noise pixels and bumps along
160 structure edges.
161 
162 Ref: Dense Scene Reconstruction with Points of Interest
163 
164 Q.-Y. Zhou and V. Koltun
165 
166 In SIGGRAPH, 2013)");
167  py::detail::bind_copy_functions<ScalableTSDFVolume>(scalable_tsdfvolume);
168  scalable_tsdfvolume
169  .def(py::init([](double voxel_length, double sdf_trunc,
170  TSDFVolumeColorType color_type,
171  int volume_unit_resolution,
172  int depth_sampling_stride) {
173  return new ScalableTSDFVolume(
174  voxel_length, sdf_trunc, color_type,
175  volume_unit_resolution, depth_sampling_stride);
176  }),
177  "voxel_length"_a, "sdf_trunc"_a, "color_type"_a,
178  "volume_unit_resolution"_a = 16, "depth_sampling_stride"_a = 4)
179  .def("__repr__",
180  [](const ScalableTSDFVolume &vol) {
181  return std::string(
182  ""
183  "ScalableTSDFVolume ") +
185  ? std::string("without color.")
186  : std::string("with color."));
187  })
188  .def("extract_voxel_point_cloud",
190  "Debug function to extract the voxel data into a point "
191  "cloud.");
192  docstring::ClassMethodDocInject(m, "ScalableTSDFVolume",
193  "extract_voxel_point_cloud");
194 }
195 
196 void pybind_integration_methods(py::module &m) {
197  // Currently empty
198 }
199 
200 void pybind_integration(py::module &m) {
201  py::module m_submodule =
202  m.def_submodule("integration", "Integration pipeline.");
203  pybind_integration_classes(m_submodule);
204  pybind_integration_methods(m_submodule);
205 }
206 
207 } // namespace integration
208 } // namespace pipelines
209 } // namespace cloudViewer
std::shared_ptr< core::Tensor > image
Contains the pinhole camera intrinsic parameters.
RGBDImage is for a pair of registered color and depth images,.
Definition: RGBDImage.h:27
void Reset() override
Function to reset the TSDFVolume.
Definition: integration.cpp:25
std::shared_ptr< ccPointCloud > ExtractPointCloud() override
Function to extract a point cloud with normals.
Definition: integration.cpp:32
void Integrate(const geometry::RGBDImage &image, const camera::PinholeCameraIntrinsic &intrinsic, const Eigen::Matrix4d &extrinsic) override
Function to integrate an RGB-D image into the volume.
Definition: integration.cpp:26
std::shared_ptr< ccMesh > ExtractTriangleMesh() override
Function to extract a triangle mesh, using the marching cubes algorithm. (https://en....
Definition: integration.cpp:35
std::shared_ptr< ccPointCloud > ExtractVoxelPointCloud()
Debug function to extract the voxel data into a point cloud.
Base class of the Truncated Signed Distance Function (TSDF) volume.
Definition: TSDFVolume.h:43
double sdf_trunc_
Truncation value for signed distance function (SDF).
Definition: TSDFVolume.h:78
virtual std::shared_ptr< ccMesh > ExtractTriangleMesh()=0
Function to extract a triangle mesh, using the marching cubes algorithm. (https://en....
virtual void Reset()=0
Function to reset the TSDFVolume.
virtual void Integrate(const geometry::RGBDImage &image, const camera::PinholeCameraIntrinsic &intrinsic, const Eigen::Matrix4d &extrinsic)=0
Function to integrate an RGB-D image into the volume.
double voxel_length_
Length of the voxel in meters.
Definition: TSDFVolume.h:76
virtual std::shared_ptr< ccPointCloud > ExtractPointCloud()=0
Function to extract a point cloud with normals.
TSDFVolumeColorType color_type_
Color type of the TSDF volume.
Definition: TSDFVolume.h:80
UniformTSDFVolume implements the classic TSDF volume with uniform voxel grid (Curless and Levoy 1996)...
std::shared_ptr< geometry::VoxelGrid > ExtractVoxelGrid() const
Debug function to extract the voxel data VoxelGrid.
std::shared_ptr< ccPointCloud > ExtractVoxelPointCloud() const
Debug function to extract the voxel data into a VoxelGrid.
std::vector< Eigen::Vector2d > ExtractVolumeTSDF() const
Debug function to extract the volume TSDF data into a vector array.
void InjectVolumeTSDF(const std::vector< Eigen::Vector2d > &sharedvoxels)
Debug function to inject voxel TSDF data into the volume.
std::vector< Eigen::Vector3d > ExtractVolumeColor() const
Debug function to extract the volume color data into a vector array.
double length_
Total length, where voxel_length = length / resolution.
void InjectVolumeColor(const std::vector< Eigen::Vector3d > &sharedcolors)
Debug function to inject voxel Color data into the volume.
__host__ __device__ float length(float2 v)
Definition: cutil_math.h:1162
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_integration_classes(py::module &m)
Definition: integration.cpp:40
void pybind_integration_methods(py::module &m)
void pybind_integration(py::module &m)
Generic file read and write utility for python interface.