13#include <boost/python/extract.hpp>
17#define PY_ARRAY_UNIQUE_SYMBOL CORE_ARRAY_API
18#define NO_IMPORT_ARRAY
19#include <numpy/arrayobject.h>
21using boost::python::extract;
33 extract<Kernel::Matrix<double>> converter(p);
34 if (converter.check()) {
40 std::ostringstream msg;
41 msg <<
"Cannot convert object to Matrix. Expected numpy array, found " << p.ptr()->ob_type->tp_name;
42 throw std::invalid_argument(msg.str());
46 std::ostringstream msg;
47 msg <<
"Error converting numpy array to Matrix. Expected ndim=2, found "
49 << ndim <<
" dimensions.";
50 throw std::invalid_argument(msg.str());
62 return extract<Kernel::Matrix<double>>(
m_obj)();
66 const auto shape = PyArray_DIMS(ndarray);
67 npy_intp nx(shape[0]), ny(shape[1]);
69 for (npy_intp i = 0; i < nx; i++) {
71 for (npy_intp j = 0; j < ny; j++) {
72 row[j] = *((
double *)PyArray_GETPTR2(ndarray, i, j));
tagPyArrayObject PyArrayObject
static bool check(const boost::python::object &obj)
Check if a python object points to an array type object.
const boost::python::object & m_obj
A reference to the object.
Kernel::Matrix< double > operator()()
Produces a V3D object from the given PyObject.
PyObjectToMatrix(const boost::python::object &p)
Construct the converter object with the given Python object.
bool m_alreadyMatrix
Is the object a wrapped instance of Matrix<double>