|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtGui module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
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 |
|
48 QT_BEGIN_HEADER |
|
49 |
|
50 QT_BEGIN_NAMESPACE |
|
51 |
|
52 QT_MODULE(Gui) |
|
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 setIdentity(); |
|
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 toValueArray(T *values); |
|
80 |
|
81 T *data() { return m[0]; } |
|
82 const T *data() const { return m[0]; } |
|
83 const T *constData() const { return m[0]; } |
|
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 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 setIdentity(); |
|
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>::setIdentity() |
|
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 index = 0; index < N * M; ++index) |
|
201 m[0][index] += other.m[0][index]; |
|
202 return *this; |
|
203 } |
|
204 |
|
205 template <int N, int M, typename T> |
|
206 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator-=(const QGenericMatrix<N, M, T>& other) |
|
207 { |
|
208 for (int index = 0; index < N * M; ++index) |
|
209 m[0][index] -= other.m[0][index]; |
|
210 return *this; |
|
211 } |
|
212 |
|
213 template <int N, int M, typename T> |
|
214 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator*=(T factor) |
|
215 { |
|
216 for (int index = 0; index < N * M; ++index) |
|
217 m[0][index] *= factor; |
|
218 return *this; |
|
219 } |
|
220 |
|
221 template <int N, int M, typename T> |
|
222 Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator==(const QGenericMatrix<N, M, T>& other) const |
|
223 { |
|
224 for (int index = 0; index < N * M; ++index) { |
|
225 if (m[0][index] != other.m[0][index]) |
|
226 return false; |
|
227 } |
|
228 return true; |
|
229 } |
|
230 |
|
231 template <int N, int M, typename T> |
|
232 Q_OUTOFLINE_TEMPLATE bool QGenericMatrix<N, M, T>::operator!=(const QGenericMatrix<N, M, T>& other) const |
|
233 { |
|
234 for (int index = 0; index < N * M; ++index) { |
|
235 if (m[0][index] != other.m[0][index]) |
|
236 return true; |
|
237 } |
|
238 return false; |
|
239 } |
|
240 |
|
241 template <int N, int M, typename T> |
|
242 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T>& QGenericMatrix<N, M, T>::operator/=(T divisor) |
|
243 { |
|
244 for (int index = 0; index < N * M; ++index) |
|
245 m[0][index] /= divisor; |
|
246 return *this; |
|
247 } |
|
248 |
|
249 template <int N, int M, typename T> |
|
250 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator+(const QGenericMatrix<N, M, T>& m1, const QGenericMatrix<N, M, T>& m2) |
|
251 { |
|
252 QGenericMatrix<N, M, T> result(1); |
|
253 for (int index = 0; index < N * M; ++index) |
|
254 result.m[0][index] = m1.m[0][index] + m2.m[0][index]; |
|
255 return result; |
|
256 } |
|
257 |
|
258 template <int N, int M, typename T> |
|
259 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator-(const QGenericMatrix<N, M, T>& m1, const QGenericMatrix<N, M, T>& m2) |
|
260 { |
|
261 QGenericMatrix<N, M, T> result(1); |
|
262 for (int index = 0; index < N * M; ++index) |
|
263 result.m[0][index] = m1.m[0][index] - m2.m[0][index]; |
|
264 return result; |
|
265 } |
|
266 |
|
267 template <int N, int M1, int M2, typename T> |
|
268 Q_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(1); |
|
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 |
|
282 template <int N, int M, typename T> |
|
283 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator-(const QGenericMatrix<N, M, T>& matrix) |
|
284 { |
|
285 QGenericMatrix<N, M, T> result(1); |
|
286 for (int index = 0; index < N * M; ++index) |
|
287 result.m[0][index] = -matrix.m[0][index]; |
|
288 return result; |
|
289 } |
|
290 |
|
291 template <int N, int M, typename T> |
|
292 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator*(T factor, const QGenericMatrix<N, M, T>& matrix) |
|
293 { |
|
294 QGenericMatrix<N, M, T> result(1); |
|
295 for (int index = 0; index < N * M; ++index) |
|
296 result.m[0][index] = matrix.m[0][index] * factor; |
|
297 return result; |
|
298 } |
|
299 |
|
300 template <int N, int M, typename T> |
|
301 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator*(const QGenericMatrix<N, M, T>& matrix, T factor) |
|
302 { |
|
303 QGenericMatrix<N, M, T> result(1); |
|
304 for (int index = 0; index < N * M; ++index) |
|
305 result.m[0][index] = matrix.m[0][index] * factor; |
|
306 return result; |
|
307 } |
|
308 |
|
309 template <int N, int M, typename T> |
|
310 Q_OUTOFLINE_TEMPLATE QGenericMatrix<N, M, T> operator/(const QGenericMatrix<N, M, T>& matrix, T divisor) |
|
311 { |
|
312 QGenericMatrix<N, M, T> result(1); |
|
313 for (int index = 0; index < N * M; ++index) |
|
314 result.m[0][index] = matrix.m[0][index] / divisor; |
|
315 return result; |
|
316 } |
|
317 |
|
318 template <int N, int M, typename T> |
|
319 Q_OUTOFLINE_TEMPLATE void QGenericMatrix<N, M, T>::toValueArray(T *values) |
|
320 { |
|
321 for (int col = 0; col < N; ++col) |
|
322 for (int row = 0; row < M; ++row) |
|
323 values[row * N + col] = T(m[col][row]); |
|
324 } |
|
325 |
|
326 // Define aliases for the useful variants of QGenericMatrix. |
|
327 typedef QGenericMatrix<2, 2, qreal> QMatrix2x2; |
|
328 typedef QGenericMatrix<2, 3, qreal> QMatrix2x3; |
|
329 typedef QGenericMatrix<2, 4, qreal> QMatrix2x4; |
|
330 typedef QGenericMatrix<3, 2, qreal> QMatrix3x2; |
|
331 typedef QGenericMatrix<3, 3, qreal> QMatrix3x3; |
|
332 typedef QGenericMatrix<3, 4, qreal> QMatrix3x4; |
|
333 typedef QGenericMatrix<4, 2, qreal> QMatrix4x2; |
|
334 typedef QGenericMatrix<4, 3, qreal> QMatrix4x3; |
|
335 |
|
336 #ifndef QT_NO_DEBUG_STREAM |
|
337 |
|
338 template <int N, int M, typename T> |
|
339 QDebug operator<<(QDebug dbg, const QGenericMatrix<N, M, T> &m) |
|
340 { |
|
341 dbg.nospace() << "QGenericMatrix<" << N << ", " << M |
|
342 << ", " << QTypeInfo<T>::name() |
|
343 << ">(" << endl << qSetFieldWidth(10); |
|
344 for (int row = 0; row < M; ++row) { |
|
345 for (int col = 0; col < N; ++col) |
|
346 dbg << m(row, col); |
|
347 dbg << endl; |
|
348 } |
|
349 dbg << qSetFieldWidth(0) << ')'; |
|
350 return dbg.space(); |
|
351 } |
|
352 |
|
353 #endif |
|
354 |
|
355 QT_END_NAMESPACE |
|
356 |
|
357 Q_DECLARE_METATYPE(QMatrix2x2) |
|
358 Q_DECLARE_METATYPE(QMatrix2x3) |
|
359 Q_DECLARE_METATYPE(QMatrix2x4) |
|
360 Q_DECLARE_METATYPE(QMatrix3x2) |
|
361 Q_DECLARE_METATYPE(QMatrix3x3) |
|
362 Q_DECLARE_METATYPE(QMatrix3x4) |
|
363 Q_DECLARE_METATYPE(QMatrix4x2) |
|
364 Q_DECLARE_METATYPE(QMatrix4x3) |
|
365 |
|
366 QT_END_HEADER |
|
367 |
|
368 #endif |