1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QQUATERNION_H
41#define QQUATERNION_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtGui/qgenericmatrix.h>
45#include <QtGui/qvector3d.h>
46#include <QtGui/qvector4d.h>
47
48QT_BEGIN_NAMESPACE
49
50
51#ifndef QT_NO_QUATERNION
52
53class QMatrix4x4;
54class QVariant;
55
56class Q_GUI_EXPORT QQuaternion
57{
58public:
59 QQuaternion();
60 explicit QQuaternion(Qt::Initialization) {}
61 QQuaternion(float scalar, float xpos, float ypos, float zpos);
62#ifndef QT_NO_VECTOR3D
63 QQuaternion(float scalar, const QVector3D& vector);
64#endif
65#ifndef QT_NO_VECTOR4D
66 explicit QQuaternion(const QVector4D& vector);
67#endif
68
69 bool isNull() const;
70 bool isIdentity() const;
71
72#ifndef QT_NO_VECTOR3D
73 QVector3D vector() const;
74 void setVector(const QVector3D& vector);
75#endif
76 void setVector(float x, float y, float z);
77
78 float x() const;
79 float y() const;
80 float z() const;
81 float scalar() const;
82
83 void setX(float x);
84 void setY(float y);
85 void setZ(float z);
86 void setScalar(float scalar);
87
88 Q_DECL_CONSTEXPR static inline float dotProduct(const QQuaternion &q1, const QQuaternion &q2);
89
90 float length() const;
91 float lengthSquared() const;
92
93 Q_REQUIRED_RESULT QQuaternion normalized() const;
94 void normalize();
95
96 inline QQuaternion inverted() const;
97
98 Q_REQUIRED_RESULT QQuaternion conjugated() const;
99#if QT_DEPRECATED_SINCE(5, 5)
100 Q_REQUIRED_RESULT QT_DEPRECATED QQuaternion conjugate() const;
101#endif
102
103 QVector3D rotatedVector(const QVector3D& vector) const;
104
105 QQuaternion &operator+=(const QQuaternion &quaternion);
106 QQuaternion &operator-=(const QQuaternion &quaternion);
107 QQuaternion &operator*=(float factor);
108 QQuaternion &operator*=(const QQuaternion &quaternion);
109 QQuaternion &operator/=(float divisor);
110
111 friend inline bool operator==(const QQuaternion &q1, const QQuaternion &q2);
112 friend inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2);
113 friend inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2);
114 friend inline const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2);
115 friend inline const QQuaternion operator*(float factor, const QQuaternion &quaternion);
116 friend inline const QQuaternion operator*(const QQuaternion &quaternion, float factor);
117 friend inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2);
118 friend inline const QQuaternion operator-(const QQuaternion &quaternion);
119 friend inline const QQuaternion operator/(const QQuaternion &quaternion, float divisor);
120
121 friend inline bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2);
122
123#ifndef QT_NO_VECTOR4D
124 QVector4D toVector4D() const;
125#endif
126
127 operator QVariant() const;
128
129#ifndef QT_NO_VECTOR3D
130 inline void getAxisAndAngle(QVector3D *axis, float *angle) const;
131 static QQuaternion fromAxisAndAngle(const QVector3D& axis, float angle);
132#endif
133 void getAxisAndAngle(float *x, float *y, float *z, float *angle) const;
134 static QQuaternion fromAxisAndAngle
135 (float x, float y, float z, float angle);
136
137#ifndef QT_NO_VECTOR3D
138 inline QVector3D toEulerAngles() const;
139 static inline QQuaternion fromEulerAngles(const QVector3D &eulerAngles);
140#endif
141 void getEulerAngles(float *pitch, float *yaw, float *roll) const;
142 static QQuaternion fromEulerAngles(float pitch, float yaw, float roll);
143
144 QMatrix3x3 toRotationMatrix() const;
145 static QQuaternion fromRotationMatrix(const QMatrix3x3 &rot3x3);
146
147#ifndef QT_NO_VECTOR3D
148 void getAxes(QVector3D *xAxis, QVector3D *yAxis, QVector3D *zAxis) const;
149 static QQuaternion fromAxes(const QVector3D &xAxis, const QVector3D &yAxis, const QVector3D &zAxis);
150
151 static QQuaternion fromDirection(const QVector3D &direction, const QVector3D &up);
152
153 static QQuaternion rotationTo(const QVector3D &from, const QVector3D &to);
154#endif
155
156 static QQuaternion slerp
157 (const QQuaternion& q1, const QQuaternion& q2, float t);
158 static QQuaternion nlerp
159 (const QQuaternion& q1, const QQuaternion& q2, float t);
160
161private:
162 float wp, xp, yp, zp;
163};
164
165Q_DECLARE_TYPEINFO(QQuaternion, Q_MOVABLE_TYPE);
166
167inline QQuaternion::QQuaternion() : wp(1.0f), xp(0.0f), yp(0.0f), zp(0.0f) {}
168
169inline QQuaternion::QQuaternion(float aScalar, float xpos, float ypos, float zpos) : wp(aScalar), xp(xpos), yp(ypos), zp(zpos) {}
170
171QT_WARNING_PUSH
172QT_WARNING_DISABLE_CLANG("-Wfloat-equal")
173QT_WARNING_DISABLE_GCC("-Wfloat-equal")
174inline bool QQuaternion::isNull() const
175{
176 return wp == 0.0f && xp == 0.0f && yp == 0.0f && zp == 0.0f;
177}
178
179inline bool QQuaternion::isIdentity() const
180{
181 return wp == 1.0f && xp == 0.0f && yp == 0.0f && zp == 0.0f;
182}
183
184inline bool operator==(const QQuaternion &q1, const QQuaternion &q2)
185{
186 return q1.wp == q2.wp && q1.xp == q2.xp && q1.yp == q2.yp && q1.zp == q2.zp;
187}
188QT_WARNING_POP
189
190inline float QQuaternion::x() const { return xp; }
191inline float QQuaternion::y() const { return yp; }
192inline float QQuaternion::z() const { return zp; }
193inline float QQuaternion::scalar() const { return wp; }
194
195inline void QQuaternion::setX(float aX) { xp = aX; }
196inline void QQuaternion::setY(float aY) { yp = aY; }
197inline void QQuaternion::setZ(float aZ) { zp = aZ; }
198inline void QQuaternion::setScalar(float aScalar) { wp = aScalar; }
199
200Q_DECL_CONSTEXPR inline float QQuaternion::dotProduct(const QQuaternion &q1, const QQuaternion &q2)
201{
202 return q1.wp * q2.wp + q1.xp * q2.xp + q1.yp * q2.yp + q1.zp * q2.zp;
203}
204
205inline QQuaternion QQuaternion::inverted() const
206{
207 // Need some extra precision if the length is very small.
208 double len = double(wp) * double(wp) +
209 double(xp) * double(xp) +
210 double(yp) * double(yp) +
211 double(zp) * double(zp);
212 if (!qFuzzyIsNull(len))
213 return QQuaternion(float(double(wp) / len), float(double(-xp) / len),
214 float(double(-yp) / len), float(double(-zp) / len));
215 return QQuaternion(0.0f, 0.0f, 0.0f, 0.0f);
216}
217
218inline QQuaternion QQuaternion::conjugated() const
219{
220 return QQuaternion(wp, -xp, -yp, -zp);
221}
222
223#if QT_DEPRECATED_SINCE(5, 5)
224inline QQuaternion QQuaternion::conjugate() const
225{
226 return conjugated();
227}
228#endif
229
230inline QQuaternion &QQuaternion::operator+=(const QQuaternion &quaternion)
231{
232 wp += quaternion.wp;
233 xp += quaternion.xp;
234 yp += quaternion.yp;
235 zp += quaternion.zp;
236 return *this;
237}
238
239inline QQuaternion &QQuaternion::operator-=(const QQuaternion &quaternion)
240{
241 wp -= quaternion.wp;
242 xp -= quaternion.xp;
243 yp -= quaternion.yp;
244 zp -= quaternion.zp;
245 return *this;
246}
247
248inline QQuaternion &QQuaternion::operator*=(float factor)
249{
250 wp *= factor;
251 xp *= factor;
252 yp *= factor;
253 zp *= factor;
254 return *this;
255}
256
257inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2)
258{
259 float yy = (q1.wp - q1.yp) * (q2.wp + q2.zp);
260 float zz = (q1.wp + q1.yp) * (q2.wp - q2.zp);
261 float ww = (q1.zp + q1.xp) * (q2.xp + q2.yp);
262 float xx = ww + yy + zz;
263 float qq = 0.5f * (xx + (q1.zp - q1.xp) * (q2.xp - q2.yp));
264
265 float w = qq - ww + (q1.zp - q1.yp) * (q2.yp - q2.zp);
266 float x = qq - xx + (q1.xp + q1.wp) * (q2.xp + q2.wp);
267 float y = qq - yy + (q1.wp - q1.xp) * (q2.yp + q2.zp);
268 float z = qq - zz + (q1.zp + q1.yp) * (q2.wp - q2.xp);
269
270 return QQuaternion(w, x, y, z);
271}
272
273inline QQuaternion &QQuaternion::operator*=(const QQuaternion &quaternion)
274{
275 *this = *this * quaternion;
276 return *this;
277}
278
279inline QQuaternion &QQuaternion::operator/=(float divisor)
280{
281 wp /= divisor;
282 xp /= divisor;
283 yp /= divisor;
284 zp /= divisor;
285 return *this;
286}
287
288inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2)
289{
290 return !operator==(q1, q2);
291}
292
293inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2)
294{
295 return QQuaternion(q1.wp + q2.wp, q1.xp + q2.xp, q1.yp + q2.yp, q1.zp + q2.zp);
296}
297
298inline const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2)
299{
300 return QQuaternion(q1.wp - q2.wp, q1.xp - q2.xp, q1.yp - q2.yp, q1.zp - q2.zp);
301}
302
303inline const QQuaternion operator*(float factor, const QQuaternion &quaternion)
304{
305 return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor);
306}
307
308inline const QQuaternion operator*(const QQuaternion &quaternion, float factor)
309{
310 return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor);
311}
312
313inline const QQuaternion operator-(const QQuaternion &quaternion)
314{
315 return QQuaternion(-quaternion.wp, -quaternion.xp, -quaternion.yp, -quaternion.zp);
316}
317
318inline const QQuaternion operator/(const QQuaternion &quaternion, float divisor)
319{
320 return QQuaternion(quaternion.wp / divisor, quaternion.xp / divisor, quaternion.yp / divisor, quaternion.zp / divisor);
321}
322
323inline bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2)
324{
325 return qFuzzyCompare(q1.wp, q2.wp) &&
326 qFuzzyCompare(q1.xp, q2.xp) &&
327 qFuzzyCompare(q1.yp, q2.yp) &&
328 qFuzzyCompare(q1.zp, q2.zp);
329}
330
331#ifndef QT_NO_VECTOR3D
332
333inline QQuaternion::QQuaternion(float aScalar, const QVector3D& aVector)
334 : wp(aScalar), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
335
336inline void QQuaternion::setVector(const QVector3D& aVector)
337{
338 xp = aVector.x();
339 yp = aVector.y();
340 zp = aVector.z();
341}
342
343inline QVector3D QQuaternion::vector() const
344{
345 return QVector3D(xp, yp, zp);
346}
347
348inline QVector3D operator*(const QQuaternion &quaternion, const QVector3D &vec)
349{
350 return quaternion.rotatedVector(vec);
351}
352
353inline void QQuaternion::getAxisAndAngle(QVector3D *axis, float *angle) const
354{
355 float aX, aY, aZ;
356 getAxisAndAngle(&aX, &aY, &aZ, angle);
357 *axis = QVector3D(aX, aY, aZ);
358}
359
360inline QVector3D QQuaternion::toEulerAngles() const
361{
362 float pitch, yaw, roll;
363 getEulerAngles(&pitch, &yaw, &roll);
364 return QVector3D(pitch, yaw, roll);
365}
366
367inline QQuaternion QQuaternion::fromEulerAngles(const QVector3D &eulerAngles)
368{
369 return QQuaternion::fromEulerAngles(eulerAngles.x(), eulerAngles.y(), eulerAngles.z());
370}
371
372#endif
373
374inline void QQuaternion::setVector(float aX, float aY, float aZ)
375{
376 xp = aX;
377 yp = aY;
378 zp = aZ;
379}
380
381#ifndef QT_NO_VECTOR4D
382
383inline QQuaternion::QQuaternion(const QVector4D& aVector)
384 : wp(aVector.w()), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
385
386inline QVector4D QQuaternion::toVector4D() const
387{
388 return QVector4D(xp, yp, zp, wp);
389}
390
391#endif
392
393#ifndef QT_NO_DEBUG_STREAM
394Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QQuaternion &q);
395#endif
396
397#ifndef QT_NO_DATASTREAM
398Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QQuaternion &);
399Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QQuaternion &);
400#endif
401
402#endif
403
404QT_END_NAMESPACE
405
406#endif
407