| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#ifndef PY_GEOMETRYPY_H |
| 27 |
#define PY_GEOMETRYPY_H |
| 28 |
|
| 29 |
#include <CXX/Extensions.hxx> |
| 30 |
#include <FCGlobal.h> |
| 31 |
|
| 32 |
#include <Base/BoundBoxPy.h> |
| 33 |
#include <Base/MatrixPy.h> |
| 34 |
#include <Base/RotationPy.h> |
| 35 |
#include <Base/PlacementPy.h> |
| 36 |
|
| 37 |
|
| 38 |
namespace Base |
| 39 |
{ |
| 40 |
class Matrix4D; |
| 41 |
class Rotation; |
| 42 |
class Placement; |
| 43 |
template<typename T> |
| 44 |
class Vector3; |
| 45 |
using Vector3d = Vector3<double>; |
| 46 |
using Vector3f = Vector3<float>; |
| 47 |
|
| 48 |
template<typename T> |
| 49 |
inline Vector3<T> getVectorFromTuple(PyObject* py) |
| 50 |
{ |
| 51 |
Py::Sequence tuple(py); |
| 52 |
if (tuple.size() != 3) { |
| 53 |
throw Py::ValueError("Expected sequence of size 3"); |
| 54 |
} |
| 55 |
|
| 56 |
T vx = static_cast<T>(Py::Float(tuple[0])); |
| 57 |
T vy = static_cast<T>(Py::Float(tuple[1])); |
| 58 |
T vz = static_cast<T>(Py::Float(tuple[2])); |
| 59 |
|
| 60 |
return Vector3<T>(vx, vy, vz); |
| 61 |
} |
| 62 |
|
| 63 |
class BaseExport Vector2dPy: public Py::PythonClass<Vector2dPy> // NOLINT |
| 64 |
{ |
| 65 |
public: |
| 66 |
static Py::PythonType& behaviors(); |
| 67 |
static PyTypeObject* type_object(); |
| 68 |
static bool check(PyObject* py); |
| 69 |
|
| 70 |
static Py::PythonClassObject<Vector2dPy> create(const Vector2d&); |
| 71 |
static Py::PythonClassObject<Vector2dPy> create(double vx, double vy); |
| 72 |
Vector2dPy(Py::PythonClassInstance* self, Py::Tuple& args, Py::Dict& kwds); |
| 73 |
~Vector2dPy() override; |
| 74 |
|
| 75 |
static void init_type(); |
| 76 |
Py::Object getattro(const Py::String& name_) override; |
| 77 |
int setattro(const Py::String& name_, const Py::Object& value) override; |
| 78 |
Py::Object repr() override; |
| 79 |
inline const Vector2d& value() const |
| 80 |
{ |
| 81 |
return v; |
| 82 |
} |
| 83 |
inline void setValue(const Vector2d& n) |
| 84 |
{ |
| 85 |
v = n; |
| 86 |
} |
| 87 |
inline void setValue(double vx, double vy) |
| 88 |
{ |
| 89 |
v.x = vx; |
| 90 |
v.y = vy; |
| 91 |
} |
| 92 |
|
| 93 |
/** @name methods for group handling */ |
| 94 |
//@{ |
| 95 |
Py::Object number_negative() override; |
| 96 |
Py::Object number_positive() override; |
| 97 |
Py::Object number_absolute() override; |
| 98 |
Py::Object number_invert() override; |
| 99 |
Py::Object number_int() override; |
| 100 |
Py::Object number_float() override; |
| 101 |
Py::Object number_add(const Py::Object&) override; |
| 102 |
Py::Object number_subtract(const Py::Object&) override; |
| 103 |
Py::Object number_multiply(const Py::Object&) override; |
| 104 |
Py::Object number_remainder(const Py::Object&) override; |
| 105 |
Py::Object number_divmod(const Py::Object&) override; |
| 106 |
Py::Object number_lshift(const Py::Object&) override; |
| 107 |
Py::Object number_rshift(const Py::Object&) override; |
| 108 |
Py::Object number_and(const Py::Object&) override; |
| 109 |
Py::Object number_xor(const Py::Object&) override; |
| 110 |
Py::Object number_or(const Py::Object&) override; |
| 111 |
Py::Object number_power(const Py::Object&, const Py::Object&) override; |
| 112 |
//@} |
| 113 |
|
| 114 |
Py::Object isNull(const Py::Tuple&); |
| 115 |
Py::Object length(const Py::Tuple&); |
| 116 |
Py::Object atan2(const Py::Tuple&); |
| 117 |
Py::Object square(const Py::Tuple&); |
| 118 |
Py::Object scale(const Py::Tuple&); |
| 119 |
Py::Object rotate(const Py::Tuple&); |
| 120 |
Py::Object normalize(const Py::Tuple&); |
| 121 |
Py::Object perpendicular(const Py::Tuple&); |
| 122 |
Py::Object distance(const Py::Tuple&); |
| 123 |
Py::Object isEqual(const Py::Tuple&); |
| 124 |
Py::Object getAngle(const Py::Tuple&); |
| 125 |
Py::Object projectToLine(const Py::Tuple&); |
| 126 |
|
| 127 |
private: |
| 128 |
Vector2d v; |
| 129 |
}; |
| 130 |
|
| 131 |
} // namespace Base |
| 132 |
|
| 133 |
namespace Py |
| 134 |
{ |
| 135 |
|
| 136 |
using Vector2d = PythonClassObject<Base::Vector2dPy>; |
| 137 |
|
| 138 |
inline Base::Vector2d toVector2d(PyObject* py) |
| 139 |
{ |
| 140 |
Base::Vector2dPy* py2d = Py::Vector2d(py).getCxxObject(); |
| 141 |
return py2d ? py2d->value() : Base::Vector2d(); |
| 142 |
} |
| 143 |
|
| 144 |
inline Base::Vector2d toVector2d(const Object& py) |
| 145 |
{ |
| 146 |
Base::Vector2dPy* py2d = Py::Vector2d(py).getCxxObject(); |
| 147 |
return py2d ? py2d->value() : Base::Vector2d(); |
| 148 |
} |
| 149 |
|
| 150 |
// Implementing the vector class in the fashion of the PyCXX library. |
| 151 |
class BaseExport Vector: public Object // NOLINT |
| 152 |
{ |
| 153 |
public: |
| 154 |
explicit Vector(PyObject* pyob, bool owned) |
| 155 |
: Object(pyob, owned) |
| 156 |
{ |
| 157 |
validate(); |
| 158 |
} |
| 159 |
|
| 160 |
Vector(const Vector& ob) |
| 161 |
: Object(ob) |
| 162 |
{ |
| 163 |
validate(); |
| 164 |
} |
| 165 |
|
| 166 |
explicit Vector(const Base::Vector3d&); |
| 167 |
explicit Vector(const Base::Vector3f&); |
| 168 |
bool accepts(PyObject* obj) const override; |
| 169 |
|
| 170 |
Vector(const Object& other) |
| 171 |
: Object(other.ptr()) |
| 172 |
{ |
| 173 |
validate(); |
| 174 |
} |
| 175 |
Vector& operator=(const Object& rhs) |
| 176 |
{ |
| 177 |
return (*this = *rhs); |
| 178 |
} |
| 179 |
Vector& operator=(const Vector& rhs) |
| 180 |
{ |
| 181 |
return (*this = *rhs); |
| 182 |
} |
| 183 |
|
| 184 |
Vector& operator=(PyObject* rhsp); |
| 185 |
Vector& operator=(const Base::Vector3d&); |
| 186 |
Vector& operator=(const Base::Vector3f&); |
| 187 |
operator Base::Vector3d() const |
| 188 |
{ |
| 189 |
return toVector(); |
| 190 |
} |
| 191 |
|
| 192 |
Base::Vector3d toVector() const; |
| 193 |
|
| 194 |
private: |
| 195 |
static int Vector_TypeCheck(PyObject*); |
| 196 |
}; |
| 197 |
|
| 198 |
/** |
| 199 |
* This is a template class to provide wrapper classes for geometric classes like |
| 200 |
* Base::Matrix4D, Base::Rotation and their Python binding classes. |
| 201 |
* Since the class inherits from Py::Object it can be used in the same fashion as |
| 202 |
* Py::String, Py::List, etc. to simplify the usage with them. |
| 203 |
* @author Werner Mayer |
| 204 |
*/ |
| 205 |
// The first template parameter represents the basic geometric class e.g. Rotation, |
| 206 |
// the second parameter is reserved for its Python binding class, i.e. RotationPy. |
| 207 |
// The third template parameter is the definition of a pointer to the method |
| 208 |
// of the Python binding class to return the managed geometric instance. In our |
| 209 |
// example this is the method RotationPy::getRotationPtr. |
| 210 |
template<class T, class PyT, T* (PyT::*valuePtr)() const> |
| 211 |
class GeometryT: public Object // NOLINT |
| 212 |
{ |
| 213 |
public: |
| 214 |
explicit GeometryT(PyObject* pyob, bool owned) |
| 215 |
: Object(pyob, owned) |
| 216 |
{ |
| 217 |
validate(); |
| 218 |
} |
| 219 |
GeometryT(const GeometryT& ob) |
| 220 |
: Object(*ob) |
| 221 |
{ |
| 222 |
validate(); |
| 223 |
} |
| 224 |
explicit GeometryT() |
| 225 |
{ |
| 226 |
set(new PyT(new T()), true); |
| 227 |
validate(); |
| 228 |
} |
| 229 |
explicit GeometryT(const T& val) |
| 230 |
{ |
| 231 |
set(new PyT(new T(val)), true); |
| 232 |
validate(); |
| 233 |
} |
| 234 |
GeometryT(const Object& other) |
| 235 |
: Object(other.ptr()) |
| 236 |
{ |
| 237 |
validate(); |
| 238 |
} |
| 239 |
bool accepts(PyObject* pyob) const override |
| 240 |
{ |
| 241 |
return pyob && Geometry_TypeCheck(pyob); |
| 242 |
} |
| 243 |
GeometryT& operator=(const Object& rhs) |
| 244 |
{ |
| 245 |
*this = *rhs; |
| 246 |
return *this; |
| 247 |
} |
| 248 |
GeometryT& operator=(PyObject* rhsp) |
| 249 |
{ |
| 250 |
if (ptr() == rhsp) { |
| 251 |
return *this; |
| 252 |
} |
| 253 |
set(rhsp, false); |
| 254 |
return *this; |
| 255 |
} |
| 256 |
GeometryT& operator=(const T& val) |
| 257 |
{ |
| 258 |
set(new PyT(val), true); |
| 259 |
return *this; |
| 260 |
} |
| 261 |
const T& getValue() const |
| 262 |
{ |
| 263 |
// cast the PyObject pointer to the matching sub-class |
| 264 |
// and call then the defined member function |
| 265 |
PyT* py = static_cast<PyT*>(ptr()); |
| 266 |
T* val = (py->*valuePtr)(); |
| 267 |
return *val; |
| 268 |
} |
| 269 |
operator T() const |
| 270 |
{ |
| 271 |
return getValue(); |
| 272 |
} |
| 273 |
PyT* getPy() const |
| 274 |
{ |
| 275 |
PyT* py = static_cast<PyT*>(ptr()); |
| 276 |
return py; |
| 277 |
} |
| 278 |
|
| 279 |
private: |
| 280 |
static int Geometry_TypeCheck(PyObject* obj) |
| 281 |
{ |
| 282 |
return PyObject_TypeCheck(obj, &(PyT::Type)); |
| 283 |
} |
| 284 |
}; |
| 285 |
|
| 286 |
// PyCXX wrapper classes Py::Matrix, Py::Rotation, Py::Placement, ... |
| 287 |
using BoundingBox = GeometryT<Base::BoundBox3d, Base::BoundBoxPy, &Base::BoundBoxPy::getBoundBoxPtr>; |
| 288 |
using Matrix = GeometryT<Base::Matrix4D, Base::MatrixPy, &Base::MatrixPy::getMatrixPtr>; |
| 289 |
using Rotation = GeometryT<Base::Rotation, Base::RotationPy, &Base::RotationPy::getRotationPtr>; |
| 290 |
using Placement = GeometryT<Base::Placement, Base::PlacementPy, &Base::PlacementPy::getPlacementPtr>; |
| 291 |
|
| 292 |
} // namespace Py |
| 293 |
|
| 294 |
#endif // PY_GEOMETRYPY_H |