Skip to content

Commit a9565a6

Browse files
committed
Added integer and unsigned integer conversion support to eigen typemaps
1 parent e15d841 commit a9565a6

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

source/FAST/Python/EigenTypemaps.i

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,29 @@
4949
return true;
5050
}
5151

52+
template <class U>
53+
PyObject* convertNumber(U x);
54+
55+
template <>
56+
PyObject* convertNumber(float x) {
57+
return PyFloat_FromDouble((double)x);
58+
}
59+
60+
template <>
61+
PyObject* convertNumber(int x) {
62+
return PyLong_FromLong((long)x);
63+
}
64+
65+
template <>
66+
PyObject* convertNumber(unsigned int x) {
67+
return PyLong_FromUnsignedLong((unsigned long)x);
68+
}
69+
5270
template <class T>
5371
bool EigenVectorToPyTuple(T input, PyObject** output) {
5472
*output = PyTuple_New(input.size());
5573
for(int i = 0; i < input.size(); i++) {
56-
PyTuple_SetItem(*output, i, PyFloat_FromDouble((double)input(i)));
74+
PyTuple_SetItem(*output, i, convertNumber(input(i)));
5775
}
5876
return true;
5977
}
@@ -198,7 +216,6 @@
198216
}
199217

200218
%enddef // End eigen vector typemaps macro
201-
// TODO better signed/unsigned integer handling
202219

203220
// Matrix typemaps
204221
%define %eigen_matrix_typemaps(CLASS...)
@@ -241,7 +258,7 @@
241258
for(Py_ssize_t i = 0 ; i != $1.rows(); ++i) {
242259
PyObject* innerList = PyList_New($1.cols());
243260
for(Py_ssize_t j = 0 ; j != $1.cols(); ++j) {
244-
PyList_SetItem(innerList, j, PyFloat_FromDouble((double)$1(i, j)));
261+
PyList_SetItem(innerList, j, convertNumber($1(i, j)));
245262
}
246263
PyList_SetItem($result, i, innerList);
247264
}

0 commit comments

Comments
 (0)