ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
slam.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 
10 #include "pybind/docstring.h"
11 
12 namespace cloudViewer {
13 namespace t {
14 namespace pipelines {
15 namespace slam {
16 
17 static const std::unordered_map<std::string, std::string>
19  {"voxel_size", "The voxel size of the volume in meters."},
20  {"block_resolution",
21  "Resolution of local dense voxel blocks. By default 16 "
22  "is used to create 16^3 voxel blocks."},
23  {"block_count",
24  "Number of estimate blocks per scene with the block "
25  "resolution set to 16 and the 6mm voxel resolution. "
26  "Typically 20000 for small scenes (a desk), 40000 for medium "
27  "scenes (a bedroom), 80000 for large scenes (an "
28  "apartment)."},
29  {"transformation", "A 4x4 3D transformation matrix."},
30  {"device", "The CPU or CUDA device used for the object."},
31  {"depth_max",
32  "The max clipping depth to filter noisy observations too "
33  "far."},
34  {"depth_min", "The min clipping depth."},
35  {"depth_scale",
36  "The scale factor to convert raw depth into meters."},
37  {"input_frame",
38  "The frame that contains raw depth and optionally images with "
39  "the same size from the input."},
40  {"model_frame",
41  "The frame that contains ray casted depth and optionally "
42  "color from the volumetric model."},
43  {"estimated_number",
44  "Estimated number of surface points. Use -1 if no estimation "
45  "is available."},
46  {"weight_threshold",
47  "Weight threshold to filter outlier voxel blocks."},
48  {"height", "Height of an image frame."},
49  {"width", "Width of an image frame."},
50  {"intrinsics", "Intrinsic matrix stored in a 3x3 Tensor."},
51  {"trunc_voxel_multiplier",
52  "Truncation distance multiplier in voxel size for signed "
53  "distance. For instance, "
54  "--trunc_voxel_multiplier=8 with --voxel_size=0.006(m) "
55  "creates a truncation distance of 0.048(m)."}};
56 
57 void pybind_slam(py::module &m) {
58  py::module m_slam = m.def_submodule("slam", "Tensor DenseSLAM pipeline.");
59  py::class_<Model> model(m_slam, "Model",
60  "Volumetric model for Dense SLAM.");
61  py::detail::bind_copy_functions<Model>(model);
62  model.def(py::init<>());
63  model.def(py::init<float, int, int, core::Tensor, core::Device>(),
64  "Constructor of a VoxelBlockGrid", "voxel_size"_a,
65  "block_resolution"_a = 16, " block_count"_a = 10000,
66  "transformation"_a = core::Tensor::Eye(4, core::Float64,
67  core::Device("CPU:0")),
68  "device"_a = core::Device("CUDA:0"));
69  docstring::ClassMethodDocInject(m_slam, "Model", "__init__",
71 
72  model.def("get_current_frame_pose", &Model::GetCurrentFramePose);
73  model.def("update_frame_pose", &Model::UpdateFramePose);
74 
75  model.def("synthesize_model_frame", &Model::SynthesizeModelFrame,
76  py::call_guard<py::gil_scoped_release>(),
77  "Synthesize frame from the volumetric model using ray casting.",
78  "model_frame"_a, "depth_scale"_a = 1000.0, "depth_min"_a = 0.1,
79  "depth_max"_a = 3.0, "trunc_voxel_multiplier"_a = 8.0,
80  "enable_color"_a = false, "weight_threshold"_a = -1.0);
81  docstring::ClassMethodDocInject(m_slam, "Model", "synthesize_model_frame",
83 
84  model.def(
85  "track_frame_to_model", &Model::TrackFrameToModel,
86  py::call_guard<py::gil_scoped_release>(),
87  "Track input frame against raycasted frame from model.",
88  "input_frame"_a, "model_frame"_a, "depth_scale"_a = 1000.0,
89  "depth_max"_a = 3.0, "depth_diff"_a = 0.07,
90  py::arg_v("method", odometry::Method::PointToPlane,
91  "Method.PointToPlane"),
92  "criteria"_a = (std::vector<odometry::OdometryConvergenceCriteria>){
93  6, 3, 1});
94  docstring::ClassMethodDocInject(m_slam, "Model", "track_frame_to_model",
96 
97  model.def("integrate", &Model::Integrate,
98  py::call_guard<py::gil_scoped_release>(),
99  "Integrate an input frame to a volume.", "input_frame"_a,
100  "depth_scale"_a = 1000.0, "depth_max"_a = 3.0,
101  "trunc_voxel_multiplier"_a = 8.0);
102  docstring::ClassMethodDocInject(m_slam, "Model", "integrate",
104 
105  model.def("extract_pointcloud", &Model::ExtractPointCloud,
106  py::call_guard<py::gil_scoped_release>(),
107  "Extract point cloud from the volumetric model.",
108  "weight_threshold"_a = 3.0, "estimated_number"_a = -1);
109  docstring::ClassMethodDocInject(m_slam, "Model", "extract_pointcloud",
111 
112  model.def("extract_trianglemesh", &Model::ExtractTriangleMesh,
113  py::call_guard<py::gil_scoped_release>(),
114  "Extract triangle mesh from the volumetric model.",
115  "weight_threshold"_a = 3.0, "estimated_number"_a = -1);
116  docstring::ClassMethodDocInject(m_slam, "Model", "extract_trianglemesh",
118 
119  model.def(
120  "get_hashmap", &Model::GetHashMap,
121  "Get the underlying hash map from 3D coordinates to voxel blocks.");
122  model.def_readwrite("voxel_grid", &Model::voxel_grid_,
123  "Get the maintained VoxelBlockGrid.");
124  model.def_readwrite("frustum_block_coords", &Model::frustum_block_coords_,
125  "Active block coordinates from prior integration");
126  model.def_readwrite("transformation_frame_to_world",
128  "Get the 4x4 transformation matrix from the current "
129  "frame to the world frame.");
130  model.def_readwrite("frame_id", &Model::frame_id_,
131  "Get the current frame index in a sequence.");
132 
133  py::class_<Frame> frame(m_slam, "Frame",
134  "A frame container that stores a map from keys "
135  "(color, depth) to tensor images.");
136  py::detail::bind_copy_functions<Frame>(frame);
137  frame.def(py::init<int, int, core::Tensor, core::Device>(), "height"_a,
138  "width"_a, "intrinsics"_a, "device"_a);
139  docstring::ClassMethodDocInject(m_slam, "Frame", "__init__",
141 
142  frame.def("height", &Frame::GetHeight);
143  frame.def("width", &Frame::GetWidth);
144 
145  frame.def("set_data", &Frame::SetData,
146  "Set a 2D tensor to a image to the given key in the map.");
147  frame.def("get_data", &Frame::GetData,
148  "Get a 2D tensor from a image from the given key in the map.");
149  frame.def("set_data_from_image", &Frame::SetDataFromImage,
150  "Set a 2D image to the given key in the map.");
151  frame.def("get_data_as_image", &Frame::GetDataAsImage,
152  "Get a 2D image from from the given key in the map.");
153 }
154 
155 } // namespace slam
156 } // namespace pipelines
157 } // namespace t
158 } // namespace cloudViewer
Rect frame
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:418
void SetDataFromImage(const std::string &name, const t::geometry::Image &data)
Definition: Frame.h:53
core::Tensor GetData(const std::string &name) const
Definition: Frame.h:43
void SetData(const std::string &name, const core::Tensor &data)
Definition: Frame.h:40
t::geometry::Image GetDataAsImage(const std::string &name) const
Definition: Frame.h:58
void UpdateFramePose(int frame_id, const core::Tensor &T_frame_to_world)
Definition: Model.h:37
void SynthesizeModelFrame(Frame &raycast_frame, float depth_scale=1000.0, float depth_min=0.1, float depth_max=3.0, float trunc_voxel_multiplier=8.0, bool enable_color=true, float weight_threshold=-1.0)
Definition: Model.cpp:38
odometry::OdometryResult TrackFrameToModel(const Frame &input_frame, const Frame &raycast_frame, float depth_scale=1000.0, float depth_max=3.0, float depth_diff=0.07, odometry::Method method=odometry::Method::PointToPlane, const std::vector< odometry::OdometryConvergenceCriteria > &criteria={6, 3, 1})
Definition: Model.cpp:68
core::Tensor GetCurrentFramePose() const
Definition: Model.h:36
t::geometry::TriangleMesh ExtractTriangleMesh(float weight_threshold=3.0f, int estimated_number=-1)
Definition: Model.cpp:113
void Integrate(const Frame &input_frame, float depth_scale=1000.0, float depth_max=3.0, float trunc_voxel_multiplier=8.0f)
Definition: Model.cpp:91
core::HashMap GetHashMap()
Get block hashmap int the VoxelBlockGrid.
Definition: Model.cpp:118
t::geometry::PointCloud ExtractPointCloud(float weight_threshold=3.0f, int estimated_number=-1)
Definition: Model.cpp:108
t::geometry::VoxelBlockGrid voxel_grid_
Maintained volumetric map.
Definition: Model.h:122
core::Tensor frustum_block_coords_
Active block coordinates from prior integration.
Definition: Model.h:125
const Dtype Float64
Definition: Dtype.cpp:43
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
static const std::unordered_map< std::string, std::string > map_shared_argument_docstrings
Definition: slam.cpp:18
void pybind_slam(py::module &m)
Definition: slam.cpp:57
Generic file read and write utility for python interface.