Skip to content

Commit

Permalink
some work on the places where buffer is still needed
Browse files Browse the repository at this point in the history
  • Loading branch information
fraguada committed Oct 28, 2024
1 parent 85def9c commit 4cd7f66
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
35 changes: 35 additions & 0 deletions src/bindings/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <nanobind/stl/string.h>
#include <nanobind/stl/tuple.h>
#include <nanobind/operators.h>
#include <vector>
namespace py = nanobind;
typedef nanobind::module_ rh3dmpymodule;
#define RH3DM_PYTHON_BINDING(name, variable) NB_MODULE(name, variable)
Expand Down Expand Up @@ -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<ssize_t> shape;
std::vector<ssize_t> strides;

buffer_info(
std::string format,
std::vector<ssize_t> shape_in,
std::vector<ssize_t> 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<typename T>
Expand Down
10 changes: 8 additions & 2 deletions src/bindings/bnd_draco.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(info.size), (const char*)info.ptr);
})
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/bnd_extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(info.size), info.ptr);
Expand Down
13 changes: 13 additions & 0 deletions src/bindings/bnd_nurbscurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>::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"))
Expand Down

0 comments on commit 4cd7f66

Please sign in to comment.