Debian Codegraph @

src/Base/Matrix.h

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 #ifndef BASE_MATRIX_H
27 #define BASE_MATRIX_H
28
29 #include <array>
30 #include <cmath>
31 #include <string>
32
33 #include "Vector3D.h"
34 #ifndef FC_GLOBAL_H
35 # include <FCGlobal.h>
36 #endif
37
38
39 namespace Base
40 {
41
42 enum class ScaleType
43 {
44 Other = -1,
45 NoScaling = 0,
46 NonUniformRight = 1,
47 NonUniformLeft = 2,
48 Uniform = 3
49 };
50
51 /**
52 * The Matrix4D class.
53 */
54 class BaseExport Matrix4D // NOLINT(cppcoreguidelines-special-member-functions)
55 {
56 using traits_type = float_traits<double>;
57
58 public:
59 /// Default constructor
60 /*!
61 * Initialises to an identity matrix
62 */
63 Matrix4D();
64
65 // clang-format off
66 /// Construction
67 Matrix4D(float a11, float a12, float a13, float a14,
68 float a21, float a22, float a23, float a24,
69 float a31, float a32, float a33, float a34,
70 float a41, float a42, float a43, float a44);
71 /// Construction
72 Matrix4D(double a11, double a12, double a13, double a14,
73 double a21, double a22, double a23, double a24,
74 double a31, double a32, double a33, double a34,
75 double a41, double a42, double a43, double a44);
76 // clang-format on
77 /// Construction
78 Matrix4D(const Matrix4D& mat);
79 /// Construction with an Axis
80 Matrix4D(const Vector3f& rclBase, const Vector3f& rclDir, float fAngle);
81 Matrix4D(const Vector3d& rclBase, const Vector3d& rclDir, double fAngle);
82 /// Destruction
83 ~Matrix4D() = default;
84
85 /** @name Operators */
86 //@{
87 /// Matrix addition
88 inline Matrix4D operator+(const Matrix4D& mat) const;
89 inline Matrix4D& operator+=(const Matrix4D& mat);
90 /// Matrix subtraction
91 inline Matrix4D operator-(const Matrix4D& mat) const;
92 inline Matrix4D& operator-=(const Matrix4D& mat);
93 /// Matrix multiplication
94 inline Matrix4D& operator*=(const Matrix4D& mat);
95 /// Assignment
96 inline Matrix4D& operator=(const Matrix4D& mat);
97 /// Matrix multiplication
98 inline Matrix4D operator*(const Matrix4D& mat) const;
99 /// Multiplication matrix with vector
100 inline Vector3f operator*(const Vector3f& vec) const;
101 inline Vector3d operator*(const Vector3d& vec) const;
102 inline void multVec(const Vector3d& src, Vector3d& dst) const;
103 inline void multVec(const Vector3f& src, Vector3f& dst) const;
104 inline Matrix4D operator*(double scalar) const;
105 inline Matrix4D& operator*=(double scalar);
106 /// Comparison
107 inline bool operator!=(const Matrix4D& mat) const;
108 /// Comparison
109 inline bool operator==(const Matrix4D& mat) const;
110 /// Index operator
111 inline std::array<double, 4>& operator[](unsigned int usNdx);
112 /// Index operator
113 inline const std::array<double, 4>& operator[](unsigned int usNdx) const;
114 /// Get vector of row
115 inline Vector3d getRow(unsigned int usNdx) const;
116 /// Get vector of column
117 inline Vector3d getCol(unsigned int usNdx) const;
118 /// Get vector of diagonal
119 inline Vector3d diagonal() const;
120 /// Get trace of the 3x3 matrix
121 inline double trace3() const;
122 /// Get trace of the 4x4 matrix
123 inline double trace() const;
124 /// Set row to vector
125 inline void setRow(unsigned int usNdx, const Vector3d& vec);
126 /// Set column to vector
127 inline void setCol(unsigned int usNdx, const Vector3d& vec);
128 /// Set diagonal to vector
129 inline void setDiagonal(const Vector3d& vec);
130 /// Compute the determinant of the matrix
131 double determinant() const;
132 /// Compute the determinant of the 3x3 sub-matrix
133 double determinant3() const;
134 /// Analyse the transformation
135 std::string analyse() const;
136 /// Outer product (Dyadic product)
137 Matrix4D& Outer(const Vector3f& rV1, const Vector3f& rV2);
138 Matrix4D& Outer(const Vector3d& rV1, const Vector3d& rV2);
139 /// Hat operator (skew symmetric)
140 Matrix4D& Hat(const Vector3f& rV);
141 Matrix4D& Hat(const Vector3d& rV);
142 //@}
143
144 void getMatrix(double dMtrx[16]) const;
145 void setMatrix(const double dMtrx[16]);
146 /// get the matrix in OpenGL style
147 void getGLMatrix(double dMtrx[16]) const;
148 /// set the matrix in OpenGL style
149 void setGLMatrix(const double dMtrx[16]);
150
151 unsigned long getMemSpace();
152
153 /** @name Manipulation */
154 //@{
155 /// Makes unity matrix
156 void setToUnity();
157 /// Checks if this is the unit matrix
158 bool isUnity() const;
159 /// Checks if this is the unit matrix
160 bool isUnity(double tol) const;
161 /// Makes a null matrix
162 void nullify();
163 /// Checks if this is the null matrix
164 bool isNull() const;
165 /// moves the coordinatesystem for the x,y,z value
166 void move(float x, float y, float z)
167 {
168 move(Vector3f(x, y, z));
169 }
170 void move(double x, double y, double z)
171 {
172 move(Vector3d(x, y, z));
173 }
174 /// moves the coordinatesystem for the vector
175 void move(const Vector3f& vec);
176 void move(const Vector3d& vec);
177 /// scale for the vector
178 void scale(float x, float y, float z)
179 {
180 scale(Vector3f(x, y, z));
181 }
182 void scale(double x, double y, double z)
183 {
184 scale(Vector3d(x, y, z));
185 }
186 /// scale for the x,y,z value
187 void scale(const Vector3f& vec);
188 void scale(const Vector3d& vec);
189 /// uniform scale
190 void scale(float scalexyz)
191 {
192 scale(Vector3f(scalexyz, scalexyz, scalexyz));
193 }
194 void scale(double scalexyz)
195 {
196 scale(Vector3d(scalexyz, scalexyz, scalexyz));
197 }
198 /// Check for scaling factor
199 ScaleType hasScale(double tol = 0.0) const;
200 /// Decompose matrix into pure shear, scale, rotation and move
201 std::array<Matrix4D, 4> decompose() const;
202 /// Rotate around the X axis (in transformed space) for the given value in radians
203 void rotX(double fAngle);
204 /// Rotate around the Y axis (in transformed space) for the given value in radians
205 void rotY(double fAngle);
206 /// Rotate around the Z axis (in transformed space) for the given value in radians
207 void rotZ(double fAngle);
208 /// Rotate around an arbitrary axis passing the origin in radians
209 void rotLine(const Vector3f& vec, float fAngle);
210 /// Rotate around an arbitrary axis passing the origin in radians
211 void rotLine(const Vector3d& vec, double fAngle);
212 /// Rotate around an arbitrary axis that needn't necessarily pass the origin in radians
213 void rotLine(const Vector3f& rclBase, const Vector3f& rclDir, float fAngle);
214 /// Rotate around an arbitrary axis that needn't necessarily pass the origin in radians
215 void rotLine(const Vector3d& rclBase, const Vector3d& rclDir, double fAngle);
216 /// Extract the rotation axis and angle. Therefore the 3x3 submatrix must be orthogonal.
217 bool toAxisAngle(Vector3f& rclBase, Vector3f& rclDir, float& fAngle, float& fTranslation) const;
218 bool toAxisAngle(Vector3d& rclBase, Vector3d& rclDir, double& fAngle, double& fTranslation) const;
219 /// transform (move,scale,rotate) around a point
220 void transform(const Vector3f& vec, const Matrix4D& mat);
221 void transform(const Vector3d& vec, const Matrix4D& mat);
222 /// Matrix is expected to have a 3x3 rotation submatrix.
223 void inverse();
224 /// Matrix is expected to have a 3x3 rotation submatrix.
225 void inverseOrthogonal();
226 /// Arbitrary, non-singular matrix
227 void inverseGauss();
228 void transpose();
229 //@}
230
231 void Print() const;
232 /// write the 16 double of the matrix into a string
233 std::string toString() const;
234 /// read the 16 double of the matrix from a string
235 void fromString(const std::string& str);
236
237 private:
238 using Array2d = std::array<std::array<double, 4>, 4>;
239 Array2d dMtrx4D;
240 };
241
242 inline Matrix4D Matrix4D::operator+(const Matrix4D& mat) const
243 {
244 Matrix4D newMat(*this);
245 return newMat += mat;
246 }
247
248 inline Matrix4D& Matrix4D::operator+=(const Matrix4D& mat)
249 {
250 for (int i = 0; i < 4; i++) {
251 for (int j = 0; j < 4; j++) {
252 dMtrx4D[i][j] += mat[i][j];
253 }
254 }
255
256 return *this;
257 }
258
259 inline Matrix4D Matrix4D::operator-(const Matrix4D& mat) const
260 {
261 Matrix4D newMat(*this);
262 return newMat -= mat;
263 }
264
265 inline Matrix4D& Matrix4D::operator-=(const Matrix4D& mat)
266 {
267 for (int i = 0; i < 4; i++) {
268 for (int j = 0; j < 4; j++) {
269 dMtrx4D[i][j] -= mat[i][j];
270 }
271 }
272
273 return *this;
274 }
275
276 inline Matrix4D& Matrix4D::operator*=(const Matrix4D& mat)
277 {
278 (*this) = (*this) * mat;
279 return *this;
280 }
281
282 inline Matrix4D Matrix4D::operator*(const Matrix4D& mat) const
283 {
284 Matrix4D clMat;
285
286 for (int i = 0; i < 4; i++) {
287 for (int j = 0; j < 4; j++) {
288 clMat.dMtrx4D[i][j] = 0;
289 for (int e = 0; e < 4; e++) {
290 clMat.dMtrx4D[i][j] += dMtrx4D[i][e] * mat.dMtrx4D[e][j];
291 }
292 }
293 }
294
295 return clMat;
296 }
297
298 inline Matrix4D& Matrix4D::operator=(const Matrix4D& mat)
299 {
300 if (this == &mat) {
301 return *this;
302 }
303
304 for (int i = 0; i < 4; i++) {
305 for (int j = 0; j < 4; j++) {
306 dMtrx4D[i][j] = mat.dMtrx4D[i][j];
307 }
308 }
309
310 return *this;
311 }
312
313 inline Vector3f Matrix4D::operator*(const Vector3f& vec) const
314 {
315 Vector3f dst;
316 multVec(vec, dst);
317 return dst;
318 }
319
320 inline Vector3d Matrix4D::operator*(const Vector3d& vec) const
321 {
322 Vector3d dst;
323 multVec(vec, dst);
324 return dst;
325 }
326
327 inline void Matrix4D::multVec(const Vector3d& src, Vector3d& dst) const
328 {
329 // clang-format off
330 double dx = (dMtrx4D[0][0] * src.x + dMtrx4D[0][1] * src.y + dMtrx4D[0][2] * src.z + dMtrx4D[0][3]);
331 double dy = (dMtrx4D[1][0] * src.x + dMtrx4D[1][1] * src.y + dMtrx4D[1][2] * src.z + dMtrx4D[1][3]);
332 double dz = (dMtrx4D[2][0] * src.x + dMtrx4D[2][1] * src.y + dMtrx4D[2][2] * src.z + dMtrx4D[2][3]);
333 dst.Set(dx, dy, dz);
334 // clang-format on
335 }
336
337 inline void Matrix4D::multVec(const Vector3f& src, Vector3f& dst) const
338 {
339 double sx = static_cast<double>(src.x);
340 double sy = static_cast<double>(src.y);
341 double sz = static_cast<double>(src.z);
342
343 double dx = (dMtrx4D[0][0] * sx + dMtrx4D[0][1] * sy + dMtrx4D[0][2] * sz + dMtrx4D[0][3]);
344 double dy = (dMtrx4D[1][0] * sx + dMtrx4D[1][1] * sy + dMtrx4D[1][2] * sz + dMtrx4D[1][3]);
345 double dz = (dMtrx4D[2][0] * sx + dMtrx4D[2][1] * sy + dMtrx4D[2][2] * sz + dMtrx4D[2][3]);
346 dst.Set(static_cast<float>(dx), static_cast<float>(dy), static_cast<float>(dz));
347 }
348
349 inline Matrix4D Matrix4D::operator*(double scalar) const
350 {
351 Matrix4D newMat(*this);
352 return newMat *= scalar;
353 }
354
355 inline Matrix4D& Matrix4D::operator*=(double scalar)
356 {
357 // NOLINTBEGIN
358 for (unsigned int i = 0; i < 4; i++) {
359 for (unsigned int j = 0; j < 4; j++) {
360 dMtrx4D[i][j] *= scalar;
361 }
362 }
363 // NOLINTEND
364 return *this;
365 }
366
367 inline bool Matrix4D::operator==(const Matrix4D& mat) const
368 {
369 for (int i = 0; i < 4; i++) {
370 for (int j = 0; j < 4; j++) {
371 if (fabs(dMtrx4D[i][j] - mat.dMtrx4D[i][j]) > traits_type::epsilon()) {
372 return false;
373 }
374 }
375 }
376
377 return true;
378 }
379
380 inline bool Matrix4D::operator!=(const Matrix4D& mat) const
381 {
382 return !((*this) == mat);
383 }
384
385 inline Vector3f& operator*=(Vector3f& vec, const Matrix4D& mat)
386 {
387 mat.multVec(vec, vec);
388 return vec;
389 }
390
391 inline std::array<double, 4>& Matrix4D::operator[](unsigned int usNdx)
392 {
393 return dMtrx4D[usNdx];
394 }
395
396 inline const std::array<double, 4>& Matrix4D::operator[](unsigned int usNdx) const
397 {
398 return dMtrx4D[usNdx];
399 }
400
401 inline Vector3d Matrix4D::getRow(unsigned int usNdx) const
402 {
403 return Vector3d(dMtrx4D[usNdx][0], dMtrx4D[usNdx][1], dMtrx4D[usNdx][2]);
404 }
405
406 inline Vector3d Matrix4D::getCol(unsigned int usNdx) const
407 {
408 return Vector3d(dMtrx4D[0][usNdx], dMtrx4D[1][usNdx], dMtrx4D[2][usNdx]);
409 }
410
411 inline Vector3d Matrix4D::diagonal() const
412 {
413 return Vector3d(dMtrx4D[0][0], dMtrx4D[1][1], dMtrx4D[2][2]);
414 }
415
416 inline double Matrix4D::trace3() const
417 {
418 return dMtrx4D[0][0] + dMtrx4D[1][1] + dMtrx4D[2][2];
419 }
420
421 inline double Matrix4D::trace() const
422 {
423 return dMtrx4D[0][0] + dMtrx4D[1][1] + dMtrx4D[2][2] + dMtrx4D[3][3];
424 }
425
426 inline void Matrix4D::setRow(unsigned int usNdx, const Vector3d& vec)
427 {
428 dMtrx4D[usNdx][0] = vec.x;
429 dMtrx4D[usNdx][1] = vec.y;
430 dMtrx4D[usNdx][2] = vec.z;
431 }
432
433 inline void Matrix4D::setCol(unsigned int usNdx, const Vector3d& vec)
434 {
435 dMtrx4D[0][usNdx] = vec.x;
436 dMtrx4D[1][usNdx] = vec.y;
437 dMtrx4D[2][usNdx] = vec.z;
438 }
439
440 inline void Matrix4D::setDiagonal(const Vector3d& vec)
441 {
442 dMtrx4D[0][0] = vec.x;
443 dMtrx4D[1][1] = vec.y;
444 dMtrx4D[2][2] = vec.z;
445 }
446
447 } // namespace Base
448
449
450 #endif // BASE_MATRIX_H