GeometryPyCXX.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
00093
00094
00095
00096
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
00141
00142 PyT* py = static_cast<PyT*>(ptr());
00143 T* v = (py->*valuePtr)();
00144 return *v;
00145 }
00146 operator T() const
00147 {
00148
00149
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
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