eigen-to-python.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2014-2020 CNRS INRIA
3 //
4 
5 #ifndef __eigenpy_eigen_to_python_hpp__
6 #define __eigenpy_eigen_to_python_hpp__
7 
8 #include "eigenpy/fwd.hpp"
9 #include "eigenpy/numpy-type.hpp"
12 
13 #include <boost/type_traits.hpp>
14 
15 namespace boost { namespace python {
16 
17  template<typename MatrixRef, class MakeHolder>
19  {
20  template <class U>
21  inline PyObject* operator()(U const& mat) const
22  {
23  return eigenpy::EigenToPy<MatrixRef>::convert(const_cast<U&>(mat));
24  }
25 
26 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
27  inline PyTypeObject const*
28  get_pytype() const
29  {
30  return converter::registered_pytype<MatrixRef>::get_pytype();
31  }
32 #endif
33  };
34 
35  template <typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime, int Options, int MaxRowsAtCompileTime, int MaxColsAtCompileTime, class MakeHolder>
36  struct to_python_indirect<Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime,Options,MaxRowsAtCompileTime,MaxColsAtCompileTime>&,MakeHolder>
37  : to_python_indirect_eigen<Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime,Options,MaxRowsAtCompileTime,MaxColsAtCompileTime>&,MakeHolder>
38  {
39  };
40 
41  template <typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime, int Options, int MaxRowsAtCompileTime, int MaxColsAtCompileTime, class MakeHolder>
42  struct to_python_indirect<const Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime,Options,MaxRowsAtCompileTime,MaxColsAtCompileTime>&,MakeHolder>
43  : to_python_indirect_eigen<const Eigen::Matrix<Scalar,RowsAtCompileTime,ColsAtCompileTime,Options,MaxRowsAtCompileTime,MaxColsAtCompileTime>&,MakeHolder>
44  {
45  };
46 
47 }}
48 
49 namespace eigenpy
50 {
51  namespace bp = boost::python;
52 
53  template<typename MatType, typename _Scalar>
54  struct EigenToPy
55  {
56  static PyObject* convert(typename boost::add_reference<typename boost::add_const<MatType>::type>::type mat)
57  {
58  typedef typename boost::remove_const<typename boost::remove_reference<MatType>::type>::type MatrixDerived;
59 
60  assert( (mat.rows()<INT_MAX) && (mat.cols()<INT_MAX)
61  && "Matrix range larger than int ... should never happen." );
62  const npy_intp R = (npy_intp)mat.rows(), C = (npy_intp)mat.cols();
63 
64  PyArrayObject* pyArray;
65  // Allocate Python memory
66  if( ( ((!(C == 1) != !(R == 1)) && !MatrixDerived::IsVectorAtCompileTime) || MatrixDerived::IsVectorAtCompileTime)
67  && NumpyType::getType() == ARRAY_TYPE) // Handle array with a single dimension
68  {
69  npy_intp shape[1] = { C == 1 ? R : C };
70  pyArray = NumpyAllocator<MatType>::allocate(const_cast<MatrixDerived &>(mat.derived()),
71  1,shape);
72  }
73  else
74  {
75  npy_intp shape[2] = { R,C };
76  pyArray = NumpyAllocator<MatType>::allocate(const_cast<MatrixDerived &>(mat.derived()),
77  2,shape);
78  }
79 
80  // Create an instance (either np.array or np.matrix)
81  return NumpyType::make(pyArray).ptr();
82  }
83  };
84 
85  template<typename MatType, int Options, typename Stride, typename _Scalar>
86  struct EigenToPy< Eigen::Ref<MatType,Options,Stride>,_Scalar >
87  {
88  static PyObject* convert(const Eigen::Ref<MatType,Options,Stride> & mat)
89  {
90  typedef Eigen::Ref<MatType,Options,Stride> EigenRef;
91 
92  assert( (mat.rows()<INT_MAX) && (mat.cols()<INT_MAX)
93  && "Matrix range larger than int ... should never happen." );
94  const npy_intp R = (npy_intp)mat.rows(), C = (npy_intp)mat.cols();
95 
96  PyArrayObject* pyArray;
97  // Allocate Python memory
98  if( ( ((!(C == 1) != !(R == 1)) && !MatType::IsVectorAtCompileTime) || MatType::IsVectorAtCompileTime)
99  && NumpyType::getType() == ARRAY_TYPE) // Handle array with a single dimension
100  {
101  npy_intp shape[1] = { C == 1 ? R : C };
102  pyArray = NumpyAllocator<EigenRef>::allocate(const_cast<EigenRef &>(mat),1,shape);
103  }
104  else
105  {
106  npy_intp shape[2] = { R,C };
107  pyArray = NumpyAllocator<EigenRef>::allocate(const_cast<EigenRef &>(mat),2,shape);
108  }
109 
110  // Create an instance (either np.array or np.matrix)
111  return NumpyType::make(pyArray).ptr();
112  }
113  };
114 
115  template<typename MatType>
117  {
118  static void registration()
119  {
120  bp::to_python_converter<MatType,EigenToPy<MatType> >();
121  }
122  };
123 }
124 
125 #endif // __eigenpy_eigen_to_python_hpp__
static PyObject * convert(const Eigen::Ref< MatType, Options, Stride > &mat)
Definition: complex.cpp:7
PyTypeObject const * get_pytype() const
PyObject * operator()(U const &mat) const
static PyObject * convert(typename boost::add_reference< typename boost::add_const< MatType >::type >::type mat)
Definition: python.py:1


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Apr 17 2021 02:37:59