Debian Codegraph @

src/Base/PlacementPyImp.cpp

raw
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 #include "GeometryPyCXX.h"
26 #include "Matrix.h"
27 #include "PyWrapParseTupleAndKeywords.h"
28 #include "Tools.h"
29
30 // generated out of Placement.pyi
31 #include "PlacementPy.h"
32 #include "PlacementPy.cpp"
33
34 #include "MatrixPy.h"
35 #include "RotationPy.h"
36 #include "VectorPy.h"
37
38 using namespace Base;
39
40 // returns a string which represents the object e.g. when printed in python
41 std::string PlacementPy::representation() const
42 {
43 double yaw {};
44 double pitch {};
45 double roll {};
46 PlacementPy::PointerType ptr = getPlacementPtr();
47 std::stringstream str;
48 ptr->getRotation().getYawPitchRoll(yaw, pitch, roll);
49
50 str << "Placement [Pos=(";
51 str << ptr->getPosition().x << "," << ptr->getPosition().y << "," << ptr->getPosition().z;
52 str << "), Yaw-Pitch-Roll=(" << yaw << "," << pitch << "," << roll << ")]";
53
54 return str.str();
55 }
56
57 PyObject* PlacementPy::PyMake(PyTypeObject* /*unused*/, PyObject* /*unused*/, PyObject* /*unused*/)
58 {
59 // create a new instance of PlacementPy and the Twin object
60 return new PlacementPy(new Placement);
61 }
62
63 // clang-format off
64 // constructor method
65 int PlacementPy::PyInit(PyObject* args, PyObject* /*kwd*/)
66 {
67 PyObject* o {};
68 if (PyArg_ParseTuple(args, "")) {
69 return 0;
70 }
71
72 PyErr_Clear();
73 if (PyArg_ParseTuple(args, "O!", &(MatrixPy::Type), &o)) {
74 try {
75 Matrix4D mat = static_cast<MatrixPy*>(o)->value();
76 getPlacementPtr()->fromMatrix(mat);
77 return 0;
78 }
79 catch (const Exception& e) {
80 PyErr_SetString(e.getPyExceptionType(), e.what());
81 return -1;
82 }
83 }
84
85 PyErr_Clear();
86 if (PyArg_ParseTuple(args, "O!", &(PlacementPy::Type), &o)) {
87 Placement* plm = static_cast<PlacementPy*>(o)->getPlacementPtr();
88 *(getPlacementPtr()) = *plm;
89 return 0;
90 }
91
92 PyErr_Clear();
93 PyObject* d {};
94 double angle {};
95 if (PyArg_ParseTuple(args,
96 "O!O!d",
97 &(VectorPy::Type), &o,
98 &(VectorPy::Type), &d,
99 &angle)) {
100 // NOTE: The first parameter defines the translation, the second the rotation axis
101 // and the last parameter defines the rotation angle in degree.
102 Rotation rot(static_cast<VectorPy*>(d)->value(),
103 toRadians(angle));
104 *getPlacementPtr() = Placement(static_cast<VectorPy*>(o)->value(), rot);
105 return 0;
106 }
107
108 PyErr_Clear();
109 if (PyArg_ParseTuple(args,
110 "O!O!",
111 &(VectorPy::Type), &o,
112 &(RotationPy::Type), &d)) {
113 Vector3d* pos = static_cast<VectorPy*>(o)->getVectorPtr();
114 getPlacementPtr()->setPosition(*pos);
115 Rotation* rot = static_cast<RotationPy*>(d)->getRotationPtr();
116 getPlacementPtr()->setRotation(*rot);
117 return 0;
118 }
119
120 PyErr_Clear();
121 PyObject* c {};
122 if (PyArg_ParseTuple(args,
123 "O!O!O!",
124 &(VectorPy::Type), &o,
125 &(RotationPy::Type), &d,
126 &(VectorPy::Type), &c)) {
127 Vector3d* pos = static_cast<VectorPy*>(o)->getVectorPtr();
128 Rotation* rot = static_cast<RotationPy*>(d)->getRotationPtr();
129 Vector3d* cnt = static_cast<VectorPy*>(c)->getVectorPtr();
130 Placement p(*pos, *rot, *cnt);
131 getPlacementPtr()->operator=(p);
132 return 0;
133 }
134
135 PyErr_SetString(PyExc_TypeError, "empty parameter list, matrix or placement expected");
136 return -1;
137 }
138 // clang-format on
139
140 PyObject* PlacementPy::richCompare(PyObject* v, PyObject* w, int op)
141 {
142 if (PyObject_TypeCheck(v, &(PlacementPy::Type)) && PyObject_TypeCheck(w, &(PlacementPy::Type))) {
143 Placement p1 = *static_cast<PlacementPy*>(v)->getPlacementPtr();
144 Placement p2 = *static_cast<PlacementPy*>(w)->getPlacementPtr();
145
146 PyObject* res = nullptr;
147 if (op != Py_EQ && op != Py_NE) {
148 PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Placement");
149 return nullptr;
150 }
151 if (op == Py_EQ) {
152 res = (p1 == p2) ? Py_True : Py_False;
153 Py_INCREF(res);
154 return res;
155 }
156 res = (p1 != p2) ? Py_True : Py_False;
157 Py_INCREF(res);
158 return res;
159 }
160 // This always returns False
161 Py_INCREF(Py_NotImplemented);
162 return Py_NotImplemented;
163 }
164
165 PyObject* PlacementPy::move(PyObject* args)
166 {
167 PyObject* vec {};
168 if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &vec)) {
169 return nullptr;
170 }
171 getPlacementPtr()->move(static_cast<VectorPy*>(vec)->value());
172 Py_Return;
173 }
174
175 PyObject* PlacementPy::translate(PyObject* args)
176 {
177 return move(args);
178 }
179
180 // clang-format off
181 PyObject* PlacementPy::rotate(PyObject* args, PyObject* kwds)
182 {
183 double angle {};
184 static const std::array<const char*, 5> kwlist {"center", "axis", "angle", "comp", nullptr};
185 Vector3d center;
186 Vector3d axis;
187 PyObject* pyComp = Py_False; // NOLINT
188
189 if (!Wrapped_ParseTupleAndKeywords(args,
190 kwds,
191 "(ddd)(ddd)d|O!",
192 kwlist,
193 &center.x, &center.y, &center.z,
194 &axis.x, &axis.y, &axis.z,
195 &angle, &PyBool_Type, &pyComp)) {
196 return nullptr;
197 }
198
199 try {
200 /*
201 * if comp is False, we retain the original behaviour that - contrary to the (old)
202 * documentation - generates Placements different from TopoShape.rotate() to ensure
203 * compatibility for existing code
204 */
205 bool comp = asBoolean(pyComp);
206
207 if (!comp) {
208 getPlacementPtr()->multRight(
209 Placement(Vector3d(), Rotation(axis, toRadians<double>(angle)), center));
210 }
211 else {
212 // multiply new Placement the same way TopoShape.rotate() does
213 getPlacementPtr()->multLeft(
214 Placement(Vector3d(), Rotation(axis, toRadians<double>(angle)), center));
215 }
216
217 Py_Return;
218 }
219 catch (const Py::Exception&) {
220 return nullptr;
221 }
222 }
223 // clang-format on
224
225 PyObject* PlacementPy::multiply(PyObject* args) const
226 {
227 PyObject* plm {};
228 if (!PyArg_ParseTuple(args, "O!", &(PlacementPy::Type), &plm)) {
229 return nullptr;
230 }
231 Placement mult = (*getPlacementPtr()) * (*static_cast<PlacementPy*>(plm)->getPlacementPtr());
232 return new PlacementPy(new Placement(mult));
233 }
234
235 PyObject* PlacementPy::multVec(PyObject* args) const
236 {
237 PyObject* vec {};
238 if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &vec)) {
239 return nullptr;
240 }
241 Vector3d pnt(static_cast<VectorPy*>(vec)->value());
242 getPlacementPtr()->multVec(pnt, pnt);
243 return new VectorPy(new Vector3d(pnt));
244 }
245
246 PyObject* PlacementPy::copy(PyObject* args) const
247 {
248 if (!PyArg_ParseTuple(args, "")) {
249 return nullptr;
250 }
251 return new PlacementPy(new Placement(*getPlacementPtr()));
252 }
253
254 PyObject* PlacementPy::toMatrix(PyObject* args) const
255 {
256 if (!PyArg_ParseTuple(args, "")) {
257 return nullptr;
258 }
259 Matrix4D mat = getPlacementPtr()->toMatrix();
260 return new MatrixPy(new Matrix4D(mat));
261 }
262
263 PyObject* PlacementPy::inverse(PyObject* args) const
264 {
265 if (!PyArg_ParseTuple(args, "")) {
266 return nullptr;
267 }
268 Placement p = getPlacementPtr()->inverse();
269 return new PlacementPy(new Placement(p));
270 }
271
272 PyObject* PlacementPy::pow(PyObject* args) const
273 {
274 double t {};
275 PyObject* shorten = Py_True;
276 if (!PyArg_ParseTuple(args, "d|O!", &t, &(PyBool_Type), &shorten)) {
277 return nullptr;
278 }
279 Placement ret = getPlacementPtr()->pow(t, asBoolean(shorten));
280 return new PlacementPy(new Placement(ret));
281 }
282
283
284 PyObject* PlacementPy::sclerp(PyObject* args) const
285 {
286 PyObject* pyplm2 {};
287 double t {};
288 PyObject* shorten = Py_True;
289 if (!PyArg_ParseTuple(args, "O!d|O!", &(PlacementPy::Type), &pyplm2, &t, &(PyBool_Type), &shorten)) {
290 return nullptr;
291 }
292 Placement plm2 = static_cast<PlacementPy*>(pyplm2)->value();
293 Placement ret = Placement::sclerp(*getPlacementPtr(), plm2, t, asBoolean(shorten));
294 return new PlacementPy(new Placement(ret));
295 }
296
297 PyObject* PlacementPy::slerp(PyObject* args) const
298 {
299 PyObject* pyplm2 {};
300 double t {};
301 if (!PyArg_ParseTuple(args, "O!d", &(PlacementPy::Type), &pyplm2, &t)) {
302 return nullptr;
303 }
304 Placement plm2 = static_cast<PlacementPy*>(pyplm2)->value();
305 Placement ret = Placement::slerp(*getPlacementPtr(), plm2, t);
306 return new PlacementPy(new Placement(ret));
307 }
308
309 PyObject* PlacementPy::isIdentity(PyObject* args) const
310 {
311 double tol = 0.0;
312 if (!PyArg_ParseTuple(args, "|d", &tol)) {
313 return nullptr;
314 }
315 bool none = tol > 0 ? getPlacementPtr()->isIdentity(tol) : getPlacementPtr()->isIdentity();
316 return Py_BuildValue("O", (none ? Py_True : Py_False));
317 }
318
319 PyObject* PlacementPy::isSame(PyObject* args) const
320 {
321 PyObject* plm {};
322 double tol = 0.0;
323 if (!PyArg_ParseTuple(args, "O!|d", &PlacementPy::Type, &plm, &tol)) {
324 return nullptr;
325 }
326
327 Placement plm1 = *getPlacementPtr();
328 Placement plm2 = *static_cast<PlacementPy*>(plm)->getPlacementPtr();
329 bool same = tol > 0.0 ? plm1.isSame(plm2, tol) : plm1.isSame(plm2);
330 return Py_BuildValue("O", (same ? Py_True : Py_False));
331 }
332
333 Py::Object PlacementPy::getBase() const
334 {
335 return Py::Vector(getPlacementPtr()->getPosition()); // NOLINT
336 }
337
338 void PlacementPy::setBase(Py::Object arg)
339 {
340 getPlacementPtr()->setPosition(Py::Vector(arg).toVector());
341 }
342
343 Py::Object PlacementPy::getRotation() const
344 {
345 return Py::Rotation(getPlacementPtr()->getRotation());
346 }
347
348 void PlacementPy::setRotation(Py::Object arg)
349 {
350 Py::Rotation rot;
351 if (rot.accepts(arg.ptr())) {
352 getPlacementPtr()->setRotation(static_cast<Rotation>(Py::Rotation(arg)));
353 return;
354 }
355 Py::Tuple tuple;
356 if (tuple.accepts(arg.ptr())) {
357 tuple = arg;
358 getPlacementPtr()->setRotation(Rotation(
359 static_cast<double>(Py::Float(tuple[0])),
360 static_cast<double>(Py::Float(tuple[1])),
361 static_cast<double>(Py::Float(tuple[2])),
362 static_cast<double>(Py::Float(tuple[3]))
363 ));
364 return;
365 }
366
367 throw Py::TypeError("either Rotation or tuple of four floats expected");
368 }
369
370 Py::Object PlacementPy::getMatrix() const
371 {
372 return Py::Matrix(getPlacementPtr()->toMatrix());
373 }
374
375 void PlacementPy::setMatrix(Py::Object arg)
376 {
377 Py::Matrix mat;
378 if (!mat.accepts(arg.ptr())) {
379 throw Py::TypeError("Expect type Matrix");
380 }
381 try {
382 mat = arg;
383 getPlacementPtr()->fromMatrix(mat);
384 }
385 catch (const ValueError& e) {
386 throw Py::ValueError(e.what());
387 }
388 }
389
390 PyObject* PlacementPy::getCustomAttributes(const char* attr) const
391 {
392 // for backward compatibility
393 if (strcmp(attr, "isNull") == 0) {
394 PyObject* w {};
395 PyObject* res {};
396 w = PyUnicode_InternFromString("isIdentity");
397 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
398 res = PyObject_GenericGetAttr(const_cast<PlacementPy*>(this), w);
399 Py_XDECREF(w);
400 return res;
401 }
402 return nullptr;
403 }
404
405 int PlacementPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
406 {
407 return 0;
408 }
409
410 PyObject* PlacementPy::number_multiply_handler(PyObject* self, PyObject* other)
411 {
412 if (PyObject_TypeCheck(self, &(PlacementPy::Type))) {
413 Placement a = static_cast<PlacementPy*>(self)->value();
414
415 if (PyObject_TypeCheck(other, &(VectorPy::Type))) {
416 Vector3d res;
417 a.multVec(static_cast<VectorPy*>(other)->value(), res);
418 return new VectorPy(res);
419 }
420
421 if (PyObject_TypeCheck(other, &(RotationPy::Type))) {
422 Placement b(Vector3d(), static_cast<RotationPy*>(other)->value());
423 return new PlacementPy(a * b);
424 }
425
426 if (PyObject_TypeCheck(other, &(PlacementPy::Type))) {
427 const auto& b = static_cast<PlacementPy*>(other)->value();
428 return new PlacementPy(a * b);
429 }
430
431 if (PyObject_TypeCheck(other, &(MatrixPy::Type))) {
432 const auto& b = static_cast<MatrixPy*>(other)->value();
433 return new MatrixPy(a.toMatrix() * b);
434 }
435 }
436
437 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
438 return nullptr;
439 }
440
441 PyObject* PlacementPy::number_power_handler(PyObject* self, PyObject* other, PyObject* arg)
442 {
443 Py::Object pw(other);
444 Py::Tuple tup(1);
445 tup[0] = pw;
446
447 double pw_v {};
448 if (!PyArg_ParseTuple(tup.ptr(), "d", &pw_v)) {
449 return nullptr;
450 }
451 if (!PyObject_TypeCheck(self, &(PlacementPy::Type)) || arg != Py_None) {
452 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
453 return nullptr;
454 }
455
456 Placement a = static_cast<PlacementPy*>(self)->value();
457 return new PlacementPy(a.pow(pw_v));
458 }
459
460 PyObject* PlacementPy::number_add_handler(PyObject* /*self*/, PyObject* /*other*/)
461 {
462 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
463 return nullptr;
464 }
465
466 PyObject* PlacementPy::number_subtract_handler(PyObject* /*self*/, PyObject* /*other*/)
467 {
468 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
469 return nullptr;
470 }
471
472 PyObject* PlacementPy::number_divide_handler(PyObject* /*self*/, PyObject* /*other*/)
473 {
474 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
475 return nullptr;
476 }
477
478 PyObject* PlacementPy::number_remainder_handler(PyObject* /*self*/, PyObject* /*other*/)
479 {
480 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
481 return nullptr;
482 }
483
484 PyObject* PlacementPy::number_divmod_handler(PyObject* /*self*/, PyObject* /*other*/)
485 {
486 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
487 return nullptr;
488 }
489
490 PyObject* PlacementPy::number_negative_handler(PyObject* /*self*/)
491 {
492 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
493 return nullptr;
494 }
495
496 PyObject* PlacementPy::number_positive_handler(PyObject* /*self*/)
497 {
498 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
499 return nullptr;
500 }
501
502 PyObject* PlacementPy::number_absolute_handler(PyObject* /*self*/)
503 {
504 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
505 return nullptr;
506 }
507
508 int PlacementPy::number_nonzero_handler(PyObject* /*self*/)
509 {
510 return 1;
511 }
512
513 PyObject* PlacementPy::number_invert_handler(PyObject* /*self*/)
514 {
515 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
516 return nullptr;
517 }
518
519 PyObject* PlacementPy::number_lshift_handler(PyObject* /*self*/, PyObject* /*other*/)
520 {
521 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
522 return nullptr;
523 }
524
525 PyObject* PlacementPy::number_rshift_handler(PyObject* /*self*/, PyObject* /*other*/)
526 {
527 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
528 return nullptr;
529 }
530
531 PyObject* PlacementPy::number_and_handler(PyObject* /*self*/, PyObject* /*other*/)
532 {
533 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
534 return nullptr;
535 }
536
537 PyObject* PlacementPy::number_xor_handler(PyObject* /*self*/, PyObject* /*other*/)
538 {
539 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
540 return nullptr;
541 }
542
543 PyObject* PlacementPy::number_or_handler(PyObject* /*self*/, PyObject* /*other*/)
544 {
545 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
546 return nullptr;
547 }
548
549 PyObject* PlacementPy::number_int_handler(PyObject* /*self*/)
550 {
551 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
552 return nullptr;
553 }
554
555 PyObject* PlacementPy::number_float_handler(PyObject* /*self*/)
556 {
557 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
558 return nullptr;
559 }