From f29fb6d288310eb84a64f089800463f5320c439a Mon Sep 17 00:00:00 2001 From: Justin Carpentier <justin.carpentier@inria.fr> Date: Fri, 21 Feb 2020 18:35:16 +0100 Subject: [PATCH] core: move EigenToPy to a dedicated file --- CMakeLists.txt | 1 + include/eigenpy/details.hpp | 41 +---------------------- include/eigenpy/eigen-to-python.hpp | 52 +++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 40 deletions(-) create mode 100644 include/eigenpy/eigen-to-python.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c26134..bac2737 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,6 +107,7 @@ SET(${PROJECT_NAME}_HEADERS include/eigenpy/details.hpp include/eigenpy/fwd.hpp include/eigenpy/eigen-allocator.hpp + include/eigenpy/eigen-to-python.hpp include/eigenpy/map.hpp include/eigenpy/geometry.hpp include/eigenpy/geometry-conversion.hpp diff --git a/include/eigenpy/details.hpp b/include/eigenpy/details.hpp index b989d2f..b29878b 100644 --- a/include/eigenpy/details.hpp +++ b/include/eigenpy/details.hpp @@ -17,12 +17,12 @@ #include "eigenpy/eigenpy.hpp" #include "eigenpy/eigen-allocator.hpp" +#include "eigenpy/eigen-to-python.hpp" #include "eigenpy/registration.hpp" #include "eigenpy/map.hpp" #include "eigenpy/exception.hpp" -#define GET_PY_ARRAY_TYPE(array) PyArray_ObjectType(reinterpret_cast<PyObject *>(array), 0) namespace boost { namespace python { namespace detail { @@ -59,45 +59,7 @@ namespace boost { namespace python { namespace detail { namespace eigenpy { - namespace bp = boost::python; - - /* --- TO PYTHON -------------------------------------------------------------- */ - template<typename MatType> - struct EigenToPy - { - static PyObject* convert(MatType const & mat) - { - typedef typename MatType::Scalar Scalar; - assert( (mat.rows()<INT_MAX) && (mat.cols()<INT_MAX) - && "Matrix range larger than int ... should never happen." ); - const npy_intp R = (npy_intp)mat.rows(), C = (npy_intp)mat.cols(); - - PyArrayObject* pyArray; - // Allocate Python memory - std::cout << "basic convert" << std::endl; - if( ( ((!(C == 1) != !(R == 1)) && !MatType::IsVectorAtCompileTime) || MatType::IsVectorAtCompileTime) - && NumpyType::getType() == ARRAY_TYPE) // Handle array with a single dimension - { - std::cout << "mat1\n" << mat << std::endl; - npy_intp shape[1] = { C == 1 ? R : C }; - pyArray = (PyArrayObject*) PyArray_SimpleNew(1, shape, - NumpyEquivalentType<Scalar>::type_code); - } - else - { - npy_intp shape[2] = { R,C }; - pyArray = (PyArrayObject*) PyArray_SimpleNew(2, shape, - NumpyEquivalentType<Scalar>::type_code); - } - - // Copy data - EigenAllocator<MatType>::copy(mat,pyArray); - - // Create an instance (either np.array or np.matrix) - return NumpyType::getInstance().make(pyArray).ptr(); - } - }; /* --- FROM PYTHON ------------------------------------------------------------ */ @@ -341,7 +303,6 @@ namespace eigenpy &EigenFromPy<MatType>::construct,bp::type_id<MatType>()); } }; -#endif template<typename MatType,typename EigenEquivalentType> diff --git a/include/eigenpy/eigen-to-python.hpp b/include/eigenpy/eigen-to-python.hpp new file mode 100644 index 0000000..6c343f2 --- /dev/null +++ b/include/eigenpy/eigen-to-python.hpp @@ -0,0 +1,52 @@ +// +// Copyright (c) 2014-2020 CNRS INRIA +// + +#ifndef __eigenpy_eigen_to_python_hpp__ +#define __eigenpy_eigen_to_python_hpp__ + +#include "eigenpy/fwd.hpp" +#include "eigenpy/numpy-type.hpp" +#include "eigenpy/eigen-allocator.hpp" + +namespace eigenpy +{ + namespace bp = boost::python; + + template<typename MatType> + struct EigenToPy + { + static PyObject* convert(MatType const & mat) + { + typedef typename MatType::Scalar Scalar; + assert( (mat.rows()<INT_MAX) && (mat.cols()<INT_MAX) + && "Matrix range larger than int ... should never happen." ); + const npy_intp R = (npy_intp)mat.rows(), C = (npy_intp)mat.cols(); + + PyArrayObject* pyArray; + // Allocate Python memory + if( ( ((!(C == 1) != !(R == 1)) && !MatType::IsVectorAtCompileTime) || MatType::IsVectorAtCompileTime) + && NumpyType::getType() == ARRAY_TYPE) // Handle array with a single dimension + { + npy_intp shape[1] = { C == 1 ? R : C }; + pyArray = (PyArrayObject*) PyArray_SimpleNew(1, shape, + NumpyEquivalentType<Scalar>::type_code); + } + else + { + npy_intp shape[2] = { R,C }; + pyArray = (PyArrayObject*) PyArray_SimpleNew(2, shape, + NumpyEquivalentType<Scalar>::type_code); + } + + // Copy data + EigenAllocator<MatType>::copy(mat,pyArray); + + // Create an instance (either np.array or np.matrix) + return NumpyType::getInstance().make(pyArray).ptr(); + } + }; + +} + +#endif // __eigenpy_eigen_to_python_hpp__ -- GitLab