MatrixPyImp.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 
00026 #include <climits>
00027 #include "Base/Matrix.h"
00028 
00029 // inclusion of the generated files (generated out of MatrixPy.xml)
00030 #include "VectorPy.h"
00031 #include "GeometryPyCXX.h"
00032 #include "MatrixPy.h"
00033 #include "MatrixPy.cpp"
00034 
00035 using namespace Base;
00036 
00037 // returns a string which represents the object e.g. when printed in python
00038 std::string MatrixPy::representation(void) const
00039 {
00040     const Base::Matrix4D& m = *(this->getMatrixPtr());
00041     std::stringstream str;
00042     str << "Matrix (";
00043     str << "(" << m[0][0] << ","<< m[0][1] << ","<< m[0][2] << ","<< m[0][3] << ")" << ",";
00044     str << "(" << m[1][0] << ","<< m[1][1] << ","<< m[1][2] << ","<< m[1][3] << ")"<< ",";
00045     str << "(" << m[2][0] << ","<< m[2][1] << ","<< m[2][2] << ","<< m[2][3] << ")"<< ",";
00046     str << "(" << m[3][0] << ","<< m[3][1] << ","<< m[3][2] << ","<< m[3][3] << ")";
00047     str << ")";
00048 
00049     return str.str();
00050 }
00051 
00052 PyObject *MatrixPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00053 {
00054     // create a new instance of MatrixPy and the Twin object 
00055     return new MatrixPy(new Matrix4D);
00056 }
00057 
00058 // constructor method
00059 int MatrixPy::PyInit(PyObject* args, PyObject* /*kwd*/)
00060 {
00061     double a11=1.0, a12=0.0, a13=0.0, a14=0.0;
00062     double a21=0.0, a22=1.0, a23=0.0, a24=0.0;
00063     double a31=0.0, a32=0.0, a33=1.0, a34=0.0;
00064     double a41=0.0, a42=0.0, a43=0.0, a44=1.0;
00065 
00066     if (PyArg_ParseTuple(args, "|dddddddddddddddd",
00067                           &a11,&a12,&a13,&a14,
00068                           &a21,&a22,&a23,&a24,
00069                           &a31,&a32,&a33,&a34,
00070                           &a41,&a42,&a43,&a44)) {
00071         MatrixPy::PointerType ptr = reinterpret_cast<MatrixPy::PointerType>(_pcTwinPointer);
00072         (*ptr) = Matrix4D(a11,a12,a13,a14,
00073                           a21,a22,a23,a24,
00074                           a31,a32,a33,a34,
00075                           a41,a42,a43,a44);
00076         return 0;
00077     }
00078 
00079     PyErr_Clear();
00080     PyObject *o;
00081     if (PyArg_ParseTuple(args, "O!", &(Base::MatrixPy::Type), &o)) {
00082         MatrixPy::PointerType ptr = reinterpret_cast<MatrixPy::PointerType>(_pcTwinPointer);
00083         (*ptr) = static_cast<MatrixPy*>(o)->value();
00084         return 0;
00085     }
00086 
00087     PyErr_SetString(PyExc_Exception, "matrix or up to 16 floats expected");
00088     return -1;
00089 }
00090 
00091 PyObject* MatrixPy::number_add_handler(PyObject *self, PyObject *other)
00092 {
00093     if (!PyObject_TypeCheck(self, &(MatrixPy::Type))) {
00094         PyErr_SetString(PyExc_TypeError, "First arg must be Matrix");
00095         return 0;
00096     }
00097     if (!PyObject_TypeCheck(other, &(MatrixPy::Type))) {
00098         PyErr_SetString(PyExc_TypeError, "Second arg must be Matrix");
00099         return 0;
00100     }
00101     Base::Matrix4D a = static_cast<MatrixPy*>(self)->value();
00102     Base::Matrix4D b = static_cast<MatrixPy*>(other)->value();
00103     return new MatrixPy(a+b);
00104 }
00105 
00106 PyObject* MatrixPy::number_subtract_handler(PyObject *self, PyObject *other)
00107 {
00108     if (!PyObject_TypeCheck(self, &(MatrixPy::Type))) {
00109         PyErr_SetString(PyExc_TypeError, "First arg must be Matrix");
00110         return 0;
00111     }
00112     if (!PyObject_TypeCheck(other, &(MatrixPy::Type))) {
00113         PyErr_SetString(PyExc_TypeError, "Second arg must be Matrix");
00114         return 0;
00115     }
00116     Base::Matrix4D a = static_cast<MatrixPy*>(self)->value();
00117     Base::Matrix4D b = static_cast<MatrixPy*>(other)->value();
00118     return new MatrixPy(a-b);
00119 }
00120 
00121 PyObject* MatrixPy::number_multiply_handler(PyObject *self, PyObject *other)
00122 {
00123     if (!PyObject_TypeCheck(self, &(MatrixPy::Type))) {
00124         PyErr_SetString(PyExc_TypeError, "First arg must be Matrix");
00125         return 0;
00126     }
00127     if (!PyObject_TypeCheck(other, &(MatrixPy::Type))) {
00128         PyErr_SetString(PyExc_TypeError, "Second arg must be Matrix");
00129         return 0;
00130     }
00131     Base::Matrix4D a = static_cast<MatrixPy*>(self)->value();
00132     Base::Matrix4D b = static_cast<MatrixPy*>(other)->value();
00133     return new MatrixPy(a*b);
00134 }
00135 
00136 PyObject* MatrixPy::move(PyObject * args)
00137 {
00138     double x,y,z;
00139     Base::Vector3d vec;
00140     PyObject *pcVecObj;
00141 
00142     if (PyArg_ParseTuple(args, "ddd", &x,&y,&z)) {   // convert args: Python->C
00143         vec.x = x;
00144         vec.y = y;
00145         vec.z = z;
00146     }
00147     else if (PyArg_ParseTuple(args, "O!:three floats or a vector is needed", 
00148         &PyTuple_Type, &pcVecObj)) {
00149         vec = getVectorFromTuple<double>(pcVecObj);
00150         // clears the error from the first PyArg_ParseTuple()6
00151         PyErr_Clear();
00152     }
00153     else if (PyArg_ParseTuple(args, "O!:three floats or a vector is needed", 
00154         &(Base::VectorPy::Type), &pcVecObj)) {
00155         Base::VectorPy  *pcObject = static_cast<Base::VectorPy*>(pcVecObj);
00156         Base::Vector3d* val = pcObject->getVectorPtr();
00157         vec.Set(val->x,val->y,val->z);
00158         // clears the error from the first PyArg_ParseTuple()6
00159         PyErr_Clear();
00160     }
00161     else
00162         return NULL;
00163 
00164     PY_TRY {
00165         getMatrixPtr()->move(vec);
00166     }
00167     PY_CATCH;
00168 
00169     Py_Return;
00170 }
00171 
00172 PyObject* MatrixPy::scale(PyObject * args)
00173 {
00174     double x,y,z;
00175     Base::Vector3d vec;
00176     PyObject *pcVecObj;
00177 
00178     if (PyArg_ParseTuple(args, "ddd", &x,&y,&z)) {   // convert args: Python->C
00179         vec.x = x;
00180         vec.y = y;
00181         vec.z = z;
00182     }
00183     else if (PyArg_ParseTuple(args, "O!:three floats or a vector is needed", 
00184         &PyTuple_Type, &pcVecObj)) {
00185         vec = getVectorFromTuple<double>(pcVecObj);
00186         // clears the error from the first PyArg_ParseTuple()6
00187         PyErr_Clear();
00188     }
00189     else if (PyArg_ParseTuple(args, "O!:three floats or a vector is needed", &(Base::VectorPy::Type), &pcVecObj)) {
00190         // convert args: Python->C
00191         Base::VectorPy  *pcObject = static_cast<Base::VectorPy*>(pcVecObj);
00192         Base::Vector3d* val = pcObject->getVectorPtr();
00193         vec.Set(val->x,val->y,val->z);
00194         // clears the error from the first PyArg_ParseTuple()6
00195         PyErr_Clear();
00196     }
00197     else
00198         return NULL;
00199 
00200     PY_TRY {
00201         getMatrixPtr()->scale(vec);
00202     }
00203     PY_CATCH;
00204 
00205     Py_Return;
00206 }
00207 
00208 PyObject* MatrixPy::unity(PyObject * args)
00209 {
00210     if (!PyArg_ParseTuple(args, ""))     // convert args: Python->C
00211       return NULL;                             // NULL triggers exception
00212     PY_TRY {
00213         getMatrixPtr()->setToUnity();
00214     }
00215     PY_CATCH;
00216 
00217     Py_Return;
00218 }
00219 
00220 PyObject* MatrixPy::transform(PyObject * args)
00221 {
00222     Base::Vector3d vec;
00223     Matrix4D mat;
00224     PyObject *pcVecObj,*pcMatObj;
00225 
00226     if (PyArg_ParseTuple(args, "O!O!: a transform point (Vector) and a transform matrix (Matrix) is needed",
00227         &(Base::VectorPy::Type), &pcVecObj, &(MatrixPy::Type), &pcMatObj) ) {   // convert args: Python->C
00228         Base::VectorPy  *pcObject = static_cast<Base::VectorPy*>(pcVecObj);
00229         Base::Vector3d* val = pcObject->getVectorPtr();
00230         vec.Set(val->x,val->y,val->z);
00231         mat = *(static_cast<MatrixPy*>(pcMatObj)->getMatrixPtr());
00232         // clears the error from the first PyArg_ParseTuple()6
00233         PyErr_Clear();
00234     }
00235     else
00236         return NULL;                                 // NULL triggers exception
00237 
00238     PY_TRY {
00239         getMatrixPtr()->transform(vec,mat);
00240     }
00241     PY_CATCH;
00242 
00243     Py_Return;
00244 }
00245 
00246 PyObject* MatrixPy::rotateX(PyObject * args)
00247 {
00248     double a;
00249     if (!PyArg_ParseTuple(args, "d: angle to rotate (double) needed", &a))     // convert args: Python->C
00250         return NULL;                                 // NULL triggers exception
00251 
00252     PY_TRY {
00253         getMatrixPtr()->rotX(a);
00254     }
00255     PY_CATCH;
00256 
00257     Py_Return;
00258 }
00259 
00260 PyObject* MatrixPy::rotateY(PyObject * args)
00261 {
00262     double a;
00263     if (!PyArg_ParseTuple(args, "d: angle to rotate (double) needed", &a))     // convert args: Python->C
00264         return NULL;                                 // NULL triggers exception
00265 
00266     PY_TRY {
00267         getMatrixPtr()->rotY(a);
00268     }
00269     PY_CATCH;
00270 
00271     Py_Return;
00272 }
00273 
00274 PyObject* MatrixPy::rotateZ(PyObject * args)
00275 {
00276     double a;
00277     if (!PyArg_ParseTuple(args, "d: angle to rotate (double) needed", &a))     // convert args: Python->C
00278         return NULL;                                 // NULL triggers exception
00279 
00280     PY_TRY {
00281         getMatrixPtr()->rotZ(a);
00282     }
00283     PY_CATCH;
00284 
00285     Py_Return;
00286 }
00287 
00288 PyObject* MatrixPy::multiply(PyObject * args)
00289 {
00290     PyObject* o;
00291     if (PyArg_ParseTuple(args, "O!", &(MatrixPy::Type), &o)) {
00292         Matrix4D mat = (*getMatrixPtr()) * static_cast<Base::MatrixPy*>(o)->value();
00293         return new MatrixPy(new Matrix4D(mat));
00294     }
00295 
00296     PyErr_Clear();
00297     if (PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &o)) {
00298         Vector3d vec = (*getMatrixPtr()) * static_cast<Base::VectorPy*>(o)->value();
00299         return new VectorPy(new Vector3d(vec));
00300     }
00301 
00302     PyErr_SetString(PyExc_Exception, "either vector or matrix expected");
00303     return 0;
00304 }
00305 
00306 PyObject* MatrixPy::invert(PyObject * args)
00307 {
00308     if (!PyArg_ParseTuple(args, ""))
00309         return NULL;
00310 
00311     PY_TRY {
00312         if (getMatrixPtr()->determinant() > DBL_EPSILON)
00313             getMatrixPtr()->inverse();
00314         else {
00315             PyErr_SetString(PyExc_Exception, "Cannot invert singular matrix");
00316             return 0;
00317         }
00318     }
00319     PY_CATCH;
00320 
00321     Py_Return;
00322 }
00323 
00324 PyObject* MatrixPy::inverse(PyObject * args)
00325 {
00326     if (!PyArg_ParseTuple(args, ""))
00327         return NULL;
00328 
00329     PY_TRY {
00330         if (getMatrixPtr()->determinant() > DBL_EPSILON) {
00331             Base::Matrix4D m = *getMatrixPtr();
00332             m.inverse();
00333             return new MatrixPy(m);
00334         }
00335         else {
00336             PyErr_SetString(PyExc_Exception, "Cannot invert singular matrix");
00337             return 0;
00338         }
00339     }
00340     PY_CATCH;
00341 
00342     Py_Return;
00343 }
00344 
00345 PyObject* MatrixPy::determinant(PyObject * args)
00346 {
00347     if (!PyArg_ParseTuple(args, ""))
00348         return NULL;
00349     return PyFloat_FromDouble(getMatrixPtr()->determinant());
00350 }
00351 
00352 Py::Float MatrixPy::getA11(void) const
00353 {
00354     double val = (*this->getMatrixPtr())[0][0];
00355     return Py::Float(val);
00356 }
00357 
00358 void  MatrixPy::setA11(Py::Float arg)
00359 {
00360     (*this->getMatrixPtr())[0][0] = (double)arg;
00361 }
00362 
00363 Py::Float MatrixPy::getA12(void) const
00364 {
00365     double val = (*this->getMatrixPtr())[0][1];
00366     return Py::Float(val);
00367 }
00368 
00369 void  MatrixPy::setA12(Py::Float arg)
00370 {
00371     (*this->getMatrixPtr())[0][1] = (double)arg;
00372 }
00373 
00374 Py::Float MatrixPy::getA13(void) const
00375 {
00376     double val = (*this->getMatrixPtr())[0][2];
00377     return Py::Float(val);
00378 }
00379 
00380 void  MatrixPy::setA13(Py::Float arg)
00381 {
00382     (*this->getMatrixPtr())[0][2] = (double)arg;
00383 }
00384 
00385 Py::Float MatrixPy::getA14(void) const
00386 {
00387     double val = (*this->getMatrixPtr())[0][3];
00388     return Py::Float(val);
00389 }
00390 
00391 void  MatrixPy::setA14(Py::Float arg)
00392 {
00393     (*this->getMatrixPtr())[0][3] = (double)arg;
00394 }
00395 
00396 Py::Float MatrixPy::getA21(void) const
00397 {
00398     double val = (*this->getMatrixPtr())[1][0];
00399     return Py::Float(val);
00400 }
00401 
00402 void  MatrixPy::setA21(Py::Float arg)
00403 {
00404     (*this->getMatrixPtr())[1][0] = (double)arg;
00405 }
00406 
00407 Py::Float MatrixPy::getA22(void) const
00408 {
00409     double val = (*this->getMatrixPtr())[1][1];
00410     return Py::Float(val);
00411 }
00412 
00413 void  MatrixPy::setA22(Py::Float arg)
00414 {
00415     (*this->getMatrixPtr())[1][1] = (double)arg;
00416 }
00417 
00418 Py::Float MatrixPy::getA23(void) const
00419 {
00420     double val = (*this->getMatrixPtr())[1][2];
00421     return Py::Float(val);
00422 }
00423 
00424 void  MatrixPy::setA23(Py::Float arg)
00425 {
00426     (*this->getMatrixPtr())[1][2] = (double)arg;
00427 }
00428 
00429 Py::Float MatrixPy::getA24(void) const
00430 {
00431     double val = (*this->getMatrixPtr())[1][3];
00432     return Py::Float(val);
00433 }
00434 
00435 void  MatrixPy::setA24(Py::Float arg)
00436 {
00437     (*this->getMatrixPtr())[1][3] = (double)arg;
00438 }
00439 
00440 Py::Float MatrixPy::getA31(void) const
00441 {
00442     double val = (*this->getMatrixPtr())[2][0];
00443     return Py::Float(val);
00444 }
00445 
00446 void  MatrixPy::setA31(Py::Float arg)
00447 {
00448     (*this->getMatrixPtr())[2][0] = (double)arg;
00449 }
00450 
00451 Py::Float MatrixPy::getA32(void) const
00452 {
00453     double val = (*this->getMatrixPtr())[2][1];
00454     return Py::Float(val);
00455 }
00456 
00457 void  MatrixPy::setA32(Py::Float arg)
00458 {
00459     (*this->getMatrixPtr())[2][1] = (double)arg;
00460 }
00461 
00462 Py::Float MatrixPy::getA33(void) const
00463 {
00464     double val = (*this->getMatrixPtr())[2][2];
00465     return Py::Float(val);
00466 }
00467 
00468 void  MatrixPy::setA33(Py::Float arg)
00469 {
00470     (*this->getMatrixPtr())[2][2] = (double)arg;
00471 }
00472 
00473 Py::Float MatrixPy::getA34(void) const
00474 {
00475     double val = (*this->getMatrixPtr())[2][3];
00476     return Py::Float(val);
00477 }
00478 
00479 void  MatrixPy::setA34(Py::Float arg)
00480 {
00481     (*this->getMatrixPtr())[2][3] = (double)arg;
00482 }
00483 
00484 Py::Float MatrixPy::getA41(void) const
00485 {
00486     double val = (*this->getMatrixPtr())[2][0];
00487     return Py::Float(val);
00488 }
00489 
00490 void  MatrixPy::setA41(Py::Float arg)
00491 {
00492     (*this->getMatrixPtr())[3][0] = (double)arg;
00493 }
00494 
00495 Py::Float MatrixPy::getA42(void) const
00496 {
00497     double val = (*this->getMatrixPtr())[3][1];
00498     return Py::Float(val);
00499 }
00500 
00501 void  MatrixPy::setA42(Py::Float arg)
00502 {
00503     (*this->getMatrixPtr())[3][1] = (double)arg;
00504 }
00505 
00506 Py::Float MatrixPy::getA43(void) const
00507 {
00508     double val = (*this->getMatrixPtr())[3][2];
00509     return Py::Float(val);
00510 }
00511 
00512 void  MatrixPy::setA43(Py::Float arg)
00513 {
00514     (*this->getMatrixPtr())[3][2] = (double)arg;
00515 }
00516 
00517 Py::Float MatrixPy::getA44(void) const
00518 {
00519     double val = (*this->getMatrixPtr())[3][3];
00520     return Py::Float(val);
00521 }
00522 
00523 void  MatrixPy::setA44(Py::Float arg)
00524 {
00525     (*this->getMatrixPtr())[3][3] = (double)arg;
00526 }
00527 
00528 Py::List MatrixPy::getA(void) const
00529 {
00530     return Py::List();
00531 }
00532 
00533 void MatrixPy::setA(Py::List /*arg*/)
00534 {
00535 
00536 }
00537 
00538 PyObject *MatrixPy::getCustomAttributes(const char* /*attr*/) const
00539 {
00540     return 0;
00541 }
00542 
00543 int MatrixPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00544 {
00545     return 0; 
00546 }

Generated on Wed Nov 23 19:00:21 2011 for FreeCAD by  doxygen 1.6.1