src/matrix.cpp

changeset 26
3a9e761e4faa
parent 24
1a0faaaceb84
equal deleted inserted replaced
25:6de5ac1fb471 26:3a9e761e4faa
26 {topLeft(1, 0), topLeft(1, 1), topLeft(1, 2), translation.y}, 26 {topLeft(1, 0), topLeft(1, 1), topLeft(1, 2), translation.y},
27 {topLeft(2, 0), topLeft(2, 1), topLeft(2, 2), translation.z}, 27 {topLeft(2, 0), topLeft(2, 1), topLeft(2, 2), translation.z},
28 {0, 0, 0, 1} 28 {0, 0, 0, 1}
29 }}; 29 }};
30 } 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 }

mercurial