diff --git a/include/eigenpy/details.hpp b/include/eigenpy/details.hpp index 1cbe269f45c8deca4f0618abcb8fa5a0c4a04d91..222b361f5eb60752b855c33700787c7978c3c679 100644 --- a/include/eigenpy/details.hpp +++ b/include/eigenpy/details.hpp @@ -172,6 +172,39 @@ namespace eigenpy bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType; }; + + template<typename MatType, bool IsVectorAtCompileTime = MatType::IsVectorAtCompileTime> + struct initEigenObject + { + static MatType * run(PyArrayObject * pyArray, void * storage) + { + assert(PyArray_NDIM(pyArray) == 2); + + const int rows = (int)PyArray_DIMS(pyArray)[0]; + const int cols = (int)PyArray_DIMS(pyArray)[1]; + + return new (storage) MatType(rows,cols); + } + }; + + template<typename MatType> + struct initEigenObject<MatType,true> + { + static MatType * run(PyArrayObject * pyArray, void * storage) + { + if(PyArray_NDIM(pyArray) == 1) + { + const int rows_or_cols = (int)PyArray_DIMS(pyArray)[0]; + return new (storage) MatType(rows_or_cols); + } + else + { + const int rows = (int)PyArray_DIMS(pyArray)[0]; + const int cols = (int)PyArray_DIMS(pyArray)[1]; + return new (storage) MatType(rows,cols); + } + } + }; template<typename MatType> struct EigenObjectAllocator @@ -181,10 +214,7 @@ namespace eigenpy static void allocate(PyArrayObject * pyArray, void * storage) { - const int rows = (int)PyArray_DIMS(pyArray)[0]; - const int cols = (int)PyArray_DIMS(pyArray)[1]; - - Type * mat_ptr = new (storage) Type(rows,cols); + Type * mat_ptr = initEigenObject<Type>::run(pyArray,storage); if(NumpyEquivalentType<Scalar>::type_code == GET_PY_ARRAY_TYPE(pyArray)) {