Qt 4.8
qquaternion.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QQUATERNION_H
43 #define QQUATERNION_H
44 
45 #include <QtGui/qvector3d.h>
46 #include <QtGui/qvector4d.h>
47 
49 
51 
52 QT_MODULE(Gui)
53 
54 #ifndef QT_NO_QUATERNION
55 
56 class QMatrix4x4;
57 class QVariant;
58 
60 {
61 public:
62  QQuaternion();
63  QQuaternion(qreal scalar, qreal xpos, qreal ypos, qreal zpos);
64 #ifndef QT_NO_VECTOR3D
65  QQuaternion(qreal scalar, const QVector3D& vector);
66 #endif
67 #ifndef QT_NO_VECTOR4D
68  explicit QQuaternion(const QVector4D& vector);
69 #endif
70 
71  bool isNull() const;
72  bool isIdentity() const;
73 
74 #ifndef QT_NO_VECTOR3D
75  QVector3D vector() const;
76  void setVector(const QVector3D& vector);
77 #endif
78  void setVector(qreal x, qreal y, qreal z);
79 
80  qreal x() const;
81  qreal y() const;
82  qreal z() const;
83  qreal scalar() const;
84 
85  void setX(qreal x);
86  void setY(qreal y);
87  void setZ(qreal z);
88  void setScalar(qreal scalar);
89 
90  qreal length() const;
91  qreal lengthSquared() const;
92 
93  QQuaternion normalized() const;
94  void normalize();
95 
96  QQuaternion conjugate() const;
97 
98  QVector3D rotatedVector(const QVector3D& vector) const;
99 
100  QQuaternion &operator+=(const QQuaternion &quaternion);
101  QQuaternion &operator-=(const QQuaternion &quaternion);
102  QQuaternion &operator*=(qreal factor);
103  QQuaternion &operator*=(const QQuaternion &quaternion);
104  QQuaternion &operator/=(qreal divisor);
105 
106  friend inline bool operator==(const QQuaternion &q1, const QQuaternion &q2);
107  friend inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2);
108  friend inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2);
109  friend inline const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2);
110  friend inline const QQuaternion operator*(qreal factor, const QQuaternion &quaternion);
111  friend inline const QQuaternion operator*(const QQuaternion &quaternion, qreal factor);
112  friend inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2);
113  friend inline const QQuaternion operator-(const QQuaternion &quaternion);
114  friend inline const QQuaternion operator/(const QQuaternion &quaternion, qreal divisor);
115 
116  friend inline bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2);
117 
118 #ifndef QT_NO_VECTOR4D
119  QVector4D toVector4D() const;
120 #endif
121 
122  operator QVariant() const;
123 
124 #ifndef QT_NO_VECTOR3D
125  static QQuaternion fromAxisAndAngle(const QVector3D& axis, qreal angle);
126 #endif
127  static QQuaternion fromAxisAndAngle
128  (qreal x, qreal y, qreal z, qreal angle);
129 
130  static QQuaternion slerp
131  (const QQuaternion& q1, const QQuaternion& q2, qreal t);
132  static QQuaternion nlerp
133  (const QQuaternion& q1, const QQuaternion& q2, qreal t);
134 
135 private:
136  qreal wp, xp, yp, zp;
137 };
138 
140 
141 inline QQuaternion::QQuaternion() : wp(1.0f), xp(0.0f), yp(0.0f), zp(0.0f) {}
142 
143 inline QQuaternion::QQuaternion(qreal aScalar, qreal xpos, qreal ypos, qreal zpos) : wp(aScalar), xp(xpos), yp(ypos), zp(zpos) {}
144 
145 
146 inline bool QQuaternion::isNull() const
147 {
148  return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && qIsNull(wp);
149 }
150 
151 inline bool QQuaternion::isIdentity() const
152 {
153  return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && wp == 1.0f;
154 }
155 
156 inline qreal QQuaternion::x() const { return qreal(xp); }
157 inline qreal QQuaternion::y() const { return qreal(yp); }
158 inline qreal QQuaternion::z() const { return qreal(zp); }
159 inline qreal QQuaternion::scalar() const { return qreal(wp); }
160 
161 inline void QQuaternion::setX(qreal aX) { xp = aX; }
162 inline void QQuaternion::setY(qreal aY) { yp = aY; }
163 inline void QQuaternion::setZ(qreal aZ) { zp = aZ; }
164 inline void QQuaternion::setScalar(qreal aScalar) { wp = aScalar; }
165 
167 {
168  return QQuaternion(wp, -xp, -yp, -zp);
169 }
170 
172 {
173  xp += quaternion.xp;
174  yp += quaternion.yp;
175  zp += quaternion.zp;
176  wp += quaternion.wp;
177  return *this;
178 }
179 
181 {
182  xp -= quaternion.xp;
183  yp -= quaternion.yp;
184  zp -= quaternion.zp;
185  wp -= quaternion.wp;
186  return *this;
187 }
188 
190 {
191  xp *= factor;
192  yp *= factor;
193  zp *= factor;
194  wp *= factor;
195  return *this;
196 }
197 
198 inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2)
199 {
200  qreal ww = (q1.zp + q1.xp) * (q2.xp + q2.yp);
201  qreal yy = (q1.wp - q1.yp) * (q2.wp + q2.zp);
202  qreal zz = (q1.wp + q1.yp) * (q2.wp - q2.zp);
203  qreal xx = ww + yy + zz;
204  qreal qq = 0.5 * (xx + (q1.zp - q1.xp) * (q2.xp - q2.yp));
205 
206  qreal w = qq - ww + (q1.zp - q1.yp) * (q2.yp - q2.zp);
207  qreal x = qq - xx + (q1.xp + q1.wp) * (q2.xp + q2.wp);
208  qreal y = qq - yy + (q1.wp - q1.xp) * (q2.yp + q2.zp);
209  qreal z = qq - zz + (q1.zp + q1.yp) * (q2.wp - q2.xp);
210 
211  return QQuaternion(w, x, y, z);
212 }
213 
215 {
216  *this = *this * quaternion;
217  return *this;
218 }
219 
221 {
222  xp /= divisor;
223  yp /= divisor;
224  zp /= divisor;
225  wp /= divisor;
226  return *this;
227 }
228 
229 inline bool operator==(const QQuaternion &q1, const QQuaternion &q2)
230 {
231  return q1.xp == q2.xp && q1.yp == q2.yp && q1.zp == q2.zp && q1.wp == q2.wp;
232 }
233 
234 inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2)
235 {
236  return q1.xp != q2.xp || q1.yp != q2.yp || q1.zp != q2.zp || q1.wp != q2.wp;
237 }
238 
239 inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2)
240 {
241  return QQuaternion(q1.wp + q2.wp, q1.xp + q2.xp, q1.yp + q2.yp, q1.zp + q2.zp);
242 }
243 
244 inline const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2)
245 {
246  return QQuaternion(q1.wp - q2.wp, q1.xp - q2.xp, q1.yp - q2.yp, q1.zp - q2.zp);
247 }
248 
249 inline const QQuaternion operator*(qreal factor, const QQuaternion &quaternion)
250 {
251  return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor);
252 }
253 
254 inline const QQuaternion operator*(const QQuaternion &quaternion, qreal factor)
255 {
256  return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor);
257 }
258 
259 inline const QQuaternion operator-(const QQuaternion &quaternion)
260 {
261  return QQuaternion(-quaternion.wp, -quaternion.xp, -quaternion.yp, -quaternion.zp);
262 }
263 
264 inline const QQuaternion operator/(const QQuaternion &quaternion, qreal divisor)
265 {
266  return QQuaternion(quaternion.wp / divisor, quaternion.xp / divisor, quaternion.yp / divisor, quaternion.zp / divisor);
267 }
268 
269 inline bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2)
270 {
271  return qFuzzyCompare(q1.xp, q2.xp) &&
272  qFuzzyCompare(q1.yp, q2.yp) &&
273  qFuzzyCompare(q1.zp, q2.zp) &&
274  qFuzzyCompare(q1.wp, q2.wp);
275 }
276 
277 #ifndef QT_NO_VECTOR3D
278 
279 inline QQuaternion::QQuaternion(qreal aScalar, const QVector3D& aVector)
280  : wp(aScalar), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
281 
282 inline void QQuaternion::setVector(const QVector3D& aVector)
283 {
284  xp = aVector.x();
285  yp = aVector.y();
286  zp = aVector.z();
287 }
288 
290 {
291  return QVector3D(xp, yp, zp);
292 }
293 
294 #endif
295 
296 inline void QQuaternion::setVector(qreal aX, qreal aY, qreal aZ)
297 {
298  xp = aX;
299  yp = aY;
300  zp = aZ;
301 }
302 
303 #ifndef QT_NO_VECTOR4D
304 
305 inline QQuaternion::QQuaternion(const QVector4D& aVector)
306  : wp(aVector.w()), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
307 
309 {
310  return QVector4D(xp, yp, zp, wp);
311 }
312 
313 #endif
314 
315 #ifndef QT_NO_DEBUG_STREAM
317 #endif
318 
319 #ifndef QT_NO_DATASTREAM
322 #endif
323 
324 #endif
325 
327 
329 
330 #endif
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QDataStream & operator>>(QDataStream &stream, QEasingCurve &easing)
Reads an easing curve from the given stream into the given easing curve and returns a reference to th...
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
QQuaternion()
Constructs an identity quaternion, i.
Definition: qquaternion.h:141
friend bool operator!=(const QQuaternion &q1, const QQuaternion &q2)
Returns true if q1 is not equal to q2; otherwise returns false.
Definition: qquaternion.h:234
The QVector3D class represents a vector or vertex in 3D space.
Definition: qvector3d.h:60
double qreal
Definition: qglobal.h:1193
qreal x() const
Returns the x coordinate of this quaternion&#39;s vector.
Definition: qquaternion.h:156
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setZ(qreal z)
Sets the z coordinate of this quaternion&#39;s vector to the given z coordinate.
Definition: qquaternion.h:163
#define QT_MODULE(x)
Definition: qglobal.h:2783
bool isNull() const
Returns true if the x, y, z, and scalar components of this quaternion are set to 0.
Definition: qquaternion.h:146
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
friend const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2)
Returns a QQuaternion object that is the sum of the given quaternions, q1 and q2; each component is a...
Definition: qquaternion.h:239
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
void setX(qreal x)
Sets the x coordinate of this quaternion&#39;s vector to the given x coordinate.
Definition: qquaternion.h:161
bool operator==(const QQuaternion &q1, const QQuaternion &q2)
Definition: qquaternion.h:229
The QVector4D class represents a vector or vertex in 4D space.
Definition: qvector4d.h:60
friend bool operator==(const QQuaternion &q1, const QQuaternion &q2)
Returns true if q1 is equal to q2; otherwise returns false.
Definition: qquaternion.h:229
friend const QQuaternion operator/(const QQuaternion &quaternion, qreal divisor)
Returns the QQuaternion object formed by dividing all components of the given quaternion by the given...
Definition: qquaternion.h:264
bool operator!=(const QQuaternion &q1, const QQuaternion &q2)
Definition: qquaternion.h:234
qreal x() const
Returns the x coordinate of this point.
Definition: qvector3d.h:161
bool isIdentity() const
Returns true if the x, y, and z components of this quaternion are set to 0.
Definition: qquaternion.h:151
static bool qIsNull(double d)
Definition: qglobal.h:2061
QQuaternion & operator-=(const QQuaternion &quaternion)
Subtracts the given quaternion from this quaternion and returns a reference to this quaternion...
Definition: qquaternion.h:180
QDataStream & operator<<(QDataStream &stream, const QQuaternion &quaternion)
Writes the given quaternion to the given stream and returns a reference to the stream.
QVector3D vector() const
Returns the vector component of this quaternion.
Definition: qquaternion.h:289
QQuaternion & operator/=(qreal divisor)
Divides this quaternion&#39;s components by the given divisor, and returns a reference to this quaternion...
Definition: qquaternion.h:220
QQuaternion & operator+=(const QQuaternion &quaternion)
Adds the given quaternion to this quaternion and returns a reference to this quaternion.
Definition: qquaternion.h:171
qreal scalar() const
Returns the scalar component of this quaternion.
Definition: qquaternion.h:159
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
friend const QQuaternion operator*(qreal factor, const QQuaternion &quaternion)
Definition: qquaternion.h:249
void setScalar(qreal scalar)
Sets the scalar component of this quaternion to scalar.
Definition: qquaternion.h:164
Q_DECLARE_TYPEINFO(QQuaternion, Q_MOVABLE_TYPE)
void setVector(const QVector3D &vector)
Sets the vector component of this quaternion to vector.
Definition: qquaternion.h:282
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition: qmatrix4x4.h:63
qreal z() const
Returns the z coordinate of this point.
Definition: qvector3d.h:163
bool qFuzzyCompare(const QQuaternion &q1, const QQuaternion &q2)
Definition: qquaternion.h:269
qreal y() const
Returns the y coordinate of this quaternion&#39;s vector.
Definition: qquaternion.h:157
const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2)
Definition: qquaternion.h:239
qreal angle(const QPointF &p1, const QPointF &p2)
QQuaternion & operator*=(qreal factor)
Multiplies this quaternion&#39;s components by the given factor, and returns a reference to this quaterni...
Definition: qquaternion.h:189
qreal z() const
Returns the z coordinate of this quaternion&#39;s vector.
Definition: qquaternion.h:158
const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2)
Definition: qquaternion.h:244
qreal y() const
Returns the y coordinate of this point.
Definition: qvector3d.h:162
The QQuaternion class represents a quaternion consisting of a vector and scalar.
Definition: qquaternion.h:59
QQuaternion conjugate() const
Returns the conjugate of this quaternion, which is (-x, -y, -z, scalar).
Definition: qquaternion.h:166
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
const QQuaternion operator*(const QQuaternion &q1, const QQuaternion &q2)
Definition: qquaternion.h:198
timeval & operator+=(timeval &t1, const timeval &t2)
Definition: qcore_unix_p.h:120
const QQuaternion operator/(const QQuaternion &quaternion, qreal divisor)
Definition: qquaternion.h:264
void setY(qreal y)
Sets the y coordinate of this quaternion&#39;s vector to the given y coordinate.
Definition: qquaternion.h:162
#define QT_END_HEADER
Definition: qglobal.h:137
static void normalize(double &x, double &y)
friend bool qFuzzyCompare(const QQuaternion &q1, const QQuaternion &q2)
Returns true if q1 and q2 are equal, allowing for a small fuzziness factor for floating-point compari...
Definition: qquaternion.h:269
static bool isNull(const QVariant::Private *d)
Definition: qvariant.cpp:300
friend const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2)
Returns a QQuaternion object that is formed by subtracting q2 from q1; each component is subtracted s...
Definition: qquaternion.h:244
QVector4D toVector4D() const
Returns this quaternion as a 4D vector.
Definition: qquaternion.h:308