1
/***************************************************************************
2
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "PreCompiled.h"
26
#include <Base/GeometryPyCXX.h>
27
#include <Base/Tools.h>
28
#include <Base/PyWrapParseTupleAndKeywords.h>
30
// inclusion of the generated files (generated out of RotationPy.xml)
31
#include "RotationPy.h"
32
#include "RotationPy.cpp"
38
// returns a string which represents the object e.g. when printed in python
39
std::string RotationPy::representation() const
41
RotationPy::PointerType ptr = getRotationPtr();
42
Py::Float q0(ptr->getValue()[0]);
43
Py::Float q1(ptr->getValue()[1]);
44
Py::Float q2(ptr->getValue()[2]);
45
Py::Float q3(ptr->getValue()[3]);
46
std::stringstream str;
48
str << static_cast<std::string>(q0.repr()) << ", " << static_cast<std::string>(q1.repr())
49
<< ", " << static_cast<std::string>(q2.repr()) << ", "
50
<< static_cast<std::string>(q3.repr());
56
PyObject* RotationPy::PyMake(PyTypeObject* /*unused*/, PyObject* /*unused*/, PyObject* /*unused*/)
58
// create a new instance of RotationPy and the Twin object
59
return new RotationPy(new Rotation);
64
int RotationPy::PyInit(PyObject* args, PyObject* kwds)
67
if (PyArg_ParseTuple(args, "")) {
72
if (PyArg_ParseTuple(args, "O!", &(Base::RotationPy::Type), &o)) {
73
Base::Rotation* rot = static_cast<Base::RotationPy*>(o)->getRotationPtr();
74
getRotationPtr()->setValue(rot->getValue());
80
static const std::array<const char*, 3> kw_deg {"Axis", "Degree", nullptr};
81
if (Base::Wrapped_ParseTupleAndKeywords(args,
85
&(Base::VectorPy::Type),
88
// NOTE: The last parameter defines the rotation angle in degree.
89
getRotationPtr()->setValue(static_cast<Base::VectorPy*>(o)->value(),
90
Base::toRadians<double>(angle));
95
static const std::array<const char*, 3> kw_rad {"Axis", "Radian", nullptr};
96
if (Base::Wrapped_ParseTupleAndKeywords(args,
100
&(Base::VectorPy::Type),
103
getRotationPtr()->setValue(static_cast<Base::VectorPy*>(o)->value(), angle);
108
if (PyArg_ParseTuple(args, "O!", &(Base::MatrixPy::Type), &o)) {
110
getRotationPtr()->setValue(static_cast<Base::MatrixPy*>(o)->value());
113
catch (const Base::Exception& e) {
114
PyErr_SetString(e.getPyExceptionType(), e.what());
124
if (PyArg_ParseTuple(args, "dddd", &q0, &q1, &q2, &q3)) {
125
getRotationPtr()->setValue(q0, q1, q2, q3);
133
if (PyArg_ParseTuple(args, "ddd", &y, &p, &r)) {
134
getRotationPtr()->setYawPitchRoll(y, p, r);
143
if (PyArg_ParseTuple(args, "sddd", &seq, &a, &b, &c)) {
146
getRotationPtr()->setEulerAngles(Rotation::eulerSequenceFromName(seq), a, b, c);
153
double a11 = 1.0, a12 = 0.0, a13 = 0.0, a14 = 0.0;
154
double a21 = 0.0, a22 = 1.0, a23 = 0.0, a24 = 0.0;
155
double a31 = 0.0, a32 = 0.0, a33 = 1.0, a34 = 0.0;
156
double a41 = 0.0, a42 = 0.0, a43 = 0.0, a44 = 1.0;
159
// try read a 4x4 matrix
161
if (PyArg_ParseTuple(args,
163
&a11, &a12, &a13, &a14,
164
&a21, &a22, &a23, &a24,
165
&a31, &a32, &a33, &a34,
166
&a41, &a42, &a43, &a44)) {
168
Matrix4D mtx(a11, a12, a13, a14,
172
getRotationPtr()->setValue(mtx);
175
catch (const Base::Exception& e) {
176
PyErr_SetString(e.getPyExceptionType(), e.what());
181
// try read a 3x3 matrix
183
if (PyArg_ParseTuple(args, "ddddddddd",
188
Matrix4D mtx(a11, a12, a13, a14,
192
getRotationPtr()->setValue(mtx);
195
catch (const Base::Exception& e) {
196
PyErr_SetString(e.getPyExceptionType(), e.what());
204
if (PyArg_ParseTuple(args,
206
&(Base::VectorPy::Type), &v1,
207
&(Base::VectorPy::Type), &v2)) {
208
Py::Vector from(v1, false);
209
Py::Vector to(v2, false);
210
getRotationPtr()->setValue(from.toVector(), to.toVector());
216
const char* priority = nullptr;
217
if (PyArg_ParseTuple(args,
219
&(Base::VectorPy::Type), &v1,
220
&(Base::VectorPy::Type), &v2,
221
&(Base::VectorPy::Type), &v3,
223
Py::Vector xdir(v1, false);
224
Py::Vector ydir(v2, false);
225
Py::Vector zdir(v3, false);
230
*getRotationPtr() = (Rotation::makeRotationByAxes(xdir.toVector(),
235
catch (Base::Exception& e) {
237
str += "FreeCAD exception thrown (";
240
PyErr_SetString(Base::PyExc_FC_GeneralError, str.c_str());
247
PyErr_SetString(PyExc_TypeError,
248
"Rotation constructor accepts:\n"
249
"-- empty parameter list\n"
250
"-- Rotation object\n"
251
"-- four floats (a quaternion)\n"
252
"-- three floats (yaw, pitch, roll)\n"
253
"-- Vector (rotation axis) and float (rotation angle)\n"
254
"-- two Vectors (two axes)\n"
256
"-- 16 floats (4x4 matrix)\n"
257
"-- 9 floats (3x3 matrix)\n"
258
"-- 3 vectors + optional string");
263
PyObject* RotationPy::richCompare(PyObject* v, PyObject* w, int op)
265
if (PyObject_TypeCheck(v, &(RotationPy::Type)) && PyObject_TypeCheck(w, &(RotationPy::Type))) {
266
Base::Rotation r1 = *static_cast<RotationPy*>(v)->getRotationPtr();
267
Base::Rotation r2 = *static_cast<RotationPy*>(w)->getRotationPtr();
269
PyObject* res = nullptr;
270
if (op != Py_EQ && op != Py_NE) {
271
PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Rotation");
275
res = (r1 == r2) ? Py_True : Py_False;
279
res = (r1 != r2) ? Py_True : Py_False;
283
// This always returns False
284
Py_INCREF(Py_NotImplemented);
285
return Py_NotImplemented;
288
PyObject* RotationPy::invert(PyObject* args)
290
if (!PyArg_ParseTuple(args, "")) {
293
this->getRotationPtr()->invert();
297
PyObject* RotationPy::inverted(PyObject* args)
299
if (!PyArg_ParseTuple(args, "")) {
302
Rotation mult = this->getRotationPtr()->inverse();
303
return new RotationPy(new Rotation(mult));
306
PyObject* RotationPy::multiply(PyObject* args)
309
if (!PyArg_ParseTuple(args, "O!", &(RotationPy::Type), &rot)) {
312
Rotation mult = (*getRotationPtr()) * (*static_cast<RotationPy*>(rot)->getRotationPtr());
313
return new RotationPy(new Rotation(mult));
316
PyObject* RotationPy::multVec(PyObject* args)
319
if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj)) {
322
Base::Vector3d vec(static_cast<VectorPy*>(obj)->value());
323
getRotationPtr()->multVec(vec, vec);
324
return new VectorPy(new Vector3d(vec));
327
PyObject* RotationPy::slerp(PyObject* args)
331
if (!PyArg_ParseTuple(args, "O!d", &(RotationPy::Type), &rot, &t)) {
334
Rotation* rot0 = this->getRotationPtr();
335
Rotation* rot1 = static_cast<RotationPy*>(rot)->getRotationPtr();
336
Rotation sl = Rotation::slerp(*rot0, *rot1, t);
337
return new RotationPy(new Rotation(sl));
340
PyObject* RotationPy::setYawPitchRoll(PyObject* args)
345
if (!PyArg_ParseTuple(args, "ddd", &A, &B, &C)) {
348
this->getRotationPtr()->setYawPitchRoll(A, B, C);
352
PyObject* RotationPy::getYawPitchRoll(PyObject* args)
354
if (!PyArg_ParseTuple(args, "")) {
360
this->getRotationPtr()->getYawPitchRoll(A, B, C);
363
tuple.setItem(0, Py::Float(A));
364
tuple.setItem(1, Py::Float(B));
365
tuple.setItem(2, Py::Float(C));
366
return Py::new_reference_to(tuple);
369
PyObject* RotationPy::setEulerAngles(PyObject* args)
375
if (!PyArg_ParseTuple(args, "sddd", &seq, &A, &B, &C)) {
380
getRotationPtr()->setEulerAngles(Rotation::eulerSequenceFromName(seq), A, B, C);
383
catch (const Base::Exception& e) {
389
PyObject* RotationPy::toEulerAngles(PyObject* args)
391
const char* seq = nullptr;
392
if (!PyArg_ParseTuple(args, "|s", &seq)) {
397
for (int i = 1; i < Rotation::EulerSequenceLast; ++i) {
398
res.append(Py::String(Rotation::eulerSequenceName((Rotation::EulerSequence)i)));
400
return Py::new_reference_to(res);
408
this->getRotationPtr()->getEulerAngles(Rotation::eulerSequenceFromName(seq), A, B, C);
411
tuple.setItem(0, Py::Float(A));
412
tuple.setItem(1, Py::Float(B));
413
tuple.setItem(2, Py::Float(C));
414
return Py::new_reference_to(tuple);
419
PyObject* RotationPy::toMatrix(PyObject* args)
421
if (!PyArg_ParseTuple(args, "")) {
425
getRotationPtr()->getValue(mat);
426
return new MatrixPy(new Matrix4D(mat));
429
PyObject* RotationPy::isSame(PyObject* args)
433
if (!PyArg_ParseTuple(args, "O!|d", &(RotationPy::Type), &rot, &tol)) {
436
Base::Rotation rot1 = *getRotationPtr();
437
Base::Rotation rot2 = *static_cast<RotationPy*>(rot)->getRotationPtr();
438
bool same = tol > 0.0 ? rot1.isSame(rot2, tol) : rot1.isSame(rot2);
439
return Py_BuildValue("O", (same ? Py_True : Py_False));
442
PyObject* RotationPy::isIdentity(PyObject* args)
445
if (!PyArg_ParseTuple(args, "|d", &tol)) {
448
bool null = tol > 0.0 ? getRotationPtr()->isIdentity(tol) : getRotationPtr()->isIdentity();
449
return Py_BuildValue("O", (null ? Py_True : Py_False));
452
PyObject* RotationPy::isNull(PyObject* args)
454
if (!PyArg_ParseTuple(args, "")) {
457
bool null = getRotationPtr()->isNull();
458
return Py_BuildValue("O", (null ? Py_True : Py_False));
461
Py::Tuple RotationPy::getQ() const
467
this->getRotationPtr()->getValue(q0, q1, q2, q3);
470
tuple.setItem(0, Py::Float(q0));
471
tuple.setItem(1, Py::Float(q1));
472
tuple.setItem(2, Py::Float(q2));
473
tuple.setItem(3, Py::Float(q3));
477
void RotationPy::setQ(Py::Tuple arg)
479
double q0 = static_cast<double>(Py::Float(arg.getItem(0)));
480
double q1 = static_cast<double>(Py::Float(arg.getItem(1)));
481
double q2 = static_cast<double>(Py::Float(arg.getItem(2)));
482
double q3 = static_cast<double>(Py::Float(arg.getItem(3)));
483
this->getRotationPtr()->setValue(q0, q1, q2, q3);
486
Py::Object RotationPy::getRawAxis() const
490
this->getRotationPtr()->getRawValue(axis, angle);
491
return Py::Vector(axis); // NOLINT
494
Py::Object RotationPy::getAxis() const
498
this->getRotationPtr()->getValue(axis, angle);
499
return Py::Vector(axis); // NOLINT
502
void RotationPy::setAxis(Py::Object arg)
506
this->getRotationPtr()->getValue(axis, angle);
507
axis = Py::Vector(arg).toVector();
508
this->getRotationPtr()->setValue(axis, angle);
511
Py::Float RotationPy::getAngle() const
515
this->getRotationPtr()->getValue(axis, angle);
516
return Py::Float(angle);
519
void RotationPy::setAngle(Py::Float arg)
523
this->getRotationPtr()->getRawValue(axis, angle);
524
angle = static_cast<double>(arg);
525
this->getRotationPtr()->setValue(axis, angle);
528
PyObject* RotationPy::getCustomAttributes(const char* attr) const
530
if (strcmp(attr, "Matrix") == 0) {
532
this->getRotationPtr()->getValue(mat);
533
return new MatrixPy(mat);
535
if (strcmp(attr, "Yaw") == 0) {
539
this->getRotationPtr()->getYawPitchRoll(A, B, C);
540
return PyFloat_FromDouble(A);
542
if (strcmp(attr, "Pitch") == 0) {
546
this->getRotationPtr()->getYawPitchRoll(A, B, C);
547
return PyFloat_FromDouble(B);
549
if (strcmp(attr, "Roll") == 0) {
553
this->getRotationPtr()->getYawPitchRoll(A, B, C);
554
return PyFloat_FromDouble(C);
556
if (strcmp(attr, "toEuler") == 0) {
557
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
558
Py::Object self(const_cast<RotationPy*>(this), false);
559
return Py::new_reference_to(self.getAttr("getYawPitchRoll"));
564
int RotationPy::setCustomAttributes(const char* attr, PyObject* obj)
566
if (strcmp(attr, "Matrix") == 0) {
567
if (PyObject_TypeCheck(obj, &(MatrixPy::Type))) {
569
this->getRotationPtr()->setValue(*static_cast<MatrixPy*>(obj)->getMatrixPtr());
572
catch (const Base::Exception& e) {
573
PyErr_SetString(e.getPyExceptionType(), e.what());
578
else if (strcmp(attr, "Axes") == 0) {
579
if (PySequence_Check(obj)) {
580
Py::Sequence sequence(obj);
581
if (sequence.size() == 2) {
582
Py::Object vec1 = sequence.getItem(0);
583
Py::Object vec2 = sequence.getItem(1);
584
if (PyObject_TypeCheck(vec1.ptr(), &(VectorPy::Type))
585
&& PyObject_TypeCheck(vec2.ptr(), &(VectorPy::Type))) {
586
Base::Vector3d* pt1 = static_cast<VectorPy*>(vec1.ptr())->getVectorPtr();
587
Base::Vector3d* pt2 = static_cast<VectorPy*>(vec2.ptr())->getVectorPtr();
588
this->getRotationPtr()->setValue(*pt1, *pt2);
594
else if (strcmp(attr, "Yaw") == 0) {
595
if (PyNumber_Check(obj)) {
596
double V = PyFloat_AsDouble(obj);
600
this->getRotationPtr()->getYawPitchRoll(A, B, C);
601
this->getRotationPtr()->setYawPitchRoll(V, B, C);
605
else if (strcmp(attr, "Pitch") == 0) {
606
if (PyNumber_Check(obj)) {
607
double V = PyFloat_AsDouble(obj);
611
this->getRotationPtr()->getYawPitchRoll(A, B, C);
612
this->getRotationPtr()->setYawPitchRoll(A, V, C);
616
else if (strcmp(attr, "Roll") == 0) {
617
if (PyNumber_Check(obj)) {
618
double V = PyFloat_AsDouble(obj);
622
this->getRotationPtr()->getYawPitchRoll(A, B, C);
623
this->getRotationPtr()->setYawPitchRoll(A, B, V);
630
PyObject* RotationPy::number_multiply_handler(PyObject* self, PyObject* other)
632
if (PyObject_TypeCheck(self, &(RotationPy::Type))) {
633
auto a = static_cast<RotationPy*>(self)->value();
635
if (PyObject_TypeCheck(other, &(VectorPy::Type))) {
637
a.multVec(static_cast<VectorPy*>(other)->value(), res);
638
return new VectorPy(res);
641
if (PyObject_TypeCheck(other, &(PlacementPy::Type))) {
642
const auto& b = static_cast<PlacementPy*>(other)->value();
643
return new PlacementPy(Placement(Vector3d(), a) * b);
646
if (PyObject_TypeCheck(other, &(RotationPy::Type))) {
647
const auto& b = static_cast<RotationPy*>(other)->value();
648
return new RotationPy(a * b);
651
if (PyObject_TypeCheck(other, &(MatrixPy::Type))) {
652
const auto& b = static_cast<MatrixPy*>(other)->value();
655
return new MatrixPy(mat * b);
659
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
663
PyObject* RotationPy::number_power_handler(PyObject* self, PyObject* other, PyObject* arg)
665
if (!PyObject_TypeCheck(self, &(RotationPy::Type)) || !PyLong_Check(other) || arg != Py_None) {
666
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
670
Rotation a = static_cast<RotationPy*>(self)->value();
671
long b = Py::Long(other);
676
a.getRawValue(axis, rfAngle);
677
rfAngle *= double(b);
678
a.setValue(axis, rfAngle);
680
return new RotationPy(a);
683
PyObject* RotationPy::number_add_handler(PyObject* /*self*/, PyObject* /*other*/)
685
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
689
PyObject* RotationPy::number_subtract_handler(PyObject* /*self*/, PyObject* /*other*/)
691
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
695
PyObject* RotationPy::number_divide_handler(PyObject* /*self*/, PyObject* /*other*/)
697
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
701
PyObject* RotationPy::number_remainder_handler(PyObject* /*self*/, PyObject* /*other*/)
703
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
707
PyObject* RotationPy::number_divmod_handler(PyObject* /*self*/, PyObject* /*other*/)
709
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
713
PyObject* RotationPy::number_negative_handler(PyObject* /*self*/)
715
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
719
PyObject* RotationPy::number_positive_handler(PyObject* /*self*/)
721
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
725
PyObject* RotationPy::number_absolute_handler(PyObject* /*self*/)
727
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
731
int RotationPy::number_nonzero_handler(PyObject* /*self*/)
736
PyObject* RotationPy::number_invert_handler(PyObject* /*self*/)
738
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
742
PyObject* RotationPy::number_lshift_handler(PyObject* /*self*/, PyObject* /*other*/)
744
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
748
PyObject* RotationPy::number_rshift_handler(PyObject* /*self*/, PyObject* /*other*/)
750
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
754
PyObject* RotationPy::number_and_handler(PyObject* /*self*/, PyObject* /*other*/)
756
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
760
PyObject* RotationPy::number_xor_handler(PyObject* /*self*/, PyObject* /*other*/)
762
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
766
PyObject* RotationPy::number_or_handler(PyObject* /*self*/, PyObject* /*other*/)
768
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
772
PyObject* RotationPy::number_int_handler(PyObject* /*self*/)
774
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
778
PyObject* RotationPy::number_float_handler(PyObject* /*self*/)
780
PyErr_SetString(PyExc_NotImplementedError, "Not implemented");