Debian Codegraph @

src/Base/Matrix.cpp

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2005 Imetric 3D GmbH *
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 #include <cstring>
27 #include <sstream>
28
29 #include "Matrix.h"
30 #include "Converter.h"
31
32
33 using namespace Base;
34
35 // clang-format off
36 Matrix4D::Matrix4D()
37 : dMtrx4D {{{1., 0., 0., 0.},
38 {0., 1., 0., 0.},
39 {0., 0., 1., 0.},
40 {0., 0., 0., 1.}}}
41 {}
42
43 Matrix4D::Matrix4D(float a11, float a12, float a13, float a14,
44 float a21, float a22, float a23, float a24,
45 float a31, float a32, float a33, float a34,
46 float a41, float a42, float a43, float a44)
47 : dMtrx4D {{{a11, a12, a13, a14},
48 {a21, a22, a23, a24},
49 {a31, a32, a33, a34},
50 {a41, a42, a43, a44}}}
51 {}
52
53 Matrix4D::Matrix4D(double a11, double a12, double a13, double a14,
54 double a21, double a22, double a23, double a24,
55 double a31, double a32, double a33, double a34,
56 double a41, double a42, double a43, double a44)
57 : dMtrx4D {{{a11, a12, a13, a14},
58 {a21, a22, a23, a24},
59 {a31, a32, a33, a34},
60 {a41, a42, a43, a44}}}
61 {}
62 // clang-format on
63
64 Matrix4D::Matrix4D(const Matrix4D& mat)
65 : Matrix4D()
66 {
67 (*this) = mat;
68 }
69
70 Matrix4D::Matrix4D(const Vector3f& rclBase, const Vector3f& rclDir, float fAngle)
71 : Matrix4D()
72 {
73 this->rotLine(rclBase, rclDir, fAngle);
74 }
75
76 Matrix4D::Matrix4D(const Vector3d& rclBase, const Vector3d& rclDir, double fAngle)
77 : Matrix4D()
78 {
79 this->rotLine(rclBase, rclDir, fAngle);
80 }
81
82 void Matrix4D::setToUnity()
83 {
84 dMtrx4D[0][0] = 1.0;
85 dMtrx4D[0][1] = 0.0;
86 dMtrx4D[0][2] = 0.0;
87 dMtrx4D[0][3] = 0.0;
88 dMtrx4D[1][0] = 0.0;
89 dMtrx4D[1][1] = 1.0;
90 dMtrx4D[1][2] = 0.0;
91 dMtrx4D[1][3] = 0.0;
92 dMtrx4D[2][0] = 0.0;
93 dMtrx4D[2][1] = 0.0;
94 dMtrx4D[2][2] = 1.0;
95 dMtrx4D[2][3] = 0.0;
96 dMtrx4D[3][0] = 0.0;
97 dMtrx4D[3][1] = 0.0;
98 dMtrx4D[3][2] = 0.0;
99 dMtrx4D[3][3] = 1.0;
100 }
101
102 bool Matrix4D::isUnity() const
103 {
104 return isUnity(0.0);
105 }
106
107 bool Matrix4D::isUnity(double tol) const
108 {
109 for (int i = 0; i < 4; i++) {
110 for (int j = 0; j < 4; j++) {
111 if (i == j) {
112 if (fabs(dMtrx4D[i][j] - 1.0) > tol) {
113 return false;
114 }
115 }
116 else {
117 if (fabs(dMtrx4D[i][j]) > tol) {
118 return false;
119 }
120 }
121 }
122 }
123
124 return true;
125 }
126
127 void Matrix4D::nullify()
128 {
129 dMtrx4D[0][0] = 0.0;
130 dMtrx4D[0][1] = 0.0;
131 dMtrx4D[0][2] = 0.0;
132 dMtrx4D[0][3] = 0.0;
133 dMtrx4D[1][0] = 0.0;
134 dMtrx4D[1][1] = 0.0;
135 dMtrx4D[1][2] = 0.0;
136 dMtrx4D[1][3] = 0.0;
137 dMtrx4D[2][0] = 0.0;
138 dMtrx4D[2][1] = 0.0;
139 dMtrx4D[2][2] = 0.0;
140 dMtrx4D[2][3] = 0.0;
141 dMtrx4D[3][0] = 0.0;
142 dMtrx4D[3][1] = 0.0;
143 dMtrx4D[3][2] = 0.0;
144 dMtrx4D[3][3] = 0.0;
145 }
146
147 bool Matrix4D::isNull() const
148 {
149 // NOLINTBEGIN
150 for (int i = 0; i < 4; i++) {
151 for (int j = 0; j < 4; j++) {
152 if (dMtrx4D[i][j] != 0.0) {
153 return false;
154 }
155 }
156 }
157 // NOLINTEND
158
159 return true;
160 }
161
162 double Matrix4D::determinant() const
163 {
164 double fA0 = dMtrx4D[0][0] * dMtrx4D[1][1] - dMtrx4D[0][1] * dMtrx4D[1][0];
165 double fA1 = dMtrx4D[0][0] * dMtrx4D[1][2] - dMtrx4D[0][2] * dMtrx4D[1][0];
166 double fA2 = dMtrx4D[0][0] * dMtrx4D[1][3] - dMtrx4D[0][3] * dMtrx4D[1][0];
167 double fA3 = dMtrx4D[0][1] * dMtrx4D[1][2] - dMtrx4D[0][2] * dMtrx4D[1][1];
168 double fA4 = dMtrx4D[0][1] * dMtrx4D[1][3] - dMtrx4D[0][3] * dMtrx4D[1][1];
169 double fA5 = dMtrx4D[0][2] * dMtrx4D[1][3] - dMtrx4D[0][3] * dMtrx4D[1][2];
170 double fB0 = dMtrx4D[2][0] * dMtrx4D[3][1] - dMtrx4D[2][1] * dMtrx4D[3][0];
171 double fB1 = dMtrx4D[2][0] * dMtrx4D[3][2] - dMtrx4D[2][2] * dMtrx4D[3][0];
172 double fB2 = dMtrx4D[2][0] * dMtrx4D[3][3] - dMtrx4D[2][3] * dMtrx4D[3][0];
173 double fB3 = dMtrx4D[2][1] * dMtrx4D[3][2] - dMtrx4D[2][2] * dMtrx4D[3][1];
174 double fB4 = dMtrx4D[2][1] * dMtrx4D[3][3] - dMtrx4D[2][3] * dMtrx4D[3][1];
175 double fB5 = dMtrx4D[2][2] * dMtrx4D[3][3] - dMtrx4D[2][3] * dMtrx4D[3][2];
176 double fDet = fA0 * fB5 - fA1 * fB4 + fA2 * fB3 + fA3 * fB2 - fA4 * fB1 + fA5 * fB0;
177 return fDet;
178 }
179
180 double Matrix4D::determinant3() const
181 {
182 double va = dMtrx4D[0][0] * dMtrx4D[1][1] * dMtrx4D[2][2];
183 double vb = dMtrx4D[0][1] * dMtrx4D[1][2] * dMtrx4D[2][0];
184 double vc = dMtrx4D[1][0] * dMtrx4D[2][1] * dMtrx4D[0][2];
185 double vd = dMtrx4D[0][2] * dMtrx4D[1][1] * dMtrx4D[2][0];
186 double ve = dMtrx4D[1][0] * dMtrx4D[0][1] * dMtrx4D[2][2];
187 double vf = dMtrx4D[0][0] * dMtrx4D[2][1] * dMtrx4D[1][2];
188 double det = (va + vb + vc) - (vd + ve + vf);
189 return det;
190 }
191
192 void Matrix4D::move(const Vector3f& vec)
193 {
194 move(convertTo<Vector3d>(vec));
195 }
196
197 void Matrix4D::move(const Vector3d& vec)
198 {
199 dMtrx4D[0][3] += vec.x;
200 dMtrx4D[1][3] += vec.y;
201 dMtrx4D[2][3] += vec.z;
202 }
203
204 void Matrix4D::scale(const Vector3f& vec)
205 {
206 scale(convertTo<Vector3d>(vec));
207 }
208
209 void Matrix4D::scale(const Vector3d& vec)
210 {
211 Matrix4D clMat;
212
213 clMat.dMtrx4D[0][0] = vec.x;
214 clMat.dMtrx4D[1][1] = vec.y;
215 clMat.dMtrx4D[2][2] = vec.z;
216 (*this) = clMat * (*this);
217 }
218
219 void Matrix4D::rotX(double fAngle)
220 {
221 Matrix4D clMat;
222 double fsin {};
223 double fcos {};
224
225 fsin = sin(fAngle);
226 fcos = cos(fAngle);
227 clMat.dMtrx4D[1][1] = fcos;
228 clMat.dMtrx4D[2][2] = fcos;
229 clMat.dMtrx4D[1][2] = -fsin;
230 clMat.dMtrx4D[2][1] = fsin;
231
232 (*this) = clMat * (*this);
233 }
234
235 void Matrix4D::rotY(double fAngle)
236 {
237 Matrix4D clMat;
238 double fsin {};
239 double fcos {};
240
241 fsin = sin(fAngle);
242 fcos = cos(fAngle);
243 clMat.dMtrx4D[0][0] = fcos;
244 clMat.dMtrx4D[2][2] = fcos;
245 clMat.dMtrx4D[2][0] = -fsin;
246 clMat.dMtrx4D[0][2] = fsin;
247
248 (*this) = clMat * (*this);
249 }
250
251 void Matrix4D::rotZ(double fAngle)
252 {
253 Matrix4D clMat;
254 double fsin {};
255 double fcos {};
256
257 fsin = sin(fAngle);
258 fcos = cos(fAngle);
259 clMat.dMtrx4D[0][0] = fcos;
260 clMat.dMtrx4D[1][1] = fcos;
261 clMat.dMtrx4D[0][1] = -fsin;
262 clMat.dMtrx4D[1][0] = fsin;
263
264 (*this) = clMat * (*this);
265 }
266
267 void Matrix4D::rotLine(const Vector3d& vec, double fAngle)
268 {
269 // **** algorithm was taken from a math book
270 Matrix4D clMA;
271 Matrix4D clMB;
272 Matrix4D clMC;
273 Matrix4D clMRot;
274 Vector3d clRotAxis(vec);
275 double fcos {};
276 double fsin {};
277
278 // set all entries to "0"
279 clMA.nullify();
280 clMB.nullify();
281 clMC.nullify();
282
283 // ** normalize the rotation axis
284 clRotAxis.Normalize();
285
286 // ** set the rotation matrix (formula taken from a math book) */
287 fcos = cos(fAngle);
288 fsin = sin(fAngle);
289
290 clMA.dMtrx4D[0][0] = (1 - fcos) * clRotAxis.x * clRotAxis.x;
291 clMA.dMtrx4D[0][1] = (1 - fcos) * clRotAxis.x * clRotAxis.y;
292 clMA.dMtrx4D[0][2] = (1 - fcos) * clRotAxis.x * clRotAxis.z;
293 clMA.dMtrx4D[1][0] = (1 - fcos) * clRotAxis.x * clRotAxis.y;
294 clMA.dMtrx4D[1][1] = (1 - fcos) * clRotAxis.y * clRotAxis.y;
295 clMA.dMtrx4D[1][2] = (1 - fcos) * clRotAxis.y * clRotAxis.z;
296 clMA.dMtrx4D[2][0] = (1 - fcos) * clRotAxis.x * clRotAxis.z;
297 clMA.dMtrx4D[2][1] = (1 - fcos) * clRotAxis.y * clRotAxis.z;
298 clMA.dMtrx4D[2][2] = (1 - fcos) * clRotAxis.z * clRotAxis.z;
299
300 clMB.dMtrx4D[0][0] = fcos;
301 clMB.dMtrx4D[1][1] = fcos;
302 clMB.dMtrx4D[2][2] = fcos;
303
304 clMC.dMtrx4D[0][1] = -fsin * clRotAxis.z;
305 clMC.dMtrx4D[0][2] = fsin * clRotAxis.y;
306 clMC.dMtrx4D[1][0] = fsin * clRotAxis.z;
307 clMC.dMtrx4D[1][2] = -fsin * clRotAxis.x;
308 clMC.dMtrx4D[2][0] = -fsin * clRotAxis.y;
309 clMC.dMtrx4D[2][1] = fsin * clRotAxis.x;
310
311 for (int i = 0; i < 3; i++) {
312 for (int j = 0; j < 3; j++) {
313 clMRot.dMtrx4D[i][j] = clMA.dMtrx4D[i][j] + clMB.dMtrx4D[i][j] + clMC.dMtrx4D[i][j];
314 }
315 }
316
317 (*this) = clMRot * (*this);
318 }
319
320 void Matrix4D::rotLine(const Vector3f& vec, float fAngle)
321 {
322 Vector3d tmp = convertTo<Vector3d>(vec);
323 rotLine(tmp, static_cast<double>(fAngle));
324 }
325
326 void Matrix4D::rotLine(const Vector3d& rclBase, const Vector3d& rclDir, double fAngle)
327 {
328 Matrix4D clMRot;
329 clMRot.rotLine(rclDir, fAngle);
330 transform(rclBase, clMRot);
331 }
332
333 void Matrix4D::rotLine(const Vector3f& rclBase, const Vector3f& rclDir, float fAngle)
334 {
335 Vector3d pnt = convertTo<Vector3d>(rclBase);
336 Vector3d dir = convertTo<Vector3d>(rclDir);
337 rotLine(pnt, dir, static_cast<double>(fAngle));
338 }
339
340 /**
341 * If this matrix describes a rotation around an arbitrary axis with a translation (in axis
342 * direction) then the base point of the axis, its direction, the rotation angle and the translation
343 * part get calculated. In this case the return value is set to true, if this matrix doesn't
344 * describe a rotation false is returned.
345 *
346 * The translation vector can be calculated with \a fTranslation * \a rclDir, whereas the length of
347 * \a rclDir is normalized to 1.
348 *
349 * Note: In case the \a fTranslation part is zero then passing \a rclBase, \a rclDir and \a rfAngle
350 * to a new matrix object creates an identical matrix.
351 */
352 bool Matrix4D::toAxisAngle(Vector3f& rclBase, Vector3f& rclDir, float& rfAngle, float& fTranslation) const
353 {
354 Vector3d pnt = convertTo<Vector3d>(rclBase);
355 Vector3d dir = convertTo<Vector3d>(rclDir);
356 double dAngle = static_cast<double>(rfAngle);
357 double dTranslation = static_cast<double>(fTranslation);
358
359 bool ok = toAxisAngle(pnt, dir, dAngle, dTranslation);
360 if (ok) {
361 rclBase = convertTo<Vector3f>(pnt);
362 rclDir = convertTo<Vector3f>(dir);
363 rfAngle = static_cast<float>(dAngle);
364 fTranslation = static_cast<float>(dTranslation);
365 }
366
367 return ok;
368 }
369
370 bool Matrix4D::toAxisAngle(Vector3d& rclBase, Vector3d& rclDir, double& rfAngle, double& fTranslation) const
371 {
372 // First check if the 3x3 submatrix is orthogonal
373 for (int i = 0; i < 3; i++) {
374 // length must be one
375 if (fabs(
376 dMtrx4D[0][i] * dMtrx4D[0][i] + dMtrx4D[1][i] * dMtrx4D[1][i]
377 + dMtrx4D[2][i] * dMtrx4D[2][i] - 1.0
378 )
379 > 0.01) {
380 return false;
381 }
382 // scalar product with other rows must be zero
383 if (fabs(
384 dMtrx4D[0][i] * dMtrx4D[0][(i + 1) % 3] + dMtrx4D[1][i] * dMtrx4D[1][(i + 1) % 3]
385 + dMtrx4D[2][i] * dMtrx4D[2][(i + 1) % 3]
386 )
387 > 0.01) {
388 return false;
389 }
390 }
391
392 // Okay, the 3x3 matrix is orthogonal.
393 // Note: The section to get the rotation axis and angle was taken from WildMagic Library.
394 //
395 // Let (x,y,z) be the unit-length axis and let A be an angle of rotation.
396 // The rotation matrix is R = I + sin(A)*P + (1-cos(A))*P^2 where
397 // I is the identity and
398 //
399 // +- -+
400 // P = | 0 -z +y |
401 // | +z 0 -x |
402 // | -y +x 0 |
403 // +- -+
404 //
405 // If A > 0, R represents a counterclockwise rotation about the axis in
406 // the sense of looking from the tip of the axis vector towards the
407 // origin. Some algebra will show that
408 //
409 // cos(A) = (trace(R)-1)/2 and R - R^t = 2*sin(A)*P
410 //
411 // In the event that A = pi, R-R^t = 0 which prevents us from extracting
412 // the axis through P. Instead note that R = I+2*P^2 when A = pi, so
413 // P^2 = (R-I)/2. The diagonal entries of P^2 are x^2-1, y^2-1, and
414 // z^2-1. We can solve these for axis (x,y,z). Because the angle is pi,
415 // it does not matter which sign you choose on the square roots.
416 //
417 // For more details see also http://www.math.niu.edu/~rusin/known-math/97/rotations
418
419 double fTrace = dMtrx4D[0][0] + dMtrx4D[1][1] + dMtrx4D[2][2];
420 double fCos = 0.5 * (fTrace - 1.0);
421 rfAngle = acos(fCos); // in [0,PI]
422
423 if (rfAngle > 0.0) {
424 if (rfAngle < std::numbers::pi) {
425 rclDir.x = (dMtrx4D[2][1] - dMtrx4D[1][2]);
426 rclDir.y = (dMtrx4D[0][2] - dMtrx4D[2][0]);
427 rclDir.z = (dMtrx4D[1][0] - dMtrx4D[0][1]);
428 rclDir.Normalize();
429 }
430 else {
431 // angle is PI
432 double fHalfInverse {};
433 if (dMtrx4D[0][0] >= dMtrx4D[1][1]) {
434 // r00 >= r11
435 if (dMtrx4D[0][0] >= dMtrx4D[2][2]) {
436 // r00 is maximum diagonal term
437 rclDir.x = (0.5 * sqrt(dMtrx4D[0][0] - dMtrx4D[1][1] - dMtrx4D[2][2] + 1.0));
438 fHalfInverse = 0.5 / rclDir.x;
439 rclDir.y = (fHalfInverse * dMtrx4D[0][1]);
440 rclDir.z = (fHalfInverse * dMtrx4D[0][2]);
441 }
442 else {
443 // r22 is maximum diagonal term
444 rclDir.z = (0.5 * sqrt(dMtrx4D[2][2] - dMtrx4D[0][0] - dMtrx4D[1][1] + 1.0));
445 fHalfInverse = 0.5 / rclDir.z;
446 rclDir.x = (fHalfInverse * dMtrx4D[0][2]);
447 rclDir.y = (fHalfInverse * dMtrx4D[1][2]);
448 }
449 }
450 else {
451 // r11 > r00
452 if (dMtrx4D[1][1] >= dMtrx4D[2][2]) {
453 // r11 is maximum diagonal term
454 rclDir.y = (0.5 * sqrt(dMtrx4D[1][1] - dMtrx4D[0][0] - dMtrx4D[2][2] + 1.0));
455 fHalfInverse = 0.5 / rclDir.y;
456 rclDir.x = (fHalfInverse * dMtrx4D[0][1]);
457 rclDir.z = (fHalfInverse * dMtrx4D[1][2]);
458 }
459 else {
460 // r22 is maximum diagonal term
461 rclDir.z = (0.5 * sqrt(dMtrx4D[2][2] - dMtrx4D[0][0] - dMtrx4D[1][1] + 1.0));
462 fHalfInverse = 0.5 / rclDir.z;
463 rclDir.x = (fHalfInverse * dMtrx4D[0][2]);
464 rclDir.y = (fHalfInverse * dMtrx4D[1][2]);
465 }
466 }
467 }
468 }
469 else {
470 // The angle is 0 and the matrix is the identity. Any axis will
471 // work, so just use the x-axis.
472 rclDir.x = 1.0;
473 rclDir.y = 0.0;
474 rclDir.z = 0.0;
475 rclBase.x = 0.0;
476 rclBase.y = 0.0;
477 rclBase.z = 0.0;
478 }
479
480 // This is the translation part in axis direction
481 fTranslation = (dMtrx4D[0][3] * rclDir.x + dMtrx4D[1][3] * rclDir.y + dMtrx4D[2][3] * rclDir.z);
482 Vector3d cPnt(dMtrx4D[0][3], dMtrx4D[1][3], dMtrx4D[2][3]);
483 cPnt = cPnt - fTranslation * rclDir;
484
485 // This is the base point of the rotation axis
486 if (rfAngle > 0.0) {
487 double factor = 0.5 * (1.0 + fTrace) / sin(rfAngle);
488 rclBase.x = (0.5 * (cPnt.x + factor * (rclDir.y * cPnt.z - rclDir.z * cPnt.y)));
489 rclBase.y = (0.5 * (cPnt.y + factor * (rclDir.z * cPnt.x - rclDir.x * cPnt.z)));
490 rclBase.z = (0.5 * (cPnt.z + factor * (rclDir.x * cPnt.y - rclDir.y * cPnt.x)));
491 }
492
493 return true;
494 }
495
496 void Matrix4D::transform(const Vector3f& vec, const Matrix4D& mat)
497 {
498 move(-vec);
499 (*this) = mat * (*this);
500 move(vec);
501 }
502
503 void Matrix4D::transform(const Vector3d& vec, const Matrix4D& mat)
504 {
505 move(-vec);
506 (*this) = mat * (*this);
507 move(vec);
508 }
509
510 void Matrix4D::inverse()
511 {
512 Matrix4D clInvTrlMat;
513 Matrix4D clInvRotMat;
514
515 /**** Herausnehmen und Inversion der TranslationsMatrix
516 aus der TransformationMatrix ****/
517 for (int i = 0; i < 3; i++) {
518 clInvTrlMat.dMtrx4D[i][3] = -dMtrx4D[i][3];
519 }
520
521 /**** Herausnehmen und Inversion der RotationsMatrix
522 aus der TransformationMatrix ****/
523 for (int i = 0; i < 3; i++) {
524 for (int j = 0; j < 3; j++) {
525 clInvRotMat.dMtrx4D[i][j] = dMtrx4D[j][i];
526 }
527 }
528
529 /**** inv(M) = inv(Mtrl * Mrot) = inv(Mrot) * inv(Mtrl) ****/
530 (*this) = clInvRotMat * clInvTrlMat;
531 }
532
533 using Matrix = double*;
534
535 // NOLINTBEGIN
536 void Matrix_gauss(Matrix a, Matrix b)
537 {
538 int ipiv[4], indxr[4], indxc[4];
539 int i {}, j {}, k {}, l {}, ll {};
540 int irow = 0, icol = 0;
541 double big {}, pivinv {};
542 double dum {};
543 for (j = 0; j < 4; j++) {
544 ipiv[j] = 0;
545 }
546 for (i = 0; i < 4; i++) {
547 big = 0;
548 for (j = 0; j < 4; j++) {
549 if (ipiv[j] != 1) {
550 for (k = 0; k < 4; k++) {
551 if (ipiv[k] == 0) {
552 if (fabs(a[4 * j + k]) >= big) {
553 big = fabs(a[4 * j + k]);
554 irow = j;
555 icol = k;
556 }
557 }
558 else if (ipiv[k] > 1) {
559 return; /* Singular matrix */
560 }
561 }
562 }
563 }
564 ipiv[icol] = ipiv[icol] + 1;
565 if (irow != icol) {
566 for (l = 0; l < 4; l++) {
567 dum = a[4 * irow + l];
568 a[4 * irow + l] = a[4 * icol + l];
569 a[4 * icol + l] = dum;
570 }
571 for (l = 0; l < 4; l++) {
572 dum = b[4 * irow + l];
573 b[4 * irow + l] = b[4 * icol + l];
574 b[4 * icol + l] = dum;
575 }
576 }
577 indxr[i] = irow;
578 indxc[i] = icol;
579 if (a[4 * icol + icol] == 0.0) {
580 return;
581 }
582 pivinv = 1.0 / a[4 * icol + icol];
583 a[4 * icol + icol] = 1.0;
584 for (l = 0; l < 4; l++) {
585 a[4 * icol + l] = a[4 * icol + l] * pivinv;
586 }
587 for (l = 0; l < 4; l++) {
588 b[4 * icol + l] = b[4 * icol + l] * pivinv;
589 }
590 for (ll = 0; ll < 4; ll++) {
591 if (ll != icol) {
592 dum = a[4 * ll + icol];
593 a[4 * ll + icol] = 0;
594 for (l = 0; l < 4; l++) {
595 a[4 * ll + l] = a[4 * ll + l] - a[4 * icol + l] * dum;
596 }
597 for (l = 0; l < 4; l++) {
598 b[4 * ll + l] = b[4 * ll + l] - b[4 * icol + l] * dum;
599 }
600 }
601 }
602 }
603 for (l = 3; l >= 0; l--) {
604 if (indxr[l] != indxc[l]) {
605 for (k = 0; k < 4; k++) {
606 dum = a[4 * k + indxr[l]];
607 a[4 * k + indxr[l]] = a[4 * k + indxc[l]];
608 a[4 * k + indxc[l]] = dum;
609 }
610 }
611 }
612 }
613 // NOLINTEND
614
615 void Matrix4D::inverseOrthogonal()
616 {
617 Base::Vector3d vec(dMtrx4D[0][3], dMtrx4D[1][3], dMtrx4D[2][3]);
618 transpose();
619 multVec(vec, vec);
620 dMtrx4D[0][3] = -vec.x;
621 dMtrx4D[3][0] = 0;
622 dMtrx4D[1][3] = -vec.y;
623 dMtrx4D[3][1] = 0;
624 dMtrx4D[2][3] = -vec.z;
625 dMtrx4D[3][2] = 0;
626 }
627
628 void Matrix4D::inverseGauss()
629 {
630 double matrix[16];
631 // clang-format off
632 double inversematrix[16] = {1, 0, 0, 0,
633 0, 1, 0, 0,
634 0, 0, 1, 0,
635 0, 0, 0, 1};
636 // clang-format on
637 getGLMatrix(matrix);
638
639 Matrix_gauss(matrix, inversematrix);
640
641 setGLMatrix(inversematrix);
642 }
643
644 void Matrix4D::getMatrix(double dMtrx[16]) const
645 {
646 for (int i = 0; i < 4; i++) {
647 for (int j = 0; j < 4; j++) {
648 dMtrx[4 * i + j] = dMtrx4D[i][j];
649 }
650 }
651 }
652
653 void Matrix4D::setMatrix(const double dMtrx[16])
654 {
655 for (int i = 0; i < 4; i++) {
656 for (int j = 0; j < 4; j++) {
657 dMtrx4D[i][j] = dMtrx[4 * i + j];
658 }
659 }
660 }
661
662 void Matrix4D::getGLMatrix(double dMtrx[16]) const
663 {
664 for (int i = 0; i < 4; i++) {
665 for (int j = 0; j < 4; j++) {
666 dMtrx[i + 4 * j] = dMtrx4D[i][j];
667 }
668 }
669 }
670
671 void Matrix4D::setGLMatrix(const double dMtrx[16])
672 {
673 for (int i = 0; i < 4; i++) {
674 for (int j = 0; j < 4; j++) {
675 dMtrx4D[i][j] = dMtrx[i + 4 * j];
676 }
677 }
678 }
679
680 unsigned long Matrix4D::getMemSpace()
681 {
682 return sizeof(Matrix4D);
683 }
684
685 void Matrix4D::Print() const
686 {
687 // NOLINTBEGIN
688 for (int i = 0; i < 4; i++) {
689 printf("%9.3f %9.3f %9.3f %9.3f\n", dMtrx4D[i][0], dMtrx4D[i][1], dMtrx4D[i][2], dMtrx4D[i][3]);
690 }
691 // NOLINTEND
692 }
693
694 void Matrix4D::transpose()
695 {
696 std::swap(dMtrx4D[0][1], dMtrx4D[1][0]);
697 std::swap(dMtrx4D[0][2], dMtrx4D[2][0]);
698 std::swap(dMtrx4D[0][3], dMtrx4D[3][0]);
699 std::swap(dMtrx4D[1][2], dMtrx4D[2][1]);
700 std::swap(dMtrx4D[1][3], dMtrx4D[3][1]);
701 std::swap(dMtrx4D[2][3], dMtrx4D[3][2]);
702 }
703
704
705 // write the 12 double of the matrix in a stream
706 std::string Matrix4D::toString() const
707 {
708 std::stringstream str;
709 // NOLINTBEGIN
710 for (int i = 0; i < 4; i++) {
711 for (int j = 0; j < 4; j++) {
712 str << dMtrx4D[i][j] << " ";
713 }
714 }
715 // NOLINTEND
716
717 return str.str();
718 }
719
720 // read the 12 double of the matrix from a stream
721 void Matrix4D::fromString(const std::string& str)
722 {
723 std::stringstream input;
724 input.str(str);
725
726 // NOLINTBEGIN
727 for (int i = 0; i < 4; i++) {
728 for (int j = 0; j < 4; j++) {
729 input >> dMtrx4D[i][j];
730 }
731 }
732 // NOLINTEND
733 }
734
735 // Analyse the a transformation Matrix and describe the transformation
736 std::string Matrix4D::analyse() const
737 {
738 const double eps = 1.0e-06;
739 bool hastranslation = (dMtrx4D[0][3] != 0.0 || dMtrx4D[1][3] != 0.0 || dMtrx4D[2][3] != 0.0);
740 const Base::Matrix4D unityMatrix = Base::Matrix4D();
741 std::string text;
742 if (*this == unityMatrix) {
743 text = "Unity Matrix";
744 }
745 else {
746 if (dMtrx4D[3][0] != 0.0 || dMtrx4D[3][1] != 0.0 || dMtrx4D[3][2] != 0.0
747 || dMtrx4D[3][3] != 1.0) {
748 text = "Projection";
749 }
750 else // translation and affine
751 {
752 if (dMtrx4D[0][1] == 0.0 && dMtrx4D[0][2] == 0.0 && dMtrx4D[1][0] == 0.0
753 && dMtrx4D[1][2] == 0.0 && dMtrx4D[2][0] == 0.0 && dMtrx4D[2][1] == 0.0) // scaling
754 {
755 std::ostringstream stringStream;
756 stringStream << "Scale [" << dMtrx4D[0][0] << ", " << dMtrx4D[1][1] << ", "
757 << dMtrx4D[2][2] << "]";
758 text = stringStream.str();
759 }
760 else {
761 Base::Matrix4D sub;
762 sub[0][0] = dMtrx4D[0][0];
763 sub[0][1] = dMtrx4D[0][1];
764 sub[0][2] = dMtrx4D[0][2];
765 sub[1][0] = dMtrx4D[1][0];
766 sub[1][1] = dMtrx4D[1][1];
767 sub[1][2] = dMtrx4D[1][2];
768 sub[2][0] = dMtrx4D[2][0];
769 sub[2][1] = dMtrx4D[2][1];
770 sub[2][2] = dMtrx4D[2][2];
771
772 Base::Matrix4D trp = sub;
773 trp.transpose();
774 trp = trp * sub;
775 bool ortho = true;
776 for (unsigned int i = 0; i < 4 && ortho; i++) {
777 for (unsigned int j = 0; j < 4 && ortho; j++) {
778 if (i != j) {
779 if (fabs(trp[i][j]) > eps) {
780 ortho = false;
781 break;
782 }
783 }
784 }
785 }
786
787 double determinant = sub.determinant();
788 if (ortho) {
789 if (fabs(determinant - 1.0) < eps) // rotation
790 {
791 text = "Rotation Matrix";
792 }
793 else {
794 if (fabs(determinant + 1.0) < eps) // rotation
795 {
796 text = "Rotinversion Matrix";
797 }
798 else // scaling with rotation
799 {
800 std::ostringstream stringStream;
801 stringStream << "Scale and Rotate ";
802 if (determinant < 0.0) {
803 stringStream << "and Invert ";
804 }
805 stringStream << "[ " << sqrt(trp[0][0]) << ", " << sqrt(trp[1][1])
806 << ", " << sqrt(trp[2][2]) << "]";
807 text = stringStream.str();
808 }
809 }
810 }
811 else {
812 std::ostringstream stringStream;
813 stringStream << "Affine with det= " << determinant;
814 text = stringStream.str();
815 }
816 }
817 }
818 if (hastranslation) {
819 text += " with Translation";
820 }
821 }
822 return text;
823 }
824
825 Matrix4D& Matrix4D::Outer(const Vector3f& rV1, const Vector3f& rV2)
826 {
827 setToUnity();
828
829 Outer(convertTo<Vector3d>(rV1), convertTo<Vector3d>(rV2));
830
831 return *this;
832 }
833
834 Matrix4D& Matrix4D::Outer(const Vector3d& rV1, const Vector3d& rV2)
835 {
836 setToUnity();
837
838 dMtrx4D[0][0] = rV1.x * rV2.x;
839 dMtrx4D[0][1] = rV1.x * rV2.y;
840 dMtrx4D[0][2] = rV1.x * rV2.z;
841
842 dMtrx4D[1][0] = rV1.y * rV2.x;
843 dMtrx4D[1][1] = rV1.y * rV2.y;
844 dMtrx4D[1][2] = rV1.y * rV2.z;
845
846 dMtrx4D[2][0] = rV1.z * rV2.x;
847 dMtrx4D[2][1] = rV1.z * rV2.y;
848 dMtrx4D[2][2] = rV1.z * rV2.z;
849
850 return *this;
851 }
852
853 Matrix4D& Matrix4D::Hat(const Vector3f& rV)
854 {
855 setToUnity();
856
857 Hat(convertTo<Vector3d>(rV));
858
859 return *this;
860 }
861
862 Matrix4D& Matrix4D::Hat(const Vector3d& rV)
863 {
864 setToUnity();
865
866 dMtrx4D[0][0] = 0.0;
867 dMtrx4D[0][1] = -rV.z;
868 dMtrx4D[0][2] = rV.y;
869
870 dMtrx4D[1][0] = rV.z;
871 dMtrx4D[1][1] = 0.0;
872 dMtrx4D[1][2] = -rV.x;
873
874 dMtrx4D[2][0] = -rV.y;
875 dMtrx4D[2][1] = rV.x;
876 dMtrx4D[2][2] = 0.0;
877
878 return *this;
879 }
880
881 ScaleType Matrix4D::hasScale(double tol) const
882 {
883 const double defaultTolerance = 1e-9;
884 // check for uniform scaling
885 //
886 // For a scaled rotation matrix it matters whether
887 // the scaling was applied from the left or right side.
888 // Only in case of uniform scaling it doesn't make a difference.
889 if (tol == 0.0) {
890 tol = defaultTolerance;
891 }
892
893 // check if the absolute values are proportionally close or equal
894 auto closeAbs = [&](double val_a, double val_b) {
895 double abs_a = fabs(val_a);
896 double abs_b = fabs(val_b);
897 if (abs_b > abs_a) {
898 return (abs_b - abs_a) / abs_b <= tol;
899 }
900 if (abs_a > abs_b) {
901 return (abs_a - abs_b) / abs_a <= tol;
902 }
903 return true;
904 };
905
906 // get column vectors
907 double dx = getCol(0).Sqr();
908 double dy = getCol(1).Sqr();
909 double dz = getCol(2).Sqr();
910 double dxyz = sqrt(dx * dy * dz);
911
912 // get row vectors
913 double du = getRow(0).Sqr();
914 double dv = getRow(1).Sqr();
915 double dw = getRow(2).Sqr();
916 double duvw = sqrt(du * dv * dw);
917
918 double d3 = determinant3();
919
920 // This could be e.g. a projection, a shearing,... matrix
921 if (!closeAbs(dxyz, d3) && !closeAbs(duvw, d3)) {
922 return ScaleType::Other;
923 }
924
925 if (closeAbs(duvw, d3) && (!closeAbs(du, dv) || !closeAbs(dv, dw))) {
926 return ScaleType::NonUniformLeft;
927 }
928
929 if (closeAbs(dxyz, d3) && (!closeAbs(dx, dy) || !closeAbs(dy, dz))) {
930 return ScaleType::NonUniformRight;
931 }
932
933 if (fabs(d3 - 1.0) > tol) {
934 return ScaleType::Uniform;
935 }
936
937 return ScaleType::NoScaling;
938 }
939
940 std::array<Matrix4D, 4> Matrix4D::decompose() const
941 {
942 // decompose the matrix to shear, scale, rotation and move
943 // so that matrix = move * rotation * scale * shear
944 // return an array of matrices
945 Matrix4D moveMatrix;
946 Matrix4D rotationMatrix;
947 Matrix4D scaleMatrix;
948 Matrix4D residualMatrix(*this);
949 // extract transform
950 moveMatrix.move(residualMatrix.getCol(3));
951 residualMatrix.setCol(3, Vector3d());
952 // find and extract rotation
953 int prim_dir = -1;
954 std::array<Vector3d, 3> dirs = {Vector3d(1., 0., 0.), Vector3d(0., 1., 0.), Vector3d(0., 0., 1.)};
955 for (int i = 0; i < 3; i++) {
956 if (residualMatrix.getCol(i).IsNull()) {
957 continue;
958 }
959 if (prim_dir < 0) {
960 dirs[i] = residualMatrix.getCol(i);
961 dirs[i].Normalize();
962 prim_dir = i;
963 continue;
964 }
965
966 Vector3d cross = dirs[prim_dir].Cross(residualMatrix.getCol(i));
967 if (cross.IsNull()) {
968 continue;
969 }
970 cross.Normalize();
971 int last_dir = 3 - i - prim_dir;
972 if (i - prim_dir == 1) {
973 dirs[last_dir] = cross;
974 dirs[i] = cross.Cross(dirs[prim_dir]);
975 }
976 else {
977 dirs[last_dir] = -cross;
978 dirs[i] = dirs[prim_dir].Cross(-cross);
979 }
980 prim_dir = -2; // done
981 break;
982 }
983 if (prim_dir >= 0) {
984 // handle case with only one valid direction
985 Vector3d cross = dirs[prim_dir].Cross(Vector3d(0., 0., 1.));
986 if (cross.IsNull()) {
987 cross = dirs[prim_dir].Cross(Vector3d(0., 1., 0.));
988 }
989 dirs[(prim_dir + 1) % 3] = cross;
990 dirs[(prim_dir + 2) % 3] = dirs[prim_dir].Cross(cross);
991 }
992 rotationMatrix.setCol(0, dirs[0]);
993 rotationMatrix.setCol(1, dirs[1]);
994 rotationMatrix.setCol(2, dirs[2]);
995 rotationMatrix.inverseGauss();
996 residualMatrix = rotationMatrix * residualMatrix;
997 // To keep signs of the scale factors equal
998 if (residualMatrix.determinant() < 0) {
999 rotationMatrix.rotZ(std::numbers::pi);
1000 residualMatrix.rotZ(std::numbers::pi);
1001 }
1002 rotationMatrix.inverseGauss();
1003 // extract scale
1004 double xScale = residualMatrix.dMtrx4D[0][0];
1005 double yScale = residualMatrix.dMtrx4D[1][1];
1006 double zScale = residualMatrix.dMtrx4D[2][2];
1007 scaleMatrix.dMtrx4D[0][0] = xScale;
1008 scaleMatrix.dMtrx4D[1][1] = yScale;
1009 scaleMatrix.dMtrx4D[2][2] = zScale;
1010 // The remaining shear
1011 residualMatrix.scale(
1012 xScale != 0 ? 1.0 / xScale : 1.0,
1013 yScale != 0 ? 1.0 / yScale : 1.0,
1014 zScale != 0 ? 1.0 / zScale : 1.0
1015 );
1016 // Restore trace in shear matrix
1017 residualMatrix.setDiagonal(Vector3d(1.0, 1.0, 1.0));
1018 // Remove values close to zero
1019 for (int i = 0; i < 3; i++) {
1020 if (std::abs(scaleMatrix.dMtrx4D[i][i]) < 1e-15) {
1021 scaleMatrix.dMtrx4D[i][i] = 0.0;
1022 }
1023 for (int j = 0; j < 3; j++) {
1024 if (std::abs(residualMatrix.dMtrx4D[i][j]) < 1e-15) {
1025 residualMatrix.dMtrx4D[i][j] = 0.0;
1026 }
1027 if (std::abs(rotationMatrix.dMtrx4D[i][j]) < 1e-15) {
1028 rotationMatrix.dMtrx4D[i][j] = 0.0;
1029 }
1030 }
1031 }
1032 return std::array<Matrix4D, 4> {residualMatrix, scaleMatrix, rotationMatrix, moveMatrix};
1033 }