|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the 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/qvector2d.h> |
|
45 #include <QtGui/qvector3d.h> |
|
46 #include <QtGui/qvector4d.h> |
|
47 |
|
48 class tst_QVector : public QObject |
|
49 { |
|
50 Q_OBJECT |
|
51 public: |
|
52 tst_QVector() {} |
|
53 ~tst_QVector() {} |
|
54 |
|
55 private slots: |
|
56 void create2(); |
|
57 void create3(); |
|
58 void create4(); |
|
59 |
|
60 void length2_data(); |
|
61 void length2(); |
|
62 void length3_data(); |
|
63 void length3(); |
|
64 void length4_data(); |
|
65 void length4(); |
|
66 |
|
67 void normalized2_data(); |
|
68 void normalized2(); |
|
69 void normalized3_data(); |
|
70 void normalized3(); |
|
71 void normalized4_data(); |
|
72 void normalized4(); |
|
73 |
|
74 void normalize2_data(); |
|
75 void normalize2(); |
|
76 void normalize3_data(); |
|
77 void normalize3(); |
|
78 void normalize4_data(); |
|
79 void normalize4(); |
|
80 |
|
81 void compare2(); |
|
82 void compare3(); |
|
83 void compare4(); |
|
84 |
|
85 void add2_data(); |
|
86 void add2(); |
|
87 void add3_data(); |
|
88 void add3(); |
|
89 void add4_data(); |
|
90 void add4(); |
|
91 |
|
92 void subtract2_data(); |
|
93 void subtract2(); |
|
94 void subtract3_data(); |
|
95 void subtract3(); |
|
96 void subtract4_data(); |
|
97 void subtract4(); |
|
98 |
|
99 void multiply2_data(); |
|
100 void multiply2(); |
|
101 void multiply3_data(); |
|
102 void multiply3(); |
|
103 void multiply4_data(); |
|
104 void multiply4(); |
|
105 |
|
106 void multiplyFactor2_data(); |
|
107 void multiplyFactor2(); |
|
108 void multiplyFactor3_data(); |
|
109 void multiplyFactor3(); |
|
110 void multiplyFactor4_data(); |
|
111 void multiplyFactor4(); |
|
112 |
|
113 void divide2_data(); |
|
114 void divide2(); |
|
115 void divide3_data(); |
|
116 void divide3(); |
|
117 void divide4_data(); |
|
118 void divide4(); |
|
119 |
|
120 void negate2_data(); |
|
121 void negate2(); |
|
122 void negate3_data(); |
|
123 void negate3(); |
|
124 void negate4_data(); |
|
125 void negate4(); |
|
126 |
|
127 void crossProduct_data(); |
|
128 void crossProduct(); |
|
129 void normal_data(); |
|
130 void normal(); |
|
131 void distanceToPlane_data(); |
|
132 void distanceToPlane(); |
|
133 void distanceToLine_data(); |
|
134 void distanceToLine(); |
|
135 |
|
136 void dotProduct2_data(); |
|
137 void dotProduct2(); |
|
138 void dotProduct3_data(); |
|
139 void dotProduct3(); |
|
140 void dotProduct4_data(); |
|
141 void dotProduct4(); |
|
142 |
|
143 void properties(); |
|
144 void metaTypes(); |
|
145 }; |
|
146 |
|
147 // QVector2/3/4D use float internally, which can sometimes lead |
|
148 // to precision issues when converting to and from qreal. |
|
149 // This fuzzy compare is slightly "fuzzier" than the default |
|
150 // qFuzzyCompare for qreal to compensate. |
|
151 static bool fuzzyCompare(qreal x, qreal y) |
|
152 { |
|
153 return qFuzzyIsNull((float)(x - y)); |
|
154 } |
|
155 |
|
156 // Test the creation of QVector2D objects in various ways: |
|
157 // construct, copy, and modify. |
|
158 void tst_QVector::create2() |
|
159 { |
|
160 QVector2D null; |
|
161 QCOMPARE(null.x(), (qreal)0.0f); |
|
162 QCOMPARE(null.y(), (qreal)0.0f); |
|
163 QVERIFY(null.isNull()); |
|
164 |
|
165 QVector2D v1(1.0f, 2.5f); |
|
166 QCOMPARE(v1.x(), (qreal)1.0f); |
|
167 QCOMPARE(v1.y(), (qreal)2.5f); |
|
168 QVERIFY(!v1.isNull()); |
|
169 |
|
170 QVector2D v1i(1, 2); |
|
171 QCOMPARE(v1i.x(), (qreal)1.0f); |
|
172 QCOMPARE(v1i.y(), (qreal)2.0f); |
|
173 QVERIFY(!v1i.isNull()); |
|
174 |
|
175 QVector2D v2(v1); |
|
176 QCOMPARE(v2.x(), (qreal)1.0f); |
|
177 QCOMPARE(v2.y(), (qreal)2.5f); |
|
178 QVERIFY(!v2.isNull()); |
|
179 |
|
180 QVector2D v4; |
|
181 QCOMPARE(v4.x(), (qreal)0.0f); |
|
182 QCOMPARE(v4.y(), (qreal)0.0f); |
|
183 QVERIFY(v4.isNull()); |
|
184 v4 = v1; |
|
185 QCOMPARE(v4.x(), (qreal)1.0f); |
|
186 QCOMPARE(v4.y(), (qreal)2.5f); |
|
187 QVERIFY(!v4.isNull()); |
|
188 |
|
189 QVector2D v5(QPoint(1, 2)); |
|
190 QCOMPARE(v5.x(), (qreal)1.0f); |
|
191 QCOMPARE(v5.y(), (qreal)2.0f); |
|
192 QVERIFY(!v5.isNull()); |
|
193 |
|
194 QVector2D v6(QPointF(1, 2.5)); |
|
195 QCOMPARE(v6.x(), (qreal)1.0f); |
|
196 QCOMPARE(v6.y(), (qreal)2.5f); |
|
197 QVERIFY(!v6.isNull()); |
|
198 |
|
199 QVector2D v7(QVector3D(1.0f, 2.5f, 54.25f)); |
|
200 QCOMPARE(v7.x(), (qreal)1.0f); |
|
201 QCOMPARE(v7.y(), (qreal)2.5f); |
|
202 QVERIFY(!v6.isNull()); |
|
203 |
|
204 QVector2D v8(QVector4D(1.0f, 2.5f, 54.25f, 34.0f)); |
|
205 QCOMPARE(v8.x(), (qreal)1.0f); |
|
206 QCOMPARE(v8.y(), (qreal)2.5f); |
|
207 QVERIFY(!v6.isNull()); |
|
208 |
|
209 v1.setX(3.0f); |
|
210 QCOMPARE(v1.x(), (qreal)3.0f); |
|
211 QCOMPARE(v1.y(), (qreal)2.5f); |
|
212 QVERIFY(!v1.isNull()); |
|
213 |
|
214 v1.setY(10.5f); |
|
215 QCOMPARE(v1.x(), (qreal)3.0f); |
|
216 QCOMPARE(v1.y(), (qreal)10.5f); |
|
217 QVERIFY(!v1.isNull()); |
|
218 |
|
219 v1.setX(0.0f); |
|
220 v1.setY(0.0f); |
|
221 QCOMPARE(v1.x(), (qreal)0.0f); |
|
222 QCOMPARE(v1.y(), (qreal)0.0f); |
|
223 QVERIFY(v1.isNull()); |
|
224 |
|
225 QPoint p1 = v8.toPoint(); |
|
226 QCOMPARE(p1.x(), 1); |
|
227 QCOMPARE(p1.y(), 3); |
|
228 |
|
229 QPointF p2 = v8.toPointF(); |
|
230 QCOMPARE((qreal)p2.x(), (qreal)1.0f); |
|
231 QCOMPARE((qreal)p2.y(), (qreal)2.5f); |
|
232 |
|
233 QVector3D v9 = v8.toVector3D(); |
|
234 QCOMPARE(v9.x(), (qreal)1.0f); |
|
235 QCOMPARE(v9.y(), (qreal)2.5f); |
|
236 QCOMPARE(v9.z(), (qreal)0.0f); |
|
237 |
|
238 QVector4D v10 = v8.toVector4D(); |
|
239 QCOMPARE(v10.x(), (qreal)1.0f); |
|
240 QCOMPARE(v10.y(), (qreal)2.5f); |
|
241 QCOMPARE(v10.z(), (qreal)0.0f); |
|
242 QCOMPARE(v10.w(), (qreal)0.0f); |
|
243 } |
|
244 |
|
245 // Test the creation of QVector3D objects in various ways: |
|
246 // construct, copy, and modify. |
|
247 void tst_QVector::create3() |
|
248 { |
|
249 QVector3D null; |
|
250 QCOMPARE(null.x(), (qreal)0.0f); |
|
251 QCOMPARE(null.y(), (qreal)0.0f); |
|
252 QCOMPARE(null.z(), (qreal)0.0f); |
|
253 QVERIFY(null.isNull()); |
|
254 |
|
255 QVector3D v1(1.0f, 2.5f, -89.25f); |
|
256 QCOMPARE(v1.x(), (qreal)1.0f); |
|
257 QCOMPARE(v1.y(), (qreal)2.5f); |
|
258 QCOMPARE(v1.z(), (qreal)-89.25f); |
|
259 QVERIFY(!v1.isNull()); |
|
260 |
|
261 QVector3D v1i(1, 2, -89); |
|
262 QCOMPARE(v1i.x(), (qreal)1.0f); |
|
263 QCOMPARE(v1i.y(), (qreal)2.0f); |
|
264 QCOMPARE(v1i.z(), (qreal)-89.0f); |
|
265 QVERIFY(!v1i.isNull()); |
|
266 |
|
267 QVector3D v2(v1); |
|
268 QCOMPARE(v2.x(), (qreal)1.0f); |
|
269 QCOMPARE(v2.y(), (qreal)2.5f); |
|
270 QCOMPARE(v2.z(), (qreal)-89.25f); |
|
271 QVERIFY(!v2.isNull()); |
|
272 |
|
273 QVector3D v3(1.0f, 2.5f, 0.0f); |
|
274 QCOMPARE(v3.x(), (qreal)1.0f); |
|
275 QCOMPARE(v3.y(), (qreal)2.5f); |
|
276 QCOMPARE(v3.z(), (qreal)0.0f); |
|
277 QVERIFY(!v3.isNull()); |
|
278 |
|
279 QVector3D v3i(1, 2, 0); |
|
280 QCOMPARE(v3i.x(), (qreal)1.0f); |
|
281 QCOMPARE(v3i.y(), (qreal)2.0f); |
|
282 QCOMPARE(v3i.z(), (qreal)0.0f); |
|
283 QVERIFY(!v3i.isNull()); |
|
284 |
|
285 QVector3D v4; |
|
286 QCOMPARE(v4.x(), (qreal)0.0f); |
|
287 QCOMPARE(v4.y(), (qreal)0.0f); |
|
288 QCOMPARE(v4.z(), (qreal)0.0f); |
|
289 QVERIFY(v4.isNull()); |
|
290 v4 = v1; |
|
291 QCOMPARE(v4.x(), (qreal)1.0f); |
|
292 QCOMPARE(v4.y(), (qreal)2.5f); |
|
293 QCOMPARE(v4.z(), (qreal)-89.25f); |
|
294 QVERIFY(!v4.isNull()); |
|
295 |
|
296 QVector3D v5(QPoint(1, 2)); |
|
297 QCOMPARE(v5.x(), (qreal)1.0f); |
|
298 QCOMPARE(v5.y(), (qreal)2.0f); |
|
299 QCOMPARE(v5.z(), (qreal)0.0f); |
|
300 QVERIFY(!v5.isNull()); |
|
301 |
|
302 QVector3D v6(QPointF(1, 2.5)); |
|
303 QCOMPARE(v6.x(), (qreal)1.0f); |
|
304 QCOMPARE(v6.y(), (qreal)2.5f); |
|
305 QCOMPARE(v6.z(), (qreal)0.0f); |
|
306 QVERIFY(!v6.isNull()); |
|
307 |
|
308 QVector3D v7(QVector2D(1.0f, 2.5f)); |
|
309 QCOMPARE(v7.x(), (qreal)1.0f); |
|
310 QCOMPARE(v7.y(), (qreal)2.5f); |
|
311 QCOMPARE(v7.z(), (qreal)0.0f); |
|
312 QVERIFY(!v7.isNull()); |
|
313 |
|
314 QVector3D v8(QVector2D(1.0f, 2.5f), 54.25f); |
|
315 QCOMPARE(v8.x(), (qreal)1.0f); |
|
316 QCOMPARE(v8.y(), (qreal)2.5f); |
|
317 QCOMPARE(v8.z(), (qreal)54.25f); |
|
318 QVERIFY(!v8.isNull()); |
|
319 |
|
320 QVector3D v9(QVector4D(1.0f, 2.5f, 54.25f, 34.0f)); |
|
321 QCOMPARE(v9.x(), (qreal)1.0f); |
|
322 QCOMPARE(v9.y(), (qreal)2.5f); |
|
323 QCOMPARE(v9.z(), (qreal)54.25f); |
|
324 QVERIFY(!v9.isNull()); |
|
325 |
|
326 v1.setX(3.0f); |
|
327 QCOMPARE(v1.x(), (qreal)3.0f); |
|
328 QCOMPARE(v1.y(), (qreal)2.5f); |
|
329 QCOMPARE(v1.z(), (qreal)-89.25f); |
|
330 QVERIFY(!v1.isNull()); |
|
331 |
|
332 v1.setY(10.5f); |
|
333 QCOMPARE(v1.x(), (qreal)3.0f); |
|
334 QCOMPARE(v1.y(), (qreal)10.5f); |
|
335 QCOMPARE(v1.z(), (qreal)-89.25f); |
|
336 QVERIFY(!v1.isNull()); |
|
337 |
|
338 v1.setZ(15.5f); |
|
339 QCOMPARE(v1.x(), (qreal)3.0f); |
|
340 QCOMPARE(v1.y(), (qreal)10.5f); |
|
341 QCOMPARE(v1.z(), (qreal)15.5f); |
|
342 QVERIFY(!v1.isNull()); |
|
343 |
|
344 v1.setX(0.0f); |
|
345 v1.setY(0.0f); |
|
346 v1.setZ(0.0f); |
|
347 QCOMPARE(v1.x(), (qreal)0.0f); |
|
348 QCOMPARE(v1.y(), (qreal)0.0f); |
|
349 QCOMPARE(v1.z(), (qreal)0.0f); |
|
350 QVERIFY(v1.isNull()); |
|
351 |
|
352 QPoint p1 = v8.toPoint(); |
|
353 QCOMPARE(p1.x(), 1); |
|
354 QCOMPARE(p1.y(), 3); |
|
355 |
|
356 QPointF p2 = v8.toPointF(); |
|
357 QCOMPARE((qreal)p2.x(), (qreal)1.0f); |
|
358 QCOMPARE((qreal)p2.y(), (qreal)2.5f); |
|
359 |
|
360 QVector2D v10 = v8.toVector2D(); |
|
361 QCOMPARE(v10.x(), (qreal)1.0f); |
|
362 QCOMPARE(v10.y(), (qreal)2.5f); |
|
363 |
|
364 QVector4D v11 = v8.toVector4D(); |
|
365 QCOMPARE(v11.x(), (qreal)1.0f); |
|
366 QCOMPARE(v11.y(), (qreal)2.5f); |
|
367 QCOMPARE(v11.z(), (qreal)54.25f); |
|
368 QCOMPARE(v11.w(), (qreal)0.0f); |
|
369 } |
|
370 |
|
371 // Test the creation of QVector4D objects in various ways: |
|
372 // construct, copy, and modify. |
|
373 void tst_QVector::create4() |
|
374 { |
|
375 QVector4D null; |
|
376 QCOMPARE(null.x(), (qreal)0.0f); |
|
377 QCOMPARE(null.y(), (qreal)0.0f); |
|
378 QCOMPARE(null.z(), (qreal)0.0f); |
|
379 QCOMPARE(null.w(), (qreal)0.0f); |
|
380 QVERIFY(null.isNull()); |
|
381 |
|
382 QVector4D v1(1.0f, 2.5f, -89.25f, 34.0f); |
|
383 QCOMPARE(v1.x(), (qreal)1.0f); |
|
384 QCOMPARE(v1.y(), (qreal)2.5f); |
|
385 QCOMPARE(v1.z(), (qreal)-89.25f); |
|
386 QCOMPARE(v1.w(), (qreal)34.0f); |
|
387 QVERIFY(!v1.isNull()); |
|
388 |
|
389 QVector4D v1i(1, 2, -89, 34); |
|
390 QCOMPARE(v1i.x(), (qreal)1.0f); |
|
391 QCOMPARE(v1i.y(), (qreal)2.0f); |
|
392 QCOMPARE(v1i.z(), (qreal)-89.0f); |
|
393 QCOMPARE(v1i.w(), (qreal)34.0f); |
|
394 QVERIFY(!v1i.isNull()); |
|
395 |
|
396 QVector4D v2(v1); |
|
397 QCOMPARE(v2.x(), (qreal)1.0f); |
|
398 QCOMPARE(v2.y(), (qreal)2.5f); |
|
399 QCOMPARE(v2.z(), (qreal)-89.25f); |
|
400 QCOMPARE(v2.w(), (qreal)34.0f); |
|
401 QVERIFY(!v2.isNull()); |
|
402 |
|
403 QVector4D v3(1.0f, 2.5f, 0.0f, 0.0f); |
|
404 QCOMPARE(v3.x(), (qreal)1.0f); |
|
405 QCOMPARE(v3.y(), (qreal)2.5f); |
|
406 QCOMPARE(v3.z(), (qreal)0.0f); |
|
407 QCOMPARE(v3.w(), (qreal)0.0f); |
|
408 QVERIFY(!v3.isNull()); |
|
409 |
|
410 QVector4D v3i(1, 2, 0, 0); |
|
411 QCOMPARE(v3i.x(), (qreal)1.0f); |
|
412 QCOMPARE(v3i.y(), (qreal)2.0f); |
|
413 QCOMPARE(v3i.z(), (qreal)0.0f); |
|
414 QCOMPARE(v3i.w(), (qreal)0.0f); |
|
415 QVERIFY(!v3i.isNull()); |
|
416 |
|
417 QVector4D v3b(1.0f, 2.5f, -89.25f, 0.0f); |
|
418 QCOMPARE(v3b.x(), (qreal)1.0f); |
|
419 QCOMPARE(v3b.y(), (qreal)2.5f); |
|
420 QCOMPARE(v3b.z(), (qreal)-89.25f); |
|
421 QCOMPARE(v3b.w(), (qreal)0.0f); |
|
422 QVERIFY(!v3b.isNull()); |
|
423 |
|
424 QVector4D v3bi(1, 2, -89, 0); |
|
425 QCOMPARE(v3bi.x(), (qreal)1.0f); |
|
426 QCOMPARE(v3bi.y(), (qreal)2.0f); |
|
427 QCOMPARE(v3bi.z(), (qreal)-89.0f); |
|
428 QCOMPARE(v3bi.w(), (qreal)0.0f); |
|
429 QVERIFY(!v3bi.isNull()); |
|
430 |
|
431 QVector4D v4; |
|
432 QCOMPARE(v4.x(), (qreal)0.0f); |
|
433 QCOMPARE(v4.y(), (qreal)0.0f); |
|
434 QCOMPARE(v4.z(), (qreal)0.0f); |
|
435 QCOMPARE(v4.w(), (qreal)0.0f); |
|
436 QVERIFY(v4.isNull()); |
|
437 v4 = v1; |
|
438 QCOMPARE(v4.x(), (qreal)1.0f); |
|
439 QCOMPARE(v4.y(), (qreal)2.5f); |
|
440 QCOMPARE(v4.z(), (qreal)-89.25f); |
|
441 QCOMPARE(v4.w(), (qreal)34.0f); |
|
442 QVERIFY(!v4.isNull()); |
|
443 |
|
444 QVector4D v5(QPoint(1, 2)); |
|
445 QCOMPARE(v5.x(), (qreal)1.0f); |
|
446 QCOMPARE(v5.y(), (qreal)2.0f); |
|
447 QCOMPARE(v5.z(), (qreal)0.0f); |
|
448 QCOMPARE(v5.w(), (qreal)0.0f); |
|
449 QVERIFY(!v5.isNull()); |
|
450 |
|
451 QVector4D v6(QPointF(1, 2.5)); |
|
452 QCOMPARE(v6.x(), (qreal)1.0f); |
|
453 QCOMPARE(v6.y(), (qreal)2.5f); |
|
454 QCOMPARE(v6.z(), (qreal)0.0f); |
|
455 QCOMPARE(v6.w(), (qreal)0.0f); |
|
456 QVERIFY(!v6.isNull()); |
|
457 |
|
458 QVector4D v7(QVector2D(1.0f, 2.5f)); |
|
459 QCOMPARE(v7.x(), (qreal)1.0f); |
|
460 QCOMPARE(v7.y(), (qreal)2.5f); |
|
461 QCOMPARE(v7.z(), (qreal)0.0f); |
|
462 QCOMPARE(v7.w(), (qreal)0.0f); |
|
463 QVERIFY(!v7.isNull()); |
|
464 |
|
465 QVector4D v8(QVector3D(1.0f, 2.5f, -89.25f)); |
|
466 QCOMPARE(v8.x(), (qreal)1.0f); |
|
467 QCOMPARE(v8.y(), (qreal)2.5f); |
|
468 QCOMPARE(v8.z(), (qreal)-89.25f); |
|
469 QCOMPARE(v8.w(), (qreal)0.0f); |
|
470 QVERIFY(!v8.isNull()); |
|
471 |
|
472 QVector4D v9(QVector3D(1.0f, 2.5f, -89.25f), 34); |
|
473 QCOMPARE(v9.x(), (qreal)1.0f); |
|
474 QCOMPARE(v9.y(), (qreal)2.5f); |
|
475 QCOMPARE(v9.z(), (qreal)-89.25f); |
|
476 QCOMPARE(v9.w(), (qreal)34.0f); |
|
477 QVERIFY(!v9.isNull()); |
|
478 |
|
479 QVector4D v10(QVector2D(1.0f, 2.5f), 23.5f, -8); |
|
480 QCOMPARE(v10.x(), (qreal)1.0f); |
|
481 QCOMPARE(v10.y(), (qreal)2.5f); |
|
482 QCOMPARE(v10.z(), (qreal)23.5f); |
|
483 QCOMPARE(v10.w(), (qreal)-8.0f); |
|
484 QVERIFY(!v10.isNull()); |
|
485 |
|
486 v1.setX(3.0f); |
|
487 QCOMPARE(v1.x(), (qreal)3.0f); |
|
488 QCOMPARE(v1.y(), (qreal)2.5f); |
|
489 QCOMPARE(v1.z(), (qreal)-89.25f); |
|
490 QCOMPARE(v1.w(), (qreal)34.0f); |
|
491 QVERIFY(!v1.isNull()); |
|
492 |
|
493 v1.setY(10.5f); |
|
494 QCOMPARE(v1.x(), (qreal)3.0f); |
|
495 QCOMPARE(v1.y(), (qreal)10.5f); |
|
496 QCOMPARE(v1.z(), (qreal)-89.25f); |
|
497 QCOMPARE(v1.w(), (qreal)34.0f); |
|
498 QVERIFY(!v1.isNull()); |
|
499 |
|
500 v1.setZ(15.5f); |
|
501 QCOMPARE(v1.x(), (qreal)3.0f); |
|
502 QCOMPARE(v1.y(), (qreal)10.5f); |
|
503 QCOMPARE(v1.z(), (qreal)15.5f); |
|
504 QCOMPARE(v1.w(), (qreal)34.0f); |
|
505 QVERIFY(!v1.isNull()); |
|
506 |
|
507 v1.setW(6.0f); |
|
508 QCOMPARE(v1.x(), (qreal)3.0f); |
|
509 QCOMPARE(v1.y(), (qreal)10.5f); |
|
510 QCOMPARE(v1.z(), (qreal)15.5f); |
|
511 QCOMPARE(v1.w(), (qreal)6.0f); |
|
512 QVERIFY(!v1.isNull()); |
|
513 |
|
514 v1.setX(0.0f); |
|
515 v1.setY(0.0f); |
|
516 v1.setZ(0.0f); |
|
517 v1.setW(0.0f); |
|
518 QCOMPARE(v1.x(), (qreal)0.0f); |
|
519 QCOMPARE(v1.y(), (qreal)0.0f); |
|
520 QCOMPARE(v1.z(), (qreal)0.0f); |
|
521 QCOMPARE(v1.w(), (qreal)0.0f); |
|
522 QVERIFY(v1.isNull()); |
|
523 |
|
524 QPoint p1 = v8.toPoint(); |
|
525 QCOMPARE(p1.x(), 1); |
|
526 QCOMPARE(p1.y(), 3); |
|
527 |
|
528 QPointF p2 = v8.toPointF(); |
|
529 QCOMPARE((qreal)p2.x(), (qreal)1.0f); |
|
530 QCOMPARE((qreal)p2.y(), (qreal)2.5f); |
|
531 |
|
532 QVector2D v11 = v8.toVector2D(); |
|
533 QCOMPARE(v11.x(), (qreal)1.0f); |
|
534 QCOMPARE(v11.y(), (qreal)2.5f); |
|
535 |
|
536 QVector3D v12 = v8.toVector3D(); |
|
537 QCOMPARE(v12.x(), (qreal)1.0f); |
|
538 QCOMPARE(v12.y(), (qreal)2.5f); |
|
539 QCOMPARE(v12.z(), (qreal)-89.25f); |
|
540 |
|
541 QVector2D v13 = v9.toVector2DAffine(); |
|
542 QVERIFY(fuzzyCompare(v13.x(), (qreal)(1.0f / 34.0f))); |
|
543 QVERIFY(fuzzyCompare(v13.y(), (qreal)(2.5f / 34.0f))); |
|
544 |
|
545 QVector4D zerow(1.0f, 2.0f, 3.0f, 0.0f); |
|
546 v13 = zerow.toVector2DAffine(); |
|
547 QVERIFY(v13.isNull()); |
|
548 |
|
549 QVector3D v14 = v9.toVector3DAffine(); |
|
550 QVERIFY(fuzzyCompare(v14.x(), (qreal)(1.0f / 34.0f))); |
|
551 QVERIFY(fuzzyCompare(v14.y(), (qreal)(2.5f / 34.0f))); |
|
552 QVERIFY(fuzzyCompare(v14.z(), (qreal)(-89.25f / 34.0f))); |
|
553 |
|
554 v14 = zerow.toVector3DAffine(); |
|
555 QVERIFY(v14.isNull()); |
|
556 } |
|
557 |
|
558 // Test vector length computation for 2D vectors. |
|
559 void tst_QVector::length2_data() |
|
560 { |
|
561 QTest::addColumn<qreal>("x"); |
|
562 QTest::addColumn<qreal>("y"); |
|
563 QTest::addColumn<qreal>("len"); |
|
564 |
|
565 QTest::newRow("null") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
566 QTest::newRow("1x") << (qreal)1.0f << (qreal)0.0f << (qreal)1.0f; |
|
567 QTest::newRow("1y") << (qreal)0.0f << (qreal)1.0f << (qreal)1.0f; |
|
568 QTest::newRow("-1x") << (qreal)-1.0f << (qreal)0.0f << (qreal)1.0f; |
|
569 QTest::newRow("-1y") << (qreal)0.0f << (qreal)-1.0f << (qreal)1.0f; |
|
570 QTest::newRow("two") << (qreal)2.0f << (qreal)-2.0f << (qreal)qSqrt(8.0f); |
|
571 } |
|
572 void tst_QVector::length2() |
|
573 { |
|
574 QFETCH(qreal, x); |
|
575 QFETCH(qreal, y); |
|
576 QFETCH(qreal, len); |
|
577 |
|
578 QVector2D v(x, y); |
|
579 QCOMPARE(v.length(), len); |
|
580 QCOMPARE(v.lengthSquared(), x * x + y * y); |
|
581 } |
|
582 |
|
583 // Test vector length computation for 3D vectors. |
|
584 void tst_QVector::length3_data() |
|
585 { |
|
586 QTest::addColumn<qreal>("x"); |
|
587 QTest::addColumn<qreal>("y"); |
|
588 QTest::addColumn<qreal>("z"); |
|
589 QTest::addColumn<qreal>("len"); |
|
590 |
|
591 QTest::newRow("null") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
592 QTest::newRow("1x") << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; |
|
593 QTest::newRow("1y") << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)1.0f; |
|
594 QTest::newRow("1z") << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)1.0f; |
|
595 QTest::newRow("-1x") << (qreal)-1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; |
|
596 QTest::newRow("-1y") << (qreal)0.0f << (qreal)-1.0f << (qreal)0.0f << (qreal)1.0f; |
|
597 QTest::newRow("-1z") << (qreal)0.0f << (qreal)0.0f << (qreal)-1.0f << (qreal)1.0f; |
|
598 QTest::newRow("two") << (qreal)2.0f << (qreal)-2.0f << (qreal)2.0f << (qreal)qSqrt(12.0f); |
|
599 } |
|
600 void tst_QVector::length3() |
|
601 { |
|
602 QFETCH(qreal, x); |
|
603 QFETCH(qreal, y); |
|
604 QFETCH(qreal, z); |
|
605 QFETCH(qreal, len); |
|
606 |
|
607 QVector3D v(x, y, z); |
|
608 QCOMPARE(v.length(), len); |
|
609 QCOMPARE(v.lengthSquared(), x * x + y * y + z * z); |
|
610 } |
|
611 |
|
612 // Test vector length computation for 4D vectors. |
|
613 void tst_QVector::length4_data() |
|
614 { |
|
615 QTest::addColumn<qreal>("x"); |
|
616 QTest::addColumn<qreal>("y"); |
|
617 QTest::addColumn<qreal>("z"); |
|
618 QTest::addColumn<qreal>("w"); |
|
619 QTest::addColumn<qreal>("len"); |
|
620 |
|
621 QTest::newRow("null") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
622 QTest::newRow("1x") << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; |
|
623 QTest::newRow("1y") << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; |
|
624 QTest::newRow("1z") << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)1.0f; |
|
625 QTest::newRow("1w") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)1.0f; |
|
626 QTest::newRow("-1x") << (qreal)-1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; |
|
627 QTest::newRow("-1y") << (qreal)0.0f << (qreal)-1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f; |
|
628 QTest::newRow("-1z") << (qreal)0.0f << (qreal)0.0f << (qreal)-1.0f << (qreal)0.0f << (qreal)1.0f; |
|
629 QTest::newRow("-1w") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)-1.0f << (qreal)1.0f; |
|
630 QTest::newRow("two") << (qreal)2.0f << (qreal)-2.0f << (qreal)2.0f << (qreal)2.0f << (qreal)qSqrt(16.0f); |
|
631 } |
|
632 void tst_QVector::length4() |
|
633 { |
|
634 QFETCH(qreal, x); |
|
635 QFETCH(qreal, y); |
|
636 QFETCH(qreal, z); |
|
637 QFETCH(qreal, w); |
|
638 QFETCH(qreal, len); |
|
639 |
|
640 QVector4D v(x, y, z, w); |
|
641 QCOMPARE(v.length(), len); |
|
642 QCOMPARE(v.lengthSquared(), x * x + y * y + z * z + w * w); |
|
643 } |
|
644 |
|
645 // Test the unit vector conversion for 2D vectors. |
|
646 void tst_QVector::normalized2_data() |
|
647 { |
|
648 // Use the same test data as the length test. |
|
649 length2_data(); |
|
650 } |
|
651 void tst_QVector::normalized2() |
|
652 { |
|
653 QFETCH(qreal, x); |
|
654 QFETCH(qreal, y); |
|
655 QFETCH(qreal, len); |
|
656 |
|
657 QVector2D v(x, y); |
|
658 QVector2D u = v.normalized(); |
|
659 if (v.isNull()) |
|
660 QVERIFY(u.isNull()); |
|
661 else |
|
662 QVERIFY(fuzzyCompare(u.length(), qreal(1.0f))); |
|
663 QVERIFY(fuzzyCompare(u.x() * len, v.x())); |
|
664 QVERIFY(fuzzyCompare(u.y() * len, v.y())); |
|
665 } |
|
666 |
|
667 // Test the unit vector conversion for 3D vectors. |
|
668 void tst_QVector::normalized3_data() |
|
669 { |
|
670 // Use the same test data as the length test. |
|
671 length3_data(); |
|
672 } |
|
673 void tst_QVector::normalized3() |
|
674 { |
|
675 QFETCH(qreal, x); |
|
676 QFETCH(qreal, y); |
|
677 QFETCH(qreal, z); |
|
678 QFETCH(qreal, len); |
|
679 |
|
680 QVector3D v(x, y, z); |
|
681 QVector3D u = v.normalized(); |
|
682 if (v.isNull()) |
|
683 QVERIFY(u.isNull()); |
|
684 else |
|
685 QVERIFY(fuzzyCompare(u.length(), qreal(1.0f))); |
|
686 QVERIFY(fuzzyCompare(u.x() * len, v.x())); |
|
687 QVERIFY(fuzzyCompare(u.y() * len, v.y())); |
|
688 QVERIFY(fuzzyCompare(u.z() * len, v.z())); |
|
689 } |
|
690 |
|
691 // Test the unit vector conversion for 4D vectors. |
|
692 void tst_QVector::normalized4_data() |
|
693 { |
|
694 // Use the same test data as the length test. |
|
695 length4_data(); |
|
696 } |
|
697 void tst_QVector::normalized4() |
|
698 { |
|
699 QFETCH(qreal, x); |
|
700 QFETCH(qreal, y); |
|
701 QFETCH(qreal, z); |
|
702 QFETCH(qreal, w); |
|
703 QFETCH(qreal, len); |
|
704 |
|
705 QVector4D v(x, y, z, w); |
|
706 QVector4D u = v.normalized(); |
|
707 if (v.isNull()) |
|
708 QVERIFY(u.isNull()); |
|
709 else |
|
710 QVERIFY(fuzzyCompare(u.length(), qreal(1.0f))); |
|
711 QVERIFY(fuzzyCompare(u.x() * len, v.x())); |
|
712 QVERIFY(fuzzyCompare(u.y() * len, v.y())); |
|
713 QVERIFY(fuzzyCompare(u.z() * len, v.z())); |
|
714 QVERIFY(fuzzyCompare(u.w() * len, v.w())); |
|
715 } |
|
716 |
|
717 // Test the unit vector conversion for 2D vectors. |
|
718 void tst_QVector::normalize2_data() |
|
719 { |
|
720 // Use the same test data as the length test. |
|
721 length2_data(); |
|
722 } |
|
723 void tst_QVector::normalize2() |
|
724 { |
|
725 QFETCH(qreal, x); |
|
726 QFETCH(qreal, y); |
|
727 |
|
728 QVector2D v(x, y); |
|
729 bool isNull = v.isNull(); |
|
730 v.normalize(); |
|
731 if (isNull) |
|
732 QVERIFY(v.isNull()); |
|
733 else |
|
734 QVERIFY(fuzzyCompare(v.length(), qreal(1.0f))); |
|
735 } |
|
736 |
|
737 // Test the unit vector conversion for 3D vectors. |
|
738 void tst_QVector::normalize3_data() |
|
739 { |
|
740 // Use the same test data as the length test. |
|
741 length3_data(); |
|
742 } |
|
743 void tst_QVector::normalize3() |
|
744 { |
|
745 QFETCH(qreal, x); |
|
746 QFETCH(qreal, y); |
|
747 QFETCH(qreal, z); |
|
748 |
|
749 QVector3D v(x, y, z); |
|
750 bool isNull = v.isNull(); |
|
751 v.normalize(); |
|
752 if (isNull) |
|
753 QVERIFY(v.isNull()); |
|
754 else |
|
755 QVERIFY(fuzzyCompare(v.length(), qreal(1.0f))); |
|
756 } |
|
757 |
|
758 // Test the unit vector conversion for 4D vectors. |
|
759 void tst_QVector::normalize4_data() |
|
760 { |
|
761 // Use the same test data as the length test. |
|
762 length4_data(); |
|
763 } |
|
764 void tst_QVector::normalize4() |
|
765 { |
|
766 QFETCH(qreal, x); |
|
767 QFETCH(qreal, y); |
|
768 QFETCH(qreal, z); |
|
769 QFETCH(qreal, w); |
|
770 |
|
771 QVector4D v(x, y, z, w); |
|
772 bool isNull = v.isNull(); |
|
773 v.normalize(); |
|
774 if (isNull) |
|
775 QVERIFY(v.isNull()); |
|
776 else |
|
777 QVERIFY(fuzzyCompare(v.length(), qreal(1.0f))); |
|
778 } |
|
779 |
|
780 // Test the comparison operators for 2D vectors. |
|
781 void tst_QVector::compare2() |
|
782 { |
|
783 QVector2D v1(1, 2); |
|
784 QVector2D v2(1, 2); |
|
785 QVector2D v3(3, 2); |
|
786 QVector2D v4(1, 3); |
|
787 |
|
788 QVERIFY(v1 == v2); |
|
789 QVERIFY(v1 != v3); |
|
790 QVERIFY(v1 != v4); |
|
791 } |
|
792 |
|
793 // Test the comparison operators for 3D vectors. |
|
794 void tst_QVector::compare3() |
|
795 { |
|
796 QVector3D v1(1, 2, 4); |
|
797 QVector3D v2(1, 2, 4); |
|
798 QVector3D v3(3, 2, 4); |
|
799 QVector3D v4(1, 3, 4); |
|
800 QVector3D v5(1, 2, 3); |
|
801 |
|
802 QVERIFY(v1 == v2); |
|
803 QVERIFY(v1 != v3); |
|
804 QVERIFY(v1 != v4); |
|
805 QVERIFY(v1 != v5); |
|
806 } |
|
807 |
|
808 // Test the comparison operators for 4D vectors. |
|
809 void tst_QVector::compare4() |
|
810 { |
|
811 QVector4D v1(1, 2, 4, 8); |
|
812 QVector4D v2(1, 2, 4, 8); |
|
813 QVector4D v3(3, 2, 4, 8); |
|
814 QVector4D v4(1, 3, 4, 8); |
|
815 QVector4D v5(1, 2, 3, 8); |
|
816 QVector4D v6(1, 2, 4, 3); |
|
817 |
|
818 QVERIFY(v1 == v2); |
|
819 QVERIFY(v1 != v3); |
|
820 QVERIFY(v1 != v4); |
|
821 QVERIFY(v1 != v5); |
|
822 QVERIFY(v1 != v6); |
|
823 } |
|
824 |
|
825 // Test vector addition for 2D vectors. |
|
826 void tst_QVector::add2_data() |
|
827 { |
|
828 QTest::addColumn<qreal>("x1"); |
|
829 QTest::addColumn<qreal>("y1"); |
|
830 QTest::addColumn<qreal>("x2"); |
|
831 QTest::addColumn<qreal>("y2"); |
|
832 QTest::addColumn<qreal>("x3"); |
|
833 QTest::addColumn<qreal>("y3"); |
|
834 |
|
835 QTest::newRow("null") |
|
836 << (qreal)0.0f << (qreal)0.0f |
|
837 << (qreal)0.0f << (qreal)0.0f |
|
838 << (qreal)0.0f << (qreal)0.0f; |
|
839 |
|
840 QTest::newRow("xonly") |
|
841 << (qreal)1.0f << (qreal)0.0f |
|
842 << (qreal)2.0f << (qreal)0.0f |
|
843 << (qreal)3.0f << (qreal)0.0f; |
|
844 |
|
845 QTest::newRow("yonly") |
|
846 << (qreal)0.0f << (qreal)1.0f |
|
847 << (qreal)0.0f << (qreal)2.0f |
|
848 << (qreal)0.0f << (qreal)3.0f; |
|
849 |
|
850 QTest::newRow("all") |
|
851 << (qreal)1.0f << (qreal)2.0f |
|
852 << (qreal)4.0f << (qreal)5.0f |
|
853 << (qreal)5.0f << (qreal)7.0f; |
|
854 } |
|
855 void tst_QVector::add2() |
|
856 { |
|
857 QFETCH(qreal, x1); |
|
858 QFETCH(qreal, y1); |
|
859 QFETCH(qreal, x2); |
|
860 QFETCH(qreal, y2); |
|
861 QFETCH(qreal, x3); |
|
862 QFETCH(qreal, y3); |
|
863 |
|
864 QVector2D v1(x1, y1); |
|
865 QVector2D v2(x2, y2); |
|
866 QVector2D v3(x3, y3); |
|
867 |
|
868 QVERIFY((v1 + v2) == v3); |
|
869 |
|
870 QVector2D v4(v1); |
|
871 v4 += v2; |
|
872 QVERIFY(v4 == v3); |
|
873 |
|
874 QCOMPARE(v4.x(), v1.x() + v2.x()); |
|
875 QCOMPARE(v4.y(), v1.y() + v2.y()); |
|
876 } |
|
877 |
|
878 // Test vector addition for 3D vectors. |
|
879 void tst_QVector::add3_data() |
|
880 { |
|
881 QTest::addColumn<qreal>("x1"); |
|
882 QTest::addColumn<qreal>("y1"); |
|
883 QTest::addColumn<qreal>("z1"); |
|
884 QTest::addColumn<qreal>("x2"); |
|
885 QTest::addColumn<qreal>("y2"); |
|
886 QTest::addColumn<qreal>("z2"); |
|
887 QTest::addColumn<qreal>("x3"); |
|
888 QTest::addColumn<qreal>("y3"); |
|
889 QTest::addColumn<qreal>("z3"); |
|
890 |
|
891 QTest::newRow("null") |
|
892 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
893 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
894 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
895 |
|
896 QTest::newRow("xonly") |
|
897 << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
898 << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f |
|
899 << (qreal)3.0f << (qreal)0.0f << (qreal)0.0f; |
|
900 |
|
901 QTest::newRow("yonly") |
|
902 << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f |
|
903 << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f |
|
904 << (qreal)0.0f << (qreal)3.0f << (qreal)0.0f; |
|
905 |
|
906 QTest::newRow("zonly") |
|
907 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
908 << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f |
|
909 << (qreal)0.0f << (qreal)0.0f << (qreal)3.0f; |
|
910 |
|
911 QTest::newRow("all") |
|
912 << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f |
|
913 << (qreal)4.0f << (qreal)5.0f << (qreal)-6.0f |
|
914 << (qreal)5.0f << (qreal)7.0f << (qreal)-3.0f; |
|
915 } |
|
916 void tst_QVector::add3() |
|
917 { |
|
918 QFETCH(qreal, x1); |
|
919 QFETCH(qreal, y1); |
|
920 QFETCH(qreal, z1); |
|
921 QFETCH(qreal, x2); |
|
922 QFETCH(qreal, y2); |
|
923 QFETCH(qreal, z2); |
|
924 QFETCH(qreal, x3); |
|
925 QFETCH(qreal, y3); |
|
926 QFETCH(qreal, z3); |
|
927 |
|
928 QVector3D v1(x1, y1, z1); |
|
929 QVector3D v2(x2, y2, z2); |
|
930 QVector3D v3(x3, y3, z3); |
|
931 |
|
932 QVERIFY((v1 + v2) == v3); |
|
933 |
|
934 QVector3D v4(v1); |
|
935 v4 += v2; |
|
936 QVERIFY(v4 == v3); |
|
937 |
|
938 QCOMPARE(v4.x(), v1.x() + v2.x()); |
|
939 QCOMPARE(v4.y(), v1.y() + v2.y()); |
|
940 QCOMPARE(v4.z(), v1.z() + v2.z()); |
|
941 } |
|
942 |
|
943 // Test vector addition for 4D vectors. |
|
944 void tst_QVector::add4_data() |
|
945 { |
|
946 QTest::addColumn<qreal>("x1"); |
|
947 QTest::addColumn<qreal>("y1"); |
|
948 QTest::addColumn<qreal>("z1"); |
|
949 QTest::addColumn<qreal>("w1"); |
|
950 QTest::addColumn<qreal>("x2"); |
|
951 QTest::addColumn<qreal>("y2"); |
|
952 QTest::addColumn<qreal>("z2"); |
|
953 QTest::addColumn<qreal>("w2"); |
|
954 QTest::addColumn<qreal>("x3"); |
|
955 QTest::addColumn<qreal>("y3"); |
|
956 QTest::addColumn<qreal>("z3"); |
|
957 QTest::addColumn<qreal>("w3"); |
|
958 |
|
959 QTest::newRow("null") |
|
960 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
961 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
962 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
963 |
|
964 QTest::newRow("xonly") |
|
965 << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
966 << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
967 << (qreal)3.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
968 |
|
969 QTest::newRow("yonly") |
|
970 << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
971 << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f |
|
972 << (qreal)0.0f << (qreal)3.0f << (qreal)0.0f << (qreal)0.0f; |
|
973 |
|
974 QTest::newRow("zonly") |
|
975 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f |
|
976 << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f |
|
977 << (qreal)0.0f << (qreal)0.0f << (qreal)3.0f << (qreal)0.0f; |
|
978 |
|
979 QTest::newRow("wonly") |
|
980 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
981 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f |
|
982 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)3.0f; |
|
983 |
|
984 QTest::newRow("all") |
|
985 << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f << (qreal)8.0f |
|
986 << (qreal)4.0f << (qreal)5.0f << (qreal)-6.0f << (qreal)9.0f |
|
987 << (qreal)5.0f << (qreal)7.0f << (qreal)-3.0f << (qreal)17.0f; |
|
988 } |
|
989 void tst_QVector::add4() |
|
990 { |
|
991 QFETCH(qreal, x1); |
|
992 QFETCH(qreal, y1); |
|
993 QFETCH(qreal, z1); |
|
994 QFETCH(qreal, w1); |
|
995 QFETCH(qreal, x2); |
|
996 QFETCH(qreal, y2); |
|
997 QFETCH(qreal, z2); |
|
998 QFETCH(qreal, w2); |
|
999 QFETCH(qreal, x3); |
|
1000 QFETCH(qreal, y3); |
|
1001 QFETCH(qreal, z3); |
|
1002 QFETCH(qreal, w3); |
|
1003 |
|
1004 QVector4D v1(x1, y1, z1, w1); |
|
1005 QVector4D v2(x2, y2, z2, w2); |
|
1006 QVector4D v3(x3, y3, z3, w3); |
|
1007 |
|
1008 QVERIFY((v1 + v2) == v3); |
|
1009 |
|
1010 QVector4D v4(v1); |
|
1011 v4 += v2; |
|
1012 QVERIFY(v4 == v3); |
|
1013 |
|
1014 QCOMPARE(v4.x(), v1.x() + v2.x()); |
|
1015 QCOMPARE(v4.y(), v1.y() + v2.y()); |
|
1016 QCOMPARE(v4.z(), v1.z() + v2.z()); |
|
1017 QCOMPARE(v4.w(), v1.w() + v2.w()); |
|
1018 } |
|
1019 |
|
1020 // Test vector subtraction for 2D vectors. |
|
1021 void tst_QVector::subtract2_data() |
|
1022 { |
|
1023 // Use the same test data as the add test. |
|
1024 add2_data(); |
|
1025 } |
|
1026 void tst_QVector::subtract2() |
|
1027 { |
|
1028 QFETCH(qreal, x1); |
|
1029 QFETCH(qreal, y1); |
|
1030 QFETCH(qreal, x2); |
|
1031 QFETCH(qreal, y2); |
|
1032 QFETCH(qreal, x3); |
|
1033 QFETCH(qreal, y3); |
|
1034 |
|
1035 QVector2D v1(x1, y1); |
|
1036 QVector2D v2(x2, y2); |
|
1037 QVector2D v3(x3, y3); |
|
1038 |
|
1039 QVERIFY((v3 - v1) == v2); |
|
1040 QVERIFY((v3 - v2) == v1); |
|
1041 |
|
1042 QVector2D v4(v3); |
|
1043 v4 -= v1; |
|
1044 QVERIFY(v4 == v2); |
|
1045 |
|
1046 QCOMPARE(v4.x(), v3.x() - v1.x()); |
|
1047 QCOMPARE(v4.y(), v3.y() - v1.y()); |
|
1048 |
|
1049 QVector2D v5(v3); |
|
1050 v5 -= v2; |
|
1051 QVERIFY(v5 == v1); |
|
1052 |
|
1053 QCOMPARE(v5.x(), v3.x() - v2.x()); |
|
1054 QCOMPARE(v5.y(), v3.y() - v2.y()); |
|
1055 } |
|
1056 |
|
1057 // Test vector subtraction for 3D vectors. |
|
1058 void tst_QVector::subtract3_data() |
|
1059 { |
|
1060 // Use the same test data as the add test. |
|
1061 add3_data(); |
|
1062 } |
|
1063 void tst_QVector::subtract3() |
|
1064 { |
|
1065 QFETCH(qreal, x1); |
|
1066 QFETCH(qreal, y1); |
|
1067 QFETCH(qreal, z1); |
|
1068 QFETCH(qreal, x2); |
|
1069 QFETCH(qreal, y2); |
|
1070 QFETCH(qreal, z2); |
|
1071 QFETCH(qreal, x3); |
|
1072 QFETCH(qreal, y3); |
|
1073 QFETCH(qreal, z3); |
|
1074 |
|
1075 QVector3D v1(x1, y1, z1); |
|
1076 QVector3D v2(x2, y2, z2); |
|
1077 QVector3D v3(x3, y3, z3); |
|
1078 |
|
1079 QVERIFY((v3 - v1) == v2); |
|
1080 QVERIFY((v3 - v2) == v1); |
|
1081 |
|
1082 QVector3D v4(v3); |
|
1083 v4 -= v1; |
|
1084 QVERIFY(v4 == v2); |
|
1085 |
|
1086 QCOMPARE(v4.x(), v3.x() - v1.x()); |
|
1087 QCOMPARE(v4.y(), v3.y() - v1.y()); |
|
1088 QCOMPARE(v4.z(), v3.z() - v1.z()); |
|
1089 |
|
1090 QVector3D v5(v3); |
|
1091 v5 -= v2; |
|
1092 QVERIFY(v5 == v1); |
|
1093 |
|
1094 QCOMPARE(v5.x(), v3.x() - v2.x()); |
|
1095 QCOMPARE(v5.y(), v3.y() - v2.y()); |
|
1096 QCOMPARE(v5.z(), v3.z() - v2.z()); |
|
1097 } |
|
1098 |
|
1099 // Test vector subtraction for 4D vectors. |
|
1100 void tst_QVector::subtract4_data() |
|
1101 { |
|
1102 // Use the same test data as the add test. |
|
1103 add4_data(); |
|
1104 } |
|
1105 void tst_QVector::subtract4() |
|
1106 { |
|
1107 QFETCH(qreal, x1); |
|
1108 QFETCH(qreal, y1); |
|
1109 QFETCH(qreal, z1); |
|
1110 QFETCH(qreal, w1); |
|
1111 QFETCH(qreal, x2); |
|
1112 QFETCH(qreal, y2); |
|
1113 QFETCH(qreal, z2); |
|
1114 QFETCH(qreal, w2); |
|
1115 QFETCH(qreal, x3); |
|
1116 QFETCH(qreal, y3); |
|
1117 QFETCH(qreal, z3); |
|
1118 QFETCH(qreal, w3); |
|
1119 |
|
1120 QVector4D v1(x1, y1, z1, w1); |
|
1121 QVector4D v2(x2, y2, z2, w2); |
|
1122 QVector4D v3(x3, y3, z3, w3); |
|
1123 |
|
1124 QVERIFY((v3 - v1) == v2); |
|
1125 QVERIFY((v3 - v2) == v1); |
|
1126 |
|
1127 QVector4D v4(v3); |
|
1128 v4 -= v1; |
|
1129 QVERIFY(v4 == v2); |
|
1130 |
|
1131 QCOMPARE(v4.x(), v3.x() - v1.x()); |
|
1132 QCOMPARE(v4.y(), v3.y() - v1.y()); |
|
1133 QCOMPARE(v4.z(), v3.z() - v1.z()); |
|
1134 QCOMPARE(v4.w(), v3.w() - v1.w()); |
|
1135 |
|
1136 QVector4D v5(v3); |
|
1137 v5 -= v2; |
|
1138 QVERIFY(v5 == v1); |
|
1139 |
|
1140 QCOMPARE(v5.x(), v3.x() - v2.x()); |
|
1141 QCOMPARE(v5.y(), v3.y() - v2.y()); |
|
1142 QCOMPARE(v5.z(), v3.z() - v2.z()); |
|
1143 QCOMPARE(v5.w(), v3.w() - v2.w()); |
|
1144 } |
|
1145 |
|
1146 // Test component-wise vector multiplication for 2D vectors. |
|
1147 void tst_QVector::multiply2_data() |
|
1148 { |
|
1149 QTest::addColumn<qreal>("x1"); |
|
1150 QTest::addColumn<qreal>("y1"); |
|
1151 QTest::addColumn<qreal>("x2"); |
|
1152 QTest::addColumn<qreal>("y2"); |
|
1153 QTest::addColumn<qreal>("x3"); |
|
1154 QTest::addColumn<qreal>("y3"); |
|
1155 |
|
1156 QTest::newRow("null") |
|
1157 << (qreal)0.0f << (qreal)0.0f |
|
1158 << (qreal)0.0f << (qreal)0.0f |
|
1159 << (qreal)0.0f << (qreal)0.0f; |
|
1160 |
|
1161 QTest::newRow("xonly") |
|
1162 << (qreal)1.0f << (qreal)0.0f |
|
1163 << (qreal)2.0f << (qreal)0.0f |
|
1164 << (qreal)2.0f << (qreal)0.0f; |
|
1165 |
|
1166 QTest::newRow("yonly") |
|
1167 << (qreal)0.0f << (qreal)1.0f |
|
1168 << (qreal)0.0f << (qreal)2.0f |
|
1169 << (qreal)0.0f << (qreal)2.0f; |
|
1170 |
|
1171 QTest::newRow("all") |
|
1172 << (qreal)1.0f << (qreal)2.0f |
|
1173 << (qreal)4.0f << (qreal)5.0f |
|
1174 << (qreal)4.0f << (qreal)10.0f; |
|
1175 } |
|
1176 void tst_QVector::multiply2() |
|
1177 { |
|
1178 QFETCH(qreal, x1); |
|
1179 QFETCH(qreal, y1); |
|
1180 QFETCH(qreal, x2); |
|
1181 QFETCH(qreal, y2); |
|
1182 QFETCH(qreal, x3); |
|
1183 QFETCH(qreal, y3); |
|
1184 |
|
1185 QVector2D v1(x1, y1); |
|
1186 QVector2D v2(x2, y2); |
|
1187 QVector2D v3(x3, y3); |
|
1188 |
|
1189 QVERIFY((v1 * v2) == v3); |
|
1190 |
|
1191 QVector2D v4(v1); |
|
1192 v4 *= v2; |
|
1193 QVERIFY(v4 == v3); |
|
1194 |
|
1195 QCOMPARE(v4.x(), v1.x() * v2.x()); |
|
1196 QCOMPARE(v4.y(), v1.y() * v2.y()); |
|
1197 } |
|
1198 |
|
1199 // Test component-wise vector multiplication for 3D vectors. |
|
1200 void tst_QVector::multiply3_data() |
|
1201 { |
|
1202 QTest::addColumn<qreal>("x1"); |
|
1203 QTest::addColumn<qreal>("y1"); |
|
1204 QTest::addColumn<qreal>("z1"); |
|
1205 QTest::addColumn<qreal>("x2"); |
|
1206 QTest::addColumn<qreal>("y2"); |
|
1207 QTest::addColumn<qreal>("z2"); |
|
1208 QTest::addColumn<qreal>("x3"); |
|
1209 QTest::addColumn<qreal>("y3"); |
|
1210 QTest::addColumn<qreal>("z3"); |
|
1211 |
|
1212 QTest::newRow("null") |
|
1213 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1214 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1215 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
1216 |
|
1217 QTest::newRow("xonly") |
|
1218 << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
1219 << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f |
|
1220 << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f; |
|
1221 |
|
1222 QTest::newRow("yonly") |
|
1223 << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f |
|
1224 << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f |
|
1225 << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f; |
|
1226 |
|
1227 QTest::newRow("zonly") |
|
1228 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
1229 << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f |
|
1230 << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f; |
|
1231 |
|
1232 QTest::newRow("all") |
|
1233 << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f |
|
1234 << (qreal)4.0f << (qreal)5.0f << (qreal)-6.0f |
|
1235 << (qreal)4.0f << (qreal)10.0f << (qreal)-18.0f; |
|
1236 } |
|
1237 void tst_QVector::multiply3() |
|
1238 { |
|
1239 QFETCH(qreal, x1); |
|
1240 QFETCH(qreal, y1); |
|
1241 QFETCH(qreal, z1); |
|
1242 QFETCH(qreal, x2); |
|
1243 QFETCH(qreal, y2); |
|
1244 QFETCH(qreal, z2); |
|
1245 QFETCH(qreal, x3); |
|
1246 QFETCH(qreal, y3); |
|
1247 QFETCH(qreal, z3); |
|
1248 |
|
1249 QVector3D v1(x1, y1, z1); |
|
1250 QVector3D v2(x2, y2, z2); |
|
1251 QVector3D v3(x3, y3, z3); |
|
1252 |
|
1253 QVERIFY((v1 * v2) == v3); |
|
1254 |
|
1255 QVector3D v4(v1); |
|
1256 v4 *= v2; |
|
1257 QVERIFY(v4 == v3); |
|
1258 |
|
1259 QCOMPARE(v4.x(), v1.x() * v2.x()); |
|
1260 QCOMPARE(v4.y(), v1.y() * v2.y()); |
|
1261 QCOMPARE(v4.z(), v1.z() * v2.z()); |
|
1262 } |
|
1263 |
|
1264 // Test component-wise vector multiplication for 4D vectors. |
|
1265 void tst_QVector::multiply4_data() |
|
1266 { |
|
1267 QTest::addColumn<qreal>("x1"); |
|
1268 QTest::addColumn<qreal>("y1"); |
|
1269 QTest::addColumn<qreal>("z1"); |
|
1270 QTest::addColumn<qreal>("w1"); |
|
1271 QTest::addColumn<qreal>("x2"); |
|
1272 QTest::addColumn<qreal>("y2"); |
|
1273 QTest::addColumn<qreal>("z2"); |
|
1274 QTest::addColumn<qreal>("w2"); |
|
1275 QTest::addColumn<qreal>("x3"); |
|
1276 QTest::addColumn<qreal>("y3"); |
|
1277 QTest::addColumn<qreal>("z3"); |
|
1278 QTest::addColumn<qreal>("w3"); |
|
1279 |
|
1280 QTest::newRow("null") |
|
1281 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1282 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1283 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
1284 |
|
1285 QTest::newRow("xonly") |
|
1286 << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1287 << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1288 << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
1289 |
|
1290 QTest::newRow("yonly") |
|
1291 << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
1292 << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f |
|
1293 << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f; |
|
1294 |
|
1295 QTest::newRow("zonly") |
|
1296 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f |
|
1297 << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f |
|
1298 << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f; |
|
1299 |
|
1300 QTest::newRow("wonly") |
|
1301 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
1302 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f |
|
1303 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f; |
|
1304 |
|
1305 QTest::newRow("all") |
|
1306 << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f << (qreal)8.0f |
|
1307 << (qreal)4.0f << (qreal)5.0f << (qreal)-6.0f << (qreal)9.0f |
|
1308 << (qreal)4.0f << (qreal)10.0f << (qreal)-18.0f << (qreal)72.0f; |
|
1309 } |
|
1310 void tst_QVector::multiply4() |
|
1311 { |
|
1312 QFETCH(qreal, x1); |
|
1313 QFETCH(qreal, y1); |
|
1314 QFETCH(qreal, z1); |
|
1315 QFETCH(qreal, w1); |
|
1316 QFETCH(qreal, x2); |
|
1317 QFETCH(qreal, y2); |
|
1318 QFETCH(qreal, z2); |
|
1319 QFETCH(qreal, w2); |
|
1320 QFETCH(qreal, x3); |
|
1321 QFETCH(qreal, y3); |
|
1322 QFETCH(qreal, z3); |
|
1323 QFETCH(qreal, w3); |
|
1324 |
|
1325 QVector4D v1(x1, y1, z1, w1); |
|
1326 QVector4D v2(x2, y2, z2, w2); |
|
1327 QVector4D v3(x3, y3, z3, w3); |
|
1328 |
|
1329 QVERIFY((v1 * v2) == v3); |
|
1330 |
|
1331 QVector4D v4(v1); |
|
1332 v4 *= v2; |
|
1333 QVERIFY(v4 == v3); |
|
1334 |
|
1335 QCOMPARE(v4.x(), v1.x() * v2.x()); |
|
1336 QCOMPARE(v4.y(), v1.y() * v2.y()); |
|
1337 QCOMPARE(v4.z(), v1.z() * v2.z()); |
|
1338 QCOMPARE(v4.w(), v1.w() * v2.w()); |
|
1339 } |
|
1340 |
|
1341 // Test vector multiplication by a factor for 2D vectors. |
|
1342 void tst_QVector::multiplyFactor2_data() |
|
1343 { |
|
1344 QTest::addColumn<qreal>("x1"); |
|
1345 QTest::addColumn<qreal>("y1"); |
|
1346 QTest::addColumn<qreal>("factor"); |
|
1347 QTest::addColumn<qreal>("x2"); |
|
1348 QTest::addColumn<qreal>("y2"); |
|
1349 |
|
1350 QTest::newRow("null") |
|
1351 << (qreal)0.0f << (qreal)0.0f |
|
1352 << (qreal)100.0f |
|
1353 << (qreal)0.0f << (qreal)0.0f; |
|
1354 |
|
1355 QTest::newRow("xonly") |
|
1356 << (qreal)1.0f << (qreal)0.0f |
|
1357 << (qreal)2.0f |
|
1358 << (qreal)2.0f << (qreal)0.0f; |
|
1359 |
|
1360 QTest::newRow("yonly") |
|
1361 << (qreal)0.0f << (qreal)1.0f |
|
1362 << (qreal)2.0f |
|
1363 << (qreal)0.0f << (qreal)2.0f; |
|
1364 |
|
1365 QTest::newRow("all") |
|
1366 << (qreal)1.0f << (qreal)2.0f |
|
1367 << (qreal)2.0f |
|
1368 << (qreal)2.0f << (qreal)4.0f; |
|
1369 |
|
1370 QTest::newRow("allzero") |
|
1371 << (qreal)1.0f << (qreal)2.0f |
|
1372 << (qreal)0.0f |
|
1373 << (qreal)0.0f << (qreal)0.0f; |
|
1374 } |
|
1375 void tst_QVector::multiplyFactor2() |
|
1376 { |
|
1377 QFETCH(qreal, x1); |
|
1378 QFETCH(qreal, y1); |
|
1379 QFETCH(qreal, factor); |
|
1380 QFETCH(qreal, x2); |
|
1381 QFETCH(qreal, y2); |
|
1382 |
|
1383 QVector2D v1(x1, y1); |
|
1384 QVector2D v2(x2, y2); |
|
1385 |
|
1386 QVERIFY((v1 * factor) == v2); |
|
1387 QVERIFY((factor * v1) == v2); |
|
1388 |
|
1389 QVector2D v3(v1); |
|
1390 v3 *= factor; |
|
1391 QVERIFY(v3 == v2); |
|
1392 |
|
1393 QCOMPARE(v3.x(), v1.x() * factor); |
|
1394 QCOMPARE(v3.y(), v1.y() * factor); |
|
1395 } |
|
1396 |
|
1397 // Test vector multiplication by a factor for 3D vectors. |
|
1398 void tst_QVector::multiplyFactor3_data() |
|
1399 { |
|
1400 QTest::addColumn<qreal>("x1"); |
|
1401 QTest::addColumn<qreal>("y1"); |
|
1402 QTest::addColumn<qreal>("z1"); |
|
1403 QTest::addColumn<qreal>("factor"); |
|
1404 QTest::addColumn<qreal>("x2"); |
|
1405 QTest::addColumn<qreal>("y2"); |
|
1406 QTest::addColumn<qreal>("z2"); |
|
1407 |
|
1408 QTest::newRow("null") |
|
1409 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1410 << (qreal)100.0f |
|
1411 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
1412 |
|
1413 QTest::newRow("xonly") |
|
1414 << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
1415 << (qreal)2.0f |
|
1416 << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f; |
|
1417 |
|
1418 QTest::newRow("yonly") |
|
1419 << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f |
|
1420 << (qreal)2.0f |
|
1421 << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f; |
|
1422 |
|
1423 QTest::newRow("zonly") |
|
1424 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
1425 << (qreal)2.0f |
|
1426 << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f; |
|
1427 |
|
1428 QTest::newRow("all") |
|
1429 << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f |
|
1430 << (qreal)2.0f |
|
1431 << (qreal)2.0f << (qreal)4.0f << (qreal)-6.0f; |
|
1432 |
|
1433 QTest::newRow("allzero") |
|
1434 << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f |
|
1435 << (qreal)0.0f |
|
1436 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
1437 } |
|
1438 void tst_QVector::multiplyFactor3() |
|
1439 { |
|
1440 QFETCH(qreal, x1); |
|
1441 QFETCH(qreal, y1); |
|
1442 QFETCH(qreal, z1); |
|
1443 QFETCH(qreal, factor); |
|
1444 QFETCH(qreal, x2); |
|
1445 QFETCH(qreal, y2); |
|
1446 QFETCH(qreal, z2); |
|
1447 |
|
1448 QVector3D v1(x1, y1, z1); |
|
1449 QVector3D v2(x2, y2, z2); |
|
1450 |
|
1451 QVERIFY((v1 * factor) == v2); |
|
1452 QVERIFY((factor * v1) == v2); |
|
1453 |
|
1454 QVector3D v3(v1); |
|
1455 v3 *= factor; |
|
1456 QVERIFY(v3 == v2); |
|
1457 |
|
1458 QCOMPARE(v3.x(), v1.x() * factor); |
|
1459 QCOMPARE(v3.y(), v1.y() * factor); |
|
1460 QCOMPARE(v3.z(), v1.z() * factor); |
|
1461 } |
|
1462 |
|
1463 // Test vector multiplication by a factor for 4D vectors. |
|
1464 void tst_QVector::multiplyFactor4_data() |
|
1465 { |
|
1466 QTest::addColumn<qreal>("x1"); |
|
1467 QTest::addColumn<qreal>("y1"); |
|
1468 QTest::addColumn<qreal>("z1"); |
|
1469 QTest::addColumn<qreal>("w1"); |
|
1470 QTest::addColumn<qreal>("factor"); |
|
1471 QTest::addColumn<qreal>("x2"); |
|
1472 QTest::addColumn<qreal>("y2"); |
|
1473 QTest::addColumn<qreal>("z2"); |
|
1474 QTest::addColumn<qreal>("w2"); |
|
1475 |
|
1476 QTest::newRow("null") |
|
1477 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1478 << (qreal)100.0f |
|
1479 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
1480 |
|
1481 QTest::newRow("xonly") |
|
1482 << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1483 << (qreal)2.0f |
|
1484 << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
1485 |
|
1486 QTest::newRow("yonly") |
|
1487 << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
1488 << (qreal)2.0f |
|
1489 << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f; |
|
1490 |
|
1491 QTest::newRow("zonly") |
|
1492 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f |
|
1493 << (qreal)2.0f |
|
1494 << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f; |
|
1495 |
|
1496 QTest::newRow("wonly") |
|
1497 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
1498 << (qreal)2.0f |
|
1499 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f; |
|
1500 |
|
1501 QTest::newRow("all") |
|
1502 << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)4.0f |
|
1503 << (qreal)2.0f |
|
1504 << (qreal)2.0f << (qreal)4.0f << (qreal)-6.0f << (qreal)8.0f; |
|
1505 |
|
1506 QTest::newRow("allzero") |
|
1507 << (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)4.0f |
|
1508 << (qreal)0.0f |
|
1509 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f; |
|
1510 } |
|
1511 void tst_QVector::multiplyFactor4() |
|
1512 { |
|
1513 QFETCH(qreal, x1); |
|
1514 QFETCH(qreal, y1); |
|
1515 QFETCH(qreal, z1); |
|
1516 QFETCH(qreal, w1); |
|
1517 QFETCH(qreal, factor); |
|
1518 QFETCH(qreal, x2); |
|
1519 QFETCH(qreal, y2); |
|
1520 QFETCH(qreal, z2); |
|
1521 QFETCH(qreal, w2); |
|
1522 |
|
1523 QVector4D v1(x1, y1, z1, w1); |
|
1524 QVector4D v2(x2, y2, z2, w2); |
|
1525 |
|
1526 QVERIFY((v1 * factor) == v2); |
|
1527 QVERIFY((factor * v1) == v2); |
|
1528 |
|
1529 QVector4D v3(v1); |
|
1530 v3 *= factor; |
|
1531 QVERIFY(v3 == v2); |
|
1532 |
|
1533 QCOMPARE(v3.x(), v1.x() * factor); |
|
1534 QCOMPARE(v3.y(), v1.y() * factor); |
|
1535 QCOMPARE(v3.z(), v1.z() * factor); |
|
1536 QCOMPARE(v3.w(), v1.w() * factor); |
|
1537 } |
|
1538 |
|
1539 // Test vector division by a factor for 2D vectors. |
|
1540 void tst_QVector::divide2_data() |
|
1541 { |
|
1542 // Use the same test data as the multiply test. |
|
1543 multiplyFactor2_data(); |
|
1544 } |
|
1545 void tst_QVector::divide2() |
|
1546 { |
|
1547 QFETCH(qreal, x1); |
|
1548 QFETCH(qreal, y1); |
|
1549 QFETCH(qreal, factor); |
|
1550 QFETCH(qreal, x2); |
|
1551 QFETCH(qreal, y2); |
|
1552 |
|
1553 QVector2D v1(x1, y1); |
|
1554 QVector2D v2(x2, y2); |
|
1555 |
|
1556 if (factor == (qreal)0.0f) |
|
1557 return; |
|
1558 |
|
1559 QVERIFY((v2 / factor) == v1); |
|
1560 |
|
1561 QVector2D v3(v2); |
|
1562 v3 /= factor; |
|
1563 QVERIFY(v3 == v1); |
|
1564 |
|
1565 QCOMPARE(v3.x(), v2.x() / factor); |
|
1566 QCOMPARE(v3.y(), v2.y() / factor); |
|
1567 } |
|
1568 |
|
1569 // Test vector division by a factor for 3D vectors. |
|
1570 void tst_QVector::divide3_data() |
|
1571 { |
|
1572 // Use the same test data as the multiply test. |
|
1573 multiplyFactor3_data(); |
|
1574 } |
|
1575 void tst_QVector::divide3() |
|
1576 { |
|
1577 QFETCH(qreal, x1); |
|
1578 QFETCH(qreal, y1); |
|
1579 QFETCH(qreal, z1); |
|
1580 QFETCH(qreal, factor); |
|
1581 QFETCH(qreal, x2); |
|
1582 QFETCH(qreal, y2); |
|
1583 QFETCH(qreal, z2); |
|
1584 |
|
1585 QVector3D v1(x1, y1, z1); |
|
1586 QVector3D v2(x2, y2, z2); |
|
1587 |
|
1588 if (factor == (qreal)0.0f) |
|
1589 return; |
|
1590 |
|
1591 QVERIFY((v2 / factor) == v1); |
|
1592 |
|
1593 QVector3D v3(v2); |
|
1594 v3 /= factor; |
|
1595 QVERIFY(v3 == v1); |
|
1596 |
|
1597 QCOMPARE(v3.x(), v2.x() / factor); |
|
1598 QCOMPARE(v3.y(), v2.y() / factor); |
|
1599 QCOMPARE(v3.z(), v2.z() / factor); |
|
1600 } |
|
1601 |
|
1602 // Test vector division by a factor for 4D vectors. |
|
1603 void tst_QVector::divide4_data() |
|
1604 { |
|
1605 // Use the same test data as the multiply test. |
|
1606 multiplyFactor4_data(); |
|
1607 } |
|
1608 void tst_QVector::divide4() |
|
1609 { |
|
1610 QFETCH(qreal, x1); |
|
1611 QFETCH(qreal, y1); |
|
1612 QFETCH(qreal, z1); |
|
1613 QFETCH(qreal, w1); |
|
1614 QFETCH(qreal, factor); |
|
1615 QFETCH(qreal, x2); |
|
1616 QFETCH(qreal, y2); |
|
1617 QFETCH(qreal, z2); |
|
1618 QFETCH(qreal, w2); |
|
1619 |
|
1620 QVector4D v1(x1, y1, z1, w1); |
|
1621 QVector4D v2(x2, y2, z2, w2); |
|
1622 |
|
1623 if (factor == (qreal)0.0f) |
|
1624 return; |
|
1625 |
|
1626 QVERIFY((v2 / factor) == v1); |
|
1627 |
|
1628 QVector4D v3(v2); |
|
1629 v3 /= factor; |
|
1630 QVERIFY(v3 == v1); |
|
1631 |
|
1632 QCOMPARE(v3.x(), v2.x() / factor); |
|
1633 QCOMPARE(v3.y(), v2.y() / factor); |
|
1634 QCOMPARE(v3.z(), v2.z() / factor); |
|
1635 QCOMPARE(v3.w(), v2.w() / factor); |
|
1636 } |
|
1637 |
|
1638 // Test vector negation for 2D vectors. |
|
1639 void tst_QVector::negate2_data() |
|
1640 { |
|
1641 // Use the same test data as the add test. |
|
1642 add2_data(); |
|
1643 } |
|
1644 void tst_QVector::negate2() |
|
1645 { |
|
1646 QFETCH(qreal, x1); |
|
1647 QFETCH(qreal, y1); |
|
1648 |
|
1649 QVector2D v1(x1, y1); |
|
1650 QVector2D v2(-x1, -y1); |
|
1651 |
|
1652 QVERIFY(-v1 == v2); |
|
1653 } |
|
1654 |
|
1655 // Test vector negation for 3D vectors. |
|
1656 void tst_QVector::negate3_data() |
|
1657 { |
|
1658 // Use the same test data as the add test. |
|
1659 add3_data(); |
|
1660 } |
|
1661 void tst_QVector::negate3() |
|
1662 { |
|
1663 QFETCH(qreal, x1); |
|
1664 QFETCH(qreal, y1); |
|
1665 QFETCH(qreal, z1); |
|
1666 |
|
1667 QVector3D v1(x1, y1, z1); |
|
1668 QVector3D v2(-x1, -y1, -z1); |
|
1669 |
|
1670 QVERIFY(-v1 == v2); |
|
1671 } |
|
1672 |
|
1673 // Test vector negation for 4D vectors. |
|
1674 void tst_QVector::negate4_data() |
|
1675 { |
|
1676 // Use the same test data as the add test. |
|
1677 add4_data(); |
|
1678 } |
|
1679 void tst_QVector::negate4() |
|
1680 { |
|
1681 QFETCH(qreal, x1); |
|
1682 QFETCH(qreal, y1); |
|
1683 QFETCH(qreal, z1); |
|
1684 QFETCH(qreal, w1); |
|
1685 |
|
1686 QVector4D v1(x1, y1, z1, w1); |
|
1687 QVector4D v2(-x1, -y1, -z1, -w1); |
|
1688 |
|
1689 QVERIFY(-v1 == v2); |
|
1690 } |
|
1691 |
|
1692 // Test the computation of vector cross-products. |
|
1693 void tst_QVector::crossProduct_data() |
|
1694 { |
|
1695 QTest::addColumn<qreal>("x1"); |
|
1696 QTest::addColumn<qreal>("y1"); |
|
1697 QTest::addColumn<qreal>("z1"); |
|
1698 QTest::addColumn<qreal>("x2"); |
|
1699 QTest::addColumn<qreal>("y2"); |
|
1700 QTest::addColumn<qreal>("z2"); |
|
1701 QTest::addColumn<qreal>("x3"); |
|
1702 QTest::addColumn<qreal>("y3"); |
|
1703 QTest::addColumn<qreal>("z3"); |
|
1704 QTest::addColumn<qreal>("dot"); |
|
1705 |
|
1706 QTest::newRow("null") |
|
1707 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1708 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1709 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1710 << (qreal)0.0f; |
|
1711 |
|
1712 QTest::newRow("unitvec") |
|
1713 << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
1714 << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f |
|
1715 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
1716 << (qreal)0.0f; |
|
1717 |
|
1718 QTest::newRow("complex") |
|
1719 << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f |
|
1720 << (qreal)4.0f << (qreal)5.0f << (qreal)6.0f |
|
1721 << (qreal)-3.0f << (qreal)6.0f << (qreal)-3.0f |
|
1722 << (qreal)32.0f; |
|
1723 } |
|
1724 void tst_QVector::crossProduct() |
|
1725 { |
|
1726 QFETCH(qreal, x1); |
|
1727 QFETCH(qreal, y1); |
|
1728 QFETCH(qreal, z1); |
|
1729 QFETCH(qreal, x2); |
|
1730 QFETCH(qreal, y2); |
|
1731 QFETCH(qreal, z2); |
|
1732 QFETCH(qreal, x3); |
|
1733 QFETCH(qreal, y3); |
|
1734 QFETCH(qreal, z3); |
|
1735 |
|
1736 QVector3D v1(x1, y1, z1); |
|
1737 QVector3D v2(x2, y2, z2); |
|
1738 QVector3D v3(x3, y3, z3); |
|
1739 |
|
1740 QVector3D v4 = QVector3D::crossProduct(v1, v2); |
|
1741 QVERIFY(v4 == v3); |
|
1742 |
|
1743 // Compute the cross-product long-hand and check again. |
|
1744 qreal xres = y1 * z2 - z1 * y2; |
|
1745 qreal yres = z1 * x2 - x1 * z2; |
|
1746 qreal zres = x1 * y2 - y1 * x2; |
|
1747 |
|
1748 QCOMPARE(v4.x(), xres); |
|
1749 QCOMPARE(v4.y(), yres); |
|
1750 QCOMPARE(v4.z(), zres); |
|
1751 } |
|
1752 |
|
1753 // Test the computation of normals. |
|
1754 void tst_QVector::normal_data() |
|
1755 { |
|
1756 // Use the same test data as the crossProduct test. |
|
1757 crossProduct_data(); |
|
1758 } |
|
1759 void tst_QVector::normal() |
|
1760 { |
|
1761 QFETCH(qreal, x1); |
|
1762 QFETCH(qreal, y1); |
|
1763 QFETCH(qreal, z1); |
|
1764 QFETCH(qreal, x2); |
|
1765 QFETCH(qreal, y2); |
|
1766 QFETCH(qreal, z2); |
|
1767 QFETCH(qreal, x3); |
|
1768 QFETCH(qreal, y3); |
|
1769 QFETCH(qreal, z3); |
|
1770 |
|
1771 QVector3D v1(x1, y1, z1); |
|
1772 QVector3D v2(x2, y2, z2); |
|
1773 QVector3D v3(x3, y3, z3); |
|
1774 |
|
1775 QVERIFY(QVector3D::normal(v1, v2) == v3.normalized()); |
|
1776 QVERIFY(QVector3D::normal(QVector3D(), v1, v2) == v3.normalized()); |
|
1777 |
|
1778 QVector3D point(1.0f, 2.0f, 3.0f); |
|
1779 QVERIFY(QVector3D::normal(point, v1 + point, v2 + point) == v3.normalized()); |
|
1780 } |
|
1781 |
|
1782 // Test distance to plane calculations. |
|
1783 void tst_QVector::distanceToPlane_data() |
|
1784 { |
|
1785 QTest::addColumn<qreal>("x1"); // Point on plane |
|
1786 QTest::addColumn<qreal>("y1"); |
|
1787 QTest::addColumn<qreal>("z1"); |
|
1788 QTest::addColumn<qreal>("x2"); // Normal to plane |
|
1789 QTest::addColumn<qreal>("y2"); |
|
1790 QTest::addColumn<qreal>("z2"); |
|
1791 QTest::addColumn<qreal>("x3"); // Point to test for distance |
|
1792 QTest::addColumn<qreal>("y3"); |
|
1793 QTest::addColumn<qreal>("z3"); |
|
1794 QTest::addColumn<qreal>("x4"); // Second point on plane |
|
1795 QTest::addColumn<qreal>("y4"); |
|
1796 QTest::addColumn<qreal>("z4"); |
|
1797 QTest::addColumn<qreal>("x5"); // Third point on plane |
|
1798 QTest::addColumn<qreal>("y5"); |
|
1799 QTest::addColumn<qreal>("z5"); |
|
1800 QTest::addColumn<qreal>("distance"); |
|
1801 |
|
1802 QTest::newRow("null") |
|
1803 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1804 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
1805 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1806 << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
1807 << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f |
|
1808 << (qreal)0.0f; |
|
1809 |
|
1810 QTest::newRow("above") |
|
1811 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1812 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
1813 << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f |
|
1814 << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
1815 << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f |
|
1816 << (qreal)2.0f; |
|
1817 |
|
1818 QTest::newRow("below") |
|
1819 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1820 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
1821 << (qreal)-1.0f << (qreal)1.0f << (qreal)-2.0f |
|
1822 << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
1823 << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f |
|
1824 << (qreal)-2.0f; |
|
1825 } |
|
1826 void tst_QVector::distanceToPlane() |
|
1827 { |
|
1828 QFETCH(qreal, x1); |
|
1829 QFETCH(qreal, y1); |
|
1830 QFETCH(qreal, z1); |
|
1831 QFETCH(qreal, x2); |
|
1832 QFETCH(qreal, y2); |
|
1833 QFETCH(qreal, z2); |
|
1834 QFETCH(qreal, x3); |
|
1835 QFETCH(qreal, y3); |
|
1836 QFETCH(qreal, z3); |
|
1837 QFETCH(qreal, x4); |
|
1838 QFETCH(qreal, y4); |
|
1839 QFETCH(qreal, z4); |
|
1840 QFETCH(qreal, x5); |
|
1841 QFETCH(qreal, y5); |
|
1842 QFETCH(qreal, z5); |
|
1843 QFETCH(qreal, distance); |
|
1844 |
|
1845 QVector3D v1(x1, y1, z1); |
|
1846 QVector3D v2(x2, y2, z2); |
|
1847 QVector3D v3(x3, y3, z3); |
|
1848 QVector3D v4(x4, y4, z4); |
|
1849 QVector3D v5(x5, y5, z5); |
|
1850 |
|
1851 QCOMPARE(v3.distanceToPlane(v1, v2), distance); |
|
1852 QCOMPARE(v3.distanceToPlane(v1, v4, v5), distance); |
|
1853 } |
|
1854 |
|
1855 // Test distance to line calculations. |
|
1856 void tst_QVector::distanceToLine_data() |
|
1857 { |
|
1858 QTest::addColumn<qreal>("x1"); // Point on line |
|
1859 QTest::addColumn<qreal>("y1"); |
|
1860 QTest::addColumn<qreal>("z1"); |
|
1861 QTest::addColumn<qreal>("x2"); // Direction of the line |
|
1862 QTest::addColumn<qreal>("y2"); |
|
1863 QTest::addColumn<qreal>("z2"); |
|
1864 QTest::addColumn<qreal>("x3"); // Point to test for distance |
|
1865 QTest::addColumn<qreal>("y3"); |
|
1866 QTest::addColumn<qreal>("z3"); |
|
1867 QTest::addColumn<qreal>("distance"); |
|
1868 |
|
1869 QTest::newRow("null") |
|
1870 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1871 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
1872 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1873 << (qreal)0.0f; |
|
1874 |
|
1875 QTest::newRow("on line") |
|
1876 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1877 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
1878 << (qreal)0.0f << (qreal)0.0f << (qreal)5.0f |
|
1879 << (qreal)0.0f; |
|
1880 |
|
1881 QTest::newRow("off line") |
|
1882 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1883 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
1884 << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
1885 << (qreal)1.0f; |
|
1886 |
|
1887 QTest::newRow("off line 2") |
|
1888 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1889 << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f |
|
1890 << (qreal)0.0f << (qreal)-2.0f << (qreal)0.0f |
|
1891 << (qreal)2.0f; |
|
1892 |
|
1893 QTest::newRow("points") |
|
1894 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1895 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
1896 << (qreal)0.0f << (qreal)5.0f << (qreal)0.0f |
|
1897 << (qreal)5.0f; |
|
1898 } |
|
1899 void tst_QVector::distanceToLine() |
|
1900 { |
|
1901 QFETCH(qreal, x1); |
|
1902 QFETCH(qreal, y1); |
|
1903 QFETCH(qreal, z1); |
|
1904 QFETCH(qreal, x2); |
|
1905 QFETCH(qreal, y2); |
|
1906 QFETCH(qreal, z2); |
|
1907 QFETCH(qreal, x3); |
|
1908 QFETCH(qreal, y3); |
|
1909 QFETCH(qreal, z3); |
|
1910 QFETCH(qreal, distance); |
|
1911 |
|
1912 QVector3D v1(x1, y1, z1); |
|
1913 QVector3D v2(x2, y2, z2); |
|
1914 QVector3D v3(x3, y3, z3); |
|
1915 |
|
1916 QCOMPARE(v3.distanceToLine(v1, v2), distance); |
|
1917 } |
|
1918 |
|
1919 // Test the computation of dot products for 2D vectors. |
|
1920 void tst_QVector::dotProduct2_data() |
|
1921 { |
|
1922 QTest::addColumn<qreal>("x1"); |
|
1923 QTest::addColumn<qreal>("y1"); |
|
1924 QTest::addColumn<qreal>("x2"); |
|
1925 QTest::addColumn<qreal>("y2"); |
|
1926 QTest::addColumn<qreal>("dot"); |
|
1927 |
|
1928 QTest::newRow("null") |
|
1929 << (qreal)0.0f << (qreal)0.0f |
|
1930 << (qreal)0.0f << (qreal)0.0f |
|
1931 << (qreal)0.0f; |
|
1932 |
|
1933 QTest::newRow("unitvec") |
|
1934 << (qreal)1.0f << (qreal)0.0f |
|
1935 << (qreal)0.0f << (qreal)1.0f |
|
1936 << (qreal)0.0f; |
|
1937 |
|
1938 QTest::newRow("complex") |
|
1939 << (qreal)1.0f << (qreal)2.0f |
|
1940 << (qreal)4.0f << (qreal)5.0f |
|
1941 << (qreal)14.0f; |
|
1942 } |
|
1943 void tst_QVector::dotProduct2() |
|
1944 { |
|
1945 QFETCH(qreal, x1); |
|
1946 QFETCH(qreal, y1); |
|
1947 QFETCH(qreal, x2); |
|
1948 QFETCH(qreal, y2); |
|
1949 QFETCH(qreal, dot); |
|
1950 |
|
1951 QVector2D v1(x1, y1); |
|
1952 QVector2D v2(x2, y2); |
|
1953 |
|
1954 QVERIFY(QVector2D::dotProduct(v1, v2) == dot); |
|
1955 |
|
1956 // Compute the dot-product long-hand and check again. |
|
1957 qreal d = x1 * x2 + y1 * y2; |
|
1958 |
|
1959 QCOMPARE(QVector2D::dotProduct(v1, v2), d); |
|
1960 } |
|
1961 |
|
1962 // Test the computation of dot products for 3D vectors. |
|
1963 void tst_QVector::dotProduct3_data() |
|
1964 { |
|
1965 // Use the same test data as the crossProduct test. |
|
1966 crossProduct_data(); |
|
1967 } |
|
1968 void tst_QVector::dotProduct3() |
|
1969 { |
|
1970 QFETCH(qreal, x1); |
|
1971 QFETCH(qreal, y1); |
|
1972 QFETCH(qreal, z1); |
|
1973 QFETCH(qreal, x2); |
|
1974 QFETCH(qreal, y2); |
|
1975 QFETCH(qreal, z2); |
|
1976 QFETCH(qreal, x3); |
|
1977 QFETCH(qreal, y3); |
|
1978 QFETCH(qreal, z3); |
|
1979 QFETCH(qreal, dot); |
|
1980 |
|
1981 Q_UNUSED(x3); |
|
1982 Q_UNUSED(y3); |
|
1983 Q_UNUSED(z3); |
|
1984 |
|
1985 QVector3D v1(x1, y1, z1); |
|
1986 QVector3D v2(x2, y2, z2); |
|
1987 |
|
1988 QVERIFY(QVector3D::dotProduct(v1, v2) == dot); |
|
1989 |
|
1990 // Compute the dot-product long-hand and check again. |
|
1991 qreal d = x1 * x2 + y1 * y2 + z1 * z2; |
|
1992 |
|
1993 QCOMPARE(QVector3D::dotProduct(v1, v2), d); |
|
1994 } |
|
1995 |
|
1996 // Test the computation of dot products for 4D vectors. |
|
1997 void tst_QVector::dotProduct4_data() |
|
1998 { |
|
1999 QTest::addColumn<qreal>("x1"); |
|
2000 QTest::addColumn<qreal>("y1"); |
|
2001 QTest::addColumn<qreal>("z1"); |
|
2002 QTest::addColumn<qreal>("w1"); |
|
2003 QTest::addColumn<qreal>("x2"); |
|
2004 QTest::addColumn<qreal>("y2"); |
|
2005 QTest::addColumn<qreal>("z2"); |
|
2006 QTest::addColumn<qreal>("w2"); |
|
2007 QTest::addColumn<qreal>("dot"); |
|
2008 |
|
2009 QTest::newRow("null") |
|
2010 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
2011 << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
2012 << (qreal)0.0f; |
|
2013 |
|
2014 QTest::newRow("unitvec") |
|
2015 << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f |
|
2016 << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f |
|
2017 << (qreal)0.0f; |
|
2018 |
|
2019 QTest::newRow("complex") |
|
2020 << (qreal)1.0f << (qreal)2.0f << (qreal)3.0f << (qreal)4.0f |
|
2021 << (qreal)4.0f << (qreal)5.0f << (qreal)6.0f << (qreal)7.0f |
|
2022 << (qreal)60.0f; |
|
2023 } |
|
2024 void tst_QVector::dotProduct4() |
|
2025 { |
|
2026 QFETCH(qreal, x1); |
|
2027 QFETCH(qreal, y1); |
|
2028 QFETCH(qreal, z1); |
|
2029 QFETCH(qreal, w1); |
|
2030 QFETCH(qreal, x2); |
|
2031 QFETCH(qreal, y2); |
|
2032 QFETCH(qreal, z2); |
|
2033 QFETCH(qreal, w2); |
|
2034 QFETCH(qreal, dot); |
|
2035 |
|
2036 QVector4D v1(x1, y1, z1, w1); |
|
2037 QVector4D v2(x2, y2, z2, w2); |
|
2038 |
|
2039 QVERIFY(QVector4D::dotProduct(v1, v2) == dot); |
|
2040 |
|
2041 // Compute the dot-product long-hand and check again. |
|
2042 qreal d = x1 * x2 + y1 * y2 + z1 * z2 + w1 * w2; |
|
2043 |
|
2044 QCOMPARE(QVector4D::dotProduct(v1, v2), d); |
|
2045 } |
|
2046 |
|
2047 class tst_QVectorProperties : public QObject |
|
2048 { |
|
2049 Q_OBJECT |
|
2050 Q_PROPERTY(QVector2D vector2D READ vector2D WRITE setVector2D) |
|
2051 Q_PROPERTY(QVector3D vector3D READ vector3D WRITE setVector3D) |
|
2052 Q_PROPERTY(QVector4D vector4D READ vector4D WRITE setVector4D) |
|
2053 public: |
|
2054 tst_QVectorProperties(QObject *parent = 0) : QObject(parent) {} |
|
2055 |
|
2056 QVector2D vector2D() const { return v2; } |
|
2057 void setVector2D(const QVector2D& value) { v2 = value; } |
|
2058 |
|
2059 QVector3D vector3D() const { return v3; } |
|
2060 void setVector3D(const QVector3D& value) { v3 = value; } |
|
2061 |
|
2062 QVector4D vector4D() const { return v4; } |
|
2063 void setVector4D(const QVector4D& value) { v4 = value; } |
|
2064 |
|
2065 private: |
|
2066 QVector2D v2; |
|
2067 QVector3D v3; |
|
2068 QVector4D v4; |
|
2069 }; |
|
2070 |
|
2071 // Test getting and setting vector properties via the metaobject system. |
|
2072 void tst_QVector::properties() |
|
2073 { |
|
2074 tst_QVectorProperties obj; |
|
2075 |
|
2076 obj.setVector2D(QVector2D(1.0f, 2.0f)); |
|
2077 obj.setVector3D(QVector3D(3.0f, 4.0f, 5.0f)); |
|
2078 obj.setVector4D(QVector4D(6.0f, 7.0f, 8.0f, 9.0f)); |
|
2079 |
|
2080 QVector2D v2 = qVariantValue<QVector2D>(obj.property("vector2D")); |
|
2081 QCOMPARE(v2.x(), (qreal)1.0f); |
|
2082 QCOMPARE(v2.y(), (qreal)2.0f); |
|
2083 |
|
2084 QVector3D v3 = qVariantValue<QVector3D>(obj.property("vector3D")); |
|
2085 QCOMPARE(v3.x(), (qreal)3.0f); |
|
2086 QCOMPARE(v3.y(), (qreal)4.0f); |
|
2087 QCOMPARE(v3.z(), (qreal)5.0f); |
|
2088 |
|
2089 QVector4D v4 = qVariantValue<QVector4D>(obj.property("vector4D")); |
|
2090 QCOMPARE(v4.x(), (qreal)6.0f); |
|
2091 QCOMPARE(v4.y(), (qreal)7.0f); |
|
2092 QCOMPARE(v4.z(), (qreal)8.0f); |
|
2093 QCOMPARE(v4.w(), (qreal)9.0f); |
|
2094 |
|
2095 obj.setProperty("vector2D", |
|
2096 qVariantFromValue(QVector2D(-1.0f, -2.0f))); |
|
2097 obj.setProperty("vector3D", |
|
2098 qVariantFromValue(QVector3D(-3.0f, -4.0f, -5.0f))); |
|
2099 obj.setProperty("vector4D", |
|
2100 qVariantFromValue(QVector4D(-6.0f, -7.0f, -8.0f, -9.0f))); |
|
2101 |
|
2102 v2 = qVariantValue<QVector2D>(obj.property("vector2D")); |
|
2103 QCOMPARE(v2.x(), (qreal)-1.0f); |
|
2104 QCOMPARE(v2.y(), (qreal)-2.0f); |
|
2105 |
|
2106 v3 = qVariantValue<QVector3D>(obj.property("vector3D")); |
|
2107 QCOMPARE(v3.x(), (qreal)-3.0f); |
|
2108 QCOMPARE(v3.y(), (qreal)-4.0f); |
|
2109 QCOMPARE(v3.z(), (qreal)-5.0f); |
|
2110 |
|
2111 v4 = qVariantValue<QVector4D>(obj.property("vector4D")); |
|
2112 QCOMPARE(v4.x(), (qreal)-6.0f); |
|
2113 QCOMPARE(v4.y(), (qreal)-7.0f); |
|
2114 QCOMPARE(v4.z(), (qreal)-8.0f); |
|
2115 QCOMPARE(v4.w(), (qreal)-9.0f); |
|
2116 } |
|
2117 |
|
2118 void tst_QVector::metaTypes() |
|
2119 { |
|
2120 QVERIFY(QMetaType::type("QVector2D") == QMetaType::QVector2D); |
|
2121 QVERIFY(QMetaType::type("QVector3D") == QMetaType::QVector3D); |
|
2122 QVERIFY(QMetaType::type("QVector4D") == QMetaType::QVector4D); |
|
2123 |
|
2124 QCOMPARE(QByteArray(QMetaType::typeName(QMetaType::QVector2D)), |
|
2125 QByteArray("QVector2D")); |
|
2126 QCOMPARE(QByteArray(QMetaType::typeName(QMetaType::QVector3D)), |
|
2127 QByteArray("QVector3D")); |
|
2128 QCOMPARE(QByteArray(QMetaType::typeName(QMetaType::QVector4D)), |
|
2129 QByteArray("QVector4D")); |
|
2130 |
|
2131 QVERIFY(QMetaType::isRegistered(QMetaType::QVector2D)); |
|
2132 QVERIFY(QMetaType::isRegistered(QMetaType::QVector3D)); |
|
2133 QVERIFY(QMetaType::isRegistered(QMetaType::QVector4D)); |
|
2134 |
|
2135 QVERIFY(qMetaTypeId<QVector2D>() == QMetaType::QVector2D); |
|
2136 QVERIFY(qMetaTypeId<QVector3D>() == QMetaType::QVector3D); |
|
2137 QVERIFY(qMetaTypeId<QVector4D>() == QMetaType::QVector4D); |
|
2138 } |
|
2139 |
|
2140 QTEST_APPLESS_MAIN(tst_QVector) |
|
2141 |
|
2142 #include "tst_qvectornd.moc" |