diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c261343b0b04559ee5691b6d305ac3257c52d1e..bac27375a6b7e64727eaba89aa7db2850e3df8db 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 b989d2fadcf77686873512d5578ec86e14ff4256..b29878b4dd49b3e4255bb115fb7c9f5817d8df2b 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 0000000000000000000000000000000000000000..6c343f2d4a590770393f7e506562eb094a7c2b17 --- /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__