Skip to content

Commit 02d4380

Browse files
authored
Merge pull request #138 from jcarpent/devel
core: fix issue when converting form Matrix to Eigen::MatrixBase<Matrix>
2 parents ac25cd4 + fa17abc commit 02d4380

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

include/eigenpy/details.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,37 @@ struct implicit<Eigen::MatrixBase<MatType>,MatType>
4949
}
5050
};
5151

52+
template<class MatType>
53+
struct implicit<MatType,Eigen::MatrixBase<MatType> >
54+
{
55+
typedef MatType Source;
56+
typedef Eigen::MatrixBase<MatType> Target;
57+
58+
static void* convertible(PyObject* obj)
59+
{
60+
// Find a converter which can produce a Source instance from
61+
// obj. The user has told us that Source can be converted to
62+
// Target, and instantiating construct() below, ensures that
63+
// at compile-time.
64+
return implicit_rvalue_convertible_from_python(obj, registered<Source>::converters)
65+
? obj : 0;
66+
}
67+
68+
static void construct(PyObject* obj, rvalue_from_python_stage1_data* data)
69+
{
70+
void* storage = ((rvalue_from_python_storage<Source>*)data)->storage.bytes;
71+
72+
arg_from_python<Source> get_source(obj);
73+
bool convertible = get_source.convertible();
74+
BOOST_VERIFY(convertible);
75+
76+
new (storage) Source(get_source());
77+
78+
// record successful construction
79+
data->convertible = storage;
80+
}
81+
};
82+
5283
}}} // namespace boost::python::converter
5384

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

unittest/python/test_matrix.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
if verbose: print("===> From Py to Eigen::MatrixXd")
3333
Mref = np.reshape(np.matrix(range(64),np.double),[8,8])
3434

35+
# Test base function
36+
Mref_from_base = eigenpy.base(Mref)
37+
assert( np.array_equal(Mref,Mref_from_base) );
38+
3539
if verbose: print("===> Matrix 8x8")
3640
M = Mref
3741
assert( np.array_equal(M,eigenpy.reflex(M,verbose)) );

0 commit comments

Comments
 (0)