author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 22 Apr 2010 16:15:11 +0300 | |
branch | RCL_3 |
changeset 14 | 8c4229025c0b |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite 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 |
#include <QtTest/QtTest> |
|
43 |
#include <QtCore/qmath.h> |
|
44 |
#include <QtGui/qmatrix4x4.h> |
|
45 |
||
46 |
class tst_QMatrixNxN : public QObject |
|
47 |
{ |
|
48 |
Q_OBJECT |
|
49 |
public: |
|
50 |
tst_QMatrixNxN() {} |
|
51 |
~tst_QMatrixNxN() {} |
|
52 |
||
53 |
private slots: |
|
54 |
void create2x2(); |
|
55 |
void create3x3(); |
|
56 |
void create4x4(); |
|
57 |
void create4x3(); |
|
58 |
||
59 |
void isIdentity2x2(); |
|
60 |
void isIdentity3x3(); |
|
61 |
void isIdentity4x4(); |
|
62 |
void isIdentity4x3(); |
|
63 |
||
64 |
void compare2x2(); |
|
65 |
void compare3x3(); |
|
66 |
void compare4x4(); |
|
67 |
void compare4x3(); |
|
68 |
||
69 |
void transposed2x2(); |
|
70 |
void transposed3x3(); |
|
71 |
void transposed4x4(); |
|
72 |
void transposed4x3(); |
|
73 |
||
74 |
void add2x2_data(); |
|
75 |
void add2x2(); |
|
76 |
void add3x3_data(); |
|
77 |
void add3x3(); |
|
78 |
void add4x4_data(); |
|
79 |
void add4x4(); |
|
80 |
void add4x3_data(); |
|
81 |
void add4x3(); |
|
82 |
||
83 |
void subtract2x2_data(); |
|
84 |
void subtract2x2(); |
|
85 |
void subtract3x3_data(); |
|
86 |
void subtract3x3(); |
|
87 |
void subtract4x4_data(); |
|
88 |
void subtract4x4(); |
|
89 |
void subtract4x3_data(); |
|
90 |
void subtract4x3(); |
|
91 |
||
92 |
void multiply2x2_data(); |
|
93 |
void multiply2x2(); |
|
94 |
void multiply3x3_data(); |
|
95 |
void multiply3x3(); |
|
96 |
void multiply4x4_data(); |
|
97 |
void multiply4x4(); |
|
98 |
void multiply4x3_data(); |
|
99 |
void multiply4x3(); |
|
100 |
||
101 |
void multiplyFactor2x2_data(); |
|
102 |
void multiplyFactor2x2(); |
|
103 |
void multiplyFactor3x3_data(); |
|
104 |
void multiplyFactor3x3(); |
|
105 |
void multiplyFactor4x4_data(); |
|
106 |
void multiplyFactor4x4(); |
|
107 |
void multiplyFactor4x3_data(); |
|
108 |
void multiplyFactor4x3(); |
|
109 |
||
110 |
void divideFactor2x2_data(); |
|
111 |
void divideFactor2x2(); |
|
112 |
void divideFactor3x3_data(); |
|
113 |
void divideFactor3x3(); |
|
114 |
void divideFactor4x4_data(); |
|
115 |
void divideFactor4x4(); |
|
116 |
void divideFactor4x3_data(); |
|
117 |
void divideFactor4x3(); |
|
118 |
||
119 |
void negate2x2_data(); |
|
120 |
void negate2x2(); |
|
121 |
void negate3x3_data(); |
|
122 |
void negate3x3(); |
|
123 |
void negate4x4_data(); |
|
124 |
void negate4x4(); |
|
125 |
void negate4x3_data(); |
|
126 |
void negate4x3(); |
|
127 |
||
128 |
void inverted4x4_data(); |
|
129 |
void inverted4x4(); |
|
130 |
||
131 |
void orthonormalInverse4x4(); |
|
132 |
||
133 |
void scale4x4_data(); |
|
134 |
void scale4x4(); |
|
135 |
||
136 |
void translate4x4_data(); |
|
137 |
void translate4x4(); |
|
138 |
||
139 |
void rotate4x4_data(); |
|
140 |
void rotate4x4(); |
|
141 |
||
142 |
void normalMatrix_data(); |
|
143 |
void normalMatrix(); |
|
144 |
||
145 |
void optimizedTransforms(); |
|
146 |
||
147 |
void ortho(); |
|
148 |
void frustum(); |
|
149 |
void perspective(); |
|
150 |
void flipCoordinates(); |
|
151 |
||
152 |
void convertGeneric(); |
|
153 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
154 |
void optimize_data(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
void optimize(); |
0 | 156 |
|
157 |
void columnsAndRows(); |
|
158 |
||
159 |
void convertQMatrix(); |
|
160 |
void convertQTransform(); |
|
161 |
||
162 |
void fill(); |
|
163 |
||
164 |
void mapRect_data(); |
|
165 |
void mapRect(); |
|
166 |
||
167 |
void mapVector_data(); |
|
168 |
void mapVector(); |
|
169 |
||
170 |
void properties(); |
|
171 |
void metaTypes(); |
|
172 |
||
173 |
private: |
|
174 |
static void setMatrix(QMatrix2x2& m, const qreal *values); |
|
175 |
static void setMatrixDirect(QMatrix2x2& m, const qreal *values); |
|
176 |
static bool isSame(const QMatrix2x2& m, const qreal *values); |
|
177 |
static bool isIdentity(const QMatrix2x2& m); |
|
178 |
||
179 |
static void setMatrix(QMatrix3x3& m, const qreal *values); |
|
180 |
static void setMatrixDirect(QMatrix3x3& m, const qreal *values); |
|
181 |
static bool isSame(const QMatrix3x3& m, const qreal *values); |
|
182 |
static bool isIdentity(const QMatrix3x3& m); |
|
183 |
||
184 |
static void setMatrix(QMatrix4x4& m, const qreal *values); |
|
185 |
static void setMatrixDirect(QMatrix4x4& m, const qreal *values); |
|
186 |
static bool isSame(const QMatrix4x4& m, const qreal *values); |
|
187 |
static bool isIdentity(const QMatrix4x4& m); |
|
188 |
||
189 |
static void setMatrix(QMatrix4x3& m, const qreal *values); |
|
190 |
static void setMatrixDirect(QMatrix4x3& m, const qreal *values); |
|
191 |
static bool isSame(const QMatrix4x3& m, const qreal *values); |
|
192 |
static bool isIdentity(const QMatrix4x3& m); |
|
193 |
}; |
|
194 |
||
195 |
static const qreal nullValues2[] = |
|
196 |
{0.0f, 0.0f, |
|
197 |
0.0f, 0.0f}; |
|
198 |
||
199 |
static qreal const identityValues2[16] = |
|
200 |
{1.0f, 0.0f, |
|
201 |
0.0f, 1.0f}; |
|
202 |
||
203 |
static const qreal doubleIdentity2[] = |
|
204 |
{2.0f, 0.0f, |
|
205 |
0.0f, 2.0f}; |
|
206 |
||
207 |
static qreal const uniqueValues2[16] = |
|
208 |
{1.0f, 2.0f, |
|
209 |
5.0f, 6.0f}; |
|
210 |
||
211 |
static qreal const transposedValues2[16] = |
|
212 |
{1.0f, 5.0f, |
|
213 |
2.0f, 6.0f}; |
|
214 |
||
215 |
static const qreal nullValues3[] = |
|
216 |
{0.0f, 0.0f, 0.0f, |
|
217 |
0.0f, 0.0f, 0.0f, |
|
218 |
0.0f, 0.0f, 0.0f}; |
|
219 |
||
220 |
static qreal const identityValues3[16] = |
|
221 |
{1.0f, 0.0f, 0.0f, |
|
222 |
0.0f, 1.0f, 0.0f, |
|
223 |
0.0f, 0.0f, 1.0f}; |
|
224 |
||
225 |
static const qreal doubleIdentity3[] = |
|
226 |
{2.0f, 0.0f, 0.0f, |
|
227 |
0.0f, 2.0f, 0.0f, |
|
228 |
0.0f, 0.0f, 2.0f}; |
|
229 |
||
230 |
static qreal const uniqueValues3[16] = |
|
231 |
{1.0f, 2.0f, 3.0f, |
|
232 |
5.0f, 6.0f, 7.0f, |
|
233 |
9.0f, 10.0f, 11.0f}; |
|
234 |
||
235 |
static qreal const transposedValues3[16] = |
|
236 |
{1.0f, 5.0f, 9.0f, |
|
237 |
2.0f, 6.0f, 10.0f, |
|
238 |
3.0f, 7.0f, 11.0f}; |
|
239 |
||
240 |
static const qreal nullValues4[] = |
|
241 |
{0.0f, 0.0f, 0.0f, 0.0f, |
|
242 |
0.0f, 0.0f, 0.0f, 0.0f, |
|
243 |
0.0f, 0.0f, 0.0f, 0.0f, |
|
244 |
0.0f, 0.0f, 0.0f, 0.0f}; |
|
245 |
||
246 |
static qreal const identityValues4[16] = |
|
247 |
{1.0f, 0.0f, 0.0f, 0.0f, |
|
248 |
0.0f, 1.0f, 0.0f, 0.0f, |
|
249 |
0.0f, 0.0f, 1.0f, 0.0f, |
|
250 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
251 |
||
252 |
static const qreal doubleIdentity4[] = |
|
253 |
{2.0f, 0.0f, 0.0f, 0.0f, |
|
254 |
0.0f, 2.0f, 0.0f, 0.0f, |
|
255 |
0.0f, 0.0f, 2.0f, 0.0f, |
|
256 |
0.0f, 0.0f, 0.0f, 2.0f}; |
|
257 |
||
258 |
static qreal const uniqueValues4[16] = |
|
259 |
{1.0f, 2.0f, 3.0f, 4.0f, |
|
260 |
5.0f, 6.0f, 7.0f, 8.0f, |
|
261 |
9.0f, 10.0f, 11.0f, 12.0f, |
|
262 |
13.0f, 14.0f, 15.0f, 16.0f}; |
|
263 |
||
264 |
static qreal const transposedValues4[16] = |
|
265 |
{1.0f, 5.0f, 9.0f, 13.0f, |
|
266 |
2.0f, 6.0f, 10.0f, 14.0f, |
|
267 |
3.0f, 7.0f, 11.0f, 15.0f, |
|
268 |
4.0f, 8.0f, 12.0f, 16.0f}; |
|
269 |
||
270 |
static const qreal nullValues4x3[] = |
|
271 |
{0.0f, 0.0f, 0.0f, 0.0f, |
|
272 |
0.0f, 0.0f, 0.0f, 0.0f, |
|
273 |
0.0f, 0.0f, 0.0f, 0.0f}; |
|
274 |
||
275 |
static qreal const identityValues4x3[12] = |
|
276 |
{1.0f, 0.0f, 0.0f, 0.0f, |
|
277 |
0.0f, 1.0f, 0.0f, 0.0f, |
|
278 |
0.0f, 0.0f, 1.0f, 0.0f}; |
|
279 |
||
280 |
static qreal const doubleIdentity4x3[12] = |
|
281 |
{2.0f, 0.0f, 0.0f, 0.0f, |
|
282 |
0.0f, 2.0f, 0.0f, 0.0f, |
|
283 |
0.0f, 0.0f, 2.0f, 0.0f}; |
|
284 |
||
285 |
static qreal const uniqueValues4x3[12] = |
|
286 |
{1.0f, 2.0f, 3.0f, 4.0f, |
|
287 |
5.0f, 6.0f, 7.0f, 8.0f, |
|
288 |
9.0f, 10.0f, 11.0f, 12.0f}; |
|
289 |
||
290 |
static qreal const transposedValues3x4[12] = |
|
291 |
{1.0f, 5.0f, 9.0f, |
|
292 |
2.0f, 6.0f, 10.0f, |
|
293 |
3.0f, 7.0f, 11.0f, |
|
294 |
4.0f, 8.0f, 12.0f}; |
|
295 |
||
296 |
// Set a matrix to a specified array of values, which are assumed |
|
297 |
// to be in row-major order. This sets the values using floating-point. |
|
298 |
void tst_QMatrixNxN::setMatrix(QMatrix2x2& m, const qreal *values) |
|
299 |
{ |
|
300 |
for (int row = 0; row < 2; ++row) |
|
301 |
for (int col = 0; col < 2; ++col) |
|
302 |
m(row, col) = values[row * 2 + col]; |
|
303 |
} |
|
304 |
void tst_QMatrixNxN::setMatrix(QMatrix3x3& m, const qreal *values) |
|
305 |
{ |
|
306 |
for (int row = 0; row < 3; ++row) |
|
307 |
for (int col = 0; col < 3; ++col) |
|
308 |
m(row, col) = values[row * 3 + col]; |
|
309 |
} |
|
310 |
void tst_QMatrixNxN::setMatrix(QMatrix4x4& m, const qreal *values) |
|
311 |
{ |
|
312 |
for (int row = 0; row < 4; ++row) |
|
313 |
for (int col = 0; col < 4; ++col) |
|
314 |
m(row, col) = values[row * 4 + col]; |
|
315 |
} |
|
316 |
void tst_QMatrixNxN::setMatrix(QMatrix4x3& m, const qreal *values) |
|
317 |
{ |
|
318 |
for (int row = 0; row < 3; ++row) |
|
319 |
for (int col = 0; col < 4; ++col) |
|
320 |
m(row, col) = values[row * 4 + col]; |
|
321 |
} |
|
322 |
||
323 |
// Set a matrix to a specified array of values, which are assumed |
|
324 |
// to be in row-major order. This sets the values directly into |
|
325 |
// the internal data() array. |
|
326 |
void tst_QMatrixNxN::setMatrixDirect(QMatrix2x2& m, const qreal *values) |
|
327 |
{ |
|
328 |
qreal *data = m.data(); |
|
329 |
for (int row = 0; row < 2; ++row) { |
|
330 |
for (int col = 0; col < 2; ++col) { |
|
331 |
data[row + col * 2] = values[row * 2 + col]; |
|
332 |
} |
|
333 |
} |
|
334 |
} |
|
335 |
void tst_QMatrixNxN::setMatrixDirect(QMatrix3x3& m, const qreal *values) |
|
336 |
{ |
|
337 |
qreal *data = m.data(); |
|
338 |
for (int row = 0; row < 3; ++row) { |
|
339 |
for (int col = 0; col < 3; ++col) { |
|
340 |
data[row + col * 3] = values[row * 3 + col]; |
|
341 |
} |
|
342 |
} |
|
343 |
} |
|
344 |
void tst_QMatrixNxN::setMatrixDirect(QMatrix4x4& m, const qreal *values) |
|
345 |
{ |
|
346 |
qreal *data = m.data(); |
|
347 |
for (int row = 0; row < 4; ++row) { |
|
348 |
for (int col = 0; col < 4; ++col) { |
|
349 |
data[row + col * 4] = values[row * 4 + col]; |
|
350 |
} |
|
351 |
} |
|
352 |
} |
|
353 |
void tst_QMatrixNxN::setMatrixDirect(QMatrix4x3& m, const qreal *values) |
|
354 |
{ |
|
355 |
qreal *data = m.data(); |
|
356 |
for (int row = 0; row < 3; ++row) { |
|
357 |
for (int col = 0; col < 4; ++col) { |
|
358 |
data[row + col * 3] = values[row * 4 + col]; |
|
359 |
} |
|
360 |
} |
|
361 |
} |
|
362 |
||
363 |
// QVector2/3/4D use float internally, which can sometimes lead |
|
364 |
// to precision issues when converting to and from qreal during |
|
365 |
// operations involving QMatrix4x4. This fuzzy compare is slightly |
|
366 |
// "fuzzier" than the default qFuzzyCompare for qreal to compensate. |
|
367 |
static bool fuzzyCompare(qreal x, qreal y) |
|
368 |
{ |
|
369 |
return qFuzzyIsNull((float)(x - y)); |
|
370 |
} |
|
371 |
||
372 |
static bool fuzzyCompare(const QVector3D &v1, const QVector3D &v2) |
|
373 |
{ |
|
374 |
if (!fuzzyCompare(v1.x(), v2.x())) |
|
375 |
return false; |
|
376 |
if (!fuzzyCompare(v1.y(), v2.y())) |
|
377 |
return false; |
|
378 |
if (!fuzzyCompare(v1.z(), v2.z())) |
|
379 |
return false; |
|
380 |
return true; |
|
381 |
} |
|
382 |
||
383 |
// Determine if a matrix is the same as a specified array of values. |
|
384 |
// The values are assumed to be specified in row-major order. |
|
385 |
bool tst_QMatrixNxN::isSame(const QMatrix2x2& m, const qreal *values) |
|
386 |
{ |
|
387 |
const qreal *mv = m.constData(); |
|
388 |
for (int row = 0; row < 2; ++row) { |
|
389 |
for (int col = 0; col < 2; ++col) { |
|
390 |
// Check the values using the operator() function. |
|
391 |
if (!fuzzyCompare(m(row, col), values[row * 2 + col])) { |
|
392 |
qDebug() << "floating-point failure at" << row << col << "actual =" << m(row, col) << "expected =" << values[row * 2 + col]; |
|
393 |
return false; |
|
394 |
} |
|
395 |
||
396 |
// Check the values using direct access, which verifies that the values |
|
397 |
// are stored internally in column-major order. |
|
398 |
if (!fuzzyCompare(mv[col * 2 + row], values[row * 2 + col])) { |
|
399 |
qDebug() << "column floating-point failure at" << row << col << "actual =" << mv[col * 2 + row] << "expected =" << values[row * 2 + col]; |
|
400 |
return false; |
|
401 |
} |
|
402 |
} |
|
403 |
} |
|
404 |
return true; |
|
405 |
} |
|
406 |
bool tst_QMatrixNxN::isSame(const QMatrix3x3& m, const qreal *values) |
|
407 |
{ |
|
408 |
const qreal *mv = m.constData(); |
|
409 |
for (int row = 0; row < 3; ++row) { |
|
410 |
for (int col = 0; col < 3; ++col) { |
|
411 |
// Check the values using the operator() access function. |
|
412 |
if (!fuzzyCompare(m(row, col), values[row * 3 + col])) { |
|
413 |
qDebug() << "floating-point failure at" << row << col << "actual =" << m(row, col) << "expected =" << values[row * 3 + col]; |
|
414 |
return false; |
|
415 |
} |
|
416 |
||
417 |
// Check the values using direct access, which verifies that the values |
|
418 |
// are stored internally in column-major order. |
|
419 |
if (!fuzzyCompare(mv[col * 3 + row], values[row * 3 + col])) { |
|
420 |
qDebug() << "column floating-point failure at" << row << col << "actual =" << mv[col * 3 + row] << "expected =" << values[row * 3 + col]; |
|
421 |
return false; |
|
422 |
} |
|
423 |
} |
|
424 |
} |
|
425 |
return true; |
|
426 |
} |
|
427 |
bool tst_QMatrixNxN::isSame(const QMatrix4x4& m, const qreal *values) |
|
428 |
{ |
|
429 |
const qreal *mv = m.constData(); |
|
430 |
for (int row = 0; row < 4; ++row) { |
|
431 |
for (int col = 0; col < 4; ++col) { |
|
432 |
// Check the values using the operator() access function. |
|
433 |
if (!fuzzyCompare(m(row, col), values[row * 4 + col])) { |
|
434 |
qDebug() << "floating-point failure at" << row << col << "actual =" << m(row, col) << "expected =" << values[row * 4 + col]; |
|
435 |
return false; |
|
436 |
} |
|
437 |
||
438 |
// Check the values using direct access, which verifies that the values |
|
439 |
// are stored internally in column-major order. |
|
440 |
if (!fuzzyCompare(mv[col * 4 + row], values[row * 4 + col])) { |
|
441 |
qDebug() << "column floating-point failure at" << row << col << "actual =" << mv[col * 4 + row] << "expected =" << values[row * 4 + col]; |
|
442 |
return false; |
|
443 |
} |
|
444 |
} |
|
445 |
} |
|
446 |
return true; |
|
447 |
} |
|
448 |
bool tst_QMatrixNxN::isSame(const QMatrix4x3& m, const qreal *values) |
|
449 |
{ |
|
450 |
const qreal *mv = m.constData(); |
|
451 |
for (int row = 0; row < 3; ++row) { |
|
452 |
for (int col = 0; col < 4; ++col) { |
|
453 |
// Check the values using the operator() access function. |
|
454 |
if (!fuzzyCompare(m(row, col), values[row * 4 + col])) { |
|
455 |
qDebug() << "floating-point failure at" << row << col << "actual =" << m(row, col) << "expected =" << values[row * 4 + col]; |
|
456 |
return false; |
|
457 |
} |
|
458 |
||
459 |
// Check the values using direct access, which verifies that the values |
|
460 |
// are stored internally in column-major order. |
|
461 |
if (!fuzzyCompare(mv[col * 3 + row], values[row * 4 + col])) { |
|
462 |
qDebug() << "column floating-point failure at" << row << col << "actual =" << mv[col * 3 + row] << "expected =" << values[row * 4 + col]; |
|
463 |
return false; |
|
464 |
} |
|
465 |
} |
|
466 |
} |
|
467 |
return true; |
|
468 |
} |
|
469 |
||
470 |
// Determine if a matrix is the identity. |
|
471 |
bool tst_QMatrixNxN::isIdentity(const QMatrix2x2& m) |
|
472 |
{ |
|
473 |
return isSame(m, identityValues2); |
|
474 |
} |
|
475 |
bool tst_QMatrixNxN::isIdentity(const QMatrix3x3& m) |
|
476 |
{ |
|
477 |
return isSame(m, identityValues3); |
|
478 |
} |
|
479 |
bool tst_QMatrixNxN::isIdentity(const QMatrix4x4& m) |
|
480 |
{ |
|
481 |
return isSame(m, identityValues4); |
|
482 |
} |
|
483 |
bool tst_QMatrixNxN::isIdentity(const QMatrix4x3& m) |
|
484 |
{ |
|
485 |
return isSame(m, identityValues4x3); |
|
486 |
} |
|
487 |
||
488 |
// Test the creation of QMatrix2x2 objects in various ways: |
|
489 |
// construct, copy, and modify. |
|
490 |
void tst_QMatrixNxN::create2x2() |
|
491 |
{ |
|
492 |
QMatrix2x2 m1; |
|
493 |
QVERIFY(isIdentity(m1)); |
|
494 |
QVERIFY(m1.isIdentity()); |
|
495 |
||
496 |
QMatrix2x2 m2; |
|
497 |
setMatrix(m2, uniqueValues2); |
|
498 |
QVERIFY(isSame(m2, uniqueValues2)); |
|
499 |
QVERIFY(!m2.isIdentity()); |
|
500 |
||
501 |
QMatrix2x2 m3; |
|
502 |
setMatrixDirect(m3, uniqueValues2); |
|
503 |
QVERIFY(isSame(m3, uniqueValues2)); |
|
504 |
||
505 |
QMatrix2x2 m4(m3); |
|
506 |
QVERIFY(isSame(m4, uniqueValues2)); |
|
507 |
||
508 |
QMatrix2x2 m5; |
|
509 |
m5 = m3; |
|
510 |
QVERIFY(isSame(m5, uniqueValues2)); |
|
511 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
512 |
m5.setToIdentity(); |
0 | 513 |
QVERIFY(isIdentity(m5)); |
514 |
||
515 |
QMatrix2x2 m6(uniqueValues2); |
|
516 |
QVERIFY(isSame(m6, uniqueValues2)); |
|
517 |
qreal vals[4]; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
518 |
m6.copyDataTo(vals); |
0 | 519 |
for (int index = 0; index < 4; ++index) |
520 |
QCOMPARE(vals[index], uniqueValues2[index]); |
|
521 |
} |
|
522 |
||
523 |
// Test the creation of QMatrix3x3 objects in various ways: |
|
524 |
// construct, copy, and modify. |
|
525 |
void tst_QMatrixNxN::create3x3() |
|
526 |
{ |
|
527 |
QMatrix3x3 m1; |
|
528 |
QVERIFY(isIdentity(m1)); |
|
529 |
QVERIFY(m1.isIdentity()); |
|
530 |
||
531 |
QMatrix3x3 m2; |
|
532 |
setMatrix(m2, uniqueValues3); |
|
533 |
QVERIFY(isSame(m2, uniqueValues3)); |
|
534 |
QVERIFY(!m2.isIdentity()); |
|
535 |
||
536 |
QMatrix3x3 m3; |
|
537 |
setMatrixDirect(m3, uniqueValues3); |
|
538 |
QVERIFY(isSame(m3, uniqueValues3)); |
|
539 |
||
540 |
QMatrix3x3 m4(m3); |
|
541 |
QVERIFY(isSame(m4, uniqueValues3)); |
|
542 |
||
543 |
QMatrix3x3 m5; |
|
544 |
m5 = m3; |
|
545 |
QVERIFY(isSame(m5, uniqueValues3)); |
|
546 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
547 |
m5.setToIdentity(); |
0 | 548 |
QVERIFY(isIdentity(m5)); |
549 |
||
550 |
QMatrix3x3 m6(uniqueValues3); |
|
551 |
QVERIFY(isSame(m6, uniqueValues3)); |
|
552 |
qreal vals[9]; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
553 |
m6.copyDataTo(vals); |
0 | 554 |
for (int index = 0; index < 9; ++index) |
555 |
QCOMPARE(vals[index], uniqueValues3[index]); |
|
556 |
} |
|
557 |
||
558 |
// Test the creation of QMatrix4x4 objects in various ways: |
|
559 |
// construct, copy, and modify. |
|
560 |
void tst_QMatrixNxN::create4x4() |
|
561 |
{ |
|
562 |
QMatrix4x4 m1; |
|
563 |
QVERIFY(isIdentity(m1)); |
|
564 |
QVERIFY(m1.isIdentity()); |
|
565 |
||
566 |
QMatrix4x4 m2; |
|
567 |
setMatrix(m2, uniqueValues4); |
|
568 |
QVERIFY(isSame(m2, uniqueValues4)); |
|
569 |
QVERIFY(!m2.isIdentity()); |
|
570 |
||
571 |
QMatrix4x4 m3; |
|
572 |
setMatrixDirect(m3, uniqueValues4); |
|
573 |
QVERIFY(isSame(m3, uniqueValues4)); |
|
574 |
||
575 |
QMatrix4x4 m4(m3); |
|
576 |
QVERIFY(isSame(m4, uniqueValues4)); |
|
577 |
||
578 |
QMatrix4x4 m5; |
|
579 |
m5 = m3; |
|
580 |
QVERIFY(isSame(m5, uniqueValues4)); |
|
581 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
582 |
m5.setToIdentity(); |
0 | 583 |
QVERIFY(isIdentity(m5)); |
584 |
||
585 |
QMatrix4x4 m6(uniqueValues4); |
|
586 |
QVERIFY(isSame(m6, uniqueValues4)); |
|
587 |
qreal vals[16]; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
588 |
m6.copyDataTo(vals); |
0 | 589 |
for (int index = 0; index < 16; ++index) |
590 |
QCOMPARE(vals[index], uniqueValues4[index]); |
|
591 |
||
592 |
QMatrix4x4 m8 |
|
593 |
(uniqueValues4[0], uniqueValues4[1], uniqueValues4[2], uniqueValues4[3], |
|
594 |
uniqueValues4[4], uniqueValues4[5], uniqueValues4[6], uniqueValues4[7], |
|
595 |
uniqueValues4[8], uniqueValues4[9], uniqueValues4[10], uniqueValues4[11], |
|
596 |
uniqueValues4[12], uniqueValues4[13], uniqueValues4[14], uniqueValues4[15]); |
|
597 |
QVERIFY(isSame(m8, uniqueValues4)); |
|
598 |
} |
|
599 |
||
600 |
// Test the creation of QMatrix4x3 objects in various ways: |
|
601 |
// construct, copy, and modify. |
|
602 |
void tst_QMatrixNxN::create4x3() |
|
603 |
{ |
|
604 |
QMatrix4x3 m1; |
|
605 |
QVERIFY(isIdentity(m1)); |
|
606 |
QVERIFY(m1.isIdentity()); |
|
607 |
||
608 |
QMatrix4x3 m2; |
|
609 |
setMatrix(m2, uniqueValues4x3); |
|
610 |
QVERIFY(isSame(m2, uniqueValues4x3)); |
|
611 |
QVERIFY(!m2.isIdentity()); |
|
612 |
||
613 |
QMatrix4x3 m3; |
|
614 |
setMatrixDirect(m3, uniqueValues4x3); |
|
615 |
QVERIFY(isSame(m3, uniqueValues4x3)); |
|
616 |
||
617 |
QMatrix4x3 m4(m3); |
|
618 |
QVERIFY(isSame(m4, uniqueValues4x3)); |
|
619 |
||
620 |
QMatrix4x3 m5; |
|
621 |
m5 = m3; |
|
622 |
QVERIFY(isSame(m5, uniqueValues4x3)); |
|
623 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
624 |
m5.setToIdentity(); |
0 | 625 |
QVERIFY(isIdentity(m5)); |
626 |
||
627 |
QMatrix4x3 m6(uniqueValues4x3); |
|
628 |
QVERIFY(isSame(m6, uniqueValues4x3)); |
|
629 |
qreal vals[12]; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
630 |
m6.copyDataTo(vals); |
0 | 631 |
for (int index = 0; index < 12; ++index) |
632 |
QCOMPARE(vals[index], uniqueValues4x3[index]); |
|
633 |
} |
|
634 |
||
635 |
// Test isIdentity() for 2x2 matrices. |
|
636 |
void tst_QMatrixNxN::isIdentity2x2() |
|
637 |
{ |
|
638 |
for (int i = 0; i < 2 * 2; ++i) { |
|
639 |
QMatrix2x2 m; |
|
640 |
QVERIFY(m.isIdentity()); |
|
641 |
m.data()[i] = 42.0f; |
|
642 |
QVERIFY(!m.isIdentity()); |
|
643 |
} |
|
644 |
} |
|
645 |
||
646 |
// Test isIdentity() for 3x3 matrices. |
|
647 |
void tst_QMatrixNxN::isIdentity3x3() |
|
648 |
{ |
|
649 |
for (int i = 0; i < 3 * 3; ++i) { |
|
650 |
QMatrix3x3 m; |
|
651 |
QVERIFY(m.isIdentity()); |
|
652 |
m.data()[i] = 42.0f; |
|
653 |
QVERIFY(!m.isIdentity()); |
|
654 |
} |
|
655 |
} |
|
656 |
||
657 |
// Test isIdentity() for 4x4 matrices. |
|
658 |
void tst_QMatrixNxN::isIdentity4x4() |
|
659 |
{ |
|
660 |
for (int i = 0; i < 4 * 4; ++i) { |
|
661 |
QMatrix4x4 m; |
|
662 |
QVERIFY(m.isIdentity()); |
|
663 |
m.data()[i] = 42.0f; |
|
664 |
QVERIFY(!m.isIdentity()); |
|
665 |
} |
|
666 |
||
667 |
// Force the "Identity" flag bit to be lost and check again. |
|
668 |
QMatrix4x4 m2; |
|
669 |
m2.data()[0] = 1.0f; |
|
670 |
QVERIFY(m2.isIdentity()); |
|
671 |
} |
|
672 |
||
673 |
// Test isIdentity() for 4x3 matrices. |
|
674 |
void tst_QMatrixNxN::isIdentity4x3() |
|
675 |
{ |
|
676 |
for (int i = 0; i < 4 * 3; ++i) { |
|
677 |
QMatrix4x3 m; |
|
678 |
QVERIFY(m.isIdentity()); |
|
679 |
m.data()[i] = 42.0f; |
|
680 |
QVERIFY(!m.isIdentity()); |
|
681 |
} |
|
682 |
} |
|
683 |
||
684 |
// Test 2x2 matrix comparisons. |
|
685 |
void tst_QMatrixNxN::compare2x2() |
|
686 |
{ |
|
687 |
QMatrix2x2 m1(uniqueValues2); |
|
688 |
QMatrix2x2 m2(uniqueValues2); |
|
689 |
QMatrix2x2 m3(transposedValues2); |
|
690 |
||
691 |
QVERIFY(m1 == m2); |
|
692 |
QVERIFY(!(m1 != m2)); |
|
693 |
QVERIFY(m1 != m3); |
|
694 |
QVERIFY(!(m1 == m3)); |
|
695 |
} |
|
696 |
||
697 |
// Test 3x3 matrix comparisons. |
|
698 |
void tst_QMatrixNxN::compare3x3() |
|
699 |
{ |
|
700 |
QMatrix3x3 m1(uniqueValues3); |
|
701 |
QMatrix3x3 m2(uniqueValues3); |
|
702 |
QMatrix3x3 m3(transposedValues3); |
|
703 |
||
704 |
QVERIFY(m1 == m2); |
|
705 |
QVERIFY(!(m1 != m2)); |
|
706 |
QVERIFY(m1 != m3); |
|
707 |
QVERIFY(!(m1 == m3)); |
|
708 |
} |
|
709 |
||
710 |
// Test 4x4 matrix comparisons. |
|
711 |
void tst_QMatrixNxN::compare4x4() |
|
712 |
{ |
|
713 |
QMatrix4x4 m1(uniqueValues4); |
|
714 |
QMatrix4x4 m2(uniqueValues4); |
|
715 |
QMatrix4x4 m3(transposedValues4); |
|
716 |
||
717 |
QVERIFY(m1 == m2); |
|
718 |
QVERIFY(!(m1 != m2)); |
|
719 |
QVERIFY(m1 != m3); |
|
720 |
QVERIFY(!(m1 == m3)); |
|
721 |
} |
|
722 |
||
723 |
// Test 4x3 matrix comparisons. |
|
724 |
void tst_QMatrixNxN::compare4x3() |
|
725 |
{ |
|
726 |
QMatrix4x3 m1(uniqueValues4x3); |
|
727 |
QMatrix4x3 m2(uniqueValues4x3); |
|
728 |
QMatrix4x3 m3(transposedValues3x4); |
|
729 |
||
730 |
QVERIFY(m1 == m2); |
|
731 |
QVERIFY(!(m1 != m2)); |
|
732 |
QVERIFY(m1 != m3); |
|
733 |
QVERIFY(!(m1 == m3)); |
|
734 |
} |
|
735 |
||
736 |
// Test matrix 2x2 transpose operations. |
|
737 |
void tst_QMatrixNxN::transposed2x2() |
|
738 |
{ |
|
739 |
// Transposing the identity should result in the identity. |
|
740 |
QMatrix2x2 m1; |
|
741 |
QMatrix2x2 m2 = m1.transposed(); |
|
742 |
QVERIFY(isIdentity(m2)); |
|
743 |
||
744 |
// Transpose a more interesting matrix that allows us to track |
|
745 |
// exactly where each source element ends up. |
|
746 |
QMatrix2x2 m3(uniqueValues2); |
|
747 |
QMatrix2x2 m4 = m3.transposed(); |
|
748 |
QVERIFY(isSame(m4, transposedValues2)); |
|
749 |
||
750 |
// Transpose in-place, just to check that the compiler is sane. |
|
751 |
m3 = m3.transposed(); |
|
752 |
QVERIFY(isSame(m3, transposedValues2)); |
|
753 |
} |
|
754 |
||
755 |
// Test matrix 3x3 transpose operations. |
|
756 |
void tst_QMatrixNxN::transposed3x3() |
|
757 |
{ |
|
758 |
// Transposing the identity should result in the identity. |
|
759 |
QMatrix3x3 m1; |
|
760 |
QMatrix3x3 m2 = m1.transposed(); |
|
761 |
QVERIFY(isIdentity(m2)); |
|
762 |
||
763 |
// Transpose a more interesting matrix that allows us to track |
|
764 |
// exactly where each source element ends up. |
|
765 |
QMatrix3x3 m3(uniqueValues3); |
|
766 |
QMatrix3x3 m4 = m3.transposed(); |
|
767 |
QVERIFY(isSame(m4, transposedValues3)); |
|
768 |
||
769 |
// Transpose in-place, just to check that the compiler is sane. |
|
770 |
m3 = m3.transposed(); |
|
771 |
QVERIFY(isSame(m3, transposedValues3)); |
|
772 |
} |
|
773 |
||
774 |
// Test matrix 4x4 transpose operations. |
|
775 |
void tst_QMatrixNxN::transposed4x4() |
|
776 |
{ |
|
777 |
// Transposing the identity should result in the identity. |
|
778 |
QMatrix4x4 m1; |
|
779 |
QMatrix4x4 m2 = m1.transposed(); |
|
780 |
QVERIFY(isIdentity(m2)); |
|
781 |
||
782 |
// Transpose a more interesting matrix that allows us to track |
|
783 |
// exactly where each source element ends up. |
|
784 |
QMatrix4x4 m3(uniqueValues4); |
|
785 |
QMatrix4x4 m4 = m3.transposed(); |
|
786 |
QVERIFY(isSame(m4, transposedValues4)); |
|
787 |
||
788 |
// Transpose in-place, just to check that the compiler is sane. |
|
789 |
m3 = m3.transposed(); |
|
790 |
QVERIFY(isSame(m3, transposedValues4)); |
|
791 |
} |
|
792 |
||
793 |
// Test matrix 4x3 transpose operations. |
|
794 |
void tst_QMatrixNxN::transposed4x3() |
|
795 |
{ |
|
796 |
QMatrix4x3 m3(uniqueValues4x3); |
|
797 |
QMatrix3x4 m4 = m3.transposed(); |
|
798 |
qreal values[12]; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
799 |
m4.copyDataTo(values); |
0 | 800 |
for (int index = 0; index < 12; ++index) |
801 |
QCOMPARE(values[index], transposedValues3x4[index]); |
|
802 |
} |
|
803 |
||
804 |
// Test matrix addition for 2x2 matrices. |
|
805 |
void tst_QMatrixNxN::add2x2_data() |
|
806 |
{ |
|
807 |
QTest::addColumn<void *>("m1Values"); |
|
808 |
QTest::addColumn<void *>("m2Values"); |
|
809 |
QTest::addColumn<void *>("m3Values"); |
|
810 |
||
811 |
QTest::newRow("null") |
|
812 |
<< (void *)nullValues2 << (void *)nullValues2 << (void *)nullValues2; |
|
813 |
||
814 |
QTest::newRow("identity/null") |
|
815 |
<< (void *)identityValues2 << (void *)nullValues2 << (void *)identityValues2; |
|
816 |
||
817 |
QTest::newRow("identity/identity") |
|
818 |
<< (void *)identityValues2 << (void *)identityValues2 << (void *)doubleIdentity2; |
|
819 |
||
820 |
static qreal const sumValues[16] = |
|
821 |
{2.0f, 7.0f, |
|
822 |
7.0f, 12.0f}; |
|
823 |
QTest::newRow("unique") |
|
824 |
<< (void *)uniqueValues2 << (void *)transposedValues2 << (void *)sumValues; |
|
825 |
} |
|
826 |
void tst_QMatrixNxN::add2x2() |
|
827 |
{ |
|
828 |
QFETCH(void *, m1Values); |
|
829 |
QFETCH(void *, m2Values); |
|
830 |
QFETCH(void *, m3Values); |
|
831 |
||
832 |
QMatrix2x2 m1((const qreal *)m1Values); |
|
833 |
QMatrix2x2 m2((const qreal *)m2Values); |
|
834 |
||
835 |
QMatrix2x2 m4(m1); |
|
836 |
m4 += m2; |
|
837 |
QVERIFY(isSame(m4, (const qreal *)m3Values)); |
|
838 |
||
839 |
QMatrix2x2 m5; |
|
840 |
m5 = m1 + m2; |
|
841 |
QVERIFY(isSame(m5, (const qreal *)m3Values)); |
|
842 |
} |
|
843 |
||
844 |
// Test matrix addition for 3x3 matrices. |
|
845 |
void tst_QMatrixNxN::add3x3_data() |
|
846 |
{ |
|
847 |
QTest::addColumn<void *>("m1Values"); |
|
848 |
QTest::addColumn<void *>("m2Values"); |
|
849 |
QTest::addColumn<void *>("m3Values"); |
|
850 |
||
851 |
QTest::newRow("null") |
|
852 |
<< (void *)nullValues3 << (void *)nullValues3 << (void *)nullValues3; |
|
853 |
||
854 |
QTest::newRow("identity/null") |
|
855 |
<< (void *)identityValues3 << (void *)nullValues3 << (void *)identityValues3; |
|
856 |
||
857 |
QTest::newRow("identity/identity") |
|
858 |
<< (void *)identityValues3 << (void *)identityValues3 << (void *)doubleIdentity3; |
|
859 |
||
860 |
static qreal const sumValues[16] = |
|
861 |
{2.0f, 7.0f, 12.0f, |
|
862 |
7.0f, 12.0f, 17.0f, |
|
863 |
12.0f, 17.0f, 22.0f}; |
|
864 |
QTest::newRow("unique") |
|
865 |
<< (void *)uniqueValues3 << (void *)transposedValues3 << (void *)sumValues; |
|
866 |
} |
|
867 |
void tst_QMatrixNxN::add3x3() |
|
868 |
{ |
|
869 |
QFETCH(void *, m1Values); |
|
870 |
QFETCH(void *, m2Values); |
|
871 |
QFETCH(void *, m3Values); |
|
872 |
||
873 |
QMatrix3x3 m1((const qreal *)m1Values); |
|
874 |
QMatrix3x3 m2((const qreal *)m2Values); |
|
875 |
||
876 |
QMatrix3x3 m4(m1); |
|
877 |
m4 += m2; |
|
878 |
QVERIFY(isSame(m4, (const qreal *)m3Values)); |
|
879 |
||
880 |
QMatrix3x3 m5; |
|
881 |
m5 = m1 + m2; |
|
882 |
QVERIFY(isSame(m5, (const qreal *)m3Values)); |
|
883 |
} |
|
884 |
||
885 |
// Test matrix addition for 4x4 matrices. |
|
886 |
void tst_QMatrixNxN::add4x4_data() |
|
887 |
{ |
|
888 |
QTest::addColumn<void *>("m1Values"); |
|
889 |
QTest::addColumn<void *>("m2Values"); |
|
890 |
QTest::addColumn<void *>("m3Values"); |
|
891 |
||
892 |
QTest::newRow("null") |
|
893 |
<< (void *)nullValues4 << (void *)nullValues4 << (void *)nullValues4; |
|
894 |
||
895 |
QTest::newRow("identity/null") |
|
896 |
<< (void *)identityValues4 << (void *)nullValues4 << (void *)identityValues4; |
|
897 |
||
898 |
QTest::newRow("identity/identity") |
|
899 |
<< (void *)identityValues4 << (void *)identityValues4 << (void *)doubleIdentity4; |
|
900 |
||
901 |
static qreal const sumValues[16] = |
|
902 |
{2.0f, 7.0f, 12.0f, 17.0f, |
|
903 |
7.0f, 12.0f, 17.0f, 22.0f, |
|
904 |
12.0f, 17.0f, 22.0f, 27.0f, |
|
905 |
17.0f, 22.0f, 27.0f, 32.0f}; |
|
906 |
QTest::newRow("unique") |
|
907 |
<< (void *)uniqueValues4 << (void *)transposedValues4 << (void *)sumValues; |
|
908 |
} |
|
909 |
void tst_QMatrixNxN::add4x4() |
|
910 |
{ |
|
911 |
QFETCH(void *, m1Values); |
|
912 |
QFETCH(void *, m2Values); |
|
913 |
QFETCH(void *, m3Values); |
|
914 |
||
915 |
QMatrix4x4 m1((const qreal *)m1Values); |
|
916 |
QMatrix4x4 m2((const qreal *)m2Values); |
|
917 |
||
918 |
QMatrix4x4 m4(m1); |
|
919 |
m4 += m2; |
|
920 |
QVERIFY(isSame(m4, (const qreal *)m3Values)); |
|
921 |
||
922 |
QMatrix4x4 m5; |
|
923 |
m5 = m1 + m2; |
|
924 |
QVERIFY(isSame(m5, (const qreal *)m3Values)); |
|
925 |
} |
|
926 |
||
927 |
// Test matrix addition for 4x3 matrices. |
|
928 |
void tst_QMatrixNxN::add4x3_data() |
|
929 |
{ |
|
930 |
QTest::addColumn<void *>("m1Values"); |
|
931 |
QTest::addColumn<void *>("m2Values"); |
|
932 |
QTest::addColumn<void *>("m3Values"); |
|
933 |
||
934 |
QTest::newRow("null") |
|
935 |
<< (void *)nullValues4x3 << (void *)nullValues4x3 << (void *)nullValues4x3; |
|
936 |
||
937 |
QTest::newRow("identity/null") |
|
938 |
<< (void *)identityValues4x3 << (void *)nullValues4x3 << (void *)identityValues4x3; |
|
939 |
||
940 |
QTest::newRow("identity/identity") |
|
941 |
<< (void *)identityValues4x3 << (void *)identityValues4x3 << (void *)doubleIdentity4x3; |
|
942 |
||
943 |
static qreal const sumValues[16] = |
|
944 |
{2.0f, 7.0f, 12.0f, 6.0f, |
|
945 |
11.0f, 16.0f, 10.0f, 15.0f, |
|
946 |
20.0f, 14.0f, 19.0f, 24.0f}; |
|
947 |
QTest::newRow("unique") |
|
948 |
<< (void *)uniqueValues4x3 << (void *)transposedValues3x4 << (void *)sumValues; |
|
949 |
} |
|
950 |
void tst_QMatrixNxN::add4x3() |
|
951 |
{ |
|
952 |
QFETCH(void *, m1Values); |
|
953 |
QFETCH(void *, m2Values); |
|
954 |
QFETCH(void *, m3Values); |
|
955 |
||
956 |
QMatrix4x3 m1((const qreal *)m1Values); |
|
957 |
QMatrix4x3 m2((const qreal *)m2Values); |
|
958 |
||
959 |
QMatrix4x3 m4(m1); |
|
960 |
m4 += m2; |
|
961 |
QVERIFY(isSame(m4, (const qreal *)m3Values)); |
|
962 |
||
963 |
QMatrix4x3 m5; |
|
964 |
m5 = m1 + m2; |
|
965 |
QVERIFY(isSame(m5, (const qreal *)m3Values)); |
|
966 |
} |
|
967 |
||
968 |
// Test matrix subtraction for 2x2 matrices. |
|
969 |
void tst_QMatrixNxN::subtract2x2_data() |
|
970 |
{ |
|
971 |
// Use the same test cases as the add test. |
|
972 |
add2x2_data(); |
|
973 |
} |
|
974 |
void tst_QMatrixNxN::subtract2x2() |
|
975 |
{ |
|
976 |
QFETCH(void *, m1Values); |
|
977 |
QFETCH(void *, m2Values); |
|
978 |
QFETCH(void *, m3Values); |
|
979 |
||
980 |
QMatrix2x2 m1((const qreal *)m1Values); |
|
981 |
QMatrix2x2 m2((const qreal *)m2Values); |
|
982 |
QMatrix2x2 m3((const qreal *)m3Values); |
|
983 |
||
984 |
QMatrix2x2 m4(m3); |
|
985 |
m4 -= m1; |
|
986 |
QVERIFY(isSame(m4, (const qreal *)m2Values)); |
|
987 |
||
988 |
QMatrix2x2 m5; |
|
989 |
m5 = m3 - m1; |
|
990 |
QVERIFY(isSame(m5, (const qreal *)m2Values)); |
|
991 |
||
992 |
QMatrix2x2 m6(m3); |
|
993 |
m6 -= m2; |
|
994 |
QVERIFY(isSame(m6, (const qreal *)m1Values)); |
|
995 |
||
996 |
QMatrix2x2 m7; |
|
997 |
m7 = m3 - m2; |
|
998 |
QVERIFY(isSame(m7, (const qreal *)m1Values)); |
|
999 |
} |
|
1000 |
||
1001 |
// Test matrix subtraction for 3x3 matrices. |
|
1002 |
void tst_QMatrixNxN::subtract3x3_data() |
|
1003 |
{ |
|
1004 |
// Use the same test cases as the add test. |
|
1005 |
add3x3_data(); |
|
1006 |
} |
|
1007 |
void tst_QMatrixNxN::subtract3x3() |
|
1008 |
{ |
|
1009 |
QFETCH(void *, m1Values); |
|
1010 |
QFETCH(void *, m2Values); |
|
1011 |
QFETCH(void *, m3Values); |
|
1012 |
||
1013 |
QMatrix3x3 m1((const qreal *)m1Values); |
|
1014 |
QMatrix3x3 m2((const qreal *)m2Values); |
|
1015 |
QMatrix3x3 m3((const qreal *)m3Values); |
|
1016 |
||
1017 |
QMatrix3x3 m4(m3); |
|
1018 |
m4 -= m1; |
|
1019 |
QVERIFY(isSame(m4, (const qreal *)m2Values)); |
|
1020 |
||
1021 |
QMatrix3x3 m5; |
|
1022 |
m5 = m3 - m1; |
|
1023 |
QVERIFY(isSame(m5, (const qreal *)m2Values)); |
|
1024 |
||
1025 |
QMatrix3x3 m6(m3); |
|
1026 |
m6 -= m2; |
|
1027 |
QVERIFY(isSame(m6, (const qreal *)m1Values)); |
|
1028 |
||
1029 |
QMatrix3x3 m7; |
|
1030 |
m7 = m3 - m2; |
|
1031 |
QVERIFY(isSame(m7, (const qreal *)m1Values)); |
|
1032 |
} |
|
1033 |
||
1034 |
// Test matrix subtraction for 4x4 matrices. |
|
1035 |
void tst_QMatrixNxN::subtract4x4_data() |
|
1036 |
{ |
|
1037 |
// Use the same test cases as the add test. |
|
1038 |
add4x4_data(); |
|
1039 |
} |
|
1040 |
void tst_QMatrixNxN::subtract4x4() |
|
1041 |
{ |
|
1042 |
QFETCH(void *, m1Values); |
|
1043 |
QFETCH(void *, m2Values); |
|
1044 |
QFETCH(void *, m3Values); |
|
1045 |
||
1046 |
QMatrix4x4 m1((const qreal *)m1Values); |
|
1047 |
QMatrix4x4 m2((const qreal *)m2Values); |
|
1048 |
QMatrix4x4 m3((const qreal *)m3Values); |
|
1049 |
||
1050 |
QMatrix4x4 m4(m3); |
|
1051 |
m4 -= m1; |
|
1052 |
QVERIFY(isSame(m4, (const qreal *)m2Values)); |
|
1053 |
||
1054 |
QMatrix4x4 m5; |
|
1055 |
m5 = m3 - m1; |
|
1056 |
QVERIFY(isSame(m5, (const qreal *)m2Values)); |
|
1057 |
||
1058 |
QMatrix4x4 m6(m3); |
|
1059 |
m6 -= m2; |
|
1060 |
QVERIFY(isSame(m6, (const qreal *)m1Values)); |
|
1061 |
||
1062 |
QMatrix4x4 m7; |
|
1063 |
m7 = m3 - m2; |
|
1064 |
QVERIFY(isSame(m7, (const qreal *)m1Values)); |
|
1065 |
} |
|
1066 |
||
1067 |
// Test matrix subtraction for 4x3 matrices. |
|
1068 |
void tst_QMatrixNxN::subtract4x3_data() |
|
1069 |
{ |
|
1070 |
// Use the same test cases as the add test. |
|
1071 |
add4x3_data(); |
|
1072 |
} |
|
1073 |
void tst_QMatrixNxN::subtract4x3() |
|
1074 |
{ |
|
1075 |
QFETCH(void *, m1Values); |
|
1076 |
QFETCH(void *, m2Values); |
|
1077 |
QFETCH(void *, m3Values); |
|
1078 |
||
1079 |
QMatrix4x3 m1((const qreal *)m1Values); |
|
1080 |
QMatrix4x3 m2((const qreal *)m2Values); |
|
1081 |
QMatrix4x3 m3((const qreal *)m3Values); |
|
1082 |
||
1083 |
QMatrix4x3 m4(m3); |
|
1084 |
m4 -= m1; |
|
1085 |
QVERIFY(isSame(m4, (const qreal *)m2Values)); |
|
1086 |
||
1087 |
QMatrix4x3 m5; |
|
1088 |
m5 = m3 - m1; |
|
1089 |
QVERIFY(isSame(m5, (const qreal *)m2Values)); |
|
1090 |
||
1091 |
QMatrix4x3 m6(m3); |
|
1092 |
m6 -= m2; |
|
1093 |
QVERIFY(isSame(m6, (const qreal *)m1Values)); |
|
1094 |
||
1095 |
QMatrix4x3 m7; |
|
1096 |
m7 = m3 - m2; |
|
1097 |
QVERIFY(isSame(m7, (const qreal *)m1Values)); |
|
1098 |
} |
|
1099 |
||
1100 |
// Test matrix multiplication for 2x2 matrices. |
|
1101 |
void tst_QMatrixNxN::multiply2x2_data() |
|
1102 |
{ |
|
1103 |
QTest::addColumn<void *>("m1Values"); |
|
1104 |
QTest::addColumn<void *>("m2Values"); |
|
1105 |
QTest::addColumn<void *>("m3Values"); |
|
1106 |
||
1107 |
QTest::newRow("null") |
|
1108 |
<< (void *)nullValues2 << (void *)nullValues2 << (void *)nullValues2; |
|
1109 |
||
1110 |
QTest::newRow("null/unique") |
|
1111 |
<< (void *)nullValues2 << (void *)uniqueValues2 << (void *)nullValues2; |
|
1112 |
||
1113 |
QTest::newRow("unique/null") |
|
1114 |
<< (void *)uniqueValues2 << (void *)nullValues2 << (void *)nullValues2; |
|
1115 |
||
1116 |
QTest::newRow("unique/identity") |
|
1117 |
<< (void *)uniqueValues2 << (void *)identityValues2 << (void *)uniqueValues2; |
|
1118 |
||
1119 |
QTest::newRow("identity/unique") |
|
1120 |
<< (void *)identityValues2 << (void *)uniqueValues2 << (void *)uniqueValues2; |
|
1121 |
||
1122 |
static qreal uniqueResult[4]; |
|
1123 |
for (int row = 0; row < 2; ++row) { |
|
1124 |
for (int col = 0; col < 2; ++col) { |
|
1125 |
qreal sum = 0.0f; |
|
1126 |
for (int j = 0; j < 2; ++j) |
|
1127 |
sum += uniqueValues2[row * 2 + j] * transposedValues2[j * 2 + col]; |
|
1128 |
uniqueResult[row * 2 + col] = sum; |
|
1129 |
} |
|
1130 |
} |
|
1131 |
||
1132 |
QTest::newRow("unique/transposed") |
|
1133 |
<< (void *)uniqueValues2 << (void *)transposedValues2 << (void *)uniqueResult; |
|
1134 |
} |
|
1135 |
void tst_QMatrixNxN::multiply2x2() |
|
1136 |
{ |
|
1137 |
QFETCH(void *, m1Values); |
|
1138 |
QFETCH(void *, m2Values); |
|
1139 |
QFETCH(void *, m3Values); |
|
1140 |
||
1141 |
QMatrix2x2 m1((const qreal *)m1Values); |
|
1142 |
QMatrix2x2 m2((const qreal *)m2Values); |
|
1143 |
||
1144 |
QMatrix2x2 m5; |
|
1145 |
m5 = m1 * m2; |
|
1146 |
QVERIFY(isSame(m5, (const qreal *)m3Values)); |
|
1147 |
} |
|
1148 |
||
1149 |
// Test matrix multiplication for 3x3 matrices. |
|
1150 |
void tst_QMatrixNxN::multiply3x3_data() |
|
1151 |
{ |
|
1152 |
QTest::addColumn<void *>("m1Values"); |
|
1153 |
QTest::addColumn<void *>("m2Values"); |
|
1154 |
QTest::addColumn<void *>("m3Values"); |
|
1155 |
||
1156 |
QTest::newRow("null") |
|
1157 |
<< (void *)nullValues3 << (void *)nullValues3 << (void *)nullValues3; |
|
1158 |
||
1159 |
QTest::newRow("null/unique") |
|
1160 |
<< (void *)nullValues3 << (void *)uniqueValues3 << (void *)nullValues3; |
|
1161 |
||
1162 |
QTest::newRow("unique/null") |
|
1163 |
<< (void *)uniqueValues3 << (void *)nullValues3 << (void *)nullValues3; |
|
1164 |
||
1165 |
QTest::newRow("unique/identity") |
|
1166 |
<< (void *)uniqueValues3 << (void *)identityValues3 << (void *)uniqueValues3; |
|
1167 |
||
1168 |
QTest::newRow("identity/unique") |
|
1169 |
<< (void *)identityValues3 << (void *)uniqueValues3 << (void *)uniqueValues3; |
|
1170 |
||
1171 |
static qreal uniqueResult[9]; |
|
1172 |
for (int row = 0; row < 3; ++row) { |
|
1173 |
for (int col = 0; col < 3; ++col) { |
|
1174 |
qreal sum = 0.0f; |
|
1175 |
for (int j = 0; j < 3; ++j) |
|
1176 |
sum += uniqueValues3[row * 3 + j] * transposedValues3[j * 3 + col]; |
|
1177 |
uniqueResult[row * 3 + col] = sum; |
|
1178 |
} |
|
1179 |
} |
|
1180 |
||
1181 |
QTest::newRow("unique/transposed") |
|
1182 |
<< (void *)uniqueValues3 << (void *)transposedValues3 << (void *)uniqueResult; |
|
1183 |
} |
|
1184 |
void tst_QMatrixNxN::multiply3x3() |
|
1185 |
{ |
|
1186 |
QFETCH(void *, m1Values); |
|
1187 |
QFETCH(void *, m2Values); |
|
1188 |
QFETCH(void *, m3Values); |
|
1189 |
||
1190 |
QMatrix3x3 m1((const qreal *)m1Values); |
|
1191 |
QMatrix3x3 m2((const qreal *)m2Values); |
|
1192 |
||
1193 |
QMatrix3x3 m5; |
|
1194 |
m5 = m1 * m2; |
|
1195 |
QVERIFY(isSame(m5, (const qreal *)m3Values)); |
|
1196 |
} |
|
1197 |
||
1198 |
// Test matrix multiplication for 4x4 matrices. |
|
1199 |
void tst_QMatrixNxN::multiply4x4_data() |
|
1200 |
{ |
|
1201 |
QTest::addColumn<void *>("m1Values"); |
|
1202 |
QTest::addColumn<void *>("m2Values"); |
|
1203 |
QTest::addColumn<void *>("m3Values"); |
|
1204 |
||
1205 |
QTest::newRow("null") |
|
1206 |
<< (void *)nullValues4 << (void *)nullValues4 << (void *)nullValues4; |
|
1207 |
||
1208 |
QTest::newRow("null/unique") |
|
1209 |
<< (void *)nullValues4 << (void *)uniqueValues4 << (void *)nullValues4; |
|
1210 |
||
1211 |
QTest::newRow("unique/null") |
|
1212 |
<< (void *)uniqueValues4 << (void *)nullValues4 << (void *)nullValues4; |
|
1213 |
||
1214 |
QTest::newRow("unique/identity") |
|
1215 |
<< (void *)uniqueValues4 << (void *)identityValues4 << (void *)uniqueValues4; |
|
1216 |
||
1217 |
QTest::newRow("identity/unique") |
|
1218 |
<< (void *)identityValues4 << (void *)uniqueValues4 << (void *)uniqueValues4; |
|
1219 |
||
1220 |
static qreal uniqueResult[16]; |
|
1221 |
for (int row = 0; row < 4; ++row) { |
|
1222 |
for (int col = 0; col < 4; ++col) { |
|
1223 |
qreal sum = 0.0f; |
|
1224 |
for (int j = 0; j < 4; ++j) |
|
1225 |
sum += uniqueValues4[row * 4 + j] * transposedValues4[j * 4 + col]; |
|
1226 |
uniqueResult[row * 4 + col] = sum; |
|
1227 |
} |
|
1228 |
} |
|
1229 |
||
1230 |
QTest::newRow("unique/transposed") |
|
1231 |
<< (void *)uniqueValues4 << (void *)transposedValues4 << (void *)uniqueResult; |
|
1232 |
} |
|
1233 |
void tst_QMatrixNxN::multiply4x4() |
|
1234 |
{ |
|
1235 |
QFETCH(void *, m1Values); |
|
1236 |
QFETCH(void *, m2Values); |
|
1237 |
QFETCH(void *, m3Values); |
|
1238 |
||
1239 |
QMatrix4x4 m1((const qreal *)m1Values); |
|
1240 |
QMatrix4x4 m2((const qreal *)m2Values); |
|
1241 |
||
1242 |
QMatrix4x4 m4; |
|
1243 |
m4 = m1; |
|
1244 |
m4 *= m2; |
|
1245 |
QVERIFY(isSame(m4, (const qreal *)m3Values)); |
|
1246 |
||
1247 |
QMatrix4x4 m5; |
|
1248 |
m5 = m1 * m2; |
|
1249 |
QVERIFY(isSame(m5, (const qreal *)m3Values)); |
|
1250 |
} |
|
1251 |
||
1252 |
// Test matrix multiplication for 4x3 matrices. |
|
1253 |
void tst_QMatrixNxN::multiply4x3_data() |
|
1254 |
{ |
|
1255 |
QTest::addColumn<void *>("m1Values"); |
|
1256 |
QTest::addColumn<void *>("m2Values"); |
|
1257 |
QTest::addColumn<void *>("m3Values"); |
|
1258 |
||
1259 |
QTest::newRow("null") |
|
1260 |
<< (void *)nullValues4x3 << (void *)nullValues4x3 << (void *)nullValues3; |
|
1261 |
||
1262 |
QTest::newRow("null/unique") |
|
1263 |
<< (void *)nullValues4x3 << (void *)uniqueValues4x3 << (void *)nullValues3; |
|
1264 |
||
1265 |
QTest::newRow("unique/null") |
|
1266 |
<< (void *)uniqueValues4x3 << (void *)nullValues4x3 << (void *)nullValues3; |
|
1267 |
||
1268 |
static qreal uniqueResult[9]; |
|
1269 |
for (int row = 0; row < 3; ++row) { |
|
1270 |
for (int col = 0; col < 3; ++col) { |
|
1271 |
qreal sum = 0.0f; |
|
1272 |
for (int j = 0; j < 4; ++j) |
|
1273 |
sum += uniqueValues4x3[row * 4 + j] * transposedValues3x4[j * 3 + col]; |
|
1274 |
uniqueResult[row * 3 + col] = sum; |
|
1275 |
} |
|
1276 |
} |
|
1277 |
||
1278 |
QTest::newRow("unique/transposed") |
|
1279 |
<< (void *)uniqueValues4x3 << (void *)transposedValues3x4 << (void *)uniqueResult; |
|
1280 |
} |
|
1281 |
void tst_QMatrixNxN::multiply4x3() |
|
1282 |
{ |
|
1283 |
QFETCH(void *, m1Values); |
|
1284 |
QFETCH(void *, m2Values); |
|
1285 |
QFETCH(void *, m3Values); |
|
1286 |
||
1287 |
QMatrix4x3 m1((const qreal *)m1Values); |
|
1288 |
QMatrix3x4 m2((const qreal *)m2Values); |
|
1289 |
||
1290 |
QGenericMatrix<3, 3, qreal> m4; |
|
1291 |
m4 = m1 * m2; |
|
1292 |
qreal values[9]; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1293 |
m4.copyDataTo(values); |
0 | 1294 |
for (int index = 0; index < 9; ++index) |
1295 |
QCOMPARE(values[index], ((const qreal *)m3Values)[index]); |
|
1296 |
} |
|
1297 |
||
1298 |
// Test matrix multiplication by a factor for 2x2 matrices. |
|
1299 |
void tst_QMatrixNxN::multiplyFactor2x2_data() |
|
1300 |
{ |
|
1301 |
QTest::addColumn<void *>("m1Values"); |
|
1302 |
QTest::addColumn<qreal>("factor"); |
|
1303 |
QTest::addColumn<void *>("m2Values"); |
|
1304 |
||
1305 |
QTest::newRow("null") |
|
1306 |
<< (void *)nullValues2 << (qreal)1.0f << (void *)nullValues2; |
|
1307 |
||
1308 |
QTest::newRow("double identity") |
|
1309 |
<< (void *)identityValues2 << (qreal)2.0f << (void *)doubleIdentity2; |
|
1310 |
||
1311 |
static qreal const values[16] = |
|
1312 |
{1.0f, 2.0f, |
|
1313 |
5.0f, 6.0f}; |
|
1314 |
static qreal const doubleValues[16] = |
|
1315 |
{2.0f, 4.0f, |
|
1316 |
10.0f, 12.0f}; |
|
1317 |
static qreal const negDoubleValues[16] = |
|
1318 |
{-2.0f, -4.0f, |
|
1319 |
-10.0f, -12.0f}; |
|
1320 |
||
1321 |
QTest::newRow("unique") |
|
1322 |
<< (void *)values << (qreal)2.0f << (void *)doubleValues; |
|
1323 |
||
1324 |
QTest::newRow("neg") |
|
1325 |
<< (void *)values << (qreal)-2.0f << (void *)negDoubleValues; |
|
1326 |
||
1327 |
QTest::newRow("zero") |
|
1328 |
<< (void *)values << (qreal)0.0f << (void *)nullValues4; |
|
1329 |
} |
|
1330 |
void tst_QMatrixNxN::multiplyFactor2x2() |
|
1331 |
{ |
|
1332 |
QFETCH(void *, m1Values); |
|
1333 |
QFETCH(qreal, factor); |
|
1334 |
QFETCH(void *, m2Values); |
|
1335 |
||
1336 |
QMatrix2x2 m1((const qreal *)m1Values); |
|
1337 |
||
1338 |
QMatrix2x2 m3; |
|
1339 |
m3 = m1; |
|
1340 |
m3 *= factor; |
|
1341 |
QVERIFY(isSame(m3, (const qreal *)m2Values)); |
|
1342 |
||
1343 |
QMatrix2x2 m4; |
|
1344 |
m4 = m1 * factor; |
|
1345 |
QVERIFY(isSame(m4, (const qreal *)m2Values)); |
|
1346 |
||
1347 |
QMatrix2x2 m5; |
|
1348 |
m5 = factor * m1; |
|
1349 |
QVERIFY(isSame(m5, (const qreal *)m2Values)); |
|
1350 |
} |
|
1351 |
||
1352 |
// Test matrix multiplication by a factor for 3x3 matrices. |
|
1353 |
void tst_QMatrixNxN::multiplyFactor3x3_data() |
|
1354 |
{ |
|
1355 |
QTest::addColumn<void *>("m1Values"); |
|
1356 |
QTest::addColumn<qreal>("factor"); |
|
1357 |
QTest::addColumn<void *>("m2Values"); |
|
1358 |
||
1359 |
QTest::newRow("null") |
|
1360 |
<< (void *)nullValues3 << (qreal)1.0f << (void *)nullValues3; |
|
1361 |
||
1362 |
QTest::newRow("double identity") |
|
1363 |
<< (void *)identityValues3 << (qreal)2.0f << (void *)doubleIdentity3; |
|
1364 |
||
1365 |
static qreal const values[16] = |
|
1366 |
{1.0f, 2.0f, 3.0f, |
|
1367 |
5.0f, 6.0f, 7.0f, |
|
1368 |
9.0f, 10.0f, 11.0f}; |
|
1369 |
static qreal const doubleValues[16] = |
|
1370 |
{2.0f, 4.0f, 6.0f, |
|
1371 |
10.0f, 12.0f, 14.0f, |
|
1372 |
18.0f, 20.0f, 22.0f}; |
|
1373 |
static qreal const negDoubleValues[16] = |
|
1374 |
{-2.0f, -4.0f, -6.0f, |
|
1375 |
-10.0f, -12.0f, -14.0f, |
|
1376 |
-18.0f, -20.0f, -22.0f}; |
|
1377 |
||
1378 |
QTest::newRow("unique") |
|
1379 |
<< (void *)values << (qreal)2.0f << (void *)doubleValues; |
|
1380 |
||
1381 |
QTest::newRow("neg") |
|
1382 |
<< (void *)values << (qreal)-2.0f << (void *)negDoubleValues; |
|
1383 |
||
1384 |
QTest::newRow("zero") |
|
1385 |
<< (void *)values << (qreal)0.0f << (void *)nullValues4; |
|
1386 |
} |
|
1387 |
void tst_QMatrixNxN::multiplyFactor3x3() |
|
1388 |
{ |
|
1389 |
QFETCH(void *, m1Values); |
|
1390 |
QFETCH(qreal, factor); |
|
1391 |
QFETCH(void *, m2Values); |
|
1392 |
||
1393 |
QMatrix3x3 m1((const qreal *)m1Values); |
|
1394 |
||
1395 |
QMatrix3x3 m3; |
|
1396 |
m3 = m1; |
|
1397 |
m3 *= factor; |
|
1398 |
QVERIFY(isSame(m3, (const qreal *)m2Values)); |
|
1399 |
||
1400 |
QMatrix3x3 m4; |
|
1401 |
m4 = m1 * factor; |
|
1402 |
QVERIFY(isSame(m4, (const qreal *)m2Values)); |
|
1403 |
||
1404 |
QMatrix3x3 m5; |
|
1405 |
m5 = factor * m1; |
|
1406 |
QVERIFY(isSame(m5, (const qreal *)m2Values)); |
|
1407 |
} |
|
1408 |
||
1409 |
// Test matrix multiplication by a factor for 4x4 matrices. |
|
1410 |
void tst_QMatrixNxN::multiplyFactor4x4_data() |
|
1411 |
{ |
|
1412 |
QTest::addColumn<void *>("m1Values"); |
|
1413 |
QTest::addColumn<qreal>("factor"); |
|
1414 |
QTest::addColumn<void *>("m2Values"); |
|
1415 |
||
1416 |
QTest::newRow("null") |
|
1417 |
<< (void *)nullValues4 << (qreal)1.0f << (void *)nullValues4; |
|
1418 |
||
1419 |
QTest::newRow("double identity") |
|
1420 |
<< (void *)identityValues4 << (qreal)2.0f << (void *)doubleIdentity4; |
|
1421 |
||
1422 |
static qreal const values[16] = |
|
1423 |
{1.0f, 2.0f, 3.0f, 4.0f, |
|
1424 |
5.0f, 6.0f, 7.0f, 8.0f, |
|
1425 |
9.0f, 10.0f, 11.0f, 12.0f, |
|
1426 |
13.0f, 14.0f, 15.0f, 16.0f}; |
|
1427 |
static qreal const doubleValues[16] = |
|
1428 |
{2.0f, 4.0f, 6.0f, 8.0f, |
|
1429 |
10.0f, 12.0f, 14.0f, 16.0f, |
|
1430 |
18.0f, 20.0f, 22.0f, 24.0f, |
|
1431 |
26.0f, 28.0f, 30.0f, 32.0f}; |
|
1432 |
static qreal const negDoubleValues[16] = |
|
1433 |
{-2.0f, -4.0f, -6.0f, -8.0f, |
|
1434 |
-10.0f, -12.0f, -14.0f, -16.0f, |
|
1435 |
-18.0f, -20.0f, -22.0f, -24.0f, |
|
1436 |
-26.0f, -28.0f, -30.0f, -32.0f}; |
|
1437 |
||
1438 |
QTest::newRow("unique") |
|
1439 |
<< (void *)values << (qreal)2.0f << (void *)doubleValues; |
|
1440 |
||
1441 |
QTest::newRow("neg") |
|
1442 |
<< (void *)values << (qreal)-2.0f << (void *)negDoubleValues; |
|
1443 |
||
1444 |
QTest::newRow("zero") |
|
1445 |
<< (void *)values << (qreal)0.0f << (void *)nullValues4; |
|
1446 |
} |
|
1447 |
void tst_QMatrixNxN::multiplyFactor4x4() |
|
1448 |
{ |
|
1449 |
QFETCH(void *, m1Values); |
|
1450 |
QFETCH(qreal, factor); |
|
1451 |
QFETCH(void *, m2Values); |
|
1452 |
||
1453 |
QMatrix4x4 m1((const qreal *)m1Values); |
|
1454 |
||
1455 |
QMatrix4x4 m3; |
|
1456 |
m3 = m1; |
|
1457 |
m3 *= factor; |
|
1458 |
QVERIFY(isSame(m3, (const qreal *)m2Values)); |
|
1459 |
||
1460 |
QMatrix4x4 m4; |
|
1461 |
m4 = m1 * factor; |
|
1462 |
QVERIFY(isSame(m4, (const qreal *)m2Values)); |
|
1463 |
||
1464 |
QMatrix4x4 m5; |
|
1465 |
m5 = factor * m1; |
|
1466 |
QVERIFY(isSame(m5, (const qreal *)m2Values)); |
|
1467 |
} |
|
1468 |
||
1469 |
// Test matrix multiplication by a factor for 4x3 matrices. |
|
1470 |
void tst_QMatrixNxN::multiplyFactor4x3_data() |
|
1471 |
{ |
|
1472 |
QTest::addColumn<void *>("m1Values"); |
|
1473 |
QTest::addColumn<qreal>("factor"); |
|
1474 |
QTest::addColumn<void *>("m2Values"); |
|
1475 |
||
1476 |
QTest::newRow("null") |
|
1477 |
<< (void *)nullValues4x3 << (qreal)1.0f << (void *)nullValues4x3; |
|
1478 |
||
1479 |
QTest::newRow("double identity") |
|
1480 |
<< (void *)identityValues4x3 << (qreal)2.0f << (void *)doubleIdentity4x3; |
|
1481 |
||
1482 |
static qreal const values[12] = |
|
1483 |
{1.0f, 2.0f, 3.0f, 4.0f, |
|
1484 |
5.0f, 6.0f, 7.0f, 8.0f, |
|
1485 |
9.0f, 10.0f, 11.0f, 12.0f}; |
|
1486 |
static qreal const doubleValues[12] = |
|
1487 |
{2.0f, 4.0f, 6.0f, 8.0f, |
|
1488 |
10.0f, 12.0f, 14.0f, 16.0f, |
|
1489 |
18.0f, 20.0f, 22.0f, 24.0f}; |
|
1490 |
static qreal const negDoubleValues[12] = |
|
1491 |
{-2.0f, -4.0f, -6.0f, -8.0f, |
|
1492 |
-10.0f, -12.0f, -14.0f, -16.0f, |
|
1493 |
-18.0f, -20.0f, -22.0f, -24.0f}; |
|
1494 |
||
1495 |
QTest::newRow("unique") |
|
1496 |
<< (void *)values << (qreal)2.0f << (void *)doubleValues; |
|
1497 |
||
1498 |
QTest::newRow("neg") |
|
1499 |
<< (void *)values << (qreal)-2.0f << (void *)negDoubleValues; |
|
1500 |
||
1501 |
QTest::newRow("zero") |
|
1502 |
<< (void *)values << (qreal)0.0f << (void *)nullValues4x3; |
|
1503 |
} |
|
1504 |
void tst_QMatrixNxN::multiplyFactor4x3() |
|
1505 |
{ |
|
1506 |
QFETCH(void *, m1Values); |
|
1507 |
QFETCH(qreal, factor); |
|
1508 |
QFETCH(void *, m2Values); |
|
1509 |
||
1510 |
QMatrix4x3 m1((const qreal *)m1Values); |
|
1511 |
||
1512 |
QMatrix4x3 m3; |
|
1513 |
m3 = m1; |
|
1514 |
m3 *= factor; |
|
1515 |
QVERIFY(isSame(m3, (const qreal *)m2Values)); |
|
1516 |
||
1517 |
QMatrix4x3 m4; |
|
1518 |
m4 = m1 * factor; |
|
1519 |
QVERIFY(isSame(m4, (const qreal *)m2Values)); |
|
1520 |
||
1521 |
QMatrix4x3 m5; |
|
1522 |
m5 = factor * m1; |
|
1523 |
QVERIFY(isSame(m5, (const qreal *)m2Values)); |
|
1524 |
} |
|
1525 |
||
1526 |
// Test matrix division by a factor for 2x2 matrices. |
|
1527 |
void tst_QMatrixNxN::divideFactor2x2_data() |
|
1528 |
{ |
|
1529 |
// Use the same test cases as the multiplyFactor test. |
|
1530 |
multiplyFactor2x2_data(); |
|
1531 |
} |
|
1532 |
void tst_QMatrixNxN::divideFactor2x2() |
|
1533 |
{ |
|
1534 |
QFETCH(void *, m1Values); |
|
1535 |
QFETCH(qreal, factor); |
|
1536 |
QFETCH(void *, m2Values); |
|
1537 |
||
1538 |
if (factor == 0.0f) |
|
1539 |
return; |
|
1540 |
||
1541 |
QMatrix2x2 m2((const qreal *)m2Values); |
|
1542 |
||
1543 |
QMatrix2x2 m3; |
|
1544 |
m3 = m2; |
|
1545 |
m3 /= factor; |
|
1546 |
QVERIFY(isSame(m3, (const qreal *)m1Values)); |
|
1547 |
||
1548 |
QMatrix2x2 m4; |
|
1549 |
m4 = m2 / factor; |
|
1550 |
QVERIFY(isSame(m4, (const qreal *)m1Values)); |
|
1551 |
} |
|
1552 |
||
1553 |
// Test matrix division by a factor for 3x3 matrices. |
|
1554 |
void tst_QMatrixNxN::divideFactor3x3_data() |
|
1555 |
{ |
|
1556 |
// Use the same test cases as the multiplyFactor test. |
|
1557 |
multiplyFactor3x3_data(); |
|
1558 |
} |
|
1559 |
void tst_QMatrixNxN::divideFactor3x3() |
|
1560 |
{ |
|
1561 |
QFETCH(void *, m1Values); |
|
1562 |
QFETCH(qreal, factor); |
|
1563 |
QFETCH(void *, m2Values); |
|
1564 |
||
1565 |
if (factor == 0.0f) |
|
1566 |
return; |
|
1567 |
||
1568 |
QMatrix3x3 m2((const qreal *)m2Values); |
|
1569 |
||
1570 |
QMatrix3x3 m3; |
|
1571 |
m3 = m2; |
|
1572 |
m3 /= factor; |
|
1573 |
QVERIFY(isSame(m3, (const qreal *)m1Values)); |
|
1574 |
||
1575 |
QMatrix3x3 m4; |
|
1576 |
m4 = m2 / factor; |
|
1577 |
QVERIFY(isSame(m4, (const qreal *)m1Values)); |
|
1578 |
} |
|
1579 |
||
1580 |
// Test matrix division by a factor for 4x4 matrices. |
|
1581 |
void tst_QMatrixNxN::divideFactor4x4_data() |
|
1582 |
{ |
|
1583 |
// Use the same test cases as the multiplyFactor test. |
|
1584 |
multiplyFactor4x4_data(); |
|
1585 |
} |
|
1586 |
void tst_QMatrixNxN::divideFactor4x4() |
|
1587 |
{ |
|
1588 |
QFETCH(void *, m1Values); |
|
1589 |
QFETCH(qreal, factor); |
|
1590 |
QFETCH(void *, m2Values); |
|
1591 |
||
1592 |
if (factor == 0.0f) |
|
1593 |
return; |
|
1594 |
||
1595 |
QMatrix4x4 m2((const qreal *)m2Values); |
|
1596 |
||
1597 |
QMatrix4x4 m3; |
|
1598 |
m3 = m2; |
|
1599 |
m3 /= factor; |
|
1600 |
QVERIFY(isSame(m3, (const qreal *)m1Values)); |
|
1601 |
||
1602 |
QMatrix4x4 m4; |
|
1603 |
m4 = m2 / factor; |
|
1604 |
QVERIFY(isSame(m4, (const qreal *)m1Values)); |
|
1605 |
} |
|
1606 |
||
1607 |
// Test matrix division by a factor for 4x3 matrices. |
|
1608 |
void tst_QMatrixNxN::divideFactor4x3_data() |
|
1609 |
{ |
|
1610 |
// Use the same test cases as the multiplyFactor test. |
|
1611 |
multiplyFactor4x3_data(); |
|
1612 |
} |
|
1613 |
void tst_QMatrixNxN::divideFactor4x3() |
|
1614 |
{ |
|
1615 |
QFETCH(void *, m1Values); |
|
1616 |
QFETCH(qreal, factor); |
|
1617 |
QFETCH(void *, m2Values); |
|
1618 |
||
1619 |
if (factor == 0.0f) |
|
1620 |
return; |
|
1621 |
||
1622 |
QMatrix4x3 m2((const qreal *)m2Values); |
|
1623 |
||
1624 |
QMatrix4x3 m3; |
|
1625 |
m3 = m2; |
|
1626 |
m3 /= factor; |
|
1627 |
QVERIFY(isSame(m3, (const qreal *)m1Values)); |
|
1628 |
||
1629 |
QMatrix4x3 m4; |
|
1630 |
m4 = m2 / factor; |
|
1631 |
QVERIFY(isSame(m4, (const qreal *)m1Values)); |
|
1632 |
} |
|
1633 |
||
1634 |
// Test matrix negation for 2x2 matrices. |
|
1635 |
void tst_QMatrixNxN::negate2x2_data() |
|
1636 |
{ |
|
1637 |
// Use the same test cases as the multiplyFactor test. |
|
1638 |
multiplyFactor2x2_data(); |
|
1639 |
} |
|
1640 |
void tst_QMatrixNxN::negate2x2() |
|
1641 |
{ |
|
1642 |
QFETCH(void *, m1Values); |
|
1643 |
||
1644 |
const qreal *values = (const qreal *)m1Values; |
|
1645 |
||
1646 |
QMatrix2x2 m1(values); |
|
1647 |
||
1648 |
qreal negated[4]; |
|
1649 |
for (int index = 0; index < 4; ++index) |
|
1650 |
negated[index] = -values[index]; |
|
1651 |
||
1652 |
QMatrix2x2 m2; |
|
1653 |
m2 = -m1; |
|
1654 |
QVERIFY(isSame(m2, negated)); |
|
1655 |
} |
|
1656 |
||
1657 |
// Test matrix negation for 3x3 matrices. |
|
1658 |
void tst_QMatrixNxN::negate3x3_data() |
|
1659 |
{ |
|
1660 |
// Use the same test cases as the multiplyFactor test. |
|
1661 |
multiplyFactor3x3_data(); |
|
1662 |
} |
|
1663 |
void tst_QMatrixNxN::negate3x3() |
|
1664 |
{ |
|
1665 |
QFETCH(void *, m1Values); |
|
1666 |
||
1667 |
const qreal *values = (const qreal *)m1Values; |
|
1668 |
||
1669 |
QMatrix3x3 m1(values); |
|
1670 |
||
1671 |
qreal negated[9]; |
|
1672 |
for (int index = 0; index < 9; ++index) |
|
1673 |
negated[index] = -values[index]; |
|
1674 |
||
1675 |
QMatrix3x3 m2; |
|
1676 |
m2 = -m1; |
|
1677 |
QVERIFY(isSame(m2, negated)); |
|
1678 |
} |
|
1679 |
||
1680 |
// Test matrix negation for 4x4 matrices. |
|
1681 |
void tst_QMatrixNxN::negate4x4_data() |
|
1682 |
{ |
|
1683 |
// Use the same test cases as the multiplyFactor test. |
|
1684 |
multiplyFactor4x4_data(); |
|
1685 |
} |
|
1686 |
void tst_QMatrixNxN::negate4x4() |
|
1687 |
{ |
|
1688 |
QFETCH(void *, m1Values); |
|
1689 |
||
1690 |
const qreal *values = (const qreal *)m1Values; |
|
1691 |
||
1692 |
QMatrix4x4 m1(values); |
|
1693 |
||
1694 |
qreal negated[16]; |
|
1695 |
for (int index = 0; index < 16; ++index) |
|
1696 |
negated[index] = -values[index]; |
|
1697 |
||
1698 |
QMatrix4x4 m2; |
|
1699 |
m2 = -m1; |
|
1700 |
QVERIFY(isSame(m2, negated)); |
|
1701 |
} |
|
1702 |
||
1703 |
// Test matrix negation for 4x3 matrices. |
|
1704 |
void tst_QMatrixNxN::negate4x3_data() |
|
1705 |
{ |
|
1706 |
// Use the same test cases as the multiplyFactor test. |
|
1707 |
multiplyFactor4x3_data(); |
|
1708 |
} |
|
1709 |
void tst_QMatrixNxN::negate4x3() |
|
1710 |
{ |
|
1711 |
QFETCH(void *, m1Values); |
|
1712 |
||
1713 |
const qreal *values = (const qreal *)m1Values; |
|
1714 |
||
1715 |
QMatrix4x3 m1(values); |
|
1716 |
||
1717 |
qreal negated[12]; |
|
1718 |
for (int index = 0; index < 12; ++index) |
|
1719 |
negated[index] = -values[index]; |
|
1720 |
||
1721 |
QMatrix4x3 m2; |
|
1722 |
m2 = -m1; |
|
1723 |
QVERIFY(isSame(m2, negated)); |
|
1724 |
} |
|
1725 |
||
1726 |
// Matrix inverted. This is a more straight-forward implementation |
|
1727 |
// of the algorithm at http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q24 |
|
1728 |
// than the optimized version in the QMatrix4x4 code. Hopefully it is |
|
1729 |
// easier to verify that this version is the same as the reference. |
|
1730 |
||
1731 |
struct Matrix3 |
|
1732 |
{ |
|
1733 |
qreal v[9]; |
|
1734 |
}; |
|
1735 |
struct Matrix4 |
|
1736 |
{ |
|
1737 |
qreal v[16]; |
|
1738 |
}; |
|
1739 |
||
1740 |
static qreal m3Determinant(const Matrix3& m) |
|
1741 |
{ |
|
1742 |
return m.v[0] * (m.v[4] * m.v[8] - m.v[7] * m.v[5]) - |
|
1743 |
m.v[1] * (m.v[3] * m.v[8] - m.v[6] * m.v[5]) + |
|
1744 |
m.v[2] * (m.v[3] * m.v[7] - m.v[6] * m.v[4]); |
|
1745 |
} |
|
1746 |
||
1747 |
static bool m3Inverse(const Matrix3& min, Matrix3& mout) |
|
1748 |
{ |
|
1749 |
qreal det = m3Determinant(min); |
|
1750 |
if (det == 0.0f) |
|
1751 |
return false; |
|
1752 |
mout.v[0] = (min.v[4] * min.v[8] - min.v[5] * min.v[7]) / det; |
|
1753 |
mout.v[1] = -(min.v[1] * min.v[8] - min.v[2] * min.v[7]) / det; |
|
1754 |
mout.v[2] = (min.v[1] * min.v[5] - min.v[4] * min.v[2]) / det; |
|
1755 |
mout.v[3] = -(min.v[3] * min.v[8] - min.v[5] * min.v[6]) / det; |
|
1756 |
mout.v[4] = (min.v[0] * min.v[8] - min.v[6] * min.v[2]) / det; |
|
1757 |
mout.v[5] = -(min.v[0] * min.v[5] - min.v[3] * min.v[2]) / det; |
|
1758 |
mout.v[6] = (min.v[3] * min.v[7] - min.v[6] * min.v[4]) / det; |
|
1759 |
mout.v[7] = -(min.v[0] * min.v[7] - min.v[6] * min.v[1]) / det; |
|
1760 |
mout.v[8] = (min.v[0] * min.v[4] - min.v[1] * min.v[3]) / det; |
|
1761 |
return true; |
|
1762 |
} |
|
1763 |
||
1764 |
static void m3Transpose(Matrix3& m) |
|
1765 |
{ |
|
1766 |
qSwap(m.v[1], m.v[3]); |
|
1767 |
qSwap(m.v[2], m.v[6]); |
|
1768 |
qSwap(m.v[5], m.v[7]); |
|
1769 |
} |
|
1770 |
||
1771 |
static void m4Submatrix(const Matrix4& min, Matrix3& mout, int i, int j) |
|
1772 |
{ |
|
1773 |
for (int di = 0; di < 3; ++di) { |
|
1774 |
for (int dj = 0; dj < 3; ++dj) { |
|
1775 |
int si = di + ((di >= i) ? 1 : 0); |
|
1776 |
int sj = dj + ((dj >= j) ? 1 : 0); |
|
1777 |
mout.v[di * 3 + dj] = min.v[si * 4 + sj]; |
|
1778 |
} |
|
1779 |
} |
|
1780 |
} |
|
1781 |
||
1782 |
static qreal m4Determinant(const Matrix4& m) |
|
1783 |
{ |
|
1784 |
qreal det; |
|
1785 |
qreal result = 0.0f; |
|
1786 |
qreal i = 1.0f; |
|
1787 |
Matrix3 msub; |
|
1788 |
for (int n = 0; n < 4; ++n, i *= -1.0f) { |
|
1789 |
m4Submatrix(m, msub, 0, n); |
|
1790 |
det = m3Determinant(msub); |
|
1791 |
result += m.v[n] * det * i; |
|
1792 |
} |
|
1793 |
return result; |
|
1794 |
} |
|
1795 |
||
1796 |
static void m4Inverse(const Matrix4& min, Matrix4& mout) |
|
1797 |
{ |
|
1798 |
qreal det = m4Determinant(min); |
|
1799 |
Matrix3 msub; |
|
1800 |
for (int i = 0; i < 4; ++i) { |
|
1801 |
for (int j = 0; j < 4; ++j) { |
|
1802 |
qreal sign = 1.0f - ((i + j) % 2) * 2.0f; |
|
1803 |
m4Submatrix(min, msub, i, j); |
|
1804 |
mout.v[i + j * 4] = (m3Determinant(msub) * sign) / det; |
|
1805 |
} |
|
1806 |
} |
|
1807 |
} |
|
1808 |
||
1809 |
// Test matrix inverted for 4x4 matrices. |
|
1810 |
void tst_QMatrixNxN::inverted4x4_data() |
|
1811 |
{ |
|
1812 |
QTest::addColumn<void *>("m1Values"); |
|
1813 |
QTest::addColumn<void *>("m2Values"); |
|
1814 |
QTest::addColumn<bool>("invertible"); |
|
1815 |
||
1816 |
QTest::newRow("null") |
|
1817 |
<< (void *)nullValues4 << (void *)identityValues4 << false; |
|
1818 |
||
1819 |
QTest::newRow("identity") |
|
1820 |
<< (void *)identityValues4 << (void *)identityValues4 << true; |
|
1821 |
||
1822 |
QTest::newRow("unique") |
|
1823 |
<< (void *)uniqueValues4 << (void *)identityValues4 << false; |
|
1824 |
||
1825 |
static Matrix4 const invertible = { |
|
1826 |
{5.0f, 0.0f, 0.0f, 2.0f, |
|
1827 |
0.0f, 6.0f, 0.0f, 3.0f, |
|
1828 |
0.0f, 0.0f, 7.0f, 4.0f, |
|
1829 |
0.0f, 0.0f, 0.0f, 1.0f} |
|
1830 |
}; |
|
1831 |
static Matrix4 inverted; |
|
1832 |
m4Inverse(invertible, inverted); |
|
1833 |
||
1834 |
QTest::newRow("invertible") |
|
1835 |
<< (void *)invertible.v << (void *)inverted.v << true; |
|
1836 |
||
1837 |
static Matrix4 const translate = { |
|
1838 |
{1.0f, 0.0f, 0.0f, 2.0f, |
|
1839 |
0.0f, 1.0f, 0.0f, 3.0f, |
|
1840 |
0.0f, 0.0f, 1.0f, 4.0f, |
|
1841 |
0.0f, 0.0f, 0.0f, 1.0f} |
|
1842 |
}; |
|
1843 |
static Matrix4 const inverseTranslate = { |
|
1844 |
{1.0f, 0.0f, 0.0f, -2.0f, |
|
1845 |
0.0f, 1.0f, 0.0f, -3.0f, |
|
1846 |
0.0f, 0.0f, 1.0f, -4.0f, |
|
1847 |
0.0f, 0.0f, 0.0f, 1.0f} |
|
1848 |
}; |
|
1849 |
||
1850 |
QTest::newRow("translate") |
|
1851 |
<< (void *)translate.v << (void *)inverseTranslate.v << true; |
|
1852 |
} |
|
1853 |
void tst_QMatrixNxN::inverted4x4() |
|
1854 |
{ |
|
1855 |
QFETCH(void *, m1Values); |
|
1856 |
QFETCH(void *, m2Values); |
|
1857 |
QFETCH(bool, invertible); |
|
1858 |
||
1859 |
QMatrix4x4 m1((const qreal *)m1Values); |
|
1860 |
||
1861 |
if (invertible) |
|
1862 |
QVERIFY(m1.determinant() != 0.0f); |
|
1863 |
else |
|
1864 |
QVERIFY(m1.determinant() == 0.0f); |
|
1865 |
||
1866 |
Matrix4 m1alt; |
|
1867 |
memcpy(m1alt.v, (const qreal *)m1Values, sizeof(m1alt.v)); |
|
1868 |
||
1869 |
QCOMPARE(m1.determinant(), m4Determinant(m1alt)); |
|
1870 |
||
1871 |
QMatrix4x4 m2; |
|
1872 |
bool inv; |
|
1873 |
m2 = m1.inverted(&inv); |
|
1874 |
QVERIFY(isSame(m2, (const qreal *)m2Values)); |
|
1875 |
||
1876 |
if (invertible) { |
|
1877 |
QVERIFY(inv); |
|
1878 |
||
1879 |
Matrix4 m2alt; |
|
1880 |
m4Inverse(m1alt, m2alt); |
|
1881 |
QVERIFY(isSame(m2, m2alt.v)); |
|
1882 |
||
1883 |
QMatrix4x4 m3; |
|
1884 |
m3 = m1 * m2; |
|
1885 |
QVERIFY(isIdentity(m3)); |
|
1886 |
||
1887 |
QMatrix4x4 m4; |
|
1888 |
m4 = m2 * m1; |
|
1889 |
QVERIFY(isIdentity(m4)); |
|
1890 |
} else { |
|
1891 |
QVERIFY(!inv); |
|
1892 |
} |
|
1893 |
||
1894 |
// Test again, after inferring the special matrix type. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1895 |
m1.optimize(); |
0 | 1896 |
m2 = m1.inverted(&inv); |
1897 |
QVERIFY(isSame(m2, (const qreal *)m2Values)); |
|
1898 |
QCOMPARE(inv, invertible); |
|
1899 |
} |
|
1900 |
||
1901 |
void tst_QMatrixNxN::orthonormalInverse4x4() |
|
1902 |
{ |
|
1903 |
QMatrix4x4 m1; |
|
1904 |
QVERIFY(qFuzzyCompare(m1.inverted(), m1)); |
|
1905 |
||
1906 |
QMatrix4x4 m2; |
|
1907 |
m2.rotate(45.0, 1.0, 0.0, 0.0); |
|
1908 |
m2.translate(10.0, 0.0, 0.0); |
|
1909 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1910 |
// Use optimize() to drop the internal flags that |
0 | 1911 |
// mark the matrix as orthonormal. This will force inverted() |
1912 |
// to compute m3.inverted() the long way. We can then compare |
|
1913 |
// the result to what the faster algorithm produces on m2. |
|
1914 |
QMatrix4x4 m3 = m2; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1915 |
m3.optimize(); |
0 | 1916 |
bool invertible; |
1917 |
QVERIFY(qFuzzyCompare(m2.inverted(&invertible), m3.inverted())); |
|
1918 |
QVERIFY(invertible); |
|
1919 |
||
1920 |
QMatrix4x4 m4; |
|
1921 |
m4.rotate(45.0, 0.0, 1.0, 0.0); |
|
1922 |
QMatrix4x4 m5 = m4; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1923 |
m5.optimize(); |
0 | 1924 |
QVERIFY(qFuzzyCompare(m4.inverted(), m5.inverted())); |
1925 |
||
1926 |
QMatrix4x4 m6; |
|
1927 |
m1.rotate(88, 0.0, 0.0, 1.0); |
|
1928 |
m1.translate(-20.0, 20.0, 15.0); |
|
1929 |
m1.rotate(25, 1.0, 0.0, 0.0); |
|
1930 |
QMatrix4x4 m7 = m6; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1931 |
m7.optimize(); |
0 | 1932 |
QVERIFY(qFuzzyCompare(m6.inverted(), m7.inverted())); |
1933 |
} |
|
1934 |
||
1935 |
// Test the generation and use of 4x4 scale matrices. |
|
1936 |
void tst_QMatrixNxN::scale4x4_data() |
|
1937 |
{ |
|
1938 |
QTest::addColumn<qreal>("x"); |
|
1939 |
QTest::addColumn<qreal>("y"); |
|
1940 |
QTest::addColumn<qreal>("z"); |
|
1941 |
QTest::addColumn<void *>("resultValues"); |
|
1942 |
||
1943 |
static const qreal nullScale[] = |
|
1944 |
{0.0f, 0.0f, 0.0f, 0.0f, |
|
1945 |
0.0f, 0.0f, 0.0f, 0.0f, |
|
1946 |
0.0f, 0.0f, 0.0f, 0.0f, |
|
1947 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
1948 |
QTest::newRow("null") |
|
1949 |
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (void *)nullScale; |
|
1950 |
||
1951 |
QTest::newRow("identity") |
|
1952 |
<< (qreal)1.0f << (qreal)1.0f << (qreal)1.0f << (void *)identityValues4; |
|
1953 |
||
1954 |
static const qreal doubleScale[] = |
|
1955 |
{2.0f, 0.0f, 0.0f, 0.0f, |
|
1956 |
0.0f, 2.0f, 0.0f, 0.0f, |
|
1957 |
0.0f, 0.0f, 2.0f, 0.0f, |
|
1958 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
1959 |
QTest::newRow("double") |
|
1960 |
<< (qreal)2.0f << (qreal)2.0f << (qreal)2.0f << (void *)doubleScale; |
|
1961 |
||
1962 |
static const qreal complexScale[] = |
|
1963 |
{2.0f, 0.0f, 0.0f, 0.0f, |
|
1964 |
0.0f, 11.0f, 0.0f, 0.0f, |
|
1965 |
0.0f, 0.0f, -6.5f, 0.0f, |
|
1966 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
1967 |
QTest::newRow("complex") |
|
1968 |
<< (qreal)2.0f << (qreal)11.0f << (qreal)-6.5f << (void *)complexScale; |
|
1969 |
||
1970 |
static const qreal complexScale2D[] = |
|
1971 |
{2.0f, 0.0f, 0.0f, 0.0f, |
|
1972 |
0.0f, -11.0f, 0.0f, 0.0f, |
|
1973 |
0.0f, 0.0f, 1.0f, 0.0f, |
|
1974 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
1975 |
QTest::newRow("complex2D") |
|
1976 |
<< (qreal)2.0f << (qreal)-11.0f << (qreal)1.0f << (void *)complexScale2D; |
|
1977 |
} |
|
1978 |
void tst_QMatrixNxN::scale4x4() |
|
1979 |
{ |
|
1980 |
QFETCH(qreal, x); |
|
1981 |
QFETCH(qreal, y); |
|
1982 |
QFETCH(qreal, z); |
|
1983 |
QFETCH(void *, resultValues); |
|
1984 |
||
1985 |
QMatrix4x4 result((const qreal *)resultValues); |
|
1986 |
||
1987 |
QMatrix4x4 m1; |
|
1988 |
m1.scale(QVector3D(x, y, z)); |
|
1989 |
QVERIFY(isSame(m1, (const qreal *)resultValues)); |
|
1990 |
||
1991 |
QMatrix4x4 m2; |
|
1992 |
m2.scale(x, y, z); |
|
1993 |
QVERIFY(isSame(m2, (const qreal *)resultValues)); |
|
1994 |
||
1995 |
if (z == 1.0f) { |
|
1996 |
QMatrix4x4 m2b; |
|
1997 |
m2b.scale(x, y); |
|
1998 |
QVERIFY(m2b == m2); |
|
1999 |
} |
|
2000 |
||
2001 |
QVector3D v1(2.0f, 3.0f, -4.0f); |
|
2002 |
QVector3D v2 = m1 * v1; |
|
2003 |
QCOMPARE(v2.x(), (qreal)(2.0f * x)); |
|
2004 |
QCOMPARE(v2.y(), (qreal)(3.0f * y)); |
|
2005 |
QCOMPARE(v2.z(), (qreal)(-4.0f * z)); |
|
2006 |
||
2007 |
v2 = v1 * m1; |
|
2008 |
QCOMPARE(v2.x(), (qreal)(2.0f * x)); |
|
2009 |
QCOMPARE(v2.y(), (qreal)(3.0f * y)); |
|
2010 |
QCOMPARE(v2.z(), (qreal)(-4.0f * z)); |
|
2011 |
||
2012 |
QVector4D v3(2.0f, 3.0f, -4.0f, 34.0f); |
|
2013 |
QVector4D v4 = m1 * v3; |
|
2014 |
QCOMPARE(v4.x(), (qreal)(2.0f * x)); |
|
2015 |
QCOMPARE(v4.y(), (qreal)(3.0f * y)); |
|
2016 |
QCOMPARE(v4.z(), (qreal)(-4.0f * z)); |
|
2017 |
QCOMPARE(v4.w(), (qreal)34.0f); |
|
2018 |
||
2019 |
v4 = v3 * m1; |
|
2020 |
QCOMPARE(v4.x(), (qreal)(2.0f * x)); |
|
2021 |
QCOMPARE(v4.y(), (qreal)(3.0f * y)); |
|
2022 |
QCOMPARE(v4.z(), (qreal)(-4.0f * z)); |
|
2023 |
QCOMPARE(v4.w(), (qreal)34.0f); |
|
2024 |
||
2025 |
QPoint p1(2, 3); |
|
2026 |
QPoint p2 = m1 * p1; |
|
2027 |
QCOMPARE(p2.x(), (int)(2.0f * x)); |
|
2028 |
QCOMPARE(p2.y(), (int)(3.0f * y)); |
|
2029 |
||
2030 |
p2 = p1 * m1; |
|
2031 |
QCOMPARE(p2.x(), (int)(2.0f * x)); |
|
2032 |
QCOMPARE(p2.y(), (int)(3.0f * y)); |
|
2033 |
||
2034 |
QPointF p3(2.0f, 3.0f); |
|
2035 |
QPointF p4 = m1 * p3; |
|
2036 |
QCOMPARE(p4.x(), (qreal)(2.0f * x)); |
|
2037 |
QCOMPARE(p4.y(), (qreal)(3.0f * y)); |
|
2038 |
||
2039 |
p4 = p3 * m1; |
|
2040 |
QCOMPARE(p4.x(), (qreal)(2.0f * x)); |
|
2041 |
QCOMPARE(p4.y(), (qreal)(3.0f * y)); |
|
2042 |
||
2043 |
QMatrix4x4 m3(uniqueValues4); |
|
2044 |
QMatrix4x4 m4(m3); |
|
2045 |
m4.scale(x, y, z); |
|
2046 |
QVERIFY(m4 == m3 * m1); |
|
2047 |
||
2048 |
if (x == y && y == z) { |
|
2049 |
QMatrix4x4 m5; |
|
2050 |
m5.scale(x); |
|
2051 |
QVERIFY(isSame(m5, (const qreal *)resultValues)); |
|
2052 |
} |
|
2053 |
||
2054 |
if (z == 1.0f) { |
|
2055 |
QMatrix4x4 m4b(m3); |
|
2056 |
m4b.scale(x, y); |
|
2057 |
QVERIFY(m4b == m4); |
|
2058 |
} |
|
2059 |
||
2060 |
// Test coverage when the special matrix type is unknown. |
|
2061 |
||
2062 |
QMatrix4x4 m6; |
|
2063 |
m6(0, 0) = 1.0f; |
|
2064 |
m6.scale(QVector3D(x, y, z)); |
|
2065 |
QVERIFY(isSame(m6, (const qreal *)resultValues)); |
|
2066 |
||
2067 |
QMatrix4x4 m7; |
|
2068 |
m7(0, 0) = 1.0f; |
|
2069 |
m7.scale(x, y, z); |
|
2070 |
QVERIFY(isSame(m7, (const qreal *)resultValues)); |
|
2071 |
||
2072 |
if (x == y && y == z) { |
|
2073 |
QMatrix4x4 m8; |
|
2074 |
m8(0, 0) = 1.0f; |
|
2075 |
m8.scale(x); |
|
2076 |
QVERIFY(isSame(m8, (const qreal *)resultValues)); |
|
2077 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2078 |
m8.optimize(); |
0 | 2079 |
m8.scale(1.0f); |
2080 |
QVERIFY(isSame(m8, (const qreal *)resultValues)); |
|
2081 |
||
2082 |
QMatrix4x4 m9; |
|
2083 |
m9.translate(0.0f, 0.0f, 0.0f); |
|
2084 |
m9.scale(x); |
|
2085 |
QVERIFY(isSame(m9, (const qreal *)resultValues)); |
|
2086 |
} |
|
2087 |
} |
|
2088 |
||
2089 |
// Test the generation and use of 4x4 translation matrices. |
|
2090 |
void tst_QMatrixNxN::translate4x4_data() |
|
2091 |
{ |
|
2092 |
QTest::addColumn<qreal>("x"); |
|
2093 |
QTest::addColumn<qreal>("y"); |
|
2094 |
QTest::addColumn<qreal>("z"); |
|
2095 |
QTest::addColumn<void *>("resultValues"); |
|
2096 |
||
2097 |
QTest::newRow("null") |
|
2098 |
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (void *)identityValues4; |
|
2099 |
||
2100 |
static const qreal identityTranslate[] = |
|
2101 |
{1.0f, 0.0f, 0.0f, 1.0f, |
|
2102 |
0.0f, 1.0f, 0.0f, 1.0f, |
|
2103 |
0.0f, 0.0f, 1.0f, 1.0f, |
|
2104 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2105 |
QTest::newRow("identity") |
|
2106 |
<< (qreal)1.0f << (qreal)1.0f << (qreal)1.0f << (void *)identityTranslate; |
|
2107 |
||
2108 |
static const qreal complexTranslate[] = |
|
2109 |
{1.0f, 0.0f, 0.0f, 2.0f, |
|
2110 |
0.0f, 1.0f, 0.0f, 11.0f, |
|
2111 |
0.0f, 0.0f, 1.0f, -6.5f, |
|
2112 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2113 |
QTest::newRow("complex") |
|
2114 |
<< (qreal)2.0f << (qreal)11.0f << (qreal)-6.5f << (void *)complexTranslate; |
|
2115 |
||
2116 |
static const qreal complexTranslate2D[] = |
|
2117 |
{1.0f, 0.0f, 0.0f, 2.0f, |
|
2118 |
0.0f, 1.0f, 0.0f, -11.0f, |
|
2119 |
0.0f, 0.0f, 1.0f, 0.0f, |
|
2120 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2121 |
QTest::newRow("complex2D") |
|
2122 |
<< (qreal)2.0f << (qreal)-11.0f << (qreal)0.0f << (void *)complexTranslate2D; |
|
2123 |
} |
|
2124 |
void tst_QMatrixNxN::translate4x4() |
|
2125 |
{ |
|
2126 |
QFETCH(qreal, x); |
|
2127 |
QFETCH(qreal, y); |
|
2128 |
QFETCH(qreal, z); |
|
2129 |
QFETCH(void *, resultValues); |
|
2130 |
||
2131 |
QMatrix4x4 result((const qreal *)resultValues); |
|
2132 |
||
2133 |
QMatrix4x4 m1; |
|
2134 |
m1.translate(QVector3D(x, y, z)); |
|
2135 |
QVERIFY(isSame(m1, (const qreal *)resultValues)); |
|
2136 |
||
2137 |
QMatrix4x4 m2; |
|
2138 |
m2.translate(x, y, z); |
|
2139 |
QVERIFY(isSame(m2, (const qreal *)resultValues)); |
|
2140 |
||
2141 |
if (z == 0.0f) { |
|
2142 |
QMatrix4x4 m2b; |
|
2143 |
m2b.translate(x, y); |
|
2144 |
QVERIFY(m2b == m2); |
|
2145 |
} |
|
2146 |
||
2147 |
QVector3D v1(2.0f, 3.0f, -4.0f); |
|
2148 |
QVector3D v2 = m1 * v1; |
|
2149 |
QCOMPARE(v2.x(), (qreal)(2.0f + x)); |
|
2150 |
QCOMPARE(v2.y(), (qreal)(3.0f + y)); |
|
2151 |
QCOMPARE(v2.z(), (qreal)(-4.0f + z)); |
|
2152 |
||
2153 |
QVector4D v3(2.0f, 3.0f, -4.0f, 1.0f); |
|
2154 |
QVector4D v4 = m1 * v3; |
|
2155 |
QCOMPARE(v4.x(), (qreal)(2.0f + x)); |
|
2156 |
QCOMPARE(v4.y(), (qreal)(3.0f + y)); |
|
2157 |
QCOMPARE(v4.z(), (qreal)(-4.0f + z)); |
|
2158 |
QCOMPARE(v4.w(), (qreal)1.0f); |
|
2159 |
||
2160 |
QVector4D v5(2.0f, 3.0f, -4.0f, 34.0f); |
|
2161 |
QVector4D v6 = m1 * v5; |
|
2162 |
QCOMPARE(v6.x(), (qreal)(2.0f + x * 34.0f)); |
|
2163 |
QCOMPARE(v6.y(), (qreal)(3.0f + y * 34.0f)); |
|
2164 |
QCOMPARE(v6.z(), (qreal)(-4.0f + z * 34.0f)); |
|
2165 |
QCOMPARE(v6.w(), (qreal)34.0f); |
|
2166 |
||
2167 |
QPoint p1(2, 3); |
|
2168 |
QPoint p2 = m1 * p1; |
|
2169 |
QCOMPARE(p2.x(), (int)(2.0f + x)); |
|
2170 |
QCOMPARE(p2.y(), (int)(3.0f + y)); |
|
2171 |
||
2172 |
QPointF p3(2.0f, 3.0f); |
|
2173 |
QPointF p4 = m1 * p3; |
|
2174 |
QCOMPARE(p4.x(), (qreal)(2.0f + x)); |
|
2175 |
QCOMPARE(p4.y(), (qreal)(3.0f + y)); |
|
2176 |
||
2177 |
QMatrix4x4 m3(uniqueValues4); |
|
2178 |
QMatrix4x4 m4(m3); |
|
2179 |
m4.translate(x, y, z); |
|
2180 |
QVERIFY(m4 == m3 * m1); |
|
2181 |
||
2182 |
if (z == 0.0f) { |
|
2183 |
QMatrix4x4 m4b(m3); |
|
2184 |
m4b.translate(x, y); |
|
2185 |
QVERIFY(m4b == m4); |
|
2186 |
} |
|
2187 |
} |
|
2188 |
||
2189 |
// Test the generation and use of 4x4 rotation matrices. |
|
2190 |
void tst_QMatrixNxN::rotate4x4_data() |
|
2191 |
{ |
|
2192 |
QTest::addColumn<qreal>("angle"); |
|
2193 |
QTest::addColumn<qreal>("x"); |
|
2194 |
QTest::addColumn<qreal>("y"); |
|
2195 |
QTest::addColumn<qreal>("z"); |
|
2196 |
QTest::addColumn<void *>("resultValues"); |
|
2197 |
||
2198 |
static const qreal nullRotate[] = |
|
2199 |
{0.0f, 0.0f, 0.0f, 0.0f, |
|
2200 |
0.0f, 0.0f, 0.0f, 0.0f, |
|
2201 |
0.0f, 0.0f, 0.0f, 0.0f, |
|
2202 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2203 |
QTest::newRow("null") |
|
2204 |
<< (qreal)90.0f |
|
2205 |
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
2206 |
<< (void *)nullRotate; |
|
2207 |
||
2208 |
static const qreal noRotate[] = |
|
2209 |
{1.0f, 0.0f, 0.0f, 0.0f, |
|
2210 |
0.0f, 1.0f, 0.0f, 0.0f, |
|
2211 |
0.0f, 0.0f, 1.0f, 0.0f, |
|
2212 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2213 |
QTest::newRow("zerodegrees") |
|
2214 |
<< (qreal)0.0f |
|
2215 |
<< (qreal)2.0f << (qreal)3.0f << (qreal)-4.0f |
|
2216 |
<< (void *)noRotate; |
|
2217 |
||
2218 |
static const qreal xRotate[] = |
|
2219 |
{1.0f, 0.0f, 0.0f, 0.0f, |
|
2220 |
0.0f, 0.0f, -1.0f, 0.0f, |
|
2221 |
0.0f, 1.0f, 0.0f, 0.0f, |
|
2222 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2223 |
QTest::newRow("xrotate") |
|
2224 |
<< (qreal)90.0f |
|
2225 |
<< (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
2226 |
<< (void *)xRotate; |
|
2227 |
||
2228 |
static const qreal xRotateNeg[] = |
|
2229 |
{1.0f, 0.0f, 0.0f, 0.0f, |
|
2230 |
0.0f, 0.0f, 1.0f, 0.0f, |
|
2231 |
0.0f, -1.0f, 0.0f, 0.0f, |
|
2232 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2233 |
QTest::newRow("-xrotate") |
|
2234 |
<< (qreal)90.0f |
|
2235 |
<< (qreal)-1.0f << (qreal)0.0f << (qreal)0.0f |
|
2236 |
<< (void *)xRotateNeg; |
|
2237 |
||
2238 |
static const qreal yRotate[] = |
|
2239 |
{0.0f, 0.0f, 1.0f, 0.0f, |
|
2240 |
0.0f, 1.0f, 0.0f, 0.0f, |
|
2241 |
-1.0f, 0.0f, 0.0f, 0.0f, |
|
2242 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2243 |
QTest::newRow("yrotate") |
|
2244 |
<< (qreal)90.0f |
|
2245 |
<< (qreal)0.0f << (qreal)1.0f << (qreal)0.0f |
|
2246 |
<< (void *)yRotate; |
|
2247 |
||
2248 |
static const qreal yRotateNeg[] = |
|
2249 |
{0.0f, 0.0f, -1.0f, 0.0f, |
|
2250 |
0.0f, 1.0f, 0.0f, 0.0f, |
|
2251 |
1.0f, 0.0f, 0.0f, 0.0f, |
|
2252 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2253 |
QTest::newRow("-yrotate") |
|
2254 |
<< (qreal)90.0f |
|
2255 |
<< (qreal)0.0f << (qreal)-1.0f << (qreal)0.0f |
|
2256 |
<< (void *)yRotateNeg; |
|
2257 |
||
2258 |
static const qreal zRotate[] = |
|
2259 |
{0.0f, -1.0f, 0.0f, 0.0f, |
|
2260 |
1.0f, 0.0f, 0.0f, 0.0f, |
|
2261 |
0.0f, 0.0f, 1.0f, 0.0f, |
|
2262 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2263 |
QTest::newRow("zrotate") |
|
2264 |
<< (qreal)90.0f |
|
2265 |
<< (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
2266 |
<< (void *)zRotate; |
|
2267 |
||
2268 |
static const qreal zRotateNeg[] = |
|
2269 |
{0.0f, 1.0f, 0.0f, 0.0f, |
|
2270 |
-1.0f, 0.0f, 0.0f, 0.0f, |
|
2271 |
0.0f, 0.0f, 1.0f, 0.0f, |
|
2272 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2273 |
QTest::newRow("-zrotate") |
|
2274 |
<< (qreal)90.0f |
|
2275 |
<< (qreal)0.0f << (qreal)0.0f << (qreal)-1.0f |
|
2276 |
<< (void *)zRotateNeg; |
|
2277 |
||
2278 |
// Algorithm from http://en.wikipedia.org/wiki/Rotation_matrix. |
|
2279 |
// Deliberately different from the one in the code for cross-checking. |
|
2280 |
static qreal complexRotate[16]; |
|
2281 |
qreal x = 1.0f; |
|
2282 |
qreal y = 2.0f; |
|
2283 |
qreal z = -6.0f; |
|
2284 |
qreal angle = -45.0f; |
|
2285 |
qreal c = qCos(angle * M_PI / 180.0f); |
|
2286 |
qreal s = qSin(angle * M_PI / 180.0f); |
|
2287 |
qreal len = qSqrt(x * x + y * y + z * z); |
|
2288 |
qreal xu = x / len; |
|
2289 |
qreal yu = y / len; |
|
2290 |
qreal zu = z / len; |
|
2291 |
complexRotate[0] = (qreal)((1 - xu * xu) * c + xu * xu); |
|
2292 |
complexRotate[1] = (qreal)(-zu * s - xu * yu * c + xu * yu); |
|
2293 |
complexRotate[2] = (qreal)(yu * s - xu * zu * c + xu * zu); |
|
2294 |
complexRotate[3] = 0; |
|
2295 |
complexRotate[4] = (qreal)(zu * s - xu * yu * c + xu * yu); |
|
2296 |
complexRotate[5] = (qreal)((1 - yu * yu) * c + yu * yu); |
|
2297 |
complexRotate[6] = (qreal)(-xu * s - yu * zu * c + yu * zu); |
|
2298 |
complexRotate[7] = 0; |
|
2299 |
complexRotate[8] = (qreal)(-yu * s - xu * zu * c + xu * zu); |
|
2300 |
complexRotate[9] = (qreal)(xu * s - yu * zu * c + yu * zu); |
|
2301 |
complexRotate[10] = (qreal)((1 - zu * zu) * c + zu * zu); |
|
2302 |
complexRotate[11] = 0; |
|
2303 |
complexRotate[12] = 0; |
|
2304 |
complexRotate[13] = 0; |
|
2305 |
complexRotate[14] = 0; |
|
2306 |
complexRotate[15] = 1; |
|
2307 |
||
2308 |
QTest::newRow("complex") |
|
2309 |
<< (qreal)angle |
|
2310 |
<< (qreal)x << (qreal)y << (qreal)z |
|
2311 |
<< (void *)complexRotate; |
|
2312 |
} |
|
2313 |
void tst_QMatrixNxN::rotate4x4() |
|
2314 |
{ |
|
2315 |
QFETCH(qreal, angle); |
|
2316 |
QFETCH(qreal, x); |
|
2317 |
QFETCH(qreal, y); |
|
2318 |
QFETCH(qreal, z); |
|
2319 |
QFETCH(void *, resultValues); |
|
2320 |
||
2321 |
QMatrix4x4 m1; |
|
2322 |
m1.rotate(angle, QVector3D(x, y, z)); |
|
2323 |
QVERIFY(isSame(m1, (const qreal *)resultValues)); |
|
2324 |
||
2325 |
QMatrix4x4 m2; |
|
2326 |
m2.rotate(angle, x, y, z); |
|
2327 |
QVERIFY(isSame(m2, (const qreal *)resultValues)); |
|
2328 |
||
2329 |
QMatrix4x4 m3(uniqueValues4); |
|
2330 |
QMatrix4x4 m4(m3); |
|
2331 |
m4.rotate(angle, x, y, z); |
|
2332 |
QVERIFY(qFuzzyCompare(m4, m3 * m1)); |
|
2333 |
||
2334 |
// Null vectors don't make sense for quaternion rotations. |
|
2335 |
if (x != 0 || y != 0 || z != 0) { |
|
2336 |
QMatrix4x4 m5; |
|
2337 |
m5.rotate(QQuaternion::fromAxisAndAngle(QVector3D(x, y, z), angle)); |
|
2338 |
QVERIFY(isSame(m5, (const qreal *)resultValues)); |
|
2339 |
} |
|
2340 |
||
2341 |
#define ROTATE4(xin,yin,zin,win,xout,yout,zout,wout) \ |
|
2342 |
do { \ |
|
2343 |
xout = ((const qreal *)resultValues)[0] * xin + \ |
|
2344 |
((const qreal *)resultValues)[1] * yin + \ |
|
2345 |
((const qreal *)resultValues)[2] * zin + \ |
|
2346 |
((const qreal *)resultValues)[3] * win; \ |
|
2347 |
yout = ((const qreal *)resultValues)[4] * xin + \ |
|
2348 |
((const qreal *)resultValues)[5] * yin + \ |
|
2349 |
((const qreal *)resultValues)[6] * zin + \ |
|
2350 |
((const qreal *)resultValues)[7] * win; \ |
|
2351 |
zout = ((const qreal *)resultValues)[8] * xin + \ |
|
2352 |
((const qreal *)resultValues)[9] * yin + \ |
|
2353 |
((const qreal *)resultValues)[10] * zin + \ |
|
2354 |
((const qreal *)resultValues)[11] * win; \ |
|
2355 |
wout = ((const qreal *)resultValues)[12] * xin + \ |
|
2356 |
((const qreal *)resultValues)[13] * yin + \ |
|
2357 |
((const qreal *)resultValues)[14] * zin + \ |
|
2358 |
((const qreal *)resultValues)[15] * win; \ |
|
2359 |
} while (0) |
|
2360 |
||
2361 |
// Rotate various test vectors using the straight-forward approach. |
|
2362 |
qreal v1x, v1y, v1z, v1w; |
|
2363 |
ROTATE4(2.0f, 3.0f, -4.0f, 1.0f, v1x, v1y, v1z, v1w); |
|
2364 |
v1x /= v1w; |
|
2365 |
v1y /= v1w; |
|
2366 |
v1z /= v1w; |
|
2367 |
qreal v3x, v3y, v3z, v3w; |
|
2368 |
ROTATE4(2.0f, 3.0f, -4.0f, 1.0f, v3x, v3y, v3z, v3w); |
|
2369 |
qreal v5x, v5y, v5z, v5w; |
|
2370 |
ROTATE4(2.0f, 3.0f, -4.0f, 34.0f, v5x, v5y, v5z, v5w); |
|
2371 |
qreal p1x, p1y, p1z, p1w; |
|
2372 |
ROTATE4(2.0f, 3.0f, 0.0f, 1.0f, p1x, p1y, p1z, p1w); |
|
2373 |
p1x /= p1w; |
|
2374 |
p1y /= p1w; |
|
2375 |
p1z /= p1w; |
|
2376 |
||
2377 |
QVector3D v1(2.0f, 3.0f, -4.0f); |
|
2378 |
QVector3D v2 = m1 * v1; |
|
2379 |
QVERIFY(fuzzyCompare(v2.x(), v1x)); |
|
2380 |
QVERIFY(fuzzyCompare(v2.y(), v1y)); |
|
2381 |
QVERIFY(fuzzyCompare(v2.z(), v1z)); |
|
2382 |
||
2383 |
QVector4D v3(2.0f, 3.0f, -4.0f, 1.0f); |
|
2384 |
QVector4D v4 = m1 * v3; |
|
2385 |
QVERIFY(fuzzyCompare(v4.x(), v3x)); |
|
2386 |
QVERIFY(fuzzyCompare(v4.y(), v3y)); |
|
2387 |
QVERIFY(fuzzyCompare(v4.z(), v3z)); |
|
2388 |
QVERIFY(fuzzyCompare(v4.w(), v3w)); |
|
2389 |
||
2390 |
QVector4D v5(2.0f, 3.0f, -4.0f, 34.0f); |
|
2391 |
QVector4D v6 = m1 * v5; |
|
2392 |
QVERIFY(fuzzyCompare(v6.x(), v5x)); |
|
2393 |
QVERIFY(fuzzyCompare(v6.y(), v5y)); |
|
2394 |
QVERIFY(fuzzyCompare(v6.z(), v5z)); |
|
2395 |
QVERIFY(fuzzyCompare(v6.w(), v5w)); |
|
2396 |
||
2397 |
QPoint p1(2, 3); |
|
2398 |
QPoint p2 = m1 * p1; |
|
2399 |
QCOMPARE(p2.x(), qRound(p1x)); |
|
2400 |
QCOMPARE(p2.y(), qRound(p1y)); |
|
2401 |
||
2402 |
QPointF p3(2.0f, 3.0f); |
|
2403 |
QPointF p4 = m1 * p3; |
|
2404 |
QVERIFY(fuzzyCompare(p4.x(), p1x)); |
|
2405 |
QVERIFY(fuzzyCompare(p4.y(), p1y)); |
|
2406 |
||
2407 |
if (x != 0 || y != 0 || z != 0) { |
|
2408 |
QQuaternion q = QQuaternion::fromAxisAndAngle(QVector3D(x, y, z), angle); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2409 |
QVector3D vq = q.rotatedVector(v1); |
0 | 2410 |
QVERIFY(fuzzyCompare(vq.x(), v1x)); |
2411 |
QVERIFY(fuzzyCompare(vq.y(), v1y)); |
|
2412 |
QVERIFY(fuzzyCompare(vq.z(), v1z)); |
|
2413 |
} |
|
2414 |
} |
|
2415 |
||
2416 |
static bool isSame(const QMatrix3x3& m1, const Matrix3& m2) |
|
2417 |
{ |
|
2418 |
for (int row = 0; row < 3; ++row) { |
|
2419 |
for (int col = 0; col < 3; ++col) { |
|
2420 |
if (!fuzzyCompare(m1(row, col), m2.v[row * 3 + col])) |
|
2421 |
return false; |
|
2422 |
} |
|
2423 |
} |
|
2424 |
return true; |
|
2425 |
} |
|
2426 |
||
2427 |
// Test the computation of normal matrices from 4x4 transformation matrices. |
|
2428 |
void tst_QMatrixNxN::normalMatrix_data() |
|
2429 |
{ |
|
2430 |
QTest::addColumn<void *>("mValues"); |
|
2431 |
||
2432 |
QTest::newRow("identity") |
|
2433 |
<< (void *)identityValues4; |
|
2434 |
QTest::newRow("unique") |
|
2435 |
<< (void *)uniqueValues4; // Not invertible because determinant == 0. |
|
2436 |
||
2437 |
static qreal const translateValues[16] = |
|
2438 |
{1.0f, 0.0f, 0.0f, 4.0f, |
|
2439 |
0.0f, 1.0f, 0.0f, 5.0f, |
|
2440 |
0.0f, 0.0f, 1.0f, -3.0f, |
|
2441 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2442 |
static qreal const scaleValues[16] = |
|
2443 |
{2.0f, 0.0f, 0.0f, 0.0f, |
|
2444 |
0.0f, 7.0f, 0.0f, 0.0f, |
|
2445 |
0.0f, 0.0f, 9.0f, 0.0f, |
|
2446 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2447 |
static qreal const bothValues[16] = |
|
2448 |
{2.0f, 0.0f, 0.0f, 4.0f, |
|
2449 |
0.0f, 7.0f, 0.0f, 5.0f, |
|
2450 |
0.0f, 0.0f, 9.0f, -3.0f, |
|
2451 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2452 |
static qreal const nullScaleValues1[16] = |
|
2453 |
{0.0f, 0.0f, 0.0f, 4.0f, |
|
2454 |
0.0f, 7.0f, 0.0f, 5.0f, |
|
2455 |
0.0f, 0.0f, 9.0f, -3.0f, |
|
2456 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2457 |
static qreal const nullScaleValues2[16] = |
|
2458 |
{2.0f, 0.0f, 0.0f, 4.0f, |
|
2459 |
0.0f, 0.0f, 0.0f, 5.0f, |
|
2460 |
0.0f, 0.0f, 9.0f, -3.0f, |
|
2461 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2462 |
static qreal const nullScaleValues3[16] = |
|
2463 |
{2.0f, 0.0f, 0.0f, 4.0f, |
|
2464 |
0.0f, 7.0f, 0.0f, 5.0f, |
|
2465 |
0.0f, 0.0f, 0.0f, -3.0f, |
|
2466 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2467 |
||
2468 |
QTest::newRow("translate") << (void *)translateValues; |
|
2469 |
QTest::newRow("scale") << (void *)scaleValues; |
|
2470 |
QTest::newRow("both") << (void *)bothValues; |
|
2471 |
QTest::newRow("null scale 1") << (void *)nullScaleValues1; |
|
2472 |
QTest::newRow("null scale 2") << (void *)nullScaleValues2; |
|
2473 |
QTest::newRow("null scale 3") << (void *)nullScaleValues3; |
|
2474 |
} |
|
2475 |
void tst_QMatrixNxN::normalMatrix() |
|
2476 |
{ |
|
2477 |
QFETCH(void *, mValues); |
|
2478 |
const qreal *values = (const qreal *)mValues; |
|
2479 |
||
2480 |
// Compute the expected answer the long way. |
|
2481 |
Matrix3 min; |
|
2482 |
Matrix3 answer; |
|
2483 |
min.v[0] = values[0]; |
|
2484 |
min.v[1] = values[1]; |
|
2485 |
min.v[2] = values[2]; |
|
2486 |
min.v[3] = values[4]; |
|
2487 |
min.v[4] = values[5]; |
|
2488 |
min.v[5] = values[6]; |
|
2489 |
min.v[6] = values[8]; |
|
2490 |
min.v[7] = values[9]; |
|
2491 |
min.v[8] = values[10]; |
|
2492 |
bool invertible = m3Inverse(min, answer); |
|
2493 |
m3Transpose(answer); |
|
2494 |
||
2495 |
// Perform the test. |
|
2496 |
QMatrix4x4 m1(values); |
|
2497 |
QMatrix3x3 n1 = m1.normalMatrix(); |
|
2498 |
||
2499 |
if (invertible) |
|
2500 |
QVERIFY(::isSame(n1, answer)); |
|
2501 |
else |
|
2502 |
QVERIFY(isIdentity(n1)); |
|
2503 |
||
2504 |
// Perform the test again, after inferring special matrix types. |
|
2505 |
// This tests the optimized paths in the normalMatrix() function. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2506 |
m1.optimize(); |
0 | 2507 |
n1 = m1.normalMatrix(); |
2508 |
||
2509 |
if (invertible) |
|
2510 |
QVERIFY(::isSame(n1, answer)); |
|
2511 |
else |
|
2512 |
QVERIFY(isIdentity(n1)); |
|
2513 |
} |
|
2514 |
||
2515 |
// Test optimized transformations on 4x4 matrices. |
|
2516 |
void tst_QMatrixNxN::optimizedTransforms() |
|
2517 |
{ |
|
2518 |
static qreal const translateValues[16] = |
|
2519 |
{1.0f, 0.0f, 0.0f, 4.0f, |
|
2520 |
0.0f, 1.0f, 0.0f, 5.0f, |
|
2521 |
0.0f, 0.0f, 1.0f, -3.0f, |
|
2522 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2523 |
static qreal const translateDoubleValues[16] = |
|
2524 |
{1.0f, 0.0f, 0.0f, 8.0f, |
|
2525 |
0.0f, 1.0f, 0.0f, 10.0f, |
|
2526 |
0.0f, 0.0f, 1.0f, -6.0f, |
|
2527 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2528 |
static qreal const scaleValues[16] = |
|
2529 |
{2.0f, 0.0f, 0.0f, 0.0f, |
|
2530 |
0.0f, 7.0f, 0.0f, 0.0f, |
|
2531 |
0.0f, 0.0f, 9.0f, 0.0f, |
|
2532 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2533 |
static qreal const scaleDoubleValues[16] = |
|
2534 |
{4.0f, 0.0f, 0.0f, 0.0f, |
|
2535 |
0.0f, 49.0f, 0.0f, 0.0f, |
|
2536 |
0.0f, 0.0f, 81.0f, 0.0f, |
|
2537 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2538 |
static qreal const bothValues[16] = |
|
2539 |
{2.0f, 0.0f, 0.0f, 4.0f, |
|
2540 |
0.0f, 7.0f, 0.0f, 5.0f, |
|
2541 |
0.0f, 0.0f, 9.0f, -3.0f, |
|
2542 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2543 |
static qreal const bothReverseValues[16] = |
|
2544 |
{2.0f, 0.0f, 0.0f, 4.0f * 2.0f, |
|
2545 |
0.0f, 7.0f, 0.0f, 5.0f * 7.0f, |
|
2546 |
0.0f, 0.0f, 9.0f, -3.0f * 9.0f, |
|
2547 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2548 |
static qreal const bothThenTranslateValues[16] = |
|
2549 |
{2.0f, 0.0f, 0.0f, 4.0f + 2.0f * 4.0f, |
|
2550 |
0.0f, 7.0f, 0.0f, 5.0f + 7.0f * 5.0f, |
|
2551 |
0.0f, 0.0f, 9.0f, -3.0f + 9.0f * -3.0f, |
|
2552 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2553 |
static qreal const bothThenScaleValues[16] = |
|
2554 |
{4.0f, 0.0f, 0.0f, 4.0f, |
|
2555 |
0.0f, 49.0f, 0.0f, 5.0f, |
|
2556 |
0.0f, 0.0f, 81.0f, -3.0f, |
|
2557 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
2558 |
||
2559 |
QMatrix4x4 translate(translateValues); |
|
2560 |
QMatrix4x4 scale(scaleValues); |
|
2561 |
QMatrix4x4 both(bothValues); |
|
2562 |
||
2563 |
QMatrix4x4 m1; |
|
2564 |
m1.translate(4.0f, 5.0f, -3.0f); |
|
2565 |
QVERIFY(isSame(m1, translateValues)); |
|
2566 |
m1.translate(4.0f, 5.0f, -3.0f); |
|
2567 |
QVERIFY(isSame(m1, translateDoubleValues)); |
|
2568 |
||
2569 |
QMatrix4x4 m2; |
|
2570 |
m2.translate(QVector3D(4.0f, 5.0f, -3.0f)); |
|
2571 |
QVERIFY(isSame(m2, translateValues)); |
|
2572 |
m2.translate(QVector3D(4.0f, 5.0f, -3.0f)); |
|
2573 |
QVERIFY(isSame(m2, translateDoubleValues)); |
|
2574 |
||
2575 |
QMatrix4x4 m3; |
|
2576 |
m3.scale(2.0f, 7.0f, 9.0f); |
|
2577 |
QVERIFY(isSame(m3, scaleValues)); |
|
2578 |
m3.scale(2.0f, 7.0f, 9.0f); |
|
2579 |
QVERIFY(isSame(m3, scaleDoubleValues)); |
|
2580 |
||
2581 |
QMatrix4x4 m4; |
|
2582 |
m4.scale(QVector3D(2.0f, 7.0f, 9.0f)); |
|
2583 |
QVERIFY(isSame(m4, scaleValues)); |
|
2584 |
m4.scale(QVector3D(2.0f, 7.0f, 9.0f)); |
|
2585 |
QVERIFY(isSame(m4, scaleDoubleValues)); |
|
2586 |
||
2587 |
QMatrix4x4 m5; |
|
2588 |
m5.translate(4.0f, 5.0f, -3.0f); |
|
2589 |
m5.scale(2.0f, 7.0f, 9.0f); |
|
2590 |
QVERIFY(isSame(m5, bothValues)); |
|
2591 |
m5.translate(4.0f, 5.0f, -3.0f); |
|
2592 |
QVERIFY(isSame(m5, bothThenTranslateValues)); |
|
2593 |
||
2594 |
QMatrix4x4 m6; |
|
2595 |
m6.translate(QVector3D(4.0f, 5.0f, -3.0f)); |
|
2596 |
m6.scale(QVector3D(2.0f, 7.0f, 9.0f)); |
|
2597 |
QVERIFY(isSame(m6, bothValues)); |
|
2598 |
m6.translate(QVector3D(4.0f, 5.0f, -3.0f)); |
|
2599 |
QVERIFY(isSame(m6, bothThenTranslateValues)); |
|
2600 |
||
2601 |
QMatrix4x4 m7; |
|
2602 |
m7.scale(2.0f, 7.0f, 9.0f); |
|
2603 |
m7.translate(4.0f, 5.0f, -3.0f); |
|
2604 |
QVERIFY(isSame(m7, bothReverseValues)); |
|
2605 |
||
2606 |
QMatrix4x4 m8; |
|
2607 |
m8.scale(QVector3D(2.0f, 7.0f, 9.0f)); |
|
2608 |
m8.translate(QVector3D(4.0f, 5.0f, -3.0f)); |
|
2609 |
QVERIFY(isSame(m8, bothReverseValues)); |
|
2610 |
||
2611 |
QMatrix4x4 m9; |
|
2612 |
m9.translate(4.0f, 5.0f, -3.0f); |
|
2613 |
m9.scale(2.0f, 7.0f, 9.0f); |
|
2614 |
QVERIFY(isSame(m9, bothValues)); |
|
2615 |
m9.scale(2.0f, 7.0f, 9.0f); |
|
2616 |
QVERIFY(isSame(m9, bothThenScaleValues)); |
|
2617 |
||
2618 |
QMatrix4x4 m10; |
|
2619 |
m10.translate(QVector3D(4.0f, 5.0f, -3.0f)); |
|
2620 |
m10.scale(QVector3D(2.0f, 7.0f, 9.0f)); |
|
2621 |
QVERIFY(isSame(m10, bothValues)); |
|
2622 |
m10.scale(QVector3D(2.0f, 7.0f, 9.0f)); |
|
2623 |
QVERIFY(isSame(m10, bothThenScaleValues)); |
|
2624 |
} |
|
2625 |
||
2626 |
// Test orthographic projections. |
|
2627 |
void tst_QMatrixNxN::ortho() |
|
2628 |
{ |
|
2629 |
QMatrix4x4 m1; |
|
2630 |
m1.ortho(QRect(0, 0, 300, 150)); |
|
2631 |
QPointF p1 = m1 * QPointF(0, 0); |
|
2632 |
QPointF p2 = m1 * QPointF(300, 0); |
|
2633 |
QPointF p3 = m1 * QPointF(0, 150); |
|
2634 |
QPointF p4 = m1 * QPointF(300, 150); |
|
2635 |
QVector3D p5 = m1 * QVector3D(300, 150, 1); |
|
2636 |
QVERIFY(fuzzyCompare(p1.x(), -1.0)); |
|
2637 |
QVERIFY(fuzzyCompare(p1.y(), 1.0)); |
|
2638 |
QVERIFY(fuzzyCompare(p2.x(), 1.0)); |
|
2639 |
QVERIFY(fuzzyCompare(p2.y(), 1.0)); |
|
2640 |
QVERIFY(fuzzyCompare(p3.x(), -1.0)); |
|
2641 |
QVERIFY(fuzzyCompare(p3.y(), -1.0)); |
|
2642 |
QVERIFY(fuzzyCompare(p4.x(), 1.0)); |
|
2643 |
QVERIFY(fuzzyCompare(p4.y(), -1.0)); |
|
2644 |
QVERIFY(fuzzyCompare(p5.x(), (qreal)1.0)); |
|
2645 |
QVERIFY(fuzzyCompare(p5.y(), (qreal)-1.0)); |
|
2646 |
QVERIFY(fuzzyCompare(p5.z(), (qreal)-1.0)); |
|
2647 |
||
2648 |
QMatrix4x4 m2; |
|
2649 |
m2.ortho(QRectF(0, 0, 300, 150)); |
|
2650 |
p1 = m2 * QPointF(0, 0); |
|
2651 |
p2 = m2 * QPointF(300, 0); |
|
2652 |
p3 = m2 * QPointF(0, 150); |
|
2653 |
p4 = m2 * QPointF(300, 150); |
|
2654 |
p5 = m2 * QVector3D(300, 150, 1); |
|
2655 |
QVERIFY(fuzzyCompare(p1.x(), -1.0)); |
|
2656 |
QVERIFY(fuzzyCompare(p1.y(), 1.0)); |
|
2657 |
QVERIFY(fuzzyCompare(p2.x(), 1.0)); |
|
2658 |
QVERIFY(fuzzyCompare(p2.y(), 1.0)); |
|
2659 |
QVERIFY(fuzzyCompare(p3.x(), -1.0)); |
|
2660 |
QVERIFY(fuzzyCompare(p3.y(), -1.0)); |
|
2661 |
QVERIFY(fuzzyCompare(p4.x(), 1.0)); |
|
2662 |
QVERIFY(fuzzyCompare(p4.y(), -1.0)); |
|
2663 |
QVERIFY(fuzzyCompare(p5.x(), (qreal)1.0)); |
|
2664 |
QVERIFY(fuzzyCompare(p5.y(), (qreal)-1.0)); |
|
2665 |
QVERIFY(fuzzyCompare(p5.z(), (qreal)-1.0)); |
|
2666 |
||
2667 |
QMatrix4x4 m3; |
|
2668 |
m3.ortho(0, 300, 150, 0, -1, 1); |
|
2669 |
p1 = m3 * QPointF(0, 0); |
|
2670 |
p2 = m3 * QPointF(300, 0); |
|
2671 |
p3 = m3 * QPointF(0, 150); |
|
2672 |
p4 = m3 * QPointF(300, 150); |
|
2673 |
p5 = m3 * QVector3D(300, 150, 1); |
|
2674 |
QVERIFY(fuzzyCompare(p1.x(), -1.0)); |
|
2675 |
QVERIFY(fuzzyCompare(p1.y(), 1.0)); |
|
2676 |
QVERIFY(fuzzyCompare(p2.x(), 1.0)); |
|
2677 |
QVERIFY(fuzzyCompare(p2.y(), 1.0)); |
|
2678 |
QVERIFY(fuzzyCompare(p3.x(), -1.0)); |
|
2679 |
QVERIFY(fuzzyCompare(p3.y(), -1.0)); |
|
2680 |
QVERIFY(fuzzyCompare(p4.x(), 1.0)); |
|
2681 |
QVERIFY(fuzzyCompare(p4.y(), -1.0)); |
|
2682 |
QVERIFY(fuzzyCompare(p5.x(), (qreal)1.0)); |
|
2683 |
QVERIFY(fuzzyCompare(p5.y(), (qreal)-1.0)); |
|
2684 |
QVERIFY(fuzzyCompare(p5.z(), (qreal)-1.0)); |
|
2685 |
||
2686 |
QMatrix4x4 m4; |
|
2687 |
m4.ortho(0, 300, 150, 0, -2, 3); |
|
2688 |
p1 = m4 * QPointF(0, 0); |
|
2689 |
p2 = m4 * QPointF(300, 0); |
|
2690 |
p3 = m4 * QPointF(0, 150); |
|
2691 |
p4 = m4 * QPointF(300, 150); |
|
2692 |
p5 = m4 * QVector3D(300, 150, 1); |
|
2693 |
QVERIFY(fuzzyCompare(p1.x(), -1.0)); |
|
2694 |
QVERIFY(fuzzyCompare(p1.y(), 1.0)); |
|
2695 |
QVERIFY(fuzzyCompare(p2.x(), 1.0)); |
|
2696 |
QVERIFY(fuzzyCompare(p2.y(), 1.0)); |
|
2697 |
QVERIFY(fuzzyCompare(p3.x(), -1.0)); |
|
2698 |
QVERIFY(fuzzyCompare(p3.y(), -1.0)); |
|
2699 |
QVERIFY(fuzzyCompare(p4.x(), 1.0)); |
|
2700 |
QVERIFY(fuzzyCompare(p4.y(), -1.0)); |
|
2701 |
QVERIFY(fuzzyCompare(p5.x(), (qreal)1.0)); |
|
2702 |
QVERIFY(fuzzyCompare(p5.y(), (qreal)-1.0)); |
|
2703 |
QVERIFY(fuzzyCompare(p5.z(), (qreal)-0.6)); |
|
2704 |
||
2705 |
// An empty view volume should leave the matrix alone. |
|
2706 |
QMatrix4x4 m5; |
|
2707 |
m5.ortho(0, 0, 150, 0, -2, 3); |
|
2708 |
QVERIFY(m5.isIdentity()); |
|
2709 |
m5.ortho(0, 300, 150, 150, -2, 3); |
|
2710 |
QVERIFY(m5.isIdentity()); |
|
2711 |
m5.ortho(0, 300, 150, 0, 2, 2); |
|
2712 |
QVERIFY(m5.isIdentity()); |
|
2713 |
} |
|
2714 |
||
2715 |
// Test perspective frustum projections. |
|
2716 |
void tst_QMatrixNxN::frustum() |
|
2717 |
{ |
|
2718 |
QMatrix4x4 m1; |
|
2719 |
m1.frustum(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f); |
|
2720 |
QVector3D p1 = m1 * QVector3D(-1.0f, -1.0f, 1.0f); |
|
2721 |
QVector3D p2 = m1 * QVector3D(1.0f, -1.0f, 1.0f); |
|
2722 |
QVector3D p3 = m1 * QVector3D(-1.0f, 1.0f, 1.0f); |
|
2723 |
QVector3D p4 = m1 * QVector3D(1.0f, 1.0f, 1.0f); |
|
2724 |
QVector3D p5 = m1 * QVector3D(0.0f, 0.0f, 2.0f); |
|
2725 |
QVERIFY(fuzzyCompare(p1.x(), -1.0f)); |
|
2726 |
QVERIFY(fuzzyCompare(p1.y(), -1.0f)); |
|
2727 |
QVERIFY(fuzzyCompare(p1.z(), -1.0f)); |
|
2728 |
QVERIFY(fuzzyCompare(p2.x(), 1.0f)); |
|
2729 |
QVERIFY(fuzzyCompare(p2.y(), -1.0f)); |
|
2730 |
QVERIFY(fuzzyCompare(p2.z(), -1.0f)); |
|
2731 |
QVERIFY(fuzzyCompare(p3.x(), -1.0f)); |
|
2732 |
QVERIFY(fuzzyCompare(p3.y(), 1.0f)); |
|
2733 |
QVERIFY(fuzzyCompare(p3.z(), -1.0f)); |
|
2734 |
QVERIFY(fuzzyCompare(p4.x(), 1.0f)); |
|
2735 |
QVERIFY(fuzzyCompare(p4.y(), 1.0f)); |
|
2736 |
QVERIFY(fuzzyCompare(p4.z(), -1.0f)); |
|
2737 |
QVERIFY(fuzzyCompare(p5.x(), 0.0f)); |
|
2738 |
QVERIFY(fuzzyCompare(p5.y(), 0.0f)); |
|
2739 |
QVERIFY(fuzzyCompare(p5.z(), -0.5f)); |
|
2740 |
||
2741 |
// An empty view volume should leave the matrix alone. |
|
2742 |
QMatrix4x4 m5; |
|
2743 |
m5.frustum(0, 0, 150, 0, -2, 3); |
|
2744 |
QVERIFY(m5.isIdentity()); |
|
2745 |
m5.frustum(0, 300, 150, 150, -2, 3); |
|
2746 |
QVERIFY(m5.isIdentity()); |
|
2747 |
m5.frustum(0, 300, 150, 0, 2, 2); |
|
2748 |
QVERIFY(m5.isIdentity()); |
|
2749 |
} |
|
2750 |
||
2751 |
// Test perspective field-of-view projections. |
|
2752 |
void tst_QMatrixNxN::perspective() |
|
2753 |
{ |
|
2754 |
QMatrix4x4 m1; |
|
2755 |
m1.perspective(45.0f, 1.0f, -1.0f, 1.0f); |
|
2756 |
QVector3D p1 = m1 * QVector3D(-1.0f, -1.0f, 1.0f); |
|
2757 |
QVector3D p2 = m1 * QVector3D(1.0f, -1.0f, 1.0f); |
|
2758 |
QVector3D p3 = m1 * QVector3D(-1.0f, 1.0f, 1.0f); |
|
2759 |
QVector3D p4 = m1 * QVector3D(1.0f, 1.0f, 1.0f); |
|
2760 |
QVector3D p5 = m1 * QVector3D(0.0f, 0.0f, 2.0f); |
|
2761 |
QVERIFY(fuzzyCompare(p1.x(), 2.41421)); |
|
2762 |
QVERIFY(fuzzyCompare(p1.y(), 2.41421)); |
|
2763 |
QVERIFY(fuzzyCompare(p1.z(), -1)); |
|
2764 |
QVERIFY(fuzzyCompare(p2.x(), -2.41421)); |
|
2765 |
QVERIFY(fuzzyCompare(p2.y(), 2.41421)); |
|
2766 |
QVERIFY(fuzzyCompare(p2.z(), -1.0f)); |
|
2767 |
QVERIFY(fuzzyCompare(p3.x(), 2.41421)); |
|
2768 |
QVERIFY(fuzzyCompare(p3.y(), -2.41421)); |
|
2769 |
QVERIFY(fuzzyCompare(p3.z(), -1.0f)); |
|
2770 |
QVERIFY(fuzzyCompare(p4.x(), -2.41421)); |
|
2771 |
QVERIFY(fuzzyCompare(p4.y(), -2.41421)); |
|
2772 |
QVERIFY(fuzzyCompare(p4.z(), -1.0f)); |
|
2773 |
QVERIFY(fuzzyCompare(p5.x(), 0.0f)); |
|
2774 |
QVERIFY(fuzzyCompare(p5.y(), 0.0f)); |
|
2775 |
QVERIFY(fuzzyCompare(p5.z(), -0.5f)); |
|
2776 |
||
2777 |
// An empty view volume should leave the matrix alone. |
|
2778 |
QMatrix4x4 m5; |
|
2779 |
m5.perspective(45.0f, 1.0f, 0.0f, 0.0f); |
|
2780 |
QVERIFY(m5.isIdentity()); |
|
2781 |
m5.perspective(45.0f, 0.0f, -1.0f, 1.0f); |
|
2782 |
QVERIFY(m5.isIdentity()); |
|
2783 |
m5.perspective(0.0f, 1.0f, -1.0f, 1.0f); |
|
2784 |
QVERIFY(m5.isIdentity()); |
|
2785 |
} |
|
2786 |
||
2787 |
// Test left-handed vs right-handed coordinate flipping. |
|
2788 |
void tst_QMatrixNxN::flipCoordinates() |
|
2789 |
{ |
|
2790 |
QMatrix4x4 m1; |
|
2791 |
m1.flipCoordinates(); |
|
2792 |
QVector3D p1 = m1 * QVector3D(2, 3, 4); |
|
2793 |
QVERIFY(p1 == QVector3D(2, -3, -4)); |
|
2794 |
||
2795 |
QMatrix4x4 m2; |
|
2796 |
m2.scale(2.0f, 3.0f, 1.0f); |
|
2797 |
m2.flipCoordinates(); |
|
2798 |
QVector3D p2 = m2 * QVector3D(2, 3, 4); |
|
2799 |
QVERIFY(p2 == QVector3D(4, -9, -4)); |
|
2800 |
||
2801 |
QMatrix4x4 m3; |
|
2802 |
m3.translate(2.0f, 3.0f, 1.0f); |
|
2803 |
m3.flipCoordinates(); |
|
2804 |
QVector3D p3 = m3 * QVector3D(2, 3, 4); |
|
2805 |
QVERIFY(p3 == QVector3D(4, 0, -3)); |
|
2806 |
||
2807 |
QMatrix4x4 m4; |
|
2808 |
m4.rotate(90.0f, 0.0f, 0.0f, 1.0f); |
|
2809 |
m4.flipCoordinates(); |
|
2810 |
QVector3D p4 = m4 * QVector3D(2, 3, 4); |
|
2811 |
QVERIFY(p4 == QVector3D(3, 2, -4)); |
|
2812 |
} |
|
2813 |
||
2814 |
// Test conversion of generic matrices to and from the non-generic types. |
|
2815 |
void tst_QMatrixNxN::convertGeneric() |
|
2816 |
{ |
|
2817 |
QMatrix4x3 m1(uniqueValues4x3); |
|
2818 |
||
2819 |
static qreal const unique4x4[16] = { |
|
2820 |
1.0f, 2.0f, 3.0f, 4.0f, |
|
2821 |
5.0f, 6.0f, 7.0f, 8.0f, |
|
2822 |
9.0f, 10.0f, 11.0f, 12.0f, |
|
2823 |
0.0f, 0.0f, 0.0f, 1.0f |
|
2824 |
}; |
|
2825 |
#if !defined(QT_NO_MEMBER_TEMPLATES) |
|
2826 |
QMatrix4x4 m4(m1); |
|
2827 |
QVERIFY(isSame(m4, unique4x4)); |
|
2828 |
#endif |
|
2829 |
QMatrix4x4 m5 = qGenericMatrixToMatrix4x4(m1); |
|
2830 |
QVERIFY(isSame(m5, unique4x4)); |
|
2831 |
||
2832 |
static qreal const conv4x4[12] = { |
|
2833 |
1.0f, 2.0f, 3.0f, 4.0f, |
|
2834 |
5.0f, 6.0f, 7.0f, 8.0f, |
|
2835 |
9.0f, 10.0f, 11.0f, 12.0f |
|
2836 |
}; |
|
2837 |
QMatrix4x4 m9(uniqueValues4); |
|
2838 |
#if !defined(QT_NO_MEMBER_TEMPLATES) |
|
2839 |
QMatrix4x3 m10 = m9.toGenericMatrix<4, 3>(); |
|
2840 |
QVERIFY(isSame(m10, conv4x4)); |
|
2841 |
#endif |
|
2842 |
||
2843 |
QMatrix4x3 m11 = qGenericMatrixFromMatrix4x4<4, 3>(m9); |
|
2844 |
QVERIFY(isSame(m11, conv4x4)); |
|
2845 |
} |
|
2846 |
||
2847 |
// Copy of "flagBits" in qmatrix4x4.h. |
|
2848 |
enum { |
|
2849 |
Identity = 0x0001, // Identity matrix |
|
2850 |
General = 0x0002, // General matrix, unknown contents |
|
2851 |
Translation = 0x0004, // Contains a simple translation |
|
2852 |
Scale = 0x0008, // Contains a simple scale |
|
2853 |
Rotation = 0x0010 // Contains a simple rotation |
|
2854 |
}; |
|
2855 |
||
2856 |
// Structure that allows direct access to "flagBits" for testing. |
|
2857 |
struct Matrix4x4 |
|
2858 |
{ |
|
2859 |
qreal m[4][4]; |
|
2860 |
int flagBits; |
|
2861 |
}; |
|
2862 |
||
2863 |
// Test the inferring of special matrix types. |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2864 |
void tst_QMatrixNxN::optimize_data() |
0 | 2865 |
{ |
2866 |
QTest::addColumn<void *>("mValues"); |
|
2867 |
QTest::addColumn<int>("flagBits"); |
|
2868 |
||
2869 |
QTest::newRow("null") |
|
2870 |
<< (void *)nullValues4 << (int)General; |
|
2871 |
QTest::newRow("identity") |
|
2872 |
<< (void *)identityValues4 << (int)Identity; |
|
2873 |
QTest::newRow("unique") |
|
2874 |
<< (void *)uniqueValues4 << (int)General; |
|
2875 |
||
2876 |
static qreal scaleValues[16] = { |
|
2877 |
2.0f, 0.0f, 0.0f, 0.0f, |
|
2878 |
0.0f, 3.0f, 0.0f, 0.0f, |
|
2879 |
0.0f, 0.0f, 4.0f, 0.0f, |
|
2880 |
0.0f, 0.0f, 0.0f, 1.0f |
|
2881 |
}; |
|
2882 |
QTest::newRow("scale") |
|
2883 |
<< (void *)scaleValues << (int)Scale; |
|
2884 |
||
2885 |
static qreal translateValues[16] = { |
|
2886 |
1.0f, 0.0f, 0.0f, 2.0f, |
|
2887 |
0.0f, 1.0f, 0.0f, 3.0f, |
|
2888 |
0.0f, 0.0f, 1.0f, 4.0f, |
|
2889 |
0.0f, 0.0f, 0.0f, 1.0f |
|
2890 |
}; |
|
2891 |
QTest::newRow("scale") |
|
2892 |
<< (void *)translateValues << (int)Translation; |
|
2893 |
||
2894 |
static qreal bothValues[16] = { |
|
2895 |
1.0f, 0.0f, 0.0f, 2.0f, |
|
2896 |
0.0f, 2.0f, 0.0f, 0.0f, |
|
2897 |
0.0f, 0.0f, 1.0f, 4.0f, |
|
2898 |
0.0f, 0.0f, 0.0f, 1.0f |
|
2899 |
}; |
|
2900 |
QTest::newRow("both") |
|
2901 |
<< (void *)bothValues << (int)(Scale | Translation); |
|
2902 |
||
2903 |
static qreal belowValues[16] = { |
|
2904 |
1.0f, 0.0f, 0.0f, 0.0f, |
|
2905 |
0.0f, 1.0f, 0.0f, 0.0f, |
|
2906 |
0.0f, 0.0f, 1.0f, 0.0f, |
|
2907 |
4.0f, 0.0f, 0.0f, 1.0f |
|
2908 |
}; |
|
2909 |
QTest::newRow("below") |
|
2910 |
<< (void *)belowValues << (int)General; |
|
2911 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2912 |
void tst_QMatrixNxN::optimize() |
0 | 2913 |
{ |
2914 |
QFETCH(void *, mValues); |
|
2915 |
QFETCH(int, flagBits); |
|
2916 |
||
2917 |
QMatrix4x4 m((const qreal *)mValues); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2918 |
m.optimize(); |
0 | 2919 |
|
2920 |
QCOMPARE(reinterpret_cast<Matrix4x4 *>(&m)->flagBits, flagBits); |
|
2921 |
} |
|
2922 |
||
2923 |
void tst_QMatrixNxN::columnsAndRows() |
|
2924 |
{ |
|
2925 |
QMatrix4x4 m1(uniqueValues4); |
|
2926 |
||
2927 |
QVERIFY(m1.column(0) == QVector4D(1, 5, 9, 13)); |
|
2928 |
QVERIFY(m1.column(1) == QVector4D(2, 6, 10, 14)); |
|
2929 |
QVERIFY(m1.column(2) == QVector4D(3, 7, 11, 15)); |
|
2930 |
QVERIFY(m1.column(3) == QVector4D(4, 8, 12, 16)); |
|
2931 |
||
2932 |
QVERIFY(m1.row(0) == QVector4D(1, 2, 3, 4)); |
|
2933 |
QVERIFY(m1.row(1) == QVector4D(5, 6, 7, 8)); |
|
2934 |
QVERIFY(m1.row(2) == QVector4D(9, 10, 11, 12)); |
|
2935 |
QVERIFY(m1.row(3) == QVector4D(13, 14, 15, 16)); |
|
2936 |
||
2937 |
m1.setColumn(0, QVector4D(-1, -5, -9, -13)); |
|
2938 |
m1.setColumn(1, QVector4D(-2, -6, -10, -14)); |
|
2939 |
m1.setColumn(2, QVector4D(-3, -7, -11, -15)); |
|
2940 |
m1.setColumn(3, QVector4D(-4, -8, -12, -16)); |
|
2941 |
||
2942 |
QVERIFY(m1.column(0) == QVector4D(-1, -5, -9, -13)); |
|
2943 |
QVERIFY(m1.column(1) == QVector4D(-2, -6, -10, -14)); |
|
2944 |
QVERIFY(m1.column(2) == QVector4D(-3, -7, -11, -15)); |
|
2945 |
QVERIFY(m1.column(3) == QVector4D(-4, -8, -12, -16)); |
|
2946 |
||
2947 |
QVERIFY(m1.row(0) == QVector4D(-1, -2, -3, -4)); |
|
2948 |
QVERIFY(m1.row(1) == QVector4D(-5, -6, -7, -8)); |
|
2949 |
QVERIFY(m1.row(2) == QVector4D(-9, -10, -11, -12)); |
|
2950 |
QVERIFY(m1.row(3) == QVector4D(-13, -14, -15, -16)); |
|
2951 |
||
2952 |
m1.setRow(0, QVector4D(1, 5, 9, 13)); |
|
2953 |
m1.setRow(1, QVector4D(2, 6, 10, 14)); |
|
2954 |
m1.setRow(2, QVector4D(3, 7, 11, 15)); |
|
2955 |
m1.setRow(3, QVector4D(4, 8, 12, 16)); |
|
2956 |
||
2957 |
QVERIFY(m1.column(0) == QVector4D(1, 2, 3, 4)); |
|
2958 |
QVERIFY(m1.column(1) == QVector4D(5, 6, 7, 8)); |
|
2959 |
QVERIFY(m1.column(2) == QVector4D(9, 10, 11, 12)); |
|
2960 |
QVERIFY(m1.column(3) == QVector4D(13, 14, 15, 16)); |
|
2961 |
||
2962 |
QVERIFY(m1.row(0) == QVector4D(1, 5, 9, 13)); |
|
2963 |
QVERIFY(m1.row(1) == QVector4D(2, 6, 10, 14)); |
|
2964 |
QVERIFY(m1.row(2) == QVector4D(3, 7, 11, 15)); |
|
2965 |
QVERIFY(m1.row(3) == QVector4D(4, 8, 12, 16)); |
|
2966 |
} |
|
2967 |
||
2968 |
// Test converting QMatrix objects into QMatrix4x4 and then |
|
2969 |
// checking that transformations in the original perform the |
|
2970 |
// equivalent transformations in the new matrix. |
|
2971 |
void tst_QMatrixNxN::convertQMatrix() |
|
2972 |
{ |
|
2973 |
QMatrix m1; |
|
2974 |
m1.translate(-3.5, 2.0); |
|
2975 |
QPointF p1 = m1.map(QPointF(100.0, 150.0)); |
|
2976 |
QCOMPARE(p1.x(), 100.0 - 3.5); |
|
2977 |
QCOMPARE(p1.y(), 150.0 + 2.0); |
|
2978 |
||
2979 |
QMatrix4x4 m2(m1); |
|
2980 |
QPointF p2 = m2 * QPointF(100.0, 150.0); |
|
2981 |
QCOMPARE((double)p2.x(), 100.0 - 3.5); |
|
2982 |
QCOMPARE((double)p2.y(), 150.0 + 2.0); |
|
2983 |
QVERIFY(m1 == m2.toAffine()); |
|
2984 |
||
2985 |
QMatrix m3; |
|
2986 |
m3.scale(1.5, -2.0); |
|
2987 |
QPointF p3 = m3.map(QPointF(100.0, 150.0)); |
|
2988 |
QCOMPARE(p3.x(), 1.5 * 100.0); |
|
2989 |
QCOMPARE(p3.y(), -2.0 * 150.0); |
|
2990 |
||
2991 |
QMatrix4x4 m4(m3); |
|
2992 |
QPointF p4 = m4 * QPointF(100.0, 150.0); |
|
2993 |
QCOMPARE((double)p4.x(), 1.5 * 100.0); |
|
2994 |
QCOMPARE((double)p4.y(), -2.0 * 150.0); |
|
2995 |
QVERIFY(m3 == m4.toAffine()); |
|
2996 |
||
2997 |
QMatrix m5; |
|
2998 |
m5.rotate(45.0); |
|
2999 |
QPointF p5 = m5.map(QPointF(100.0, 150.0)); |
|
3000 |
||
3001 |
QMatrix4x4 m6(m5); |
|
3002 |
QPointF p6 = m6 * QPointF(100.0, 150.0); |
|
3003 |
QVERIFY(fuzzyCompare(p5.x(), p6.x())); |
|
3004 |
QVERIFY(fuzzyCompare(p5.y(), p6.y())); |
|
3005 |
||
3006 |
QMatrix m7 = m6.toAffine(); |
|
3007 |
QVERIFY(fuzzyCompare(m5.m11(), m7.m11())); |
|
3008 |
QVERIFY(fuzzyCompare(m5.m12(), m7.m12())); |
|
3009 |
QVERIFY(fuzzyCompare(m5.m21(), m7.m21())); |
|
3010 |
QVERIFY(fuzzyCompare(m5.m22(), m7.m22())); |
|
3011 |
QVERIFY(fuzzyCompare(m5.dx(), m7.dx())); |
|
3012 |
QVERIFY(fuzzyCompare(m5.dy(), m7.dy())); |
|
3013 |
} |
|
3014 |
||
3015 |
// Test converting QTransform objects into QMatrix4x4 and then |
|
3016 |
// checking that transformations in the original perform the |
|
3017 |
// equivalent transformations in the new matrix. |
|
3018 |
void tst_QMatrixNxN::convertQTransform() |
|
3019 |
{ |
|
3020 |
QTransform m1; |
|
3021 |
m1.translate(-3.5, 2.0); |
|
3022 |
QPointF p1 = m1.map(QPointF(100.0, 150.0)); |
|
3023 |
QCOMPARE(p1.x(), 100.0 - 3.5); |
|
3024 |
QCOMPARE(p1.y(), 150.0 + 2.0); |
|
3025 |
||
3026 |
QMatrix4x4 m2(m1); |
|
3027 |
QPointF p2 = m2 * QPointF(100.0, 150.0); |
|
3028 |
QCOMPARE((double)p2.x(), 100.0 - 3.5); |
|
3029 |
QCOMPARE((double)p2.y(), 150.0 + 2.0); |
|
3030 |
QVERIFY(m1 == m2.toTransform()); |
|
3031 |
||
3032 |
QTransform m3; |
|
3033 |
m3.scale(1.5, -2.0); |
|
3034 |
QPointF p3 = m3.map(QPointF(100.0, 150.0)); |
|
3035 |
QCOMPARE(p3.x(), 1.5 * 100.0); |
|
3036 |
QCOMPARE(p3.y(), -2.0 * 150.0); |
|
3037 |
||
3038 |
QMatrix4x4 m4(m3); |
|
3039 |
QPointF p4 = m4 * QPointF(100.0, 150.0); |
|
3040 |
QCOMPARE((double)p4.x(), 1.5 * 100.0); |
|
3041 |
QCOMPARE((double)p4.y(), -2.0 * 150.0); |
|
3042 |
QVERIFY(m3 == m4.toTransform()); |
|
3043 |
||
3044 |
QTransform m5; |
|
3045 |
m5.rotate(45.0); |
|
3046 |
QPointF p5 = m5.map(QPointF(100.0, 150.0)); |
|
3047 |
||
3048 |
QMatrix4x4 m6(m5); |
|
3049 |
QPointF p6 = m6 * QPointF(100.0, 150.0); |
|
3050 |
QVERIFY(fuzzyCompare(p5.x(), p6.x())); |
|
3051 |
QVERIFY(fuzzyCompare(p5.y(), p6.y())); |
|
3052 |
||
3053 |
QTransform m7 = m6.toTransform(); |
|
3054 |
QVERIFY(fuzzyCompare(m5.m11(), m7.m11())); |
|
3055 |
QVERIFY(fuzzyCompare(m5.m12(), m7.m12())); |
|
3056 |
QVERIFY(fuzzyCompare(m5.m21(), m7.m21())); |
|
3057 |
QVERIFY(fuzzyCompare(m5.m22(), m7.m22())); |
|
3058 |
QVERIFY(fuzzyCompare(m5.dx(), m7.dx())); |
|
3059 |
QVERIFY(fuzzyCompare(m5.dy(), m7.dy())); |
|
3060 |
QVERIFY(fuzzyCompare(m5.m13(), m7.m13())); |
|
3061 |
QVERIFY(fuzzyCompare(m5.m23(), m7.m23())); |
|
3062 |
QVERIFY(fuzzyCompare(m5.m33(), m7.m33())); |
|
3063 |
} |
|
3064 |
||
3065 |
// Test filling matrices with specific values. |
|
3066 |
void tst_QMatrixNxN::fill() |
|
3067 |
{ |
|
3068 |
QMatrix4x4 m1; |
|
3069 |
m1.fill(0.0f); |
|
3070 |
QVERIFY(isSame(m1, nullValues4)); |
|
3071 |
||
3072 |
static const qreal fillValues4[] = |
|
3073 |
{2.5f, 2.5f, 2.5f, 2.5f, |
|
3074 |
2.5f, 2.5f, 2.5f, 2.5f, |
|
3075 |
2.5f, 2.5f, 2.5f, 2.5f, |
|
3076 |
2.5f, 2.5f, 2.5f, 2.5f}; |
|
3077 |
m1.fill(2.5f); |
|
3078 |
QVERIFY(isSame(m1, fillValues4)); |
|
3079 |
||
3080 |
QMatrix4x3 m2; |
|
3081 |
m2.fill(0.0f); |
|
3082 |
QVERIFY(isSame(m2, nullValues4x3)); |
|
3083 |
||
3084 |
static const qreal fillValues4x3[] = |
|
3085 |
{2.5f, 2.5f, 2.5f, 2.5f, |
|
3086 |
2.5f, 2.5f, 2.5f, 2.5f, |
|
3087 |
2.5f, 2.5f, 2.5f, 2.5f}; |
|
3088 |
m2.fill(2.5f); |
|
3089 |
QVERIFY(isSame(m2, fillValues4x3)); |
|
3090 |
} |
|
3091 |
||
3092 |
// Test the mapRect() function for QRect and QRectF. |
|
3093 |
void tst_QMatrixNxN::mapRect_data() |
|
3094 |
{ |
|
3095 |
QTest::addColumn<qreal>("x"); |
|
3096 |
QTest::addColumn<qreal>("y"); |
|
3097 |
QTest::addColumn<qreal>("width"); |
|
3098 |
QTest::addColumn<qreal>("height"); |
|
3099 |
||
3100 |
QTest::newRow("null") |
|
3101 |
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
3102 |
QTest::newRow("rect") |
|
3103 |
<< (qreal)1.0f << (qreal)-20.5f << (qreal)100.0f << (qreal)63.75f; |
|
3104 |
} |
|
3105 |
void tst_QMatrixNxN::mapRect() |
|
3106 |
{ |
|
3107 |
QFETCH(qreal, x); |
|
3108 |
QFETCH(qreal, y); |
|
3109 |
QFETCH(qreal, width); |
|
3110 |
QFETCH(qreal, height); |
|
3111 |
||
3112 |
QRectF rect(x, y, width, height); |
|
3113 |
QRect recti(qRound(x), qRound(y), qRound(width), qRound(height)); |
|
3114 |
||
3115 |
QMatrix4x4 m1; |
|
3116 |
QVERIFY(m1.mapRect(rect) == rect); |
|
3117 |
QVERIFY(m1.mapRect(recti) == recti); |
|
3118 |
||
3119 |
QMatrix4x4 m2; |
|
3120 |
m2.translate(-100.5f, 64.0f); |
|
3121 |
QRectF translated = rect.translated(-100.5f, 64.0f); |
|
3122 |
QRect translatedi = QRect(qRound(recti.x() - 100.5f), recti.y() + 64, |
|
3123 |
recti.width(), recti.height()); |
|
3124 |
QVERIFY(m2.mapRect(rect) == translated); |
|
3125 |
QVERIFY(m2.mapRect(recti) == translatedi); |
|
3126 |
||
3127 |
QMatrix4x4 m3; |
|
3128 |
m3.scale(-100.5f, 64.0f); |
|
3129 |
qreal scalex = x * -100.5f; |
|
3130 |
qreal scaley = y * 64.0f; |
|
3131 |
qreal scalewid = width * -100.5f; |
|
3132 |
qreal scaleht = height * 64.0f; |
|
3133 |
if (scalewid < 0.0f) { |
|
3134 |
scalewid = -scalewid; |
|
3135 |
scalex -= scalewid; |
|
3136 |
} |
|
3137 |
if (scaleht < 0.0f) { |
|
3138 |
scaleht = -scaleht; |
|
3139 |
scaley -= scaleht; |
|
3140 |
} |
|
3141 |
QRectF scaled(scalex, scaley, scalewid, scaleht); |
|
3142 |
QVERIFY(m3.mapRect(rect) == scaled); |
|
3143 |
scalex = recti.x() * -100.5f; |
|
3144 |
scaley = recti.y() * 64.0f; |
|
3145 |
scalewid = recti.width() * -100.5f; |
|
3146 |
scaleht = recti.height() * 64.0f; |
|
3147 |
if (scalewid < 0.0f) { |
|
3148 |
scalewid = -scalewid; |
|
3149 |
scalex -= scalewid; |
|
3150 |
} |
|
3151 |
if (scaleht < 0.0f) { |
|
3152 |
scaleht = -scaleht; |
|
3153 |
scaley -= scaleht; |
|
3154 |
} |
|
3155 |
QRect scaledi(qRound(scalex), qRound(scaley), |
|
3156 |
qRound(scalewid), qRound(scaleht)); |
|
3157 |
QVERIFY(m3.mapRect(recti) == scaledi); |
|
3158 |
||
3159 |
QMatrix4x4 m4; |
|
3160 |
m4.translate(-100.5f, 64.0f); |
|
3161 |
m4.scale(-2.5f, 4.0f); |
|
3162 |
qreal transx1 = x * -2.5f - 100.5f; |
|
3163 |
qreal transy1 = y * 4.0f + 64.0f; |
|
3164 |
qreal transx2 = (x + width) * -2.5f - 100.5f; |
|
3165 |
qreal transy2 = (y + height) * 4.0f + 64.0f; |
|
3166 |
if (transx1 > transx2) |
|
3167 |
qSwap(transx1, transx2); |
|
3168 |
if (transy1 > transy2) |
|
3169 |
qSwap(transy1, transy2); |
|
3170 |
QRectF trans(transx1, transy1, transx2 - transx1, transy2 - transy1); |
|
3171 |
QVERIFY(m4.mapRect(rect) == trans); |
|
3172 |
transx1 = recti.x() * -2.5f - 100.5f; |
|
3173 |
transy1 = recti.y() * 4.0f + 64.0f; |
|
3174 |
transx2 = (recti.x() + recti.width()) * -2.5f - 100.5f; |
|
3175 |
transy2 = (recti.y() + recti.height()) * 4.0f + 64.0f; |
|
3176 |
if (transx1 > transx2) |
|
3177 |
qSwap(transx1, transx2); |
|
3178 |
if (transy1 > transy2) |
|
3179 |
qSwap(transy1, transy2); |
|
3180 |
QRect transi(qRound(transx1), qRound(transy1), |
|
3181 |
qRound(transx2) - qRound(transx1), |
|
3182 |
qRound(transy2) - qRound(transy1)); |
|
3183 |
QVERIFY(m4.mapRect(recti) == transi); |
|
3184 |
||
3185 |
m4.rotate(45.0f, 0.0f, 0.0f, 1.0f); |
|
3186 |
||
3187 |
QTransform t4; |
|
3188 |
t4.translate(-100.5f, 64.0f); |
|
3189 |
t4.scale(-2.5f, 4.0f); |
|
3190 |
t4.rotate(45.0f); |
|
3191 |
QRectF mr = m4.mapRect(rect); |
|
3192 |
QRectF tr = t4.mapRect(rect); |
|
3193 |
QVERIFY(fuzzyCompare(mr.x(), tr.x())); |
|
3194 |
QVERIFY(fuzzyCompare(mr.y(), tr.y())); |
|
3195 |
QVERIFY(fuzzyCompare(mr.width(), tr.width())); |
|
3196 |
QVERIFY(fuzzyCompare(mr.height(), tr.height())); |
|
3197 |
||
3198 |
QRect mri = m4.mapRect(recti); |
|
3199 |
QRect tri = t4.mapRect(recti); |
|
3200 |
QVERIFY(mri == tri); |
|
3201 |
} |
|
3202 |
||
3203 |
void tst_QMatrixNxN::mapVector_data() |
|
3204 |
{ |
|
3205 |
QTest::addColumn<void *>("mValues"); |
|
3206 |
||
3207 |
QTest::newRow("null") |
|
3208 |
<< (void *)nullValues4; |
|
3209 |
||
3210 |
QTest::newRow("identity") |
|
3211 |
<< (void *)identityValues4; |
|
3212 |
||
3213 |
QTest::newRow("unique") |
|
3214 |
<< (void *)uniqueValues4; |
|
3215 |
||
3216 |
static const qreal scale[] = |
|
3217 |
{2.0f, 0.0f, 0.0f, 0.0f, |
|
3218 |
0.0f, 11.0f, 0.0f, 0.0f, |
|
3219 |
0.0f, 0.0f, -6.5f, 0.0f, |
|
3220 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
3221 |
QTest::newRow("scale") |
|
3222 |
<< (void *)scale; |
|
3223 |
||
3224 |
static const qreal scaleTranslate[] = |
|
3225 |
{2.0f, 0.0f, 0.0f, 1.0f, |
|
3226 |
0.0f, 11.0f, 0.0f, 2.0f, |
|
3227 |
0.0f, 0.0f, -6.5f, 3.0f, |
|
3228 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
3229 |
QTest::newRow("scaleTranslate") |
|
3230 |
<< (void *)scaleTranslate; |
|
3231 |
||
3232 |
static const qreal translate[] = |
|
3233 |
{1.0f, 0.0f, 0.0f, 1.0f, |
|
3234 |
0.0f, 1.0f, 0.0f, 2.0f, |
|
3235 |
0.0f, 0.0f, 1.0f, 3.0f, |
|
3236 |
0.0f, 0.0f, 0.0f, 1.0f}; |
|
3237 |
QTest::newRow("translate") |
|
3238 |
<< (void *)translate; |
|
3239 |
} |
|
3240 |
void tst_QMatrixNxN::mapVector() |
|
3241 |
{ |
|
3242 |
QFETCH(void *, mValues); |
|
3243 |
||
3244 |
QMatrix4x4 m1((const qreal *)mValues); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3245 |
m1.optimize(); |
0 | 3246 |
|
3247 |
QVector3D v(3.5f, -1.0f, 2.5f); |
|
3248 |
||
3249 |
QVector3D expected |
|
3250 |
(v.x() * m1(0, 0) + v.y() * m1(0, 1) + v.z() * m1(0, 2), |
|
3251 |
v.x() * m1(1, 0) + v.y() * m1(1, 1) + v.z() * m1(1, 2), |
|
3252 |
v.x() * m1(2, 0) + v.y() * m1(2, 1) + v.z() * m1(2, 2)); |
|
3253 |
||
3254 |
QVector3D actual = m1.mapVector(v); |
|
3255 |
||
3256 |
QVERIFY(fuzzyCompare(actual.x(), expected.x())); |
|
3257 |
QVERIFY(fuzzyCompare(actual.y(), expected.y())); |
|
3258 |
QVERIFY(fuzzyCompare(actual.z(), expected.z())); |
|
3259 |
} |
|
3260 |
||
3261 |
class tst_QMatrixNxN4x4Properties : public QObject |
|
3262 |
{ |
|
3263 |
Q_OBJECT |
|
3264 |
Q_PROPERTY(QMatrix4x4 matrix READ matrix WRITE setMatrix) |
|
3265 |
public: |
|
3266 |
tst_QMatrixNxN4x4Properties(QObject *parent = 0) : QObject(parent) {} |
|
3267 |
||
3268 |
QMatrix4x4 matrix() const { return m; } |
|
3269 |
void setMatrix(const QMatrix4x4& value) { m = value; } |
|
3270 |
||
3271 |
private: |
|
3272 |
QMatrix4x4 m; |
|
3273 |
}; |
|
3274 |
||
3275 |
// Test getting and setting matrix properties via the metaobject system. |
|
3276 |
void tst_QMatrixNxN::properties() |
|
3277 |
{ |
|
3278 |
tst_QMatrixNxN4x4Properties obj; |
|
3279 |
||
3280 |
QMatrix4x4 m1(uniqueValues4); |
|
3281 |
obj.setMatrix(m1); |
|
3282 |
||
3283 |
QMatrix4x4 m2 = qVariantValue<QMatrix4x4>(obj.property("matrix")); |
|
3284 |
QVERIFY(isSame(m2, uniqueValues4)); |
|
3285 |
||
3286 |
QMatrix4x4 m3(transposedValues4); |
|
3287 |
obj.setProperty("matrix", qVariantFromValue(m3)); |
|
3288 |
||
3289 |
m2 = qVariantValue<QMatrix4x4>(obj.property("matrix")); |
|
3290 |
QVERIFY(isSame(m2, transposedValues4)); |
|
3291 |
} |
|
3292 |
||
3293 |
void tst_QMatrixNxN::metaTypes() |
|
3294 |
{ |
|
3295 |
QVERIFY(QMetaType::type("QMatrix4x4") == QMetaType::QMatrix4x4); |
|
3296 |
||
3297 |
QCOMPARE(QByteArray(QMetaType::typeName(QMetaType::QMatrix4x4)), |
|
3298 |
QByteArray("QMatrix4x4")); |
|
3299 |
||
3300 |
QVERIFY(QMetaType::isRegistered(QMetaType::QMatrix4x4)); |
|
3301 |
||
3302 |
QVERIFY(qMetaTypeId<QMatrix4x4>() == QMetaType::QMatrix4x4); |
|
3303 |
} |
|
3304 |
||
3305 |
QTEST_APPLESS_MAIN(tst_QMatrixNxN) |
|
3306 |
||
3307 |
#include "tst_qmatrixnxn.moc" |