Skip to content

Commit c4e23b7

Browse files
committed
core: add initEigenObject runner
1 parent a467802 commit c4e23b7

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

include/eigenpy/details.hpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,39 @@ namespace eigenpy
172172
bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType;
173173

174174
};
175+
176+
template<typename MatType, bool IsVectorAtCompileTime = MatType::IsVectorAtCompileTime>
177+
struct initEigenObject
178+
{
179+
static MatType * run(PyArrayObject * pyArray, void * storage)
180+
{
181+
assert(PyArray_NDIM(pyArray) == 2);
182+
183+
const int rows = (int)PyArray_DIMS(pyArray)[0];
184+
const int cols = (int)PyArray_DIMS(pyArray)[1];
185+
186+
return new (storage) MatType(rows,cols);
187+
}
188+
};
189+
190+
template<typename MatType>
191+
struct initEigenObject<MatType,true>
192+
{
193+
static MatType * run(PyArrayObject * pyArray, void * storage)
194+
{
195+
if(PyArray_NDIM(pyArray) == 1)
196+
{
197+
const int rows_or_cols = (int)PyArray_DIMS(pyArray)[0];
198+
return new (storage) MatType(rows_or_cols);
199+
}
200+
else
201+
{
202+
const int rows = (int)PyArray_DIMS(pyArray)[0];
203+
const int cols = (int)PyArray_DIMS(pyArray)[1];
204+
return new (storage) MatType(rows,cols);
205+
}
206+
}
207+
};
175208

176209
template<typename MatType>
177210
struct EigenObjectAllocator
@@ -181,10 +214,7 @@ namespace eigenpy
181214

182215
static void allocate(PyArrayObject * pyArray, void * storage)
183216
{
184-
const int rows = (int)PyArray_DIMS(pyArray)[0];
185-
const int cols = (int)PyArray_DIMS(pyArray)[1];
186-
187-
Type * mat_ptr = new (storage) Type(rows,cols);
217+
Type * mat_ptr = initEigenObject<Type>::run(pyArray,storage);
188218

189219
if(NumpyEquivalentType<Scalar>::type_code == GET_PY_ARRAY_TYPE(pyArray))
190220
{

0 commit comments

Comments
 (0)