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
222template <int N, int M, typename T>
223Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator==(const QGenericMatrix<N, M, T>& other) const
224{
225 for (int row = 0; row < M; ++row)
226 for (int col = 0; col < N; ++col) {
227 if (m[col][row] != other.m[col][row])
228 return false;
229 }
230 return true;
231}
232
233template <int N, int M, typename T>
234Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator!=(const QGenericMatrix<N, M, T>& other) const
235{
236 for (int row = 0; row < M; ++row)
237 for (int col = 0; col < N; ++col) {
238 if (m[col][row] != other.m[col][row])
239 return true;
240 }
241 return false;
242}
243
244template <int N, int M, typename T>
245Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator/=(T divisor)
246{
247 for (int row = 0; row < M; ++row)
248 for (int col = 0; col < N; ++col)
249 m[col][row] /= divisor;
250 return *this;
251}
252
253template <int N, int M, typename T>
254Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator+(const QGenericMatrix<N, M, T>& m1, const QGenericMatrix<N, M, T>& m2)
255{
256 QGenericMatrix<N, M, T> result(Qt::Uninitialized);
257 for (int row = 0; row < M; ++row)
258 for (int col = 0; col < N; ++col)
259 result.m[col][row] = m1.m[col][row] + m2.m[col][row];
260 return result;
261}
262
263template <int N, int M, typename T>
264Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator-(const QGenericMatrix<N, M, T>& m1, const QGenericMatrix<N, M, T>& m2)
265{
266 QGenericMatrix<N, M, T> result(Qt::Uninitialized);
267 for (int row = 0; row < M; ++row)
268 for (int col = 0; col < N; ++col)
269 result.m[col][row] = m1.m[col][row] - m2.m[col][row];
270 return result;
271}
272
273template <int N, int M1, int M2, typename T>
274Q_OUTOFLINE_TEMPLATE QGenericMatrix<M1, M2, T> operator*(const QGenericMatrix<N, M2, T>& m1, const QGenericMatrix<M1, N, T>& m2)
275{
276 QGenericMatrix<M1, M2, T> result(Qt::Uninitialized);
277 for (int row = 0; row < M2; ++row) {
278 for (int col = 0; col < M1; ++col) {
279 T sum(0.0f);
280 for (int j = 0; j < N; ++j)
281 sum += m1.m[j][row] * m2.m[col][j];
282 result.m[col][row] = sum;
283 }
284 }
285 return result;
286}
287
288template <int N, int M, typename T>
289Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator-(const QGenericMatrix<N, M, T>& matrix)
290{
291 QGenericMatrix<N, M, T> result(Qt::Uninitialized);
292 for (int row = 0; row < M; ++row)
293 for (int col = 0; col < N; ++col)
294 result.m[col][row] = -matrix.m[col][row];
295 return result;
296}
297
298template <int N, int M, typename T>
299Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator*(T factor, const QGenericMatrix<N, M, T>& matrix)
300{
301 QGenericMatrix<N, M, T> result(Qt::Uninitialized);
302 for (int row = 0; row < M; ++row)
303 for (int col = 0; col < N; ++col)
304 result.m[col][row] = matrix.m[col][row] * factor;
305 return result;
306}
307
308template <int N, int M, typename T>
309Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator*(const QGenericMatrix<N, M, T>& matrix, T factor)
310{
311 QGenericMatrix<N, M, T> result(Qt::Uninitialized);
312 for (int row = 0; row < M; ++row)
313 for (int col = 0; col < N; ++col)
314 result.m[col][row] = matrix.m[col][row] * factor;
315 return result;
316}
317
318template <int N, int M, typename T>
319Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator/(const QGenericMatrix<N, M, T>& matrix, T divisor)
320{
321 QGenericMatrix<N, M, T> result(Qt::Uninitialized);
322 for (int row = 0; row < M; ++row)
323 for (int col = 0; col < N; ++col)
324 result.m[col][row] = matrix.m[col][row] / divisor;
325 return result;
326}
327
328template <int N, int M, typename T>
329Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::copyDataTo(T *values) const
330{
331 for (int col = 0; col < N; ++col)
332 for (int row = 0; row < M; ++row)
333 values[row * N + col] = T(m[col][row]);
334}
335
336// Define aliases for the useful variants of QGenericMatrix.
337typedef QGenericMatrix<2, 2, float> QMatrix2x2;
338typedef QGenericMatrix<2, 3, float> QMatrix2x3;
339typedef QGenericMatrix<2, 4, float> QMatrix2x4;
340typedef QGenericMatrix<3, 2, float> QMatrix3x2;
341typedef QGenericMatrix<3, 3, float> QMatrix3x3;
342typedef QGenericMatrix<3, 4, float> QMatrix3x4;
343typedef QGenericMatrix<4, 2, float> QMatrix4x2;
344typedef QGenericMatrix<4, 3, float> QMatrix4x3;
345
346#ifndef QT_NO_DEBUG_STREAM
347
348template <int N, int M, typename T>
349QDebug operator<<(QDebug dbg, const QGenericMatrix<N, M, T> &m)
350{
351 QDebugStateSaver saver(dbg);
352 dbg.nospace() << "QGenericMatrix<" << N << ", " << M
353 << ", " << QTypeInfo<T>::name()
354 << ">(" << Qt::endl << qSetFieldWidth(10);
355 for (int row = 0; row < M; ++row) {
356 for (int col = 0; col < N; ++col)
357 dbg << m(row, col);
358 dbg << Qt::endl;
359 }
360 dbg << qSetFieldWidth(0) << ')';
361 return dbg;
362}
363
364#endif
365
366#ifndef QT_NO_DATASTREAM
367
368template <int N, int M, typename T>
369QDataStream &operator<<(QDataStream &stream, const QGenericMatrix<N, M, T> &matrix)
370{
371 for (int row = 0; row < M; ++row)
372 for (int col = 0; col < N; ++col)
373 stream << double(matrix(row, col));
374 return stream;
375}
376
377template <int N, int M, typename T>
378QDataStream &operator>>(QDataStream &stream, QGenericMatrix<N, M, T> &matrix)
379{
380 double x;
381 for (int row = 0; row < M; ++row) {
382 for (int col = 0; col < N; ++col) {
383 stream >> x;
384 matrix(row, col) = T(x);
385 }
386 }
387 return stream;
388}
389
390#endif
391
392QT_END_NAMESPACE
393
394Q_DECLARE_METATYPE(QMatrix2x2)
395Q_DECLARE_METATYPE(QMatrix2x3)
396Q_DECLARE_METATYPE(QMatrix2x4)
397Q_DECLARE_METATYPE(QMatrix3x2)
398Q_DECLARE_METATYPE(QMatrix3x3)
399Q_DECLARE_METATYPE(QMatrix3x4)
400Q_DECLARE_METATYPE(QMatrix4x2)
401Q_DECLARE_METATYPE(QMatrix4x3)
402
403#endif
404