|
13 | 13 | #include <iostream> |
14 | 14 |
|
15 | 15 | #include "eigenpy/eigenpy.hpp" |
| 16 | +#include "eigenpy/numpy-type.hpp" |
16 | 17 | #include "eigenpy/registration.hpp" |
17 | 18 | #include "eigenpy/map.hpp" |
18 | 19 | #include "eigenpy/exception.hpp" |
@@ -64,124 +65,6 @@ namespace eigenpy |
64 | 65 |
|
65 | 66 | namespace bp = boost::python; |
66 | 67 |
|
67 | | - enum NP_TYPE |
68 | | - { |
69 | | - MATRIX_TYPE, |
70 | | - ARRAY_TYPE |
71 | | - }; |
72 | | - |
73 | | - struct NumpyType |
74 | | - { |
75 | | - |
76 | | - static NumpyType & getInstance() |
77 | | - { |
78 | | - static NumpyType instance; |
79 | | - return instance; |
80 | | - } |
81 | | - |
82 | | - operator bp::object () { return CurrentNumpyType; } |
83 | | - |
84 | | - bp::object make(PyArrayObject* pyArray, bool copy = false) |
85 | | - { return make((PyObject*)pyArray,copy); } |
86 | | - |
87 | | - bp::object make(PyObject* pyObj, bool copy = false) |
88 | | - { |
89 | | - bp::object m; |
90 | | - if(PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(CurrentNumpyType.ptr()),NumpyMatrixType)) |
91 | | - m = NumpyMatrixObject(bp::object(bp::handle<>(pyObj)), bp::object(), copy); |
92 | | -// m = NumpyAsMatrixObject(bp::object(bp::handle<>(pyObj))); |
93 | | - else if(PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(CurrentNumpyType.ptr()),NumpyArrayType)) |
94 | | - m = bp::object(bp::handle<>(pyObj)); // nothing to do here |
95 | | - |
96 | | - Py_INCREF(m.ptr()); |
97 | | - return m; |
98 | | - } |
99 | | - |
100 | | - static void setNumpyType(bp::object & obj) |
101 | | - { |
102 | | - PyTypeObject * obj_type = PyType_Check(obj.ptr()) ? reinterpret_cast<PyTypeObject*>(obj.ptr()) : obj.ptr()->ob_type; |
103 | | - if(PyType_IsSubtype(obj_type,getInstance().NumpyMatrixType)) |
104 | | - switchToNumpyMatrix(); |
105 | | - else if(PyType_IsSubtype(obj_type,getInstance().NumpyArrayType)) |
106 | | - switchToNumpyArray(); |
107 | | - } |
108 | | - |
109 | | - static void switchToNumpyArray() |
110 | | - { |
111 | | - getInstance().CurrentNumpyType = getInstance().NumpyArrayObject; |
112 | | - getType() = ARRAY_TYPE; |
113 | | - } |
114 | | - |
115 | | - static void switchToNumpyMatrix() |
116 | | - { |
117 | | - getInstance().CurrentNumpyType = getInstance().NumpyMatrixObject; |
118 | | - getType() = MATRIX_TYPE; |
119 | | - } |
120 | | - |
121 | | - static NP_TYPE & getType() |
122 | | - { |
123 | | - static NP_TYPE np_type; |
124 | | - return np_type; |
125 | | - } |
126 | | - |
127 | | - static bp::object getNumpyType() |
128 | | - { |
129 | | - return getInstance().CurrentNumpyType; |
130 | | - } |
131 | | - |
132 | | - static const PyTypeObject * getNumpyMatrixType() |
133 | | - { |
134 | | - return getInstance().NumpyMatrixType; |
135 | | - } |
136 | | - |
137 | | - static const PyTypeObject * getNumpyArrayType() |
138 | | - { |
139 | | - return getInstance().NumpyArrayType; |
140 | | - } |
141 | | - |
142 | | - static bool isMatrix() |
143 | | - { |
144 | | - return PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(getInstance().CurrentNumpyType.ptr()), |
145 | | - getInstance().NumpyMatrixType); |
146 | | - } |
147 | | - |
148 | | - static bool isArray() |
149 | | - { |
150 | | - return PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(getInstance().CurrentNumpyType.ptr()), |
151 | | - getInstance().NumpyArrayType); |
152 | | - } |
153 | | - |
154 | | - protected: |
155 | | - NumpyType() |
156 | | - { |
157 | | - pyModule = bp::import("numpy"); |
158 | | -#if PY_MAJOR_VERSION >= 3 |
159 | | - // TODO I don't know why this Py_INCREF is necessary. |
160 | | - // Without it, the destructor of NumpyType SEGV sometimes. |
161 | | - Py_INCREF(pyModule.ptr()); |
162 | | -#endif |
163 | | - |
164 | | - NumpyMatrixObject = pyModule.attr("matrix"); |
165 | | - NumpyMatrixType = reinterpret_cast<PyTypeObject*>(NumpyMatrixObject.ptr()); |
166 | | - NumpyArrayObject = pyModule.attr("ndarray"); |
167 | | - NumpyArrayType = reinterpret_cast<PyTypeObject*>(NumpyArrayObject.ptr()); |
168 | | - //NumpyAsMatrixObject = pyModule.attr("asmatrix"); |
169 | | - //NumpyAsMatrixType = reinterpret_cast<PyTypeObject*>(NumpyAsMatrixObject.ptr()); |
170 | | - |
171 | | - CurrentNumpyType = NumpyArrayObject; // default conversion |
172 | | - getType() = ARRAY_TYPE; |
173 | | - } |
174 | | - |
175 | | - bp::object CurrentNumpyType; |
176 | | - bp::object pyModule; |
177 | | - |
178 | | - // Numpy types |
179 | | - bp::object NumpyMatrixObject; PyTypeObject * NumpyMatrixType; |
180 | | - //bp::object NumpyAsMatrixObject; PyTypeObject * NumpyAsMatrixType; |
181 | | - bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType; |
182 | | - |
183 | | - }; |
184 | | - |
185 | 68 | template<typename MatType, bool IsVectorAtCompileTime = MatType::IsVectorAtCompileTime> |
186 | 69 | struct initEigenObject |
187 | 70 | { |
|
0 commit comments