Skip to content

Commit 8d43698

Browse files
committed
core: improve conversion of Matrix to Vector
1 parent 6d2872b commit 8d43698

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

include/eigenpy/details.hpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,14 @@ namespace eigenpy
111111
struct CastMatToMat
112112
{
113113
template<typename MatrixIn, typename MatrixOut>
114-
static void run(const Eigen::MatrixBase<MatrixIn> & input, const Eigen::MatrixBase<MatrixOut> & dest)
114+
static void run(const Eigen::MatrixBase<MatrixIn> & input,
115+
const Eigen::MatrixBase<MatrixOut> & dest)
115116
{
116117
MatrixOut & dest_ = const_cast<MatrixOut &>(dest.derived());
117-
dest_ = input.template cast<NewScalar>();
118+
if(dest.rows() == input.rows())
119+
dest_ = input.template cast<NewScalar>();
120+
else
121+
dest_ = input.transpose().template cast<NewScalar>();
118122
}
119123
};
120124

@@ -126,6 +130,7 @@ namespace eigenpy
126130
const Eigen::MatrixBase<MatrixOut> & /*dest*/)
127131
{
128132
// do nothing
133+
assert("Must never happened");
129134
}
130135
};
131136

@@ -192,9 +197,15 @@ namespace eigenpy
192197
const MatrixDerived & mat = const_cast<const MatrixDerived &>(mat_.derived());
193198
const int pyArray_Type = GET_PY_ARRAY_TYPE(pyArray);
194199

195-
if(pyArray_Type == NumpyEquivalentType<Scalar>::type_code)
200+
typedef typename MapNumpy<MatType,Scalar>::EigenMap MapType;
201+
202+
if(pyArray_Type == NumpyEquivalentType<Scalar>::type_code) // no cast needed
196203
{
197-
MapNumpy<MatType,Scalar>::map(pyArray) = mat; // no cast needed
204+
MapType map_pyArray = MapNumpy<MatType,Scalar>::map(pyArray);
205+
if(mat.rows() == map_pyArray.rows())
206+
map_pyArray = mat;
207+
else
208+
map_pyArray = mat.transpose();
198209
return;
199210
}
200211

@@ -260,13 +271,14 @@ namespace eigenpy
260271
typedef typename MatType::Scalar Scalar;
261272
assert( (mat.rows()<INT_MAX) && (mat.cols()<INT_MAX)
262273
&& "Matrix range larger than int ... should never happen." );
263-
const int R = (int)mat.rows(), C = (int)mat.cols();
274+
const npy_intp R = (npy_intp)mat.rows(), C = (npy_intp)mat.cols();
264275

265276
PyArrayObject* pyArray;
266277
// Allocate Python memory
267-
if(C == 1 && NumpyType::getType() == ARRAY_TYPE) // Handle array with a single dimension
278+
if( ( ((C == 1 xor R == 1) && !MatType::IsVectorAtCompileTime) || MatType::IsVectorAtCompileTime)
279+
&& NumpyType::getType() == ARRAY_TYPE) // Handle array with a single dimension
268280
{
269-
npy_intp shape[1] = { R };
281+
npy_intp shape[1] = { C == 1 ? R : C };
270282
pyArray = (PyArrayObject*) PyArray_SimpleNew(1, shape,
271283
NumpyEquivalentType<Scalar>::type_code);
272284
}

0 commit comments

Comments
 (0)