5#include <QtCore/qdatastream.h>
6#include <QtCore/qmath.h>
7#include <QtCore/qvariant.h>
8#include <QtCore/qdebug.h>
14#ifndef QT_NO_QUATERNION
18
19
20
21
22
23
24
25
26
29
30
31
32
33
36
37
38
39
40
41
44
45
46
47
48
53
54
55
56
57
58
59
62
63
64
65
66
67
70
71
72
73
74
75
80
81
82
83
84
85
90
91
92
93
96
97
98
99
104
105
106
107
108
111
112
113
114
115
116
119
120
121
122
123
124
127
128
129
130
131
132
135
136
137
138
139
140
143
144
145
146
147
148
151
152
153
154
155
156
157
160
161
162
163
164
165
166
169
170
171
172
173
174
175
178
179
180
181
182
183
186
187
188
189
190
191
192
195
196
197
198
199float QQuaternion::length()
const
201 return qHypot(xp, yp, zp, wp);
205
206
207
208
209
210
211
212float QQuaternion::lengthSquared()
const
214 return xp * xp + yp * yp + zp * zp + wp * wp;
218
219
220
221
222
223
224
225
226
227QQuaternion QQuaternion::normalized()
const
229 const float scale = length();
230 if (qFuzzyIsNull(scale))
231 return QQuaternion(0.0f, 0.0f, 0.0f, 0.0f);
232 return *
this / scale;
236
237
238
239
240
241void QQuaternion::normalize()
243 const float len = length();
244 if (qFuzzyIsNull(len))
254
255
256
257
258
259
260
261
264
265
266
267
268
269
272
273
274
275
276
277
278
279
280
281QVector3D QQuaternion::rotatedVector(
const QVector3D &vector)
const
283 return (*
this * QQuaternion(0, vector) * conjugated()).vector();
287
288
289
290
291
292
293
296
297
298
299
300
301
302
305
306
307
308
309
310
311
314
315
316
317
318
321
322
323
324
325
326
327
329#ifndef QT_NO_VECTOR3D
332
333
334
335
336
337
338
339
340
341
342
343
346
347
348
349
350
351QQuaternion QQuaternion::fromAxisAndAngle(
const QVector3D &axis,
float angle)
357 float a = qDegreesToRadians(angle / 2.0f);
358 float s = std::sin(a);
359 float c = std::cos(a);
360 QVector3D ax = axis.normalized();
361 return QQuaternion(c, ax.x() * s, ax.y() * s, ax.z() * s).normalized();
367
368
369
370
371
372
373
374
375
376
377void QQuaternion::getAxisAndAngle(
float *x,
float *y,
float *z,
float *angle)
const
379 Q_ASSERT(x && y && z && angle);
384 const float length = qHypot(xp, yp, zp);
385 if (!qFuzzyIsNull(length)) {
386 if (qFuzzyCompare(length, 1.0f)) {
395 *angle = qRadiansToDegrees(2.0f * std::atan2(length, wp));
398 *x = *y = *z = *angle = 0.0f;
403
404
405
406
407
408QQuaternion QQuaternion::fromAxisAndAngle
409 (
float x,
float y,
float z,
float angle)
411 float length = qHypot(x, y, z);
412 if (!qFuzzyCompare(length, 1.0f) && !qFuzzyIsNull(length)) {
417 float a = qDegreesToRadians(angle / 2.0f);
418 float s = std::sin(a);
419 float c = std::cos(a);
420 return QQuaternion(c, x * s, y * s, z * s).normalized();
423#ifndef QT_NO_VECTOR3D
426
427
428
429
430
431
432
433
436
437
438
439
440
441
442
443
444
445
446
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
468
470
471
472
473
474
475
476
477
478
479
480
481
484
485
486
487
490
491
492
493
496
497
498
499
502
503
504
505
506
507
508auto QQuaternion::eulerAngles()
const -> EulerAngles<
float>
510 EulerAngles<
float> result;
513 auto pitch = &result.pitch;
514 auto yaw = &result.yaw;
515 auto roll = &result.roll;
524 const float len = length();
525 const bool rescale = !qFuzzyIsNull(len);
526 const float xps = rescale ? xp / len : xp;
527 const float yps = rescale ? yp / len : yp;
528 const float zps = rescale ? zp / len : zp;
529 const float wps = rescale ? wp / len : wp;
531 const float xx = xps * xps;
532 const float xy = xps * yps;
533 const float xz = xps * zps;
534 const float xw = xps * wps;
535 const float yy = yps * yps;
536 const float yz = yps * zps;
537 const float yw = yps * wps;
538 const float zz = zps * zps;
539 const float zw = zps * wps;
545 constexpr float epsilon = 0.00001f;
547 const float sinp = -2.0f * (yz - xw);
548 if (std::abs(sinp) < 1.0f - epsilon) {
549 *pitch = std::asin(sinp);
550 *yaw = std::atan2(2.0f * (xz + yw), 1.0f - 2.0f * (xx + yy));
551 *roll = std::atan2(2.0f * (xy + zw), 1.0f - 2.0f * (xx + zz));
555 *pitch = std::copysign(
static_cast<
float>(
M_PI_2), sinp);
556 *yaw = 2.0f * std::atan2(yps, wps);
560 *pitch = qRadiansToDegrees(*pitch);
561 *yaw = qRadiansToDegrees(*yaw);
562 *roll = qRadiansToDegrees(*roll);
568
569
570
571
572
573
574
575
576QQuaternion QQuaternion::fromEulerAngles(
float pitch,
float yaw,
float roll)
581 pitch = qDegreesToRadians(pitch);
582 yaw = qDegreesToRadians(yaw);
583 roll = qDegreesToRadians(roll);
589 const float c1 = std::cos(yaw);
590 const float s1 = std::sin(yaw);
591 const float c2 = std::cos(roll);
592 const float s2 = std::sin(roll);
593 const float c3 = std::cos(pitch);
594 const float s3 = std::sin(pitch);
595 const float c1c2 = c1 * c2;
596 const float s1s2 = s1 * s2;
598 const float w = c1c2 * c3 + s1s2 * s3;
599 const float x = c1c2 * s3 + s1s2 * c3;
600 const float y = s1 * c2 * c3 - c1 * s2 * s3;
601 const float z = c1 * s2 * c3 - s1 * c2 * s3;
603 return QQuaternion(w, x, y, z);
607
608
609
610
611
612
613
614
615
616
617
620
621
622
623
624
625
626
627
628
629QMatrix3x3 QQuaternion::toRotationMatrix()
const
634 QMatrix3x3 rot3x3(Qt::Uninitialized);
636 const float f2x = xp + xp;
637 const float f2y = yp + yp;
638 const float f2z = zp + zp;
639 const float f2xw = f2x * wp;
640 const float f2yw = f2y * wp;
641 const float f2zw = f2z * wp;
642 const float f2xx = f2x * xp;
643 const float f2xy = f2x * yp;
644 const float f2xz = f2x * zp;
645 const float f2yy = f2y * yp;
646 const float f2yz = f2y * zp;
647 const float f2zz = f2z * zp;
649 rot3x3(0, 0) = 1.0f - (f2yy + f2zz);
650 rot3x3(0, 1) = f2xy - f2zw;
651 rot3x3(0, 2) = f2xz + f2yw;
652 rot3x3(1, 0) = f2xy + f2zw;
653 rot3x3(1, 1) = 1.0f - (f2xx + f2zz);
654 rot3x3(1, 2) = f2yz - f2xw;
655 rot3x3(2, 0) = f2xz - f2yw;
656 rot3x3(2, 1) = f2yz + f2xw;
657 rot3x3(2, 2) = 1.0f - (f2xx + f2yy);
663
664
665
666
667
668
669
670
671
672QQuaternion QQuaternion::fromRotationMatrix(
const QMatrix3x3 &rot3x3)
680 const float trace = rot3x3(0, 0) + rot3x3(1, 1) + rot3x3(2, 2);
681 if (trace > 0.00000001f) {
682 const float s = 2.0f * std::sqrt(trace + 1.0f);
684 axis[0] = (rot3x3(2, 1) - rot3x3(1, 2)) / s;
685 axis[1] = (rot3x3(0, 2) - rot3x3(2, 0)) / s;
686 axis[2] = (rot3x3(1, 0) - rot3x3(0, 1)) / s;
688 constexpr int s_next[3] = { 1, 2, 0 };
690 if (rot3x3(1, 1) > rot3x3(0, 0))
692 if (rot3x3(2, 2) > rot3x3(i, i))
697 const float s = 2.0f * std::sqrt(rot3x3(i, i) - rot3x3(j, j) - rot3x3(k, k) + 1.0f);
699 scalar = (rot3x3(k, j) - rot3x3(j, k)) / s;
700 axis[j] = (rot3x3(j, i) + rot3x3(i, j)) / s;
701 axis[k] = (rot3x3(k, i) + rot3x3(i, k)) / s;
704 return QQuaternion(scalar, axis[0], axis[1], axis[2]);
707#ifndef QT_NO_VECTOR3D
710
712
713
714
715
716
717
718
719
720
723
724
725
726
727
730
731
732
733
734
737
738
739
740
741
744
745
746
747
748
749
750auto QQuaternion::toAxes()
const -> Axes
752 const QMatrix3x3 rot3x3(toRotationMatrix());
754 return { {rot3x3(0, 0), rot3x3(1, 0), rot3x3(2, 0)},
755 {rot3x3(0, 1), rot3x3(1, 1), rot3x3(2, 1)},
756 {rot3x3(0, 2), rot3x3(1, 2), rot3x3(2, 2)} };
761
762
763
764
765
766
767
768
769
770
771
772
773
776
777
778
779
780
781
782
783
784QQuaternion QQuaternion::fromAxes(
const QVector3D &xAxis,
const QVector3D &yAxis,
const QVector3D &zAxis)
786 QMatrix3x3 rot3x3(Qt::Uninitialized);
787 rot3x3(0, 0) = xAxis.x();
788 rot3x3(1, 0) = xAxis.y();
789 rot3x3(2, 0) = xAxis.z();
790 rot3x3(0, 1) = yAxis.x();
791 rot3x3(1, 1) = yAxis.y();
792 rot3x3(2, 1) = yAxis.z();
793 rot3x3(0, 2) = zAxis.x();
794 rot3x3(1, 2) = zAxis.y();
795 rot3x3(2, 2) = zAxis.z();
797 return QQuaternion::fromRotationMatrix(rot3x3);
801
802
803
804
805
806QQuaternion QQuaternion::fromAxes(Axes axes)
808 return fromAxes(axes.x, axes.y, axes.z);
812
813
814
815
816
817
818
819
820
821QQuaternion QQuaternion::fromDirection(
const QVector3D &direction,
const QVector3D &up)
823 if (qFuzzyIsNull(direction.x()) && qFuzzyIsNull(direction.y()) && qFuzzyIsNull(direction.z()))
824 return QQuaternion();
826 const QVector3D zAxis(direction.normalized());
827 QVector3D xAxis(QVector3D::crossProduct(up, zAxis));
828 if (qFuzzyIsNull(xAxis.lengthSquared())) {
830 return QQuaternion::rotationTo(QVector3D(0.0f, 0.0f, 1.0f), zAxis);
834 const QVector3D yAxis(QVector3D::crossProduct(zAxis, xAxis));
836 return QQuaternion::fromAxes(xAxis, yAxis, zAxis);
840
841
842
843
844
845
846
847QQuaternion QQuaternion::rotationTo(
const QVector3D &from,
const QVector3D &to)
851 const QVector3D v0(from.normalized());
852 const QVector3D v1(to.normalized());
854 float d = QVector3D::dotProduct(v0, v1) + 1.0f;
857 if (qFuzzyIsNull(d)) {
858 QVector3D axis = QVector3D::crossProduct(QVector3D(1.0f, 0.0f, 0.0f), v0);
859 if (qFuzzyIsNull(axis.lengthSquared()))
860 axis = QVector3D::crossProduct(QVector3D(0.0f, 1.0f, 0.0f), v0);
864 return QQuaternion(0.0f, axis.x(), axis.y(), axis.z());
867 d = std::sqrt(2.0f * d);
868 const QVector3D axis(QVector3D::crossProduct(v0, v1) / d);
870 return QQuaternion(d * 0.5f, axis).normalized();
876
877
878
879
880
883
884
885
886
887
890
891
892
893
894
895
896
897
900
901
902
903
904
905
906
907
910
911
912
913
914
915
916
917
920
921
922
923
924
925
926
927
930
931
932
933
934
935
936
937
938
941
942
943
944
945
946
947
948
949
952
953
954
955
956
957
958
959
961#ifndef QT_NO_VECTOR3D
964
965
966
967
968
969
974
975
976
977
978
979
982
983
984
985
986
987
988
989
990
991
992QQuaternion QQuaternion::slerp
993 (
const QQuaternion &q1,
const QQuaternion &q2,
float t)
1002 QQuaternion q2b(q2);
1003 float dot = QQuaternion::dotProduct(q1, q2);
1011 float factor1 = 1.0f - t;
1013 if ((1.0f - dot) > 0.0000001) {
1014 float angle = std::acos(dot);
1015 float sinOfAngle = std::sin(angle);
1016 if (sinOfAngle > 0.0000001) {
1017 factor1 = std::sin((1.0f - t) * angle) / sinOfAngle;
1018 factor2 = std::sin(t * angle) / sinOfAngle;
1023 return q1 * factor1 + q2b * factor2;
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041QQuaternion QQuaternion::nlerp
1042 (
const QQuaternion &q1,
const QQuaternion &q2,
float t)
1051 QQuaternion q2b(q2);
1052 float dot = QQuaternion::dotProduct(q1, q2);
1057 return (q1 * (1.0f - t) + q2b * t).normalized();
1061
1062
1063QQuaternion::operator QVariant()
const
1065 return QVariant::fromValue(*
this);
1068#ifndef QT_NO_DEBUG_STREAM
1072 QDebugStateSaver saver(dbg);
1073 dbg.nospace() <<
"QQuaternion(scalar:" << q.scalar()
1074 <<
", vector:(" << q.x() <<
", "
1075 << q.y() <<
", " << q.z() <<
"))";
1081#ifndef QT_NO_DATASTREAM
1084
1085
1086
1087
1088
1089
1090
1091
1095 stream << quaternion.scalar() << quaternion.x()
1096 << quaternion.y() << quaternion.z();
1101
1102
1103
1104
1105
1106
1107
1108
1112 float scalar, x, y, z;
1117 quaternion.setScalar(scalar);
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
QDataStream & operator<<(QDataStream &stream, const QImage &image)
[0]
QDataStream & operator>>(QDataStream &stream, QImage &image)