Skip to content

Commit aacf998

Browse files
authored
Merge pull request #64 from jcarpent/devel
core: fix dimensions of vecrtor for Array conversion
2 parents 25addde + 8cf80ab commit aacf998

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

include/eigenpy/details.hpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ namespace eigenpy
4444

4545
namespace bp = boost::python;
4646

47+
enum NP_TYPE
48+
{
49+
MATRIX_TYPE,
50+
ARRAY_TYPE
51+
};
52+
4753
struct NumpyType
4854
{
4955

@@ -75,19 +81,26 @@ namespace eigenpy
7581
{
7682
PyTypeObject * obj_type = PyType_Check(obj.ptr()) ? reinterpret_cast<PyTypeObject*>(obj.ptr()) : obj.ptr()->ob_type;
7783
if(PyType_IsSubtype(obj_type,getInstance().NumpyMatrixType))
78-
getInstance().CurrentNumpyType = getInstance().NumpyMatrixObject;
84+
switchToNumpyMatrix();
7985
else if(PyType_IsSubtype(obj_type,getInstance().NumpyArrayType))
80-
getInstance().CurrentNumpyType = getInstance().NumpyArrayObject;
86+
switchToNumpyArray();
8187
}
8288

8389
static void switchToNumpyArray()
8490
{
8591
getInstance().CurrentNumpyType = getInstance().NumpyArrayObject;
92+
getInstance().np_type = ARRAY_TYPE;
8693
}
8794

8895
static void switchToNumpyMatrix()
8996
{
9097
getInstance().CurrentNumpyType = getInstance().NumpyMatrixObject;
98+
getInstance().np_type = MATRIX_TYPE;
99+
}
100+
101+
static NP_TYPE getType()
102+
{
103+
return getInstance().np_type;
91104
}
92105

93106
protected:
@@ -117,8 +130,12 @@ namespace eigenpy
117130
bp::object NumpyMatrixObject; PyTypeObject * NumpyMatrixType;
118131
//bp::object NumpyAsMatrixObject; PyTypeObject * NumpyAsMatrixType;
119132
bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType;
133+
134+
static NP_TYPE np_type;
120135
};
121136

137+
NP_TYPE NumpyType::np_type = MATRIX_TYPE;
138+
122139
template<typename MatType>
123140
struct EigenObjectAllocator
124141
{
@@ -189,7 +206,6 @@ namespace eigenpy
189206
}
190207
};
191208
#endif
192-
193209
/* --- TO PYTHON -------------------------------------------------------------- */
194210
template<typename MatType>
195211
struct EigenToPy
@@ -201,12 +217,21 @@ namespace eigenpy
201217
&& "Matrix range larger than int ... should never happen." );
202218
const int R = (int)mat.rows(), C = (int)mat.cols();
203219

204-
npy_intp shape[2] = { R,C };
205-
PyArrayObject* pyArray = (PyArrayObject*)
206-
PyArray_SimpleNew(2, shape, NumpyEquivalentType<T>::type_code);
220+
PyArrayObject* pyArray;
221+
if(C == 1 && NumpyType::getType() == ARRAY_TYPE)
222+
{
223+
npy_intp shape[1] = { R };
224+
pyArray = (PyArrayObject*) PyArray_SimpleNew(1, shape,
225+
NumpyEquivalentType<T>::type_code);
226+
}
227+
else
228+
{
229+
npy_intp shape[2] = { R,C };
230+
pyArray = (PyArrayObject*) PyArray_SimpleNew(2, shape,
231+
NumpyEquivalentType<T>::type_code);
232+
}
207233

208234
EigenObjectAllocator<MatType>::convert(mat,pyArray);
209-
210235
return NumpyType::getInstance().make(pyArray).ptr();
211236
}
212237
};

0 commit comments

Comments
 (0)