Debian Codegraph @

src/Base/MatrixPyImp.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 <limits>
26
27 #include "GeometryPyCXX.h"
28
29 // generated out of Matrix.pyi
30 #include "MatrixPy.h"
31 #include "MatrixPy.cpp"
32
33 #include "RotationPy.h"
34 #include "QuantityPy.h"
35 #include "VectorPy.h"
36
37 using namespace Base;
38
39 // returns a string which represents the object e.g. when printed in python
40 std::string MatrixPy::representation() const
41 {
42 const Matrix4D& m = *(this->getMatrixPtr());
43 std::stringstream str;
44 str << "Matrix (";
45 str << "(" << m[0][0] << "," << m[0][1] << "," << m[0][2] << "," << m[0][3] << ")"
46 << ",";
47 str << "(" << m[1][0] << "," << m[1][1] << "," << m[1][2] << "," << m[1][3] << ")"
48 << ",";
49 str << "(" << m[2][0] << "," << m[2][1] << "," << m[2][2] << "," << m[2][3] << ")"
50 << ",";
51 str << "(" << m[3][0] << "," << m[3][1] << "," << m[3][2] << "," << m[3][3] << ")";
52 str << ")";
53
54 return str.str();
55 }
56
57 PyObject* MatrixPy::PyMake(PyTypeObject* /*unused*/, PyObject* /*unused*/, PyObject* /*unused*/)
58 {
59 // create a new instance of MatrixPy and the Twin object
60 return new MatrixPy(new Matrix4D);
61 }
62
63 // constructor method
64 int MatrixPy::PyInit(PyObject* args, PyObject* /*kwd*/)
65 {
66 // NOLINTBEGIN
67 double a11 = 1.0, a12 = 0.0, a13 = 0.0, a14 = 0.0;
68 double a21 = 0.0, a22 = 1.0, a23 = 0.0, a24 = 0.0;
69 double a31 = 0.0, a32 = 0.0, a33 = 1.0, a34 = 0.0;
70 double a41 = 0.0, a42 = 0.0, a43 = 0.0, a44 = 1.0;
71 // NOLINTEND
72
73 // clang-format off
74 if (PyArg_ParseTuple(args,
75 "|dddddddddddddddd",
76 &a11, &a12, &a13, &a14,
77 &a21, &a22, &a23, &a24,
78 &a31, &a32, &a33, &a34,
79 &a41, &a42, &a43, &a44)) {
80 MatrixPy::PointerType ptr = getMatrixPtr();
81 (*ptr) = Matrix4D(a11, a12, a13, a14,
82 a21, a22, a23, a24,
83 a31, a32, a33, a34,
84 a41, a42, a43, a44);
85 return 0;
86 }
87
88 PyErr_Clear();
89 PyObject* o {};
90 if (PyArg_ParseTuple(args, "O!", &(MatrixPy::Type), &o)) {
91 MatrixPy::PointerType ptr = getMatrixPtr();
92 (*ptr) = static_cast<MatrixPy*>(o)->value();
93 return 0;
94 }
95
96 PyErr_Clear();
97 PyObject* o1 {};
98 PyObject* o2 {};
99 PyObject* o3 {};
100 PyObject* o4 {};
101 if (PyArg_ParseTuple(args, "O!O!O!|O!",
102 &(VectorPy::Type), &o1,
103 &(VectorPy::Type), &o2,
104 &(VectorPy::Type), &o3,
105 &(VectorPy::Type), &o4)) {
106 Vector3d v1 = Py::Vector(o1, false).toVector();
107 Vector3d v2 = Py::Vector(o2, false).toVector();
108 Vector3d v3 = Py::Vector(o3, false).toVector();
109 Vector3d v4;
110 if (o4) {
111 v4 = Py::Vector(o4, false).toVector();
112 }
113 MatrixPy::PointerType ptr = this->getMatrixPtr();
114
115 (*ptr)[0][0] = v1.x;
116 (*ptr)[1][0] = v1.y;
117 (*ptr)[2][0] = v1.z;
118
119 (*ptr)[0][1] = v2.x;
120 (*ptr)[1][1] = v2.y;
121 (*ptr)[2][1] = v2.z;
122
123 (*ptr)[0][2] = v3.x;
124 (*ptr)[1][2] = v3.y;
125 (*ptr)[2][2] = v3.z;
126
127 (*ptr)[0][3] = v4.x;
128 (*ptr)[1][3] = v4.y;
129 (*ptr)[2][3] = v4.z;
130
131 return 0;
132 }
133 // clang-format on
134
135 PyErr_SetString(PyExc_TypeError, "Base.Matrix, four Base.Vector or up to 16 floats expected");
136 return -1;
137 }
138
139 PyObject* MatrixPy::number_add_handler(PyObject* self, PyObject* other)
140 {
141 if (!PyObject_TypeCheck(self, &(MatrixPy::Type))) {
142 PyErr_SetString(PyExc_TypeError, "First arg must be Matrix");
143 return nullptr;
144 }
145 if (!PyObject_TypeCheck(other, &(MatrixPy::Type))) {
146 PyErr_SetString(PyExc_TypeError, "Second arg must be Matrix");
147 return nullptr;
148 }
149 Matrix4D a = static_cast<MatrixPy*>(self)->value();
150 Matrix4D b = static_cast<MatrixPy*>(other)->value();
151 return new MatrixPy(a + b);
152 }
153
154 PyObject* MatrixPy::number_subtract_handler(PyObject* self, PyObject* other)
155 {
156 if (!PyObject_TypeCheck(self, &(MatrixPy::Type))) {
157 PyErr_SetString(PyExc_TypeError, "First arg must be Matrix");
158 return nullptr;
159 }
160 if (!PyObject_TypeCheck(other, &(MatrixPy::Type))) {
161 PyErr_SetString(PyExc_TypeError, "Second arg must be Matrix");
162 return nullptr;
163 }
164 Matrix4D a = static_cast<MatrixPy*>(self)->value();
165 Matrix4D b = static_cast<MatrixPy*>(other)->value();
166 return new MatrixPy(a - b);
167 }
168
169 PyObject* MatrixPy::number_multiply_handler(PyObject* self, PyObject* other)
170 {
171 if (PyObject_TypeCheck(self, &(MatrixPy::Type))) {
172 Matrix4D a = static_cast<MatrixPy*>(self)->value();
173
174 if (PyObject_TypeCheck(other, &(VectorPy::Type))) {
175 auto b = static_cast<VectorPy*>(other)->value();
176 return new VectorPy(a * b);
177 }
178
179 if (PyObject_TypeCheck(other, &(RotationPy::Type))) {
180 auto r = static_cast<RotationPy*>(other)->value();
181 Matrix4D b;
182 r.getValue(b);
183 return new MatrixPy(a * b);
184 }
185
186 if (PyObject_TypeCheck(other, &(PlacementPy::Type))) {
187 auto b = static_cast<PlacementPy*>(other)->value();
188 return new MatrixPy(a * b.toMatrix());
189 }
190
191 if (PyObject_TypeCheck(other, &(MatrixPy::Type))) {
192 Matrix4D b = static_cast<MatrixPy*>(other)->value();
193 return new MatrixPy(a * b);
194 }
195
196 if (PyNumber_Check(other)) {
197 double v = PyFloat_AsDouble(other);
198 return new MatrixPy(a * v);
199 }
200 }
201
202 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
203 return nullptr;
204 }
205
206 PyObject* MatrixPy::number_power_handler(PyObject* self, PyObject* other, PyObject* arg)
207 {
208 if (!PyObject_TypeCheck(self, &(MatrixPy::Type)) ||
209
210 !PyLong_Check(other) || arg != Py_None) {
211 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
212 return nullptr;
213 }
214
215 Matrix4D a = static_cast<MatrixPy*>(self)->value();
216
217 long b = Py::Long(other);
218 if (b == 0) {
219 return new MatrixPy(Matrix4D());
220 }
221
222 if (b < 0) {
223 if (fabs(a.determinant()) > std::numeric_limits<double>::epsilon()) {
224 a.inverseGauss();
225 }
226 else {
227 PyErr_SetString(PyExc_RuntimeError, "Cannot invert singular matrix");
228 return nullptr;
229 }
230 b = -b;
231 }
232
233 auto res = a;
234 for (--b; b; --b) {
235 res *= a;
236 }
237 return new MatrixPy(res);
238 }
239
240 PyObject* MatrixPy::richCompare(PyObject* v, PyObject* w, int op)
241 {
242 if (PyObject_TypeCheck(v, &(MatrixPy::Type)) && PyObject_TypeCheck(w, &(MatrixPy::Type))) {
243 Matrix4D m1 = static_cast<MatrixPy*>(v)->value();
244 Matrix4D m2 = static_cast<MatrixPy*>(w)->value();
245
246 PyObject* res = nullptr;
247 if (op != Py_EQ && op != Py_NE) {
248 PyErr_SetString(PyExc_TypeError, "no ordering relation is defined for Matrix");
249 return nullptr;
250 }
251 if (op == Py_EQ) {
252 res = (m1 == m2) ? Py_True : Py_False; // NOLINT
253 Py_INCREF(res);
254 return res;
255 }
256
257 res = (m1 != m2) ? Py_True : Py_False; // NOLINT
258 Py_INCREF(res);
259 return res;
260 }
261 // This always returns False
262 Py_INCREF(Py_NotImplemented);
263 return Py_NotImplemented;
264 }
265
266 PyObject* MatrixPy::move(PyObject* args)
267 {
268 double x {};
269 double y {};
270 double z {};
271 Vector3d vec;
272 PyObject* pcVecObj {};
273
274 do { // dummy do..while for cascaded if
275 if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
276 vec.x = x;
277 vec.y = y;
278 vec.z = z;
279 break;
280 }
281 // clears the error from previous PyArg_ParseTuple()
282 PyErr_Clear();
283 if (PyArg_ParseTuple(args, "O!", &PyTuple_Type, &pcVecObj)) {
284 vec = getVectorFromTuple<double>(pcVecObj);
285 break;
286 }
287 // clears the error from previous PyArg_ParseTuple()
288 PyErr_Clear();
289 if (PyArg_ParseTuple(
290 args,
291 "O!;three floats, or a tuple, or a vector is needed",
292 &(VectorPy::Type),
293 &pcVecObj
294 )) {
295 VectorPy* pcObject = static_cast<VectorPy*>(pcVecObj);
296 Vector3d* val = pcObject->getVectorPtr();
297 vec.Set(val->x, val->y, val->z);
298 break;
299 }
300 return nullptr;
301 } while (false);
302
303 PY_TRY
304 {
305 getMatrixPtr()->move(vec);
306 Py_Return;
307 }
308 PY_CATCH;
309 }
310
311 PyObject* MatrixPy::scale(PyObject* args)
312 {
313 double x {};
314 double y {};
315 double z {};
316 Vector3d vec;
317 PyObject* pcVecObj {};
318
319 do { // dummy do..while for cascaded if
320 if (PyArg_ParseTuple(args, "ddd", &x, &y, &z)) {
321 vec.x = x;
322 vec.y = y;
323 vec.z = z;
324 break;
325 }
326 // clears the error from previous PyArg_ParseTuple()
327 PyErr_Clear();
328 if (PyArg_ParseTuple(args, "d", &x)) {
329 vec.x = vec.y = vec.z = x;
330 break;
331 }
332 // clears the error from previous PyArg_ParseTuple()
333 PyErr_Clear();
334 if (PyArg_ParseTuple(args, "O!", &PyTuple_Type, &pcVecObj)) {
335 vec = getVectorFromTuple<double>(pcVecObj);
336 break;
337 }
338 // clears the error from previous PyArg_ParseTuple()
339 PyErr_Clear();
340 if (PyArg_ParseTuple(
341 args,
342 "O!;one or three floats, or a tuple, or a vector is needed",
343 &(VectorPy::Type),
344 &pcVecObj
345 )) {
346 VectorPy* pcObject = static_cast<VectorPy*>(pcVecObj);
347 Vector3d* val = pcObject->getVectorPtr();
348 vec.Set(val->x, val->y, val->z);
349 break;
350 }
351 return nullptr;
352 } while (false);
353
354 PY_TRY
355 {
356 getMatrixPtr()->scale(vec);
357 Py_Return;
358 }
359 PY_CATCH;
360 }
361
362 PyObject* MatrixPy::hasScale(PyObject* args) const
363 {
364 double tol = 0;
365 if (!PyArg_ParseTuple(args, "|d", &tol)) {
366 return nullptr;
367 }
368
369 ScaleType type = getMatrixPtr()->hasScale(tol);
370 Py::Module mod("FreeCAD");
371 return Py::new_reference_to(
372 mod.callMemberFunction("ScaleType", Py::TupleN(Py::Long(static_cast<int>(type))))
373 );
374 }
375
376 PyObject* MatrixPy::decompose(PyObject* args) const
377 {
378 if (!PyArg_ParseTuple(args, "")) {
379 return nullptr;
380 }
381
382 auto ms = getMatrixPtr()->decompose();
383 Py::Tuple tuple(4);
384 for (int i = 0; i < 4; i++) {
385 tuple.setItem(i, Py::Matrix(ms[i]));
386 }
387 return Py::new_reference_to(tuple);
388 }
389
390 PyObject* MatrixPy::nullify()
391 {
392 PY_TRY
393 {
394 getMatrixPtr()->nullify();
395 Py_Return;
396 }
397 PY_CATCH;
398 }
399
400 PyObject* MatrixPy::isNull() const
401 {
402 PY_TRY
403 {
404 bool ok = getMatrixPtr()->isNull();
405 return Py::new_reference_to(Py::Boolean(ok));
406 }
407 PY_CATCH;
408 }
409
410 PyObject* MatrixPy::unity()
411 {
412 PY_TRY
413 {
414 getMatrixPtr()->setToUnity();
415 Py_Return;
416 }
417 PY_CATCH;
418 }
419
420 PyObject* MatrixPy::isUnity(PyObject* args) const
421 {
422 double tol = 0.0;
423 if (!PyArg_ParseTuple(args, "|d", &tol)) {
424 return nullptr;
425 }
426
427 PY_TRY
428 {
429 bool ok = getMatrixPtr()->isUnity(tol);
430 return Py::new_reference_to(Py::Boolean(ok));
431 }
432 PY_CATCH;
433 }
434
435 PyObject* MatrixPy::transform(PyObject* args)
436 {
437 Vector3d vec;
438 Matrix4D mat;
439 PyObject* pcVecObj {};
440 PyObject* pcMatObj {};
441
442 if (!PyArg_ParseTuple(
443 args,
444 "O!O!: a transform point (Vector) and a transform matrix (Matrix) is needed",
445 &(VectorPy::Type),
446 &pcVecObj,
447 &(MatrixPy::Type),
448 &pcMatObj
449 )) {
450 return nullptr;
451 }
452
453 VectorPy* pcObject = static_cast<VectorPy*>(pcVecObj);
454 Vector3d* val = pcObject->getVectorPtr();
455 vec.Set(val->x, val->y, val->z);
456 mat = *(static_cast<MatrixPy*>(pcMatObj)->getMatrixPtr());
457
458 getMatrixPtr()->transform(vec, mat);
459 Py_Return;
460 }
461
462 PyObject* MatrixPy::col(PyObject* args) const
463 {
464 int index {};
465 if (!PyArg_ParseTuple(args, "i", &index)) {
466 return nullptr;
467 }
468
469 if (index < 0 || index > 3) {
470 PyErr_SetString(PyExc_ValueError, "Index expected in the range [0, 3]");
471 return nullptr;
472 }
473
474 Matrix4D* mat = getMatrixPtr();
475 Vector3d v = mat->getCol(index);
476 return Py::new_reference_to(Py::Vector(v));
477 }
478
479 PyObject* MatrixPy::setCol(PyObject* args)
480 {
481 int index {};
482 PyObject* o {};
483 if (!PyArg_ParseTuple(args, "iO!", &index, &(VectorPy::Type), &o)) {
484 return nullptr;
485 }
486
487 if (index < 0 || index > 3) {
488 PyErr_SetString(PyExc_ValueError, "Index expected in the range [0, 3]");
489 return nullptr;
490 }
491
492 Vector3d v = Py::Vector(o, false).toVector();
493 Matrix4D* mat = getMatrixPtr();
494 mat->setCol(index, v);
495 Py_Return;
496 }
497
498 PyObject* MatrixPy::row(PyObject* args) const
499 {
500 int index {};
501 if (!PyArg_ParseTuple(args, "i", &index)) {
502 return nullptr;
503 }
504
505 if (index < 0 || index > 3) {
506 PyErr_SetString(PyExc_ValueError, "Index expected in the range [0, 3]");
507 return nullptr;
508 }
509
510 Matrix4D* mat = getMatrixPtr();
511 Vector3d v = mat->getRow(index);
512 return Py::new_reference_to(Py::Vector(v));
513 }
514
515 PyObject* MatrixPy::setRow(PyObject* args)
516 {
517 int index {};
518 PyObject* o {};
519 if (!PyArg_ParseTuple(args, "iO!", &index, &(VectorPy::Type), &o)) {
520 return nullptr;
521 }
522
523 if (index < 0 || index > 3) {
524 PyErr_SetString(PyExc_ValueError, "Index expected in the range [0, 3]");
525 return nullptr;
526 }
527
528 Vector3d v = Py::Vector(o, false).toVector();
529 Matrix4D* mat = getMatrixPtr();
530 mat->setRow(index, v);
531 Py_Return;
532 }
533
534 PyObject* MatrixPy::diagonal() const
535 {
536 Matrix4D* mat = getMatrixPtr();
537 Vector3d v = mat->diagonal();
538 return Py::new_reference_to(Py::Vector(v));
539 }
540
541 PyObject* MatrixPy::setDiagonal(PyObject* args)
542 {
543 PyObject* o {};
544 if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &o)) {
545 return nullptr;
546 }
547
548 Vector3d v = Py::Vector(o, false).toVector();
549 Matrix4D* mat = getMatrixPtr();
550 mat->setDiagonal(v);
551 Py_Return;
552 }
553
554 PyObject* MatrixPy::rotateX(PyObject* args)
555 {
556 double angle = 0;
557 do {
558 PyObject* object {};
559 if (PyArg_ParseTuple(args, "O!", &(QuantityPy::Type), &object)) {
560 Quantity* q = static_cast<QuantityPy*>(object)->getQuantityPtr();
561 if (q->getUnit() == Unit::Angle) {
562 angle = q->getValueAs(Quantity::Radian);
563 break;
564 }
565 }
566
567 PyErr_Clear();
568 if (PyArg_ParseTuple(args, "d: angle to rotate (double) needed", &angle)) {
569 break;
570 }
571
572 PyErr_SetString(PyExc_TypeError, "For angle either float or Quantity expected");
573 return nullptr;
574 } while (false);
575
576 PY_TRY
577 {
578 getMatrixPtr()->rotX(angle);
579 Py_Return;
580 }
581 PY_CATCH;
582 }
583
584 PyObject* MatrixPy::rotateY(PyObject* args)
585 {
586 double angle = 0;
587 do {
588 PyObject* object {};
589 if (PyArg_ParseTuple(args, "O!", &(QuantityPy::Type), &object)) {
590 Quantity* q = static_cast<QuantityPy*>(object)->getQuantityPtr();
591 if (q->getUnit() == Unit::Angle) {
592 angle = q->getValueAs(Quantity::Radian);
593 break;
594 }
595 }
596
597 PyErr_Clear();
598 if (PyArg_ParseTuple(args, "d: angle to rotate (double) needed", &angle)) {
599 break;
600 }
601
602 PyErr_SetString(PyExc_TypeError, "For angle either float or Quantity expected");
603 return nullptr;
604 } while (false);
605
606 PY_TRY
607 {
608 getMatrixPtr()->rotY(angle);
609 Py_Return;
610 }
611 PY_CATCH;
612 }
613
614 PyObject* MatrixPy::rotateZ(PyObject* args)
615 {
616 double angle = 0;
617 do {
618 PyObject* object {};
619 if (PyArg_ParseTuple(args, "O!", &(QuantityPy::Type), &object)) {
620 Quantity* q = static_cast<QuantityPy*>(object)->getQuantityPtr();
621 if (q->getUnit() == Unit::Angle) {
622 angle = q->getValueAs(Quantity::Radian);
623 break;
624 }
625 }
626
627 PyErr_Clear();
628 if (PyArg_ParseTuple(args, "d: angle to rotate (double) needed", &angle)) {
629 break;
630 }
631
632 PyErr_SetString(PyExc_TypeError, "For angle either float or Quantity expected");
633 return nullptr;
634 } while (false);
635
636 PY_TRY
637 {
638 getMatrixPtr()->rotZ(angle);
639 Py_Return;
640 }
641 PY_CATCH;
642 }
643
644 PyObject* MatrixPy::multiply(PyObject* args) const
645 {
646 PyObject* o {};
647 if (PyArg_ParseTuple(args, "O!", &(MatrixPy::Type), &o)) {
648 Matrix4D mat = (*getMatrixPtr()) * static_cast<MatrixPy*>(o)->value();
649 return new MatrixPy(new Matrix4D(mat));
650 }
651
652 PyErr_Clear();
653 if (PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &o)) {
654 Vector3d vec = (*getMatrixPtr()) * static_cast<VectorPy*>(o)->value();
655 return new VectorPy(new Vector3d(vec));
656 }
657
658 PyErr_SetString(PyExc_TypeError, "either vector or matrix expected");
659 return nullptr;
660 }
661
662 PyObject* MatrixPy::multVec(PyObject* args) const
663 {
664 PyObject* obj {};
665 if (!PyArg_ParseTuple(args, "O!", &(VectorPy::Type), &obj)) {
666 return nullptr;
667 }
668
669 Vector3d vec(static_cast<VectorPy*>(obj)->value());
670 getMatrixPtr()->multVec(vec, vec);
671 return new VectorPy(new Vector3d(vec));
672 }
673
674 PyObject* MatrixPy::invert()
675 {
676 PY_TRY
677 {
678 if (fabs(getMatrixPtr()->determinant()) > std::numeric_limits<double>::epsilon()) {
679 getMatrixPtr()->inverseGauss();
680 Py_Return;
681 }
682
683 PyErr_SetString(PyExc_FC_GeneralError, "Cannot invert singular matrix");
684 return nullptr;
685 }
686 PY_CATCH;
687 }
688
689 PyObject* MatrixPy::inverse() const
690 {
691 PY_TRY
692 {
693 if (fabs(getMatrixPtr()->determinant()) > std::numeric_limits<double>::epsilon()) {
694 Matrix4D m = *getMatrixPtr();
695 m.inverseGauss();
696 return new MatrixPy(m);
697 }
698
699 PyErr_SetString(PyExc_FC_GeneralError, "Cannot invert singular matrix");
700 return nullptr;
701 }
702 PY_CATCH;
703 }
704
705 PyObject* MatrixPy::determinant() const
706 {
707 return PyFloat_FromDouble(getMatrixPtr()->determinant());
708 }
709
710 PyObject* MatrixPy::submatrix(PyObject* args) const
711 {
712 int dim {};
713 if (!PyArg_ParseTuple(args, "i", &dim)) {
714 return nullptr;
715 }
716
717 if (dim < 1 || dim > 4) {
718 PyErr_SetString(PyExc_IndexError, "Dimension out of range");
719 return nullptr;
720 }
721
722 const Matrix4D& mat = *getMatrixPtr();
723 Matrix4D sub;
724 switch (dim) {
725 case 1:
726 sub[0][0] = mat[0][0];
727 break;
728 case 2:
729 sub[0][0] = mat[0][0];
730 sub[0][1] = mat[0][1];
731 sub[1][0] = mat[1][0];
732 sub[1][1] = mat[1][1];
733 break;
734 case 3:
735 sub[0][0] = mat[0][0];
736 sub[0][1] = mat[0][1];
737 sub[0][2] = mat[0][2];
738 sub[1][0] = mat[1][0];
739 sub[1][1] = mat[1][1];
740 sub[1][2] = mat[1][2];
741 sub[2][0] = mat[2][0];
742 sub[2][1] = mat[2][1];
743 sub[2][2] = mat[2][2];
744 break;
745 default:
746 sub = mat;
747 break;
748 }
749
750 return new MatrixPy(sub);
751 }
752
753 PyObject* MatrixPy::isOrthogonal(PyObject* args) const
754 {
755 double eps = 1.0e-06;
756 if (!PyArg_ParseTuple(args, "|d", &eps)) {
757 return nullptr;
758 }
759
760 const Matrix4D& mat = *getMatrixPtr();
761 Matrix4D trp = mat;
762 trp.transpose();
763 trp = trp * mat;
764
765 bool ok = true;
766 double mult = trp[0][0];
767 for (unsigned short i = 0; i < 4 && ok; i++) {
768 for (unsigned short j = 0; j < 4 && ok; j++) {
769 if (i != j) {
770 if (fabs(trp[i][j]) > eps) {
771 ok = false;
772 break;
773 }
774 }
775 else { // the main diagonal
776 if (fabs(trp[i][j] - mult) > eps) {
777 ok = false;
778 break;
779 }
780 }
781 }
782 }
783
784 return Py::new_reference_to(Py::Float(ok ? mult : 0.0));
785 }
786
787 PyObject* MatrixPy::transposed() const
788 {
789 PY_TRY
790 {
791 Matrix4D m = *getMatrixPtr();
792 m.transpose();
793 return new MatrixPy(m);
794 }
795 PY_CATCH;
796 }
797
798 PyObject* MatrixPy::transpose()
799 {
800 PY_TRY
801 {
802 getMatrixPtr()->transpose();
803 Py_Return;
804 }
805 PY_CATCH;
806 }
807
808 PyObject* MatrixPy::analyze() const
809 {
810 PY_TRY
811 {
812 std::string type = getMatrixPtr()->analyse();
813 return PyUnicode_FromString(type.c_str());
814 }
815 PY_CATCH;
816 }
817
818 Py::Float MatrixPy::getA11() const
819 {
820 double val = (*this->getMatrixPtr())[0][0];
821 return Py::Float(val);
822 }
823
824 void MatrixPy::setA11(Py::Float arg)
825 {
826 (*this->getMatrixPtr())[0][0] = static_cast<double>(arg);
827 }
828
829 Py::Float MatrixPy::getA12() const
830 {
831 double val = (*this->getMatrixPtr())[0][1];
832 return Py::Float(val);
833 }
834
835 void MatrixPy::setA12(Py::Float arg)
836 {
837 (*this->getMatrixPtr())[0][1] = static_cast<double>(arg);
838 }
839
840 Py::Float MatrixPy::getA13() const
841 {
842 double val = (*this->getMatrixPtr())[0][2];
843 return Py::Float(val);
844 }
845
846 void MatrixPy::setA13(Py::Float arg)
847 {
848 (*this->getMatrixPtr())[0][2] = static_cast<double>(arg);
849 }
850
851 Py::Float MatrixPy::getA14() const
852 {
853 double val = (*this->getMatrixPtr())[0][3];
854 return Py::Float(val);
855 }
856
857 void MatrixPy::setA14(Py::Float arg)
858 {
859 (*this->getMatrixPtr())[0][3] = static_cast<double>(arg);
860 }
861
862 Py::Float MatrixPy::getA21() const
863 {
864 double val = (*this->getMatrixPtr())[1][0];
865 return Py::Float(val);
866 }
867
868 void MatrixPy::setA21(Py::Float arg)
869 {
870 (*this->getMatrixPtr())[1][0] = static_cast<double>(arg);
871 }
872
873 Py::Float MatrixPy::getA22() const
874 {
875 double val = (*this->getMatrixPtr())[1][1];
876 return Py::Float(val);
877 }
878
879 void MatrixPy::setA22(Py::Float arg)
880 {
881 (*this->getMatrixPtr())[1][1] = static_cast<double>(arg);
882 }
883
884 Py::Float MatrixPy::getA23() const
885 {
886 double val = (*this->getMatrixPtr())[1][2];
887 return Py::Float(val);
888 }
889
890 void MatrixPy::setA23(Py::Float arg)
891 {
892 (*this->getMatrixPtr())[1][2] = static_cast<double>(arg);
893 }
894
895 Py::Float MatrixPy::getA24() const
896 {
897 double val = (*this->getMatrixPtr())[1][3];
898 return Py::Float(val);
899 }
900
901 void MatrixPy::setA24(Py::Float arg)
902 {
903 (*this->getMatrixPtr())[1][3] = static_cast<double>(arg);
904 }
905
906 Py::Float MatrixPy::getA31() const
907 {
908 double val = (*this->getMatrixPtr())[2][0];
909 return Py::Float(val);
910 }
911
912 void MatrixPy::setA31(Py::Float arg)
913 {
914 (*this->getMatrixPtr())[2][0] = static_cast<double>(arg);
915 }
916
917 Py::Float MatrixPy::getA32() const
918 {
919 double val = (*this->getMatrixPtr())[2][1];
920 return Py::Float(val);
921 }
922
923 void MatrixPy::setA32(Py::Float arg)
924 {
925 (*this->getMatrixPtr())[2][1] = static_cast<double>(arg);
926 }
927
928 Py::Float MatrixPy::getA33() const
929 {
930 double val = (*this->getMatrixPtr())[2][2];
931 return Py::Float(val);
932 }
933
934 void MatrixPy::setA33(Py::Float arg)
935 {
936 (*this->getMatrixPtr())[2][2] = static_cast<double>(arg);
937 }
938
939 Py::Float MatrixPy::getA34() const
940 {
941 double val = (*this->getMatrixPtr())[2][3];
942 return Py::Float(val);
943 }
944
945 void MatrixPy::setA34(Py::Float arg)
946 {
947 (*this->getMatrixPtr())[2][3] = static_cast<double>(arg);
948 }
949
950 Py::Float MatrixPy::getA41() const
951 {
952 double val = (*this->getMatrixPtr())[3][0];
953 return Py::Float(val);
954 }
955
956 void MatrixPy::setA41(Py::Float arg)
957 {
958 (*this->getMatrixPtr())[3][0] = static_cast<double>(arg);
959 }
960
961 Py::Float MatrixPy::getA42() const
962 {
963 double val = (*this->getMatrixPtr())[3][1];
964 return Py::Float(val);
965 }
966
967 void MatrixPy::setA42(Py::Float arg)
968 {
969 (*this->getMatrixPtr())[3][1] = static_cast<double>(arg);
970 }
971
972 Py::Float MatrixPy::getA43() const
973 {
974 double val = (*this->getMatrixPtr())[3][2];
975 return Py::Float(val);
976 }
977
978 void MatrixPy::setA43(Py::Float arg)
979 {
980 (*this->getMatrixPtr())[3][2] = static_cast<double>(arg);
981 }
982
983 Py::Float MatrixPy::getA44() const
984 {
985 double val = (*this->getMatrixPtr())[3][3];
986 return Py::Float(val);
987 }
988
989 void MatrixPy::setA44(Py::Float arg)
990 {
991 (*this->getMatrixPtr())[3][3] = static_cast<double>(arg);
992 }
993
994 Py::Sequence MatrixPy::getA() const
995 {
996 double mat[16];
997 this->getMatrixPtr()->getMatrix(mat);
998 Py::Tuple tuple(16);
999 for (int i = 0; i < 16; i++) {
1000 tuple[i] = Py::Float(mat[i]);
1001 }
1002 return tuple;
1003 }
1004
1005 void MatrixPy::setA(Py::Sequence arg)
1006 {
1007 double mat[16];
1008 this->getMatrixPtr()->getMatrix(mat);
1009
1010 int index = 0;
1011 for (Py::Sequence::iterator it = arg.begin(); it != arg.end() && index < 16; ++it) {
1012 mat[index++] = static_cast<double>(Py::Float(*it));
1013 }
1014
1015 this->getMatrixPtr()->setMatrix(mat);
1016 }
1017
1018 PyObject* MatrixPy::getCustomAttributes(const char* /*attr*/) const
1019 {
1020 return nullptr;
1021 }
1022
1023 int MatrixPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
1024 {
1025 return 0;
1026 }
1027
1028 PyObject* MatrixPy::number_divide_handler(PyObject* /*self*/, PyObject* /*other*/)
1029 {
1030 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
1031 return nullptr;
1032 }
1033
1034 PyObject* MatrixPy::number_remainder_handler(PyObject* /*self*/, PyObject* /*other*/)
1035 {
1036 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
1037 return nullptr;
1038 }
1039
1040 PyObject* MatrixPy::number_divmod_handler(PyObject* /*self*/, PyObject* /*other*/)
1041 {
1042 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
1043 return nullptr;
1044 }
1045
1046 PyObject* MatrixPy::number_negative_handler(PyObject* self)
1047 {
1048 if (!PyObject_TypeCheck(self, &(MatrixPy::Type))) {
1049 PyErr_SetString(PyExc_TypeError, "arg must be Matrix");
1050 return nullptr;
1051 }
1052
1053 Matrix4D a = static_cast<MatrixPy*>(self)->value();
1054 return new MatrixPy(a * -1);
1055 }
1056
1057 PyObject* MatrixPy::number_positive_handler(PyObject* self)
1058 {
1059 if (!PyObject_TypeCheck(self, &(MatrixPy::Type))) {
1060 PyErr_SetString(PyExc_TypeError, "arg must be Matrix");
1061 return nullptr;
1062 }
1063
1064 Matrix4D a = static_cast<MatrixPy*>(self)->value();
1065 return new MatrixPy(a);
1066 }
1067
1068 PyObject* MatrixPy::number_absolute_handler(PyObject* /*self*/)
1069 {
1070 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
1071 return nullptr;
1072 }
1073
1074 int MatrixPy::number_nonzero_handler(PyObject* /*self*/)
1075 {
1076 return 1;
1077 }
1078
1079 PyObject* MatrixPy::number_invert_handler(PyObject* /*self*/)
1080 {
1081 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
1082 return nullptr;
1083 }
1084
1085 PyObject* MatrixPy::number_lshift_handler(PyObject* /*self*/, PyObject* /*other*/)
1086 {
1087 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
1088 return nullptr;
1089 }
1090
1091 PyObject* MatrixPy::number_rshift_handler(PyObject* /*self*/, PyObject* /*other*/)
1092 {
1093 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
1094 return nullptr;
1095 }
1096
1097 PyObject* MatrixPy::number_and_handler(PyObject* /*self*/, PyObject* /*other*/)
1098 {
1099 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
1100 return nullptr;
1101 }
1102
1103 PyObject* MatrixPy::number_xor_handler(PyObject* /*self*/, PyObject* /*other*/)
1104 {
1105 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
1106 return nullptr;
1107 }
1108
1109 PyObject* MatrixPy::number_or_handler(PyObject* /*self*/, PyObject* /*other*/)
1110 {
1111 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
1112 return nullptr;
1113 }
1114
1115 PyObject* MatrixPy::number_int_handler(PyObject* /*self*/)
1116 {
1117 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
1118 return nullptr;
1119 }
1120
1121 PyObject* MatrixPy::number_float_handler(PyObject* /*self*/)
1122 {
1123 PyErr_SetString(PyExc_NotImplementedError, "Not implemented");
1124 return nullptr;
1125 }