From 3057631a841dd13986e06b0e0394a6158f1a3c68 Mon Sep 17 00:00:00 2001 From: Matt Alvarado Date: Tue, 9 Dec 2025 00:45:10 -0500 Subject: [PATCH 1/3] Fix windows wheel builds --- python/bindings.cc | 141 ++++++++++++++++++++++++++++++--------------- 1 file changed, 93 insertions(+), 48 deletions(-) diff --git a/python/bindings.cc b/python/bindings.cc index e343120c..b8bed256 100644 --- a/python/bindings.cc +++ b/python/bindings.cc @@ -74,6 +74,10 @@ namespace py = pybind11; +namespace { +using SSizeVector = std::vector; +} + PYBIND11_MODULE(_libmultisense, m) { m.doc() = "Pybind11 bindings for the LibMultiSense C++ Library"; @@ -161,49 +165,58 @@ PYBIND11_MODULE(_libmultisense, m) { if (image.format == multisense::Image::PixelFormat::H264 || image.format == multisense::Image::PixelFormat::JPEG) { + const SSizeVector shape = { + static_cast(image.raw_data->size() - image.image_data_offset) + }; + const SSizeVector strides = {static_cast(sizeof(uint8_t))}; return py::array(py::buffer_info( const_cast(image.raw_data->data() + image.image_data_offset), - sizeof(uint8_t), + static_cast(sizeof(uint8_t)), py::format_descriptor::format(), - 1, - {image.raw_data->size() - image.image_data_offset}, - {sizeof(uint8_t)})); + static_cast(shape.size()), + shape, + strides)); } - std::vector shape = {static_cast(image.height), static_cast(image.width)}; - std::vector strides; - size_t element_size = 0; + SSizeVector shape = {static_cast(image.height), static_cast(image.width)}; + SSizeVector strides; + py::ssize_t element_size = 0; std::string format; switch (image.format) { case multisense::Image::PixelFormat::MONO8: { - element_size = sizeof(uint8_t); + element_size = static_cast(sizeof(uint8_t)); format = py::format_descriptor::format(); - strides = {sizeof(uint8_t) * image.width, sizeof(uint8_t)}; + strides = {static_cast(sizeof(uint8_t) * image.width), + static_cast(sizeof(uint8_t))}; break; } case multisense::Image::PixelFormat::MONO16: { - element_size = sizeof(uint16_t); + element_size = static_cast(sizeof(uint16_t)); format = py::format_descriptor::format(); - strides = {sizeof(uint16_t) * image.width, sizeof(uint16_t)}; + strides = {static_cast(sizeof(uint16_t) * image.width), + static_cast(sizeof(uint16_t))}; break; } case multisense::Image::PixelFormat::BGR8: { - element_size = sizeof(uint8_t); + element_size = static_cast(sizeof(uint8_t)); format = py::format_descriptor::format(); shape.push_back(3); - strides = {sizeof(uint8_t) * image.width * 3, sizeof(uint8_t) * 3, sizeof(uint8_t)}; + strides = {static_cast(sizeof(uint8_t) * image.width * 3), + static_cast(sizeof(uint8_t) * 3), + static_cast(sizeof(uint8_t))}; break; } case multisense::Image::PixelFormat::FLOAT32: { - element_size = sizeof(float); + element_size = static_cast(sizeof(float)); format = py::format_descriptor::format(); - strides = {sizeof(float) * image.width, sizeof(float)}; + strides = {static_cast(sizeof(float) * image.width), + static_cast(sizeof(float))}; break; } default: {throw std::runtime_error("Unknown pixel format");} @@ -214,7 +227,7 @@ PYBIND11_MODULE(_libmultisense, m) { const_cast(image.raw_data->data() + image.image_data_offset), element_size, format, - shape.size(), + static_cast(shape.size()), shape, strides)); }) @@ -233,9 +246,9 @@ PYBIND11_MODULE(_libmultisense, m) { .def_readonly("bins", &multisense::ImageHistogram::bins) .def_property_readonly("as_array", [](const multisense::ImageHistogram& histogram) { - std::vector shape{histogram.data.size()}; - std::vector strides{sizeof(uint32_t)}; - size_t element_size = sizeof(uint32_t); + const SSizeVector shape{static_cast(histogram.data.size())}; + const SSizeVector strides{static_cast(sizeof(uint32_t))}; + const py::ssize_t element_size = static_cast(sizeof(uint32_t)); std::string format = py::format_descriptor::format(); // Map the cv::Mat to a NumPy array without copying the data @@ -243,7 +256,7 @@ PYBIND11_MODULE(_libmultisense, m) { const_cast(reinterpret_cast(histogram.data.data())), element_size, format, - shape.size(), + static_cast(shape.size()), shape, strides)); }); @@ -741,16 +754,22 @@ PYBIND11_MODULE(_libmultisense, m) { .def_readwrite("cloud", &multisense::PointCloud::cloud) .def_property_readonly("as_array", [](const multisense::PointCloud &cloud) { - const std::vector shape = {static_cast(cloud.cloud.size()), 3}; - const std::vector strides = {sizeof(multisense::Point), sizeof(float)}; - const size_t element_size = sizeof(float); + const SSizeVector shape = { + static_cast(cloud.cloud.size()), + static_cast(3) + }; + const SSizeVector strides = { + static_cast(sizeof(multisense::Point)), + static_cast(sizeof(float)) + }; + const py::ssize_t element_size = static_cast(sizeof(float)); const std::string format = py::format_descriptor::format();; return py::array(py::buffer_info( const_cast(reinterpret_cast(cloud.cloud.data())), element_size, format, - shape.size(), + static_cast(shape.size()), shape, strides)); }); @@ -760,33 +779,41 @@ PYBIND11_MODULE(_libmultisense, m) { .def_readwrite("cloud", &multisense::PointCloud::cloud) .def_property_readonly("as_array", [](const multisense::PointCloud &cloud) { - const std::vector shape = {static_cast(cloud.cloud.size()), 3}; + const SSizeVector shape = { + static_cast(cloud.cloud.size()), + static_cast(3) + }; // // Make sure we skip over the color // - const std::vector strides = {sizeof(multisense::Point), sizeof(float)}; - const size_t element_size = sizeof(multisense::Point); + const SSizeVector strides = { + static_cast(sizeof(multisense::Point)), + static_cast(sizeof(float)) + }; + const py::ssize_t element_size = static_cast(sizeof(multisense::Point)); const std::string format = py::format_descriptor::format();; return py::array(py::buffer_info( const_cast(reinterpret_cast(cloud.cloud.data())), element_size, format, - 2, + static_cast(shape.size()), shape, strides)); }) .def_property_readonly("as_raw_array", [](const multisense::PointCloud &cloud) { - const std::vector shape = {static_cast(cloud.cloud.size())}; - const std::vector strides = {sizeof(multisense::Point)}; - const size_t element_size = sizeof(multisense::Point); + const SSizeVector shape = {static_cast(cloud.cloud.size())}; + const SSizeVector strides = { + static_cast(sizeof(multisense::Point)) + }; + const py::ssize_t element_size = static_cast(sizeof(multisense::Point)); return py::array(py::buffer_info( const_cast(reinterpret_cast(cloud.cloud.data())), element_size, "13B", - 1, + static_cast(shape.size()), shape, strides)); }); @@ -797,33 +824,41 @@ PYBIND11_MODULE(_libmultisense, m) { .def_readwrite("cloud", &multisense::PointCloud::cloud) .def_property_readonly("as_array", [](const multisense::PointCloud &cloud) { - const std::vector shape = {static_cast(cloud.cloud.size()), 3}; + const SSizeVector shape = { + static_cast(cloud.cloud.size()), + static_cast(3) + }; // // Make sure we skip over the color // - const std::vector strides = {sizeof(multisense::Point), sizeof(float)}; - const size_t element_size = sizeof(multisense::Point); + const SSizeVector strides = { + static_cast(sizeof(multisense::Point)), + static_cast(sizeof(float)) + }; + const py::ssize_t element_size = static_cast(sizeof(multisense::Point)); const std::string format = py::format_descriptor::format();; return py::array(py::buffer_info( const_cast(reinterpret_cast(cloud.cloud.data())), element_size, format, - 2, + static_cast(shape.size()), shape, strides)); }) .def_property_readonly("as_raw_array", [](const multisense::PointCloud &cloud) { - const std::vector shape = {static_cast(cloud.cloud.size())}; - const std::vector strides = {sizeof(multisense::Point)}; - const size_t element_size = sizeof(multisense::Point); + const SSizeVector shape = {static_cast(cloud.cloud.size())}; + const SSizeVector strides = { + static_cast(sizeof(multisense::Point)) + }; + const py::ssize_t element_size = static_cast(sizeof(multisense::Point)); return py::array(py::buffer_info( const_cast(reinterpret_cast(cloud.cloud.data())), element_size, "14B", - 1, + static_cast(shape.size()), shape, strides)); }); @@ -833,33 +868,43 @@ PYBIND11_MODULE(_libmultisense, m) { .def_readwrite("cloud", &multisense::PointCloud>::cloud) .def_property_readonly("as_array", [](const multisense::PointCloud> &cloud) { - const std::vector shape = {static_cast(cloud.cloud.size()), 3}; + const SSizeVector shape = { + static_cast(cloud.cloud.size()), + static_cast(3) + }; // // Make sure we skip over the color // - const std::vector strides = {sizeof(multisense::Point>), sizeof(float)}; - const size_t element_size = sizeof(multisense::Point>); + const SSizeVector strides = { + static_cast(sizeof(multisense::Point>)), + static_cast(sizeof(float)) + }; + const py::ssize_t element_size = + static_cast(sizeof(multisense::Point>)); const std::string format = py::format_descriptor::format();; return py::array(py::buffer_info( const_cast(reinterpret_cast(cloud.cloud.data())), element_size, format, - 2, + static_cast(shape.size()), shape, strides)); }) .def_property_readonly("as_raw_array", [](const multisense::PointCloud> &cloud) { - const std::vector shape = {static_cast(cloud.cloud.size())}; - const std::vector strides = {sizeof(multisense::Point>)}; - const size_t element_size = sizeof(multisense::Point>); + const SSizeVector shape = {static_cast(cloud.cloud.size())}; + const SSizeVector strides = { + static_cast(sizeof(multisense::Point>)) + }; + const py::ssize_t element_size = + static_cast(sizeof(multisense::Point>)); return py::array(py::buffer_info( const_cast(reinterpret_cast(cloud.cloud.data())), element_size, "15B", - 1, + static_cast(shape.size()), shape, strides)); }); From 980bb8c3138cf9268e3e43ffe665dbca187a11ec Mon Sep 17 00:00:00 2001 From: Matt Alvarado Date: Mon, 15 Dec 2025 14:04:05 -0500 Subject: [PATCH 2/3] Revert some changes --- python/bindings.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/python/bindings.cc b/python/bindings.cc index b8bed256..78c03ff5 100644 --- a/python/bindings.cc +++ b/python/bindings.cc @@ -797,7 +797,7 @@ PYBIND11_MODULE(_libmultisense, m) { const_cast(reinterpret_cast(cloud.cloud.data())), element_size, format, - static_cast(shape.size()), + 2, shape, strides)); }) @@ -813,7 +813,7 @@ PYBIND11_MODULE(_libmultisense, m) { const_cast(reinterpret_cast(cloud.cloud.data())), element_size, "13B", - static_cast(shape.size()), + 1, shape, strides)); }); @@ -842,7 +842,7 @@ PYBIND11_MODULE(_libmultisense, m) { const_cast(reinterpret_cast(cloud.cloud.data())), element_size, format, - static_cast(shape.size()), + 2, shape, strides)); }) @@ -858,7 +858,7 @@ PYBIND11_MODULE(_libmultisense, m) { const_cast(reinterpret_cast(cloud.cloud.data())), element_size, "14B", - static_cast(shape.size()), + 1, shape, strides)); }); @@ -887,7 +887,7 @@ PYBIND11_MODULE(_libmultisense, m) { const_cast(reinterpret_cast(cloud.cloud.data())), element_size, format, - static_cast(shape.size()), + 2, shape, strides)); }) @@ -904,7 +904,7 @@ PYBIND11_MODULE(_libmultisense, m) { const_cast(reinterpret_cast(cloud.cloud.data())), element_size, "15B", - static_cast(shape.size()), + 1, shape, strides)); }); From f7d4b5ba12b464f457839d5007151a65ceac3c25 Mon Sep 17 00:00:00 2001 From: Matt Alvarado Date: Mon, 15 Dec 2025 14:08:38 -0500 Subject: [PATCH 3/3] Revert change --- python/bindings.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/bindings.cc b/python/bindings.cc index 78c03ff5..cde6995c 100644 --- a/python/bindings.cc +++ b/python/bindings.cc @@ -173,7 +173,7 @@ PYBIND11_MODULE(_libmultisense, m) { const_cast(image.raw_data->data() + image.image_data_offset), static_cast(sizeof(uint8_t)), py::format_descriptor::format(), - static_cast(shape.size()), + 1, shape, strides)); }