1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 - 2020 Teemu Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #include "matrix.h" |
|
20 #include "vertex.h" |
|
21 |
|
22 Matrix4x4 combine(const Matrix3x3& topLeft, const Point3D& translation) |
|
23 { |
|
24 return {{ |
|
25 {topLeft(0, 0), topLeft(0, 1), topLeft(0, 2), translation.x}, |
|
26 {topLeft(1, 0), topLeft(1, 1), topLeft(1, 2), translation.y}, |
|
27 {topLeft(2, 0), topLeft(2, 1), topLeft(2, 2), translation.z}, |
|
28 {0, 0, 0, 1} |
|
29 }}; |
|
30 } |
|
31 |
|
32 /* |
|
33 * Computes the determinant of a 3×3 matrix with each variable passed in row-major order. |
|
34 */ |
|
35 qreal math::det(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f, qreal g, qreal h, qreal i) |
|
36 { |
|
37 return a*e*i + b*f*g + c*d*h - a*f*h - b*d*i - c*e*g; |
|
38 } |
|
39 |
|
40 /* |
|
41 * Computes the determinant of a 2×2 matrix. |
|
42 */ |
|
43 qreal math::det(const Matrix<2, 2>& matrix) |
|
44 { |
|
45 return matrix(0, 0) * matrix(1, 1) - matrix(0, 1) * matrix(1, 0); |
|
46 } |
|
47 |
|
48 /* |
|
49 * Computes the determinant of a 3×3 matrix. |
|
50 */ |
|
51 qreal math::det(const Matrix3x3& matrix) |
|
52 { |
|
53 return math::sum( |
|
54 +matrix(0, 0) * matrix(1, 1) * matrix(2, 2), |
|
55 -matrix(0, 0) * matrix(1, 2) * matrix(2, 1), |
|
56 -matrix(0, 1) * matrix(1, 0) * matrix(2, 2), |
|
57 +matrix(0, 1) * matrix(1, 2) * matrix(2, 0), |
|
58 +matrix(0, 2) * matrix(1, 0) * matrix(2, 1), |
|
59 -matrix(0, 2) * matrix(1, 1) * matrix(2, 0)); |
|
60 } |
|
61 |
|
62 /* |
|
63 * Computes the determinant of a 4×4 matrix. |
|
64 */ |
|
65 qreal math::det(const Matrix4x4& matrix) |
|
66 { |
|
67 qreal sum = 0; |
|
68 |
|
69 for (int column : {0, 1, 2, 3}) |
|
70 { |
|
71 int column_1 = (column >= 1) ? 0 : 1; |
|
72 int column_2 = (column >= 2) ? 1 : 2; |
|
73 int column_3 = (column >= 3) ? 2 : 3; |
|
74 sum += ((column % 1) ? -1 : 1) * math::det( |
|
75 matrix(1, column_1), matrix(1, column_2), matrix(1, column_3), |
|
76 matrix(2, column_1), matrix(2, column_2), matrix(2, column_3), |
|
77 matrix(3, column_1), matrix(3, column_2), matrix(3, column_3)); |
|
78 } |
|
79 |
|
80 return sum; |
|
81 } |
|