Skip to content
Snippets Groups Projects
Verified Commit a805df9a authored by Justin Carpentier's avatar Justin Carpentier
Browse files

core: rename helper

parent c636bfcd
No related branches found
No related tags found
No related merge requests found
...@@ -11,80 +11,85 @@ ...@@ -11,80 +11,85 @@
namespace eigenpy namespace eigenpy
{ {
template<typename MatType, bool IsVectorAtCompileTime = MatType::IsVectorAtCompileTime>
struct initEigenObject namespace details
{ {
static MatType * run(PyArrayObject * pyArray, void * storage) template<typename MatType, bool IsVectorAtCompileTime = MatType::IsVectorAtCompileTime>
struct init_matrix_or_array
{ {
assert(PyArray_NDIM(pyArray) == 1 || PyArray_NDIM(pyArray) == 2); static MatType * run(PyArrayObject * pyArray, void * storage)
int rows = -1, cols = -1;
if(PyArray_NDIM(pyArray) == 2)
{
rows = (int)PyArray_DIMS(pyArray)[0];
cols = (int)PyArray_DIMS(pyArray)[1];
}
else if(PyArray_NDIM(pyArray) == 1)
{ {
rows = (int)PyArray_DIMS(pyArray)[0]; assert(PyArray_NDIM(pyArray) == 1 || PyArray_NDIM(pyArray) == 2);
cols = 1;
int rows = -1, cols = -1;
if(PyArray_NDIM(pyArray) == 2)
{
rows = (int)PyArray_DIMS(pyArray)[0];
cols = (int)PyArray_DIMS(pyArray)[1];
}
else if(PyArray_NDIM(pyArray) == 1)
{
rows = (int)PyArray_DIMS(pyArray)[0];
cols = 1;
}
return new (storage) MatType(rows,cols);
} }
};
return new (storage) MatType(rows,cols);
}
};
template<typename MatType> template<typename MatType>
struct initEigenObject<MatType,true> struct init_matrix_or_array<MatType,true>
{
static MatType * run(PyArrayObject * pyArray, void * storage)
{ {
if(PyArray_NDIM(pyArray) == 1) static MatType * run(PyArrayObject * pyArray, void * storage)
{ {
const int rows_or_cols = (int)PyArray_DIMS(pyArray)[0]; if(PyArray_NDIM(pyArray) == 1)
return new (storage) MatType(rows_or_cols); {
} const int rows_or_cols = (int)PyArray_DIMS(pyArray)[0];
else return new (storage) MatType(rows_or_cols);
{ }
const int rows = (int)PyArray_DIMS(pyArray)[0]; else
const int cols = (int)PyArray_DIMS(pyArray)[1]; {
return new (storage) MatType(rows,cols); const int rows = (int)PyArray_DIMS(pyArray)[0];
const int cols = (int)PyArray_DIMS(pyArray)[1];
return new (storage) MatType(rows,cols);
}
} }
} };
};
template<typename Scalar, typename NewScalar, bool cast_is_valid = FromTypeToType<Scalar,NewScalar>::value > template<typename Scalar, typename NewScalar, bool cast_is_valid = FromTypeToType<Scalar,NewScalar>::value >
struct CastMatToMat struct cast_matrix_or_array
{
template<typename MatrixIn, typename MatrixOut>
static void run(const Eigen::MatrixBase<MatrixIn> & input,
const Eigen::MatrixBase<MatrixOut> & dest)
{ {
MatrixOut & dest_ = const_cast<MatrixOut &>(dest.derived()); template<typename MatrixIn, typename MatrixOut>
if(dest.rows() == input.rows()) static void run(const Eigen::MatrixBase<MatrixIn> & input,
dest_ = input.template cast<NewScalar>(); const Eigen::MatrixBase<MatrixOut> & dest)
else {
dest_ = input.transpose().template cast<NewScalar>(); MatrixOut & dest_ = const_cast<MatrixOut &>(dest.derived());
} if(dest.rows() == input.rows())
}; dest_ = input.template cast<NewScalar>();
else
dest_ = input.transpose().template cast<NewScalar>();
}
};
template<typename Scalar, typename NewScalar> template<typename Scalar, typename NewScalar>
struct CastMatToMat<Scalar,NewScalar,false> struct cast_matrix_or_array<Scalar,NewScalar,false>
{
template<typename MatrixIn, typename MatrixOut>
static void run(const Eigen::MatrixBase<MatrixIn> & /*input*/,
const Eigen::MatrixBase<MatrixOut> & /*dest*/)
{ {
// do nothing template<typename MatrixIn, typename MatrixOut>
assert("Must never happened"); static void run(const Eigen::MatrixBase<MatrixIn> & /*input*/,
} const Eigen::MatrixBase<MatrixOut> & /*dest*/)
}; {
// do nothing
assert("Must never happened");
}
};
} // namespace details
#define EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX(MatType,Scalar,NewScalar,pyArray,mat) \ #define EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX(MatType,Scalar,NewScalar,pyArray,mat) \
CastMatToMat<Scalar,NewScalar>::run(MapNumpy<MatType,Scalar>::map(pyArray),mat) details::cast_matrix_or_array<Scalar,NewScalar>::run(MapNumpy<MatType,Scalar>::map(pyArray),mat)
#define EIGENPY_CAST_FROM_EIGEN_MATRIX_TO_PYARRAY(MatType,Scalar,NewScalar,mat,pyArray) \ #define EIGENPY_CAST_FROM_EIGEN_MATRIX_TO_PYARRAY(MatType,Scalar,NewScalar,mat,pyArray) \
CastMatToMat<Scalar,NewScalar>::run(mat,MapNumpy<MatType,NewScalar>::map(pyArray)) details::cast_matrix_or_array<Scalar,NewScalar>::run(mat,MapNumpy<MatType,NewScalar>::map(pyArray))
template<typename MatType> template<typename MatType>
struct EigenObjectAllocator struct EigenObjectAllocator
...@@ -94,7 +99,7 @@ namespace eigenpy ...@@ -94,7 +99,7 @@ namespace eigenpy
static void allocate(PyArrayObject * pyArray, void * storage) static void allocate(PyArrayObject * pyArray, void * storage)
{ {
Type * mat_ptr = initEigenObject<Type>::run(pyArray,storage); Type * mat_ptr = details::init_matrix_or_array<Type>::run(pyArray,storage);
Type & mat = *mat_ptr; Type & mat = *mat_ptr;
const int pyArray_Type = EIGENPY_GET_PY_ARRAY_TYPE(pyArray); const int pyArray_Type = EIGENPY_GET_PY_ARRAY_TYPE(pyArray);
......
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