1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the $MODULE$ of the Qt Toolkit.
**
** $TROLLTECH_DUAL_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <QtCore/qmath.h>
#include "math3dincludes.h"
class tst_QQuaternion : public QObject
{
Q_OBJECT
public:
tst_QQuaternion() {}
~tst_QQuaternion() {}
private slots:
void create();
void length_data();
void length();
void normalized_data();
void normalized();
void normalize_data();
void normalize();
void compare();
void add_data();
void add();
void subtract_data();
void subtract();
void multiply_data();
void multiply();
void multiplyFactor_data();
void multiplyFactor();
void divide_data();
void divide();
void negate_data();
void negate();
void conjugate_data();
void conjugate();
void fromAxisAndAngle_data();
void fromAxisAndAngle();
void interpolate_data();
void interpolate();
};
// qFuzzyCompare isn't quite "fuzzy" enough to handle conversion
// to fixed-point and back again. So create "fuzzier" compares.
static bool fuzzyCompare(float x, float y)
{
float diff = x - y;
if (diff < 0.0f)
diff = -diff;
return (diff < 0.001);
}
// Test the creation of QQuaternion objects in various ways:
// construct, copy, and modify.
void tst_QQuaternion::create()
{
QQuaternion identity;
QCOMPARE(identity.x(), (qreal)0.0f);
QCOMPARE(identity.y(), (qreal)0.0f);
QCOMPARE(identity.z(), (qreal)0.0f);
QCOMPARE(identity.scalar(), (qreal)1.0f);
QVERIFY(identity.isIdentity());
QQuaternion v1(34.0f, 1.0f, 2.5f, -89.25f);
QCOMPARE(v1.x(), (qreal)1.0f);
QCOMPARE(v1.y(), (qreal)2.5f);
QCOMPARE(v1.z(), (qreal)-89.25f);
QCOMPARE(v1.scalar(), (qreal)34.0f);
QVERIFY(!v1.isNull());
QQuaternion v1i(34, 1, 2, -89);
QCOMPARE(v1i.x(), (qreal)1.0f);
QCOMPARE(v1i.y(), (qreal)2.0f);
QCOMPARE(v1i.z(), (qreal)-89.0f);
QCOMPARE(v1i.scalar(), (qreal)34.0f);
QVERIFY(!v1i.isNull());
QQuaternion v2(v1);
QCOMPARE(v2.x(), (qreal)1.0f);
QCOMPARE(v2.y(), (qreal)2.5f);
QCOMPARE(v2.z(), (qreal)-89.25f);
QCOMPARE(v2.scalar(), (qreal)34.0f);
QVERIFY(!v2.isNull());
QQuaternion v4;
QCOMPARE(v4.x(), (qreal)0.0f);
QCOMPARE(v4.y(), (qreal)0.0f);
QCOMPARE(v4.z(), (qreal)0.0f);
QCOMPARE(v4.scalar(), (qreal)1.0f);
QVERIFY(v4.isIdentity());
v4 = v1;
QCOMPARE(v4.x(), (qreal)1.0f);
QCOMPARE(v4.y(), (qreal)2.5f);
QCOMPARE(v4.z(), (qreal)-89.25f);
QCOMPARE(v4.scalar(), (qreal)34.0f);
QVERIFY(!v4.isNull());
QQuaternion v9(34, QVector3D(1.0f, 2.5f, -89.25f));
QCOMPARE(v9.x(), (qreal)1.0f);
QCOMPARE(v9.y(), (qreal)2.5f);
QCOMPARE(v9.z(), (qreal)-89.25f);
QCOMPARE(v9.scalar(), (qreal)34.0f);
QVERIFY(!v9.isNull());
v1.setX(3.0f);
QCOMPARE(v1.x(), (qreal)3.0f);
QCOMPARE(v1.y(), (qreal)2.5f);
QCOMPARE(v1.z(), (qreal)-89.25f);
QCOMPARE(v1.scalar(), (qreal)34.0f);
QVERIFY(!v1.isNull());
v1.setY(10.5f);
QCOMPARE(v1.x(), (qreal)3.0f);
QCOMPARE(v1.y(), (qreal)10.5f);
QCOMPARE(v1.z(), (qreal)-89.25f);
QCOMPARE(v1.scalar(), (qreal)34.0f);
QVERIFY(!v1.isNull());
v1.setZ(15.5f);
QCOMPARE(v1.x(), (qreal)3.0f);
QCOMPARE(v1.y(), (qreal)10.5f);
QCOMPARE(v1.z(), (qreal)15.5f);
QCOMPARE(v1.scalar(), (qreal)34.0f);
QVERIFY(!v1.isNull());
v1.setScalar(6.0f);
QCOMPARE(v1.x(), (qreal)3.0f);
QCOMPARE(v1.y(), (qreal)10.5f);
QCOMPARE(v1.z(), (qreal)15.5f);
QCOMPARE(v1.scalar(), (qreal)6.0f);
QVERIFY(!v1.isNull());
v1.setVector(2.0f, 6.5f, -1.25f);
QCOMPARE(v1.x(), (qreal)2.0f);
QCOMPARE(v1.y(), (qreal)6.5f);
QCOMPARE(v1.z(), (qreal)-1.25f);
QCOMPARE(v1.scalar(), (qreal)6.0f);
QVERIFY(!v1.isNull());
QVERIFY(v1.vector() == QVector3D(2.0f, 6.5f, -1.25f));
v1.setVector(QVector3D(-2.0f, -6.5f, 1.25f));
QCOMPARE(v1.x(), (qreal)-2.0f);
QCOMPARE(v1.y(), (qreal)-6.5f);
QCOMPARE(v1.z(), (qreal)1.25f);
QCOMPARE(v1.scalar(), (qreal)6.0f);
QVERIFY(!v1.isNull());
QVERIFY(v1.vector() == QVector3D(-2.0f, -6.5f, 1.25f));
v1.setX(0.0f);
v1.setY(0.0f);
v1.setZ(0.0f);
v1.setScalar(0.0f);
QCOMPARE(v1.x(), (qreal)0.0f);
QCOMPARE(v1.y(), (qreal)0.0f);
QCOMPARE(v1.z(), (qreal)0.0f);
QCOMPARE(v1.scalar(), (qreal)0.0f);
QVERIFY(v1.isNull());
QVector4D v10 = v9.toVector4D();
QCOMPARE(v10.x(), (qreal)1.0f);
QCOMPARE(v10.y(), (qreal)2.5f);
QCOMPARE(v10.z(), (qreal)-89.25f);
QCOMPARE(v10.w(), (qreal)34.0f);
}
// Test length computation for quaternions.
void tst_QQuaternion::length_data()
{
QTest::addColumn<qreal>("x");
QTest::addColumn<qreal>("y");
QTest::addColumn<qreal>("z");
QTest::addColumn<qreal>("w");
QTest::addColumn<qreal>("len");
QTest::newRow("null") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f;
QTest::newRow("1x") << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f;
QTest::newRow("1y") << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f;
QTest::newRow("1z") << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)1.0f;
QTest::newRow("1w") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)1.0f;
QTest::newRow("-1x") << (qreal)-1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f;
QTest::newRow("-1y") << (qreal)0.0f << (qreal)-1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f;
QTest::newRow("-1z") << (qreal)0.0f << (qreal)0.0f << (qreal)-1.0f << (qreal)0.0f << (qreal)1.0f;
QTest::newRow("-1w") << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)-1.0f << (qreal)1.0f;
QTest::newRow("two") << (qreal)2.0f << (qreal)-2.0f << (qreal)2.0f << (qreal)2.0f << (qreal)qSqrt(16.0f);
}
void tst_QQuaternion::length()
{
QFETCH(qreal, x);
QFETCH(qreal, y);
QFETCH(qreal, z);
QFETCH(qreal, w);
QFETCH(qreal, len);
QQuaternion v(w, x, y, z);
QCOMPARE((float)(v.length()), (float)len);
QCOMPARE((float)(v.lengthSquared()), (float)(x * x + y * y + z * z + w * w));
}
// Test the unit vector conversion for quaternions.
void tst_QQuaternion::normalized_data()
{
// Use the same test data as the length test.
length_data();
}
void tst_QQuaternion::normalized()
{
QFETCH(qreal, x);
QFETCH(qreal, y);
QFETCH(qreal, z);
QFETCH(qreal, w);
QFETCH(qreal, len);
QQuaternion v(w, x, y, z);
QQuaternion u = v.normalized();
if (v.isNull())
QVERIFY(u.isNull());
else
QCOMPARE((float)(u.length()), (float)1.0f);
QCOMPARE((float)(u.x() * len), (float)(v.x()));
QCOMPARE((float)(u.y() * len), (float)(v.y()));
QCOMPARE((float)(u.z() * len), (float)(v.z()));
QCOMPARE((float)(u.scalar() * len), (float)(v.scalar()));
}
// Test the unit vector conversion for quaternions.
void tst_QQuaternion::normalize_data()
{
// Use the same test data as the length test.
length_data();
}
void tst_QQuaternion::normalize()
{
QFETCH(qreal, x);
QFETCH(qreal, y);
QFETCH(qreal, z);
QFETCH(qreal, w);
QQuaternion v(w, x, y, z);
bool isNull = v.isNull();
v.normalize();
if (isNull)
QVERIFY(v.isNull());
else
QCOMPARE((float)(v.length()), (float)1.0f);
}
// Test the comparison operators for quaternions.
void tst_QQuaternion::compare()
{
QQuaternion v1(8, 1, 2, 4);
QQuaternion v2(8, 1, 2, 4);
QQuaternion v3(8, 3, 2, 4);
QQuaternion v4(8, 1, 3, 4);
QQuaternion v5(8, 1, 2, 3);
QQuaternion v6(3, 1, 2, 4);
QVERIFY(v1 == v2);
QVERIFY(v1 != v3);
QVERIFY(v1 != v4);
QVERIFY(v1 != v5);
QVERIFY(v1 != v6);
}
// Test addition for quaternions.
void tst_QQuaternion::add_data()
{
QTest::addColumn<qreal>("x1");
QTest::addColumn<qreal>("y1");
QTest::addColumn<qreal>("z1");
QTest::addColumn<qreal>("w1");
QTest::addColumn<qreal>("x2");
QTest::addColumn<qreal>("y2");
QTest::addColumn<qreal>("z2");
QTest::addColumn<qreal>("w2");
QTest::addColumn<qreal>("x3");
QTest::addColumn<qreal>("y3");
QTest::addColumn<qreal>("z3");
QTest::addColumn<qreal>("w3");
QTest::newRow("null")
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f;
QTest::newRow("xonly")
<< (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f
<< (qreal)2.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f
<< (qreal)3.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f;
QTest::newRow("yonly")
<< (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f
<< (qreal)0.0f << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f
<< (qreal)0.0f << (qreal)3.0f << (qreal)0.0f << (qreal)0.0f;
QTest::newRow("zonly")
<< (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f
<< (qreal)0.0f << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f
<< (qreal)0.0f << (qreal)0.0f << (qreal)3.0f << (qreal)0.0f;
QTest::newRow("wonly")
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)3.0f;
QTest::newRow("all")
<< (qreal)1.0f << (qreal)2.0f << (qreal)3.0f << (qreal)8.0f
<< (qreal)4.0f << (qreal)5.0f << (qreal)-6.0f << (qreal)9.0f
<< (qreal)5.0f << (qreal)7.0f << (qreal)-3.0f << (qreal)17.0f;
}
void tst_QQuaternion::add()
{
QFETCH(qreal, x1);
QFETCH(qreal, y1);
QFETCH(qreal, z1);
QFETCH(qreal, w1);
QFETCH(qreal, x2);
QFETCH(qreal, y2);
QFETCH(qreal, z2);
QFETCH(qreal, w2);
QFETCH(qreal, x3);
QFETCH(qreal, y3);
QFETCH(qreal, z3);
QFETCH(qreal, w3);
QQuaternion v1(w1, x1, y1, z1);
QQuaternion v2(w2, x2, y2, z2);
QQuaternion v3(w3, x3, y3, z3);
QVERIFY((v1 + v2) == v3);
QQuaternion v4(v1);
v4 += v2;
QVERIFY(v4 == v3);
QCOMPARE(v4.x(), v1.x() + v2.x());
QCOMPARE(v4.y(), v1.y() + v2.y());
QCOMPARE(v4.z(), v1.z() + v2.z());
QCOMPARE(v4.scalar(), v1.scalar() + v2.scalar());
}
// Test subtraction for quaternions.
void tst_QQuaternion::subtract_data()
{
// Use the same test data as the add test.
add_data();
}
void tst_QQuaternion::subtract()
{
QFETCH(qreal, x1);
QFETCH(qreal, y1);
QFETCH(qreal, z1);
QFETCH(qreal, w1);
QFETCH(qreal, x2);
QFETCH(qreal, y2);
QFETCH(qreal, z2);
QFETCH(qreal, w2);
QFETCH(qreal, x3);
QFETCH(qreal, y3);
QFETCH(qreal, z3);
QFETCH(qreal, w3);
QQuaternion v1(w1, x1, y1, z1);
QQuaternion v2(w2, x2, y2, z2);
QQuaternion v3(w3, x3, y3, z3);
QVERIFY((v3 - v1) == v2);
QVERIFY((v3 - v2) == v1);
QQuaternion v4(v3);
v4 -= v1;
QVERIFY(v4 == v2);
QCOMPARE(v4.x(), v3.x() - v1.x());
QCOMPARE(v4.y(), v3.y() - v1.y());
QCOMPARE(v4.z(), v3.z() - v1.z());
QCOMPARE(v4.scalar(), v3.scalar() - v1.scalar());
QQuaternion v5(v3);
v5 -= v2;
QVERIFY(v5 == v1);
QCOMPARE(v5.x(), v3.x() - v2.x());
QCOMPARE(v5.y(), v3.y() - v2.y());
QCOMPARE(v5.z(), v3.z() - v2.z());
QCOMPARE(v5.scalar(), v3.scalar() - v2.scalar());
}
// Test quaternion multiplication.
void tst_QQuaternion::multiply_data()
{
QTest::addColumn<qreal>("x1");
QTest::addColumn<qreal>("y1");
QTest::addColumn<qreal>("z1");
QTest::addColumn<qreal>("w1");
QTest::addColumn<qreal>("x2");
QTest::addColumn<qreal>("y2");
QTest::addColumn<qreal>("z2");
QTest::addColumn<qreal>("w2");
QTest::newRow("null")
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f;
QTest::newRow("unitvec")
<< (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f
<< (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)1.0f;
QTest::newRow("complex")
<< (qreal)1.0f << (qreal)2.0f << (qreal)3.0f << (qreal)7.0f
<< (qreal)4.0f << (qreal)5.0f << (qreal)6.0f << (qreal)8.0f;
}
void tst_QQuaternion::multiply()
{
QFETCH(qreal, x1);
QFETCH(qreal, y1);
QFETCH(qreal, z1);
QFETCH(qreal, w1);
QFETCH(qreal, x2);
QFETCH(qreal, y2);
QFETCH(qreal, z2);
QFETCH(qreal, w2);
QQuaternion q1(w1, x1, y1, z1);
QQuaternion q2(w2, x2, y2, z2);
// Use the simple algorithm at:
// http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q53
// to calculate the answer we expect to get.
QVector3D v1(x1, y1, z1);
QVector3D v2(x2, y2, z2);
qreal scalar = w1 * w2 - QVector3D::dotProduct(v1, v2);
QVector3D vector = w1 * v2 + w2 * v1 + QVector3D::crossProduct(v1, v2);
QQuaternion result(scalar, vector);
QVERIFY((q1 * q2) == result);
}
// Test multiplication by a factor for quaternions.
void tst_QQuaternion::multiplyFactor_data()
{
QTest::addColumn<qreal>("x1");
QTest::addColumn<qreal>("y1");
QTest::addColumn<qreal>("z1");
QTest::addColumn<qreal>("w1");
QTest::addColumn<qreal>("factor");
QTest::addColumn<qreal>("x2");
QTest::addColumn<qreal>("y2");
QTest::addColumn<qreal>("z2");
QTest::addColumn<qreal>("w2");
QTest::newRow("null")
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f
<< (qreal)100.0f
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f;
QTest::newRow("xonly")
<< (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f
<< (qreal)2.0f
<< (qreal)2.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f;
QTest::newRow("yonly")
<< (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)0.0f
<< (qreal)2.0f
<< (qreal)0.0f << (qreal)2.0f << (qreal)0.0f << (qreal)0.0f;
QTest::newRow("zonly")
<< (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)0.0f
<< (qreal)2.0f
<< (qreal)0.0f << (qreal)0.0f << (qreal)2.0f << (qreal)0.0f;
QTest::newRow("wonly")
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)1.0f
<< (qreal)2.0f
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)2.0f;
QTest::newRow("all")
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)4.0f
<< (qreal)2.0f
<< (qreal)2.0f << (qreal)4.0f << (qreal)-6.0f << (qreal)8.0f;
QTest::newRow("allzero")
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)4.0f
<< (qreal)0.0f
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f;
}
void tst_QQuaternion::multiplyFactor()
{
QFETCH(qreal, x1);
QFETCH(qreal, y1);
QFETCH(qreal, z1);
QFETCH(qreal, w1);
QFETCH(qreal, factor);
QFETCH(qreal, x2);
QFETCH(qreal, y2);
QFETCH(qreal, z2);
QFETCH(qreal, w2);
QQuaternion v1(w1, x1, y1, z1);
QQuaternion v2(w2, x2, y2, z2);
QVERIFY((v1 * factor) == v2);
QVERIFY((factor * v1) == v2);
QQuaternion v3(v1);
v3 *= factor;
QVERIFY(v3 == v2);
QCOMPARE(v3.x(), v1.x() * factor);
QCOMPARE(v3.y(), v1.y() * factor);
QCOMPARE(v3.z(), v1.z() * factor);
QCOMPARE(v3.scalar(), v1.scalar() * factor);
}
// Test division by a factor for quaternions.
void tst_QQuaternion::divide_data()
{
// Use the same test data as the multiply test.
multiplyFactor_data();
}
void tst_QQuaternion::divide()
{
QFETCH(qreal, x1);
QFETCH(qreal, y1);
QFETCH(qreal, z1);
QFETCH(qreal, w1);
QFETCH(qreal, factor);
QFETCH(qreal, x2);
QFETCH(qreal, y2);
QFETCH(qreal, z2);
QFETCH(qreal, w2);
QQuaternion v1(w1, x1, y1, z1);
QQuaternion v2(w2, x2, y2, z2);
if (factor == (qreal)0.0f)
return;
QVERIFY((v2 / factor) == v1);
QQuaternion v3(v2);
v3 /= factor;
QVERIFY(v3 == v1);
QCOMPARE(v3.x(), v2.x() / factor);
QCOMPARE(v3.y(), v2.y() / factor);
QCOMPARE(v3.z(), v2.z() / factor);
QCOMPARE(v3.scalar(), v2.scalar() / factor);
}
// Test negation for quaternions.
void tst_QQuaternion::negate_data()
{
// Use the same test data as the add test.
add_data();
}
void tst_QQuaternion::negate()
{
QFETCH(qreal, x1);
QFETCH(qreal, y1);
QFETCH(qreal, z1);
QFETCH(qreal, w1);
QQuaternion v1(w1, x1, y1, z1);
QQuaternion v2(-w1, -x1, -y1, -z1);
QVERIFY(-v1 == v2);
}
// Test quaternion conjugate calculations.
void tst_QQuaternion::conjugate_data()
{
// Use the same test data as the add test.
add_data();
}
void tst_QQuaternion::conjugate()
{
QFETCH(qreal, x1);
QFETCH(qreal, y1);
QFETCH(qreal, z1);
QFETCH(qreal, w1);
QQuaternion v1(w1, x1, y1, z1);
QQuaternion v2(w1, -x1, -y1, -z1);
QVERIFY(v1.conjugate() == v2);
}
// Test quaternion creation from an axis and an angle.
void tst_QQuaternion::fromAxisAndAngle_data()
{
QTest::addColumn<qreal>("x1");
QTest::addColumn<qreal>("y1");
QTest::addColumn<qreal>("z1");
QTest::addColumn<qreal>("angle");
QTest::newRow("null")
<< (qreal)0.0f << (qreal)0.0f << (qreal)0.0f << (qreal)0.0f;
QTest::newRow("xonly")
<< (qreal)1.0f << (qreal)0.0f << (qreal)0.0f << (qreal)90.0f;
QTest::newRow("yonly")
<< (qreal)0.0f << (qreal)1.0f << (qreal)0.0f << (qreal)180.0f;
QTest::newRow("zonly")
<< (qreal)0.0f << (qreal)0.0f << (qreal)1.0f << (qreal)270.0f;
QTest::newRow("complex")
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)45.0f;
}
void tst_QQuaternion::fromAxisAndAngle()
{
QFETCH(qreal, x1);
QFETCH(qreal, y1);
QFETCH(qreal, z1);
QFETCH(qreal, angle);
// Use a straight-forward implementation of the algorithm at:
// http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56
// to calculate the answer we expect to get.
QVector3D vector = QVector3D(x1, y1, z1).normalized();
qreal sin_a = qSin((angle * M_PI / 180.0) / 2.0);
qreal cos_a = qCos((angle * M_PI / 180.0) / 2.0);
QQuaternion result((qreal)cos_a,
(qreal)(vector.x() * sin_a),
(qreal)(vector.y() * sin_a),
(qreal)(vector.z() * sin_a));
result = result.normalized();
QQuaternion answer = QQuaternion::fromAxisAndAngle(QVector3D(x1, y1, z1), angle);
QVERIFY(fuzzyCompare(answer.x(), result.x()));
QVERIFY(fuzzyCompare(answer.y(), result.y()));
QVERIFY(fuzzyCompare(answer.z(), result.z()));
QVERIFY(fuzzyCompare(answer.scalar(), result.scalar()));
answer = QQuaternion::fromAxisAndAngle(x1, y1, z1, angle);
QVERIFY(fuzzyCompare(answer.x(), result.x()));
QVERIFY(fuzzyCompare(answer.y(), result.y()));
QVERIFY(fuzzyCompare(answer.z(), result.z()));
QVERIFY(fuzzyCompare(answer.scalar(), result.scalar()));
}
// Test spherical interpolation of quaternions.
void tst_QQuaternion::interpolate_data()
{
QTest::addColumn<qreal>("x1");
QTest::addColumn<qreal>("y1");
QTest::addColumn<qreal>("z1");
QTest::addColumn<qreal>("angle1");
QTest::addColumn<qreal>("x2");
QTest::addColumn<qreal>("y2");
QTest::addColumn<qreal>("z2");
QTest::addColumn<qreal>("angle2");
QTest::addColumn<qreal>("t");
QTest::addColumn<qreal>("x3");
QTest::addColumn<qreal>("y3");
QTest::addColumn<qreal>("z3");
QTest::addColumn<qreal>("angle3");
QTest::newRow("first")
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f
<< (qreal)0.0f
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f;
QTest::newRow("first2")
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f
<< (qreal)-0.5f
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f;
QTest::newRow("second")
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f
<< (qreal)1.0f
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f;
QTest::newRow("second2")
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f
<< (qreal)1.5f
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f;
QTest::newRow("middle")
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)90.0f
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)180.0f
<< (qreal)0.5f
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)135.0f;
QTest::newRow("wide angle")
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)0.0f
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)270.0f
<< (qreal)0.5f
<< (qreal)1.0f << (qreal)2.0f << (qreal)-3.0f << (qreal)-45.0f;
}
void tst_QQuaternion::interpolate()
{
QFETCH(qreal, x1);
QFETCH(qreal, y1);
QFETCH(qreal, z1);
QFETCH(qreal, angle1);
QFETCH(qreal, x2);
QFETCH(qreal, y2);
QFETCH(qreal, z2);
QFETCH(qreal, angle2);
QFETCH(qreal, t);
QFETCH(qreal, x3);
QFETCH(qreal, y3);
QFETCH(qreal, z3);
QFETCH(qreal, angle3);
QQuaternion q1 = QQuaternion::fromAxisAndAngle(x1, y1, z1, angle1);
QQuaternion q2 = QQuaternion::fromAxisAndAngle(x2, y2, z2, angle2);
QQuaternion q3 = QQuaternion::fromAxisAndAngle(x3, y3, z3, angle3);
QQuaternion result = QQuaternion::interpolate(q1, q2, t);
QVERIFY(fuzzyCompare(result.x(), q3.x()));
QVERIFY(fuzzyCompare(result.y(), q3.y()));
QVERIFY(fuzzyCompare(result.z(), q3.z()));
QVERIFY(fuzzyCompare(result.scalar(), q3.scalar()));
}
// Force the fixed-point version of the test case to put a
// different test name on the front of failure reports.
class tst_QQuaternionFixed : public tst_QQuaternion
{
Q_OBJECT
public:
tst_QQuaternionFixed() {}
~tst_QQuaternionFixed() {}
};
#ifdef QT_GL_FIXED_PREFERRED
QTEST_APPLESS_MAIN(tst_QQuaternionFixed)
#else
QTEST_APPLESS_MAIN(tst_QQuaternion)
#endif
#include "tst_qquaternion.moc"
|