From 5454f80dde307113097130d4262ef208f49a4274 Mon Sep 17 00:00:00 2001 From: Joris Vaillant Date: Wed, 25 Sep 2024 12:37:16 +0200 Subject: [PATCH 1/3] python: Manage new numpy2 __array__ API --- include/pinocchio/bindings/python/spatial/force.hpp | 4 ++-- include/pinocchio/bindings/python/spatial/inertia.hpp | 4 ++-- include/pinocchio/bindings/python/spatial/motion.hpp | 4 ++-- include/pinocchio/bindings/python/spatial/se3.hpp | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/pinocchio/bindings/python/spatial/force.hpp b/include/pinocchio/bindings/python/spatial/force.hpp index 999277a3c9..8b88eb9f6a 100644 --- a/include/pinocchio/bindings/python/spatial/force.hpp +++ b/include/pinocchio/bindings/python/spatial/force.hpp @@ -161,7 +161,7 @@ namespace pinocchio "__array__", bp::make_function( (typename Force::ToVectorReturnType(Force::*)()) & Force::toVector, bp::return_internal_reference<>())) - .def("__array__", &__array__, bp::return_internal_reference<>()) + .def("__array__", &__array__, (bp::arg("self"), bp::arg("dtype")=bp::object(), bp::arg("copy")=bp::object()), bp::return_internal_reference<>()) #ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION .def_pickle(Pickle()) #endif @@ -197,7 +197,7 @@ namespace pinocchio } private: - static typename Force::ToVectorConstReturnType __array__(const Force & self, bp::object) + static typename Force::ToVectorConstReturnType __array__(const Force & self, bp::object, bp::object) { return self.toVector(); } diff --git a/include/pinocchio/bindings/python/spatial/inertia.hpp b/include/pinocchio/bindings/python/spatial/inertia.hpp index 31b617923e..74eb01baf7 100644 --- a/include/pinocchio/bindings/python/spatial/inertia.hpp +++ b/include/pinocchio/bindings/python/spatial/inertia.hpp @@ -193,7 +193,7 @@ namespace pinocchio .staticmethod("FromCapsule") .def("__array__", (Matrix6(Inertia::*)() const) & Inertia::matrix) - .def("__array__", &__array__) + .def("__array__", &__array__, (bp::arg("self"), bp::arg("dtype")=bp::object(), bp::arg("copy")=bp::object())) #ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION .def_pickle(Pickle()) #endif @@ -273,7 +273,7 @@ namespace pinocchio } private: - static Matrix6 __array__(const Inertia & self, bp::object) + static Matrix6 __array__(const Inertia & self, bp::object, bp::object) { return self.matrix(); } diff --git a/include/pinocchio/bindings/python/spatial/motion.hpp b/include/pinocchio/bindings/python/spatial/motion.hpp index 0a33bb22fe..f77c5fe653 100644 --- a/include/pinocchio/bindings/python/spatial/motion.hpp +++ b/include/pinocchio/bindings/python/spatial/motion.hpp @@ -184,7 +184,7 @@ namespace pinocchio "__array__", bp::make_function( (typename Motion::ToVectorReturnType(Motion::*)()) & Motion::toVector, bp::return_internal_reference<>())) - .def("__array__", &__array__, bp::return_internal_reference<>()) + .def("__array__", &__array__, (bp::arg("self"), bp::arg("dtype")=bp::object(), bp::arg("copy")=bp::object()), bp::return_internal_reference<>()) #ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION .def_pickle(Pickle()) #endif @@ -220,7 +220,7 @@ namespace pinocchio } private: - static typename Motion::ToVectorConstReturnType __array__(const Motion & self, bp::object) + static typename Motion::ToVectorConstReturnType __array__(const Motion & self, bp::object, bp::object) { return self.toVector(); } diff --git a/include/pinocchio/bindings/python/spatial/se3.hpp b/include/pinocchio/bindings/python/spatial/se3.hpp index 55e11bc99d..2a9c11966b 100644 --- a/include/pinocchio/bindings/python/spatial/se3.hpp +++ b/include/pinocchio/bindings/python/spatial/se3.hpp @@ -197,7 +197,7 @@ namespace pinocchio .staticmethod("Interpolate") .def("__array__", &SE3::toHomogeneousMatrix) - .def("__array__", &__array__) + .def("__array__", &__array__, (bp::arg("self"), bp::arg("dtype")=bp::object(), bp::arg("copy")=bp::object())) #ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION .def_pickle(Pickle()) @@ -230,7 +230,7 @@ namespace pinocchio } private: - static Matrix4 __array__(const SE3 & self, bp::object) + static Matrix4 __array__(const SE3 & self, bp::object, bp::object) { return self.toHomogeneousMatrix(); } From 55169dae8abaeedea081b373f594a8d6f363aebb Mon Sep 17 00:00:00 2001 From: Joris Vaillant Date: Wed, 25 Sep 2024 12:40:35 +0200 Subject: [PATCH 2/3] changelog: Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff5c41beeb..b266f34b23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Default visualizer can be changed with `PINOCCHIO_VIEWER` environment variable ([#2419](https://github.com/stack-of-tasks/pinocchio/pull/2419)) - Add more Python and C++ examples related to inverse kinematics with 3d tasks ([#2428](https://github.com/stack-of-tasks/pinocchio/pull/2428)) - Add parsing of equality/connect tag for closed-loop chains for MJCF format ([#2413](https://github.com/stack-of-tasks/pinocchio/pull/2413)) +- Add compatibility with NumPy 2 `__array__` API ([#2436](https://github.com/stack-of-tasks/pinocchio/pull/2436)) ### Fixed - Fix linkage of Boost.Serialization on Windows ([#2400](https://github.com/stack-of-tasks/pinocchio/pull/2400)) From ed3439c394ceef3387f6b086d13385bfaab5cc82 Mon Sep 17 00:00:00 2001 From: Joris Vaillant Date: Wed, 25 Sep 2024 13:53:01 +0200 Subject: [PATCH 3/3] python: Apply clang-format --- include/pinocchio/bindings/python/spatial/force.hpp | 8 ++++++-- include/pinocchio/bindings/python/spatial/inertia.hpp | 4 +++- include/pinocchio/bindings/python/spatial/motion.hpp | 8 ++++++-- include/pinocchio/bindings/python/spatial/se3.hpp | 4 +++- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/pinocchio/bindings/python/spatial/force.hpp b/include/pinocchio/bindings/python/spatial/force.hpp index 8b88eb9f6a..4ccb66506c 100644 --- a/include/pinocchio/bindings/python/spatial/force.hpp +++ b/include/pinocchio/bindings/python/spatial/force.hpp @@ -161,7 +161,10 @@ namespace pinocchio "__array__", bp::make_function( (typename Force::ToVectorReturnType(Force::*)()) & Force::toVector, bp::return_internal_reference<>())) - .def("__array__", &__array__, (bp::arg("self"), bp::arg("dtype")=bp::object(), bp::arg("copy")=bp::object()), bp::return_internal_reference<>()) + .def( + "__array__", &__array__, + (bp::arg("self"), bp::arg("dtype") = bp::object(), bp::arg("copy") = bp::object()), + bp::return_internal_reference<>()) #ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION .def_pickle(Pickle()) #endif @@ -197,7 +200,8 @@ namespace pinocchio } private: - static typename Force::ToVectorConstReturnType __array__(const Force & self, bp::object, bp::object) + static typename Force::ToVectorConstReturnType + __array__(const Force & self, bp::object, bp::object) { return self.toVector(); } diff --git a/include/pinocchio/bindings/python/spatial/inertia.hpp b/include/pinocchio/bindings/python/spatial/inertia.hpp index 74eb01baf7..ec829e5619 100644 --- a/include/pinocchio/bindings/python/spatial/inertia.hpp +++ b/include/pinocchio/bindings/python/spatial/inertia.hpp @@ -193,7 +193,9 @@ namespace pinocchio .staticmethod("FromCapsule") .def("__array__", (Matrix6(Inertia::*)() const) & Inertia::matrix) - .def("__array__", &__array__, (bp::arg("self"), bp::arg("dtype")=bp::object(), bp::arg("copy")=bp::object())) + .def( + "__array__", &__array__, + (bp::arg("self"), bp::arg("dtype") = bp::object(), bp::arg("copy") = bp::object())) #ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION .def_pickle(Pickle()) #endif diff --git a/include/pinocchio/bindings/python/spatial/motion.hpp b/include/pinocchio/bindings/python/spatial/motion.hpp index f77c5fe653..5606682b1e 100644 --- a/include/pinocchio/bindings/python/spatial/motion.hpp +++ b/include/pinocchio/bindings/python/spatial/motion.hpp @@ -184,7 +184,10 @@ namespace pinocchio "__array__", bp::make_function( (typename Motion::ToVectorReturnType(Motion::*)()) & Motion::toVector, bp::return_internal_reference<>())) - .def("__array__", &__array__, (bp::arg("self"), bp::arg("dtype")=bp::object(), bp::arg("copy")=bp::object()), bp::return_internal_reference<>()) + .def( + "__array__", &__array__, + (bp::arg("self"), bp::arg("dtype") = bp::object(), bp::arg("copy") = bp::object()), + bp::return_internal_reference<>()) #ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION .def_pickle(Pickle()) #endif @@ -220,7 +223,8 @@ namespace pinocchio } private: - static typename Motion::ToVectorConstReturnType __array__(const Motion & self, bp::object, bp::object) + static typename Motion::ToVectorConstReturnType + __array__(const Motion & self, bp::object, bp::object) { return self.toVector(); } diff --git a/include/pinocchio/bindings/python/spatial/se3.hpp b/include/pinocchio/bindings/python/spatial/se3.hpp index 2a9c11966b..7fe855a9e5 100644 --- a/include/pinocchio/bindings/python/spatial/se3.hpp +++ b/include/pinocchio/bindings/python/spatial/se3.hpp @@ -197,7 +197,9 @@ namespace pinocchio .staticmethod("Interpolate") .def("__array__", &SE3::toHomogeneousMatrix) - .def("__array__", &__array__, (bp::arg("self"), bp::arg("dtype")=bp::object(), bp::arg("copy")=bp::object())) + .def( + "__array__", &__array__, + (bp::arg("self"), bp::arg("dtype") = bp::object(), bp::arg("copy") = bp::object())) #ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION .def_pickle(Pickle())