Debian Codegraph @

src/pycolmap/mvs/normal_map.cc

raw
1 #include "colmap/mvs/normal_map.h"
2
3 #include "pycolmap/helpers.h"
4 #include "pycolmap/pybind11_extension.h"
5
6 #include <pybind11/eigen.h>
7 #include <pybind11/pybind11.h>
8
9 using namespace colmap;
10 using namespace colmap::mvs;
11 using namespace pybind11::literals;
12 namespace py = pybind11;
13
14 #ifdef _MSC_VER // If compiling with MSVC
15 #include <stddef.h>
16 typedef ptrdiff_t ssize_t;
17 #endif
18
19 py::array_t<float> ArrayFromNormalMap(const NormalMap& self) {
20 const std::vector<ssize_t> shape = {static_cast<ssize_t>(self.GetHeight()),
21 static_cast<ssize_t>(self.GetWidth()),
22 static_cast<ssize_t>(self.GetDepth())};
23 py::array_t<float> output(shape);
24 py::buffer_info output_info = output.request();
25 float* output_ptr = reinterpret_cast<float*>(output_info.ptr);
26 std::memcpy(output_ptr, self.GetPtr(), self.GetNumBytes());
27 return output;
28 }
29
30 NormalMap NormalMapFromArray(py::array_t<float, py::array::c_style> array) {
31 if (array.ndim() != 3) {
32 throw std::runtime_error("Input array must have 3 dimensions!");
33 }
34
35 const int width = array.shape(1);
36 const int height = array.shape(0);
37 const int depth = array.shape(2);
38
39 if (width == 0 || height == 0) {
40 throw std::runtime_error(
41 "Input array must have positive width and height!");
42 }
43
44 if (depth != 3) {
45 throw std::runtime_error("Input array must have 3 channels for normals!");
46 }
47
48 NormalMap output(width, height);
49
50 const float* input_ptr = static_cast<float*>(array.request().ptr);
51 std::memcpy(output.GetPtr(), input_ptr, output.GetNumBytes());
52 return output;
53 }
54
55 void BindNormalMap(pybind11::module& m) {
56 py::classh<NormalMap>(m, "NormalMap")
57 .def(py::init<>())
58 .def(py::init<size_t, size_t>(), "width"_a, "height"_a)
59 .def("to_array", &ArrayFromNormalMap)
60 .def_static("from_array",
61 &NormalMapFromArray,
62 "array"_a,
63 "Create normal map as a copy of array. Returns normal map "
64 "with shape (H, W, 3) where the 3 channels represent the "
65 "x, y, z components of the normal vectors.")
66 .def("rescale", &NormalMap::Rescale, "factor"_a, "Rescale normal map.")
67 .def("downsize",
68 &NormalMap::Downsize,
69 "max_width"_a,
70 "max_height"_a,
71 "Downsize normal map to fit maximum dimensions.")
72 .def("to_bitmap",
73 &NormalMap::ToBitmap,
74 "Convert normal map to bitmap for visualization.")
75 .def_property_readonly(
76 "width", &NormalMap::GetWidth, "Width of the normal map.")
77 .def_property_readonly(
78 "height", &NormalMap::GetHeight, "Height of the normal map.")
79 .def("read",
80 &NormalMap::Read,
81 "path"_a,
82 "Read normal map from file at given path.")
83 .def("write",
84 &NormalMap::Write,
85 "path"_a,
86 "Write normal map to file at given path.")
87 .def("__repr__", &CreateRepresentation<NormalMap>);
88 }