/src/qtbase/src/gui/math3d/qgenericmatrix.h
Line | Count | Source (jump to first uncovered line) |
1 | | /**************************************************************************** |
2 | | ** |
3 | | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | | ** Contact: https://www.qt.io/licensing/ |
5 | | ** |
6 | | ** This file is part of the QtGui module of the Qt Toolkit. |
7 | | ** |
8 | | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | | ** Commercial License Usage |
10 | | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | | ** accordance with the commercial license agreement provided with the |
12 | | ** Software or, alternatively, in accordance with the terms contained in |
13 | | ** a written agreement between you and The Qt Company. For licensing terms |
14 | | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | | ** information use the contact form at https://www.qt.io/contact-us. |
16 | | ** |
17 | | ** GNU Lesser General Public License Usage |
18 | | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | | ** General Public License version 3 as published by the Free Software |
20 | | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | | ** packaging of this file. Please review the following information to |
22 | | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | | ** |
25 | | ** GNU General Public License Usage |
26 | | ** Alternatively, this file may be used under the terms of the GNU |
27 | | ** General Public License version 2.0 or (at your option) the GNU General |
28 | | ** Public license version 3 or any later version approved by the KDE Free |
29 | | ** Qt Foundation. The licenses are as published by the Free Software |
30 | | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | | ** included in the packaging of this file. Please review the following |
32 | | ** information to ensure the GNU General Public License requirements will |
33 | | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | | ** |
36 | | ** $QT_END_LICENSE$ |
37 | | ** |
38 | | ****************************************************************************/ |
39 | | |
40 | | #ifndef QGENERICMATRIX_H |
41 | | #define QGENERICMATRIX_H |
42 | | |
43 | | #include <QtGui/qtguiglobal.h> |
44 | | #include <QtCore/qmetatype.h> |
45 | | #include <QtCore/qdebug.h> |
46 | | #include <QtCore/qdatastream.h> |
47 | | |
48 | | QT_BEGIN_NAMESPACE |
49 | | |
50 | | |
51 | | template <int N, int M, typename T> |
52 | | class QGenericMatrix |
53 | | { |
54 | | public: |
55 | | QGenericMatrix(); |
56 | 0 | explicit QGenericMatrix(Qt::Initialization) {} |
57 | | explicit QGenericMatrix(const T *values); |
58 | | |
59 | | const T& operator()(int row, int column) const; |
60 | | T& operator()(int row, int column); |
61 | | |
62 | | bool isIdentity() const; |
63 | | void setToIdentity(); |
64 | | |
65 | | void fill(T value); |
66 | | |
67 | | Q_REQUIRED_RESULT QGenericMatrix<M, N, T> transposed() const; |
68 | | |
69 | | QGenericMatrix<N, M, T>& operator+=(const QGenericMatrix<N, M, T>& other); |
70 | | QGenericMatrix<N, M, T>& operator-=(const QGenericMatrix<N, M, T>& other); |
71 | | QGenericMatrix<N, M, T>& operator*=(T factor); |
72 | | QGenericMatrix<N, M, T>& operator/=(T divisor); |
73 | | bool operator==(const QGenericMatrix<N, M, T>& other) const; |
74 | | bool operator!=(const QGenericMatrix<N, M, T>& other) const; |
75 | | |
76 | | void copyDataTo(T *values) const; |
77 | | |
78 | 0 | T *data() { return *m; } |
79 | | const T *data() const { return *m; } |
80 | | const T *constData() const { return *m; } |
81 | | |
82 | | #if !defined(Q_NO_TEMPLATE_FRIENDS) |
83 | | template<int NN, int MM, typename TT> |
84 | | friend QGenericMatrix<NN, MM, TT> operator+(const QGenericMatrix<NN, MM, TT>& m1, const QGenericMatrix<NN, MM, TT>& m2); |
85 | | template<int NN, int MM, typename TT> |
86 | | friend QGenericMatrix<NN, MM, TT> operator-(const QGenericMatrix<NN, MM, TT>& m1, const QGenericMatrix<NN, MM, TT>& m2); |
87 | | template<int NN, int M1, int M2, typename TT> |
88 | | friend QGenericMatrix<M1, M2, TT> operator*(const QGenericMatrix<NN, M2, TT>& m1, const QGenericMatrix<M1, NN, TT>& m2); |
89 | | template<int NN, int MM, typename TT> |
90 | | friend QGenericMatrix<NN, MM, TT> operator-(const QGenericMatrix<NN, MM, TT>& matrix); |
91 | | template<int NN, int MM, typename TT> |
92 | | friend QGenericMatrix<NN, MM, TT> operator*(TT factor, const QGenericMatrix<NN, MM, TT>& matrix); |
93 | | template<int NN, int MM, typename TT> |
94 | | friend QGenericMatrix<NN, MM, TT> operator*(const QGenericMatrix<NN, MM, TT>& matrix, TT factor); |
95 | | template<int NN, int MM, typename TT> |
96 | | friend QGenericMatrix<NN, MM, TT> operator/(const QGenericMatrix<NN, MM, TT>& matrix, TT divisor); |
97 | | |
98 | | private: |
99 | | #endif |
100 | | T m[N][M]; // Column-major order to match OpenGL. |
101 | | |
102 | | #if !defined(Q_NO_TEMPLATE_FRIENDS) |
103 | | template <int NN, int MM, typename TT> |
104 | | friend class QGenericMatrix; |
105 | | #endif |
106 | | }; |
107 | | template <int N, int M, typename T> |
108 | | class QTypeInfo<QGenericMatrix<N, M, T> > |
109 | | : public QTypeInfoMerger<QGenericMatrix<N, M, T>, T> |
110 | | { |
111 | | #if QT_VERSION < QT_VERSION_CHECK(6,0,0) |
112 | | public: |
113 | | enum { |
114 | | isStatic = true, |
115 | | }; // at least Q_RELOCATABLE_TYPE, for BC during Qt 5 |
116 | | #endif |
117 | | }; |
118 | | |
119 | | template <int N, int M, typename T> |
120 | | Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix() |
121 | 0 | { |
122 | 0 | setToIdentity(); |
123 | 0 | } |
124 | | |
125 | | template <int N, int M, typename T> |
126 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix(const T *values) |
127 | | { |
128 | | for (int col = 0; col < N; ++col) |
129 | | for (int row = 0; row < M; ++row) |
130 | | m[col][row] = values[row * N + col]; |
131 | | } |
132 | | |
133 | | template <int N, int M, typename T> |
134 | | Q_INLINE_TEMPLATE const T& QGenericMatrix<N, M, T>::operator()(int row, int column) const |
135 | 0 | { |
136 | 0 | Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N); |
137 | 0 | return m[column][row]; |
138 | 0 | } |
139 | | |
140 | | template <int N, int M, typename T> |
141 | | Q_INLINE_TEMPLATE T& QGenericMatrix<N, M, T>::operator()(int row, int column) |
142 | 0 | { |
143 | 0 | Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N); |
144 | 0 | return m[column][row]; |
145 | 0 | } |
146 | | |
147 | | template <int N, int M, typename T> |
148 | | Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::isIdentity() const |
149 | | { |
150 | | for (int col = 0; col < N; ++col) { |
151 | | for (int row = 0; row < M; ++row) { |
152 | | if (row == col) { |
153 | | if (m[col][row] != 1.0f) |
154 | | return false; |
155 | | } else { |
156 | | if (m[col][row] != 0.0f) |
157 | | return false; |
158 | | } |
159 | | } |
160 | | } |
161 | | return true; |
162 | | } |
163 | | |
164 | | template <int N, int M, typename T> |
165 | | Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::setToIdentity() |
166 | 0 | { |
167 | 0 | for (int col = 0; col < N; ++col) { |
168 | 0 | for (int row = 0; row < M; ++row) { |
169 | 0 | if (row == col) |
170 | 0 | m[col][row] = 1.0f; |
171 | 0 | else |
172 | 0 | m[col][row] = 0.0f; |
173 | 0 | } |
174 | 0 | } |
175 | 0 | } Unexecuted instantiation: QGenericMatrix<2, 2, float>::setToIdentity() Unexecuted instantiation: QGenericMatrix<2, 3, float>::setToIdentity() Unexecuted instantiation: QGenericMatrix<2, 4, float>::setToIdentity() Unexecuted instantiation: QGenericMatrix<3, 2, float>::setToIdentity() Unexecuted instantiation: QGenericMatrix<3, 3, float>::setToIdentity() Unexecuted instantiation: QGenericMatrix<3, 4, float>::setToIdentity() Unexecuted instantiation: QGenericMatrix<4, 2, float>::setToIdentity() Unexecuted instantiation: QGenericMatrix<4, 3, float>::setToIdentity() |
176 | | |
177 | | template <int N, int M, typename T> |
178 | | Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::fill(T value) |
179 | | { |
180 | | for (int col = 0; col < N; ++col) |
181 | | for (int row = 0; row < M; ++row) |
182 | | m[col][row] = value; |
183 | | } |
184 | | |
185 | | template <int N, int M, typename T> |
186 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<M, N, T> QGenericMatrix<N, M, T>::transposed() const |
187 | | { |
188 | | QGenericMatrix<M, N, T> result(Qt::Uninitialized); |
189 | | for (int row = 0; row < M; ++row) |
190 | | for (int col = 0; col < N; ++col) |
191 | | result.m[row][col] = m[col][row]; |
192 | | return result; |
193 | | } |
194 | | |
195 | | template <int N, int M, typename T> |
196 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator+=(const QGenericMatrix<N, M, T>& other) |
197 | | { |
198 | | for (int row = 0; row < M; ++row) |
199 | | for (int col = 0; col < N; ++col) |
200 | | m[col][row] += other.m[col][row]; |
201 | | return *this; |
202 | | } |
203 | | |
204 | | template <int N, int M, typename T> |
205 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator-=(const QGenericMatrix<N, M, T>& other) |
206 | | { |
207 | | for (int row = 0; row < M; ++row) |
208 | | for (int col = 0; col < N; ++col) |
209 | | m[col][row] -= other.m[col][row]; |
210 | | return *this; |
211 | | } |
212 | | |
213 | | template <int N, int M, typename T> |
214 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator*=(T factor) |
215 | | { |
216 | | for (int row = 0; row < M; ++row) |
217 | | for (int col = 0; col < N; ++col) |
218 | | m[col][row] *= factor; |
219 | | return *this; |
220 | | } |
221 | | |
222 | | QT_WARNING_PUSH |
223 | | QT_WARNING_DISABLE_CLANG("-Wfloat-equal") |
224 | | QT_WARNING_DISABLE_GCC("-Wfloat-equal") |
225 | | QT_WARNING_DISABLE_INTEL(1572) |
226 | | |
227 | | template <int N, int M, typename T> |
228 | | Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator==(const QGenericMatrix<N, M, T>& other) const |
229 | | { |
230 | | for (int row = 0; row < M; ++row) |
231 | | for (int col = 0; col < N; ++col) { |
232 | | if (m[col][row] != other.m[col][row]) |
233 | | return false; |
234 | | } |
235 | | return true; |
236 | | } |
237 | | |
238 | | template <int N, int M, typename T> |
239 | | Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator!=(const QGenericMatrix<N, M, T>& other) const |
240 | | { |
241 | | return !(*this == other); |
242 | | } |
243 | | |
244 | | QT_WARNING_POP |
245 | | |
246 | | template <int N, int M, typename T> |
247 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator/=(T divisor) |
248 | | { |
249 | | for (int row = 0; row < M; ++row) |
250 | | for (int col = 0; col < N; ++col) |
251 | | m[col][row] /= divisor; |
252 | | return *this; |
253 | | } |
254 | | |
255 | | template <int N, int M, typename T> |
256 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator+(const QGenericMatrix<N, M, T>& m1, const QGenericMatrix<N, M, T>& m2) |
257 | | { |
258 | | QGenericMatrix<N, M, T> result(Qt::Uninitialized); |
259 | | for (int row = 0; row < M; ++row) |
260 | | for (int col = 0; col < N; ++col) |
261 | | result.m[col][row] = m1.m[col][row] + m2.m[col][row]; |
262 | | return result; |
263 | | } |
264 | | |
265 | | template <int N, int M, typename T> |
266 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator-(const QGenericMatrix<N, M, T>& m1, const QGenericMatrix<N, M, T>& m2) |
267 | | { |
268 | | QGenericMatrix<N, M, T> result(Qt::Uninitialized); |
269 | | for (int row = 0; row < M; ++row) |
270 | | for (int col = 0; col < N; ++col) |
271 | | result.m[col][row] = m1.m[col][row] - m2.m[col][row]; |
272 | | return result; |
273 | | } |
274 | | |
275 | | template <int N, int M1, int M2, typename T> |
276 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<M1, M2, T> operator*(const QGenericMatrix<N, M2, T>& m1, const QGenericMatrix<M1, N, T>& m2) |
277 | | { |
278 | | QGenericMatrix<M1, M2, T> result(Qt::Uninitialized); |
279 | | for (int row = 0; row < M2; ++row) { |
280 | | for (int col = 0; col < M1; ++col) { |
281 | | T sum(0.0f); |
282 | | for (int j = 0; j < N; ++j) |
283 | | sum += m1.m[j][row] * m2.m[col][j]; |
284 | | result.m[col][row] = sum; |
285 | | } |
286 | | } |
287 | | return result; |
288 | | } |
289 | | |
290 | | template <int N, int M, typename T> |
291 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator-(const QGenericMatrix<N, M, T>& matrix) |
292 | | { |
293 | | QGenericMatrix<N, M, T> result(Qt::Uninitialized); |
294 | | for (int row = 0; row < M; ++row) |
295 | | for (int col = 0; col < N; ++col) |
296 | | result.m[col][row] = -matrix.m[col][row]; |
297 | | return result; |
298 | | } |
299 | | |
300 | | template <int N, int M, typename T> |
301 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator*(T factor, const QGenericMatrix<N, M, T>& matrix) |
302 | | { |
303 | | QGenericMatrix<N, M, T> result(Qt::Uninitialized); |
304 | | for (int row = 0; row < M; ++row) |
305 | | for (int col = 0; col < N; ++col) |
306 | | result.m[col][row] = matrix.m[col][row] * factor; |
307 | | return result; |
308 | | } |
309 | | |
310 | | template <int N, int M, typename T> |
311 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator*(const QGenericMatrix<N, M, T>& matrix, T factor) |
312 | | { |
313 | | QGenericMatrix<N, M, T> result(Qt::Uninitialized); |
314 | | for (int row = 0; row < M; ++row) |
315 | | for (int col = 0; col < N; ++col) |
316 | | result.m[col][row] = matrix.m[col][row] * factor; |
317 | | return result; |
318 | | } |
319 | | |
320 | | template <int N, int M, typename T> |
321 | | Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator/(const QGenericMatrix<N, M, T>& matrix, T divisor) |
322 | | { |
323 | | QGenericMatrix<N, M, T> result(Qt::Uninitialized); |
324 | | for (int row = 0; row < M; ++row) |
325 | | for (int col = 0; col < N; ++col) |
326 | | result.m[col][row] = matrix.m[col][row] / divisor; |
327 | | return result; |
328 | | } |
329 | | |
330 | | template <int N, int M, typename T> |
331 | | Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::copyDataTo(T *values) const |
332 | | { |
333 | | for (int col = 0; col < N; ++col) |
334 | | for (int row = 0; row < M; ++row) |
335 | | values[row * N + col] = T(m[col][row]); |
336 | | } |
337 | | |
338 | | // Define aliases for the useful variants of QGenericMatrix. |
339 | | typedef QGenericMatrix<2, 2, float> QMatrix2x2; |
340 | | typedef QGenericMatrix<2, 3, float> QMatrix2x3; |
341 | | typedef QGenericMatrix<2, 4, float> QMatrix2x4; |
342 | | typedef QGenericMatrix<3, 2, float> QMatrix3x2; |
343 | | typedef QGenericMatrix<3, 3, float> QMatrix3x3; |
344 | | typedef QGenericMatrix<3, 4, float> QMatrix3x4; |
345 | | typedef QGenericMatrix<4, 2, float> QMatrix4x2; |
346 | | typedef QGenericMatrix<4, 3, float> QMatrix4x3; |
347 | | |
348 | | #ifndef QT_NO_DEBUG_STREAM |
349 | | |
350 | | template <int N, int M, typename T> |
351 | | QDebug operator<<(QDebug dbg, const QGenericMatrix<N, M, T> &m) |
352 | | { |
353 | | QDebugStateSaver saver(dbg); |
354 | | dbg.nospace() << "QGenericMatrix<" << N << ", " << M |
355 | | << ", " << QTypeInfo<T>::name() |
356 | | << ">(" << Qt::endl << qSetFieldWidth(10); |
357 | | for (int row = 0; row < M; ++row) { |
358 | | for (int col = 0; col < N; ++col) |
359 | | dbg << m(row, col); |
360 | | dbg << Qt::endl; |
361 | | } |
362 | | dbg << qSetFieldWidth(0) << ')'; |
363 | | return dbg; |
364 | | } |
365 | | |
366 | | #endif |
367 | | |
368 | | #ifndef QT_NO_DATASTREAM |
369 | | |
370 | | template <int N, int M, typename T> |
371 | | QDataStream &operator<<(QDataStream &stream, const QGenericMatrix<N, M, T> &matrix) |
372 | | { |
373 | | for (int row = 0; row < M; ++row) |
374 | | for (int col = 0; col < N; ++col) |
375 | | stream << double(matrix(row, col)); |
376 | | return stream; |
377 | | } |
378 | | |
379 | | template <int N, int M, typename T> |
380 | | QDataStream &operator>>(QDataStream &stream, QGenericMatrix<N, M, T> &matrix) |
381 | | { |
382 | | double x; |
383 | | for (int row = 0; row < M; ++row) { |
384 | | for (int col = 0; col < N; ++col) { |
385 | | stream >> x; |
386 | | matrix(row, col) = T(x); |
387 | | } |
388 | | } |
389 | | return stream; |
390 | | } |
391 | | |
392 | | #endif |
393 | | |
394 | | QT_END_NAMESPACE |
395 | | |
396 | | Q_DECLARE_METATYPE(QMatrix2x2) |
397 | | Q_DECLARE_METATYPE(QMatrix2x3) |
398 | | Q_DECLARE_METATYPE(QMatrix2x4) |
399 | | Q_DECLARE_METATYPE(QMatrix3x2) |
400 | | Q_DECLARE_METATYPE(QMatrix3x3) |
401 | | Q_DECLARE_METATYPE(QMatrix3x4) |
402 | | Q_DECLARE_METATYPE(QMatrix4x2) |
403 | | Q_DECLARE_METATYPE(QMatrix4x3) |
404 | | |
405 | | #endif |