This repository has been archived by the owner on Aug 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patheigen_numpy.cc
242 lines (205 loc) · 7.1 KB
/
eigen_numpy.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <Eigen/Eigen>
#include <boost/numpy.hpp>
#include <glog/logging.h>
#include <numpy/arrayobject.h>
namespace bp = boost::python;
namespace np = boost::numpy;
using namespace Eigen;
template <typename SCALAR>
struct NumpyEquivalentType {};
template <> struct NumpyEquivalentType<double> {enum { type_code = NPY_DOUBLE };};
template <> struct NumpyEquivalentType<int> {enum { type_code = NPY_INT };};
template <> struct NumpyEquivalentType<float> {enum { type_code = NPY_FLOAT };};
template <> struct NumpyEquivalentType<std::complex<double> > {enum { type_code = NPY_CDOUBLE };};
template <typename SourceType, typename DestType >
static void copy_array(const SourceType* source, DestType* dest,
const npy_int &nb_rows, const npy_int &nb_cols,
const bool &isSourceTypeNumpy = false, const bool &isDestRowMajor = true,
const bool& isSourceRowMajor = true,
const npy_int &numpy_row_stride = 1, const npy_int &numpy_col_stride = 1)
{
// determine source strides
int row_stride = 1, col_stride = 1;
if (isSourceTypeNumpy) {
row_stride = numpy_row_stride;
col_stride = numpy_col_stride;
} else {
if (isSourceRowMajor) {
row_stride = nb_cols;
} else {
col_stride = nb_rows;
}
}
if (isDestRowMajor) {
for (int r=0; r<nb_rows; r++) {
for (int c=0; c<nb_cols; c++) {
*dest = source[r*row_stride + c*col_stride];
dest++;
}
}
} else {
for (int c=0; c<nb_cols; c++) {
for (int r=0; r<nb_rows; r++) {
*dest = source[r*row_stride + c*col_stride];
dest++;
}
}
}
}
template<class MatType> // MatrixXf or MatrixXd
struct EigenMatrixToPython {
static PyObject* convert(const MatType& mat) {
npy_intp shape[2] = { mat.rows(), mat.cols() };
PyArrayObject* python_array = (PyArrayObject*)PyArray_SimpleNew(
2, shape, NumpyEquivalentType<typename MatType::Scalar>::type_code);
copy_array(mat.data(),
(typename MatType::Scalar*)PyArray_DATA(python_array),
mat.rows(),
mat.cols(),
false,
true,
MatType::Flags & Eigen::RowMajorBit);
return (PyObject*)python_array;
}
};
template<typename MatType>
struct EigenMatrixFromPython {
typedef typename MatType::Scalar T;
EigenMatrixFromPython() {
bp::converter::registry::push_back(&convertible,
&construct,
bp::type_id<MatType>());
}
static void* convertible(PyObject* obj_ptr) {
if (!PyArray_Check(obj_ptr)) {
LOG(ERROR) << "PyArray_Check failed";
return 0;
}
if (PyArray_NDIM(obj_ptr) > 2) {
LOG(ERROR) << "dim > 2";
return 0;
}
if (PyArray_ObjectType(obj_ptr, 0) != NumpyEquivalentType<typename MatType::Scalar>::type_code) {
LOG(ERROR) << "types not compatible";
return 0;
}
int flags = PyArray_FLAGS(obj_ptr);
if (!(flags & NPY_ARRAY_C_CONTIGUOUS)) {
LOG(ERROR) << "Contiguous C array required";
return 0;
}
if (!(flags & NPY_ARRAY_ALIGNED)) {
LOG(ERROR) << "Aligned array required";
return 0;
}
return obj_ptr;
}
static void construct(PyObject* obj_ptr,
bp::converter::rvalue_from_python_stage1_data* data) {
const int R = MatType::RowsAtCompileTime;
const int C = MatType::ColsAtCompileTime;
using bp::extract;
PyArrayObject *array = reinterpret_cast<PyArrayObject*>(obj_ptr);
int ndims = PyArray_NDIM(obj_ptr);
int dtype_size = (PyArray_DESCR(obj_ptr))->elsize;
int s1 = PyArray_STRIDE(obj_ptr, 0);
CHECK_EQ(0, s1 % dtype_size);
int s2 = 0;
if (ndims > 1) {
s2 = PyArray_STRIDE(obj_ptr, 1);
CHECK_EQ(0, s2 % dtype_size);
}
int nrows = R;
int ncols = C;
if (ndims == 2) {
if (R != Eigen::Dynamic) {
CHECK_EQ(R, array->dimensions[0]);
} else {
nrows = array->dimensions[0];
}
if (C != Eigen::Dynamic) {
CHECK_EQ(C, array->dimensions[1]);
} else {
ncols = array->dimensions[1];
}
} else {
CHECK_EQ(1, ndims);
// Vector are a somehow special case because for Eigen, everything is
// a 2D array with a dimension set to 1, but to numpy, vectors are 1D
// arrays
// So we could get a 1x4 array for a Vector4
// For a vector, at least one of R, C must be 1
CHECK(R == 1 || C == 1);
if (R == 1) {
if (C != Eigen::Dynamic) {
CHECK_EQ(C, array->dimensions[0]);
} else {
ncols = array->dimensions[0];
}
// We have received a 1xC array and want to transform to VectorCd,
// so we need to transpose
// TODO: An alternative is to add wrappers for RowVector, but maybe
// implicit transposition is more natural
std::swap(s1, s2);
} else {
if (R != Eigen::Dynamic) {
CHECK_EQ(R, array->dimensions[0]);
} else {
nrows = array->dimensions[0];
}
}
}
T* raw_data = reinterpret_cast<T*>(PyArray_DATA(array));
typedef Map<Matrix<T, Dynamic, Dynamic, RowMajor>, Aligned,
Stride<Dynamic, Dynamic>> MapType;
void* storage=((bp::converter::rvalue_from_python_storage<MatType>*)
(data))->storage.bytes;
new (storage) MatType;
MatType* emat = (MatType*)storage;
// TODO: This is a (potentially) expensive copy operation. There should
// be a better way
*emat = MapType(raw_data, nrows, ncols,
Stride<Dynamic, Dynamic>(s1/dtype_size, s2/dtype_size));
data->convertible = storage;
}
};
#define EIGEN_MATRIX_CONVERTER(Type) \
EigenMatrixFromPython<Type>(); \
bp::to_python_converter<Type, EigenMatrixToPython<Type> >();
#define MAT_CONV(R, C, T) \
typedef Matrix<T, R, C> Matrix ## R ## C ## T; \
EIGEN_MATRIX_CONVERTER(Matrix ## R ## C ## T);
// This require a MAT_CONV for that Matrix type to be registered first
#define MAP_CONV(R, C, T) \
typedef Map<Matrix ## R ## C ## T> Map ## R ## C ## T; \
EIGEN_MATRIX_CONVERTER(Map ## R ## C ## T);
#define T_CONV(R, C, T) \
typedef Transpose<Matrix ## R ## C ## T> Transpose ## R ## C ## T; \
EIGEN_MATRIX_CONVERTER(Transpose ## R ## C ## T);
#define BLOCK_CONV(R, C, BR, BC, T) \
typedef Block<Matrix ## R ## C ## T, BR, BC> Block ## R ## C ## BR ## BC ## T; \
EIGEN_MATRIX_CONVERTER(Block ## R ## C ## BR ## BC ## T);
static const int X = Eigen::Dynamic;
void SetupEigenConverters() {
import_array();
EIGEN_MATRIX_CONVERTER(Matrix2f);
EIGEN_MATRIX_CONVERTER(Matrix2d);
EIGEN_MATRIX_CONVERTER(Matrix3f);
EIGEN_MATRIX_CONVERTER(Matrix3d);
EIGEN_MATRIX_CONVERTER(Matrix4f);
EIGEN_MATRIX_CONVERTER(Matrix4d);
EIGEN_MATRIX_CONVERTER(Vector2f);
EIGEN_MATRIX_CONVERTER(Vector3f);
EIGEN_MATRIX_CONVERTER(Vector4f);
EIGEN_MATRIX_CONVERTER(Vector2d);
EIGEN_MATRIX_CONVERTER(Vector3d);
EIGEN_MATRIX_CONVERTER(Vector4d);
MAT_CONV(2, 3, double);
MAT_CONV(X, 3, double);
MAT_CONV(X, X, double);
MAT_CONV(X, 1, double);
MAT_CONV(1, 4, double);
MAT_CONV(1, X, double);
MAT_CONV(3, 4, double);
MAT_CONV(2, X, double);
}