From 561796776612b236fecd57abcaf88d93d3991e1c Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Mon, 28 Dec 2020 17:36:32 +0100 Subject: [PATCH 1/9] geometry: move overload to visitor --- include/eigenpy/angle-axis.hpp | 4 ++-- include/eigenpy/quaternion.hpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/eigenpy/angle-axis.hpp b/include/eigenpy/angle-axis.hpp index ea78f4dff..d5ad5aa14 100644 --- a/include/eigenpy/angle-axis.hpp +++ b/include/eigenpy/angle-axis.hpp @@ -34,8 +34,6 @@ namespace eigenpy } }; - BOOST_PYTHON_FUNCTION_OVERLOADS(isApproxAngleAxis_overload,call::isApprox,2,3) - template class AngleAxisVisitor : public bp::def_visitor< AngleAxisVisitor > @@ -48,6 +46,8 @@ namespace eigenpy typedef typename Eigen::Quaternion Quaternion; typedef Eigen::RotationBase RotationBase; + BOOST_PYTHON_FUNCTION_OVERLOADS(isApproxAngleAxis_overload,call::isApprox,2,3) + public: template diff --git a/include/eigenpy/quaternion.hpp b/include/eigenpy/quaternion.hpp index 6d5f1ffc8..05d88a64c 100644 --- a/include/eigenpy/quaternion.hpp +++ b/include/eigenpy/quaternion.hpp @@ -88,8 +88,6 @@ namespace eigenpy } }; - BOOST_PYTHON_FUNCTION_OVERLOADS(isApproxQuaternion_overload,call::isApprox,2,3) - template class QuaternionVisitor : public bp::def_visitor< QuaternionVisitor > @@ -103,6 +101,8 @@ namespace eigenpy typedef typename QuaternionBase::Matrix3 Matrix3; typedef typename QuaternionBase::AngleAxisType AngleAxis; + + BOOST_PYTHON_FUNCTION_OVERLOADS(isApproxQuaternion_overload,call::isApprox,2,3) public: From b083d753f68c5ac595dc4700936d784e7521bfd3 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Mon, 28 Dec 2020 17:40:06 +0100 Subject: [PATCH 2/9] core: add inline --- include/eigenpy/user-type.hpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/include/eigenpy/user-type.hpp b/include/eigenpy/user-type.hpp index 7181063e5..20c12cabf 100644 --- a/include/eigenpy/user-type.hpp +++ b/include/eigenpy/user-type.hpp @@ -16,13 +16,13 @@ namespace eigenpy template::type_code> struct SpecialMethods { - static void copyswap(void * /*dst*/, void * /*src*/, int /*swap*/, void * /*arr*/) {}; - static PyObject * getitem(void * /*ip*/, void * /*ap*/) { return NULL; }; - static int setitem(PyObject * /*op*/, void * /*ov*/, void * /*ap*/) { return -1; } - static void copyswapn(void * /*dest*/, long /*dstride*/, void * /*src*/, - long /*sstride*/, long /*n*/, int /*swap*/, void * /*arr*/) {}; - static npy_bool nonzero(void * /*ip*/, void * /*array*/) { return (npy_bool)false; }; - static void dotfunc(void * /*ip0_*/, npy_intp /*is0*/, void * /*ip1_*/, npy_intp /*is1*/, + inline static void copyswap(void * /*dst*/, void * /*src*/, int /*swap*/, void * /*arr*/) /*{}*/; + inline static PyObject * getitem(void * /*ip*/, void * /*ap*/) /*{ return NULL; }*/; + inline static int setitem(PyObject * /*op*/, void * /*ov*/, void * /*ap*/) /*{ return -1; }*/; + inline static void copyswapn(void * /*dest*/, long /*dstride*/, void * /*src*/, + long /*sstride*/, long /*n*/, int /*swap*/, void * /*arr*/) /*{}*/; + inline static npy_bool nonzero(void * /*ip*/, void * /*array*/) /*{ return (npy_bool)false; }*/; + inline static void dotfunc(void * /*ip0_*/, npy_intp /*is0*/, void * /*ip1_*/, npy_intp /*is1*/, void * /*op*/, npy_intp /*n*/, void * /*arr*/); // static void cast(void * /*from*/, void * /*to*/, npy_intp /*n*/, void * /*fromarr*/, void * /*toarr*/) {}; }; @@ -30,7 +30,7 @@ namespace eigenpy template struct SpecialMethods { - static void copyswap(void * dst, void * src, int swap, void * /*arr*/) + inline static void copyswap(void * dst, void * src, int swap, void * /*arr*/) { // std::cout << "copyswap" << std::endl; if (src != NULL) @@ -48,7 +48,7 @@ namespace eigenpy } } - static PyObject * getitem(void * ip, void * ap) + inline static PyObject * getitem(void * ip, void * ap) { // std::cout << "getitem" << std::endl; PyArrayObject * py_array = static_cast(ap); @@ -68,7 +68,7 @@ namespace eigenpy } } - static int setitem(PyObject * src_obj, void * dest_ptr, void * array) + inline static int setitem(PyObject * src_obj, void * dest_ptr, void * array) { // std::cout << "setitem" << std::endl; if(array == NULL) @@ -103,8 +103,8 @@ namespace eigenpy return 0; } - static void copyswapn(void * dst, long dstride, void * src, long sstride, - long n, int swap, void * array) + inline static void copyswapn(void * dst, long dstride, void * src, long sstride, + long n, int swap, void * array) { // std::cout << "copyswapn" << std::endl; @@ -122,7 +122,7 @@ namespace eigenpy } } - static npy_bool nonzero(void * ip, void * array) + inline static npy_bool nonzero(void * ip, void * array) { // std::cout << "nonzero" << std::endl; static const T ZeroValue = T(0); @@ -141,8 +141,8 @@ namespace eigenpy } } - static void dotfunc(void * ip0_, npy_intp is0, void * ip1_, npy_intp is1, - void * op, npy_intp n, void * /*arr*/) + inline static void dotfunc(void * ip0_, npy_intp is0, void * ip1_, npy_intp is1, + void * op, npy_intp n, void * /*arr*/) { T res = T(0); char *ip0 = (char*)ip0_, *ip1 = (char*)ip1_; From 121f6e769920bde06a1af9d2256a6a850ee7578f Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Mon, 28 Dec 2020 17:40:25 +0100 Subject: [PATCH 3/9] test: print type code value --- unittest/user_type.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/unittest/user_type.cpp b/unittest/user_type.cpp index b128648a3..a6fdeee89 100644 --- a/unittest/user_type.cpp +++ b/unittest/user_type.cpp @@ -89,7 +89,8 @@ void expose_custom_type(const std::string & name) .def("__repr__",&Type::print) ; - eigenpy::registerNewType(); + int code = eigenpy::registerNewType(); + std::cout << "code: " << code << std::endl; eigenpy::registerCommonUfunc(); } From 8bbaa0522fc5a2e10276f69dd8126e208cdf1717 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Mon, 28 Dec 2020 18:01:53 +0100 Subject: [PATCH 4/9] core: allows template specialization of EigenToPy --- include/eigenpy/eigen-to-python.hpp | 6 +++--- include/eigenpy/fwd.hpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/eigenpy/eigen-to-python.hpp b/include/eigenpy/eigen-to-python.hpp index bd89fec52..8b1ac1d08 100644 --- a/include/eigenpy/eigen-to-python.hpp +++ b/include/eigenpy/eigen-to-python.hpp @@ -50,7 +50,7 @@ namespace eigenpy { namespace bp = boost::python; - template + template struct EigenToPy { static PyObject* convert(typename boost::add_reference::type>::type mat) @@ -82,8 +82,8 @@ namespace eigenpy } }; - template - struct EigenToPy< Eigen::Ref > + template + struct EigenToPy< Eigen::Ref,_Scalar > { static PyObject* convert(const Eigen::Ref & mat) { diff --git a/include/eigenpy/fwd.hpp b/include/eigenpy/fwd.hpp index 506a1d93a..3028c6ffd 100644 --- a/include/eigenpy/fwd.hpp +++ b/include/eigenpy/fwd.hpp @@ -28,8 +28,8 @@ namespace eigenpy { - template struct EigenToPy; template struct EigenFromPy; + template::type::Scalar> struct EigenToPy; } #endif // ifndef __eigenpy_fwd_hpp__ From ca5c9252255e58d0f6348f472ac0e4e5d7acd4a2 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Mon, 28 Dec 2020 18:02:08 +0100 Subject: [PATCH 5/9] core: allows template specialization of EigenFromPy --- include/eigenpy/eigen-from-python.hpp | 55 ++++++++++++++------------- include/eigenpy/fwd.hpp | 2 +- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/include/eigenpy/eigen-from-python.hpp b/include/eigenpy/eigen-from-python.hpp index 1a55bcbab..f86165987 100644 --- a/include/eigenpy/eigen-from-python.hpp +++ b/include/eigenpy/eigen-from-python.hpp @@ -275,13 +275,13 @@ namespace eigenpy memory->convertible = storage->storage.bytes; } - template + template struct EigenFromPy { typedef typename MatType::Scalar Scalar; /// \brief Determine if pyObj can be converted into a MatType object - static void* convertible(PyArrayObject* pyArray); + static void* convertible(PyObject* pyObj); /// \brief Allocate memory and copy pyObj in the new storage static void construct(PyObject* pyObj, @@ -290,12 +290,14 @@ namespace eigenpy static void registration(); }; - template - void* EigenFromPy::convertible(PyArrayObject* pyArray) + template + void* EigenFromPy::convertible(PyObject* pyObj) { - if(!call_PyArray_Check(reinterpret_cast(pyArray))) + if(!call_PyArray_Check(reinterpret_cast(pyObj))) return 0; + PyArrayObject * pyArray = reinterpret_cast(pyObj); + if(!np_type_is_convertible_into_scalar(EIGENPY_GET_PY_ARRAY_TYPE(pyArray))) return 0; @@ -403,15 +405,15 @@ namespace eigenpy return pyArray; } - template - void EigenFromPy::construct(PyObject* pyObj, - bp::converter::rvalue_from_python_stage1_data* memory) + template + void EigenFromPy::construct(PyObject* pyObj, + bp::converter::rvalue_from_python_stage1_data* memory) { eigen_from_py_construct(pyObj,memory); } - template - void EigenFromPy::registration() + template + void EigenFromPy::registration() { bp::converter::registry::push_back (reinterpret_cast(&EigenFromPy::convertible), @@ -431,7 +433,7 @@ namespace eigenpy // Add conversion to Eigen::EigenBase typedef Eigen::EigenBase EigenBase; - EigenFromPy::registration(); + EigenFromPy::registration(); // Add conversion to Eigen::PlainObjectBase typedef Eigen::PlainObjectBase PlainObjectBase; @@ -449,8 +451,8 @@ namespace eigenpy } }; - template - struct EigenFromPy< Eigen::MatrixBase > : EigenFromPy + template + struct EigenFromPy< Eigen::MatrixBase, _Scalar > : EigenFromPy { typedef EigenFromPy EigenFromPyDerived; typedef Eigen::MatrixBase Base; @@ -463,8 +465,8 @@ namespace eigenpy } }; - template - struct EigenFromPy< Eigen::EigenBase > : EigenFromPy + template + struct EigenFromPy< Eigen::EigenBase, _Scalar > : EigenFromPy { typedef EigenFromPy EigenFromPyDerived; typedef Eigen::EigenBase Base; @@ -477,8 +479,8 @@ namespace eigenpy } }; - template - struct EigenFromPy< Eigen::PlainObjectBase > : EigenFromPy + template + struct EigenFromPy< Eigen::PlainObjectBase, _Scalar > : EigenFromPy { typedef EigenFromPy EigenFromPyDerived; typedef Eigen::PlainObjectBase Base; @@ -493,20 +495,21 @@ namespace eigenpy #if EIGEN_VERSION_AT_LEAST(3,2,0) - template - struct EigenFromPy > + template + struct EigenFromPy,_Scalar> { typedef Eigen::Ref RefType; typedef typename MatType::Scalar Scalar; /// \brief Determine if pyObj can be converted into a MatType object - static void* convertible(PyArrayObject * pyArray) + static void* convertible(PyObject * pyObj) { - if(!call_PyArray_Check(reinterpret_cast(pyArray))) + if(!call_PyArray_Check(pyObj)) return 0; + PyArrayObject * pyArray = reinterpret_cast(pyObj); if(!PyArray_ISWRITEABLE(pyArray)) return 0; - return EigenFromPy::convertible(pyArray); + return EigenFromPy::convertible(pyObj); } static void registration() @@ -517,16 +520,16 @@ namespace eigenpy } }; - template - struct EigenFromPy > + template + struct EigenFromPy,_Scalar> { typedef const Eigen::Ref ConstRefType; typedef typename MatType::Scalar Scalar; /// \brief Determine if pyObj can be converted into a MatType object - static void* convertible(PyArrayObject * pyArray) + static void* convertible(PyObject * pyObj) { - return EigenFromPy::convertible(pyArray); + return EigenFromPy::convertible(pyObj); } static void registration() diff --git a/include/eigenpy/fwd.hpp b/include/eigenpy/fwd.hpp index 3028c6ffd..02ec33450 100644 --- a/include/eigenpy/fwd.hpp +++ b/include/eigenpy/fwd.hpp @@ -28,8 +28,8 @@ namespace eigenpy { - template struct EigenFromPy; template::type::Scalar> struct EigenToPy; + template::type::Scalar> struct EigenFromPy; } #endif // ifndef __eigenpy_fwd_hpp__ From 7df6480eb95b5a25c5ed4a962e3bbe8db170f102 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Tue, 29 Dec 2020 10:36:21 +0100 Subject: [PATCH 6/9] core: fix template specialization --- include/eigenpy/eigen-from-python.hpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/eigenpy/eigen-from-python.hpp b/include/eigenpy/eigen-from-python.hpp index f86165987..ee879f6f1 100644 --- a/include/eigenpy/eigen-from-python.hpp +++ b/include/eigenpy/eigen-from-python.hpp @@ -451,8 +451,8 @@ namespace eigenpy } }; - template - struct EigenFromPy< Eigen::MatrixBase, _Scalar > : EigenFromPy + template + struct EigenFromPy< Eigen::MatrixBase > : EigenFromPy { typedef EigenFromPy EigenFromPyDerived; typedef Eigen::MatrixBase Base; @@ -465,8 +465,8 @@ namespace eigenpy } }; - template - struct EigenFromPy< Eigen::EigenBase, _Scalar > : EigenFromPy + template + struct EigenFromPy< Eigen::EigenBase, typename MatType::Scalar > : EigenFromPy { typedef EigenFromPy EigenFromPyDerived; typedef Eigen::EigenBase Base; @@ -479,8 +479,8 @@ namespace eigenpy } }; - template - struct EigenFromPy< Eigen::PlainObjectBase, _Scalar > : EigenFromPy + template + struct EigenFromPy< Eigen::PlainObjectBase > : EigenFromPy { typedef EigenFromPy EigenFromPyDerived; typedef Eigen::PlainObjectBase Base; @@ -495,8 +495,8 @@ namespace eigenpy #if EIGEN_VERSION_AT_LEAST(3,2,0) - template - struct EigenFromPy,_Scalar> + template + struct EigenFromPy > { typedef Eigen::Ref RefType; typedef typename MatType::Scalar Scalar; @@ -520,8 +520,8 @@ namespace eigenpy } }; - template - struct EigenFromPy,_Scalar> + template + struct EigenFromPy > { typedef const Eigen::Ref ConstRefType; typedef typename MatType::Scalar Scalar; From 656219336748c0f6fd3ad0914f2c2a67b345892a Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Tue, 29 Dec 2020 10:36:37 +0100 Subject: [PATCH 7/9] core: add new signature to allocate Eigen matrices --- include/eigenpy/eigen-allocator.hpp | 49 +++++++++++++++++++---------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/include/eigenpy/eigen-allocator.hpp b/include/eigenpy/eigen-allocator.hpp index 343f76811..ca66b7be6 100644 --- a/include/eigenpy/eigen-allocator.hpp +++ b/include/eigenpy/eigen-allocator.hpp @@ -19,50 +19,67 @@ namespace eigenpy template struct init_matrix_or_array { + static MatType * run(int rows, int cols, void * storage) + { + if(storage) + return new (storage) MatType(rows,cols); + else + return new MatType(rows,cols); + } + static MatType * run(PyArrayObject * pyArray, void * storage = NULL) { assert(PyArray_NDIM(pyArray) == 1 || PyArray_NDIM(pyArray) == 2); int rows = -1, cols = -1; - if(PyArray_NDIM(pyArray) == 2) + const int ndim = PyArray_NDIM(pyArray); + if(ndim == 2) { rows = (int)PyArray_DIMS(pyArray)[0]; cols = (int)PyArray_DIMS(pyArray)[1]; } - else if(PyArray_NDIM(pyArray) == 1) + else if(ndim == 1) { rows = (int)PyArray_DIMS(pyArray)[0]; cols = 1; } - if(storage) - return new (storage) MatType(rows,cols); - else - return new MatType(rows,cols); + return run(rows,cols,storage); } }; template struct init_matrix_or_array { + static MatType * run(int rows, int cols, void * storage) + { + if(storage) + return new (storage) MatType(rows,cols); + else + return new MatType(rows,cols); + } + + static MatType * run(int size, void * storage) + { + if(storage) + return new (storage) MatType(size); + else + return new MatType(size); + } + static MatType * run(PyArrayObject * pyArray, void * storage = NULL) { - if(PyArray_NDIM(pyArray) == 1) + const int ndim = PyArray_NDIM(pyArray); + if(ndim == 1) { - const int rows_or_cols = (int)PyArray_DIMS(pyArray)[0]; - if(storage) - return new (storage) MatType(rows_or_cols); - else - return new MatType(rows_or_cols); + const int size = (int)PyArray_DIMS(pyArray)[0]; + return run(size,storage); } else { const int rows = (int)PyArray_DIMS(pyArray)[0]; const int cols = (int)PyArray_DIMS(pyArray)[1]; - if(storage) - return new (storage) MatType(rows,cols); - else - return new MatType(rows,cols); + return run(rows,cols,storage); } } }; From 8ff3cfdeb6973a339ba7cedde5993ef70f094d7c Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Tue, 29 Dec 2020 10:38:47 +0100 Subject: [PATCH 8/9] cmake: sync submodule --- cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake b/cmake index 000a90190..5cb34667b 160000 --- a/cmake +++ b/cmake @@ -1 +1 @@ -Subproject commit 000a90190d1dca8b028f437095f2fddff0f9839b +Subproject commit 5cb34667bde62b5676e2b869dc6d13d598a4e2e3 From c3e869da041917d88831d832d72c93563e2f4faf Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Tue, 29 Dec 2020 10:39:05 +0100 Subject: [PATCH 9/9] ros: update version --- package.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.xml b/package.xml index 75bd76e0d..337205aaa 100644 --- a/package.xml +++ b/package.xml @@ -1,7 +1,7 @@ eigenpy - 2.5.0 + 2.6.0 Bindings between Numpy and Eigen using Boost.Python Justin Carpentier Wolfgang Merkt