6 #ifndef __eigenpy_numpy_map_hpp__ 7 #define __eigenpy_numpy_map_hpp__ 15 template<
typename MatType,
typename InputScalar,
int AlignmentValue,
typename Str
ide,
bool IsVector = MatType::IsVectorAtCompileTime>
19 template<typename MatType, typename InputScalar, int AlignmentValue = EIGENPY_NO_ALIGNMENT_VALUE, typename Stride = typename StrideType<MatType>::type>
25 static EigenMap map(PyArrayObject* pyArray,
bool swap_dimensions =
false);
36 template<
typename MatType,
typename InputScalar,
int AlignmentValue,
typename Str
ide>
39 typedef Eigen::Matrix<InputScalar,MatType::RowsAtCompileTime,MatType::ColsAtCompileTime,MatType::Options>
EquivalentInputMatrixType;
40 typedef Eigen::Map<EquivalentInputMatrixType,AlignmentValue,Stride>
EigenMap;
42 static EigenMap
mapImpl(PyArrayObject* pyArray,
bool swap_dimensions =
false)
45 OuterStrideAtCompileTime = Stride::OuterStrideAtCompileTime,
46 InnerStrideAtCompileTime = Stride::InnerStrideAtCompileTime,
49 assert(PyArray_NDIM(pyArray) == 2 || PyArray_NDIM(pyArray) == 1);
51 const long int itemsize = PyArray_ITEMSIZE(pyArray);
52 int inner_stride = -1, outer_stride = -1;
54 if(PyArray_NDIM(pyArray) == 2)
56 assert( (PyArray_DIMS(pyArray)[0] < INT_MAX)
57 && (PyArray_DIMS(pyArray)[1] < INT_MAX)
58 && (PyArray_STRIDE(pyArray,0) < INT_MAX)
59 && (PyArray_STRIDE(pyArray,1) < INT_MAX) );
61 rows = (int)PyArray_DIMS(pyArray)[0];
62 cols = (int)PyArray_DIMS(pyArray)[1];
64 if(EquivalentInputMatrixType::IsRowMajor)
66 inner_stride = (int)PyArray_STRIDE(pyArray, 1) / (int)itemsize;
67 outer_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
71 inner_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
72 outer_stride = (int)PyArray_STRIDE(pyArray, 1) / (int)itemsize;
75 else if(PyArray_NDIM(pyArray) == 1)
77 assert( (PyArray_DIMS(pyArray)[0] < INT_MAX)
78 && (PyArray_STRIDE(pyArray,0) < INT_MAX));
82 rows = (int)PyArray_DIMS(pyArray)[0];
85 inner_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
91 cols = (int)PyArray_DIMS(pyArray)[0];
94 outer_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
99 if(InnerStrideAtCompileTime==0 && OuterStrideAtCompileTime==Eigen::Dynamic)
101 outer_stride = std::max(inner_stride,outer_stride); inner_stride = 0;
104 Stride stride(OuterStrideAtCompileTime==Eigen::Dynamic?outer_stride:OuterStrideAtCompileTime,
105 InnerStrideAtCompileTime==Eigen::Dynamic?inner_stride:InnerStrideAtCompileTime);
107 if( (MatType::RowsAtCompileTime != rows)
108 && (MatType::RowsAtCompileTime != Eigen::Dynamic) )
109 {
throw eigenpy::Exception(
"The number of rows does not fit with the matrix type."); }
111 if( (MatType::ColsAtCompileTime !=
cols)
112 && (MatType::ColsAtCompileTime != Eigen::Dynamic) )
113 {
throw eigenpy::Exception(
"The number of columns does not fit with the matrix type."); }
115 InputScalar* pyData =
reinterpret_cast<InputScalar*
>(PyArray_DATA(pyArray));
117 return EigenMap(pyData, rows,
cols, stride);
121 template<
typename MatType,
typename InputScalar,
int AlignmentValue,
typename Str
ide>
125 typedef Eigen::Map<EquivalentInputMatrixType,AlignmentValue,Stride>
EigenMap;
127 static EigenMap
mapImpl(PyArrayObject* pyArray,
bool swap_dimensions =
false)
130 assert( PyArray_NDIM(pyArray) <= 2 );
133 if( PyArray_NDIM(pyArray)==1 ) rowMajor = 0;
134 else if (PyArray_DIMS(pyArray)[0] == 0) rowMajor = 0;
135 else if (PyArray_DIMS(pyArray)[1] == 0) rowMajor = 1;
136 else rowMajor = (PyArray_DIMS(pyArray)[0]>PyArray_DIMS(pyArray)[1])?0:1;
138 assert( (PyArray_DIMS(pyArray)[rowMajor]< INT_MAX)
139 && (PyArray_STRIDE(pyArray, rowMajor) ));
140 const int R = (int)PyArray_DIMS(pyArray)[rowMajor];
141 const long int itemsize = PyArray_ITEMSIZE(pyArray);
142 const int stride = (int) PyArray_STRIDE(pyArray, rowMajor) / (int) itemsize;;
144 if( (MatType::MaxSizeAtCompileTime != R)
145 && (MatType::MaxSizeAtCompileTime != Eigen::Dynamic) )
146 {
throw eigenpy::Exception(
"The number of elements does not fit with the vector type."); }
148 InputScalar* pyData =
reinterpret_cast<InputScalar*
>(PyArray_DATA(pyArray));
150 return EigenMap( pyData, R, Stride(stride) );
154 template<
typename MatType,
typename InputScalar,
int AlignmentValue,
typename Str
ide>
158 return Impl::mapImpl(pyArray,swap_dimensions);
163 #endif // define __eigenpy_numpy_map_hpp__
NumpyMapTraits< MatType, InputScalar, AlignmentValue, Stride > Impl
static EigenMap map(PyArrayObject *pyArray, bool swap_dimensions=false)
#define EIGENPY_UNUSED_VARIABLE(var)