|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 - 2016 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 "../basics.h" |
|
20 #include "../format.h" |
|
21 #include "matrix.h" |
|
22 |
|
23 /** |
|
24 * @brief Matrix::Matrix |
|
25 * Default-constructor for a matrix -- does not actually initialize the member values. |
|
26 */ |
|
27 Matrix::Matrix() {} |
|
28 |
|
29 /** |
|
30 * @brief Matrix::Matrix |
|
31 * @param values |
|
32 * Initializes the matrix from a C array |
|
33 * Note: the array must have (at least) 9 values! |
|
34 */ |
|
35 Matrix::Matrix (double values[]) |
|
36 { |
|
37 for (int i = 0; i < 9; ++i) |
|
38 m_values[i] = values[i]; |
|
39 } |
|
40 |
|
41 /** |
|
42 * @brief Matrix::Matrix |
|
43 * @param fillvalue |
|
44 * Constructs a matrix from a single fill value. |
|
45 */ |
|
46 Matrix::Matrix (double fillvalue) |
|
47 { |
|
48 for (int i = 0; i < 9; ++i) |
|
49 m_values[i] = fillvalue; |
|
50 } |
|
51 |
|
52 /** |
|
53 * @brief Matrix::Matrix |
|
54 * @param values |
|
55 * Constructs a matrix from an initializer list. |
|
56 * Note: the initializer list must have exactly 9 values. |
|
57 */ |
|
58 Matrix::Matrix (const std::initializer_list<double>& values) |
|
59 { |
|
60 if (values.size() == 9) |
|
61 memcpy (&m_values[0], values.begin(), sizeof m_values); |
|
62 } |
|
63 |
|
64 /** |
|
65 * @brief Matrix::dump |
|
66 * Prints the matrix out. |
|
67 */ |
|
68 void Matrix::dump() const |
|
69 { |
|
70 for (int i = 0; i < 3; ++i) |
|
71 { |
|
72 for (int j = 0; j < 3; ++j) |
|
73 print ("%1\t", m_values[i * 3 + j]); |
|
74 |
|
75 print ("\n"); |
|
76 } |
|
77 } |
|
78 |
|
79 /** |
|
80 * @brief Matrix::toString |
|
81 * @return a string representation of the matrix |
|
82 */ |
|
83 QString Matrix::toString() const |
|
84 { |
|
85 QString val; |
|
86 |
|
87 for (int i = 0; i < 9; ++i) |
|
88 { |
|
89 if (i > 0) |
|
90 val += ' '; |
|
91 |
|
92 val += QString::number (m_values[i]); |
|
93 } |
|
94 |
|
95 return val; |
|
96 } |
|
97 |
|
98 /** |
|
99 * @brief Matrix::zero |
|
100 * Fills the matrix with zeros |
|
101 */ |
|
102 void Matrix::zero() |
|
103 { |
|
104 memset (&m_values[0], 0, sizeof m_values); |
|
105 } |
|
106 |
|
107 /** |
|
108 * @brief Matrix::multiply |
|
109 * @param other |
|
110 * @return this matrix multiplied with @c other. |
|
111 * @note @c a.mult(b) is not equivalent to @c b.mult(a) |
|
112 */ |
|
113 Matrix Matrix::multiply (const Matrix& other) const |
|
114 { |
|
115 Matrix result; |
|
116 result.zero(); |
|
117 |
|
118 for (int i = 0; i < 3; ++i) |
|
119 for (int j = 0; j < 3; ++j) |
|
120 for (int k = 0; k < 3; ++k) |
|
121 result[(i * 3) + j] += m_values[(i * 3) + k] * other[(k * 3) + j]; |
|
122 |
|
123 return result; |
|
124 } |
|
125 |
|
126 /** |
|
127 * @brief Matrix::determinant |
|
128 * @return the matrix's determinant |
|
129 */ |
|
130 double Matrix::determinant() const |
|
131 { |
|
132 return (value (0) * value (4) * value (8)) + |
|
133 (value (1) * value (5) * value (6)) + |
|
134 (value (2) * value (3) * value (7)) - |
|
135 (value (2) * value (4) * value (6)) - |
|
136 (value (1) * value (3) * value (8)) - |
|
137 (value (0) * value (5) * value (7)); |
|
138 } |
|
139 |
|
140 /** |
|
141 * @brief Matrix::operator == |
|
142 * @param other |
|
143 * @return whether the two matrices are equal |
|
144 */ |
|
145 bool Matrix::operator==(const Matrix& other) const |
|
146 { |
|
147 for (int i = 0; i < countof(m_values); ++i) |
|
148 { |
|
149 if (not qFuzzyCompare(m_values[i], other.m_values[i])) |
|
150 return false; |
|
151 } |
|
152 return true; |
|
153 } |
|
154 |
|
155 /** |
|
156 * @brief Matrix::operator != |
|
157 * @param other |
|
158 * @return whether the two matrices are not equal |
|
159 */ |
|
160 bool Matrix::operator!=(const Matrix& other) const |
|
161 { |
|
162 return not operator==(other); |
|
163 } |
|
164 |