Commit 0c5cd310 authored by Jan Möbius's avatar Jan Möbius

Use numpy arrays as default type for vector in OpenFlipper

parent 2858b782
/*! \page changelog Changelog
- <b>OpenFlipper 4.0 ( ?/?/? )</b>
- <b>OpenFlipper 4.0 ( 25/02/2019 )</b>
- <b>Breaking changes</b>
- Picking functions use size_t instead of unsigned int now (Required to get rid of a lot of warnings during compilation)
- Enabled AUTOMOC in cmake for QT ( If you get redefinition errors, guard the corresponding headers with a #pragma once ; If you get gl.h before glew.h errors, move your widgets not using OpenGL into a subdir and add that to the DIRS macro in your CMakeLists.txt)
......
......@@ -76,7 +76,6 @@ namespace pybind11 { namespace detail {
return NULL;
}
/* Now try to convert into a C++ int */
value = QString::fromUtf8(ptr, size);
......@@ -113,7 +112,7 @@ namespace pybind11 { namespace detail {
* instance or return false upon failure. The second argument
* indicates whether implicit conversions should be applied.
*/
bool load(handle src, bool ) {
bool load(handle src, bool convert ) {
/* Extract PyObject from handle */
PyObject* source = src.ptr();
......@@ -139,6 +138,35 @@ namespace pybind11 { namespace detail {
/* Now try to convert into a C++ int */
value = Vector(PyFloat_AsDouble(PyTuple_GetItem(source,0)),PyFloat_AsDouble(PyTuple_GetItem(source,1)),PyFloat_AsDouble(PyTuple_GetItem(source,2)));
} else if ( py::cast<py::array>(source) ) {
py::array array = py::cast<py::array>(source);
if ( array.size() != 3) {
PyErr_SetString(PyExc_RuntimeError, "Vector Conversion: Numpy Array size should be 3!");
throw py::error_already_set();
return false;
}
if (!convert && !py::array_t<double>::check_(src)) {
PyErr_SetString(PyExc_RuntimeError, "Vector Conversion: Numpy Array, wrong dtype, conversion disabled");
return false;
}
auto buf = py::array_t<double, py::array::c_style | py::array::forcecast>::ensure(src);
if (!buf) {
PyErr_SetString(PyExc_RuntimeError, "Vector Conversion: Numpy Array, conversion failed.");
return false;
}
if (buf.ndim() != 1 || buf.size() != 3) {
PyErr_SetString(PyExc_RuntimeError, "Vector Conversion: Numpy Array dimension or size error. Dimension should be one, size 3!");
return false;
}
value = Vector(buf.data());
return true;
} else {
PyErr_SetString(PyExc_RuntimeError, "Vector Conversion: Not a list or a tuple.");
throw py::error_already_set();
......@@ -156,8 +184,14 @@ namespace pybind11 { namespace detail {
* ``return_value_policy::reference_internal``) and are generally
* ignored by implicit casters.
*/
static handle cast(Vector src, return_value_policy /* policy */, handle /* parent */) {
return PyTuple_Pack(3, PyFloat_FromDouble(src[0]),PyFloat_FromDouble(src[1]),PyFloat_FromDouble(src[2]));
static handle cast(Vector src, return_value_policy /* policy */, handle parent ) {
// Create numpy array
// py::array a({3,1}, src.data());
py::array a(3, src.data());
return a.release();
// return PyTuple_Pack(3, PyFloat_FromDouble(src[0]),PyFloat_FromDouble(src[1]),PyFloat_FromDouble(src[2]));
}
};
}} // namespace pybind11::detail
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment