Base: add Python number protocol support to Placement/Rotation
This commit is contained in:
@@ -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))) {
|
||||
@@ -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))) {
|
||||
@@ -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);
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
Namespace="Base"
|
||||
Constructor="true"
|
||||
Delete="true"
|
||||
NumberProtocol="true"
|
||||
RichCompare="true"
|
||||
FatherNamespace="Base">
|
||||
<Documentation>
|
||||
@@ -118,5 +119,11 @@ Placement(Base, Axis, Angle) -- define position and rotation
|
||||
</Documentation>
|
||||
<Parameter Name="Matrix" Type="Object" />
|
||||
</Attribute>
|
||||
<ClassDeclarations>public:
|
||||
PlacementPy(const Placement & pla, PyTypeObject *T = &Type)
|
||||
:PyObjectBase(new Placement(pla),T){}
|
||||
Placement value() const
|
||||
{ return *(getPlacementPtr()); }
|
||||
</ClassDeclarations>
|
||||
</PythonExport>
|
||||
</GenerateModel>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
Namespace="Base"
|
||||
Constructor="true"
|
||||
Delete="true"
|
||||
NumberProtocol="true"
|
||||
RichCompare="true"
|
||||
FatherNamespace="Base">
|
||||
<Documentation>
|
||||
|
||||
@@ -384,4 +384,197 @@ int RotationPy::setCustomAttributes(const char* attr, PyObject* obj)
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject* RotationPy::number_multiply_handler(PyObject *self, PyObject *other)
|
||||
{
|
||||
if (PyObject_TypeCheck(self, &(RotationPy::Type))) {
|
||||
auto a = static_cast<RotationPy*>(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, &(PlacementPy::Type))) {
|
||||
const auto &b = static_cast<PlacementPy*>(other)->value();
|
||||
return new PlacementPy(Placement(Vector3d(),a)*b);
|
||||
}
|
||||
|
||||
if (PyObject_TypeCheck(other, &(RotationPy::Type))) {
|
||||
const auto &b = static_cast<RotationPy*>(other)->value();
|
||||
return new RotationPy(a*b);
|
||||
}
|
||||
|
||||
if (PyObject_TypeCheck(other, &(MatrixPy::Type))) {
|
||||
const auto &b = static_cast<MatrixPy*>(other)->value();
|
||||
Matrix4D mat;
|
||||
a.getValue(mat);
|
||||
return new MatrixPy(mat*b);
|
||||
}
|
||||
}
|
||||
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_power_handler (PyObject* self, PyObject* other, PyObject* arg)
|
||||
{
|
||||
if (!PyObject_TypeCheck(self, &(RotationPy::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;
|
||||
}
|
||||
|
||||
Rotation a = static_cast<RotationPy*>(self)->value();
|
||||
|
||||
long b = Py::Int(other);
|
||||
if(!b)
|
||||
return new RotationPy(Rotation());
|
||||
|
||||
if(b < 0) {
|
||||
b = 1+b;
|
||||
a.invert();
|
||||
}
|
||||
auto res = a;
|
||||
for(;b;--b)
|
||||
res *= a;
|
||||
return new RotationPy(res);
|
||||
}
|
||||
|
||||
PyObject* RotationPy::number_add_handler(PyObject * /*self*/, PyObject * /*other*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject* RotationPy::number_subtract_handler(PyObject * /*self*/, PyObject * /*other*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_divide_handler (PyObject* /*self*/, PyObject* /*other*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_remainder_handler (PyObject* /*self*/, PyObject* /*other*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_divmod_handler (PyObject* /*self*/, PyObject* /*other*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_negative_handler (PyObject* /*self*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_positive_handler (PyObject* /*self*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_absolute_handler (PyObject* /*self*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RotationPy::number_nonzero_handler (PyObject* /*self*/)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_invert_handler (PyObject* /*self*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_lshift_handler (PyObject* /*self*/, PyObject* /*other*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_rshift_handler (PyObject* /*self*/, PyObject* /*other*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_and_handler (PyObject* /*self*/, PyObject* /*other*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_xor_handler (PyObject* /*self*/, PyObject* /*other*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_or_handler (PyObject* /*self*/, PyObject* /*other*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
int RotationPy::number_coerce_handler (PyObject ** /*self*/, PyObject ** /*other*/)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
PyObject * RotationPy::number_int_handler (PyObject * /*self*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
PyObject * RotationPy::number_long_handler (PyObject * /*self*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
PyObject * RotationPy::number_float_handler (PyObject * /*self*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
PyObject * RotationPy::number_oct_handler (PyObject * /*self*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject * RotationPy::number_hex_handler (PyObject * /*self*/)
|
||||
{
|
||||
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -151,6 +151,13 @@ PyObject* VectorPy::number_multiply_handler(PyObject *self, PyObject *other)
|
||||
return new MatrixPy(b);
|
||||
}
|
||||
|
||||
if (PyObject_TypeCheck(other, &PlacementPy::Type)) {
|
||||
Base::Placement b = static_cast<PlacementPy*>(other)->value();
|
||||
Vector3d res;
|
||||
b.multVec(a,res);
|
||||
return new VectorPy(res);
|
||||
}
|
||||
|
||||
if (PyObject_TypeCheck(other, &RotationPy::Type)) {
|
||||
Base::Rotation b = static_cast<RotationPy*>(other)->value();
|
||||
return new VectorPy(b.multVec(a));
|
||||
|
||||
Reference in New Issue
Block a user