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
48QT_BEGIN_NAMESPACE
49
50
51template <int N, int M, typename T>
52class QGenericMatrix
53{
54public:
55 QGenericMatrix();
56 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 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
98private:
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};
107template <int N, int M, typename T>
108class QTypeInfo<QGenericMatrix<N, M, T> >
109 : public QTypeInfoMerger<QGenericMatrix<N, M, T>, T>
110{
111#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
112public:
113 enum {
114 isStatic = true,
115 }; // at least Q_RELOCATABLE_TYPE, for BC during Qt 5
116#endif
117};
118
119template <int N, int M, typename T>
120Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix()
121{
122 setToIdentity();
123}
124
125template <int N, int M, typename T>
126Q_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
133template <int N, int M, typename T>
134Q_INLINE_TEMPLATE const T& QGenericMatrix<N, M, T>::operator()(int row, int column) const
135{
136 Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N);
137 return m[column][row];
138}
139
140template <int N, int M, typename T>
141Q_INLINE_TEMPLATE T& QGenericMatrix<N, M, T>::operator()(int row, int column)
142{
143 Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N);
144 return m[column][row];
145}
146
147template <int N, int M, typename T>
148Q_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
164template <int N, int M, typename T>
165Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::setToIdentity()
166{
167 for (int col = 0; col < N; ++col) {
168 for (int row = 0; row < M; ++row) {
169 if (row == col)
170 m[col][row] = 1.0f;
171 else
172 m[col][row] = 0.0f;
173 }
174 }
175}
176
177template <int N, int M, typename T>
178Q_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
185template <int N, int M, typename T>
186Q_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
195template <int N, int M, typename T>
196Q_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
204template <int N, int M, typename T>
205Q_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
213template <int N, int M, typename T>
214Q_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
222QT_WARNING_PUSH
223QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
224QT_WARNING_DISABLE_GCC("-Wfloat-equal")
225QT_WARNING_DISABLE_INTEL(1572)
226
227template <int N, int M, typename T>
228Q_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
238template <int N, int M, typename T>
239Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator!=(const QGenericMatrix<N, M, T>& other) const
240{
241 return !(*this == other);
242}
243
244QT_WARNING_POP
245
246template <int N, int M, typename T>
247Q_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
255template <int N, int M, typename T>
256Q_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
265template <int N, int M, typename T>
266Q_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
275template <int N, int M1, int M2, typename T>
276Q_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
290template <int N, int M, typename T>
291Q_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
300template <int N, int M, typename T>
301Q_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
310template <int N, int M, typename T>
311Q_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
320template <int N, int M, typename T>
321Q_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
330template <int N, int M, typename T>
331Q_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.
339typedef QGenericMatrix<2, 2, float> QMatrix2x2;
340typedef QGenericMatrix<2, 3, float> QMatrix2x3;
341typedef QGenericMatrix<2, 4, float> QMatrix2x4;
342typedef QGenericMatrix<3, 2, float> QMatrix3x2;
343typedef QGenericMatrix<3, 3, float> QMatrix3x3;
344typedef QGenericMatrix<3, 4, float> QMatrix3x4;
345typedef QGenericMatrix<4, 2, float> QMatrix4x2;
346typedef QGenericMatrix<4, 3, float> QMatrix4x3;
347
348#ifndef QT_NO_DEBUG_STREAM
349
350template <int N, int M, typename T>
351QDebug 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(width: 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(width: 0) << ')';
363 return dbg;
364}
365
366#endif
367
368#ifndef QT_NO_DATASTREAM
369
370template <int N, int M, typename T>
371QDataStream &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
379template <int N, int M, typename T>
380QDataStream &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
394QT_END_NAMESPACE
395
396Q_DECLARE_METATYPE(QMatrix2x2)
397Q_DECLARE_METATYPE(QMatrix2x3)
398Q_DECLARE_METATYPE(QMatrix2x4)
399Q_DECLARE_METATYPE(QMatrix3x2)
400Q_DECLARE_METATYPE(QMatrix3x3)
401Q_DECLARE_METATYPE(QMatrix3x4)
402Q_DECLARE_METATYPE(QMatrix4x2)
403Q_DECLARE_METATYPE(QMatrix4x3)
404
405#endif
406

source code of qtbase/src/gui/math3d/qgenericmatrix.h