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 (!qFuzzyIsNull(length) && !qFuzzyCompare(length, 1.0f)) {
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
482
483
484
487
488
489
490
493
494
495
496
499
500
501
502
505
506
507
508
509
510
511auto QQuaternion::eulerAngles()
const -> EulerAngles<
float>
513 EulerAngles<
float> result;
516 auto pitch = &result.pitch;
517 auto yaw = &result.yaw;
518 auto roll = &result.roll;
527 const float len = length();
528 const bool rescale = !qFuzzyIsNull(len);
529 const float xps = rescale ? xp / len : xp;
530 const float yps = rescale ? yp / len : yp;
531 const float zps = rescale ? zp / len : zp;
532 const float wps = rescale ? wp / len : wp;
534 const float xx = xps * xps;
535 const float xy = xps * yps;
536 const float xz = xps * zps;
537 const float xw = xps * wps;
538 const float yy = yps * yps;
539 const float yz = yps * zps;
540 const float yw = yps * wps;
541 const float zz = zps * zps;
542 const float zw = zps * wps;
548 constexpr float epsilon = 0.00001f;
550 const float sinp = -2.0f * (yz - xw);
551 if (std::abs(sinp) < 1.0f - epsilon) {
552 *pitch = std::asin(sinp);
553 *yaw = std::atan2(2.0f * (xz + yw), 1.0f - 2.0f * (xx + yy));
554 *roll = std::atan2(2.0f * (xy + zw), 1.0f - 2.0f * (xx + zz));
558 *pitch = std::copysign(
static_cast<
float>(
M_PI_2), sinp);
559 *yaw = 2.0f * std::atan2(yps, wps);
563 *pitch = qRadiansToDegrees(*pitch);
564 *yaw = qRadiansToDegrees(*yaw);
565 *roll = qRadiansToDegrees(*roll);
571
572
573
574
575
576
577
578
579QQuaternion QQuaternion::fromEulerAngles(
float pitch,
float yaw,
float roll)
584 pitch = qDegreesToRadians(pitch);
585 yaw = qDegreesToRadians(yaw);
586 roll = qDegreesToRadians(roll);
592 const float c1 = std::cos(yaw);
593 const float s1 = std::sin(yaw);
594 const float c2 = std::cos(roll);
595 const float s2 = std::sin(roll);
596 const float c3 = std::cos(pitch);
597 const float s3 = std::sin(pitch);
598 const float c1c2 = c1 * c2;
599 const float s1s2 = s1 * s2;
601 const float w = c1c2 * c3 + s1s2 * s3;
602 const float x = c1c2 * s3 + s1s2 * c3;
603 const float y = s1 * c2 * c3 - c1 * s2 * s3;
604 const float z = c1 * s2 * c3 - s1 * c2 * s3;
606 return QQuaternion(w, x, y, z);
610
611
612
613
614
615
616
617
618
619
620
623
624
625
626
627
628
629
630
631
632QMatrix3x3 QQuaternion::toRotationMatrix()
const
637 QMatrix3x3 rot3x3(Qt::Uninitialized);
639 const float f2x = xp + xp;
640 const float f2y = yp + yp;
641 const float f2z = zp + zp;
642 const float f2xw = f2x * wp;
643 const float f2yw = f2y * wp;
644 const float f2zw = f2z * wp;
645 const float f2xx = f2x * xp;
646 const float f2xy = f2x * yp;
647 const float f2xz = f2x * zp;
648 const float f2yy = f2y * yp;
649 const float f2yz = f2y * zp;
650 const float f2zz = f2z * zp;
652 rot3x3(0, 0) = 1.0f - (f2yy + f2zz);
653 rot3x3(0, 1) = f2xy - f2zw;
654 rot3x3(0, 2) = f2xz + f2yw;
655 rot3x3(1, 0) = f2xy + f2zw;
656 rot3x3(1, 1) = 1.0f - (f2xx + f2zz);
657 rot3x3(1, 2) = f2yz - f2xw;
658 rot3x3(2, 0) = f2xz - f2yw;
659 rot3x3(2, 1) = f2yz + f2xw;
660 rot3x3(2, 2) = 1.0f - (f2xx + f2yy);
666
667
668
669
670
671
672
673
674
675QQuaternion QQuaternion::fromRotationMatrix(
const QMatrix3x3 &rot3x3)
683 const float trace = rot3x3(0, 0) + rot3x3(1, 1) + rot3x3(2, 2);
684 if (trace > 0.00000001f) {
685 const float s = 2.0f * std::sqrt(trace + 1.0f);
687 axis[0] = (rot3x3(2, 1) - rot3x3(1, 2)) / s;
688 axis[1] = (rot3x3(0, 2) - rot3x3(2, 0)) / s;
689 axis[2] = (rot3x3(1, 0) - rot3x3(0, 1)) / s;
691 constexpr int s_next[3] = { 1, 2, 0 };
693 if (rot3x3(1, 1) > rot3x3(0, 0))
695 if (rot3x3(2, 2) > rot3x3(i, i))
700 const float s = 2.0f * std::sqrt(rot3x3(i, i) - rot3x3(j, j) - rot3x3(k, k) + 1.0f);
702 scalar = (rot3x3(k, j) - rot3x3(j, k)) / s;
703 axis[j] = (rot3x3(j, i) + rot3x3(i, j)) / s;
704 axis[k] = (rot3x3(k, i) + rot3x3(i, k)) / s;
707 return QQuaternion(scalar, axis[0], axis[1], axis[2]);
710#ifndef QT_NO_VECTOR3D
713
715
716
717
718
719
720
721
722
723
726
727
728
729
730
733
734
735
736
737
740
741
742
743
744
747
748
749
750
751
752
753auto QQuaternion::toAxes()
const -> Axes
755 const QMatrix3x3 rot3x3(toRotationMatrix());
757 return { {rot3x3(0, 0), rot3x3(1, 0), rot3x3(2, 0)},
758 {rot3x3(0, 1), rot3x3(1, 1), rot3x3(2, 1)},
759 {rot3x3(0, 2), rot3x3(1, 2), rot3x3(2, 2)} };
764
765
766
767
768
769
770
771
772
773
774
775
776
779
780
781
782
783
784
785
786
787QQuaternion QQuaternion::fromAxes(
const QVector3D &xAxis,
const QVector3D &yAxis,
const QVector3D &zAxis)
789 QMatrix3x3 rot3x3(Qt::Uninitialized);
790 rot3x3(0, 0) = xAxis.x();
791 rot3x3(1, 0) = xAxis.y();
792 rot3x3(2, 0) = xAxis.z();
793 rot3x3(0, 1) = yAxis.x();
794 rot3x3(1, 1) = yAxis.y();
795 rot3x3(2, 1) = yAxis.z();
796 rot3x3(0, 2) = zAxis.x();
797 rot3x3(1, 2) = zAxis.y();
798 rot3x3(2, 2) = zAxis.z();
800 return QQuaternion::fromRotationMatrix(rot3x3);
804
805
806
807
808
809QQuaternion QQuaternion::fromAxes(Axes axes)
811 return fromAxes(axes.x, axes.y, axes.z);
815
816
817
818
819
820
821
822
823
824QQuaternion QQuaternion::fromDirection(
const QVector3D &direction,
const QVector3D &up)
826 if (qFuzzyIsNull(direction.x()) && qFuzzyIsNull(direction.y()) && qFuzzyIsNull(direction.z()))
827 return QQuaternion();
829 const QVector3D zAxis(direction.normalized());
830 QVector3D xAxis(QVector3D::crossProduct(up, zAxis));
831 if (qFuzzyIsNull(xAxis.lengthSquared())) {
833 return QQuaternion::rotationTo(QVector3D(0.0f, 0.0f, 1.0f), zAxis);
837 const QVector3D yAxis(QVector3D::crossProduct(zAxis, xAxis));
839 return QQuaternion::fromAxes(xAxis, yAxis, zAxis);
843
844
845
846
847
848
849
850QQuaternion QQuaternion::rotationTo(
const QVector3D &from,
const QVector3D &to)
854 const QVector3D v0(from.normalized());
855 const QVector3D v1(to.normalized());
857 float d = QVector3D::dotProduct(v0, v1) + 1.0f;
860 if (qFuzzyIsNull(d)) {
861 QVector3D axis = QVector3D::crossProduct(QVector3D(1.0f, 0.0f, 0.0f), v0);
862 if (qFuzzyIsNull(axis.lengthSquared()))
863 axis = QVector3D::crossProduct(QVector3D(0.0f, 1.0f, 0.0f), v0);
867 return QQuaternion(0.0f, axis.x(), axis.y(), axis.z());
870 d = std::sqrt(2.0f * d);
871 const QVector3D axis(QVector3D::crossProduct(v0, v1) / d);
873 return QQuaternion(d * 0.5f, axis).normalized();
879
880
881
882
883
886
887
888
889
890
893
894
895
896
897
898
899
900
903
904
905
906
907
908
909
910
913
914
915
916
917
918
919
920
923
924
925
926
927
928
929
930
933
934
935
936
937
938
939
940
941
944
945
946
947
948
949
950
951
952
955
956
957
958
959
960
961
962
964#ifndef QT_NO_VECTOR3D
967
968
969
970
971
972
977
978
979
980
981
982
985
986
987
988
989
990
991
992
993
994
995QQuaternion QQuaternion::slerp
996 (
const QQuaternion &q1,
const QQuaternion &q2,
float t)
1005 QQuaternion q2b(q2);
1006 float dot = QQuaternion::dotProduct(q1, q2);
1014 float factor1 = 1.0f - t;
1016 if ((1.0f - dot) > 0.0000001) {
1017 float angle = std::acos(dot);
1018 float sinOfAngle = std::sin(angle);
1019 if (sinOfAngle > 0.0000001) {
1020 factor1 = std::sin((1.0f - t) * angle) / sinOfAngle;
1021 factor2 = std::sin(t * angle) / sinOfAngle;
1026 return q1 * factor1 + q2b * factor2;
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044QQuaternion QQuaternion::nlerp
1045 (
const QQuaternion &q1,
const QQuaternion &q2,
float t)
1054 QQuaternion q2b(q2);
1055 float dot = QQuaternion::dotProduct(q1, q2);
1060 return (q1 * (1.0f - t) + q2b * t).normalized();
1064
1065
1066QQuaternion::operator QVariant()
const
1068 return QVariant::fromValue(*
this);
1071#ifndef QT_NO_DEBUG_STREAM
1075 QDebugStateSaver saver(dbg);
1076 dbg.nospace() <<
"QQuaternion(scalar:" << q.scalar()
1077 <<
", vector:(" << q.x() <<
", "
1078 << q.y() <<
", " << q.z() <<
"))";
1084#ifndef QT_NO_DATASTREAM
1087
1088
1089
1090
1091
1092
1093
1094
1098 stream << quaternion.scalar() << quaternion.x()
1099 << quaternion.y() << quaternion.z();
1104
1105
1106
1107
1108
1109
1110
1111
1115 float scalar, x, y, z;
1120 quaternion.setScalar(scalar);
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
QDataStream & operator<<(QDataStream &stream, const QImage &image)
[0]
QDataStream & operator>>(QDataStream &stream, QImage &image)