Skip to content

Commit 0fb2a72

Browse files
authored
Merge pull request #158 from jcarpent/devel
Template specialization for sized Eigen::{MatrixBase,EigenBase}
2 parents 2451ff4 + a29f6f8 commit 0fb2a72

File tree

4 files changed

+91
-15
lines changed

4 files changed

+91
-15
lines changed

include/eigenpy/details.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,38 @@
2020

2121
#define GET_PY_ARRAY_TYPE(array) PyArray_ObjectType(reinterpret_cast<PyObject *>(array), 0)
2222

23+
namespace boost { namespace python { namespace detail {
24+
25+
template<class MatType>
26+
struct referent_size<Eigen::MatrixBase<MatType>&>
27+
{
28+
BOOST_STATIC_CONSTANT(
29+
std::size_t, value = sizeof(MatType));
30+
};
31+
32+
template<class MatType>
33+
struct referent_size<Eigen::MatrixBase<MatType> >
34+
{
35+
BOOST_STATIC_CONSTANT(
36+
std::size_t, value = sizeof(MatType));
37+
};
38+
39+
template<class MatType>
40+
struct referent_size<Eigen::EigenBase<MatType>&>
41+
{
42+
BOOST_STATIC_CONSTANT(
43+
std::size_t, value = sizeof(MatType));
44+
};
45+
46+
template<class MatType>
47+
struct referent_size<Eigen::EigenBase<MatType> >
48+
{
49+
BOOST_STATIC_CONSTANT(
50+
std::size_t, value = sizeof(MatType));
51+
};
52+
53+
}}}
54+
2355
namespace eigenpy
2456
{
2557
template <typename SCALAR> struct NumpyEquivalentType {};

include/eigenpy/details/rvalue_from_python_data.hpp

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
* Copyright 2018-2020, INRIA
3+
*/
4+
15
#ifndef __eigenpy_details_rvalue_from_python_data_hpp__
26
#define __eigenpy_details_rvalue_from_python_data_hpp__
37

@@ -10,44 +14,81 @@ namespace boost
1014
{
1115
namespace converter
1216
{
13-
17+
1418
/// \brief Template specialization of rvalue_from_python_data
1519
template<typename Derived>
1620
struct rvalue_from_python_data<Eigen::MatrixBase<Derived> const & >
17-
: rvalue_from_python_storage<Eigen::MatrixBase<Derived> const & >
21+
: rvalue_from_python_storage<Derived const & >
22+
{
23+
typedef Eigen::MatrixBase<Derived> const & T;
24+
25+
# if (!defined(__MWERKS__) || __MWERKS__ >= 0x3000) \
26+
&& (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 245) \
27+
&& (!defined(__DECCXX_VER) || __DECCXX_VER > 60590014) \
28+
&& !defined(BOOST_PYTHON_SYNOPSIS) /* Synopsis' OpenCXX has trouble parsing this */
29+
// This must always be a POD struct with m_data its first member.
30+
BOOST_STATIC_ASSERT(BOOST_PYTHON_OFFSETOF(rvalue_from_python_storage<T>,stage1) == 0);
31+
# endif
32+
33+
// The usual constructor
34+
rvalue_from_python_data(rvalue_from_python_stage1_data const & _stage1)
35+
{
36+
this->stage1 = _stage1;
37+
}
38+
39+
// This constructor just sets m_convertible -- used by
40+
// implicitly_convertible<> to perform the final step of the
41+
// conversion, where the construct() function is already known.
42+
rvalue_from_python_data(void* convertible)
43+
{
44+
this->stage1.convertible = convertible;
45+
}
46+
47+
// Destroys any object constructed in the storage.
48+
~rvalue_from_python_data()
49+
{
50+
if (this->stage1.convertible == this->storage.bytes)
51+
static_cast<Derived *>((void *)this->storage.bytes)->~Derived();
52+
}
53+
};
54+
55+
/// \brief Template specialization of rvalue_from_python_data
56+
template<typename Derived>
57+
struct rvalue_from_python_data<Eigen::EigenBase<Derived> const & >
58+
: rvalue_from_python_storage<Derived const & >
1859
{
1960
typedef Eigen::MatrixBase<Derived> const & T;
20-
61+
2162
# if (!defined(__MWERKS__) || __MWERKS__ >= 0x3000) \
2263
&& (!defined(__EDG_VERSION__) || __EDG_VERSION__ >= 245) \
2364
&& (!defined(__DECCXX_VER) || __DECCXX_VER > 60590014) \
2465
&& !defined(BOOST_PYTHON_SYNOPSIS) /* Synopsis' OpenCXX has trouble parsing this */
2566
// This must always be a POD struct with m_data its first member.
2667
BOOST_STATIC_ASSERT(BOOST_PYTHON_OFFSETOF(rvalue_from_python_storage<T>,stage1) == 0);
2768
# endif
28-
69+
2970
// The usual constructor
3071
rvalue_from_python_data(rvalue_from_python_stage1_data const & _stage1)
3172
{
3273
this->stage1 = _stage1;
3374
}
34-
75+
3576
// This constructor just sets m_convertible -- used by
3677
// implicitly_convertible<> to perform the final step of the
3778
// conversion, where the construct() function is already known.
3879
rvalue_from_python_data(void* convertible)
3980
{
4081
this->stage1.convertible = convertible;
4182
}
42-
83+
4384
// Destroys any object constructed in the storage.
4485
~rvalue_from_python_data()
4586
{
4687
if (this->stage1.convertible == this->storage.bytes)
4788
static_cast<Derived *>((void *)this->storage.bytes)->~Derived();
4889
}
4990
};
50-
91+
5192
}
5293
}
5394
} // namespace boost::python::converter

include/eigenpy/utils/is-approx.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright 2020 INRIA
3-
*/
2+
* Copyright 2020 INRIA
3+
*/
44

55
#ifndef __eigenpy_utils_scalar_is_approx_hpp__
66
#define __eigenpy_utils_scalar_is_approx_hpp__
@@ -10,15 +10,16 @@
1010
namespace eigenpy
1111
{
1212
template<typename MatrixType1, typename MatrixType2>
13-
inline EIGEN_DONT_INLINE bool is_approx(const MatrixType1 & mat1,
14-
const MatrixType2 & mat2,
13+
inline EIGEN_DONT_INLINE bool is_approx(const Eigen::MatrixBase<MatrixType1> & mat1,
14+
const Eigen::MatrixBase<MatrixType2> & mat2,
1515
const typename MatrixType1::Scalar & prec)
1616
{
17-
return mat1.derived().isApprox(mat2.derived(),prec);
17+
return mat1.isApprox(mat2,prec);
1818
}
1919

2020
template<typename MatrixType1, typename MatrixType2>
21-
inline bool is_approx(const MatrixType1 & mat1, const MatrixType2 & mat2)
21+
inline EIGEN_DONT_INLINE bool is_approx(const Eigen::MatrixBase<MatrixType1> & mat1,
22+
const Eigen::MatrixBase<MatrixType2> & mat2)
2223
{
2324
return is_approx(mat1,mat2,Eigen::NumTraits<typename MatrixType1::Scalar>::dummy_precision());
2425
}

python/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ BOOST_PYTHON_MODULE(eigenpy)
4646

4747
{
4848
using namespace Eigen;
49-
bp::def("is_approx",(bool (*)(const MatrixXd &, const MatrixXd &, const double &))&is_approx<MatrixXd,MatrixXd>,
49+
50+
bp::def("is_approx",(bool (*)(const Eigen::MatrixBase<MatrixXd> &, const Eigen::MatrixBase<MatrixXd> &, const double &))&is_approx<MatrixXd,MatrixXd>,
5051
bp::args("A","B","prec"),
5152
"Returns True if A is approximately equal to B, within the precision determined by prec.");
52-
bp::def("is_approx",(bool (*)(const MatrixXd &, const MatrixXd &))&is_approx<MatrixXd,MatrixXd>,
53+
54+
bp::def("is_approx",(bool (*)(const Eigen::MatrixBase<MatrixXd> &, const Eigen::MatrixBase<MatrixXd> &))&is_approx<MatrixXd,MatrixXd>,
5355
bp::args("A","B"),
5456
"Returns True if A is approximately equal to B.");
5557
}

0 commit comments

Comments
 (0)