source: trunk/src/gui/math3d/qquaternion.h@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 9.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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
48QT_BEGIN_HEADER
49
50QT_BEGIN_NAMESPACE
51
52QT_MODULE(Gui)
53
54#ifndef QT_NO_QUATERNION
55
56class QMatrix4x4;
57class QVariant;
58
59class Q_GUI_EXPORT QQuaternion
60{
61public:
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
135private:
136 qreal wp, xp, yp, zp;
137};
138
139Q_DECLARE_TYPEINFO(QQuaternion, Q_MOVABLE_TYPE);
140
141inline QQuaternion::QQuaternion() : wp(1.0f), xp(0.0f), yp(0.0f), zp(0.0f) {}
142
143inline QQuaternion::QQuaternion(qreal aScalar, qreal xpos, qreal ypos, qreal zpos) : wp(aScalar), xp(xpos), yp(ypos), zp(zpos) {}
144
145
146inline bool QQuaternion::isNull() const
147{
148 return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && qIsNull(wp);
149}
150
151inline bool QQuaternion::isIdentity() const
152{
153 return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && wp == 1.0f;
154}
155
156inline qreal QQuaternion::x() const { return qreal(xp); }
157inline qreal QQuaternion::y() const { return qreal(yp); }
158inline qreal QQuaternion::z() const { return qreal(zp); }
159inline qreal QQuaternion::scalar() const { return qreal(wp); }
160
161inline void QQuaternion::setX(qreal aX) { xp = aX; }
162inline void QQuaternion::setY(qreal aY) { yp = aY; }
163inline void QQuaternion::setZ(qreal aZ) { zp = aZ; }
164inline void QQuaternion::setScalar(qreal aScalar) { wp = aScalar; }
165
166inline QQuaternion QQuaternion::conjugate() const
167{
168 return QQuaternion(wp, -xp, -yp, -zp);
169}
170
171inline QQuaternion &QQuaternion::operator+=(const QQuaternion &quaternion)
172{
173 xp += quaternion.xp;
174 yp += quaternion.yp;
175 zp += quaternion.zp;
176 wp += quaternion.wp;
177 return *this;
178}
179
180inline QQuaternion &QQuaternion::operator-=(const QQuaternion &quaternion)
181{
182 xp -= quaternion.xp;
183 yp -= quaternion.yp;
184 zp -= quaternion.zp;
185 wp -= quaternion.wp;
186 return *this;
187}
188
189inline QQuaternion &QQuaternion::operator*=(qreal factor)
190{
191 xp *= factor;
192 yp *= factor;
193 zp *= factor;
194 wp *= factor;
195 return *this;
196}
197
198inline 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
214inline QQuaternion &QQuaternion::operator*=(const QQuaternion &quaternion)
215{
216 *this = *this * quaternion;
217 return *this;
218}
219
220inline QQuaternion &QQuaternion::operator/=(qreal divisor)
221{
222 xp /= divisor;
223 yp /= divisor;
224 zp /= divisor;
225 wp /= divisor;
226 return *this;
227}
228
229inline 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
234inline 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
239inline 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
244inline 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
249inline 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
254inline 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
259inline const QQuaternion operator-(const QQuaternion &quaternion)
260{
261 return QQuaternion(-quaternion.wp, -quaternion.xp, -quaternion.yp, -quaternion.zp);
262}
263
264inline 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
269inline 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
279inline QQuaternion::QQuaternion(qreal aScalar, const QVector3D& aVector)
280 : wp(aScalar), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
281
282inline void QQuaternion::setVector(const QVector3D& aVector)
283{
284 xp = aVector.x();
285 yp = aVector.y();
286 zp = aVector.z();
287}
288
289inline QVector3D QQuaternion::vector() const
290{
291 return QVector3D(xp, yp, zp);
292}
293
294#endif
295
296inline 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
305inline QQuaternion::QQuaternion(const QVector4D& aVector)
306 : wp(aVector.w()), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
307
308inline QVector4D QQuaternion::toVector4D() const
309{
310 return QVector4D(xp, yp, zp, wp);
311}
312
313#endif
314
315#ifndef QT_NO_DEBUG_STREAM
316Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QQuaternion &q);
317#endif
318
319#ifndef QT_NO_DATASTREAM
320Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QQuaternion &);
321Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QQuaternion &);
322#endif
323
324#endif
325
326QT_END_NAMESPACE
327
328QT_END_HEADER
329
330#endif
Note: See TracBrowser for help on using the repository browser.