6#include <QtCore/qdatastream.h>
7#include <QtCore/qmath.h>
8#include <QtCore/qvariant.h>
9#include <QtCore/qdebug.h>
15#ifndef QT_NO_QUATERNION
22
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
42
43
44
45
46
47
48
51
52
53
54
55
56
57
58
61
62
63
64
65
66
67
70
71
72
73
76
77
78
79
81
82
83
84
92
93
94
95
96
97
98
99
100
103
104
105
106
107
110
111
112
113
114
115
118
119
120
121
122
124#ifndef QT_NO_VECTOR3D
127
128
129
130
131
132
133
136
137
138
139
140
141
144
145
146
147
148
149
154
155
156
157
158
159
161#ifndef QT_NO_VECTOR4D
164
165
166
167
170
171
172
173
178
179
180
181
182
185
186
187
188
189
190
193
194
195
196
197
198
201
202
203
204
205
206
209
210
211
212
213
214
217
218
219
220
221
222
225
226
227
228
229
230
231
234
235
236
237
238
239
240
243
244
245
246
247
248
249
252
253
254
255
256
257
260
261
262
263
264
265
266
269
270
271
272
273float QQuaternion::length()
const
275 return qHypot(xp, yp, zp, wp);
279
280
281
282
283
284
285
286float QQuaternion::lengthSquared()
const
288 return xp * xp + yp * yp + zp * zp + wp * wp;
292
293
294
295
296
297
298
299
300
301QQuaternion QQuaternion::normalized()
const
303 const float scale = length();
304 if (qFuzzyIsNull(scale))
305 return QQuaternion(0.0f, 0.0f, 0.0f, 0.0f);
306 return *
this / scale;
310
311
312
313
314
315void QQuaternion::normalize()
317 const float len = length();
318 if (qFuzzyIsNull(len))
328
329
330
331
332
333
334
335
338
339
340
341
342
343
346
347
348
349
350
351
352
353
354
355QVector3D QQuaternion::rotatedVector(
const QVector3D &vector)
const
357 return (*
this * QQuaternion(0, vector) * conjugated()).vector();
361
362
363
364
365
366
367
370
371
372
373
374
375
376
379
380
381
382
383
384
385
388
389
390
391
392
395
396
397
398
399
400
401
403#ifndef QT_NO_VECTOR3D
406
407
408
409
410
411
412
413
414
415
416
417
420
421
422
423
424
425QQuaternion QQuaternion::fromAxisAndAngle(
const QVector3D &axis,
float angle)
431 float a = qDegreesToRadians(angle / 2.0f);
432 float s = std::sin(a);
433 float c = std::cos(a);
434 QVector3D ax = axis.normalized();
435 return QQuaternion(c, ax.x() * s, ax.y() * s, ax.z() * s).normalized();
441
442
443
444
445
446
447
448
449
450
451void QQuaternion::getAxisAndAngle(
float *x,
float *y,
float *z,
float *angle)
const
453 Q_ASSERT(x && y && z && angle);
458 const float length = qHypot(xp, yp, zp);
459 if (!qFuzzyIsNull(length)) {
460 if (qFuzzyCompare(length, 1.0f)) {
469 *angle = qRadiansToDegrees(2.0f * std::atan2(length, wp));
472 *x = *y = *z = *angle = 0.0f;
477
478
479
480
481
482QQuaternion QQuaternion::fromAxisAndAngle
483 (
float x,
float y,
float z,
float angle)
485 float length = qHypot(x, y, z);
486 if (!qFuzzyIsNull(length) && !qFuzzyCompare(length, 1.0f)) {
491 float a = qDegreesToRadians(angle / 2.0f);
492 float s = std::sin(a);
493 float c = std::cos(a);
494 return QQuaternion(c, x * s, y * s, z * s).normalized();
497#ifndef QT_NO_VECTOR3D
500
501
502
503
504
505
506
507
510
511
512
513
514
515
516
517
518
519
520
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
542
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
561
562
563
564
567
568
569
570
573
574
575
576
579
580
581
582
583
584
585auto QQuaternion::eulerAngles()
const -> EulerAngles<
float>
587 EulerAngles<
float> result;
590 auto pitch = &result.pitch;
591 auto yaw = &result.yaw;
592 auto roll = &result.roll;
601 const float len = length();
602 const bool rescale = !qFuzzyIsNull(len);
603 const float xps = rescale ? xp / len : xp;
604 const float yps = rescale ? yp / len : yp;
605 const float zps = rescale ? zp / len : zp;
606 const float wps = rescale ? wp / len : wp;
608 const float xx = xps * xps;
609 const float xy = xps * yps;
610 const float xz = xps * zps;
611 const float xw = xps * wps;
612 const float yy = yps * yps;
613 const float yz = yps * zps;
614 const float yw = yps * wps;
615 const float zz = zps * zps;
616 const float zw = zps * wps;
622 constexpr float epsilon = 0.00001f;
624 const float sinp = -2.0f * (yz - xw);
625 if (std::abs(sinp) < 1.0f - epsilon) {
626 *pitch = std::asin(sinp);
627 *yaw = std::atan2(2.0f * (xz + yw), 1.0f - 2.0f * (xx + yy));
628 *roll = std::atan2(2.0f * (xy + zw), 1.0f - 2.0f * (xx + zz));
632 *pitch = std::copysign(
static_cast<
float>(
M_PI_2), sinp);
633 *yaw = 2.0f * std::atan2(yps, wps);
637 *pitch = qRadiansToDegrees(*pitch);
638 *yaw = qRadiansToDegrees(*yaw);
639 *roll = qRadiansToDegrees(*roll);
645
646
647
648
649
650
651
652
653QQuaternion QQuaternion::fromEulerAngles(
float pitch,
float yaw,
float roll)
658 pitch = qDegreesToRadians(pitch);
659 yaw = qDegreesToRadians(yaw);
660 roll = qDegreesToRadians(roll);
666 const float c1 = std::cos(yaw);
667 const float s1 = std::sin(yaw);
668 const float c2 = std::cos(roll);
669 const float s2 = std::sin(roll);
670 const float c3 = std::cos(pitch);
671 const float s3 = std::sin(pitch);
672 const float c1c2 = c1 * c2;
673 const float s1s2 = s1 * s2;
675 const float w = c1c2 * c3 + s1s2 * s3;
676 const float x = c1c2 * s3 + s1s2 * c3;
677 const float y = s1 * c2 * c3 - c1 * s2 * s3;
678 const float z = c1 * s2 * c3 - s1 * c2 * s3;
680 return QQuaternion(w, x, y, z);
684
685
686
687
688
689
690
691
692
693
694
697
698
699
700
701
702
703
704
705
706QMatrix3x3 QQuaternion::toRotationMatrix()
const
711 QMatrix3x3 rot3x3(Qt::Uninitialized);
713 const float f2x = xp + xp;
714 const float f2y = yp + yp;
715 const float f2z = zp + zp;
716 const float f2xw = f2x * wp;
717 const float f2yw = f2y * wp;
718 const float f2zw = f2z * wp;
719 const float f2xx = f2x * xp;
720 const float f2xy = f2x * yp;
721 const float f2xz = f2x * zp;
722 const float f2yy = f2y * yp;
723 const float f2yz = f2y * zp;
724 const float f2zz = f2z * zp;
726 rot3x3(0, 0) = 1.0f - (f2yy + f2zz);
727 rot3x3(0, 1) = f2xy - f2zw;
728 rot3x3(0, 2) = f2xz + f2yw;
729 rot3x3(1, 0) = f2xy + f2zw;
730 rot3x3(1, 1) = 1.0f - (f2xx + f2zz);
731 rot3x3(1, 2) = f2yz - f2xw;
732 rot3x3(2, 0) = f2xz - f2yw;
733 rot3x3(2, 1) = f2yz + f2xw;
734 rot3x3(2, 2) = 1.0f - (f2xx + f2yy);
740
741
742
743
744
745
746
747
748
749QQuaternion QQuaternion::fromRotationMatrix(
const QMatrix3x3 &rot3x3)
757 const float trace = rot3x3(0, 0) + rot3x3(1, 1) + rot3x3(2, 2);
758 if (trace > 0.00000001f) {
759 const float s = 2.0f * std::sqrt(trace + 1.0f);
761 axis[0] = (rot3x3(2, 1) - rot3x3(1, 2)) / s;
762 axis[1] = (rot3x3(0, 2) - rot3x3(2, 0)) / s;
763 axis[2] = (rot3x3(1, 0) - rot3x3(0, 1)) / s;
765 constexpr int s_next[3] = { 1, 2, 0 };
767 if (rot3x3(1, 1) > rot3x3(0, 0))
769 if (rot3x3(2, 2) > rot3x3(i, i))
774 const float s = 2.0f * std::sqrt(rot3x3(i, i) - rot3x3(j, j) - rot3x3(k, k) + 1.0f);
776 scalar = (rot3x3(k, j) - rot3x3(j, k)) / s;
777 axis[j] = (rot3x3(j, i) + rot3x3(i, j)) / s;
778 axis[k] = (rot3x3(k, i) + rot3x3(i, k)) / s;
781 return QQuaternion(scalar, axis[0], axis[1], axis[2]);
785
787
788
789
790
791
792
793
794
795
798
799
800
801
802
805
806
807
808
809
812
813
814
815
816
819
820
821
822
823
824
825auto QQuaternion::toAxes()
const -> Axes
827 const QMatrix3x3 rot3x3(toRotationMatrix());
829 return { {rot3x3(0, 0), rot3x3(1, 0), rot3x3(2, 0)},
830 {rot3x3(0, 1), rot3x3(1, 1), rot3x3(2, 1)},
831 {rot3x3(0, 2), rot3x3(1, 2), rot3x3(2, 2)} };
835
836
837
838
839
840
841
842
843
844
845
846
847
850
851
852
853
854
855
856
857
858QQuaternion QQuaternion::fromAxes(Axes axes)
860 QMatrix3x3 rot3x3(Qt::Uninitialized);
861 rot3x3(0, 0) = axes.x.x;
862 rot3x3(1, 0) = axes.x.y;
863 rot3x3(2, 0) = axes.x.z;
864 rot3x3(0, 1) = axes.y.x;
865 rot3x3(1, 1) = axes.y.y;
866 rot3x3(2, 1) = axes.y.z;
867 rot3x3(0, 2) = axes.z.x;
868 rot3x3(1, 2) = axes.z.y;
869 rot3x3(2, 2) = axes.z.z;
871 return QQuaternion::fromRotationMatrix(rot3x3);
875
876
877
878
879
881#ifndef QT_NO_VECTOR3D
884
885
886
887
888
889
890
891
892
893QQuaternion QQuaternion::fromDirection(
const QVector3D &direction,
const QVector3D &up)
895 if (qFuzzyIsNull(direction.x()) && qFuzzyIsNull(direction.y()) && qFuzzyIsNull(direction.z()))
896 return QQuaternion();
898 const QVector3D zAxis(direction.normalized());
899 QVector3D xAxis(QVector3D::crossProduct(up, zAxis));
900 if (qFuzzyIsNull(xAxis.lengthSquared())) {
902 return QQuaternion::rotationTo(QVector3D(0.0f, 0.0f, 1.0f), zAxis);
906 const QVector3D yAxis(QVector3D::crossProduct(zAxis, xAxis));
908 return QQuaternion::fromAxes(xAxis, yAxis, zAxis);
912
913
914
915
916
917
918
919QQuaternion QQuaternion::rotationTo(
const QVector3D &from,
const QVector3D &to)
923 const QVector3D v0(from.normalized());
924 const QVector3D v1(to.normalized());
926 float d = QVector3D::dotProduct(v0, v1) + 1.0f;
929 if (qFuzzyIsNull(d)) {
930 QVector3D axis = QVector3D::crossProduct(QVector3D(1.0f, 0.0f, 0.0f), v0);
931 if (qFuzzyIsNull(axis.lengthSquared()))
932 axis = QVector3D::crossProduct(QVector3D(0.0f, 1.0f, 0.0f), v0);
936 return QQuaternion(0.0f, axis.x(), axis.y(), axis.z());
939 d = std::sqrt(2.0f * d);
940 const QVector3D axis(QVector3D::crossProduct(v0, v1) / d);
942 return QQuaternion(d * 0.5f, axis).normalized();
948
949
950
951
952
955
956
957
958
959
962
963
964
965
966
967
968
969
972
973
974
975
976
977
978
979
982
983
984
985
986
987
988
989
992
993
994
995
996
997
998
999
1002
1003
1004
1005
1006
1007
1008
1009
1010
1013
1014
1015
1016
1017
1018
1019
1020
1021
1024
1025
1026
1027
1028
1029
1030
1031
1033#ifndef QT_NO_VECTOR3D
1036
1037
1038
1039
1040
1041
1046
1047
1048
1049
1050
1051
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064QQuaternion QQuaternion::slerp
1065 (
const QQuaternion &q1,
const QQuaternion &q2,
float t)
1074 QQuaternion q2b(q2);
1075 float dot = QQuaternion::dotProduct(q1, q2);
1083 float factor1 = 1.0f - t;
1085 if ((1.0f - dot) > 0.0000001) {
1086 float angle = std::acos(dot);
1087 float sinOfAngle = std::sin(angle);
1088 if (sinOfAngle > 0.0000001) {
1089 factor1 = std::sin((1.0f - t) * angle) / sinOfAngle;
1090 factor2 = std::sin(t * angle) / sinOfAngle;
1095 return q1 * factor1 + q2b * factor2;
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113QQuaternion QQuaternion::nlerp
1114 (
const QQuaternion &q1,
const QQuaternion &q2,
float t)
1123 QQuaternion q2b(q2);
1124 float dot = QQuaternion::dotProduct(q1, q2);
1129 return (q1 * (1.0f - t) + q2b * t).normalized();
1133
1134
1135QQuaternion::operator QVariant()
const
1137 return QVariant::fromValue(*
this);
1140#ifndef QT_NO_DEBUG_STREAM
1144 QDebugStateSaver saver(dbg);
1145 dbg.nospace() <<
"QQuaternion(scalar:" << q.scalar()
1146 <<
", vector:(" << q.x() <<
", "
1147 << q.y() <<
", " << q.z() <<
"))";
1153#ifndef QT_NO_DATASTREAM
1156
1157
1158
1159
1160
1161
1162
1163
1167 stream << quaternion.scalar() << quaternion.x()
1168 << quaternion.y() << quaternion.z();
1173
1174
1175
1176
1177
1178
1179
1180
1184 float scalar, x, y, z;
1189 quaternion.setScalar(scalar);
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
QDataStream & operator<<(QDataStream &stream, const QImage &image)
[0]
QDataStream & operator>>(QDataStream &stream, QImage &image)