Skip to content
Snippets Groups Projects
Unverified Commit aacf9982 authored by Justin Carpentier's avatar Justin Carpentier Committed by GitHub
Browse files

Merge pull request #64 from jcarpent/devel

core: fix dimensions of vecrtor for Array conversion
parents 25addde4 8cf80abd
No related branches found
No related tags found
No related merge requests found
...@@ -44,6 +44,12 @@ namespace eigenpy ...@@ -44,6 +44,12 @@ namespace eigenpy
namespace bp = boost::python; namespace bp = boost::python;
enum NP_TYPE
{
MATRIX_TYPE,
ARRAY_TYPE
};
struct NumpyType struct NumpyType
{ {
...@@ -75,19 +81,26 @@ namespace eigenpy ...@@ -75,19 +81,26 @@ namespace eigenpy
{ {
PyTypeObject * obj_type = PyType_Check(obj.ptr()) ? reinterpret_cast<PyTypeObject*>(obj.ptr()) : obj.ptr()->ob_type; PyTypeObject * obj_type = PyType_Check(obj.ptr()) ? reinterpret_cast<PyTypeObject*>(obj.ptr()) : obj.ptr()->ob_type;
if(PyType_IsSubtype(obj_type,getInstance().NumpyMatrixType)) if(PyType_IsSubtype(obj_type,getInstance().NumpyMatrixType))
getInstance().CurrentNumpyType = getInstance().NumpyMatrixObject; switchToNumpyMatrix();
else if(PyType_IsSubtype(obj_type,getInstance().NumpyArrayType)) else if(PyType_IsSubtype(obj_type,getInstance().NumpyArrayType))
getInstance().CurrentNumpyType = getInstance().NumpyArrayObject; switchToNumpyArray();
} }
static void switchToNumpyArray() static void switchToNumpyArray()
{ {
getInstance().CurrentNumpyType = getInstance().NumpyArrayObject; getInstance().CurrentNumpyType = getInstance().NumpyArrayObject;
getInstance().np_type = ARRAY_TYPE;
} }
static void switchToNumpyMatrix() static void switchToNumpyMatrix()
{ {
getInstance().CurrentNumpyType = getInstance().NumpyMatrixObject; getInstance().CurrentNumpyType = getInstance().NumpyMatrixObject;
getInstance().np_type = MATRIX_TYPE;
}
static NP_TYPE getType()
{
return getInstance().np_type;
} }
protected: protected:
...@@ -117,8 +130,12 @@ namespace eigenpy ...@@ -117,8 +130,12 @@ namespace eigenpy
bp::object NumpyMatrixObject; PyTypeObject * NumpyMatrixType; bp::object NumpyMatrixObject; PyTypeObject * NumpyMatrixType;
//bp::object NumpyAsMatrixObject; PyTypeObject * NumpyAsMatrixType; //bp::object NumpyAsMatrixObject; PyTypeObject * NumpyAsMatrixType;
bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType; bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType;
static NP_TYPE np_type;
}; };
NP_TYPE NumpyType::np_type = MATRIX_TYPE;
template<typename MatType> template<typename MatType>
struct EigenObjectAllocator struct EigenObjectAllocator
{ {
...@@ -189,7 +206,6 @@ namespace eigenpy ...@@ -189,7 +206,6 @@ namespace eigenpy
} }
}; };
#endif #endif
/* --- TO PYTHON -------------------------------------------------------------- */ /* --- TO PYTHON -------------------------------------------------------------- */
template<typename MatType> template<typename MatType>
struct EigenToPy struct EigenToPy
...@@ -201,12 +217,21 @@ namespace eigenpy ...@@ -201,12 +217,21 @@ namespace eigenpy
&& "Matrix range larger than int ... should never happen." ); && "Matrix range larger than int ... should never happen." );
const int R = (int)mat.rows(), C = (int)mat.cols(); const int R = (int)mat.rows(), C = (int)mat.cols();
npy_intp shape[2] = { R,C }; PyArrayObject* pyArray;
PyArrayObject* pyArray = (PyArrayObject*) if(C == 1 && NumpyType::getType() == ARRAY_TYPE)
PyArray_SimpleNew(2, shape, NumpyEquivalentType<T>::type_code); {
npy_intp shape[1] = { R };
pyArray = (PyArrayObject*) PyArray_SimpleNew(1, shape,
NumpyEquivalentType<T>::type_code);
}
else
{
npy_intp shape[2] = { R,C };
pyArray = (PyArrayObject*) PyArray_SimpleNew(2, shape,
NumpyEquivalentType<T>::type_code);
}
EigenObjectAllocator<MatType>::convert(mat,pyArray); EigenObjectAllocator<MatType>::convert(mat,pyArray);
return NumpyType::getInstance().make(pyArray).ptr(); return NumpyType::getInstance().make(pyArray).ptr();
} }
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment