diff --git a/bindings/pybind11/idyntree_core.cpp b/bindings/pybind11/idyntree_core.cpp index ba4bf16e9d8..76b0aea65be 100644 --- a/bindings/pybind11/idyntree_core.cpp +++ b/bindings/pybind11/idyntree_core.cpp @@ -1,4 +1,5 @@ #include "idyntree_core.h" +#include "idyntree_type_caster.h" #include #include @@ -24,136 +25,6 @@ namespace bindings { namespace { namespace py = ::pybind11; -void vectorDynSizeClassDefinition(py::class_& vector) { - vector.def(py::init()) - .def(py::init()) - .def("__getitem__", py::overload_cast( - &VectorDynSize::operator(), py::const_)) - .def("__setitem__", - [](VectorDynSize& the_vector, std::size_t index, double new_value) { - the_vector(index) = new_value; - }) - .def( - "__iter__", - [](const VectorDynSize& s) { - return py::make_iterator(s.begin(), s.end()); - }, - py::keep_alive< - 0, 1>() /* Essential: keep object alive while iterator exists */) - .def("__len__", &VectorDynSize::size) - .def("set_zero", &VectorDynSize::zero) - .def("resize", &VectorDynSize::resize) - .def("__repr__", &VectorDynSize::toString) - .def_buffer([](VectorDynSize& v) -> py::buffer_info { - return py::buffer_info( - v.data(), /* Pointer to buffer */ - sizeof(double), /* Size of one scalar */ - py::format_descriptor::format(), /* Python struct-style - format descriptor */ - 1, /* Number of dimensions */ - {v.size()}, /* Buffer dimensions */ - {sizeof(double)} /* Strides (in bytes) for each index */ - ); - }); -} - -template -void createFixSizeVector(pybind11::module& module, - const std::string& class_name) { - py::class_>(module, class_name.c_str(), - py::buffer_protocol()) - .def(py::init()) - .def("__getitem__", py::overload_cast( - &VectorFixSize::operator(), py::const_)) - .def("__setitem__", - [](VectorFixSize& the_vector, std::size_t index, - double new_value) { the_vector(index) = new_value; }) - .def("__len__", &VectorFixSize::size) - .def( - "__iter__", - [](const VectorFixSize& s) { - return py::make_iterator(s.begin(), s.end()); - }, - py::keep_alive< - 0, 1>() /* Essential: keep object alive while iterator exists */) - .def("set_zero", &VectorFixSize::zero) - .def("__repr__", &VectorFixSize::toString) - .def_buffer([](VectorFixSize& v) -> py::buffer_info { - return py::buffer_info( - v.data(), /* Pointer to buffer */ - sizeof(double), /* Size of one scalar */ - py::format_descriptor::format(), /* Python struct-style - format descriptor */ - 1, /* Number of dimensions */ - {v.size()}, /* Buffer dimensions */ - {sizeof(double)} /* Strides (in bytes) for each index */ - ); - }); -} - -void matrixDynSizeClassDefinition(py::class_& matrix) { - matrix.def(py::init()) - .def(py::init()) - .def("__getitem__", - [](MatrixDynSize& matrix, - std::pair indices) { - return matrix.getVal(indices.first, indices.second); - }) - .def("__setitem__", - [](MatrixDynSize& matrix, - std::pair indices, double item) { - return matrix.setVal(indices.first, indices.second, item); - }) - .def("rows", &MatrixDynSize::rows) - .def("cols", &MatrixDynSize::cols) - .def("set_zero", &MatrixDynSize::zero) - .def("resize", &MatrixDynSize::resize) - .def("__repr__", &MatrixDynSize::toString) - .def_buffer([](MatrixDynSize& m) -> py::buffer_info { - return py::buffer_info( - m.data(), /* Pointer to buffer */ - sizeof(double), /* Size of one scalar */ - py::format_descriptor::format(), /* Python struct-style - format descriptor */ - 2, /* Number of dimensions */ - {m.rows(), m.cols()}, /* Buffer dimensions */ - {sizeof(double) * m.cols(), /* Strides (in bytes) for each index */ - sizeof(double)}); - }); -} - -template -void createFixSizeMatrix(pybind11::module& module, - const std::string& class_name) { - py::class_>(module, class_name.c_str(), - py::buffer_protocol()) - .def(py::init()) - .def("__getitem__", - [](MatrixFixSize& matrix, - std::pair indices) { - return matrix.getVal(indices.first, indices.second); - }) - .def("__setitem__", - [](MatrixFixSize& matrix, - std::pair indices, double item) { - return matrix.setVal(indices.first, indices.second, item); - }) - .def("rows", &MatrixFixSize::rows) - .def("cols", &MatrixFixSize::cols) - .def("set_zero", &MatrixFixSize::zero) - .def("__repr__", &MatrixFixSize::toString) - .def_buffer([](MatrixFixSize& m) -> py::buffer_info { - return py::buffer_info( - m.data(), /* Pointer to buffer */ - sizeof(double), /* Size of one scalar */ - py::format_descriptor::format(), /* Python struct-style - format descriptor */ - 2, /* Number of dimensions */ - {m.rows(), m.cols()}, /* Buffer dimensions */ - {sizeof(double) * m.cols(), /* Strides (in bytes) for each index */ - sizeof(double)}); - }); -} void transformClassDefinition(py::class_& transform) { transform.def(py::init()) @@ -179,89 +50,64 @@ void transformClassDefinition(py::class_& transform) { } // namespace void iDynTreeCoreBindings(pybind11::module& module) { - // Vectors and matrices. - py::class_ vector_dyn(module, "VectorDynSize", - py::buffer_protocol()); - vectorDynSizeClassDefinition(vector_dyn); - createFixSizeVector<3>(module, "Vector3"); - createFixSizeVector<4>(module, "Vector4"); - createFixSizeVector<6>(module, "Vector6"); - - py::class_ matrix_dyn(module, "MatrixDynSize", - py::buffer_protocol()); - matrixDynSizeClassDefinition(matrix_dyn); - createFixSizeMatrix<3, 3>(module, "Matrix3x3"); - createFixSizeMatrix<4, 4>(module, "Matrix4x4"); - createFixSizeMatrix<6, 6>(module, "Matrix6x6"); - - // Positions, Rotations and Transforms. - py::class_>(module, "_PositionRaw") - // Do not expose constructor as we do not want users to use this class. - .def("__repr__", &PositionRaw::toString); - - py::class_(module, "Position") - .def(py::init()) - .def(py::init()) - .def(py::self + py::self) - .def(py::self - py::self) - .def(-py::self) - .def_static("Zero", &Position::Zero); - - py::class_>(module, "_RotationRaw") - // Do not expose constructor as we do not want users to use this class. - .def("__repr__", &RotationRaw::toString); - - py::class_(module, "Rotation") - .def(py::init()) - .def(py::init()) - .def("inverse", &Rotation::inverse) - .def(py::self * py::self) - .def( - "__mul__", - [](const Rotation& r, const Position& p) -> Position { - return r * p; - }, - py::is_operator()) - .def_static("Identity", &Rotation::Identity); - - py::class_ transform(module, "Transform"); - transformClassDefinition(transform); - - // Other classes. - py::class_>(module, "Direction") - .def(py::init()); - - py::class_(module, "Axis") - .def(py::init()) - .def_property("direction", &Axis::getDirection, &Axis::setDirection) - .def_property("origin", &Axis::getOrigin, &Axis::setOrigin) - .def("__repr__", &Axis::toString); - - py::class_>(module, - "RotationalInertia") - .def(py::init()); - - py::class_(module, "_SpatialInertiaRaw") - .def("from_rotational_inertia_wrt_center_of_mass", - &SpatialInertiaRaw::fromRotationalInertiaWrtCenterOfMass) - .def("get_mass", &SpatialInertiaRaw::getMass) - .def("get_center_of_mass", &SpatialInertiaRaw::getCenterOfMass) - .def("get_rotational_inertia_wrt_frame_origin", - &SpatialInertiaRaw::getRotationalInertiaWrtFrameOrigin) - .def("get_rotational_inertia_wrt_center_of_mass", - &SpatialInertiaRaw::getRotationalInertiaWrtCenterOfMass); - py::class_(module, "SpatialInertia") - .def(py::init()) - .def(py::init()) - .def_static("Zero", &SpatialInertia::Zero) - .def("as_matrix", &SpatialInertia::asMatrix) - .def(py::self + py::self) - .def("__repr__", [](const SpatialInertia& inertia) { - return inertia.asMatrix().toString(); - }); + py::class_(module, "Rotation") + .def(py::init()) + .def(py::init()) + .def("inverse", &Rotation::inverse) + .def(py::self * py::self) + .def( + "__mul__", + [](const Rotation& r, const Position& p) -> Position { return r * p; }, + py::is_operator()) + .def_static("Identity", &Rotation::Identity) + .def("__repr__", &Rotation::toString) + .def("to_numpy", + [](const Rotation& impl) { + iDynTree::Matrix3x3 m(impl.data(), 3, 3); + return m; + }) + .def("__getitem__", + [](const Rotation& matrix, std::pair indices) { + return matrix.getVal(indices.first, indices.second); + }); + + py::class_ transform(module, "Transform"); + transformClassDefinition(transform); + + // Other classes. + py::class_(module, "Axis") + .def(py::init()) + .def_property("direction", &Axis::getDirection, &Axis::setDirection) + .def_property("origin", &Axis::getOrigin, &Axis::setOrigin) + .def("__repr__", &Axis::toString); + + py::class_(module, "_SpatialInertiaRaw") + .def("from_rotational_inertia_wrt_center_of_mass", + &SpatialInertiaRaw::fromRotationalInertiaWrtCenterOfMass) + .def("get_mass", &SpatialInertiaRaw::getMass) + .def("get_center_of_mass", &SpatialInertiaRaw::getCenterOfMass) + .def("get_rotational_inertia_wrt_frame_origin", + &SpatialInertiaRaw::getRotationalInertiaWrtFrameOrigin) + .def("get_rotational_inertia_wrt_center_of_mass", + &SpatialInertiaRaw::getRotationalInertiaWrtCenterOfMass); + + py::class_(module, "SpatialInertia") + .def(py::init()) + .def(py::init()) + .def_static("Zero", &SpatialInertia::Zero) + .def("as_matrix", &SpatialInertia::asMatrix) + .def(py::self + py::self) + .def("__repr__", + [](const SpatialInertia& inertia) { return inertia.asMatrix().toString(); }); } } // namespace bindings } // namespace iDynTree diff --git a/bindings/pybind11/idyntree_model.cpp b/bindings/pybind11/idyntree_model.cpp index 65d47fa0846..7ab564f428a 100644 --- a/bindings/pybind11/idyntree_model.cpp +++ b/bindings/pybind11/idyntree_model.cpp @@ -1,4 +1,5 @@ #include "idyntree_model.h" +#include "idyntree_type_caster.h" #include "error_utilities.h" diff --git a/bindings/pybind11/idyntree_modelio_urdf.cpp b/bindings/pybind11/idyntree_modelio_urdf.cpp index e5d99bd1488..84efbb110e9 100644 --- a/bindings/pybind11/idyntree_modelio_urdf.cpp +++ b/bindings/pybind11/idyntree_modelio_urdf.cpp @@ -1,6 +1,6 @@ #include "idyntree_modelio_urdf.h" - #include "error_utilities.h" +#include "idyntree_type_caster.h" #include #include diff --git a/bindings/pybind11/idyntree_sensors.cpp b/bindings/pybind11/idyntree_sensors.cpp index 2ade2842315..13abeccc9e6 100644 --- a/bindings/pybind11/idyntree_sensors.cpp +++ b/bindings/pybind11/idyntree_sensors.cpp @@ -1,5 +1,6 @@ #include "idyntree_sensors.h" #include "error_utilities.h" +#include "idyntree_type_caster.h" #include #include