GeometryPyCXX.h

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 #ifndef PY_GEOMETRYPY_H
00025 #define PY_GEOMETRYPY_H
00026 
00027 #include <CXX/Objects.hxx>
00028 #include <Base/Vector3D.h>
00029 #include <Base/Matrix.h>
00030 #include <Base/MatrixPy.h>
00031 #include <Base/Rotation.h>
00032 #include <Base/RotationPy.h>
00033 #include <Base/Placement.h>
00034 #include <Base/PlacementPy.h>
00035 #include <Base/BoundBoxPy.h>
00036 
00037 namespace Base {
00038 template <typename T>
00039 inline Vector3<T> getVectorFromTuple(PyObject* o)
00040 {
00041     Py::Sequence tuple(o);
00042     T x = (T)Py::Float(tuple.getItem(0));
00043     T y = (T)Py::Float(tuple.getItem(1));
00044     T z = (T)Py::Float(tuple.getItem(2));
00045     return Vector3<T>(x,y,z);
00046 }
00047 }
00048 
00049 namespace Py {
00050 
00051 // Implementing the vector class in the fashion of the PyCXX library.
00052 class BaseExport Vector : public Object
00053 {
00054 public:
00055     explicit Vector (PyObject *pyob, bool owned): Object(pyob, owned) {
00056         validate();
00057     }
00058 
00059     Vector (const Vector& ob): Object(*ob) {
00060         validate();
00061     }
00062 
00063     explicit Vector (const Base::Vector3d&);
00064     explicit Vector (const Base::Vector3f&);
00065     virtual bool accepts (PyObject *pyob) const;
00066 
00067     Vector(const Object& other): Object(other.ptr()) {
00068         validate();
00069     }
00070     Vector& operator= (const Object& rhs)
00071     {
00072         return (*this = *rhs);
00073     }
00074 
00075     Vector& operator= (PyObject* rhsp);
00076     Vector& operator= (const Base::Vector3d&);
00077     Vector& operator= (const Base::Vector3f&);
00078 
00079     Base::Vector3d toVector() const;
00080 
00081 private:
00082     static int Vector_TypeCheck(PyObject *);
00083 };
00084 
00092 // The first template parameter represents the basic geometric class e.g. Rotation,
00093 // the second parameter is reserved for its Python binding class, i.e. RotationPy.
00094 // The third template parameter is the definition of a pointer to the method
00095 // of the Python binding class to return the managed geometric instance. In our
00096 // example this is the method RotationPy::getRotationPtr.
00097 template <class T, class PyT, T* (PyT::*valuePtr)() const>
00098 class GeometryT : public Object
00099 {
00100 public:
00101     explicit GeometryT (PyObject *pyob, bool owned): Object(pyob, owned) {
00102         validate();
00103     }
00104     GeometryT (const GeometryT& ob): Object(*ob) {
00105         validate();
00106     }
00107     explicit GeometryT ()
00108     {
00109         set(new PyT(new T()), true);
00110         validate();
00111     }
00112     explicit GeometryT (const T& v)
00113     {
00114         set(new PyT(new T(v)), true);
00115         validate();
00116     }
00117     GeometryT(const Object& other): Object(other.ptr()) {
00118         validate();
00119     }
00120     virtual bool accepts (PyObject *pyob) const {
00121         return pyob && Geometry_TypeCheck (pyob);
00122     }
00123     GeometryT& operator= (const Object& rhs)
00124     {
00125         return (*this = *rhs);
00126     }
00127     GeometryT& operator= (PyObject* rhsp)
00128     {
00129         if(ptr() == rhsp) return *this;
00130         set (rhsp, false);
00131         return *this;
00132     }
00133     GeometryT& operator= (const T& v)
00134     {
00135         set (new PyT(v), true);
00136         return *this;
00137     }
00138     const T& getValue() const
00139     {
00140         // cast the PyObject pointer to the matching sub-class
00141         // and call then the defined member function
00142         PyT* py = static_cast<PyT*>(ptr());
00143         T* v = (py->*valuePtr)();
00144         return *v;
00145     }
00146     operator T() const
00147     {
00148         // cast the PyObject pointer to the matching sub-class
00149         // and call then the defined member function
00150         PyT* py = static_cast<PyT*>(ptr());
00151         T* v = (py->*valuePtr)();
00152         return *v;
00153     }
00154 
00155 private:
00156     static int Geometry_TypeCheck(PyObject * obj)
00157     {
00158         return PyObject_TypeCheck(obj, &(PyT::Type));
00159     }
00160 };
00161 
00162 // PyCXX wrapper classes Py::Matrix, Py::Rotation, Py::Placement, ...
00163 typedef GeometryT<Base::BoundBox3d, Base::BoundBoxPy,
00164                  &Base::BoundBoxPy::getBoundBoxPtr>     BoundingBox;
00165 typedef GeometryT<Base::Matrix4D, Base::MatrixPy,
00166                  &Base::MatrixPy::getMatrixPtr>         Matrix;
00167 typedef GeometryT<Base::Rotation, Base::RotationPy,
00168                  &Base::RotationPy::getRotationPtr>     Rotation;
00169 typedef GeometryT<Base::Placement, Base::PlacementPy,
00170                  &Base::PlacementPy::getPlacementPtr>   Placement;
00171 
00172 }
00173 
00174 #endif // PY_GEOMETRYPY_H

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