From 81e364c990ce9bbd55055eb399d2acf2580952b2 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Wed, 19 Feb 2020 23:29:48 +0100 Subject: [PATCH 1/4] core: template specialization for sized Eigen::{MatrixBase,EigenBase} --- include/eigenpy/details.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/eigenpy/details.hpp b/include/eigenpy/details.hpp index 2fa0de5e0..9a5bddf72 100644 --- a/include/eigenpy/details.hpp +++ b/include/eigenpy/details.hpp @@ -20,6 +20,24 @@ #define GET_PY_ARRAY_TYPE(array) PyArray_ObjectType(reinterpret_cast(array), 0) +namespace boost { namespace python { namespace detail { + + template + struct referent_size&> + { + BOOST_STATIC_CONSTANT( + std::size_t, value = sizeof(MatType)); + }; + + template + struct referent_size&> + { + BOOST_STATIC_CONSTANT( + std::size_t, value = sizeof(MatType)); + }; + +}}} + namespace eigenpy { template struct NumpyEquivalentType {}; From 0d32f04128595a6394433fd6a367ef9ddde06467 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Thu, 20 Feb 2020 00:03:20 +0100 Subject: [PATCH 2/4] core: add more size for default Matrix base types --- include/eigenpy/details.hpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/eigenpy/details.hpp b/include/eigenpy/details.hpp index 9a5bddf72..9a43df4b7 100644 --- a/include/eigenpy/details.hpp +++ b/include/eigenpy/details.hpp @@ -29,6 +29,13 @@ namespace boost { namespace python { namespace detail { std::size_t, value = sizeof(MatType)); }; + template + struct referent_size > + { + BOOST_STATIC_CONSTANT( + std::size_t, value = sizeof(MatType)); + }; + template struct referent_size&> { @@ -36,6 +43,13 @@ namespace boost { namespace python { namespace detail { std::size_t, value = sizeof(MatType)); }; + template + struct referent_size > + { + BOOST_STATIC_CONSTANT( + std::size_t, value = sizeof(MatType)); + }; + }}} namespace eigenpy From b620c5b3e5e99c46bb127dc35e8710e35d7bd7f9 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Thu, 20 Feb 2020 12:01:18 +0100 Subject: [PATCH 3/4] core: fix potential bug in rvalue_from_python_data --- .../details/rvalue_from_python_data.hpp | 55 ++++++++++++++++--- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/include/eigenpy/details/rvalue_from_python_data.hpp b/include/eigenpy/details/rvalue_from_python_data.hpp index 3cd94e979..254961b9f 100644 --- a/include/eigenpy/details/rvalue_from_python_data.hpp +++ b/include/eigenpy/details/rvalue_from_python_data.hpp @@ -1,3 +1,7 @@ +/* + * Copyright 2018-2020, INRIA + */ + #ifndef __eigenpy_details_rvalue_from_python_data_hpp__ #define __eigenpy_details_rvalue_from_python_data_hpp__ @@ -10,14 +14,51 @@ namespace boost { namespace converter { - + /// \brief Template specialization of rvalue_from_python_data template struct rvalue_from_python_data const & > - : rvalue_from_python_storage const & > + : rvalue_from_python_storage + { + typedef Eigen::MatrixBase const & T; + +# if (!defined(__MWERKS__) || __MWERKS__ >= 0x3000) \ +&& (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 245) \ +&& (!defined(__DECCXX_VER) || __DECCXX_VER > 60590014) \ +&& !defined(BOOST_PYTHON_SYNOPSIS) /* Synopsis' OpenCXX has trouble parsing this */ + // This must always be a POD struct with m_data its first member. + BOOST_STATIC_ASSERT(BOOST_PYTHON_OFFSETOF(rvalue_from_python_storage,stage1) == 0); +# endif + + // The usual constructor + rvalue_from_python_data(rvalue_from_python_stage1_data const & _stage1) + { + this->stage1 = _stage1; + } + + // This constructor just sets m_convertible -- used by + // implicitly_convertible<> to perform the final step of the + // conversion, where the construct() function is already known. + rvalue_from_python_data(void* convertible) + { + this->stage1.convertible = convertible; + } + + // Destroys any object constructed in the storage. + ~rvalue_from_python_data() + { + if (this->stage1.convertible == this->storage.bytes) + static_cast((void *)this->storage.bytes)->~Derived(); + } + }; + + /// \brief Template specialization of rvalue_from_python_data + template + struct rvalue_from_python_data const & > + : rvalue_from_python_storage { typedef Eigen::MatrixBase const & T; - + # if (!defined(__MWERKS__) || __MWERKS__ >= 0x3000) \ && (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 245) \ && (!defined(__DECCXX_VER) || __DECCXX_VER > 60590014) \ @@ -25,13 +66,13 @@ namespace boost // This must always be a POD struct with m_data its first member. BOOST_STATIC_ASSERT(BOOST_PYTHON_OFFSETOF(rvalue_from_python_storage,stage1) == 0); # endif - + // The usual constructor rvalue_from_python_data(rvalue_from_python_stage1_data const & _stage1) { this->stage1 = _stage1; } - + // This constructor just sets m_convertible -- used by // implicitly_convertible<> to perform the final step of the // conversion, where the construct() function is already known. @@ -39,7 +80,7 @@ namespace boost { this->stage1.convertible = convertible; } - + // Destroys any object constructed in the storage. ~rvalue_from_python_data() { @@ -47,7 +88,7 @@ namespace boost static_cast((void *)this->storage.bytes)->~Derived(); } }; - + } } } // namespace boost::python::converter From a29f6f85b695d98b00f50dc82940562b9f938050 Mon Sep 17 00:00:00 2001 From: Justin Carpentier Date: Thu, 20 Feb 2020 12:01:57 +0100 Subject: [PATCH 4/4] python: use original signature of is_approx --- include/eigenpy/utils/is-approx.hpp | 13 +++++++------ python/main.cpp | 6 ++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/include/eigenpy/utils/is-approx.hpp b/include/eigenpy/utils/is-approx.hpp index 7f82ffde1..59d3064e9 100644 --- a/include/eigenpy/utils/is-approx.hpp +++ b/include/eigenpy/utils/is-approx.hpp @@ -1,6 +1,6 @@ /* -* Copyright 2020 INRIA -*/ + * Copyright 2020 INRIA + */ #ifndef __eigenpy_utils_scalar_is_approx_hpp__ #define __eigenpy_utils_scalar_is_approx_hpp__ @@ -10,15 +10,16 @@ namespace eigenpy { template - inline EIGEN_DONT_INLINE bool is_approx(const MatrixType1 & mat1, - const MatrixType2 & mat2, + inline EIGEN_DONT_INLINE bool is_approx(const Eigen::MatrixBase & mat1, + const Eigen::MatrixBase & mat2, const typename MatrixType1::Scalar & prec) { - return mat1.derived().isApprox(mat2.derived(),prec); + return mat1.isApprox(mat2,prec); } template - inline bool is_approx(const MatrixType1 & mat1, const MatrixType2 & mat2) + inline EIGEN_DONT_INLINE bool is_approx(const Eigen::MatrixBase & mat1, + const Eigen::MatrixBase & mat2) { return is_approx(mat1,mat2,Eigen::NumTraits::dummy_precision()); } diff --git a/python/main.cpp b/python/main.cpp index 11b35d5d8..075b819b9 100644 --- a/python/main.cpp +++ b/python/main.cpp @@ -46,10 +46,12 @@ BOOST_PYTHON_MODULE(eigenpy) { using namespace Eigen; - bp::def("is_approx",(bool (*)(const MatrixXd &, const MatrixXd &, const double &))&is_approx, + + bp::def("is_approx",(bool (*)(const Eigen::MatrixBase &, const Eigen::MatrixBase &, const double &))&is_approx, bp::args("A","B","prec"), "Returns True if A is approximately equal to B, within the precision determined by prec."); - bp::def("is_approx",(bool (*)(const MatrixXd &, const MatrixXd &))&is_approx, + + bp::def("is_approx",(bool (*)(const Eigen::MatrixBase &, const Eigen::MatrixBase &))&is_approx, bp::args("A","B"), "Returns True if A is approximately equal to B."); }