|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtGui module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
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 |
|
48 QT_BEGIN_HEADER |
|
49 |
|
50 QT_BEGIN_NAMESPACE |
|
51 |
|
52 QT_MODULE(Gui) |
|
53 |
|
54 #ifndef QT_NO_QUATERNION |
|
55 |
|
56 class QMatrix4x4; |
|
57 class QVariant; |
|
58 |
|
59 class Q_GUI_EXPORT QQuaternion |
|
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 rotateVector(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 |
|
139 inline QQuaternion::QQuaternion() : wp(1.0f), xp(0.0f), yp(0.0f), zp(0.0f) {} |
|
140 |
|
141 inline QQuaternion::QQuaternion(qreal aScalar, qreal xpos, qreal ypos, qreal zpos) : wp(aScalar), xp(xpos), yp(ypos), zp(zpos) {} |
|
142 |
|
143 |
|
144 inline bool QQuaternion::isNull() const |
|
145 { |
|
146 return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && qIsNull(wp); |
|
147 } |
|
148 |
|
149 inline bool QQuaternion::isIdentity() const |
|
150 { |
|
151 return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && wp == 1.0f; |
|
152 } |
|
153 |
|
154 inline qreal QQuaternion::x() const { return qreal(xp); } |
|
155 inline qreal QQuaternion::y() const { return qreal(yp); } |
|
156 inline qreal QQuaternion::z() const { return qreal(zp); } |
|
157 inline qreal QQuaternion::scalar() const { return qreal(wp); } |
|
158 |
|
159 inline void QQuaternion::setX(qreal aX) { xp = aX; } |
|
160 inline void QQuaternion::setY(qreal aY) { yp = aY; } |
|
161 inline void QQuaternion::setZ(qreal aZ) { zp = aZ; } |
|
162 inline void QQuaternion::setScalar(qreal aScalar) { wp = aScalar; } |
|
163 |
|
164 inline QQuaternion QQuaternion::conjugate() const |
|
165 { |
|
166 return QQuaternion(wp, -xp, -yp, -zp); |
|
167 } |
|
168 |
|
169 inline QQuaternion &QQuaternion::operator+=(const QQuaternion &quaternion) |
|
170 { |
|
171 xp += quaternion.xp; |
|
172 yp += quaternion.yp; |
|
173 zp += quaternion.zp; |
|
174 wp += quaternion.wp; |
|
175 return *this; |
|
176 } |
|
177 |
|
178 inline QQuaternion &QQuaternion::operator-=(const QQuaternion &quaternion) |
|
179 { |
|
180 xp -= quaternion.xp; |
|
181 yp -= quaternion.yp; |
|
182 zp -= quaternion.zp; |
|
183 wp -= quaternion.wp; |
|
184 return *this; |
|
185 } |
|
186 |
|
187 inline QQuaternion &QQuaternion::operator*=(qreal factor) |
|
188 { |
|
189 xp *= factor; |
|
190 yp *= factor; |
|
191 zp *= factor; |
|
192 wp *= factor; |
|
193 return *this; |
|
194 } |
|
195 |
|
196 inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2) |
|
197 { |
|
198 qreal ww = (q1.zp + q1.xp) * (q2.xp + q2.yp); |
|
199 qreal yy = (q1.wp - q1.yp) * (q2.wp + q2.zp); |
|
200 qreal zz = (q1.wp + q1.yp) * (q2.wp - q2.zp); |
|
201 qreal xx = ww + yy + zz; |
|
202 qreal qq = 0.5 * (xx + (q1.zp - q1.xp) * (q2.xp - q2.yp)); |
|
203 |
|
204 qreal w = qq - ww + (q1.zp - q1.yp) * (q2.yp - q2.zp); |
|
205 qreal x = qq - xx + (q1.xp + q1.wp) * (q2.xp + q2.wp); |
|
206 qreal y = qq - yy + (q1.wp - q1.xp) * (q2.yp + q2.zp); |
|
207 qreal z = qq - zz + (q1.zp + q1.yp) * (q2.wp - q2.xp); |
|
208 |
|
209 return QQuaternion(w, x, y, z); |
|
210 } |
|
211 |
|
212 inline QQuaternion &QQuaternion::operator*=(const QQuaternion &quaternion) |
|
213 { |
|
214 *this = *this * quaternion; |
|
215 return *this; |
|
216 } |
|
217 |
|
218 inline QQuaternion &QQuaternion::operator/=(qreal divisor) |
|
219 { |
|
220 xp /= divisor; |
|
221 yp /= divisor; |
|
222 zp /= divisor; |
|
223 wp /= divisor; |
|
224 return *this; |
|
225 } |
|
226 |
|
227 inline bool operator==(const QQuaternion &q1, const QQuaternion &q2) |
|
228 { |
|
229 return q1.xp == q2.xp && q1.yp == q2.yp && q1.zp == q2.zp && q1.wp == q2.wp; |
|
230 } |
|
231 |
|
232 inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2) |
|
233 { |
|
234 return q1.xp != q2.xp || q1.yp != q2.yp || q1.zp != q2.zp || q1.wp != q2.wp; |
|
235 } |
|
236 |
|
237 inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2) |
|
238 { |
|
239 return QQuaternion(q1.wp + q2.wp, q1.xp + q2.xp, q1.yp + q2.yp, q1.zp + q2.zp); |
|
240 } |
|
241 |
|
242 inline const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2) |
|
243 { |
|
244 return QQuaternion(q1.wp - q2.wp, q1.xp - q2.xp, q1.yp - q2.yp, q1.zp - q2.zp); |
|
245 } |
|
246 |
|
247 inline const QQuaternion operator*(qreal factor, const QQuaternion &quaternion) |
|
248 { |
|
249 return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor); |
|
250 } |
|
251 |
|
252 inline const QQuaternion operator*(const QQuaternion &quaternion, qreal factor) |
|
253 { |
|
254 return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor); |
|
255 } |
|
256 |
|
257 inline const QQuaternion operator-(const QQuaternion &quaternion) |
|
258 { |
|
259 return QQuaternion(-quaternion.wp, -quaternion.xp, -quaternion.yp, -quaternion.zp); |
|
260 } |
|
261 |
|
262 inline const QQuaternion operator/(const QQuaternion &quaternion, qreal divisor) |
|
263 { |
|
264 return QQuaternion(quaternion.wp / divisor, quaternion.xp / divisor, quaternion.yp / divisor, quaternion.zp / divisor); |
|
265 } |
|
266 |
|
267 inline bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2) |
|
268 { |
|
269 return qFuzzyCompare(q1.xp, q2.xp) && |
|
270 qFuzzyCompare(q1.yp, q2.yp) && |
|
271 qFuzzyCompare(q1.zp, q2.zp) && |
|
272 qFuzzyCompare(q1.wp, q2.wp); |
|
273 } |
|
274 |
|
275 #ifndef QT_NO_VECTOR3D |
|
276 |
|
277 inline QQuaternion::QQuaternion(qreal aScalar, const QVector3D& aVector) |
|
278 : wp(aScalar), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {} |
|
279 |
|
280 inline void QQuaternion::setVector(const QVector3D& aVector) |
|
281 { |
|
282 xp = aVector.x(); |
|
283 yp = aVector.y(); |
|
284 zp = aVector.z(); |
|
285 } |
|
286 |
|
287 inline QVector3D QQuaternion::vector() const |
|
288 { |
|
289 return QVector3D(xp, yp, zp); |
|
290 } |
|
291 |
|
292 #endif |
|
293 |
|
294 inline void QQuaternion::setVector(qreal aX, qreal aY, qreal aZ) |
|
295 { |
|
296 xp = aX; |
|
297 yp = aY; |
|
298 zp = aZ; |
|
299 } |
|
300 |
|
301 #ifndef QT_NO_VECTOR4D |
|
302 |
|
303 inline QQuaternion::QQuaternion(const QVector4D& aVector) |
|
304 : wp(aVector.w()), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {} |
|
305 |
|
306 inline QVector4D QQuaternion::toVector4D() const |
|
307 { |
|
308 return QVector4D(xp, yp, zp, wp); |
|
309 } |
|
310 |
|
311 #endif |
|
312 |
|
313 #ifndef QT_NO_DEBUG_STREAM |
|
314 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QQuaternion &q); |
|
315 #endif |
|
316 |
|
317 #ifndef QT_NO_DATASTREAM |
|
318 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QQuaternion &); |
|
319 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QQuaternion &); |
|
320 #endif |
|
321 |
|
322 #endif |
|
323 |
|
324 QT_END_NAMESPACE |
|
325 |
|
326 QT_END_HEADER |
|
327 |
|
328 #endif |