RotationPyImp.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 <Base/Rotation.h>
00027 #include <Base/Tools.h>
00028 #include <Base/GeometryPyCXX.h>
00029 
00030 // inclusion of the generated files (generated out of RotationPy.xml)
00031 #include "VectorPy.h"
00032 #include "RotationPy.h"
00033 #include "RotationPy.cpp"
00034 
00035 using namespace Base;
00036 
00037 // returns a string which represents the object e.g. when printed in python
00038 std::string RotationPy::representation(void) const
00039 {
00040     RotationPy::PointerType ptr = reinterpret_cast<RotationPy::PointerType>(_pcTwinPointer);
00041     std::stringstream str;
00042     str << "Quaternion (";
00043     str << ptr->getValue()[0] << ","<< ptr->getValue()[1] << "," << ptr->getValue()[2] << "," << ptr->getValue()[3];
00044     str << ")";
00045 
00046     return str.str();
00047 }
00048 
00049 PyObject *RotationPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00050 {
00051     // create a new instance of RotationPy and the Twin object 
00052     return new RotationPy(new Rotation);
00053 }
00054 
00055 // constructor method
00056 int RotationPy::PyInit(PyObject* args, PyObject* /*kwd*/)
00057 {
00058     PyObject* o;
00059     if (PyArg_ParseTuple(args, "")) {
00060         return 0;
00061     }
00062 
00063     PyErr_Clear();
00064     if (PyArg_ParseTuple(args, "O!", &(Base::RotationPy::Type), &o)) {
00065         Base::Rotation *rot = static_cast<Base::RotationPy*>(o)->getRotationPtr();
00066         getRotationPtr()->setValue(rot->getValue());
00067         return 0;
00068     }
00069 
00070     PyErr_Clear();
00071     double angle;
00072     if (PyArg_ParseTuple(args, "O!d", &(Base::VectorPy::Type), &o, &angle)) {
00073         // NOTE: The last parameter defines the rotation angle in degree.
00074         getRotationPtr()->setValue(static_cast<Base::VectorPy*>(o)->value(), Base::toRadians<double>(angle));
00075         return 0;
00076     }
00077 
00078     PyErr_Clear();
00079     double q0, q1, q2, q3;
00080     if (PyArg_ParseTuple(args, "dddd", &q0, &q1, &q2, &q3)) {
00081         getRotationPtr()->setValue(q0, q1, q2, q3);
00082         return 0;
00083     }
00084 
00085     PyErr_Clear();
00086     double y, p, r;
00087     if (PyArg_ParseTuple(args, "ddd", &y, &p, &r)) {
00088         getRotationPtr()->setYawPitchRoll(y, p, r);
00089         return 0;
00090     }
00091 
00092     PyErr_Clear();
00093     PyObject *v1, *v2;
00094     if (PyArg_ParseTuple(args, "O!O!", &(Base::VectorPy::Type), &v1,
00095                                        &(Base::VectorPy::Type), &v2)) {
00096         Py::Vector from(v1, false);
00097         Py::Vector to(v2, false);
00098         getRotationPtr()->setValue(from.toVector(), to.toVector());
00099         return 0;
00100     }
00101 
00102     PyErr_SetString(PyExc_Exception, "empty parameter list, four floats or Vector and float");
00103     return -1;
00104 }
00105 
00106 PyObject* RotationPy::invert(PyObject * args)
00107 {
00108     if (!PyArg_ParseTuple(args, ""))
00109         return 0;
00110     this->getRotationPtr()->invert();
00111     Py_Return;
00112 }
00113 
00114 PyObject* RotationPy::multiply(PyObject * args)
00115 {
00116     PyObject *rot;
00117     if (!PyArg_ParseTuple(args, "O!", &(RotationPy::Type), &rot))
00118         return NULL;
00119     Rotation mult = (*getRotationPtr()) * (*static_cast<RotationPy*>(rot)->getRotationPtr());
00120     return new RotationPy(new Rotation(mult));
00121 }
00122 
00123 PyObject* RotationPy::multVec(PyObject * args)
00124 {
00125     PyObject *obj;
00126     if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj))
00127         return NULL;
00128     Base::Vector3d vec(static_cast<VectorPy*>(obj)->value());
00129     getRotationPtr()->multVec(vec, vec);
00130     return new VectorPy(new Vector3d(vec));
00131 }
00132 
00133 PyObject* RotationPy::toEuler(PyObject * args)
00134 {
00135     if (!PyArg_ParseTuple(args, ""))
00136         return NULL;
00137     double A,B,C;
00138     this->getRotationPtr()->getYawPitchRoll(A,B,C);
00139 
00140     Py::Tuple tuple(3);
00141     tuple.setItem(0, Py::Float(A));
00142     tuple.setItem(1, Py::Float(B));
00143     tuple.setItem(2, Py::Float(C));
00144     return Py::new_reference_to(tuple);
00145 }
00146 
00147 Py::Tuple RotationPy::getQ(void) const
00148 {
00149     double q0, q1, q2, q3;
00150     this->getRotationPtr()->getValue(q0,q1,q2,q3);
00151 
00152     Py::Tuple tuple(4);
00153     tuple.setItem(0, Py::Float(q0));
00154     tuple.setItem(1, Py::Float(q1));
00155     tuple.setItem(2, Py::Float(q2));
00156     tuple.setItem(3, Py::Float(q3));
00157     return tuple;
00158 }
00159 
00160 void RotationPy::setQ(Py::Tuple arg)
00161 {
00162     double q0 = (double)Py::Float(arg.getItem(0));
00163     double q1 = (double)Py::Float(arg.getItem(1));
00164     double q2 = (double)Py::Float(arg.getItem(2));
00165     double q3 = (double)Py::Float(arg.getItem(3));
00166     this->getRotationPtr()->setValue(q0,q1,q2,q3);
00167 }
00168 
00169 Py::Object RotationPy::getAxis(void) const
00170 {
00171     Base::Vector3d axis; double angle;
00172     this->getRotationPtr()->getValue(axis, angle);
00173     return Py::Vector(axis);
00174 }
00175 
00176 Py::Float RotationPy::getAngle(void) const
00177 {
00178     Base::Vector3d axis; double angle;
00179     this->getRotationPtr()->getValue(axis, angle);
00180     return Py::Float(angle);
00181 }
00182 
00183 PyObject *RotationPy::getCustomAttributes(const char* /*attr*/) const
00184 {
00185     return 0;
00186 }
00187 
00188 int RotationPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00189 {
00190     return 0; 
00191 }
00192 
00193 

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