42 #include <pybind11/include/pybind11/pybind11.h> 43 #include <pybind11/include/pybind11/numpy.h> 44 #include <OpenFlipper/common/UpdateType.hh> 49 namespace pybind11 {
namespace detail {
50 template <>
struct type_caster<QString> {
57 PYBIND11_TYPE_CASTER(QString, _(
"str"));
64 bool load(handle src,
bool ) {
67 PyObject *source = src.ptr();
69 if (!PyUnicode_Check(source))
73 const char *ptr = PyUnicode_AsUTF8AndSize(source, &size);
80 value = QString::fromUtf8(ptr, size);
83 return ( !PyErr_Occurred() );
93 static handle
cast(QString src, return_value_policy , handle ) {
94 return (PyUnicode_FromString( src.toUtf8().data()) );
116 namespace pybind11 {
namespace detail {
124 PYBIND11_TYPE_CASTER(
Vector, _(
"Vector"));
131 bool load(handle src,
bool convert ) {
134 PyObject* source = src.ptr();
136 if ( PyList_Check(source) ) {
138 if ( PyList_Size(source) != 3) {
139 PyErr_SetString(PyExc_RuntimeError,
"Vector Conversion: List size should be 3!");
140 throw py::error_already_set();
145 value =
Vector(PyFloat_AsDouble(PyList_GetItem(source,0)),PyFloat_AsDouble(PyList_GetItem(source,1)),PyFloat_AsDouble(PyList_GetItem(source,2)));
147 }
else if ( PyTuple_Check(source) ) {
148 if ( PyTuple_Size(source) != 3) {
149 PyErr_SetString(PyExc_RuntimeError,
"Vector Conversion: Tuple size should be 3!");
150 throw py::error_already_set();
155 value =
Vector(PyFloat_AsDouble(PyTuple_GetItem(source,0)),PyFloat_AsDouble(PyTuple_GetItem(source,1)),PyFloat_AsDouble(PyTuple_GetItem(source,2)));
157 }
else if ( py::cast<py::array>(source) ) {
159 py::array array = py::cast<py::array>(source);
161 if ( array.size() != 3) {
162 PyErr_SetString(PyExc_RuntimeError,
"Vector Conversion: Numpy Array size should be 3!");
163 throw py::error_already_set();
167 if (!convert && !py::array_t<double>::check_(src)) {
168 PyErr_SetString(PyExc_RuntimeError,
"Vector Conversion: Numpy Array, wrong dtype, conversion disabled");
172 auto buf = py::array_t<double, py::array::c_style | py::array::forcecast>::ensure(src);
175 PyErr_SetString(PyExc_RuntimeError,
"Vector Conversion: Numpy Array, conversion failed.");
178 if (buf.ndim() != 1 || buf.size() != 3) {
179 PyErr_SetString(PyExc_RuntimeError,
"Vector Conversion: Numpy Array dimension or size error. Dimension should be one, size 3!");
183 value =
Vector(buf.data());
187 PyErr_SetString(PyExc_RuntimeError,
"Vector Conversion: Not a list or a tuple or a numpy array.");
188 throw py::error_already_set();
193 return ( !PyErr_Occurred() );
203 static handle
cast(
Vector src, return_value_policy , handle parent ) {
205 py::array a(3, src.
data());
229 namespace pybind11 {
namespace detail {
237 PYBIND11_TYPE_CASTER(
Matrix4x4, _(
"Matrix4x4"));
244 bool load(handle src,
bool convert ) {
247 PyObject* source = src.ptr();
249 if ( PyList_Check(source) ) {
251 if ( PyList_Size(source) != 16) {
252 PyErr_SetString(PyExc_RuntimeError,
"Matrix4x4 Conversion: List size should be 16!");
253 throw py::error_already_set();
257 double convert[16] = { PyFloat_AsDouble(PyList_GetItem(source,0)),
258 PyFloat_AsDouble(PyList_GetItem(source,1)),
259 PyFloat_AsDouble(PyList_GetItem(source,2)),
260 PyFloat_AsDouble(PyList_GetItem(source,3)),
261 PyFloat_AsDouble(PyList_GetItem(source,4)),
262 PyFloat_AsDouble(PyList_GetItem(source,5)),
263 PyFloat_AsDouble(PyList_GetItem(source,6)),
264 PyFloat_AsDouble(PyList_GetItem(source,7)),
265 PyFloat_AsDouble(PyList_GetItem(source,8)),
266 PyFloat_AsDouble(PyList_GetItem(source,9)),
267 PyFloat_AsDouble(PyList_GetItem(source,0)),
268 PyFloat_AsDouble(PyList_GetItem(source,11)),
269 PyFloat_AsDouble(PyList_GetItem(source,12)),
270 PyFloat_AsDouble(PyList_GetItem(source,13)),
271 PyFloat_AsDouble(PyList_GetItem(source,14)),
272 PyFloat_AsDouble(PyList_GetItem(source,15))};
277 }
else if ( PyTuple_Check(source) ) {
278 if ( PyTuple_Size(source) != 16) {
279 PyErr_SetString(PyExc_RuntimeError,
"Matrix4x4 Conversion: Tuple size should be 3!");
280 throw py::error_already_set();
284 double convert[16] = { PyFloat_AsDouble(PyTuple_GetItem(source,0)),
285 PyFloat_AsDouble(PyTuple_GetItem(source,1)),
286 PyFloat_AsDouble(PyTuple_GetItem(source,2)),
287 PyFloat_AsDouble(PyTuple_GetItem(source,3)),
288 PyFloat_AsDouble(PyTuple_GetItem(source,4)),
289 PyFloat_AsDouble(PyTuple_GetItem(source,5)),
290 PyFloat_AsDouble(PyTuple_GetItem(source,6)),
291 PyFloat_AsDouble(PyTuple_GetItem(source,7)),
292 PyFloat_AsDouble(PyTuple_GetItem(source,8)),
293 PyFloat_AsDouble(PyTuple_GetItem(source,9)),
294 PyFloat_AsDouble(PyTuple_GetItem(source,10)),
295 PyFloat_AsDouble(PyTuple_GetItem(source,11)),
296 PyFloat_AsDouble(PyTuple_GetItem(source,12)),
297 PyFloat_AsDouble(PyTuple_GetItem(source,13)),
298 PyFloat_AsDouble(PyTuple_GetItem(source,14)),
299 PyFloat_AsDouble(PyTuple_GetItem(source,15))};
304 }
else if ( py::cast<py::array>(source) ) {
306 py::array array = py::cast<py::array>(source);
308 if ( array.size() != 16) {
309 PyErr_SetString(PyExc_RuntimeError,
"Matrix4x4 Conversion: Numpy Array size should be 16!");
310 throw py::error_already_set();
314 if (!convert && !py::array_t<double>::check_(src)) {
315 PyErr_SetString(PyExc_RuntimeError,
"Matrix4x4 Conversion: Numpy Array, wrong dtype, conversion disabled");
319 auto buf = py::array_t<double, py::array::c_style | py::array::forcecast>::ensure(src);
322 PyErr_SetString(PyExc_RuntimeError,
"Matrix4x4 Conversion: Numpy Array, conversion failed.");
329 if (buf.ndim() != 2 || buf.size() != 16 || buf.shape()[0] !=4 || buf.shape()[1] !=4) {
330 PyErr_SetString(PyExc_RuntimeError,
"Matrix4x4 Conversion: Numpy Array dimension or size error. Dimension should be four, size 16, and shape 4x4!");
338 PyErr_SetString(PyExc_RuntimeError,
"Matrix4x4 Conversion: Not a list or a tuple or a numpy array.");
339 throw py::error_already_set();
344 return ( !PyErr_Occurred() );
356 py::array a({4,4}, src.data());
379 namespace pybind11 {
namespace detail {
387 PYBIND11_TYPE_CASTER(
IdList, _(
"IdList"));
394 bool load(handle src,
bool convert ) {
397 PyObject* source = src.ptr();
399 if ( PyList_Check(source) ) {
401 const auto size = PyList_Size(source);
405 for (
auto i = 0 ; i < size ; ++i ) {
406 value[i] =
static_cast<int>(PyLong_AsLong(PyList_GET_ITEM(source,i)));
409 }
else if ( PyTuple_Check(source) ) {
411 const auto size = PyTuple_Size(source);
415 for (
auto i = 0 ; i < size ; ++i ) {
416 value[i] =
static_cast<int>(PyLong_AsLong(PyTuple_GET_ITEM(source,i)));
419 }
else if ( py::cast<py::array>(source) ) {
421 py::array array = py::cast<py::array>(source);
423 if ( array.ndim() != 1) {
424 PyErr_SetString(PyExc_RuntimeError,
"IdList Conversion: Numpy Array dimension should be one!");
425 throw py::error_already_set();
429 auto buf = py::array_t<int, py::array::c_style | py::array::forcecast>::ensure(src);
432 PyErr_SetString(PyExc_RuntimeError,
"IdList Conversion: Numpy Array, conversion failed.");
436 std::cerr <<
"Buffer size : " << buf.size() << std::endl;
438 value.resize(buf.size());
439 for (
auto i = 0 ; i < buf.size() ; ++i ) {
440 value[i] = *buf.data(i) ;
446 PyErr_SetString(PyExc_RuntimeError,
"Vector Conversion: Not a list or a tuple or a numpy array.");
447 throw py::error_already_set();
452 return ( !PyErr_Occurred() );
462 static handle
cast(
IdList src, return_value_policy , handle parent ) {
464 py::array a(src.size(), src.data());
498 namespace pybind11 {
namespace detail {
506 PYBIND11_TYPE_CASTER(
UpdateType, _(
"UpdateType"));
516 PyObject *source = src.ptr();
518 if (!PyUnicode_Check(source))
522 const char *ptr = PyUnicode_AsUTF8AndSize(source, &size);
528 QString updateString = QString::fromUtf8(ptr, size);
530 if ( updateString.contains(
"UPDATE_ALL") ) {
531 std::cerr <<
"Update_ALL" << std::endl;
538 if ( updateString.contains(
"All") ) {
539 std::cerr <<
"Update_ALL" << std::endl;
547 QStringList updateList = updateString.split(
";");
549 for (
auto i = 0 ; i < updateList.size() ; ++i ) {
551 std::cerr <<
"Update " << updateList[i].toStdString() << std::endl;
557 return ( !PyErr_Occurred() );
568 return (PyUnicode_FromString(
updateTypeName(src).toUtf8().data()) );
606 namespace pybind11 {
namespace detail {
614 PYBIND11_TYPE_CASTER(
DataType, _(
"DataType"));
624 PyObject *source = src.ptr();
626 if (!PyUnicode_Check(source))
630 const char *ptr = PyUnicode_AsUTF8AndSize(source, &size);
636 QString typeString = QString::fromUtf8(ptr, size);
638 value =
typeId(typeString);
641 return ( !PyErr_Occurred() );
652 return (PyUnicode_FromString(
typeName(src).toUtf8().data()) );
static handle cast(Vector src, return_value_policy, handle parent)
const UpdateType UPDATE_ALL(UpdateTypeSet(1))
Identifier for all updates.
ACG::Matrix4x4d Matrix4x4
Standard Type for a 4x4 Matrix used for scripting.
DLLEXPORT QString typeName(DataType _id)
Get the name of a type with given id.
bool load(handle src, bool convert)
bool load(handle src, bool convert)
static handle cast(QString src, return_value_policy, handle)
Scalar * data()
access to Scalar array
std::vector< int > IdList
Standard Type for id Lists used for scripting.
static handle cast(IdList src, return_value_policy, handle parent)
bool load(handle src, bool)
QString updateTypeName(UpdateType _id)
Get the name of a type with given id.
UpdateType updateType(QString _name)
Get the id of a type with given name.
ACG::Vec3d Vector
Standard Type for 3d Vector used for scripting.
static handle cast(Matrix4x4 src, return_value_policy, handle parent)
bool load(handle src, bool convert)
bool load(handle src, bool)
DLLEXPORT DataType typeId(QString _name)
Given a dataType Identifier string this function will return the id of the datatype.
static handle cast(DataType src, return_value_policy, handle)
bool load(handle src, bool)
static handle cast(UpdateType src, return_value_policy, handle)