Skip to content
This repository has been archived by the owner on Mar 29, 2024. It is now read-only.

Commit

Permalink
following brenoust feature request on GitHub regarding Python binding…
Browse files Browse the repository at this point in the history
…s: allow to assign to assign tuples/lists to color/layout/size properties (for instance: "graph['viewColor'][n] = [128, 128, 128]")

git-svn-id: https://svn.code.sf.net/p/auber/code/tulip@12679 f5d4c5f9-c943-4916-8a57-bd8605bf961c
  • Loading branch information
anlambert committed Sep 7, 2017
1 parent 618ebc3 commit ba94a25
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 4 deletions.
71 changes: 71 additions & 0 deletions library/tulip-python/bindings/tulip-core/Color.sip
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,77 @@ class Color {
#include <tulip/Color.h>
%End

// Convert Python list or tuple of integers to a tlp::Color
%ConvertToTypeCode
// Check if type is compatible
if (sipIsErr == NULL) {
if (!PyList_Check(sipPy) || PyList_GET_SIZE(sipPy) < 3) {
if (!PyTuple_Check(sipPy) || PyTuple_GET_SIZE(sipPy) < 3) {
return 0;
}
}
if (PyList_Check(sipPy)) {
for (SIP_SSIZE_T i = 0; i < PyList_GET_SIZE(sipPy); ++i) {
PyObject *item = PyList_GET_ITEM(sipPy, i);
#if PY_MAJOR_VERSION >= 3
if (!PyLong_Check(item)) {
#else
if (!PyLong_Check(item) && !PyInt_Check(item)) {
#endif
return 0;
}
}
} else {
for (SIP_SSIZE_T i = 0; i < PyTuple_GET_SIZE(sipPy); ++i) {
PyObject *item = PyTuple_GET_ITEM(sipPy, i);
#if PY_MAJOR_VERSION >= 3
if (!PyLong_Check(item)) {
#else
if (!PyLong_Check(item) && !PyInt_Check(item)) {
#endif
return 0;
}
}
}
return 1;
}

unsigned char r = 0;
unsigned char g = 0;
unsigned char b = 0;
unsigned char a = 255;

bool isList = PyList_Check(sipPy);

#if PY_MAJOR_VERSION >= 3
r = static_cast<unsigned char>(PyLong_AsLong(isList ? PyList_GET_ITEM(sipPy, 0) : PyTuple_GET_ITEM(sipPy, 0)));
g = static_cast<unsigned char>(PyLong_AsLong(isList ? PyList_GET_ITEM(sipPy, 1) : PyTuple_GET_ITEM(sipPy, 1)));
b = static_cast<unsigned char>(PyLong_AsLong(isList ? PyList_GET_ITEM(sipPy, 2) : PyTuple_GET_ITEM(sipPy, 2)));
if (PyList_GET_SIZE(sipPy) > 3)
a = static_cast<unsigned char>(PyLong_AsLong(isList ? PyList_GET_ITEM(sipPy, 3) : PyTuple_GET_ITEM(sipPy, 3)));
#else

static struct {
unsigned char operator()(PyObject *item) const {
if (PyLong_Check(item)) {
return static_cast<unsigned char>(PyLong_AsLong(item));
} else {
return static_cast<unsigned char>(PyInt_AsLong(item));
}
}
} toUnsignedChar;

r = toUnsignedChar(isList ? PyList_GET_ITEM(sipPy, 0) : PyTuple_GET_ITEM(sipPy, 0));
g = toUnsignedChar(isList ? PyList_GET_ITEM(sipPy, 1) : PyTuple_GET_ITEM(sipPy, 1));
b = toUnsignedChar(isList ? PyList_GET_ITEM(sipPy, 2) : PyTuple_GET_ITEM(sipPy, 2));
if ((isList && PyList_GET_SIZE(sipPy) > 3) || PyTuple_GET_SIZE(sipPy) > 3)
a = toUnsignedChar(isList ? PyList_GET_ITEM(sipPy, 3) : PyTuple_GET_ITEM(sipPy, 3));
#endif

*sipCppPtr = new tlp::Color(r, g, b, a);
return sipGetState(sipTransferObj);
%End

%Docstring
This class represents a color using the RGBA color space. It can be instantiated the following way::

Expand Down
49 changes: 49 additions & 0 deletions library/tulip-python/bindings/tulip-core/Module.sip
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,31 @@ extern PyObject* evalPythonStatement(const std::string &pythonStatement);
extern std::string convertPythonUnicodeObjectToStdString(PyObject *pyUnicodeObj);
#endif

extern bool isListOrTupleAndCanConvertToVec3fType(PyObject *pyObj);

template <typename VEC_TYPE>
VEC_TYPE *convertToVec3fType(PyObject *pyObj, float zVal = 0) {
float x = 0, y = 0, z = zVal;

bool isList = PyList_Check(pyObj);

PyObject *f = PyNumber_Float(isList ? PyList_GET_ITEM(pyObj, 0) : PyTuple_GET_ITEM(pyObj, 0));
x = static_cast<float>(PyFloat_AsDouble(f));
Py_XDECREF(f);

f = PyNumber_Float(isList ? PyList_GET_ITEM(pyObj, 1) : PyTuple_GET_ITEM(pyObj, 1));
y = static_cast<float>(PyFloat_AsDouble(f));
Py_XDECREF(f);

if ((isList && PyList_GET_SIZE(pyObj) > 2) || PyTuple_GET_SIZE(pyObj) > 2) {
f = PyNumber_Float(isList ? PyList_GET_ITEM(pyObj, 2) : PyTuple_GET_ITEM(pyObj, 2));
z = static_cast<float>(PyFloat_AsDouble(f));
Py_XDECREF(f);
}

return new VEC_TYPE(x, y, z);
}

%End


Expand Down Expand Up @@ -290,6 +315,30 @@ PyObject* evalPythonStatement(const std::string &pythonStatement) {
return ret;
}

bool isListOrTupleAndCanConvertToVec3fType(PyObject *pyObj) {
if (!PyList_Check(pyObj) || PyList_GET_SIZE(pyObj) < 2) {
if (!PyTuple_Check(pyObj) || PyTuple_GET_SIZE(pyObj) < 2) {
return false;
}
}
if (PyList_Check(pyObj)) {
for (SIP_SSIZE_T i = 0; i < PyList_GET_SIZE(pyObj); ++i) {
PyObject *item = PyList_GET_ITEM(pyObj, i);
if (!PyNumber_Check(item)) {
return false;
}
}
} else {
for (SIP_SSIZE_T i = 0; i < PyTuple_GET_SIZE(pyObj); ++i) {
PyObject *item = PyTuple_GET_ITEM(pyObj, i);
if (!PyNumber_Check(item)) {
return false;
}
}
}
return true;
}

%End

%Import ../stl/Module.sip
Expand Down
11 changes: 7 additions & 4 deletions library/tulip-python/bindings/tulip-core/Size.sip
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class PySize : tlp::Vec3f /PyName=Size, TypeHint="tlp.Size"/ {
%ConvertToTypeCode

if (sipIsErr == NULL) {
if (isListOrTupleAndCanConvertToVec3fType(sipPy)) {
return 1;
}
if (sipCanConvertToType(sipPy, sipFindType("tlp::Vec3f"), SIP_NOT_NONE|SIP_NO_CONVERTORS)) {
return 1;
}
Expand All @@ -85,10 +88,10 @@ class PySize : tlp::Vec3f /PyName=Size, TypeHint="tlp.Size"/ {

int state=0, err=0;

*sipCppPtr = new tlp::PySize(*reinterpret_cast<tlp::Vec3f*>(sipConvertToType(sipPy, sipFindType("tlp::Vec3f"), Py_None, SIP_NOT_NONE|SIP_NO_CONVERTORS, &state, &err)));

if (!*sipCppPtr) {
*sipIsErr = 1;
if (PyList_Check(sipPy) || PyTuple_Check(sipPy)) {
*sipCppPtr = convertToVec3fType<tlp::PySize>(sipPy, 1);
} else {
*sipCppPtr = new tlp::PySize(*reinterpret_cast<tlp::Vec3f*>(sipConvertToType(sipPy, sipFindType("tlp::Vec3f"), Py_None, SIP_NOT_NONE|SIP_NO_CONVERTORS, &state, &err)));
}

return sipGetState(sipTransferObj);
Expand Down
10 changes: 10 additions & 0 deletions library/tulip-python/bindings/tulip-core/Vector.sip
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
namespace tlp {
class Vec3f {

// Convert Python list or tuple of numbers to a tlp::Vec3f
%ConvertToTypeCode
// Check if type is compatible
if (sipIsErr == NULL) {
return isListOrTupleAndCanConvertToVec3fType(sipPy);
}
*sipCppPtr = convertToVec3fType<tlp::Vec3f>(sipPy);
return sipGetState(sipTransferObj);
%End

%Docstring
This class represents a vector with 3 floating point components.
It can be instantiated as illustrated in the code below: ::
Expand Down

0 comments on commit ba94a25

Please sign in to comment.