VectorPyImp.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 #ifndef _PreComp_
00027 # include <sstream>
00028 #endif
00029 
00030 #include "Vector3D.h"
00031 
00032 // inclusion of the generated files (generated out of VectorPy.xml)
00033 #include "GeometryPyCXX.h"
00034 #include "VectorPy.h"
00035 #include "VectorPy.cpp"
00036 
00037 using namespace Base;
00038 
00039 // returns a string which represent the object e.g. when printed in python
00040 std::string VectorPy::representation(void) const
00041 {
00042     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00043     std::stringstream str;
00044     str << "Vector (";
00045     str << ptr->x << ", "<< ptr->y << ", "<< ptr->z;
00046     str << ")";
00047 
00048     return str.str();
00049 }
00050 
00051 PyObject *VectorPy::PyMake(struct _typeobject *, PyObject *, PyObject *)  // Python wrapper
00052 {
00053     // create a new instance of VectorPy and the Twin object 
00054     return new VectorPy(new Vector3d);
00055 }
00056 
00057 // constructor method
00058 int VectorPy::PyInit(PyObject* args, PyObject* /*kwd*/)
00059 {
00060     double  x=0.0,y=0.0,z=0.0;
00061     PyObject *object;
00062     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00063     if (PyArg_ParseTuple(args, "|ddd", &x,&y,&z)) {
00064         ptr->Set(x,y,z);
00065         return 0;
00066     }
00067     PyErr_Clear(); // set by PyArg_ParseTuple()
00068     if (PyArg_ParseTuple(args,"O!",&(Base::VectorPy::Type), &object)) {
00069         // Note: must be static_cast, not reinterpret_cast
00070         *ptr = *(static_cast<Base::VectorPy*>(object)->getVectorPtr());
00071         return 0;
00072     }
00073     PyErr_Clear(); // set by PyArg_ParseTuple()
00074     if (PyArg_ParseTuple(args,"O!",&(PyTuple_Type), &object)) {
00075         try {
00076             *ptr = getVectorFromTuple<double>(object);
00077             return 0;
00078         }
00079         catch (const Py::Exception&) {
00080             return -1;
00081         }
00082     }
00083 
00084     PyErr_SetString(PyExc_TypeError, "Either three floats, tuple or Vector expected");
00085     return -1;
00086 }
00087 
00088 PyObject* VectorPy::number_add_handler(PyObject *self, PyObject *other)
00089 {
00090     if (!PyObject_TypeCheck(self, &(VectorPy::Type))) {
00091         PyErr_SetString(PyExc_TypeError, "First arg must be Vector");
00092         return 0;
00093     }
00094     if (!PyObject_TypeCheck(other, &(VectorPy::Type))) {
00095         PyErr_SetString(PyExc_TypeError, "Second arg must be Vector");
00096         return 0;
00097     }
00098     Base::Vector3d a = static_cast<VectorPy*>(self)->value();
00099     Base::Vector3d b = static_cast<VectorPy*>(other)->value();
00100     return new VectorPy(a+b);
00101 }
00102 
00103 PyObject* VectorPy::number_subtract_handler(PyObject *self, PyObject *other)
00104 {
00105     if (!PyObject_TypeCheck(self, &(VectorPy::Type))) {
00106         PyErr_SetString(PyExc_TypeError, "First arg must be Vector");
00107         return 0;
00108     }
00109     if (!PyObject_TypeCheck(other, &(VectorPy::Type))) {
00110         PyErr_SetString(PyExc_TypeError, "Second arg must be Vector");
00111         return 0;
00112     }
00113     Base::Vector3d a = static_cast<VectorPy*>(self)->value();
00114     Base::Vector3d b = static_cast<VectorPy*>(other)->value();
00115     return new VectorPy(a-b);
00116 }
00117 
00118 PyObject* VectorPy::number_multiply_handler(PyObject *self, PyObject *other)
00119 {
00120     if (!PyObject_TypeCheck(self, &(VectorPy::Type))) {
00121         PyErr_SetString(PyExc_TypeError, "First arg must be Vector");
00122         return 0;
00123     }
00124 
00125     if (PyObject_TypeCheck(other, &(VectorPy::Type))) {
00126         Base::Vector3d a = static_cast<VectorPy*>(self) ->value();
00127         Base::Vector3d b = static_cast<VectorPy*>(other)->value();
00128         Py::Float mult(a * b);
00129         return Py::new_reference_to(mult);
00130     }
00131     else if (PyFloat_Check(other)) {
00132         Base::Vector3d a = static_cast<VectorPy*>(self) ->value();
00133         double b = PyFloat_AsDouble(other);
00134         return new VectorPy(a * b);
00135     }
00136     else {
00137         PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number");
00138         return 0;
00139     }
00140 }
00141 
00142 Py_ssize_t VectorPy::sequence_length(PyObject *)
00143 {
00144     return 3;
00145 }
00146 
00147 PyObject * VectorPy::sequence_item (PyObject *self, Py_ssize_t index)
00148 {
00149     if (!PyObject_TypeCheck(self, &(VectorPy::Type))) {
00150         PyErr_SetString(PyExc_TypeError, "first arg must be Vector");
00151         return 0;
00152     }
00153     if (index < 0 || index > 2) {
00154         PyErr_SetString(PyExc_IndexError, "index out of range");
00155         return 0;
00156     }
00157 
00158     Base::Vector3d a = static_cast<VectorPy*>(self)->value();
00159     return Py_BuildValue("d", a[index]);
00160 }
00161 
00162 int VectorPy::sequence_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
00163 {
00164     if (!PyObject_TypeCheck(self, &(VectorPy::Type))) {
00165         PyErr_SetString(PyExc_TypeError, "first arg must be Vector");
00166         return -1;
00167     }
00168     if (index < 0 || index > 2) {
00169         PyErr_SetString(PyExc_IndexError, "index out of range");
00170         return -1;
00171     }
00172 
00173     if (PyFloat_Check(value)) {
00174         VectorPy::PointerType ptr = static_cast<VectorPy*>(self)->getVectorPtr();
00175         (*ptr)[index] = PyFloat_AsDouble(value);
00176     }
00177     else {
00178         PyErr_SetString(PyExc_ValueError, "value must be float");
00179         return -1;
00180     }
00181 
00182     return 0;
00183 }
00184 
00185 PyObject*  VectorPy::add(PyObject *args)
00186 {
00187     PyObject *obj;
00188     if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj))
00189         return 0;
00190 
00191     VectorPy* vec = static_cast<VectorPy*>(obj);
00192 
00193     VectorPy::PointerType this_ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00194     VectorPy::PointerType vect_ptr = reinterpret_cast<VectorPy::PointerType>(vec->_pcTwinPointer);
00195 
00196     Base::Vector3d v = (*this_ptr) + (*vect_ptr);
00197     return new VectorPy(v);
00198 }
00199 
00200 PyObject*  VectorPy::sub(PyObject *args)
00201 {
00202     PyObject *obj;
00203     if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj))
00204         return 0;
00205 
00206     VectorPy* vec = static_cast<VectorPy*>(obj);
00207 
00208     VectorPy::PointerType this_ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00209     VectorPy::PointerType vect_ptr = reinterpret_cast<VectorPy::PointerType>(vec->_pcTwinPointer);
00210 
00211     Base::Vector3d v = (*this_ptr) - (*vect_ptr);
00212     return new VectorPy(v);
00213 }
00214 
00215 PyObject* VectorPy::richCompare(PyObject *v, PyObject *w, int op)
00216 {
00217     if (PyObject_TypeCheck(v, &(VectorPy::Type)) &&
00218         PyObject_TypeCheck(w, &(VectorPy::Type))) {
00219         Vector3d v1 = static_cast<VectorPy*>(v)->value();
00220         Vector3d v2 = static_cast<VectorPy*>(w)->value();
00221 
00222         PyObject *res=0;
00223         if (op != Py_EQ && op != Py_NE) {
00224             PyErr_SetString(PyExc_TypeError,
00225             "no ordering relation is defined for Vector");
00226             return 0;
00227         }
00228         else if (op == Py_EQ) {
00229             res = (v1 == v2) ? Py_True : Py_False;
00230             Py_INCREF(res);
00231             return res;
00232         }
00233         else {
00234             res = (v1 != v2) ? Py_True : Py_False;
00235             Py_INCREF(res);
00236             return res;
00237         }
00238     }
00239     else {
00240         Py_INCREF(Py_NotImplemented);
00241         return Py_NotImplemented;
00242     }
00243 }
00244 
00245 PyObject*  VectorPy::scale(PyObject *args)
00246 {
00247     double factorX, factorY, factorZ;
00248     if (!PyArg_ParseTuple(args, "ddd", &factorX, &factorY, &factorZ))
00249         return 0;
00250     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00251     ptr->Scale(factorX, factorY, factorZ);
00252 
00253     return Py::new_reference_to(this);
00254 }
00255 
00256 PyObject*  VectorPy::multiply(PyObject *args)
00257 {
00258     double factor;
00259     if (!PyArg_ParseTuple(args, "d", &factor))
00260         return 0;
00261     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00262     ptr->Scale(factor, factor, factor);
00263 
00264     return Py::new_reference_to(this);
00265 }
00266 
00267 PyObject*  VectorPy::dot(PyObject *args)
00268 {
00269     PyObject *obj;
00270     if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj))
00271         return 0;
00272 
00273     VectorPy* vec = static_cast<VectorPy*>(obj);
00274 
00275     VectorPy::PointerType this_ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00276     VectorPy::PointerType vect_ptr = reinterpret_cast<VectorPy::PointerType>(vec->_pcTwinPointer);
00277 
00278     Py::Float mult((*this_ptr) * (*vect_ptr));
00279     return Py::new_reference_to(mult);
00280 }
00281 
00282 PyObject*  VectorPy::cross(PyObject *args)
00283 {
00284     PyObject *obj;
00285     if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj))
00286         return 0;
00287 
00288     VectorPy* vec = static_cast<VectorPy*>(obj);
00289 
00290     VectorPy::PointerType this_ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00291     VectorPy::PointerType vect_ptr = reinterpret_cast<VectorPy::PointerType>(vec->_pcTwinPointer);
00292 
00293     Base::Vector3d v = (*this_ptr) % (*vect_ptr);
00294     return new VectorPy(v);
00295 }
00296 
00297 PyObject*  VectorPy::getAngle(PyObject *args)
00298 {
00299     PyObject *obj;
00300     if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj))
00301         return 0;
00302 
00303     VectorPy* vec = static_cast<VectorPy*>(obj);
00304 
00305     VectorPy::PointerType this_ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00306     VectorPy::PointerType vect_ptr = reinterpret_cast<VectorPy::PointerType>(vec->_pcTwinPointer);
00307 
00308     Py::Float angle(this_ptr->GetAngle(*vect_ptr));
00309     return Py::new_reference_to(angle);
00310 }
00311 
00312 PyObject*  VectorPy::normalize(PyObject *args)
00313 {
00314     if (!PyArg_ParseTuple(args, ""))
00315         return 0;
00316     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00317     if (ptr->Length() < 1.0e-6) {
00318         PyErr_SetString(PyExc_Exception, "Cannot normalize null vector");
00319         return 0;
00320     }
00321 
00322     ptr->Normalize();
00323 
00324     return Py::new_reference_to(this);
00325 }
00326 
00327 PyObject*  VectorPy::projectToLine(PyObject *args)
00328 {
00329     PyObject *base, *line;
00330     if (!PyArg_ParseTuple(args, "OO",&base, &line))
00331         return 0;
00332     if (!PyObject_TypeCheck(base, &(VectorPy::Type))) {
00333         PyErr_SetString(PyExc_TypeError, "First arg must be Vector");
00334         return 0;
00335     }
00336     if (!PyObject_TypeCheck(line, &(VectorPy::Type))) {
00337         PyErr_SetString(PyExc_TypeError, "Second arg must be Vector");
00338         return 0;
00339     }
00340 
00341     VectorPy* base_vec = static_cast<VectorPy*>(base);
00342     VectorPy* line_vec = static_cast<VectorPy*>(line);
00343 
00344     VectorPy::PointerType this_ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00345     VectorPy::PointerType base_ptr = reinterpret_cast<VectorPy::PointerType>(base_vec->_pcTwinPointer);
00346     VectorPy::PointerType line_ptr = reinterpret_cast<VectorPy::PointerType>(line_vec->_pcTwinPointer);
00347 
00348     this_ptr->ProjToLine(*base_ptr, *line_ptr);
00349 
00350     return Py::new_reference_to(this);
00351 }
00352 
00353 PyObject*  VectorPy::projectToPlane(PyObject *args)
00354 {
00355     PyObject *base, *line;
00356     if (!PyArg_ParseTuple(args, "OO",&base, &line))
00357         return 0;
00358     if (!PyObject_TypeCheck(base, &(VectorPy::Type))) {
00359         PyErr_SetString(PyExc_TypeError, "First arg must be Vector");
00360         return 0;
00361     }
00362     if (!PyObject_TypeCheck(line, &(VectorPy::Type))) {
00363         PyErr_SetString(PyExc_TypeError, "Second arg must be Vector");
00364         return 0;
00365     }
00366 
00367     VectorPy* base_vec = static_cast<VectorPy*>(base);
00368     VectorPy* line_vec = static_cast<VectorPy*>(line);
00369 
00370     VectorPy::PointerType this_ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00371     VectorPy::PointerType base_ptr = reinterpret_cast<VectorPy::PointerType>(base_vec->_pcTwinPointer);
00372     VectorPy::PointerType line_ptr = reinterpret_cast<VectorPy::PointerType>(line_vec->_pcTwinPointer);
00373 
00374     this_ptr->ProjToPlane(*base_ptr, *line_ptr);
00375 
00376     return Py::new_reference_to(this);
00377 }
00378 
00379 PyObject*  VectorPy::distanceToLine(PyObject *args)
00380 {
00381     PyObject *base, *line;
00382     if (!PyArg_ParseTuple(args, "OO",&base, &line))
00383         return 0;
00384     if (!PyObject_TypeCheck(base, &(VectorPy::Type))) {
00385         PyErr_SetString(PyExc_TypeError, "First arg must be Vector");
00386         return 0;
00387     }
00388     if (!PyObject_TypeCheck(line, &(VectorPy::Type))) {
00389         PyErr_SetString(PyExc_TypeError, "Second arg must be Vector");
00390         return 0;
00391     }
00392 
00393     VectorPy* base_vec = static_cast<VectorPy*>(base);
00394     VectorPy* line_vec = static_cast<VectorPy*>(line);
00395 
00396     VectorPy::PointerType this_ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00397     VectorPy::PointerType base_ptr = reinterpret_cast<VectorPy::PointerType>(base_vec->_pcTwinPointer);
00398     VectorPy::PointerType line_ptr = reinterpret_cast<VectorPy::PointerType>(line_vec->_pcTwinPointer);
00399 
00400     Py::Float dist(this_ptr->DistanceToLine(*base_ptr, *line_ptr));
00401     return Py::new_reference_to(dist);
00402 }
00403 
00404 PyObject*  VectorPy::distanceToPlane(PyObject *args)
00405 {
00406     PyObject *base, *line;
00407     if (!PyArg_ParseTuple(args, "OO",&base, &line))
00408         return 0;
00409     if (!PyObject_TypeCheck(base, &(VectorPy::Type))) {
00410         PyErr_SetString(PyExc_TypeError, "First arg must be Vector");
00411         return 0;
00412     }
00413     if (!PyObject_TypeCheck(line, &(VectorPy::Type))) {
00414         PyErr_SetString(PyExc_TypeError, "Second arg must be Vector");
00415         return 0;
00416     }
00417 
00418     VectorPy* base_vec = static_cast<VectorPy*>(base);
00419     VectorPy* line_vec = static_cast<VectorPy*>(line);
00420 
00421     VectorPy::PointerType this_ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00422     VectorPy::PointerType base_ptr = reinterpret_cast<VectorPy::PointerType>(base_vec->_pcTwinPointer);
00423     VectorPy::PointerType line_ptr = reinterpret_cast<VectorPy::PointerType>(line_vec->_pcTwinPointer);
00424 
00425     Py::Float dist(this_ptr->DistanceToPlane(*base_ptr, *line_ptr));
00426     return Py::new_reference_to(dist);
00427 }
00428 
00429 Py::Float VectorPy::getLength(void) const
00430 {
00431     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00432     return Py::Float(ptr->Length());
00433 }
00434 
00435 void  VectorPy::setLength(Py::Float arg)
00436 {
00437     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00438     double len = ptr->Length();
00439     if (len < 1.0e-6) {
00440         throw Py::Exception(std::string("Cannot set length of null vector"));
00441     }
00442 
00443     double val = (double)arg/len;
00444     ptr->x *= val;
00445     ptr->y *= val;
00446     ptr->z *= val;
00447 }
00448 
00449 Py::Float VectorPy::getx(void) const
00450 {
00451     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00452     return Py::Float(ptr->x);
00453 }
00454 
00455 void  VectorPy::setx(Py::Float arg)
00456 {
00457     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00458     ptr->x = (double)arg;
00459 }
00460 
00461 Py::Float VectorPy::gety(void) const
00462 {
00463     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00464     return Py::Float(ptr->y);
00465 }
00466 
00467 void  VectorPy::sety(Py::Float arg)
00468 {
00469     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00470     ptr->y = (double)arg;
00471 }
00472 
00473 Py::Float VectorPy::getz(void) const
00474 {
00475     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00476     return Py::Float(ptr->z);
00477 }
00478 
00479 void  VectorPy::setz(Py::Float arg)
00480 {
00481     VectorPy::PointerType ptr = reinterpret_cast<VectorPy::PointerType>(_pcTwinPointer);
00482     ptr->z = (double)arg;
00483 }
00484 
00485 PyObject *VectorPy::getCustomAttributes(const char* /*attr*/) const
00486 {
00487     return 0;
00488 }
00489 
00490 int VectorPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
00491 {
00492     return 0; 
00493 }
00494 
00495 

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