Merge "Merge branch 'newdocs'" into refs/staging/master
[profile/ivi/qtbase.git] / src / gui / math3d / qgenericmatrix.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 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.
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 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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QGENERICMATRIX_H
43 #define QGENERICMATRIX_H
44
45 #include <QtCore/qmetatype.h>
46 #include <QtCore/qdebug.h>
47 #include <QtCore/qdatastream.h>
48
49 QT_BEGIN_HEADER
50
51 QT_BEGIN_NAMESPACE
52
53
54 template <int N, int M, typename T>
55 class QGenericMatrix
56 {
57 public:
58     QGenericMatrix();
59     QGenericMatrix(const QGenericMatrix<N, M, T>& other);
60     explicit QGenericMatrix(const T *values);
61
62     const T& operator()(int row, int column) const;
63     T& operator()(int row, int column);
64
65     bool isIdentity() const;
66     void setToIdentity();
67
68     void fill(T value);
69
70     QGenericMatrix<M, N, T> transposed() const;
71
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;
78
79     void copyDataTo(T *values) const;
80
81     T *data() { return *m; }
82     const T *data() const { return *m; }
83     const T *constData() const { return *m; }
84
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);
100
101 private:
102 #endif
103     T m[N][M];    // Column-major order to match OpenGL.
104
105     explicit QGenericMatrix(int) {}       // Construct without initializing identity matrix.
106
107 #if !defined(Q_NO_TEMPLATE_FRIENDS)
108     template <int NN, int MM, typename TT>
109     friend class QGenericMatrix;
110 #endif
111 };
112
113 template <int N, int M, typename T>
114 Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix()
115 {
116     setToIdentity();
117 }
118
119 template <int N, int M, typename T>
120 Q_INLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix(const QGenericMatrix<N, M, T>& other)
121 {
122     for (int col = 0; col < N; ++col)
123         for (int row = 0; row < M; ++row)
124             m[col][row] = other.m[col][row];
125 }
126
127 template <int N, int M, typename T>
128 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>::QGenericMatrix(const T *values)
129 {
130     for (int col = 0; col < N; ++col)
131         for (int row = 0; row < M; ++row)
132             m[col][row] = values[row * N + col];
133 }
134
135 template <int N, int M, typename T>
136 Q_INLINE_TEMPLATE const T& QGenericMatrix<N, M, T>::operator()(int row, int column) const
137 {
138     Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N);
139     return m[column][row];
140 }
141
142 template <int N, int M, typename T>
143 Q_INLINE_TEMPLATE T& QGenericMatrix<N, M, T>::operator()(int row, int column)
144 {
145     Q_ASSERT(row >= 0 && row < M && column >= 0 && column < N);
146     return m[column][row];
147 }
148
149 template <int N, int M, typename T>
150 Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::isIdentity() const
151 {
152     for (int col = 0; col < N; ++col) {
153         for (int row = 0; row < M; ++row) {
154             if (row == col) {
155                 if (m[col][row] != 1.0f)
156                     return false;
157             } else {
158                 if (m[col][row] != 0.0f)
159                     return false;
160             }
161         }
162     }
163     return true;
164 }
165
166 template <int N, int M, typename T>
167 Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::setToIdentity()
168 {
169     for (int col = 0; col < N; ++col) {
170         for (int row = 0; row < M; ++row) {
171             if (row == col)
172                 m[col][row] = 1.0f;
173             else
174                 m[col][row] = 0.0f;
175         }
176     }
177 }
178
179 template <int N, int M, typename T>
180 Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::fill(T value)
181 {
182     for (int col = 0; col < N; ++col)
183         for (int row = 0; row < M; ++row)
184             m[col][row] = value;
185 }
186
187 template <int N, int M, typename T>
188 Q_OUTOFLINE_TEMPLATE QGenericMatrix<M, N, T> QGenericMatrix<N, M, T>::transposed() const
189 {
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];
194     return result;
195 }
196
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)
199 {
200     for (int row = 0; row < M; ++row)
201         for (int col = 0; col < N; ++col)
202             m[col][row] += other.m[col][row];
203     return *this;
204 }
205
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)
208 {
209     for (int row = 0; row < M; ++row)
210         for (int col = 0; col < N; ++col)
211             m[col][row] -= other.m[col][row];
212     return *this;
213 }
214
215 template <int N, int M, typename T>
216 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator*=(T factor)
217 {
218     for (int row = 0; row < M; ++row)
219         for (int col = 0; col < N; ++col)
220             m[col][row] *= factor;
221     return *this;
222 }
223
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
226 {
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])
230                 return false;
231         }
232     return true;
233 }
234
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
237 {
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])
241                 return true;
242         }
243     return false;
244 }
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(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];
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(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];
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(1);
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(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];
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(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;
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(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;
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(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;
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     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)
358             dbg << m(row, col);
359         dbg << endl;
360     }
361     dbg << qSetFieldWidth(0) << ')';
362     return dbg.space();
363 }
364
365 #endif
366
367 #ifndef QT_NO_DATASTREAM
368
369 template <int N, int M, typename T>
370 QDataStream &operator<<(QDataStream &stream, const QGenericMatrix<N, M, T> &matrix)
371 {
372     for (int row = 0; row < M; ++row)
373         for (int col = 0; col < N; ++col)
374             stream << double(matrix(row, col));
375     return stream;
376 }
377
378 template <int N, int M, typename T>
379 QDataStream &operator>>(QDataStream &stream, QGenericMatrix<N, M, T> &matrix)
380 {
381     double x;
382     for (int row = 0; row < M; ++row) {
383         for (int col = 0; col < N; ++col) {
384             stream >> x;
385             matrix(row, col) = T(x);
386         }
387     }
388     return stream;
389 }
390
391 #endif
392
393 QT_END_NAMESPACE
394
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)
403
404 QT_END_HEADER
405
406 #endif