@@ -54,36 +54,74 @@ namespace eigenpy
5454
5555 namespace bp = boost::python;
5656
57- struct PyMatrixType
57+ struct NumpyType
5858 {
59-
60- static PyMatrixType & getInstance ()
59+
60+ static NumpyType & getInstance ()
6161 {
62- static PyMatrixType instance;
62+ static NumpyType instance;
6363 return instance;
6464 }
6565
66- operator bp::object () { return pyMatrixType ; }
66+ operator bp::object () { return CurrentNumpyType ; }
6767
6868 bp::object make (PyArrayObject* pyArray, bool copy = false )
6969 { return make ((PyObject*)pyArray,copy); }
70+
7071 bp::object make (PyObject* pyObj, bool copy = false )
7172 {
72- bp::object m
73- = pyMatrixType (bp::object (bp::handle<>(pyObj)), bp::object (), copy);
73+ bp::object m;
74+ if (PyType_IsSubtype (reinterpret_cast <PyTypeObject*>(CurrentNumpyType.ptr ()),NumpyMatrixType))
75+ m = NumpyMatrixObject (bp::object (bp::handle<>(pyObj)), bp::object (), copy);
76+ // m = NumpyAsMatrixObject(bp::object(bp::handle<>(pyObj)));
77+ else if (PyType_IsSubtype (reinterpret_cast <PyTypeObject*>(CurrentNumpyType.ptr ()),NumpyArrayType))
78+ m = bp::object (bp::handle<>(pyObj)); // nothing to do here
79+
7480 Py_INCREF (m.ptr ());
7581 return m;
7682 }
83+
84+ static void setNumpyType (bp::object & obj)
85+ {
86+ PyTypeObject * obj_type = PyType_Check (obj.ptr ()) ? reinterpret_cast <PyTypeObject*>(obj.ptr ()) : obj.ptr ()->ob_type ;
87+ if (PyType_IsSubtype (obj_type,getInstance ().NumpyMatrixType ))
88+ getInstance ().CurrentNumpyType = getInstance ().NumpyMatrixObject ;
89+ else if (PyType_IsSubtype (obj_type,getInstance ().NumpyArrayType ))
90+ getInstance ().CurrentNumpyType = getInstance ().NumpyArrayObject ;
91+ }
92+
93+ static void switchToNumpyArray ()
94+ {
95+ getInstance ().CurrentNumpyType = getInstance ().NumpyArrayObject ;
96+ }
97+
98+ static void switchToNumpyMatrix ()
99+ {
100+ getInstance ().CurrentNumpyType = getInstance ().NumpyMatrixObject ;
101+ }
77102
78103 protected:
79- PyMatrixType ()
104+ NumpyType ()
80105 {
81106 pyModule = bp::import (" numpy" );
82- pyMatrixType = pyModule.attr (" matrix" );
107+
108+ NumpyMatrixObject = pyModule.attr (" matrix" );
109+ NumpyMatrixType = reinterpret_cast <PyTypeObject*>(NumpyMatrixObject.ptr ());
110+ NumpyAsMatrixObject = pyModule.attr (" asmatrix" );
111+ NumpyAsMatrixType = reinterpret_cast <PyTypeObject*>(NumpyAsMatrixObject.ptr ());
112+ NumpyArrayObject = pyModule.attr (" ndarray" );
113+ NumpyArrayType = reinterpret_cast <PyTypeObject*>(NumpyArrayObject.ptr ());
114+
115+ CurrentNumpyType = NumpyMatrixObject; // default conversion
83116 }
84117
85- bp::object pyMatrixType ;
118+ bp::object CurrentNumpyType ;
86119 bp::object pyModule;
120+
121+ // Numpy types
122+ bp::object NumpyMatrixObject; PyTypeObject * NumpyMatrixType;
123+ bp::object NumpyAsMatrixObject; PyTypeObject * NumpyAsMatrixType;
124+ bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType;
87125 };
88126
89127 template <typename MatType>
@@ -174,7 +212,7 @@ namespace eigenpy
174212
175213 EigenObjectAllocator<MatType>::convert (mat,pyArray);
176214
177- return PyMatrixType ::getInstance ().make (pyArray).ptr ();
215+ return NumpyType ::getInstance ().make (pyArray).ptr ();
178216 }
179217 };
180218
@@ -293,7 +331,7 @@ namespace eigenpy
293331 return obj_ptr;
294332 }
295333
296- // Convert obj_ptr into a Eigenvec
334+ // Convert obj_ptr into an Eigen::Vector
297335 static void construct (PyObject* pyObj,
298336 bp::converter::rvalue_from_python_stage1_data* memory)
299337 {
0 commit comments