#ifndef NUMPY_EIGEN_COPY_ROUTINES_HPP #define NUMPY_EIGEN_COPY_ROUTINES_HPP template struct CopyNumpyToEigenMatrix { typedef EIGEN_T matrix_t; typedef typename matrix_t::Scalar scalar_t; template void exec(EIGEN_T * M_, NPE_PY_ARRAY_OBJECT * P_) { // Assumes M is already initialized. for(int r = 0; r < M_->rows(); r++) { for(int c = 0; c < M_->cols(); c++) { T * p = static_cast(PyArray_GETPTR2(P_, r, c)); (*M_)(r,c) = static_cast(*p); } } } }; template struct CopyEigenToNumpyMatrix { typedef EIGEN_T matrix_t; typedef typename matrix_t::Scalar scalar_t; template void exec(EIGEN_T * M_, NPE_PY_ARRAY_OBJECT * P_) { // Assumes M is already initialized. for(int r = 0; r < M_->rows(); r++) { for(int c = 0; c < M_->cols(); c++) { T * p = static_cast(PyArray_GETPTR2(P_, r, c)); *p = static_cast((*M_)(r,c)); } } } }; template struct CopyEigenToNumpyVector { typedef EIGEN_T matrix_t; typedef typename matrix_t::Scalar scalar_t; template void exec(EIGEN_T * M_, NPE_PY_ARRAY_OBJECT * P_) { // Assumes M is already initialized. for(int i = 0; i < M_->size(); i++) { T * p = static_cast(PyArray_GETPTR1(P_, i)); *p = static_cast((*M_)(i)); } } }; template struct CopyNumpyToEigenVector { typedef EIGEN_T matrix_t; typedef typename matrix_t::Scalar scalar_t; template void exec(EIGEN_T * M_, NPE_PY_ARRAY_OBJECT * P_) { // Assumes M is already initialized. for(int i = 0; i < M_->size(); i++) { T * p = static_cast(PyArray_GETPTR1(P_, i)); (*M_)(i) = static_cast(*p); } } }; // Crazy syntax in this function was found here: // http://stackoverflow.com/questions/1840253/c-template-member-function-of-template-class-called-from-template-function/1840318#1840318 template< typename FUNCTOR_T> inline void numpyTypeDemuxer(typename FUNCTOR_T::matrix_t * M, NPE_PY_ARRAY_OBJECT * P) { FUNCTOR_T f; int npyType = getNpyType(P); switch(npyType) { case NPY_BOOL: f.template exec(M,P); break; case NPY_BYTE: f.template exec(M,P); break; case NPY_UBYTE: f.template exec(M,P); break; case NPY_SHORT: f.template exec(M,P); break; case NPY_USHORT: f.template exec(M,P); break; case NPY_INT: f.template exec(M,P); break; case NPY_UINT: f.template exec(M,P); break; case NPY_LONG: f.template exec(M,P); break; case NPY_ULONG: f.template exec(M,P); break; case NPY_LONGLONG: f.template exec(M,P); break; case NPY_ULONGLONG: f.template exec(M,P); break; case NPY_FLOAT: f.template exec(M,P); break; case NPY_DOUBLE: f.template exec(M,P); break; case NPY_LONGDOUBLE: f.template exec(M,P); break; default: THROW_TYPE_ERROR("Unsupported type: " << npyTypeToString(npyType)); } } #endif /* NUMPY_EIGEN_COPY_ROUTINES_HPP */