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 [[nodiscard]] 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};
112
113template <int N, int M, typename T>
114Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix()
115{
116 setToIdentity();
117}
118
119template <int N, int M, typename T>
120Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix(const T *values)
121{
122 for (int col = 0; col < N; ++col)
123 for (int row = 0; row < M; ++row)
124 m[col][row] = values[row * N + col];
125}
126
127template <int N, int M, typename T>
128Q_INLINE_TEMPLATE const T& QGenericMatrix<N, M, T>::operator()(int row, int column) const
129{
130 Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N);
131 return m[column][row];
132}
133
134template <int N, int M, typename T>
135Q_INLINE_TEMPLATE T& QGenericMatrix<N, M, T>::operator()(int row, int column)
136{
137 Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N);
138 return m[column][row];
139}
140
141template <int N, int M, typename T>
142Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::isIdentity() const
143{
144 for (int col = 0; col < N; ++col) {
145 for (int row = 0; row < M; ++row) {
146 if (row == col) {
147 if (m[col][row] != 1.0f)
148 return false;
149 } else {
150 if (m[col][row] != 0.0f)
151 return false;
152 }
153 }
154 }
155 return true;
156}
157
158template <int N, int M, typename T>
159Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::setToIdentity()
160{
161 for (int col = 0; col < N; ++col) {
162 for (int row = 0; row < M; ++row) {
163 if (row == col)
164 m[col][row] = 1.0f;
165 else
166 m[col][row] = 0.0f;
167 }
168 }
169}
170
171template <int N, int M, typename T>
172Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::fill(T value)
173{
174 for (int col = 0; col < N; ++col)
175 for (int row = 0; row < M; ++row)
176 m[col][row] = value;
177}
178
179template <int N, int M, typename T>
180Q_OUTOFLINE_TEMPLATE QGenericMatrix<M, N, T> QGenericMatrix<N, M, T>::transposed() const
181{
182 QGenericMatrix<M, N, T> result(Qt::Uninitialized);
183 for (int row = 0; row < M; ++row)
184 for (int col = 0; col < N; ++col)
185 result.m[row][col] = m[col][row];
186 return result;
187}
188
189template <int N, int M, typename T>
190Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator+=(const QGenericMatrix<N, M, T>& other)
191{
192 for (int row = 0; row < M; ++row)
193 for (int col = 0; col < N; ++col)
194 m[col][row] += other.m[col][row];
195 return *this;
196}
197
198template <int N, int M, typename T>
199Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator-=(const QGenericMatrix<N, M, T>& other)
200{
201 for (int row = 0; row < M; ++row)
202 for (int col = 0; col < N; ++col)
203 m[col][row] -= other.m[col][row];
204 return *this;
205}
206
207template <int N, int M, typename T>
208Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator*=(T factor)
209{
210 for (int row = 0; row < M; ++row)
211 for (int col = 0; col < N; ++col)
212 m[col][row] *= factor;
213 return *this;
214}
215
216QT_WARNING_PUSH
217QT_WARNING_DISABLE_FLOAT_COMPARE
218
219template <int N, int M, typename T>
220Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator==(const QGenericMatrix<N, M, T>& other) const
221{
222 for (int row = 0; row < M; ++row)
223 for (int col = 0; col < N; ++col) {
224 if (m[col][row] != other.m[col][row])
225 return false;
226 }
227 return true;
228}
229
230template <int N, int M, typename T>
231Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator!=(const QGenericMatrix<N, M, T>& other) const
232{
233 return !(*this == other);
234}
235
236QT_WARNING_POP
237
238template <int N, int M, typename T>
239Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator/=(T divisor)
240{
241 for (int row = 0; row < M; ++row)
242 for (int col = 0; col < N; ++col)
243 m[col][row] /= divisor;
244 return *this;
245}
246
247template <int N, int M, typename T>
248Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator+(const QGenericMatrix<N, M, T>& m1, const QGenericMatrix<N, M, T>& m2)
249{
250 QGenericMatrix<N, M, T> result(Qt::Uninitialized);
251 for (int row = 0; row < M; ++row)
252 for (int col = 0; col < N; ++col)
253 result.m[col][row] = m1.m[col][row] + m2.m[col][row];
254 return result;
255}
256
257template <int N, int M, typename T>
258Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator-(const QGenericMatrix<N, M, T>& m1, const QGenericMatrix<N, M, T>& m2)
259{
260 QGenericMatrix<N, M, T> result(Qt::Uninitialized);
261 for (int row = 0; row < M; ++row)
262 for (int col = 0; col < N; ++col)
263 result.m[col][row] = m1.m[col][row] - m2.m[col][row];
264 return result;
265}
266
267template <int N, int M1, int M2, typename T>
268Q_OUTOFLINE_TEMPLATE QGenericMatrix<M1, M2, T> operator*(const QGenericMatrix<N, M2, T>& m1, const QGenericMatrix<M1, N, T>& m2)
269{
270 QGenericMatrix<M1, M2, T> result(Qt::Uninitialized);
271 for (int row = 0; row < M2; ++row) {
272 for (int col = 0; col < M1; ++col) {
273 T sum(0.0f);
274 for (int j = 0; j < N; ++j)
275 sum += m1.m[j][row] * m2.m[col][j];
276 result.m[col][row] = sum;
277 }
278 }
279 return result;
280}
281
282template <int N, int M, typename T>
283Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator-(const QGenericMatrix<N, M, T>& matrix)
284{
285 QGenericMatrix<N, M, T> result(Qt::Uninitialized);
286 for (int row = 0; row < M; ++row)
287 for (int col = 0; col < N; ++col)
288 result.m[col][row] = -matrix.m[col][row];
289 return result;
290}
291
292template <int N, int M, typename T>
293Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator*(T factor, const QGenericMatrix<N, M, T>& matrix)
294{
295 QGenericMatrix<N, M, T> result(Qt::Uninitialized);
296 for (int row = 0; row < M; ++row)
297 for (int col = 0; col < N; ++col)
298 result.m[col][row] = matrix.m[col][row] * factor;
299 return result;
300}
301
302template <int N, int M, typename T>
303Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator*(const QGenericMatrix<N, M, T>& matrix, T factor)
304{
305 QGenericMatrix<N, M, T> result(Qt::Uninitialized);
306 for (int row = 0; row < M; ++row)
307 for (int col = 0; col < N; ++col)
308 result.m[col][row] = matrix.m[col][row] * factor;
309 return result;
310}
311
312template <int N, int M, typename T>
313Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator/(const QGenericMatrix<N, M, T>& matrix, T divisor)
314{
315 QGenericMatrix<N, M, T> result(Qt::Uninitialized);
316 for (int row = 0; row < M; ++row)
317 for (int col = 0; col < N; ++col)
318 result.m[col][row] = matrix.m[col][row] / divisor;
319 return result;
320}
321
322template <int N, int M, typename T>
323Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::copyDataTo(T *values) const
324{
325 for (int col = 0; col < N; ++col)
326 for (int row = 0; row < M; ++row)
327 values[row * N + col] = T(m[col][row]);
328}
329
330// Define aliases for the useful variants of QGenericMatrix.
331typedef QGenericMatrix<2, 2, float> QMatrix2x2;
332typedef QGenericMatrix<2, 3, float> QMatrix2x3;
333typedef QGenericMatrix<2, 4, float> QMatrix2x4;
334typedef QGenericMatrix<3, 2, float> QMatrix3x2;
335typedef QGenericMatrix<3, 3, float> QMatrix3x3;
336typedef QGenericMatrix<3, 4, float> QMatrix3x4;
337typedef QGenericMatrix<4, 2, float> QMatrix4x2;
338typedef QGenericMatrix<4, 3, float> QMatrix4x3;
339
340#ifndef QT_NO_DEBUG_STREAM
341
342template <int N, int M, typename T>
343QDebug operator<<(QDebug dbg, const QGenericMatrix<N, M, T> &m)
344{
345 QDebugStateSaver saver(dbg);
346 dbg.nospace() << "QGenericMatrix<" << N << ", " << M
347 << ", " << QMetaType::fromType<T>().name()
348 << ">(" << Qt::endl << qSetFieldWidth(10);
349 for (int row = 0; row < M; ++row) {
350 for (int col = 0; col < N; ++col)
351 dbg << m(row, col);
352 dbg << Qt::endl;
353 }
354 dbg << qSetFieldWidth(0) << ')';
355 return dbg;
356}
357
358#endif
359
360#ifndef QT_NO_DATASTREAM
361
362template <int N, int M, typename T>
363QDataStream &operator<<(QDataStream &stream, const QGenericMatrix<N, M, T> &matrix)
364{
365 for (int row = 0; row < M; ++row)
366 for (int col = 0; col < N; ++col)
367 stream << double(matrix(row, col));
368 return stream;
369}
370
371template <int N, int M, typename T>
372QDataStream &operator>>(QDataStream &stream, QGenericMatrix<N, M, T> &matrix)
373{
374 double x;
375 for (int row = 0; row < M; ++row) {
376 for (int col = 0; col < N; ++col) {
377 stream >> x;
378 matrix(row, col) = T(x);
379 }
380 }
381 return stream;
382}
383
384#endif
385
386QT_END_NAMESPACE
387
388Q_DECLARE_METATYPE(QMatrix2x2)
389Q_DECLARE_METATYPE(QMatrix2x3)
390Q_DECLARE_METATYPE(QMatrix2x4)
391Q_DECLARE_METATYPE(QMatrix3x2)
392Q_DECLARE_METATYPE(QMatrix3x3)
393Q_DECLARE_METATYPE(QMatrix3x4)
394Q_DECLARE_METATYPE(QMatrix4x2)
395Q_DECLARE_METATYPE(QMatrix4x3)
396
397#endif
398