Debian Codegraph @

src/pycolmap/pipeline/meshing.cc

raw
1 #include "colmap/mvs/advancing_front_meshing.h"
2 #include "colmap/mvs/delaunay_meshing.h"
3 #include "colmap/mvs/mesh_simplification.h"
4 #include "colmap/mvs/poisson_meshing.h"
5 #include "colmap/util/file.h"
6 #include "colmap/util/misc.h"
7 #include "colmap/util/ply.h"
8
9 #include "pycolmap/helpers.h"
10 #include "pycolmap/pybind11_extension.h"
11
12 #include <filesystem>
13
14 #include <pybind11/numpy.h>
15 #include <pybind11/pybind11.h>
16 #include <pybind11/stl.h>
17
18 using namespace colmap;
19 using namespace pybind11::literals;
20 namespace py = pybind11;
21
22 void BindMeshing(py::module& m) {
23 using PoissonMOpts = mvs::PoissonMeshingOptions;
24 auto PyPoissonMeshingOptions =
25 py::classh<PoissonMOpts>(m, "PoissonMeshingOptions")
26 .def(py::init<>())
27 .def_readwrite("point_weight",
28 &PoissonMOpts::point_weight,
29 "This floating point value specifies the importance "
30 "that interpolation of"
31 "the point samples is given in the formulation of the "
32 "screened Poisson"
33 "equation. The results of the original (unscreened) "
34 "Poisson Reconstruction"
35 "can be obtained by setting this value to 0.")
36 .def_readwrite("depth",
37 &PoissonMOpts::depth,
38 "This integer is the maximum depth of the tree that "
39 "will be used for surface"
40 "reconstruction. Running at depth d corresponds to "
41 "solving on a voxel grid"
42 "whose resolution is no larger than 2^d x 2^d x 2^d. "
43 "Note that since the"
44 "reconstructor adapts the octree to the sampling "
45 "density, the specified"
46 "reconstruction depth is only an upper bound.")
47 .def_readwrite("color",
48 &PoissonMOpts::color,
49 "If specified, the reconstruction code assumes that "
50 "the input is equipped"
51 "with colors and will extrapolate the color values to "
52 "the vertices of the"
53 "reconstructed mesh. The floating point value "
54 "specifies the relative"
55 "importance of finer color estimates over lower ones.")
56 .def_readwrite("trim",
57 &PoissonMOpts::trim,
58 "This floating point values specifies the value for "
59 "mesh trimming. The"
60 "subset of the mesh with signal value less than the "
61 "trim value is discarded.")
62 .def_readwrite(
63 "num_threads",
64 &PoissonMOpts::num_threads,
65 "The number of threads used for the Poisson reconstruction.")
66 .def("check", &PoissonMOpts::Check);
67 MakeDataclass(PyPoissonMeshingOptions);
68
69 using DMOpts = mvs::DelaunayMeshingOptions;
70 auto PyDelaunayMeshingOptions =
71 py::classh<DMOpts>(m, "DelaunayMeshingOptions")
72 .def(py::init<>())
73 .def_readwrite("max_proj_dist",
74 &DMOpts::max_proj_dist,
75 "Unify input points into one cell in the Delaunay "
76 "triangulation that fall"
77 "within a reprojected radius of the given pixels.")
78 .def_readwrite("max_depth_dist",
79 &DMOpts::max_depth_dist,
80 "Maximum relative depth difference between input "
81 "point and a vertex of an"
82 "existing cell in the Delaunay triangulation, "
83 "otherwise a new vertex is"
84 "created in the triangulation.")
85 .def_readwrite("visibility_sigma",
86 &DMOpts::visibility_sigma,
87 "The standard deviation of wrt. the number of images "
88 "seen by each point."
89 "Increasing this value decreases the influence of "
90 "points seen in few images.")
91 .def_readwrite("distance_sigma_factor",
92 &DMOpts::distance_sigma_factor,
93 "The factor that is applied to the computed distance "
94 "sigma, which is"
95 "automatically computed as the 25th percentile of "
96 "edge lengths. A higher"
97 "value will increase the smoothness of the surface.")
98 .def_readwrite(
99 "quality_regularization",
100 &DMOpts::quality_regularization,
101 "A higher quality regularization leads to a smoother surface.")
102 .def_readwrite("max_side_length_factor",
103 &DMOpts::max_side_length_factor,
104 "Filtering thresholds for outlier surface mesh faces. "
105 "If the longest side of"
106 "a mesh face (longest out of 3) exceeds the side "
107 "lengths of all faces at a"
108 "certain percentile by the given factor, then it is "
109 "considered an outlier"
110 "mesh face and discarded.")
111 .def_readwrite("max_side_length_percentile",
112 &DMOpts::max_side_length_percentile,
113 "Filtering thresholds for outlier surface mesh faces. "
114 "If the longest side of"
115 "a mesh face (longest out of 3) exceeds the side "
116 "lengths of all faces at a"
117 "certain percentile by the given factor, then it is "
118 "considered an outlier"
119 "mesh face and discarded.")
120 .def_readwrite("num_threads",
121 &DMOpts::num_threads,
122 "The number of threads to use for reconstruction. "
123 "Default is all threads.")
124 .def("check", &DMOpts::Check);
125 MakeDataclass(PyDelaunayMeshingOptions);
126
127 m.def(
128 "poisson_meshing",
129 [](const std::filesystem::path& input_path,
130 const std::filesystem::path& output_path,
131 const PoissonMOpts& options) -> void {
132 mvs::PoissonMeshing(options, input_path, output_path);
133 },
134 "input_path"_a,
135 "output_path"_a,
136 py::arg_v(
137 "options", mvs::PoissonMeshingOptions(), "PoissonMeshingOptions()"),
138 "Perform Poisson surface reconstruction and return true if successful.");
139
140 #ifdef COLMAP_CGAL_ENABLED
141 m.def(
142 "sparse_delaunay_meshing",
143 [](const std::filesystem::path& input_path,
144 const std::filesystem::path& output_path,
145 const DMOpts& options) -> void {
146 mvs::SparseDelaunayMeshing(options, input_path, output_path);
147 },
148 "input_path"_a,
149 "output_path"_a,
150 py::arg_v(
151 "options", mvs::DelaunayMeshingOptions(), "DelaunayMeshingOptions()"),
152 "Delaunay meshing of sparse COLMAP reconstructions.");
153
154 m.def(
155 "dense_delaunay_meshing",
156 [](const std::filesystem::path& input_path,
157 const std::filesystem::path& output_path,
158 const DMOpts& options) -> void {
159 mvs::DenseDelaunayMeshing(options, input_path, output_path);
160 },
161 "input_path"_a,
162 "output_path"_a,
163 py::arg_v(
164 "options", mvs::DelaunayMeshingOptions(), "DelaunayMeshingOptions()"),
165 "Delaunay meshing of dense COLMAP reconstructions.");
166
167 using AFMOpts = mvs::AdvancingFrontMeshingOptions;
168 auto PyAdvancingFrontMeshingOptions =
169 py::classh<AFMOpts>(m, "AdvancingFrontMeshingOptions")
170 .def(py::init<>())
171 .def_readwrite("max_edge_length",
172 &AFMOpts::max_edge_length,
173 "Maximum edge length constraint for triangles "
174 "(in world units). Set to 0 to disable.")
175 .def_readwrite("visibility_filtering",
176 &AFMOpts::visibility_filtering,
177 "Whether to use visibility-based filtering.")
178 .def_readwrite("visibility_filtering_max_intersections",
179 &AFMOpts::visibility_filtering_max_intersections,
180 "Maximum number of visibility ray intersections "
181 "before a facet is rejected.")
182 .def_readwrite("visibility_post_filtering",
183 &AFMOpts::visibility_post_filtering,
184 "If true, post-filter via AABB tree. "
185 "If false, pre-filter via Priority functor.")
186 .def_readwrite("visibility_ray_trim_offset",
187 &AFMOpts::visibility_ray_trim_offset,
188 "Offset distance (in world units) by which visibility "
189 "rays are trimmed at the target end.")
190 .def_readwrite("block_size",
191 &AFMOpts::block_size,
192 "Block size for parallel processing "
193 "(in world units, 0 to disable).")
194 .def_readwrite("block_overlap",
195 &AFMOpts::block_overlap,
196 "Overlap margin as a fraction of block_size.")
197 .def_readwrite("num_threads",
198 &AFMOpts::num_threads,
199 "The number of threads to use. -1 = all threads.")
200 .def("check", &AFMOpts::Check);
201 MakeDataclass(PyAdvancingFrontMeshingOptions);
202
203 m.def(
204 "advancing_front_meshing",
205 [](const std::filesystem::path& input_path,
206 const std::filesystem::path& output_path,
207 const AFMOpts& options) -> void {
208 mvs::AdvancingFrontMeshing(options, input_path, output_path);
209 },
210 "input_path"_a,
211 "output_path"_a,
212 py::arg_v("options",
213 mvs::AdvancingFrontMeshingOptions(),
214 "AdvancingFrontMeshingOptions()"),
215 "Advancing front surface reconstruction of dense COLMAP "
216 "reconstructions.");
217 #endif
218
219 using MSOpts = mvs::MeshSimplificationOptions;
220 auto PyMeshSimplificationOptions =
221 py::classh<MSOpts>(m, "MeshSimplificationOptions")
222 .def(py::init<>())
223 .def_readwrite("target_face_ratio",
224 &MSOpts::target_face_ratio,
225 "Fraction of faces to retain, in (0, 1].")
226 .def_readwrite("max_error",
227 &MSOpts::max_error,
228 "Maximum quadric error per collapse; 0 = disabled.")
229 .def_readwrite("boundary_weight",
230 &MSOpts::boundary_weight,
231 "Penalty weight for boundary edges; 0 = disabled.")
232 .def_readwrite(
233 "interpolate_colors",
234 &MSOpts::interpolate_colors,
235 "Blend colors on collapse vs. pick lower-error vertex.")
236 .def_readwrite("num_threads",
237 &MSOpts::num_threads,
238 "The number of threads to use for initialization. "
239 "-1 = all threads.")
240 .def("check", &MSOpts::Check);
241 MakeDataclass(PyMeshSimplificationOptions);
242
243 m.def(
244 "simplify_mesh",
245 [](const std::filesystem::path& input_path,
246 const std::filesystem::path& output_path,
247 const MSOpts& options) -> void {
248 THROW_CHECK_HAS_FILE_EXTENSION(input_path, ".ply");
249 THROW_CHECK_FILE_EXISTS(input_path);
250 THROW_CHECK_HAS_FILE_EXTENSION(output_path, ".ply");
251 const PlyMesh mesh = ReadPlyMesh(input_path).mesh;
252 const PlyMesh result = mvs::SimplifyMesh(mesh, options);
253 WriteBinaryPlyMesh(output_path, PlyTexturedMesh{result});
254 },
255 "input_path"_a,
256 "output_path"_a,
257 py::arg_v("options",
258 mvs::MeshSimplificationOptions(),
259 "MeshSimplificationOptions()"),
260 "Read a PLY mesh, simplify it using QEM decimation, and write "
261 "the result.",
262 py::call_guard<py::gil_scoped_release>());
263 }