1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QtTest>
30#include <QtCore/qmath.h>
31#include <QtGui/qvector2d.h>
32#include <QtGui/qvector3d.h>
33#include <QtGui/qvector4d.h>
34#include <QtGui/qmatrix4x4.h>
35
36class tst_QVectorND : public QObject
37{
38 Q_OBJECT
39public:
40 tst_QVectorND() {}
41 ~tst_QVectorND() {}
42
43private slots:
44 void create2();
45 void create3();
46 void create4();
47
48 void modify2();
49 void modify3();
50 void modify4();
51
52 void length2_data();
53 void length2();
54 void length3_data();
55 void length3();
56 void length4_data();
57 void length4();
58
59 void normalized2_data();
60 void normalized2();
61 void normalized3_data();
62 void normalized3();
63 void normalized4_data();
64 void normalized4();
65
66 void normalize2_data();
67 void normalize2();
68 void normalize3_data();
69 void normalize3();
70 void normalize4_data();
71 void normalize4();
72
73 void compare2();
74 void compare3();
75 void compare4();
76
77 void add2_data();
78 void add2();
79 void add3_data();
80 void add3();
81 void add4_data();
82 void add4();
83
84 void subtract2_data();
85 void subtract2();
86 void subtract3_data();
87 void subtract3();
88 void subtract4_data();
89 void subtract4();
90
91 void multiply2_data();
92 void multiply2();
93 void multiply3_data();
94 void multiply3();
95 void multiply4_data();
96 void multiply4();
97
98 void multiplyFactor2_data();
99 void multiplyFactor2();
100 void multiplyFactor3_data();
101 void multiplyFactor3();
102 void multiplyFactor4_data();
103 void multiplyFactor4();
104
105 void divide2_data();
106 void divide2();
107 void divide3_data();
108 void divide3();
109 void divide4_data();
110 void divide4();
111
112 void divideFactor2_data();
113 void divideFactor2();
114 void divideFactor3_data();
115 void divideFactor3();
116 void divideFactor4_data();
117 void divideFactor4();
118
119 void negate2_data();
120 void negate2();
121 void negate3_data();
122 void negate3();
123 void negate4_data();
124 void negate4();
125
126 void crossProduct_data();
127 void crossProduct();
128 void normal_data();
129 void normal();
130 void distanceToPoint2_data();
131 void distanceToPoint2();
132 void distanceToPoint3_data();
133 void distanceToPoint3();
134 void distanceToPlane_data();
135 void distanceToPlane();
136 void distanceToLine2_data();
137 void distanceToLine2();
138 void distanceToLine3_data();
139 void distanceToLine3();
140
141 void dotProduct2_data();
142 void dotProduct2();
143 void dotProduct3_data();
144 void dotProduct3();
145 void dotProduct4_data();
146 void dotProduct4();
147
148 void project_data();
149 void project();
150 void unproject_data();
151 void unproject();
152
153 void properties();
154 void metaTypes();
155};
156
157// Test the creation of QVector2D objects in various ways:
158// construct, copy, and modify.
159void tst_QVectorND::create2()
160{
161 QVector2D null;
162 QCOMPARE(null.x(), 0.0f);
163 QCOMPARE(null.y(), 0.0f);
164 QVERIFY(null.isNull());
165
166 QVector2D nullNegativeZero(-0.0f, -0.0f);
167 QCOMPARE(nullNegativeZero.x(), -0.0f);
168 QCOMPARE(nullNegativeZero.y(), -0.0f);
169 QVERIFY(nullNegativeZero.isNull());
170
171 QVector2D v1(1.0f, 2.5f);
172 QCOMPARE(v1.x(), 1.0f);
173 QCOMPARE(v1.y(), 2.5f);
174 QVERIFY(!v1.isNull());
175
176 QVector2D v1i(1, 2);
177 QCOMPARE(v1i.x(), 1.0f);
178 QCOMPARE(v1i.y(), 2.0f);
179 QVERIFY(!v1i.isNull());
180
181 QVector2D v2(v1);
182 QCOMPARE(v2.x(), 1.0f);
183 QCOMPARE(v2.y(), 2.5f);
184 QVERIFY(!v2.isNull());
185
186 QVector2D v4;
187 QCOMPARE(v4.x(), 0.0f);
188 QCOMPARE(v4.y(), 0.0f);
189 QVERIFY(v4.isNull());
190 v4 = v1;
191 QCOMPARE(v4.x(), 1.0f);
192 QCOMPARE(v4.y(), 2.5f);
193 QVERIFY(!v4.isNull());
194
195 QVector2D v5(QPoint(1, 2));
196 QCOMPARE(v5.x(), 1.0f);
197 QCOMPARE(v5.y(), 2.0f);
198 QVERIFY(!v5.isNull());
199
200 QVector2D v6(QPointF(1, 2.5));
201 QCOMPARE(v6.x(), 1.0f);
202 QCOMPARE(v6.y(), 2.5f);
203 QVERIFY(!v6.isNull());
204
205 QVector2D v7(QVector3D(1.0f, 2.5f, 54.25f));
206 QCOMPARE(v7.x(), 1.0f);
207 QCOMPARE(v7.y(), 2.5f);
208 QVERIFY(!v6.isNull());
209
210 QVector2D v8(QVector4D(1.0f, 2.5f, 54.25f, 34.0f));
211 QCOMPARE(v8.x(), 1.0f);
212 QCOMPARE(v8.y(), 2.5f);
213 QVERIFY(!v6.isNull());
214
215 v1.setX(3.0f);
216 QCOMPARE(v1.x(), 3.0f);
217 QCOMPARE(v1.y(), 2.5f);
218 QVERIFY(!v1.isNull());
219
220 v1.setY(10.5f);
221 QCOMPARE(v1.x(), 3.0f);
222 QCOMPARE(v1.y(), 10.5f);
223 QVERIFY(!v1.isNull());
224
225 v1.setX(0.0f);
226 v1.setY(0.0f);
227 QCOMPARE(v1.x(), 0.0f);
228 QCOMPARE(v1.y(), 0.0f);
229 QVERIFY(v1.isNull());
230
231 QPoint p1 = v8.toPoint();
232 QCOMPARE(p1.x(), 1);
233 QCOMPARE(p1.y(), 3);
234
235 QPointF p2 = v8.toPointF();
236 QCOMPARE(p2.x(), 1.0f);
237 QCOMPARE(p2.y(), 2.5f);
238
239 QVector3D v9 = v8.toVector3D();
240 QCOMPARE(v9.x(), 1.0f);
241 QCOMPARE(v9.y(), 2.5f);
242 QCOMPARE(v9.z(), 0.0f);
243
244 QVector4D v10 = v8.toVector4D();
245 QCOMPARE(v10.x(), 1.0f);
246 QCOMPARE(v10.y(), 2.5f);
247 QCOMPARE(v10.z(), 0.0f);
248 QCOMPARE(v10.w(), 0.0f);
249}
250
251// Test the creation of QVector3D objects in various ways:
252// construct, copy, and modify.
253void tst_QVectorND::create3()
254{
255 QVector3D null;
256 QCOMPARE(null.x(), 0.0f);
257 QCOMPARE(null.y(), 0.0f);
258 QCOMPARE(null.z(), 0.0f);
259 QVERIFY(null.isNull());
260
261 QVector3D nullNegativeZero(-0.0f, -0.0f, -0.0f);
262 QCOMPARE(nullNegativeZero.x(), -0.0f);
263 QCOMPARE(nullNegativeZero.y(), -0.0f);
264 QCOMPARE(nullNegativeZero.z(), -0.0f);
265 QVERIFY(nullNegativeZero.isNull());
266
267 QVector3D v1(1.0f, 2.5f, -89.25f);
268 QCOMPARE(v1.x(), 1.0f);
269 QCOMPARE(v1.y(), 2.5f);
270 QCOMPARE(v1.z(), -89.25f);
271 QVERIFY(!v1.isNull());
272
273 QVector3D v1i(1, 2, -89);
274 QCOMPARE(v1i.x(), 1.0f);
275 QCOMPARE(v1i.y(), 2.0f);
276 QCOMPARE(v1i.z(), -89.0f);
277 QVERIFY(!v1i.isNull());
278
279 QVector3D v2(v1);
280 QCOMPARE(v2.x(), 1.0f);
281 QCOMPARE(v2.y(), 2.5f);
282 QCOMPARE(v2.z(), -89.25f);
283 QVERIFY(!v2.isNull());
284
285 QVector3D v3(1.0f, 2.5f, 0.0f);
286 QCOMPARE(v3.x(), 1.0f);
287 QCOMPARE(v3.y(), 2.5f);
288 QCOMPARE(v3.z(), 0.0f);
289 QVERIFY(!v3.isNull());
290
291 QVector3D v3i(1, 2, 0);
292 QCOMPARE(v3i.x(), 1.0f);
293 QCOMPARE(v3i.y(), 2.0f);
294 QCOMPARE(v3i.z(), 0.0f);
295 QVERIFY(!v3i.isNull());
296
297 QVector3D v4;
298 QCOMPARE(v4.x(), 0.0f);
299 QCOMPARE(v4.y(), 0.0f);
300 QCOMPARE(v4.z(), 0.0f);
301 QVERIFY(v4.isNull());
302 v4 = v1;
303 QCOMPARE(v4.x(), 1.0f);
304 QCOMPARE(v4.y(), 2.5f);
305 QCOMPARE(v4.z(), -89.25f);
306 QVERIFY(!v4.isNull());
307
308 QVector3D v5(QPoint(1, 2));
309 QCOMPARE(v5.x(), 1.0f);
310 QCOMPARE(v5.y(), 2.0f);
311 QCOMPARE(v5.z(), 0.0f);
312 QVERIFY(!v5.isNull());
313
314 QVector3D v6(QPointF(1, 2.5));
315 QCOMPARE(v6.x(), 1.0f);
316 QCOMPARE(v6.y(), 2.5f);
317 QCOMPARE(v6.z(), 0.0f);
318 QVERIFY(!v6.isNull());
319
320 QVector3D v7(QVector2D(1.0f, 2.5f));
321 QCOMPARE(v7.x(), 1.0f);
322 QCOMPARE(v7.y(), 2.5f);
323 QCOMPARE(v7.z(), 0.0f);
324 QVERIFY(!v7.isNull());
325
326 QVector3D v8(QVector2D(1.0f, 2.5f), 54.25f);
327 QCOMPARE(v8.x(), 1.0f);
328 QCOMPARE(v8.y(), 2.5f);
329 QCOMPARE(v8.z(), 54.25f);
330 QVERIFY(!v8.isNull());
331
332 QVector3D v9(QVector4D(1.0f, 2.5f, 54.25f, 34.0f));
333 QCOMPARE(v9.x(), 1.0f);
334 QCOMPARE(v9.y(), 2.5f);
335 QCOMPARE(v9.z(), 54.25f);
336 QVERIFY(!v9.isNull());
337
338 v1.setX(3.0f);
339 QCOMPARE(v1.x(), 3.0f);
340 QCOMPARE(v1.y(), 2.5f);
341 QCOMPARE(v1.z(), -89.25f);
342 QVERIFY(!v1.isNull());
343
344 v1.setY(10.5f);
345 QCOMPARE(v1.x(), 3.0f);
346 QCOMPARE(v1.y(), 10.5f);
347 QCOMPARE(v1.z(), -89.25f);
348 QVERIFY(!v1.isNull());
349
350 v1.setZ(15.5f);
351 QCOMPARE(v1.x(), 3.0f);
352 QCOMPARE(v1.y(), 10.5f);
353 QCOMPARE(v1.z(), 15.5f);
354 QVERIFY(!v1.isNull());
355
356 v1.setX(0.0f);
357 v1.setY(0.0f);
358 v1.setZ(0.0f);
359 QCOMPARE(v1.x(), 0.0f);
360 QCOMPARE(v1.y(), 0.0f);
361 QCOMPARE(v1.z(), 0.0f);
362 QVERIFY(v1.isNull());
363
364 QPoint p1 = v8.toPoint();
365 QCOMPARE(p1.x(), 1);
366 QCOMPARE(p1.y(), 3);
367
368 QPointF p2 = v8.toPointF();
369 QCOMPARE(p2.x(), 1.0f);
370 QCOMPARE(p2.y(), 2.5f);
371
372 QVector2D v10 = v8.toVector2D();
373 QCOMPARE(v10.x(), 1.0f);
374 QCOMPARE(v10.y(), 2.5f);
375
376 QVector4D v11 = v8.toVector4D();
377 QCOMPARE(v11.x(), 1.0f);
378 QCOMPARE(v11.y(), 2.5f);
379 QCOMPARE(v11.z(), 54.25f);
380 QCOMPARE(v11.w(), 0.0f);
381}
382
383// Test the creation of QVector4D objects in various ways:
384// construct, copy, and modify.
385void tst_QVectorND::create4()
386{
387 QVector4D null;
388 QCOMPARE(null.x(), 0.0f);
389 QCOMPARE(null.y(), 0.0f);
390 QCOMPARE(null.z(), 0.0f);
391 QCOMPARE(null.w(), 0.0f);
392 QVERIFY(null.isNull());
393
394 QVector4D nullNegativeZero(-0.0f, -0.0f, -0.0f, -0.0f);
395 QCOMPARE(nullNegativeZero.x(), -0.0f);
396 QCOMPARE(nullNegativeZero.y(), -0.0f);
397 QCOMPARE(nullNegativeZero.z(), -0.0f);
398 QCOMPARE(nullNegativeZero.w(), -0.0f);
399 QVERIFY(nullNegativeZero.isNull());
400
401 QVector4D v1(1.0f, 2.5f, -89.25f, 34.0f);
402 QCOMPARE(v1.x(), 1.0f);
403 QCOMPARE(v1.y(), 2.5f);
404 QCOMPARE(v1.z(), -89.25f);
405 QCOMPARE(v1.w(), 34.0f);
406 QVERIFY(!v1.isNull());
407
408 QVector4D v1i(1, 2, -89, 34);
409 QCOMPARE(v1i.x(), 1.0f);
410 QCOMPARE(v1i.y(), 2.0f);
411 QCOMPARE(v1i.z(), -89.0f);
412 QCOMPARE(v1i.w(), 34.0f);
413 QVERIFY(!v1i.isNull());
414
415 QVector4D v2(v1);
416 QCOMPARE(v2.x(), 1.0f);
417 QCOMPARE(v2.y(), 2.5f);
418 QCOMPARE(v2.z(), -89.25f);
419 QCOMPARE(v2.w(), 34.0f);
420 QVERIFY(!v2.isNull());
421
422 QVector4D v3(1.0f, 2.5f, 0.0f, 0.0f);
423 QCOMPARE(v3.x(), 1.0f);
424 QCOMPARE(v3.y(), 2.5f);
425 QCOMPARE(v3.z(), 0.0f);
426 QCOMPARE(v3.w(), 0.0f);
427 QVERIFY(!v3.isNull());
428
429 QVector4D v3i(1, 2, 0, 0);
430 QCOMPARE(v3i.x(), 1.0f);
431 QCOMPARE(v3i.y(), 2.0f);
432 QCOMPARE(v3i.z(), 0.0f);
433 QCOMPARE(v3i.w(), 0.0f);
434 QVERIFY(!v3i.isNull());
435
436 QVector4D v3b(1.0f, 2.5f, -89.25f, 0.0f);
437 QCOMPARE(v3b.x(), 1.0f);
438 QCOMPARE(v3b.y(), 2.5f);
439 QCOMPARE(v3b.z(), -89.25f);
440 QCOMPARE(v3b.w(), 0.0f);
441 QVERIFY(!v3b.isNull());
442
443 QVector4D v3bi(1, 2, -89, 0);
444 QCOMPARE(v3bi.x(), 1.0f);
445 QCOMPARE(v3bi.y(), 2.0f);
446 QCOMPARE(v3bi.z(), -89.0f);
447 QCOMPARE(v3bi.w(), 0.0f);
448 QVERIFY(!v3bi.isNull());
449
450 QVector4D v4;
451 QCOMPARE(v4.x(), 0.0f);
452 QCOMPARE(v4.y(), 0.0f);
453 QCOMPARE(v4.z(), 0.0f);
454 QCOMPARE(v4.w(), 0.0f);
455 QVERIFY(v4.isNull());
456 v4 = v1;
457 QCOMPARE(v4.x(), 1.0f);
458 QCOMPARE(v4.y(), 2.5f);
459 QCOMPARE(v4.z(), -89.25f);
460 QCOMPARE(v4.w(), 34.0f);
461 QVERIFY(!v4.isNull());
462
463 QVector4D v5(QPoint(1, 2));
464 QCOMPARE(v5.x(), 1.0f);
465 QCOMPARE(v5.y(), 2.0f);
466 QCOMPARE(v5.z(), 0.0f);
467 QCOMPARE(v5.w(), 0.0f);
468 QVERIFY(!v5.isNull());
469
470 QVector4D v6(QPointF(1, 2.5));
471 QCOMPARE(v6.x(), 1.0f);
472 QCOMPARE(v6.y(), 2.5f);
473 QCOMPARE(v6.z(), 0.0f);
474 QCOMPARE(v6.w(), 0.0f);
475 QVERIFY(!v6.isNull());
476
477 QVector4D v7(QVector2D(1.0f, 2.5f));
478 QCOMPARE(v7.x(), 1.0f);
479 QCOMPARE(v7.y(), 2.5f);
480 QCOMPARE(v7.z(), 0.0f);
481 QCOMPARE(v7.w(), 0.0f);
482 QVERIFY(!v7.isNull());
483
484 QVector4D v8(QVector3D(1.0f, 2.5f, -89.25f));
485 QCOMPARE(v8.x(), 1.0f);
486 QCOMPARE(v8.y(), 2.5f);
487 QCOMPARE(v8.z(), -89.25f);
488 QCOMPARE(v8.w(), 0.0f);
489 QVERIFY(!v8.isNull());
490
491 QVector4D v9(QVector3D(1.0f, 2.5f, -89.25f), 34);
492 QCOMPARE(v9.x(), 1.0f);
493 QCOMPARE(v9.y(), 2.5f);
494 QCOMPARE(v9.z(), -89.25f);
495 QCOMPARE(v9.w(), 34.0f);
496 QVERIFY(!v9.isNull());
497
498 QVector4D v10(QVector2D(1.0f, 2.5f), 23.5f, -8);
499 QCOMPARE(v10.x(), 1.0f);
500 QCOMPARE(v10.y(), 2.5f);
501 QCOMPARE(v10.z(), 23.5f);
502 QCOMPARE(v10.w(), -8.0f);
503 QVERIFY(!v10.isNull());
504
505 v1.setX(3.0f);
506 QCOMPARE(v1.x(), 3.0f);
507 QCOMPARE(v1.y(), 2.5f);
508 QCOMPARE(v1.z(), -89.25f);
509 QCOMPARE(v1.w(), 34.0f);
510 QVERIFY(!v1.isNull());
511
512 v1.setY(10.5f);
513 QCOMPARE(v1.x(), 3.0f);
514 QCOMPARE(v1.y(), 10.5f);
515 QCOMPARE(v1.z(), -89.25f);
516 QCOMPARE(v1.w(), 34.0f);
517 QVERIFY(!v1.isNull());
518
519 v1.setZ(15.5f);
520 QCOMPARE(v1.x(), 3.0f);
521 QCOMPARE(v1.y(), 10.5f);
522 QCOMPARE(v1.z(), 15.5f);
523 QCOMPARE(v1.w(), 34.0f);
524 QVERIFY(!v1.isNull());
525
526 v1.setW(6.0f);
527 QCOMPARE(v1.x(), 3.0f);
528 QCOMPARE(v1.y(), 10.5f);
529 QCOMPARE(v1.z(), 15.5f);
530 QCOMPARE(v1.w(), 6.0f);
531 QVERIFY(!v1.isNull());
532
533 v1.setX(0.0f);
534 v1.setY(0.0f);
535 v1.setZ(0.0f);
536 v1.setW(0.0f);
537 QCOMPARE(v1.x(), 0.0f);
538 QCOMPARE(v1.y(), 0.0f);
539 QCOMPARE(v1.z(), 0.0f);
540 QCOMPARE(v1.w(), 0.0f);
541 QVERIFY(v1.isNull());
542
543 QPoint p1 = v8.toPoint();
544 QCOMPARE(p1.x(), 1);
545 QCOMPARE(p1.y(), 3);
546
547 QPointF p2 = v8.toPointF();
548 QCOMPARE(p2.x(), 1.0f);
549 QCOMPARE(p2.y(), 2.5f);
550
551 QVector2D v11 = v8.toVector2D();
552 QCOMPARE(v11.x(), 1.0f);
553 QCOMPARE(v11.y(), 2.5f);
554
555 QVector3D v12 = v8.toVector3D();
556 QCOMPARE(v12.x(), 1.0f);
557 QCOMPARE(v12.y(), 2.5f);
558 QCOMPARE(v12.z(), -89.25f);
559
560 QVector2D v13 = v9.toVector2DAffine();
561 QVERIFY(qFuzzyCompare(v13.x(), (1.0f / 34.0f)));
562 QVERIFY(qFuzzyCompare(v13.y(), (2.5f / 34.0f)));
563
564 QVector4D zerow(1.0f, 2.0f, 3.0f, 0.0f);
565 v13 = zerow.toVector2DAffine();
566 QVERIFY(v13.isNull());
567
568 QVector3D v14 = v9.toVector3DAffine();
569 QVERIFY(qFuzzyCompare(v14.x(), (1.0f / 34.0f)));
570 QVERIFY(qFuzzyCompare(v14.y(), (2.5f / 34.0f)));
571 QVERIFY(qFuzzyCompare(v14.z(), (-89.25f / 34.0f)));
572
573 v14 = zerow.toVector3DAffine();
574 QVERIFY(v14.isNull());
575}
576
577// Test modifying vectors in various ways
578void tst_QVectorND::modify2()
579{
580 const float e = 2.7182818f;
581 const float pi = 3.14159f;
582 const QVector2D p(e, pi);
583
584 QVector2D p1;
585 p1.setX(e);
586 p1.setY(pi);
587 QVERIFY(qFuzzyCompare(p, p1));
588
589 QVector2D p2;
590 p2[0] = e;
591 p2[1] = pi;
592 QVERIFY(qFuzzyCompare(p, p2));
593
594 QVector2D p3;
595 for (int i = 0; i < 2; ++i)
596 p3[i] = p[i];
597 QVERIFY(qFuzzyCompare(p, p3));
598}
599
600void tst_QVectorND::modify3()
601{
602 const float one = 1.0f;
603 const float e = 2.7182818f;
604 const float pi = 3.14159f;
605 const QVector3D p(one, e, pi);
606
607 QVector3D p1;
608 p1.setX(one);
609 p1.setY(e);
610 p1.setZ(pi);
611 QVERIFY(qFuzzyCompare(p, p1));
612
613 QVector3D p2;
614 p2[0] = one;
615 p2[1] = e;
616 p2[2] = pi;
617 QVERIFY(qFuzzyCompare(p, p2));
618
619 QVector3D p3;
620 for (int i = 0; i < 3; ++i)
621 p3[i] = p[i];
622 QVERIFY(qFuzzyCompare(p, p3));
623}
624
625void tst_QVectorND::modify4()
626{
627 const float one = 1.0f;
628 const float e = 2.7182818f;
629 const float pi = 3.14159f;
630 const float big = 1.0e6f;
631 const QVector4D p(one, e, pi, big);
632
633 QVector4D p1;
634 p1.setX(one);
635 p1.setY(e);
636 p1.setZ(pi);
637 p1.setW(big);
638 QVERIFY(qFuzzyCompare(p, p1));
639
640 QVector4D p2;
641 p2[0] = one;
642 p2[1] = e;
643 p2[2] = pi;
644 p2[3] = big;
645 QVERIFY(qFuzzyCompare(p, p2));
646
647 QVector4D p3;
648 for (int i = 0; i < 4; ++i)
649 p3[i] = p[i];
650 QVERIFY(qFuzzyCompare(p, p3));
651}
652
653// Test vector length computation for 2D vectors.
654void tst_QVectorND::length2_data()
655{
656 QTest::addColumn<float>(name: "x");
657 QTest::addColumn<float>(name: "y");
658 QTest::addColumn<float>(name: "len");
659
660 QTest::newRow(dataTag: "null") << 0.0f << 0.0f << 0.0f;
661 QTest::newRow(dataTag: "1x") << 1.0f << 0.0f << 1.0f;
662 QTest::newRow(dataTag: "1y") << 0.0f << 1.0f << 1.0f;
663 QTest::newRow(dataTag: "-1x") << -1.0f << 0.0f << 1.0f;
664 QTest::newRow(dataTag: "-1y") << 0.0f << -1.0f << 1.0f;
665 QTest::newRow(dataTag: "two") << 2.0f << -2.0f << std::sqrt(x: 8.0f);
666}
667void tst_QVectorND::length2()
668{
669 QFETCH(float, x);
670 QFETCH(float, y);
671 QFETCH(float, len);
672
673 QVector2D v(x, y);
674 QCOMPARE(v.length(), len);
675 QCOMPARE(v.lengthSquared(), x * x + y * y);
676}
677
678// Test vector length computation for 3D vectors.
679void tst_QVectorND::length3_data()
680{
681 QTest::addColumn<float>(name: "x");
682 QTest::addColumn<float>(name: "y");
683 QTest::addColumn<float>(name: "z");
684 QTest::addColumn<float>(name: "len");
685
686 QTest::newRow(dataTag: "null") << 0.0f << 0.0f << 0.0f << 0.0f;
687 QTest::newRow(dataTag: "1x") << 1.0f << 0.0f << 0.0f << 1.0f;
688 QTest::newRow(dataTag: "1y") << 0.0f << 1.0f << 0.0f << 1.0f;
689 QTest::newRow(dataTag: "1z") << 0.0f << 0.0f << 1.0f << 1.0f;
690 QTest::newRow(dataTag: "-1x") << -1.0f << 0.0f << 0.0f << 1.0f;
691 QTest::newRow(dataTag: "-1y") << 0.0f << -1.0f << 0.0f << 1.0f;
692 QTest::newRow(dataTag: "-1z") << 0.0f << 0.0f << -1.0f << 1.0f;
693 QTest::newRow(dataTag: "two") << 2.0f << -2.0f << 2.0f << std::sqrt(x: 12.0f);
694}
695void tst_QVectorND::length3()
696{
697 QFETCH(float, x);
698 QFETCH(float, y);
699 QFETCH(float, z);
700 QFETCH(float, len);
701
702 QVector3D v(x, y, z);
703 QCOMPARE(v.length(), len);
704 QCOMPARE(v.lengthSquared(), x * x + y * y + z * z);
705}
706
707// Test vector length computation for 4D vectors.
708void tst_QVectorND::length4_data()
709{
710 QTest::addColumn<float>(name: "x");
711 QTest::addColumn<float>(name: "y");
712 QTest::addColumn<float>(name: "z");
713 QTest::addColumn<float>(name: "w");
714 QTest::addColumn<float>(name: "len");
715
716 QTest::newRow(dataTag: "null") << 0.0f << 0.0f << 0.0f << 0.0f << 0.0f;
717 QTest::newRow(dataTag: "1x") << 1.0f << 0.0f << 0.0f << 0.0f << 1.0f;
718 QTest::newRow(dataTag: "1y") << 0.0f << 1.0f << 0.0f << 0.0f << 1.0f;
719 QTest::newRow(dataTag: "1z") << 0.0f << 0.0f << 1.0f << 0.0f << 1.0f;
720 QTest::newRow(dataTag: "1w") << 0.0f << 0.0f << 0.0f << 1.0f << 1.0f;
721 QTest::newRow(dataTag: "-1x") << -1.0f << 0.0f << 0.0f << 0.0f << 1.0f;
722 QTest::newRow(dataTag: "-1y") << 0.0f << -1.0f << 0.0f << 0.0f << 1.0f;
723 QTest::newRow(dataTag: "-1z") << 0.0f << 0.0f << -1.0f << 0.0f << 1.0f;
724 QTest::newRow(dataTag: "-1w") << 0.0f << 0.0f << 0.0f << -1.0f << 1.0f;
725 QTest::newRow(dataTag: "two") << 2.0f << -2.0f << 2.0f << 2.0f << std::sqrt(x: 16.0f);
726}
727void tst_QVectorND::length4()
728{
729 QFETCH(float, x);
730 QFETCH(float, y);
731 QFETCH(float, z);
732 QFETCH(float, w);
733 QFETCH(float, len);
734
735 QVector4D v(x, y, z, w);
736 QCOMPARE(v.length(), len);
737 QCOMPARE(v.lengthSquared(), x * x + y * y + z * z + w * w);
738}
739
740// Test the unit vector conversion for 2D vectors.
741void tst_QVectorND::normalized2_data()
742{
743 // Use the same test data as the length test.
744 length2_data();
745}
746void tst_QVectorND::normalized2()
747{
748 QFETCH(float, x);
749 QFETCH(float, y);
750 QFETCH(float, len);
751
752 QVector2D v(x, y);
753 QVector2D u = v.normalized();
754 if (v.isNull())
755 QVERIFY(u.isNull());
756 else
757 QVERIFY(qFuzzyCompare(u.length(), 1.0f));
758 QVERIFY(qFuzzyCompare(u.x() * len, v.x()));
759 QVERIFY(qFuzzyCompare(u.y() * len, v.y()));
760}
761
762// Test the unit vector conversion for 3D vectors.
763void tst_QVectorND::normalized3_data()
764{
765 // Use the same test data as the length test.
766 length3_data();
767}
768void tst_QVectorND::normalized3()
769{
770 QFETCH(float, x);
771 QFETCH(float, y);
772 QFETCH(float, z);
773 QFETCH(float, len);
774
775 QVector3D v(x, y, z);
776 QVector3D u = v.normalized();
777 if (v.isNull())
778 QVERIFY(u.isNull());
779 else
780 QVERIFY(qFuzzyCompare(u.length(), 1.0f));
781 QVERIFY(qFuzzyCompare(u.x() * len, v.x()));
782 QVERIFY(qFuzzyCompare(u.y() * len, v.y()));
783 QVERIFY(qFuzzyCompare(u.z() * len, v.z()));
784}
785
786// Test the unit vector conversion for 4D vectors.
787void tst_QVectorND::normalized4_data()
788{
789 // Use the same test data as the length test.
790 length4_data();
791}
792void tst_QVectorND::normalized4()
793{
794 QFETCH(float, x);
795 QFETCH(float, y);
796 QFETCH(float, z);
797 QFETCH(float, w);
798 QFETCH(float, len);
799
800 QVector4D v(x, y, z, w);
801 QVector4D u = v.normalized();
802 if (v.isNull())
803 QVERIFY(u.isNull());
804 else
805 QVERIFY(qFuzzyCompare(u.length(), 1.0f));
806 QVERIFY(qFuzzyCompare(u.x() * len, v.x()));
807 QVERIFY(qFuzzyCompare(u.y() * len, v.y()));
808 QVERIFY(qFuzzyCompare(u.z() * len, v.z()));
809 QVERIFY(qFuzzyCompare(u.w() * len, v.w()));
810}
811
812// Test the unit vector conversion for 2D vectors.
813void tst_QVectorND::normalize2_data()
814{
815 // Use the same test data as the length test.
816 length2_data();
817}
818void tst_QVectorND::normalize2()
819{
820 QFETCH(float, x);
821 QFETCH(float, y);
822
823 QVector2D v(x, y);
824 bool isNull = v.isNull();
825 v.normalize();
826 if (isNull)
827 QVERIFY(v.isNull());
828 else
829 QVERIFY(qFuzzyCompare(v.length(), 1.0f));
830}
831
832// Test the unit vector conversion for 3D vectors.
833void tst_QVectorND::normalize3_data()
834{
835 // Use the same test data as the length test.
836 length3_data();
837}
838void tst_QVectorND::normalize3()
839{
840 QFETCH(float, x);
841 QFETCH(float, y);
842 QFETCH(float, z);
843
844 QVector3D v(x, y, z);
845 bool isNull = v.isNull();
846 v.normalize();
847 if (isNull)
848 QVERIFY(v.isNull());
849 else
850 QVERIFY(qFuzzyCompare(v.length(), 1.0f));
851}
852
853// Test the unit vector conversion for 4D vectors.
854void tst_QVectorND::normalize4_data()
855{
856 // Use the same test data as the length test.
857 length4_data();
858}
859void tst_QVectorND::normalize4()
860{
861 QFETCH(float, x);
862 QFETCH(float, y);
863 QFETCH(float, z);
864 QFETCH(float, w);
865
866 QVector4D v(x, y, z, w);
867 bool isNull = v.isNull();
868 v.normalize();
869 if (isNull)
870 QVERIFY(v.isNull());
871 else
872 QVERIFY(qFuzzyCompare(v.length(), 1.0f));
873}
874
875// Test the comparison operators for 2D vectors.
876void tst_QVectorND::compare2()
877{
878 QVector2D v1(1, 2);
879 QVector2D v2(1, 2);
880 QVector2D v3(3, 2);
881 QVector2D v4(1, 3);
882
883 QCOMPARE(v1, v2);
884 QVERIFY(v1 != v3);
885 QVERIFY(v1 != v4);
886}
887
888// Test the comparison operators for 3D vectors.
889void tst_QVectorND::compare3()
890{
891 QVector3D v1(1, 2, 4);
892 QVector3D v2(1, 2, 4);
893 QVector3D v3(3, 2, 4);
894 QVector3D v4(1, 3, 4);
895 QVector3D v5(1, 2, 3);
896
897 QCOMPARE(v1, v2);
898 QVERIFY(v1 != v3);
899 QVERIFY(v1 != v4);
900 QVERIFY(v1 != v5);
901}
902
903// Test the comparison operators for 4D vectors.
904void tst_QVectorND::compare4()
905{
906 QVector4D v1(1, 2, 4, 8);
907 QVector4D v2(1, 2, 4, 8);
908 QVector4D v3(3, 2, 4, 8);
909 QVector4D v4(1, 3, 4, 8);
910 QVector4D v5(1, 2, 3, 8);
911 QVector4D v6(1, 2, 4, 3);
912
913 QCOMPARE(v1, v2);
914 QVERIFY(v1 != v3);
915 QVERIFY(v1 != v4);
916 QVERIFY(v1 != v5);
917 QVERIFY(v1 != v6);
918}
919
920// Test vector addition for 2D vectors.
921void tst_QVectorND::add2_data()
922{
923 QTest::addColumn<float>(name: "x1");
924 QTest::addColumn<float>(name: "y1");
925 QTest::addColumn<float>(name: "x2");
926 QTest::addColumn<float>(name: "y2");
927 QTest::addColumn<float>(name: "x3");
928 QTest::addColumn<float>(name: "y3");
929
930 QTest::newRow(dataTag: "null")
931 << 0.0f << 0.0f
932 << 0.0f << 0.0f
933 << 0.0f << 0.0f;
934
935 QTest::newRow(dataTag: "xonly")
936 << 1.0f << 0.0f
937 << 2.0f << 0.0f
938 << 3.0f << 0.0f;
939
940 QTest::newRow(dataTag: "yonly")
941 << 0.0f << 1.0f
942 << 0.0f << 2.0f
943 << 0.0f << 3.0f;
944
945 QTest::newRow(dataTag: "all")
946 << 1.0f << 2.0f
947 << 4.0f << 5.0f
948 << 5.0f << 7.0f;
949}
950void tst_QVectorND::add2()
951{
952 QFETCH(float, x1);
953 QFETCH(float, y1);
954 QFETCH(float, x2);
955 QFETCH(float, y2);
956 QFETCH(float, x3);
957 QFETCH(float, y3);
958
959 QVector2D v1(x1, y1);
960 QVector2D v2(x2, y2);
961 QVector2D v3(x3, y3);
962
963 QVERIFY((v1 + v2) == v3);
964
965 QVector2D v4(v1);
966 v4 += v2;
967 QCOMPARE(v4, v3);
968
969 QCOMPARE(v4.x(), v1.x() + v2.x());
970 QCOMPARE(v4.y(), v1.y() + v2.y());
971}
972
973// Test vector addition for 3D vectors.
974void tst_QVectorND::add3_data()
975{
976 QTest::addColumn<float>(name: "x1");
977 QTest::addColumn<float>(name: "y1");
978 QTest::addColumn<float>(name: "z1");
979 QTest::addColumn<float>(name: "x2");
980 QTest::addColumn<float>(name: "y2");
981 QTest::addColumn<float>(name: "z2");
982 QTest::addColumn<float>(name: "x3");
983 QTest::addColumn<float>(name: "y3");
984 QTest::addColumn<float>(name: "z3");
985
986 QTest::newRow(dataTag: "null")
987 << 0.0f << 0.0f << 0.0f
988 << 0.0f << 0.0f << 0.0f
989 << 0.0f << 0.0f << 0.0f;
990
991 QTest::newRow(dataTag: "xonly")
992 << 1.0f << 0.0f << 0.0f
993 << 2.0f << 0.0f << 0.0f
994 << 3.0f << 0.0f << 0.0f;
995
996 QTest::newRow(dataTag: "yonly")
997 << 0.0f << 1.0f << 0.0f
998 << 0.0f << 2.0f << 0.0f
999 << 0.0f << 3.0f << 0.0f;
1000
1001 QTest::newRow(dataTag: "zonly")
1002 << 0.0f << 0.0f << 1.0f
1003 << 0.0f << 0.0f << 2.0f
1004 << 0.0f << 0.0f << 3.0f;
1005
1006 QTest::newRow(dataTag: "all")
1007 << 1.0f << 2.0f << 3.0f
1008 << 4.0f << 5.0f << -6.0f
1009 << 5.0f << 7.0f << -3.0f;
1010}
1011void tst_QVectorND::add3()
1012{
1013 QFETCH(float, x1);
1014 QFETCH(float, y1);
1015 QFETCH(float, z1);
1016 QFETCH(float, x2);
1017 QFETCH(float, y2);
1018 QFETCH(float, z2);
1019 QFETCH(float, x3);
1020 QFETCH(float, y3);
1021 QFETCH(float, z3);
1022
1023 QVector3D v1(x1, y1, z1);
1024 QVector3D v2(x2, y2, z2);
1025 QVector3D v3(x3, y3, z3);
1026
1027 QVERIFY((v1 + v2) == v3);
1028
1029 QVector3D v4(v1);
1030 v4 += v2;
1031 QCOMPARE(v4, v3);
1032
1033 QCOMPARE(v4.x(), v1.x() + v2.x());
1034 QCOMPARE(v4.y(), v1.y() + v2.y());
1035 QCOMPARE(v4.z(), v1.z() + v2.z());
1036}
1037
1038// Test vector addition for 4D vectors.
1039void tst_QVectorND::add4_data()
1040{
1041 QTest::addColumn<float>(name: "x1");
1042 QTest::addColumn<float>(name: "y1");
1043 QTest::addColumn<float>(name: "z1");
1044 QTest::addColumn<float>(name: "w1");
1045 QTest::addColumn<float>(name: "x2");
1046 QTest::addColumn<float>(name: "y2");
1047 QTest::addColumn<float>(name: "z2");
1048 QTest::addColumn<float>(name: "w2");
1049 QTest::addColumn<float>(name: "x3");
1050 QTest::addColumn<float>(name: "y3");
1051 QTest::addColumn<float>(name: "z3");
1052 QTest::addColumn<float>(name: "w3");
1053
1054 QTest::newRow(dataTag: "null")
1055 << 0.0f << 0.0f << 0.0f << 0.0f
1056 << 0.0f << 0.0f << 0.0f << 0.0f
1057 << 0.0f << 0.0f << 0.0f << 0.0f;
1058
1059 QTest::newRow(dataTag: "xonly")
1060 << 1.0f << 0.0f << 0.0f << 0.0f
1061 << 2.0f << 0.0f << 0.0f << 0.0f
1062 << 3.0f << 0.0f << 0.0f << 0.0f;
1063
1064 QTest::newRow(dataTag: "yonly")
1065 << 0.0f << 1.0f << 0.0f << 0.0f
1066 << 0.0f << 2.0f << 0.0f << 0.0f
1067 << 0.0f << 3.0f << 0.0f << 0.0f;
1068
1069 QTest::newRow(dataTag: "zonly")
1070 << 0.0f << 0.0f << 1.0f << 0.0f
1071 << 0.0f << 0.0f << 2.0f << 0.0f
1072 << 0.0f << 0.0f << 3.0f << 0.0f;
1073
1074 QTest::newRow(dataTag: "wonly")
1075 << 0.0f << 0.0f << 0.0f << 1.0f
1076 << 0.0f << 0.0f << 0.0f << 2.0f
1077 << 0.0f << 0.0f << 0.0f << 3.0f;
1078
1079 QTest::newRow(dataTag: "all")
1080 << 1.0f << 2.0f << 3.0f << 8.0f
1081 << 4.0f << 5.0f << -6.0f << 9.0f
1082 << 5.0f << 7.0f << -3.0f << 17.0f;
1083}
1084void tst_QVectorND::add4()
1085{
1086 QFETCH(float, x1);
1087 QFETCH(float, y1);
1088 QFETCH(float, z1);
1089 QFETCH(float, w1);
1090 QFETCH(float, x2);
1091 QFETCH(float, y2);
1092 QFETCH(float, z2);
1093 QFETCH(float, w2);
1094 QFETCH(float, x3);
1095 QFETCH(float, y3);
1096 QFETCH(float, z3);
1097 QFETCH(float, w3);
1098
1099 QVector4D v1(x1, y1, z1, w1);
1100 QVector4D v2(x2, y2, z2, w2);
1101 QVector4D v3(x3, y3, z3, w3);
1102
1103 QVERIFY((v1 + v2) == v3);
1104
1105 QVector4D v4(v1);
1106 v4 += v2;
1107 QCOMPARE(v4, v3);
1108
1109 QCOMPARE(v4.x(), v1.x() + v2.x());
1110 QCOMPARE(v4.y(), v1.y() + v2.y());
1111 QCOMPARE(v4.z(), v1.z() + v2.z());
1112 QCOMPARE(v4.w(), v1.w() + v2.w());
1113}
1114
1115// Test vector subtraction for 2D vectors.
1116void tst_QVectorND::subtract2_data()
1117{
1118 // Use the same test data as the add test.
1119 add2_data();
1120}
1121void tst_QVectorND::subtract2()
1122{
1123 QFETCH(float, x1);
1124 QFETCH(float, y1);
1125 QFETCH(float, x2);
1126 QFETCH(float, y2);
1127 QFETCH(float, x3);
1128 QFETCH(float, y3);
1129
1130 QVector2D v1(x1, y1);
1131 QVector2D v2(x2, y2);
1132 QVector2D v3(x3, y3);
1133
1134 QVERIFY((v3 - v1) == v2);
1135 QVERIFY((v3 - v2) == v1);
1136
1137 QVector2D v4(v3);
1138 v4 -= v1;
1139 QCOMPARE(v4, v2);
1140
1141 QCOMPARE(v4.x(), v3.x() - v1.x());
1142 QCOMPARE(v4.y(), v3.y() - v1.y());
1143
1144 QVector2D v5(v3);
1145 v5 -= v2;
1146 QCOMPARE(v5, v1);
1147
1148 QCOMPARE(v5.x(), v3.x() - v2.x());
1149 QCOMPARE(v5.y(), v3.y() - v2.y());
1150}
1151
1152// Test vector subtraction for 3D vectors.
1153void tst_QVectorND::subtract3_data()
1154{
1155 // Use the same test data as the add test.
1156 add3_data();
1157}
1158void tst_QVectorND::subtract3()
1159{
1160 QFETCH(float, x1);
1161 QFETCH(float, y1);
1162 QFETCH(float, z1);
1163 QFETCH(float, x2);
1164 QFETCH(float, y2);
1165 QFETCH(float, z2);
1166 QFETCH(float, x3);
1167 QFETCH(float, y3);
1168 QFETCH(float, z3);
1169
1170 QVector3D v1(x1, y1, z1);
1171 QVector3D v2(x2, y2, z2);
1172 QVector3D v3(x3, y3, z3);
1173
1174 QVERIFY((v3 - v1) == v2);
1175 QVERIFY((v3 - v2) == v1);
1176
1177 QVector3D v4(v3);
1178 v4 -= v1;
1179 QCOMPARE(v4, v2);
1180
1181 QCOMPARE(v4.x(), v3.x() - v1.x());
1182 QCOMPARE(v4.y(), v3.y() - v1.y());
1183 QCOMPARE(v4.z(), v3.z() - v1.z());
1184
1185 QVector3D v5(v3);
1186 v5 -= v2;
1187 QCOMPARE(v5, v1);
1188
1189 QCOMPARE(v5.x(), v3.x() - v2.x());
1190 QCOMPARE(v5.y(), v3.y() - v2.y());
1191 QCOMPARE(v5.z(), v3.z() - v2.z());
1192}
1193
1194// Test vector subtraction for 4D vectors.
1195void tst_QVectorND::subtract4_data()
1196{
1197 // Use the same test data as the add test.
1198 add4_data();
1199}
1200void tst_QVectorND::subtract4()
1201{
1202 QFETCH(float, x1);
1203 QFETCH(float, y1);
1204 QFETCH(float, z1);
1205 QFETCH(float, w1);
1206 QFETCH(float, x2);
1207 QFETCH(float, y2);
1208 QFETCH(float, z2);
1209 QFETCH(float, w2);
1210 QFETCH(float, x3);
1211 QFETCH(float, y3);
1212 QFETCH(float, z3);
1213 QFETCH(float, w3);
1214
1215 QVector4D v1(x1, y1, z1, w1);
1216 QVector4D v2(x2, y2, z2, w2);
1217 QVector4D v3(x3, y3, z3, w3);
1218
1219 QVERIFY((v3 - v1) == v2);
1220 QVERIFY((v3 - v2) == v1);
1221
1222 QVector4D v4(v3);
1223 v4 -= v1;
1224 QCOMPARE(v4, v2);
1225
1226 QCOMPARE(v4.x(), v3.x() - v1.x());
1227 QCOMPARE(v4.y(), v3.y() - v1.y());
1228 QCOMPARE(v4.z(), v3.z() - v1.z());
1229 QCOMPARE(v4.w(), v3.w() - v1.w());
1230
1231 QVector4D v5(v3);
1232 v5 -= v2;
1233 QCOMPARE(v5, v1);
1234
1235 QCOMPARE(v5.x(), v3.x() - v2.x());
1236 QCOMPARE(v5.y(), v3.y() - v2.y());
1237 QCOMPARE(v5.z(), v3.z() - v2.z());
1238 QCOMPARE(v5.w(), v3.w() - v2.w());
1239}
1240
1241// Test component-wise vector multiplication for 2D vectors.
1242void tst_QVectorND::multiply2_data()
1243{
1244 QTest::addColumn<float>(name: "x1");
1245 QTest::addColumn<float>(name: "y1");
1246 QTest::addColumn<float>(name: "x2");
1247 QTest::addColumn<float>(name: "y2");
1248 QTest::addColumn<float>(name: "x3");
1249 QTest::addColumn<float>(name: "y3");
1250
1251 QTest::newRow(dataTag: "null")
1252 << 0.0f << 0.0f
1253 << 0.0f << 0.0f
1254 << 0.0f << 0.0f;
1255
1256 QTest::newRow(dataTag: "xonly")
1257 << 1.0f << 0.0f
1258 << 2.0f << 0.0f
1259 << 2.0f << 0.0f;
1260
1261 QTest::newRow(dataTag: "yonly")
1262 << 0.0f << 1.0f
1263 << 0.0f << 2.0f
1264 << 0.0f << 2.0f;
1265
1266 QTest::newRow(dataTag: "all")
1267 << 1.0f << 2.0f
1268 << 4.0f << 5.0f
1269 << 4.0f << 10.0f;
1270}
1271void tst_QVectorND::multiply2()
1272{
1273 QFETCH(float, x1);
1274 QFETCH(float, y1);
1275 QFETCH(float, x2);
1276 QFETCH(float, y2);
1277 QFETCH(float, x3);
1278 QFETCH(float, y3);
1279
1280 QVector2D v1(x1, y1);
1281 QVector2D v2(x2, y2);
1282 QVector2D v3(x3, y3);
1283
1284 QVERIFY((v1 * v2) == v3);
1285
1286 QVector2D v4(v1);
1287 v4 *= v2;
1288 QCOMPARE(v4, v3);
1289
1290 QCOMPARE(v4.x(), v1.x() * v2.x());
1291 QCOMPARE(v4.y(), v1.y() * v2.y());
1292}
1293
1294// Test component-wise vector multiplication for 3D vectors.
1295void tst_QVectorND::multiply3_data()
1296{
1297 QTest::addColumn<float>(name: "x1");
1298 QTest::addColumn<float>(name: "y1");
1299 QTest::addColumn<float>(name: "z1");
1300 QTest::addColumn<float>(name: "x2");
1301 QTest::addColumn<float>(name: "y2");
1302 QTest::addColumn<float>(name: "z2");
1303 QTest::addColumn<float>(name: "x3");
1304 QTest::addColumn<float>(name: "y3");
1305 QTest::addColumn<float>(name: "z3");
1306
1307 QTest::newRow(dataTag: "null")
1308 << 0.0f << 0.0f << 0.0f
1309 << 0.0f << 0.0f << 0.0f
1310 << 0.0f << 0.0f << 0.0f;
1311
1312 QTest::newRow(dataTag: "xonly")
1313 << 1.0f << 0.0f << 0.0f
1314 << 2.0f << 0.0f << 0.0f
1315 << 2.0f << 0.0f << 0.0f;
1316
1317 QTest::newRow(dataTag: "yonly")
1318 << 0.0f << 1.0f << 0.0f
1319 << 0.0f << 2.0f << 0.0f
1320 << 0.0f << 2.0f << 0.0f;
1321
1322 QTest::newRow(dataTag: "zonly")
1323 << 0.0f << 0.0f << 1.0f
1324 << 0.0f << 0.0f << 2.0f
1325 << 0.0f << 0.0f << 2.0f;
1326
1327 QTest::newRow(dataTag: "all")
1328 << 1.0f << 2.0f << 3.0f
1329 << 4.0f << 5.0f << -6.0f
1330 << 4.0f << 10.0f << -18.0f;
1331}
1332void tst_QVectorND::multiply3()
1333{
1334 QFETCH(float, x1);
1335 QFETCH(float, y1);
1336 QFETCH(float, z1);
1337 QFETCH(float, x2);
1338 QFETCH(float, y2);
1339 QFETCH(float, z2);
1340 QFETCH(float, x3);
1341 QFETCH(float, y3);
1342 QFETCH(float, z3);
1343
1344 QVector3D v1(x1, y1, z1);
1345 QVector3D v2(x2, y2, z2);
1346 QVector3D v3(x3, y3, z3);
1347
1348 QVERIFY((v1 * v2) == v3);
1349
1350 QVector3D v4(v1);
1351 v4 *= v2;
1352 QCOMPARE(v4, v3);
1353
1354 QCOMPARE(v4.x(), v1.x() * v2.x());
1355 QCOMPARE(v4.y(), v1.y() * v2.y());
1356 QCOMPARE(v4.z(), v1.z() * v2.z());
1357}
1358
1359// Test component-wise vector multiplication for 4D vectors.
1360void tst_QVectorND::multiply4_data()
1361{
1362 QTest::addColumn<float>(name: "x1");
1363 QTest::addColumn<float>(name: "y1");
1364 QTest::addColumn<float>(name: "z1");
1365 QTest::addColumn<float>(name: "w1");
1366 QTest::addColumn<float>(name: "x2");
1367 QTest::addColumn<float>(name: "y2");
1368 QTest::addColumn<float>(name: "z2");
1369 QTest::addColumn<float>(name: "w2");
1370 QTest::addColumn<float>(name: "x3");
1371 QTest::addColumn<float>(name: "y3");
1372 QTest::addColumn<float>(name: "z3");
1373 QTest::addColumn<float>(name: "w3");
1374
1375 QTest::newRow(dataTag: "null")
1376 << 0.0f << 0.0f << 0.0f << 0.0f
1377 << 0.0f << 0.0f << 0.0f << 0.0f
1378 << 0.0f << 0.0f << 0.0f << 0.0f;
1379
1380 QTest::newRow(dataTag: "xonly")
1381 << 1.0f << 0.0f << 0.0f << 0.0f
1382 << 2.0f << 0.0f << 0.0f << 0.0f
1383 << 2.0f << 0.0f << 0.0f << 0.0f;
1384
1385 QTest::newRow(dataTag: "yonly")
1386 << 0.0f << 1.0f << 0.0f << 0.0f
1387 << 0.0f << 2.0f << 0.0f << 0.0f
1388 << 0.0f << 2.0f << 0.0f << 0.0f;
1389
1390 QTest::newRow(dataTag: "zonly")
1391 << 0.0f << 0.0f << 1.0f << 0.0f
1392 << 0.0f << 0.0f << 2.0f << 0.0f
1393 << 0.0f << 0.0f << 2.0f << 0.0f;
1394
1395 QTest::newRow(dataTag: "wonly")
1396 << 0.0f << 0.0f << 0.0f << 1.0f
1397 << 0.0f << 0.0f << 0.0f << 2.0f
1398 << 0.0f << 0.0f << 0.0f << 2.0f;
1399
1400 QTest::newRow(dataTag: "all")
1401 << 1.0f << 2.0f << 3.0f << 8.0f
1402 << 4.0f << 5.0f << -6.0f << 9.0f
1403 << 4.0f << 10.0f << -18.0f << 72.0f;
1404}
1405void tst_QVectorND::multiply4()
1406{
1407 QFETCH(float, x1);
1408 QFETCH(float, y1);
1409 QFETCH(float, z1);
1410 QFETCH(float, w1);
1411 QFETCH(float, x2);
1412 QFETCH(float, y2);
1413 QFETCH(float, z2);
1414 QFETCH(float, w2);
1415 QFETCH(float, x3);
1416 QFETCH(float, y3);
1417 QFETCH(float, z3);
1418 QFETCH(float, w3);
1419
1420 QVector4D v1(x1, y1, z1, w1);
1421 QVector4D v2(x2, y2, z2, w2);
1422 QVector4D v3(x3, y3, z3, w3);
1423
1424 QVERIFY((v1 * v2) == v3);
1425
1426 QVector4D v4(v1);
1427 v4 *= v2;
1428 QCOMPARE(v4, v3);
1429
1430 QCOMPARE(v4.x(), v1.x() * v2.x());
1431 QCOMPARE(v4.y(), v1.y() * v2.y());
1432 QCOMPARE(v4.z(), v1.z() * v2.z());
1433 QCOMPARE(v4.w(), v1.w() * v2.w());
1434}
1435
1436// Test vector multiplication by a factor for 2D vectors.
1437void tst_QVectorND::multiplyFactor2_data()
1438{
1439 QTest::addColumn<float>(name: "x1");
1440 QTest::addColumn<float>(name: "y1");
1441 QTest::addColumn<float>(name: "factor");
1442 QTest::addColumn<float>(name: "x2");
1443 QTest::addColumn<float>(name: "y2");
1444
1445 QTest::newRow(dataTag: "null")
1446 << 0.0f << 0.0f
1447 << 100.0f
1448 << 0.0f << 0.0f;
1449
1450 QTest::newRow(dataTag: "xonly")
1451 << 1.0f << 0.0f
1452 << 2.0f
1453 << 2.0f << 0.0f;
1454
1455 QTest::newRow(dataTag: "yonly")
1456 << 0.0f << 1.0f
1457 << 2.0f
1458 << 0.0f << 2.0f;
1459
1460 QTest::newRow(dataTag: "all")
1461 << 1.0f << 2.0f
1462 << 2.0f
1463 << 2.0f << 4.0f;
1464
1465 QTest::newRow(dataTag: "allzero")
1466 << 1.0f << 2.0f
1467 << 0.0f
1468 << 0.0f << 0.0f;
1469}
1470void tst_QVectorND::multiplyFactor2()
1471{
1472 QFETCH(float, x1);
1473 QFETCH(float, y1);
1474 QFETCH(float, factor);
1475 QFETCH(float, x2);
1476 QFETCH(float, y2);
1477
1478 QVector2D v1(x1, y1);
1479 QVector2D v2(x2, y2);
1480
1481 QVERIFY((v1 * factor) == v2);
1482 QVERIFY((factor * v1) == v2);
1483
1484 QVector2D v3(v1);
1485 v3 *= factor;
1486 QCOMPARE(v3, v2);
1487
1488 QCOMPARE(v3.x(), v1.x() * factor);
1489 QCOMPARE(v3.y(), v1.y() * factor);
1490}
1491
1492// Test vector multiplication by a factor for 3D vectors.
1493void tst_QVectorND::multiplyFactor3_data()
1494{
1495 QTest::addColumn<float>(name: "x1");
1496 QTest::addColumn<float>(name: "y1");
1497 QTest::addColumn<float>(name: "z1");
1498 QTest::addColumn<float>(name: "factor");
1499 QTest::addColumn<float>(name: "x2");
1500 QTest::addColumn<float>(name: "y2");
1501 QTest::addColumn<float>(name: "z2");
1502
1503 QTest::newRow(dataTag: "null")
1504 << 0.0f << 0.0f << 0.0f
1505 << 100.0f
1506 << 0.0f << 0.0f << 0.0f;
1507
1508 QTest::newRow(dataTag: "xonly")
1509 << 1.0f << 0.0f << 0.0f
1510 << 2.0f
1511 << 2.0f << 0.0f << 0.0f;
1512
1513 QTest::newRow(dataTag: "yonly")
1514 << 0.0f << 1.0f << 0.0f
1515 << 2.0f
1516 << 0.0f << 2.0f << 0.0f;
1517
1518 QTest::newRow(dataTag: "zonly")
1519 << 0.0f << 0.0f << 1.0f
1520 << 2.0f
1521 << 0.0f << 0.0f << 2.0f;
1522
1523 QTest::newRow(dataTag: "all")
1524 << 1.0f << 2.0f << -3.0f
1525 << 2.0f
1526 << 2.0f << 4.0f << -6.0f;
1527
1528 QTest::newRow(dataTag: "allzero")
1529 << 1.0f << 2.0f << -3.0f
1530 << 0.0f
1531 << 0.0f << 0.0f << 0.0f;
1532}
1533void tst_QVectorND::multiplyFactor3()
1534{
1535 QFETCH(float, x1);
1536 QFETCH(float, y1);
1537 QFETCH(float, z1);
1538 QFETCH(float, factor);
1539 QFETCH(float, x2);
1540 QFETCH(float, y2);
1541 QFETCH(float, z2);
1542
1543 QVector3D v1(x1, y1, z1);
1544 QVector3D v2(x2, y2, z2);
1545
1546 QVERIFY((v1 * factor) == v2);
1547 QVERIFY((factor * v1) == v2);
1548
1549 QVector3D v3(v1);
1550 v3 *= factor;
1551 QCOMPARE(v3, v2);
1552
1553 QCOMPARE(v3.x(), v1.x() * factor);
1554 QCOMPARE(v3.y(), v1.y() * factor);
1555 QCOMPARE(v3.z(), v1.z() * factor);
1556}
1557
1558// Test vector multiplication by a factor for 4D vectors.
1559void tst_QVectorND::multiplyFactor4_data()
1560{
1561 QTest::addColumn<float>(name: "x1");
1562 QTest::addColumn<float>(name: "y1");
1563 QTest::addColumn<float>(name: "z1");
1564 QTest::addColumn<float>(name: "w1");
1565 QTest::addColumn<float>(name: "factor");
1566 QTest::addColumn<float>(name: "x2");
1567 QTest::addColumn<float>(name: "y2");
1568 QTest::addColumn<float>(name: "z2");
1569 QTest::addColumn<float>(name: "w2");
1570
1571 QTest::newRow(dataTag: "null")
1572 << 0.0f << 0.0f << 0.0f << 0.0f
1573 << 100.0f
1574 << 0.0f << 0.0f << 0.0f << 0.0f;
1575
1576 QTest::newRow(dataTag: "xonly")
1577 << 1.0f << 0.0f << 0.0f << 0.0f
1578 << 2.0f
1579 << 2.0f << 0.0f << 0.0f << 0.0f;
1580
1581 QTest::newRow(dataTag: "yonly")
1582 << 0.0f << 1.0f << 0.0f << 0.0f
1583 << 2.0f
1584 << 0.0f << 2.0f << 0.0f << 0.0f;
1585
1586 QTest::newRow(dataTag: "zonly")
1587 << 0.0f << 0.0f << 1.0f << 0.0f
1588 << 2.0f
1589 << 0.0f << 0.0f << 2.0f << 0.0f;
1590
1591 QTest::newRow(dataTag: "wonly")
1592 << 0.0f << 0.0f << 0.0f << 1.0f
1593 << 2.0f
1594 << 0.0f << 0.0f << 0.0f << 2.0f;
1595
1596 QTest::newRow(dataTag: "all")
1597 << 1.0f << 2.0f << -3.0f << 4.0f
1598 << 2.0f
1599 << 2.0f << 4.0f << -6.0f << 8.0f;
1600
1601 QTest::newRow(dataTag: "allzero")
1602 << 1.0f << 2.0f << -3.0f << 4.0f
1603 << 0.0f
1604 << 0.0f << 0.0f << 0.0f << 0.0f;
1605}
1606void tst_QVectorND::multiplyFactor4()
1607{
1608 QFETCH(float, x1);
1609 QFETCH(float, y1);
1610 QFETCH(float, z1);
1611 QFETCH(float, w1);
1612 QFETCH(float, factor);
1613 QFETCH(float, x2);
1614 QFETCH(float, y2);
1615 QFETCH(float, z2);
1616 QFETCH(float, w2);
1617
1618 QVector4D v1(x1, y1, z1, w1);
1619 QVector4D v2(x2, y2, z2, w2);
1620
1621 QVERIFY((v1 * factor) == v2);
1622 QVERIFY((factor * v1) == v2);
1623
1624 QVector4D v3(v1);
1625 v3 *= factor;
1626 QCOMPARE(v3, v2);
1627
1628 QCOMPARE(v3.x(), v1.x() * factor);
1629 QCOMPARE(v3.y(), v1.y() * factor);
1630 QCOMPARE(v3.z(), v1.z() * factor);
1631 QCOMPARE(v3.w(), v1.w() * factor);
1632}
1633
1634// Test component-wise vector division for 2D vectors.
1635void tst_QVectorND::divide2_data()
1636{
1637 // Use the same test data as the multiply test.
1638 multiply2_data();
1639}
1640void tst_QVectorND::divide2()
1641{
1642 QFETCH(float, x1);
1643 QFETCH(float, y1);
1644 QFETCH(float, x2);
1645 QFETCH(float, y2);
1646 QFETCH(float, x3);
1647 QFETCH(float, y3);
1648
1649 QVector2D v1(x1, y1);
1650 QVector2D v2(x2, y2);
1651 QVector2D v3(x3, y3);
1652
1653 if (v2.x() != 0.0f && v2.y() != 0.0f) {
1654 QVERIFY((v3 / v2) == v1);
1655
1656 QVector2D v4(v3);
1657 v4 /= v2;
1658 QCOMPARE(v4, v1);
1659
1660 QCOMPARE(v4.x(), v3.x() / v2.x());
1661 QCOMPARE(v4.y(), v3.y() / v2.y());
1662 }
1663 if (v1.x() != 0.0f && v1.y() != 0.0f) {
1664 QVERIFY((v3 / v1) == v2);
1665
1666 QVector2D v4(v3);
1667 v4 /= v1;
1668 QCOMPARE(v4, v2);
1669
1670 QCOMPARE(v4.x(), v3.x() / v1.x());
1671 QCOMPARE(v4.y(), v3.y() / v1.y());
1672 }
1673}
1674
1675// Test component-wise vector division for 3D vectors.
1676void tst_QVectorND::divide3_data()
1677{
1678 // Use the same test data as the multiply test.
1679 multiply3_data();
1680}
1681void tst_QVectorND::divide3()
1682{
1683 QFETCH(float, x1);
1684 QFETCH(float, y1);
1685 QFETCH(float, z1);
1686 QFETCH(float, x2);
1687 QFETCH(float, y2);
1688 QFETCH(float, z2);
1689 QFETCH(float, x3);
1690 QFETCH(float, y3);
1691 QFETCH(float, z3);
1692
1693 QVector3D v1(x1, y1, z1);
1694 QVector3D v2(x2, y2, z2);
1695 QVector3D v3(x3, y3, z3);
1696
1697 if (v2.x() != 0.0f && v2.y() != 0.0f && v2.z() != 0.0f) {
1698 QVERIFY((v3 / v2) == v1);
1699
1700 QVector3D v4(v3);
1701 v4 /= v2;
1702 QCOMPARE(v4, v1);
1703
1704 QCOMPARE(v4.x(), v3.x() / v2.x());
1705 QCOMPARE(v4.y(), v3.y() / v2.y());
1706 QCOMPARE(v4.z(), v3.z() / v2.z());
1707 }
1708 if (v1.x() != 0.0f && v1.y() != 0.0f && v1.z() != 0.0f) {
1709 QVERIFY((v3 / v1) == v2);
1710
1711 QVector3D v4(v3);
1712 v4 /= v1;
1713 QCOMPARE(v4, v2);
1714
1715 QCOMPARE(v4.x(), v3.x() / v1.x());
1716 QCOMPARE(v4.y(), v3.y() / v1.y());
1717 QCOMPARE(v4.z(), v3.z() / v1.z());
1718 }
1719}
1720
1721// Test component-wise vector division for 4D vectors.
1722void tst_QVectorND::divide4_data()
1723{
1724 // Use the same test data as the multiply test.
1725 multiply4_data();
1726}
1727void tst_QVectorND::divide4()
1728{
1729 QFETCH(float, x1);
1730 QFETCH(float, y1);
1731 QFETCH(float, z1);
1732 QFETCH(float, w1);
1733 QFETCH(float, x2);
1734 QFETCH(float, y2);
1735 QFETCH(float, z2);
1736 QFETCH(float, w2);
1737 QFETCH(float, x3);
1738 QFETCH(float, y3);
1739 QFETCH(float, z3);
1740 QFETCH(float, w3);
1741
1742 QVector4D v1(x1, y1, z1, w1);
1743 QVector4D v2(x2, y2, z2, w2);
1744 QVector4D v3(x3, y3, z3, w3);
1745
1746 if (v2.x() != 0.0f && v2.y() != 0.0f && v2.z() != 0.0f && v2.w() != 0.0f) {
1747 QVERIFY((v3 / v2) == v1);
1748
1749 QVector4D v4(v3);
1750 v4 /= v2;
1751 QCOMPARE(v4, v1);
1752
1753 QCOMPARE(v4.x(), v3.x() / v2.x());
1754 QCOMPARE(v4.y(), v3.y() / v2.y());
1755 QCOMPARE(v4.z(), v3.z() / v2.z());
1756 QCOMPARE(v4.w(), v3.w() / v2.w());
1757 }
1758 if (v1.x() != 0.0f && v1.y() != 0.0f && v1.z() != 0.0f && v1.w() != 0.0f) {
1759 QVERIFY((v3 / v1) == v2);
1760
1761 QVector4D v4(v3);
1762 v4 /= v1;
1763 QCOMPARE(v4, v2);
1764
1765 QCOMPARE(v4.x(), v3.x() / v1.x());
1766 QCOMPARE(v4.y(), v3.y() / v1.y());
1767 QCOMPARE(v4.z(), v3.z() / v1.z());
1768 QCOMPARE(v4.w(), v3.w() / v1.w());
1769 }
1770}
1771
1772// Test vector division by a factor for 2D vectors.
1773void tst_QVectorND::divideFactor2_data()
1774{
1775 // Use the same test data as the multiplyFactor test.
1776 multiplyFactor2_data();
1777}
1778void tst_QVectorND::divideFactor2()
1779{
1780 QFETCH(float, x1);
1781 QFETCH(float, y1);
1782 QFETCH(float, factor);
1783 QFETCH(float, x2);
1784 QFETCH(float, y2);
1785
1786 QVector2D v1(x1, y1);
1787 QVector2D v2(x2, y2);
1788
1789 if (factor == 0.0f)
1790 return;
1791
1792 QVERIFY((v2 / factor) == v1);
1793
1794 QVector2D v3(v2);
1795 v3 /= factor;
1796 QCOMPARE(v3, v1);
1797
1798 QCOMPARE(v3.x(), v2.x() / factor);
1799 QCOMPARE(v3.y(), v2.y() / factor);
1800}
1801
1802// Test vector division by a factor for 3D vectors.
1803void tst_QVectorND::divideFactor3_data()
1804{
1805 // Use the same test data as the multiplyFactor test.
1806 multiplyFactor3_data();
1807}
1808void tst_QVectorND::divideFactor3()
1809{
1810 QFETCH(float, x1);
1811 QFETCH(float, y1);
1812 QFETCH(float, z1);
1813 QFETCH(float, factor);
1814 QFETCH(float, x2);
1815 QFETCH(float, y2);
1816 QFETCH(float, z2);
1817
1818 QVector3D v1(x1, y1, z1);
1819 QVector3D v2(x2, y2, z2);
1820
1821 if (factor == 0.0f)
1822 return;
1823
1824 QVERIFY((v2 / factor) == v1);
1825
1826 QVector3D v3(v2);
1827 v3 /= factor;
1828 QCOMPARE(v3, v1);
1829
1830 QCOMPARE(v3.x(), v2.x() / factor);
1831 QCOMPARE(v3.y(), v2.y() / factor);
1832 QCOMPARE(v3.z(), v2.z() / factor);
1833}
1834
1835// Test vector division by a factor for 4D vectors.
1836void tst_QVectorND::divideFactor4_data()
1837{
1838 // Use the same test data as the multiplyFactor test.
1839 multiplyFactor4_data();
1840}
1841void tst_QVectorND::divideFactor4()
1842{
1843 QFETCH(float, x1);
1844 QFETCH(float, y1);
1845 QFETCH(float, z1);
1846 QFETCH(float, w1);
1847 QFETCH(float, factor);
1848 QFETCH(float, x2);
1849 QFETCH(float, y2);
1850 QFETCH(float, z2);
1851 QFETCH(float, w2);
1852
1853 QVector4D v1(x1, y1, z1, w1);
1854 QVector4D v2(x2, y2, z2, w2);
1855
1856 if (factor == 0.0f)
1857 return;
1858
1859 QVERIFY((v2 / factor) == v1);
1860
1861 QVector4D v3(v2);
1862 v3 /= factor;
1863 QCOMPARE(v3, v1);
1864
1865 QCOMPARE(v3.x(), v2.x() / factor);
1866 QCOMPARE(v3.y(), v2.y() / factor);
1867 QCOMPARE(v3.z(), v2.z() / factor);
1868 QCOMPARE(v3.w(), v2.w() / factor);
1869}
1870
1871// Test vector negation for 2D vectors.
1872void tst_QVectorND::negate2_data()
1873{
1874 // Use the same test data as the add test.
1875 add2_data();
1876}
1877void tst_QVectorND::negate2()
1878{
1879 QFETCH(float, x1);
1880 QFETCH(float, y1);
1881
1882 QVector2D v1(x1, y1);
1883 QVector2D v2(-x1, -y1);
1884
1885 QCOMPARE(-v1, v2);
1886}
1887
1888// Test vector negation for 3D vectors.
1889void tst_QVectorND::negate3_data()
1890{
1891 // Use the same test data as the add test.
1892 add3_data();
1893}
1894void tst_QVectorND::negate3()
1895{
1896 QFETCH(float, x1);
1897 QFETCH(float, y1);
1898 QFETCH(float, z1);
1899
1900 QVector3D v1(x1, y1, z1);
1901 QVector3D v2(-x1, -y1, -z1);
1902
1903 QCOMPARE(-v1, v2);
1904}
1905
1906// Test vector negation for 4D vectors.
1907void tst_QVectorND::negate4_data()
1908{
1909 // Use the same test data as the add test.
1910 add4_data();
1911}
1912void tst_QVectorND::negate4()
1913{
1914 QFETCH(float, x1);
1915 QFETCH(float, y1);
1916 QFETCH(float, z1);
1917 QFETCH(float, w1);
1918
1919 QVector4D v1(x1, y1, z1, w1);
1920 QVector4D v2(-x1, -y1, -z1, -w1);
1921
1922 QCOMPARE(-v1, v2);
1923}
1924
1925// Test the computation of vector cross-products.
1926void tst_QVectorND::crossProduct_data()
1927{
1928 QTest::addColumn<float>(name: "x1");
1929 QTest::addColumn<float>(name: "y1");
1930 QTest::addColumn<float>(name: "z1");
1931 QTest::addColumn<float>(name: "x2");
1932 QTest::addColumn<float>(name: "y2");
1933 QTest::addColumn<float>(name: "z2");
1934 QTest::addColumn<float>(name: "x3");
1935 QTest::addColumn<float>(name: "y3");
1936 QTest::addColumn<float>(name: "z3");
1937 QTest::addColumn<float>(name: "dot");
1938
1939 QTest::newRow(dataTag: "null")
1940 << 0.0f << 0.0f << 0.0f
1941 << 0.0f << 0.0f << 0.0f
1942 << 0.0f << 0.0f << 0.0f
1943 << 0.0f;
1944
1945 QTest::newRow(dataTag: "unitvec")
1946 << 1.0f << 0.0f << 0.0f
1947 << 0.0f << 1.0f << 0.0f
1948 << 0.0f << 0.0f << 1.0f
1949 << 0.0f;
1950
1951 QTest::newRow(dataTag: "complex")
1952 << 1.0f << 2.0f << 3.0f
1953 << 4.0f << 5.0f << 6.0f
1954 << -3.0f << 6.0f << -3.0f
1955 << 32.0f;
1956}
1957void tst_QVectorND::crossProduct()
1958{
1959 QFETCH(float, x1);
1960 QFETCH(float, y1);
1961 QFETCH(float, z1);
1962 QFETCH(float, x2);
1963 QFETCH(float, y2);
1964 QFETCH(float, z2);
1965 QFETCH(float, x3);
1966 QFETCH(float, y3);
1967 QFETCH(float, z3);
1968
1969 QVector3D v1(x1, y1, z1);
1970 QVector3D v2(x2, y2, z2);
1971 QVector3D v3(x3, y3, z3);
1972
1973 QVector3D v4 = QVector3D::crossProduct(v1, v2);
1974 QCOMPARE(v4, v3);
1975
1976 // Compute the cross-product long-hand and check again.
1977 float xres = y1 * z2 - z1 * y2;
1978 float yres = z1 * x2 - x1 * z2;
1979 float zres = x1 * y2 - y1 * x2;
1980
1981 QCOMPARE(v4.x(), xres);
1982 QCOMPARE(v4.y(), yres);
1983 QCOMPARE(v4.z(), zres);
1984}
1985
1986// Test the computation of normals.
1987void tst_QVectorND::normal_data()
1988{
1989 // Use the same test data as the crossProduct test.
1990 crossProduct_data();
1991}
1992void tst_QVectorND::normal()
1993{
1994 QFETCH(float, x1);
1995 QFETCH(float, y1);
1996 QFETCH(float, z1);
1997 QFETCH(float, x2);
1998 QFETCH(float, y2);
1999 QFETCH(float, z2);
2000 QFETCH(float, x3);
2001 QFETCH(float, y3);
2002 QFETCH(float, z3);
2003
2004 QVector3D v1(x1, y1, z1);
2005 QVector3D v2(x2, y2, z2);
2006 QVector3D v3(x3, y3, z3);
2007
2008 QVERIFY(QVector3D::normal(v1, v2) == v3.normalized());
2009 QVERIFY(QVector3D::normal(QVector3D(), v1, v2) == v3.normalized());
2010
2011 QVector3D point(1.0f, 2.0f, 3.0f);
2012 QVERIFY(QVector3D::normal(point, v1 + point, v2 + point) == v3.normalized());
2013}
2014
2015// Test distance to point calculations.
2016void tst_QVectorND::distanceToPoint2_data()
2017{
2018 QTest::addColumn<float>(name: "x1"); // Point to test for distance
2019 QTest::addColumn<float>(name: "y1");
2020 QTest::addColumn<float>(name: "x2"); // Point to test against
2021 QTest::addColumn<float>(name: "y2");
2022
2023 QTest::addColumn<float>(name: "distance");
2024
2025 QTest::newRow(dataTag: "null")
2026 << 0.0f << 0.0f
2027 << 0.0f << 1.0f
2028 << 1.0f;
2029
2030 QTest::newRow(dataTag: "on point")
2031 << 1.0f << 1.0f
2032 << 1.0f << 1.0f
2033 << 0.0f;
2034
2035 QTest::newRow(dataTag: "off point")
2036 << 0.0f << 1.0f
2037 << 0.0f << 2.0f
2038 << 1.0f;
2039
2040 QTest::newRow(dataTag: "off point 2")
2041 << 0.0f << 0.0f
2042 << 0.0f << 2.0f
2043 << 2.0f;
2044
2045 QTest::newRow(dataTag: "minus point")
2046 << 0.0f << 0.0f
2047 << 0.0f << -2.0f
2048 << 2.0f;
2049}
2050void tst_QVectorND::distanceToPoint2()
2051{
2052 QFETCH(float, x1);
2053 QFETCH(float, y1);
2054 QFETCH(float, x2);
2055 QFETCH(float, y2);
2056 QFETCH(float, distance);
2057
2058 QVector2D v1(x1, y1);
2059 QVector2D v2(x2, y2);
2060
2061 QCOMPARE(v1.distanceToPoint(v2), distance);
2062}
2063
2064// Test distance to point calculations.
2065void tst_QVectorND::distanceToPoint3_data()
2066{
2067 QTest::addColumn<float>(name: "x1"); // Point to test for distance
2068 QTest::addColumn<float>(name: "y1");
2069 QTest::addColumn<float>(name: "z1");
2070 QTest::addColumn<float>(name: "x2"); // Point to test against
2071 QTest::addColumn<float>(name: "y2");
2072 QTest::addColumn<float>(name: "z2");
2073
2074 QTest::addColumn<float>(name: "distance");
2075
2076 QTest::newRow(dataTag: "null")
2077 << 0.0f << 0.0f << 0.0f
2078 << 0.0f << 0.0f << 1.0f
2079 << 1.0f;
2080
2081 QTest::newRow(dataTag: "on point")
2082 << 0.0f << 0.0f << 0.0f
2083 << 0.0f << 0.0f << 0.0f
2084 << 0.0f;
2085
2086 QTest::newRow(dataTag: "off point")
2087 << 0.0f << 0.0f << 1.0f
2088 << 0.0f << 0.0f << 2.0f
2089 << 1.0f;
2090
2091 QTest::newRow(dataTag: "off point 2")
2092 << 0.0f << 0.0f << 0.0f
2093 << 0.0f << 2.0f << 0.0f
2094 << 2.0f;
2095
2096 QTest::newRow(dataTag: "minus point")
2097 << 0.0f << 0.0f << 0.0f
2098 << 0.0f << -2.0f << 0.0f
2099 << 2.0f;
2100}
2101void tst_QVectorND::distanceToPoint3()
2102{
2103 QFETCH(float, x1);
2104 QFETCH(float, y1);
2105 QFETCH(float, z1);
2106 QFETCH(float, x2);
2107 QFETCH(float, y2);
2108 QFETCH(float, z2);
2109 QFETCH(float, distance);
2110
2111 QVector3D v1(x1, y1, z1);
2112 QVector3D v2(x2, y2, z2);
2113
2114 QCOMPARE(v1.distanceToPoint(v2), distance);
2115}
2116
2117// Test distance to plane calculations.
2118void tst_QVectorND::distanceToPlane_data()
2119{
2120 QTest::addColumn<float>(name: "x1"); // Point on plane
2121 QTest::addColumn<float>(name: "y1");
2122 QTest::addColumn<float>(name: "z1");
2123 QTest::addColumn<float>(name: "x2"); // Normal to plane
2124 QTest::addColumn<float>(name: "y2");
2125 QTest::addColumn<float>(name: "z2");
2126 QTest::addColumn<float>(name: "x3"); // Point to test for distance
2127 QTest::addColumn<float>(name: "y3");
2128 QTest::addColumn<float>(name: "z3");
2129 QTest::addColumn<float>(name: "x4"); // Second point on plane
2130 QTest::addColumn<float>(name: "y4");
2131 QTest::addColumn<float>(name: "z4");
2132 QTest::addColumn<float>(name: "x5"); // Third point on plane
2133 QTest::addColumn<float>(name: "y5");
2134 QTest::addColumn<float>(name: "z5");
2135 QTest::addColumn<float>(name: "distance");
2136
2137 QTest::newRow(dataTag: "null")
2138 << 0.0f << 0.0f << 0.0f
2139 << 0.0f << 0.0f << 1.0f
2140 << 0.0f << 0.0f << 0.0f
2141 << 1.0f << 0.0f << 0.0f
2142 << 0.0f << 2.0f << 0.0f
2143 << 0.0f;
2144
2145 QTest::newRow(dataTag: "above")
2146 << 0.0f << 0.0f << 0.0f
2147 << 0.0f << 0.0f << 1.0f
2148 << 0.0f << 0.0f << 2.0f
2149 << 1.0f << 0.0f << 0.0f
2150 << 0.0f << 2.0f << 0.0f
2151 << 2.0f;
2152
2153 QTest::newRow(dataTag: "below")
2154 << 0.0f << 0.0f << 0.0f
2155 << 0.0f << 0.0f << 1.0f
2156 << -1.0f << 1.0f << -2.0f
2157 << 1.0f << 0.0f << 0.0f
2158 << 0.0f << 2.0f << 0.0f
2159 << -2.0f;
2160}
2161void tst_QVectorND::distanceToPlane()
2162{
2163 QFETCH(float, x1);
2164 QFETCH(float, y1);
2165 QFETCH(float, z1);
2166 QFETCH(float, x2);
2167 QFETCH(float, y2);
2168 QFETCH(float, z2);
2169 QFETCH(float, x3);
2170 QFETCH(float, y3);
2171 QFETCH(float, z3);
2172 QFETCH(float, x4);
2173 QFETCH(float, y4);
2174 QFETCH(float, z4);
2175 QFETCH(float, x5);
2176 QFETCH(float, y5);
2177 QFETCH(float, z5);
2178 QFETCH(float, distance);
2179
2180 QVector3D v1(x1, y1, z1);
2181 QVector3D v2(x2, y2, z2);
2182 QVector3D v3(x3, y3, z3);
2183 QVector3D v4(x4, y4, z4);
2184 QVector3D v5(x5, y5, z5);
2185
2186 QCOMPARE(v3.distanceToPlane(v1, v2), distance);
2187 QCOMPARE(v3.distanceToPlane(v1, v4, v5), distance);
2188}
2189
2190// Test distance to line calculations.
2191void tst_QVectorND::distanceToLine2_data()
2192{
2193 QTest::addColumn<float>(name: "x1"); // Point on line
2194 QTest::addColumn<float>(name: "y1");
2195 QTest::addColumn<float>(name: "x2"); // Direction of the line
2196 QTest::addColumn<float>(name: "y2");
2197 QTest::addColumn<float>(name: "x3"); // Point to test for distance
2198 QTest::addColumn<float>(name: "y3");
2199 QTest::addColumn<float>(name: "distance");
2200
2201 QTest::newRow(dataTag: "null")
2202 << 0.0f << 0.0f
2203 << 0.0f << 0.1f
2204 << 0.0f << 0.0f
2205 << 0.0f;
2206
2207 QTest::newRow(dataTag: "on line")
2208 << 0.0f << 0.0f
2209 << 0.0f << 1.0f
2210 << 0.0f << 5.0f
2211 << 0.0f;
2212
2213 QTest::newRow(dataTag: "off line")
2214 << 0.0f << 0.0f
2215 << 0.0f << 1.0f
2216 << 1.0f << 0.0f
2217 << 1.0f;
2218
2219 QTest::newRow(dataTag: "off line 2")
2220 << 0.0f << 0.0f
2221 << 0.0f << 1.0f
2222 << -2.0f << 0.0f
2223 << 2.0f;
2224
2225 QTest::newRow(dataTag: "points")
2226 << 0.0f << 0.0f
2227 << 0.0f << 0.0f
2228 << 0.0f << 5.0f
2229 << 5.0f;
2230}
2231
2232void tst_QVectorND::distanceToLine2()
2233{
2234 QFETCH(float, x1);
2235 QFETCH(float, y1);
2236 QFETCH(float, x2);
2237 QFETCH(float, y2);
2238 QFETCH(float, x3);
2239 QFETCH(float, y3);
2240 QFETCH(float, distance);
2241
2242 QVector2D v1(x1, y1);
2243 QVector2D v2(x2, y2);
2244 QVector2D v3(x3, y3);
2245
2246 QCOMPARE(v3.distanceToLine(v1, v2), distance);
2247}
2248// Test distance to line calculations.
2249void tst_QVectorND::distanceToLine3_data()
2250{
2251 QTest::addColumn<float>(name: "x1"); // Point on line
2252 QTest::addColumn<float>(name: "y1");
2253 QTest::addColumn<float>(name: "z1");
2254 QTest::addColumn<float>(name: "x2"); // Direction of the line
2255 QTest::addColumn<float>(name: "y2");
2256 QTest::addColumn<float>(name: "z2");
2257 QTest::addColumn<float>(name: "x3"); // Point to test for distance
2258 QTest::addColumn<float>(name: "y3");
2259 QTest::addColumn<float>(name: "z3");
2260 QTest::addColumn<float>(name: "distance");
2261
2262 QTest::newRow(dataTag: "null")
2263 << 0.0f << 0.0f << 0.0f
2264 << 0.0f << 0.0f << 1.0f
2265 << 0.0f << 0.0f << 0.0f
2266 << 0.0f;
2267
2268 QTest::newRow(dataTag: "on line")
2269 << 0.0f << 0.0f << 0.0f
2270 << 0.0f << 0.0f << 1.0f
2271 << 0.0f << 0.0f << 5.0f
2272 << 0.0f;
2273
2274 QTest::newRow(dataTag: "off line")
2275 << 0.0f << 0.0f << 0.0f
2276 << 0.0f << 0.0f << 1.0f
2277 << 1.0f << 0.0f << 0.0f
2278 << 1.0f;
2279
2280 QTest::newRow(dataTag: "off line 2")
2281 << 0.0f << 0.0f << 0.0f
2282 << 0.0f << 0.0f << 1.0f
2283 << 0.0f << -2.0f << 0.0f
2284 << 2.0f;
2285
2286 QTest::newRow(dataTag: "points")
2287 << 0.0f << 0.0f << 0.0f
2288 << 0.0f << 0.0f << 0.0f
2289 << 0.0f << 5.0f << 0.0f
2290 << 5.0f;
2291}
2292void tst_QVectorND::distanceToLine3()
2293{
2294 QFETCH(float, x1);
2295 QFETCH(float, y1);
2296 QFETCH(float, z1);
2297 QFETCH(float, x2);
2298 QFETCH(float, y2);
2299 QFETCH(float, z2);
2300 QFETCH(float, x3);
2301 QFETCH(float, y3);
2302 QFETCH(float, z3);
2303 QFETCH(float, distance);
2304
2305 QVector3D v1(x1, y1, z1);
2306 QVector3D v2(x2, y2, z2);
2307 QVector3D v3(x3, y3, z3);
2308
2309 QCOMPARE(v3.distanceToLine(v1, v2), distance);
2310}
2311
2312// Test the computation of dot products for 2D vectors.
2313void tst_QVectorND::dotProduct2_data()
2314{
2315 QTest::addColumn<float>(name: "x1");
2316 QTest::addColumn<float>(name: "y1");
2317 QTest::addColumn<float>(name: "x2");
2318 QTest::addColumn<float>(name: "y2");
2319 QTest::addColumn<float>(name: "dot");
2320
2321 QTest::newRow(dataTag: "null")
2322 << 0.0f << 0.0f
2323 << 0.0f << 0.0f
2324 << 0.0f;
2325
2326 QTest::newRow(dataTag: "unitvec")
2327 << 1.0f << 0.0f
2328 << 0.0f << 1.0f
2329 << 0.0f;
2330
2331 QTest::newRow(dataTag: "complex")
2332 << 1.0f << 2.0f
2333 << 4.0f << 5.0f
2334 << 14.0f;
2335}
2336void tst_QVectorND::dotProduct2()
2337{
2338 QFETCH(float, x1);
2339 QFETCH(float, y1);
2340 QFETCH(float, x2);
2341 QFETCH(float, y2);
2342 QFETCH(float, dot);
2343
2344 QVector2D v1(x1, y1);
2345 QVector2D v2(x2, y2);
2346
2347 QVERIFY(QVector2D::dotProduct(v1, v2) == dot);
2348
2349 // Compute the dot-product long-hand and check again.
2350 float d = x1 * x2 + y1 * y2;
2351
2352 QCOMPARE(QVector2D::dotProduct(v1, v2), d);
2353}
2354
2355// Test the computation of dot products for 3D vectors.
2356void tst_QVectorND::dotProduct3_data()
2357{
2358 // Use the same test data as the crossProduct test.
2359 crossProduct_data();
2360}
2361void tst_QVectorND::dotProduct3()
2362{
2363 QFETCH(float, x1);
2364 QFETCH(float, y1);
2365 QFETCH(float, z1);
2366 QFETCH(float, x2);
2367 QFETCH(float, y2);
2368 QFETCH(float, z2);
2369 QFETCH(float, x3);
2370 QFETCH(float, y3);
2371 QFETCH(float, z3);
2372 QFETCH(float, dot);
2373
2374 Q_UNUSED(x3);
2375 Q_UNUSED(y3);
2376 Q_UNUSED(z3);
2377
2378 QVector3D v1(x1, y1, z1);
2379 QVector3D v2(x2, y2, z2);
2380
2381 QVERIFY(QVector3D::dotProduct(v1, v2) == dot);
2382
2383 // Compute the dot-product long-hand and check again.
2384 float d = x1 * x2 + y1 * y2 + z1 * z2;
2385
2386 QCOMPARE(QVector3D::dotProduct(v1, v2), d);
2387}
2388
2389// Test the computation of dot products for 4D vectors.
2390void tst_QVectorND::dotProduct4_data()
2391{
2392 QTest::addColumn<float>(name: "x1");
2393 QTest::addColumn<float>(name: "y1");
2394 QTest::addColumn<float>(name: "z1");
2395 QTest::addColumn<float>(name: "w1");
2396 QTest::addColumn<float>(name: "x2");
2397 QTest::addColumn<float>(name: "y2");
2398 QTest::addColumn<float>(name: "z2");
2399 QTest::addColumn<float>(name: "w2");
2400 QTest::addColumn<float>(name: "dot");
2401
2402 QTest::newRow(dataTag: "null")
2403 << 0.0f << 0.0f << 0.0f << 0.0f
2404 << 0.0f << 0.0f << 0.0f << 0.0f
2405 << 0.0f;
2406
2407 QTest::newRow(dataTag: "unitvec")
2408 << 1.0f << 0.0f << 0.0f << 0.0f
2409 << 0.0f << 1.0f << 0.0f << 0.0f
2410 << 0.0f;
2411
2412 QTest::newRow(dataTag: "complex")
2413 << 1.0f << 2.0f << 3.0f << 4.0f
2414 << 4.0f << 5.0f << 6.0f << 7.0f
2415 << 60.0f;
2416}
2417void tst_QVectorND::dotProduct4()
2418{
2419 QFETCH(float, x1);
2420 QFETCH(float, y1);
2421 QFETCH(float, z1);
2422 QFETCH(float, w1);
2423 QFETCH(float, x2);
2424 QFETCH(float, y2);
2425 QFETCH(float, z2);
2426 QFETCH(float, w2);
2427 QFETCH(float, dot);
2428
2429 QVector4D v1(x1, y1, z1, w1);
2430 QVector4D v2(x2, y2, z2, w2);
2431
2432 QVERIFY(QVector4D::dotProduct(v1, v2) == dot);
2433
2434 // Compute the dot-product long-hand and check again.
2435 float d = x1 * x2 + y1 * y2 + z1 * z2 + w1 * w2;
2436
2437 QCOMPARE(QVector4D::dotProduct(v1, v2), d);
2438}
2439
2440void tst_QVectorND::project_data()
2441{
2442 QTest::addColumn<QVector3D>(name: "point");
2443 QTest::addColumn<QRect>(name: "viewport");
2444 QTest::addColumn<QMatrix4x4>(name: "projection");
2445 QTest::addColumn<QMatrix4x4>(name: "view");
2446 QTest::addColumn<QVector2D>(name: "result");
2447
2448 QMatrix4x4 projection;
2449 projection.ortho(left: -1.0f, right: 1.0f, bottom: -1.0f, top: 1.0f, nearPlane: 0.1f, farPlane: 1000.0f);
2450
2451 QMatrix4x4 view;
2452 // Located at (0, 0, 10), looking at origin, y is up
2453 view.lookAt(eye: QVector3D(0.0f, 0.0f, 10.0f), center: QVector3D(0.0f, 0.0f, 0.0f), up: QVector3D(0.0f, 1.0f, 0.0f));
2454
2455 QMatrix4x4 nullMatrix(0.0f, 0.0f, 0.0f, 0.0f,
2456 0.0f, 0.0f, 0.0f, 0.0f,
2457 0.0f, 0.0f, 0.0f, 0.0f,
2458 0.0f, 0.0f, 0.0f, 0.0f);
2459
2460 QTest::newRow(dataTag: "center")
2461 << QVector3D(0.0f, 0.0f, 0.0f)
2462 << QRect(0.0f, 0.0f, 800.0f, 600.0f)
2463 << projection
2464 << view
2465 << QVector2D(400.0f, 300.0f);
2466
2467 QTest::newRow(dataTag: "topLeft")
2468 << QVector3D(-1.0f, 1.0f, 0.0f)
2469 << QRect(0.0f, 0.0f, 800.0f, 600.0f)
2470 << projection
2471 << view
2472 << QVector2D(0.0f, 600.0f);
2473
2474 QTest::newRow(dataTag: "topRight")
2475 << QVector3D(1.0f, 1.0f, 0.0f)
2476 << QRect(0.0f, 0.0f, 800.0f, 600.0f)
2477 << projection
2478 << view
2479 << QVector2D(800.0f, 600.0f);
2480
2481 QTest::newRow(dataTag: "bottomLeft")
2482 << QVector3D(-1.0f, -1.0f, 0.0f)
2483 << QRect(0.0f, 0.0f, 800.0f, 600.0f)
2484 << projection
2485 << view
2486 << QVector2D(0.0f, 0.0f);
2487
2488 QTest::newRow(dataTag: "bottomRight")
2489 << QVector3D(1.0f, -1.0f, 0.0f)
2490 << QRect(0.0f, 0.0f, 800.0f, 600.0f)
2491 << projection
2492 << view
2493 << QVector2D(800.0f, 0.0f);
2494
2495 QTest::newRow(dataTag: "nullMatrix")
2496 << QVector3D(0.0f, 0.0f, 0.0f)
2497 << QRect(0.0f, 0.0f, 800.0f, 600.0f)
2498 << nullMatrix
2499 << nullMatrix
2500 << QVector2D(400.0f, 300.0f);
2501}
2502
2503void tst_QVectorND::project()
2504{
2505 QFETCH(QVector3D, point);
2506 QFETCH(QRect, viewport);
2507 QFETCH(QMatrix4x4, projection);
2508 QFETCH(QMatrix4x4, view);
2509 QFETCH(QVector2D, result);
2510
2511 QVector3D project = point.project(modelView: view, projection, viewport);
2512
2513 QCOMPARE(project.toVector2D(), result);
2514}
2515
2516void tst_QVectorND::unproject_data()
2517{
2518 QTest::addColumn<QVector3D>(name: "point");
2519 QTest::addColumn<QRect>(name: "viewport");
2520 QTest::addColumn<QMatrix4x4>(name: "projection");
2521 QTest::addColumn<QMatrix4x4>(name: "view");
2522 QTest::addColumn<QVector3D>(name: "result");
2523
2524 QMatrix4x4 projection;
2525 projection.ortho(left: -1.0f, right: 1.0f, bottom: -1.0f, top: 1.0f, nearPlane: 0.1f, farPlane: 1000.0f);
2526
2527 QMatrix4x4 view;
2528 // Located at (0, 0, 10), looking at origin, y is up
2529 view.lookAt(eye: QVector3D(0.0f, 0.0f, 10.0f), center: QVector3D(0.0f, 0.0f, 0.0f), up: QVector3D(0.0f, 1.0f, 0.0f));
2530
2531 QMatrix4x4 nullMatrix(0.0f, 0.0f, 0.0f, 0.0f,
2532 0.0f, 0.0f, 0.0f, 0.0f,
2533 0.0f, 0.0f, 0.0f, 0.0f,
2534 0.0f, 0.0f, 0.0f, 0.0f);
2535
2536 QTest::newRow(dataTag: "center")
2537 << QVector3D(400.0f, 300.0f, 0.0f)
2538 << QRect(0.0f, 0.0f, 800.0f, 600.0f)
2539 << projection
2540 << view
2541 << QVector3D(0.0f, 0.0f, 9.9f);
2542
2543 QTest::newRow(dataTag: "topLeft")
2544 << QVector3D(0.0f, 600.0f, 0.0f)
2545 << QRect(0.0f, 0.0f, 800.0f, 600.0f)
2546 << projection
2547 << view
2548 << QVector3D(-1.0f, 1.0f, 9.9f);
2549
2550 QTest::newRow(dataTag: "topRight")
2551 << QVector3D(800.0f, 600.0f, 0.0f)
2552 << QRect(0.0f, 0.0f, 800.0f, 600.0f)
2553 << projection
2554 << view
2555 << QVector3D(1.0f, 1.0f, 9.9f);
2556
2557 QTest::newRow(dataTag: "bottomLeft")
2558 << QVector3D(0.0f, 0.0f, 0.0f)
2559 << QRect(0.0f, 0.0f, 800.0f, 600.0f)
2560 << projection
2561 << view
2562 << QVector3D(-1.0, -1.0f, 9.9f);
2563
2564 QTest::newRow(dataTag: "bottomRight")
2565 << QVector3D(800.0f, 0.0f, 0.0f)
2566 << QRect(0.0f, 0.0f, 800.0f, 600.0f)
2567 << projection
2568 << view
2569 << QVector3D(1.0f, -1.0f, 9.9f);
2570
2571 QTest::newRow(dataTag: "nullMatrix")
2572 << QVector3D(400.0f, 300.0f, 0.0f)
2573 << QRect(0.0f, 0.0f, 800.0f, 600.0f)
2574 << nullMatrix
2575 << nullMatrix
2576 << QVector3D(0.0f, 0.0f, -1.0f);
2577
2578}
2579
2580void tst_QVectorND::unproject()
2581{
2582 QFETCH(QVector3D, point);
2583 QFETCH(QRect, viewport);
2584 QFETCH(QMatrix4x4, projection);
2585 QFETCH(QMatrix4x4, view);
2586 QFETCH(QVector3D, result);
2587
2588 QVector3D unproject = point.unproject(modelView: view, projection, viewport);
2589 QVERIFY(qFuzzyCompare(unproject, result));
2590}
2591
2592class tst_QVectorNDProperties : public QObject
2593{
2594 Q_OBJECT
2595 Q_PROPERTY(QVector2D vector2D READ vector2D WRITE setVector2D)
2596 Q_PROPERTY(QVector3D vector3D READ vector3D WRITE setVector3D)
2597 Q_PROPERTY(QVector4D vector4D READ vector4D WRITE setVector4D)
2598public:
2599 tst_QVectorNDProperties(QObject *parent = 0) : QObject(parent) {}
2600
2601 QVector2D vector2D() const { return v2; }
2602 void setVector2D(const QVector2D& value) { v2 = value; }
2603
2604 QVector3D vector3D() const { return v3; }
2605 void setVector3D(const QVector3D& value) { v3 = value; }
2606
2607 QVector4D vector4D() const { return v4; }
2608 void setVector4D(const QVector4D& value) { v4 = value; }
2609
2610private:
2611 QVector2D v2;
2612 QVector3D v3;
2613 QVector4D v4;
2614};
2615
2616// Test getting and setting vector properties via the metaobject system.
2617void tst_QVectorND::properties()
2618{
2619 tst_QVectorNDProperties obj;
2620
2621 obj.setVector2D(QVector2D(1.0f, 2.0f));
2622 obj.setVector3D(QVector3D(3.0f, 4.0f, 5.0f));
2623 obj.setVector4D(QVector4D(6.0f, 7.0f, 8.0f, 9.0f));
2624
2625 QVector2D v2 = qvariant_cast<QVector2D>(v: obj.property(name: "vector2D"));
2626 QCOMPARE(v2.x(), 1.0f);
2627 QCOMPARE(v2.y(), 2.0f);
2628
2629 QVector3D v3 = qvariant_cast<QVector3D>(v: obj.property(name: "vector3D"));
2630 QCOMPARE(v3.x(), 3.0f);
2631 QCOMPARE(v3.y(), 4.0f);
2632 QCOMPARE(v3.z(), 5.0f);
2633
2634 QVector4D v4 = qvariant_cast<QVector4D>(v: obj.property(name: "vector4D"));
2635 QCOMPARE(v4.x(), 6.0f);
2636 QCOMPARE(v4.y(), 7.0f);
2637 QCOMPARE(v4.z(), 8.0f);
2638 QCOMPARE(v4.w(), 9.0f);
2639
2640 obj.setProperty(name: "vector2D",
2641 value: QVariant::fromValue(value: QVector2D(-1.0f, -2.0f)));
2642 obj.setProperty(name: "vector3D",
2643 value: QVariant::fromValue(value: QVector3D(-3.0f, -4.0f, -5.0f)));
2644 obj.setProperty(name: "vector4D",
2645 value: QVariant::fromValue(value: QVector4D(-6.0f, -7.0f, -8.0f, -9.0f)));
2646
2647 v2 = qvariant_cast<QVector2D>(v: obj.property(name: "vector2D"));
2648 QCOMPARE(v2.x(), -1.0f);
2649 QCOMPARE(v2.y(), -2.0f);
2650
2651 v3 = qvariant_cast<QVector3D>(v: obj.property(name: "vector3D"));
2652 QCOMPARE(v3.x(), -3.0f);
2653 QCOMPARE(v3.y(), -4.0f);
2654 QCOMPARE(v3.z(), -5.0f);
2655
2656 v4 = qvariant_cast<QVector4D>(v: obj.property(name: "vector4D"));
2657 QCOMPARE(v4.x(), -6.0f);
2658 QCOMPARE(v4.y(), -7.0f);
2659 QCOMPARE(v4.z(), -8.0f);
2660 QCOMPARE(v4.w(), -9.0f);
2661}
2662
2663void tst_QVectorND::metaTypes()
2664{
2665 QCOMPARE(QMetaType::type("QVector2D"), int(QMetaType::QVector2D));
2666 QCOMPARE(QMetaType::type("QVector3D"), int(QMetaType::QVector3D));
2667 QCOMPARE(QMetaType::type("QVector4D"), int(QMetaType::QVector4D));
2668
2669 QCOMPARE(QByteArray(QMetaType::typeName(QMetaType::QVector2D)),
2670 QByteArray("QVector2D"));
2671 QCOMPARE(QByteArray(QMetaType::typeName(QMetaType::QVector3D)),
2672 QByteArray("QVector3D"));
2673 QCOMPARE(QByteArray(QMetaType::typeName(QMetaType::QVector4D)),
2674 QByteArray("QVector4D"));
2675
2676 QVERIFY(QMetaType::isRegistered(QMetaType::QVector2D));
2677 QVERIFY(QMetaType::isRegistered(QMetaType::QVector3D));
2678 QVERIFY(QMetaType::isRegistered(QMetaType::QVector4D));
2679
2680 QCOMPARE(qMetaTypeId<QVector2D>(), int(QMetaType::QVector2D));
2681 QCOMPARE(qMetaTypeId<QVector3D>(), int(QMetaType::QVector3D));
2682 QCOMPARE(qMetaTypeId<QVector4D>(), int(QMetaType::QVector4D));
2683}
2684
2685QTEST_APPLESS_MAIN(tst_QVectorND)
2686
2687#include "tst_qvectornd.moc"
2688

source code of qtbase/tests/auto/gui/math3d/qvectornd/tst_qvectornd.cpp