diff --git a/src/bindings/bindings.h b/src/bindings/bindings.h index c8d39c56..56d98f37 100644 --- a/src/bindings/bindings.h +++ b/src/bindings/bindings.h @@ -12,6 +12,7 @@ #include #include #include + #include namespace py = nanobind; typedef nanobind::module_ rh3dmpymodule; #define RH3DM_PYTHON_BINDING(name, variable) NB_MODULE(name, variable) @@ -78,6 +79,40 @@ void SetTuple(BND_TUPLE& tuple, int index, const T& value) #endif } +//buffer_info for nanobind +#if defined(ON_PYTHON_COMPILE) && defined(NANOBIND) + +struct buffer_info { + std::string format; + std::vector shape; + std::vector strides; + + buffer_info( + std::string format, + std::vector shape_in, + std::vector strides_in) + : format(std::move(format)), + shape(std::move(shape_in)), + strides(std::move(strides_in)) {} + + buffer_info(const buffer_info&) = delete; + buffer_info& operator=(const buffer_info&) = delete; + + buffer_info(buffer_info&& other) noexcept { + (*this) = std::move(other); + } + + buffer_info& operator=(buffer_info&& rhs) noexcept { + format = std::move(rhs.format); + shape = std::move(rhs.shape); + strides = std::move(rhs.strides); + return *this; + } +}; + + +#endif + BND_LIST CreateList(int count); BND_LIST NullList(); template diff --git a/src/bindings/bnd_draco.cpp b/src/bindings/bnd_draco.cpp index 2dd87d34..76be60b2 100644 --- a/src/bindings/bnd_draco.cpp +++ b/src/bindings/bnd_draco.cpp @@ -420,9 +420,15 @@ void initDracoBindings(rh3dmpymodule& m) .def_static("Compress", &BND_Draco::CompressMesh, py::arg("mesh")) .def_static("Compress", &BND_Draco::CompressMesh2, py::arg("mesh"), py::arg("options")) .def("Write", &BND_Draco::WriteToFile) -#if !defined(NANOBIND) +#if defined(NANOBIND) + .def_static("DecompressByteArray", [](py::bytes b) { + const char* data = b.c_str(); + int length = b.size(); + return BND_Draco::DecompressByteArray(length, data); + }) +#else .def_static("DecompressByteArray", [](py::buffer b) { - py::buffer_info info = b.request(); + buffer_info info = b.request(); return BND_Draco::DecompressByteArray(static_cast(info.size), (const char*)info.ptr); }) #endif diff --git a/src/bindings/bnd_extensions.cpp b/src/bindings/bnd_extensions.cpp index 4bab7cb7..61414ac6 100644 --- a/src/bindings/bnd_extensions.cpp +++ b/src/bindings/bnd_extensions.cpp @@ -1961,7 +1961,7 @@ void initExtensionsBindings(rh3dmpymodule& m) .def_static("Read", &BND_ONXModel::Read, py::arg("path")) .def_static("ReadNotes", &BND_ONXModel::ReadNotes, py::arg("path")) .def_static("ReadArchiveVersion", &BND_ONXModel::ReadArchiveVersion, py::arg("path")) -/* + /* TODO: implement FromByteArray in py .def_static("FromByteArray", [](py::buffer b) { py::buffer_info info = b.request(); return BND_ONXModel::FromByteArray(static_cast(info.size), info.ptr); diff --git a/src/bindings/bnd_nurbscurve.cpp b/src/bindings/bnd_nurbscurve.cpp index aa3035ab..94c8f08e 100644 --- a/src/bindings/bnd_nurbscurve.cpp +++ b/src/bindings/bnd_nurbscurve.cpp @@ -279,6 +279,19 @@ void initNurbsCurveBindings(rh3dmpymodule& m) {pl.GetCurve()->m_cv_stride * sizeof(double), sizeof(double)} /* Strides (in bytes) for each index */ ); }) +#elif defined(NANOBIND) + .def_buffer([](BND_NurbsCurvePointList& pl) -> buffer_info + { + return buffer_info + ( + pl.GetCurve()->m_cv, /* Pointer to buffer */ + sizeof(double), /* Size of one scalar */ + py::format_descriptor::format(), /* Python struct-style format descriptor */ + 2, /* Number of dimensions */ + {pl.Count(), pl.GetCVDims() }, /* Buffer dimensions */ + {pl.GetCurve()->m_cv_stride * sizeof(double), sizeof(double)} /* Strides (in bytes) for each index */ + ); + }) #endif .def_property_readonly("ControlPolygonLength", &BND_NurbsCurvePointList::ControlPolygonLength) .def("ChangeEndWeights", &BND_NurbsCurvePointList::ChangeEndWeights, py::arg("w0"), py::arg("w1"))