1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtGui module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #ifndef QGENERICMATRIX_H
43 #define QGENERICMATRIX_H
45 #include <QtCore/qmetatype.h>
46 #include <QtCore/qdebug.h>
47 #include <QtCore/qdatastream.h>
54 template <int N, int M, typename T>
59 QGenericMatrix(const QGenericMatrix<N, M, T>& other);
60 explicit QGenericMatrix(const T *values);
62 const T& operator()(int row, int column) const;
63 T& operator()(int row, int column);
65 bool isIdentity() const;
70 QGenericMatrix<M, N, T> transposed() const;
72 QGenericMatrix<N, M, T>& operator+=(const QGenericMatrix<N, M, T>& other);
73 QGenericMatrix<N, M, T>& operator-=(const QGenericMatrix<N, M, T>& other);
74 QGenericMatrix<N, M, T>& operator*=(T factor);
75 QGenericMatrix<N, M, T>& operator/=(T divisor);
76 bool operator==(const QGenericMatrix<N, M, T>& other) const;
77 bool operator!=(const QGenericMatrix<N, M, T>& other) const;
79 void copyDataTo(T *values) const;
81 T *data() { return *m; }
82 const T *data() const { return *m; }
83 const T *constData() const { return *m; }
85 #if !defined(Q_NO_TEMPLATE_FRIENDS)
86 template<int NN, int MM, typename TT>
87 friend QGenericMatrix<NN, MM, TT> operator+(const QGenericMatrix<NN, MM, TT>& m1, const QGenericMatrix<NN, MM, TT>& m2);
88 template<int NN, int MM, typename TT>
89 friend QGenericMatrix<NN, MM, TT> operator-(const QGenericMatrix<NN, MM, TT>& m1, const QGenericMatrix<NN, MM, TT>& m2);
90 template<int NN, int M1, int M2, typename TT>
91 friend QGenericMatrix<M1, M2, TT> operator*(const QGenericMatrix<NN, M2, TT>& m1, const QGenericMatrix<M1, NN, TT>& m2);
92 template<int NN, int MM, typename TT>
93 friend QGenericMatrix<NN, MM, TT> operator-(const QGenericMatrix<NN, MM, TT>& matrix);
94 template<int NN, int MM, typename TT>
95 friend QGenericMatrix<NN, MM, TT> operator*(TT factor, const QGenericMatrix<NN, MM, TT>& matrix);
96 template<int NN, int MM, typename TT>
97 friend QGenericMatrix<NN, MM, TT> operator*(const QGenericMatrix<NN, MM, TT>& matrix, TT factor);
98 template<int NN, int MM, typename TT>
99 friend QGenericMatrix<NN, MM, TT> operator/(const QGenericMatrix<NN, MM, TT>& matrix, TT divisor);
103 T m[N][M]; // Column-major order to match OpenGL.
105 explicit QGenericMatrix(int) {} // Construct without initializing identity matrix.
107 #if !defined(Q_NO_TEMPLATE_FRIENDS)
108 template <int NN, int MM, typename TT>
109 friend class QGenericMatrix;
113 template <int N, int M, typename T>
114 Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix()
119 template <int N, int M, typename T>
120 Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix(const QGenericMatrix<N, M, T>& other)
122 for (int col = 0; col < N; ++col)
123 for (int row = 0; row < M; ++row)
124 m[col][row] = other.m[col][row];
127 template <int N, int M, typename T>
128 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix(const T *values)
130 for (int col = 0; col < N; ++col)
131 for (int row = 0; row < M; ++row)
132 m[col][row] = values[row * N + col];
135 template <int N, int M, typename T>
136 Q_INLINE_TEMPLATE const T& QGenericMatrix<N, M, T>::operator()(int row, int column) const
138 Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N);
139 return m[column][row];
142 template <int N, int M, typename T>
143 Q_INLINE_TEMPLATE T& QGenericMatrix<N, M, T>::operator()(int row, int column)
145 Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N);
146 return m[column][row];
149 template <int N, int M, typename T>
150 Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::isIdentity() const
152 for (int col = 0; col < N; ++col) {
153 for (int row = 0; row < M; ++row) {
155 if (m[col][row] != 1.0f)
158 if (m[col][row] != 0.0f)
166 template <int N, int M, typename T>
167 Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::setToIdentity()
169 for (int col = 0; col < N; ++col) {
170 for (int row = 0; row < M; ++row) {
179 template <int N, int M, typename T>
180 Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::fill(T value)
182 for (int col = 0; col < N; ++col)
183 for (int row = 0; row < M; ++row)
187 template <int N, int M, typename T>
188 Q_OUTOFLINE_TEMPLATE QGenericMatrix<M, N, T> QGenericMatrix<N, M, T>::transposed() const
190 QGenericMatrix<M, N, T> result(1);
191 for (int row = 0; row < M; ++row)
192 for (int col = 0; col < N; ++col)
193 result.m[row][col] = m[col][row];
197 template <int N, int M, typename T>
198 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator+=(const QGenericMatrix<N, M, T>& other)
200 for (int row = 0; row < M; ++row)
201 for (int col = 0; col < N; ++col)
202 m[col][row] += other.m[col][row];
206 template <int N, int M, typename T>
207 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator-=(const QGenericMatrix<N, M, T>& other)
209 for (int row = 0; row < M; ++row)
210 for (int col = 0; col < N; ++col)
211 m[col][row] -= other.m[col][row];
215 template <int N, int M, typename T>
216 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator*=(T factor)
218 for (int row = 0; row < M; ++row)
219 for (int col = 0; col < N; ++col)
220 m[col][row] *= factor;
224 template <int N, int M, typename T>
225 Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator==(const QGenericMatrix<N, M, T>& other) const
227 for (int row = 0; row < M; ++row)
228 for (int col = 0; col < N; ++col) {
229 if (m[col][row] != other.m[col][row])
235 template <int N, int M, typename T>
236 Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator!=(const QGenericMatrix<N, M, T>& other) const
238 for (int row = 0; row < M; ++row)
239 for (int col = 0; col < N; ++col) {
240 if (m[col][row] != other.m[col][row])
246 template <int N, int M, typename T>
247 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator/=(T divisor)
249 for (int row = 0; row < M; ++row)
250 for (int col = 0; col < N; ++col)
251 m[col][row] /= divisor;
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)
258 QGenericMatrix<N, M, T> result(1);
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];
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)
268 QGenericMatrix<N, M, T> result(1);
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];
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)
278 QGenericMatrix<M1, M2, T> result(1);
279 for (int row = 0; row < M2; ++row) {
280 for (int col = 0; col < M1; ++col) {
282 for (int j = 0; j < N; ++j)
283 sum += m1.m[j][row] * m2.m[col][j];
284 result.m[col][row] = sum;
290 template <int N, int M, typename T>
291 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator-(const QGenericMatrix<N, M, T>& matrix)
293 QGenericMatrix<N, M, T> result(1);
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];
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)
303 QGenericMatrix<N, M, T> result(1);
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;
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)
313 QGenericMatrix<N, M, T> result(1);
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;
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)
323 QGenericMatrix<N, M, T> result(1);
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;
330 template <int N, int M, typename T>
331 Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::copyDataTo(T *values) const
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]);
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;
348 #ifndef QT_NO_DEBUG_STREAM
350 template <int N, int M, typename T>
351 QDebug operator<<(QDebug dbg, const QGenericMatrix<N, M, T> &m)
353 dbg.nospace() << "QGenericMatrix<" << N << ", " << M
354 << ", " << QTypeInfo<T>::name()
355 << ">(" << endl << qSetFieldWidth(10);
356 for (int row = 0; row < M; ++row) {
357 for (int col = 0; col < N; ++col)
361 dbg << qSetFieldWidth(0) << ')';
367 #ifndef QT_NO_DATASTREAM
369 template <int N, int M, typename T>
370 QDataStream &operator<<(QDataStream &stream, const QGenericMatrix<N, M, T> &matrix)
372 for (int row = 0; row < M; ++row)
373 for (int col = 0; col < N; ++col)
374 stream << double(matrix(row, col));
378 template <int N, int M, typename T>
379 QDataStream &operator>>(QDataStream &stream, QGenericMatrix<N, M, T> &matrix)
382 for (int row = 0; row < M; ++row) {
383 for (int col = 0; col < N; ++col) {
385 matrix(row, col) = T(x);
395 Q_DECLARE_METATYPE(QMatrix2x2)
396 Q_DECLARE_METATYPE(QMatrix2x3)
397 Q_DECLARE_METATYPE(QMatrix2x4)
398 Q_DECLARE_METATYPE(QMatrix3x2)
399 Q_DECLARE_METATYPE(QMatrix3x3)
400 Q_DECLARE_METATYPE(QMatrix3x4)
401 Q_DECLARE_METATYPE(QMatrix4x2)
402 Q_DECLARE_METATYPE(QMatrix4x3)