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
343
344
345
346
347
348QQuaternion QQuaternion::fromAxisAndAngle(
const QVector3D &axis,
float angle)
354 float a = qDegreesToRadians(angle / 2.0f);
355 float s = std::sin(a);
356 float c = std::cos(a);
357 QVector3D ax = axis.normalized();
358 return QQuaternion(c, ax.x() * s, ax.y() * s, ax.z() * s).normalized();
364
365
366
367
368
369
370
371void QQuaternion::getAxisAndAngle(
float *x,
float *y,
float *z,
float *angle)
const
373 Q_ASSERT(x && y && z && angle);
378 const float length = qHypot(xp, yp, zp);
379 if (!qFuzzyIsNull(length)) {
380 if (qFuzzyCompare(length, 1.0f)) {
389 *angle = qRadiansToDegrees(2.0f * std::atan2(length, wp));
392 *x = *y = *z = *angle = 0.0f;
397
398
399
400
401
402QQuaternion QQuaternion::fromAxisAndAngle
403 (
float x,
float y,
float z,
float angle)
405 float length = qHypot(x, y, z);
406 if (!qFuzzyCompare(length, 1.0f) && !qFuzzyIsNull(length)) {
411 float a = qDegreesToRadians(angle / 2.0f);
412 float s = std::sin(a);
413 float c = std::cos(a);
414 return QQuaternion(c, x * s, y * s, z * s).normalized();
417#ifndef QT_NO_VECTOR3D
420
421
422
423
424
425
426
427
428
431
432
433
434
435
436
437
438
439
440
445
446
447
448
449
450
451
452void QQuaternion::getEulerAngles(
float *pitch,
float *yaw,
float *roll)
const
454 Q_ASSERT(pitch && yaw && roll);
463 const float len = length();
464 const bool rescale = !qFuzzyIsNull(len);
465 const float xps = rescale ? xp / len : xp;
466 const float yps = rescale ? yp / len : yp;
467 const float zps = rescale ? zp / len : zp;
468 const float wps = rescale ? wp / len : wp;
470 const float xx = xps * xps;
471 const float xy = xps * yps;
472 const float xz = xps * zps;
473 const float xw = xps * wps;
474 const float yy = yps * yps;
475 const float yz = yps * zps;
476 const float yw = yps * wps;
477 const float zz = zps * zps;
478 const float zw = zps * wps;
484 constexpr float epsilon = 0.00001f;
486 const float sinp = -2.0f * (yz - xw);
487 if (std::abs(sinp) < 1.0f - epsilon) {
488 *pitch = std::asin(sinp);
489 *yaw = std::atan2(2.0f * (xz + yw), 1.0f - 2.0f * (xx + yy));
490 *roll = std::atan2(2.0f * (xy + zw), 1.0f - 2.0f * (xx + zz));
494 *pitch = std::copysign(
static_cast<
float>(
M_PI_2), sinp);
495 *yaw = 2.0f * std::atan2(yps, wps);
499 *pitch = qRadiansToDegrees(*pitch);
500 *yaw = qRadiansToDegrees(*yaw);
501 *roll = qRadiansToDegrees(*roll);
505
506
507
508
509
510
511
512
513QQuaternion QQuaternion::fromEulerAngles(
float pitch,
float yaw,
float roll)
518 pitch = qDegreesToRadians(pitch);
519 yaw = qDegreesToRadians(yaw);
520 roll = qDegreesToRadians(roll);
526 const float c1 = std::cos(yaw);
527 const float s1 = std::sin(yaw);
528 const float c2 = std::cos(roll);
529 const float s2 = std::sin(roll);
530 const float c3 = std::cos(pitch);
531 const float s3 = std::sin(pitch);
532 const float c1c2 = c1 * c2;
533 const float s1s2 = s1 * s2;
535 const float w = c1c2 * c3 + s1s2 * s3;
536 const float x = c1c2 * s3 + s1s2 * c3;
537 const float y = s1 * c2 * c3 - c1 * s2 * s3;
538 const float z = c1 * s2 * c3 - s1 * c2 * s3;
540 return QQuaternion(w, x, y, z);
544
545
546
547
548
549
550
551
552
553QMatrix3x3 QQuaternion::toRotationMatrix()
const
558 QMatrix3x3 rot3x3(Qt::Uninitialized);
560 const float f2x = xp + xp;
561 const float f2y = yp + yp;
562 const float f2z = zp + zp;
563 const float f2xw = f2x * wp;
564 const float f2yw = f2y * wp;
565 const float f2zw = f2z * wp;
566 const float f2xx = f2x * xp;
567 const float f2xy = f2x * yp;
568 const float f2xz = f2x * zp;
569 const float f2yy = f2y * yp;
570 const float f2yz = f2y * zp;
571 const float f2zz = f2z * zp;
573 rot3x3(0, 0) = 1.0f - (f2yy + f2zz);
574 rot3x3(0, 1) = f2xy - f2zw;
575 rot3x3(0, 2) = f2xz + f2yw;
576 rot3x3(1, 0) = f2xy + f2zw;
577 rot3x3(1, 1) = 1.0f - (f2xx + f2zz);
578 rot3x3(1, 2) = f2yz - f2xw;
579 rot3x3(2, 0) = f2xz - f2yw;
580 rot3x3(2, 1) = f2yz + f2xw;
581 rot3x3(2, 2) = 1.0f - (f2xx + f2yy);
587
588
589
590
591
592
593
594
595
596QQuaternion QQuaternion::fromRotationMatrix(
const QMatrix3x3 &rot3x3)
604 const float trace = rot3x3(0, 0) + rot3x3(1, 1) + rot3x3(2, 2);
605 if (trace > 0.00000001f) {
606 const float s = 2.0f * std::sqrt(trace + 1.0f);
608 axis[0] = (rot3x3(2, 1) - rot3x3(1, 2)) / s;
609 axis[1] = (rot3x3(0, 2) - rot3x3(2, 0)) / s;
610 axis[2] = (rot3x3(1, 0) - rot3x3(0, 1)) / s;
612 static int s_next[3] = { 1, 2, 0 };
614 if (rot3x3(1, 1) > rot3x3(0, 0))
616 if (rot3x3(2, 2) > rot3x3(i, i))
621 const float s = 2.0f * std::sqrt(rot3x3(i, i) - rot3x3(j, j) - rot3x3(k, k) + 1.0f);
623 scalar = (rot3x3(k, j) - rot3x3(j, k)) / s;
624 axis[j] = (rot3x3(j, i) + rot3x3(i, j)) / s;
625 axis[k] = (rot3x3(k, i) + rot3x3(i, k)) / s;
628 return QQuaternion(scalar, axis[0], axis[1], axis[2]);
631#ifndef QT_NO_VECTOR3D
634
635
636
637
638
639
640void QQuaternion::getAxes(QVector3D *xAxis, QVector3D *yAxis, QVector3D *zAxis)
const
642 Q_ASSERT(xAxis && yAxis && zAxis);
644 const QMatrix3x3 rot3x3(toRotationMatrix());
646 *xAxis = QVector3D(rot3x3(0, 0), rot3x3(1, 0), rot3x3(2, 0));
647 *yAxis = QVector3D(rot3x3(0, 1), rot3x3(1, 1), rot3x3(2, 1));
648 *zAxis = QVector3D(rot3x3(0, 2), rot3x3(1, 2), rot3x3(2, 2));
652
653
654
655
656
657
658
659
660QQuaternion QQuaternion::fromAxes(
const QVector3D &xAxis,
const QVector3D &yAxis,
const QVector3D &zAxis)
662 QMatrix3x3 rot3x3(Qt::Uninitialized);
663 rot3x3(0, 0) = xAxis.x();
664 rot3x3(1, 0) = xAxis.y();
665 rot3x3(2, 0) = xAxis.z();
666 rot3x3(0, 1) = yAxis.x();
667 rot3x3(1, 1) = yAxis.y();
668 rot3x3(2, 1) = yAxis.z();
669 rot3x3(0, 2) = zAxis.x();
670 rot3x3(1, 2) = zAxis.y();
671 rot3x3(2, 2) = zAxis.z();
673 return QQuaternion::fromRotationMatrix(rot3x3);
677
678
679
680
681
682
683
684
685
686QQuaternion QQuaternion::fromDirection(
const QVector3D &direction,
const QVector3D &up)
688 if (qFuzzyIsNull(direction.x()) && qFuzzyIsNull(direction.y()) && qFuzzyIsNull(direction.z()))
689 return QQuaternion();
691 const QVector3D zAxis(direction.normalized());
692 QVector3D xAxis(QVector3D::crossProduct(up, zAxis));
693 if (qFuzzyIsNull(xAxis.lengthSquared())) {
695 return QQuaternion::rotationTo(QVector3D(0.0f, 0.0f, 1.0f), zAxis);
699 const QVector3D yAxis(QVector3D::crossProduct(zAxis, xAxis));
701 return QQuaternion::fromAxes(xAxis, yAxis, zAxis);
705
706
707
708
709
710
711
712QQuaternion QQuaternion::rotationTo(
const QVector3D &from,
const QVector3D &to)
716 const QVector3D v0(from.normalized());
717 const QVector3D v1(to.normalized());
719 float d = QVector3D::dotProduct(v0, v1) + 1.0f;
722 if (qFuzzyIsNull(d)) {
723 QVector3D axis = QVector3D::crossProduct(QVector3D(1.0f, 0.0f, 0.0f), v0);
724 if (qFuzzyIsNull(axis.lengthSquared()))
725 axis = QVector3D::crossProduct(QVector3D(0.0f, 1.0f, 0.0f), v0);
729 return QQuaternion(0.0f, axis.x(), axis.y(), axis.z());
732 d = std::sqrt(2.0f * d);
733 const QVector3D axis(QVector3D::crossProduct(v0, v1) / d);
735 return QQuaternion(d * 0.5f, axis).normalized();
741
742
743
744
745
748
749
750
751
752
755
756
757
758
759
760
761
762
765
766
767
768
769
770
771
772
775
776
777
778
779
780
781
782
785
786
787
788
789
790
791
792
795
796
797
798
799
800
801
802
803
806
807
808
809
810
811
812
813
814
817
818
819
820
821
822
823
824
826#ifndef QT_NO_VECTOR3D
829
830
831
832
833
834
839
840
841
842
843
844
847
848
849
850
851
852
853
854
855
856
857QQuaternion QQuaternion::slerp
858 (
const QQuaternion &q1,
const QQuaternion &q2,
float t)
868 float dot = QQuaternion::dotProduct(q1, q2);
876 float factor1 = 1.0f - t;
878 if ((1.0f - dot) > 0.0000001) {
879 float angle = std::acos(dot);
880 float sinOfAngle = std::sin(angle);
881 if (sinOfAngle > 0.0000001) {
882 factor1 = std::sin((1.0f - t) * angle) / sinOfAngle;
883 factor2 = std::sin(t * angle) / sinOfAngle;
888 return q1 * factor1 + q2b * factor2;
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906QQuaternion QQuaternion::nlerp
907 (
const QQuaternion &q1,
const QQuaternion &q2,
float t)
917 float dot = QQuaternion::dotProduct(q1, q2);
922 return (q1 * (1.0f - t) + q2b * t).normalized();
926
927
928QQuaternion::operator QVariant()
const
930 return QVariant::fromValue(*
this);
933#ifndef QT_NO_DEBUG_STREAM
937 QDebugStateSaver saver(dbg);
938 dbg.nospace() <<
"QQuaternion(scalar:" << q.scalar()
939 <<
", vector:(" << q.x() <<
", "
940 << q.y() <<
", " << q.z() <<
"))";
946#ifndef QT_NO_DATASTREAM
949
950
951
952
953
954
955
956
960 stream << quaternion.scalar() << quaternion.x()
961 << quaternion.y() << quaternion.z();
966
967
968
969
970
971
972
973
977 float scalar, x, y, z;
982 quaternion.setScalar(scalar);
QDebug operator<<(QDebug dbg, const QFileInfo &fi)