Skip to content

Commit

Permalink
Base: add Python number protocol support to Placement/Rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
realthunder authored and wwmayer committed Sep 28, 2019
1 parent 8dec787 commit 83284a3
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Base/MatrixPyImp.cpp
Expand Up @@ -94,7 +94,7 @@ int MatrixPy::PyInit(PyObject* args, PyObject* /*kwd*/)
PyObject* MatrixPy::number_add_handler(PyObject *self, PyObject *other)
{
if (!PyObject_TypeCheck(self, &(MatrixPy::Type))) {
PyErr_SetString(PyExc_TypeError, "First arg must be Matrix");
PyErr_SetString(PyExc_NotImplementedError, "");
return 0;
}
if (!PyObject_TypeCheck(other, &(MatrixPy::Type))) {
Expand All @@ -109,7 +109,7 @@ PyObject* MatrixPy::number_add_handler(PyObject *self, PyObject *other)
PyObject* MatrixPy::number_subtract_handler(PyObject *self, PyObject *other)
{
if (!PyObject_TypeCheck(self, &(MatrixPy::Type))) {
PyErr_SetString(PyExc_TypeError, "First arg must be Matrix");
PyErr_SetString(PyExc_NotImplementedError, "");
return 0;
}
if (!PyObject_TypeCheck(other, &(MatrixPy::Type))) {
Expand Down Expand Up @@ -138,6 +138,11 @@ PyObject* MatrixPy::number_multiply_handler(PyObject *self, PyObject *other)
return new MatrixPy(a*b);
}

if (PyObject_TypeCheck(other, &(PlacementPy::Type))) {
auto b = static_cast<PlacementPy*>(other)->value();
return new MatrixPy(a*b.toMatrix());
}

if (PyObject_TypeCheck(other, &(MatrixPy::Type))) {
Base::Matrix4D b = static_cast<MatrixPy*>(other)->value();
return new MatrixPy(a*b);
Expand Down
7 changes: 7 additions & 0 deletions src/Base/PlacementPy.xml
Expand Up @@ -10,6 +10,7 @@
Namespace="Base"
Constructor="true"
Delete="true"
NumberProtocol="true"
RichCompare="true"
FatherNamespace="Base">
<Documentation>
Expand Down Expand Up @@ -118,5 +119,11 @@ Placement(Base, Axis, Angle) -- define position and rotation
</Documentation>
<Parameter Name="Matrix" Type="Object" />
</Attribute>
<ClassDeclarations>public:
PlacementPy(const Placement &amp; pla, PyTypeObject *T = &amp;Type)
:PyObjectBase(new Placement(pla),T){}
Placement value() const
{ return *(getPlacementPtr()); }
</ClassDeclarations>
</PythonExport>
</GenerateModel>
193 changes: 193 additions & 0 deletions src/Base/PlacementPyImp.cpp
Expand Up @@ -308,3 +308,196 @@ int PlacementPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}

PyObject* PlacementPy::number_multiply_handler(PyObject *self, PyObject *other)
{
if (PyObject_TypeCheck(self, &(PlacementPy::Type))) {
Base::Placement a = static_cast<PlacementPy*>(self)->value();

if (PyObject_TypeCheck(other, &(VectorPy::Type))) {
Vector3d res;
a.multVec(static_cast<VectorPy*>(other)->value(),res);
return new VectorPy(res);
}

if (PyObject_TypeCheck(other, &(RotationPy::Type))) {
Placement b(Vector3d(),static_cast<RotationPy*>(other)->value());
return new PlacementPy(a*b);
}

if (PyObject_TypeCheck(other, &(PlacementPy::Type))) {
const auto &b = static_cast<PlacementPy*>(other)->value();
return new PlacementPy(a*b);
}

if (PyObject_TypeCheck(other, &(MatrixPy::Type))) {
const auto &b = static_cast<MatrixPy*>(other)->value();
return new MatrixPy(a.toMatrix()*b);
}
}

PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_power_handler (PyObject* self, PyObject* other, PyObject* arg)
{
if (!PyObject_TypeCheck(self, &(PlacementPy::Type)) ||

#if PY_MAJOR_VERSION < 3
!PyInt_Check(other)
#else
!PyLong_Check(other)
#endif
|| arg != Py_None
)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

auto a = static_cast<PlacementPy*>(self)->value();

long b = Py::Int(other);
if(!b)
return new PlacementPy(Placement());

if(b < 0) {
b = 1+b;
a.invert();
}
auto res = a;
for(;b;--b)
res *= a;
return new PlacementPy(res);
}

PyObject* PlacementPy::number_add_handler(PyObject * /*self*/, PyObject * /*other*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject* PlacementPy::number_subtract_handler(PyObject * /*self*/, PyObject * /*other*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_divide_handler (PyObject* /*self*/, PyObject* /*other*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_remainder_handler (PyObject* /*self*/, PyObject* /*other*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_divmod_handler (PyObject* /*self*/, PyObject* /*other*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_negative_handler (PyObject* /*self*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_positive_handler (PyObject* /*self*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_absolute_handler (PyObject* /*self*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

int PlacementPy::number_nonzero_handler (PyObject* /*self*/)
{
return 1;
}

PyObject * PlacementPy::number_invert_handler (PyObject* /*self*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_lshift_handler (PyObject* /*self*/, PyObject* /*other*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_rshift_handler (PyObject* /*self*/, PyObject* /*other*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_and_handler (PyObject* /*self*/, PyObject* /*other*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_xor_handler (PyObject* /*self*/, PyObject* /*other*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_or_handler (PyObject* /*self*/, PyObject* /*other*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

#if PY_MAJOR_VERSION < 3
int PlacementPy::number_coerce_handler (PyObject ** /*self*/, PyObject ** /*other*/)
{
return 1;
}
#endif

PyObject * PlacementPy::number_int_handler (PyObject * /*self*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

#if PY_MAJOR_VERSION < 3
PyObject * PlacementPy::number_long_handler (PyObject * /*self*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}
#endif

PyObject * PlacementPy::number_float_handler (PyObject * /*self*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

#if PY_MAJOR_VERSION < 3
PyObject * PlacementPy::number_oct_handler (PyObject * /*self*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}

PyObject * PlacementPy::number_hex_handler (PyObject * /*self*/)
{
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
return 0;
}
#endif
1 change: 1 addition & 0 deletions src/Base/RotationPy.xml
Expand Up @@ -10,6 +10,7 @@
Namespace="Base"
Constructor="true"
Delete="true"
NumberProtocol="true"
RichCompare="true"
FatherNamespace="Base">
<Documentation>
Expand Down

0 comments on commit 83284a3

Please sign in to comment.