- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# qquaternion.h

Go to the documentation of this file.

    00001 /****************************************************************************
    00002 **
    00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    00004 ** All rights reserved.
    00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
    00006 **
    00007 ** This file is part of the QtGui module of the Qt Toolkit.
    00008 **
    00009 ** $QT_BEGIN_LICENSE:LGPL$
    00010 ** Commercial Usage
    00011 ** Licensees holding valid Qt Commercial licenses may use this file in
    00012 ** accordance with the Qt Commercial License Agreement provided with the
    00013 ** Software or, alternatively, in accordance with the terms contained in
    00014 ** a written agreement between you and Nokia.
    00015 **
    00016 ** GNU Lesser General Public License Usage
    00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
    00018 ** General Public License version 2.1 as published by the Free Software
    00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
    00020 ** packaging of this file.  Please review the following information to
    00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
    00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    00023 **
    00024 ** In addition, as a special exception, Nokia gives you certain additional
    00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
    00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
    00027 **
    00028 ** GNU General Public License Usage
    00029 ** Alternatively, this file may be used under the terms of the GNU
    00030 ** General Public License version 3.0 as published by the Free Software
    00031 ** Foundation and appearing in the file LICENSE.GPL included in the
    00032 ** packaging of this file.  Please review the following information to
    00033 ** ensure the GNU General Public License version 3.0 requirements will be
    00034 ** met: http://www.gnu.org/copyleft/gpl.html.
    00035 **
    00036 ** If you have questions regarding the use of this file, please contact
    00037 ** Nokia at qt-info@nokia.com.
    00038 ** $QT_END_LICENSE$
    00039 **
    00040 ****************************************************************************/
    00041
    00042 #ifndef QQUATERNION_H
    00043 #define QQUATERNION_H
    00044
    00045 #include <QtGui/qvector3d.h>
    00046 #include <QtGui/qvector4d.h>
    00047
    00048 QT_BEGIN_HEADER
    00049
    00050 QT_BEGIN_NAMESPACE
    00051
    00052 QT_MODULE(Gui)
    00053
    00054 #ifndef QT_NO_QUATERNION
    00055
    00056 class QMatrix4x4;
    00057 class QVariant;
    00058
    00059 class Q_GUI_EXPORT QQuaternion
    00060 {
    00061 public:
    00062     QQuaternion();
    00063     QQuaternion(qreal scalar, qreal xpos, qreal ypos, qreal zpos);
    00064 #ifndef QT_NO_VECTOR3D
    00065     QQuaternion(qreal scalar, const QVector3D& vector);
    00066 #endif
    00067 #ifndef QT_NO_VECTOR4D
    00068     explicit QQuaternion(const QVector4D& vector);
    00069 #endif
    00070
    00071     bool isNull() const;
    00072     bool isIdentity() const;
    00073
    00074 #ifndef QT_NO_VECTOR3D
    00075     QVector3D vector() const;
    00076     void setVector(const QVector3D& vector);
    00077 #endif
    00078     void setVector(qreal x, qreal y, qreal z);
    00079
    00080     qreal x() const;
    00081     qreal y() const;
    00082     qreal z() const;
    00083     qreal scalar() const;
    00084
    00085     void setX(qreal x);
    00086     void setY(qreal y);
    00087     void setZ(qreal z);
    00088     void setScalar(qreal scalar);
    00089
    00090     qreal length() const;
    00091     qreal lengthSquared() const;
    00092
    00093     QQuaternion normalized() const;
    00094     void normalize();
    00095
    00096     QQuaternion conjugate() const;
    00097
    00098     QVector3D rotatedVector(const QVector3D& vector) const;
    00099
    00100     QQuaternion &operator+=(const QQuaternion &quaternion);
    00101     QQuaternion &operator-=(const QQuaternion &quaternion);
    00102     QQuaternion &operator*=(qreal factor);
    00103     QQuaternion &operator*=(const QQuaternion &quaternion);
    00104     QQuaternion &operator/=(qreal divisor);
    00105
    00106     friend inline bool operator==(const QQuaternion &q1, const QQuaternion &q2);
    00107     friend inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2);
    00108     friend inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2);
    00109     friend inline const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2);
    00110     friend inline const QQuaternion operator*(qreal factor, const QQuaternion &quaternion);
    00111     friend inline const QQuaternion operator*(const QQuaternion &quaternion, qreal factor);
    00112     friend inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2);
    00113     friend inline const QQuaternion operator-(const QQuaternion &quaternion);
    00114     friend inline const QQuaternion operator/(const QQuaternion &quaternion, qreal divisor);
    00115
    00116     friend inline bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2);
    00117
    00118 #ifndef QT_NO_VECTOR4D
    00119     QVector4D toVector4D() const;
    00120 #endif
    00121
    00122     operator QVariant() const;
    00123
    00124 #ifndef QT_NO_VECTOR3D
    00125     static QQuaternion fromAxisAndAngle(const QVector3D& axis, qreal angle);
    00126 #endif
    00127     static QQuaternion fromAxisAndAngle
    00128             (qreal x, qreal y, qreal z, qreal angle);
    00129
    00130     static QQuaternion slerp
    00131         (const QQuaternion& q1, const QQuaternion& q2, qreal t);
    00132     static QQuaternion nlerp
    00133         (const QQuaternion& q1, const QQuaternion& q2, qreal t);
    00134
    00135 private:
    00136     qreal wp, xp, yp, zp;
    00137 };
    00138
    00139 Q_DECLARE_TYPEINFO(QQuaternion, Q_MOVABLE_TYPE);
    00140
    00141 inline QQuaternion::QQuaternion() : wp(1.0f), xp(0.0f), yp(0.0f), zp(0.0f) {}
    00142
    00143 inline QQuaternion::QQuaternion(qreal aScalar, qreal xpos, qreal ypos, qreal zpos) : wp(aScalar), xp(xpos), yp(ypos), zp(zpos) {}
    00144
    00145
    00146 inline bool QQuaternion::isNull() const
    00147 {
    00148     return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && qIsNull(wp);
    00149 }
    00150
    00151 inline bool QQuaternion::isIdentity() const
    00152 {
    00153     return qIsNull(xp) && qIsNull(yp) && qIsNull(zp) && wp == 1.0f;
    00154 }
    00155
    00156 inline qreal QQuaternion::x() const { return qreal(xp); }
    00157 inline qreal QQuaternion::y() const { return qreal(yp); }
    00158 inline qreal QQuaternion::z() const { return qreal(zp); }
    00159 inline qreal QQuaternion::scalar() const { return qreal(wp); }
    00160
    00161 inline void QQuaternion::setX(qreal aX) { xp = aX; }
    00162 inline void QQuaternion::setY(qreal aY) { yp = aY; }
    00163 inline void QQuaternion::setZ(qreal aZ) { zp = aZ; }
    00164 inline void QQuaternion::setScalar(qreal aScalar) { wp = aScalar; }
    00165
    00166 inline QQuaternion QQuaternion::conjugate() const
    00167 {
    00168     return QQuaternion(wp, -xp, -yp, -zp);
    00169 }
    00170
    00171 inline QQuaternion &QQuaternion::operator+=(const QQuaternion &quaternion)
    00172 {
    00173     xp += quaternion.xp;
    00174     yp += quaternion.yp;
    00175     zp += quaternion.zp;
    00176     wp += quaternion.wp;
    00177     return *this;
    00178 }
    00179
    00180 inline QQuaternion &QQuaternion::operator-=(const QQuaternion &quaternion)
    00181 {
    00182     xp -= quaternion.xp;
    00183     yp -= quaternion.yp;
    00184     zp -= quaternion.zp;
    00185     wp -= quaternion.wp;
    00186     return *this;
    00187 }
    00188
    00189 inline QQuaternion &QQuaternion::operator*=(qreal factor)
    00190 {
    00191     xp *= factor;
    00192     yp *= factor;
    00193     zp *= factor;
    00194     wp *= factor;
    00195     return *this;
    00196 }
    00197
    00198 inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2)
    00199 {
    00200     qreal ww = (q1.zp + q1.xp) * (q2.xp + q2.yp);
    00201     qreal yy = (q1.wp - q1.yp) * (q2.wp + q2.zp);
    00202     qreal zz = (q1.wp + q1.yp) * (q2.wp - q2.zp);
    00203     qreal xx = ww + yy + zz;
    00204     qreal qq = 0.5 * (xx + (q1.zp - q1.xp) * (q2.xp - q2.yp));
    00205
    00206     qreal w = qq - ww + (q1.zp - q1.yp) * (q2.yp - q2.zp);
    00207     qreal x = qq - xx + (q1.xp + q1.wp) * (q2.xp + q2.wp);
    00208     qreal y = qq - yy + (q1.wp - q1.xp) * (q2.yp + q2.zp);
    00209     qreal z = qq - zz + (q1.zp + q1.yp) * (q2.wp - q2.xp);
    00210
    00211     return QQuaternion(w, x, y, z);
    00212 }
    00213
    00214 inline QQuaternion &QQuaternion::operator*=(const QQuaternion &quaternion)
    00215 {
    00216     *this = *this * quaternion;
    00217     return *this;
    00218 }
    00219
    00220 inline QQuaternion &QQuaternion::operator/=(qreal divisor)
    00221 {
    00222     xp /= divisor;
    00223     yp /= divisor;
    00224     zp /= divisor;
    00225     wp /= divisor;
    00226     return *this;
    00227 }
    00228
    00229 inline bool operator==(const QQuaternion &q1, const QQuaternion &q2)
    00230 {
    00231     return q1.xp == q2.xp && q1.yp == q2.yp && q1.zp == q2.zp && q1.wp == q2.wp;
    00232 }
    00233
    00234 inline bool operator!=(const QQuaternion &q1, const QQuaternion &q2)
    00235 {
    00236     return q1.xp != q2.xp || q1.yp != q2.yp || q1.zp != q2.zp || q1.wp != q2.wp;
    00237 }
    00238
    00239 inline const QQuaternion operator+(const QQuaternion &q1, const QQuaternion &q2)
    00240 {
    00241     return QQuaternion(q1.wp + q2.wp, q1.xp + q2.xp, q1.yp + q2.yp, q1.zp + q2.zp);
    00242 }
    00243
    00244 inline const QQuaternion operator-(const QQuaternion &q1, const QQuaternion &q2)
    00245 {
    00246     return QQuaternion(q1.wp - q2.wp, q1.xp - q2.xp, q1.yp - q2.yp, q1.zp - q2.zp);
    00247 }
    00248
    00249 inline const QQuaternion operator*(qreal factor, const QQuaternion &quaternion)
    00250 {
    00251     return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor);
    00252 }
    00253
    00254 inline const QQuaternion operator*(const QQuaternion &quaternion, qreal factor)
    00255 {
    00256     return QQuaternion(quaternion.wp * factor, quaternion.xp * factor, quaternion.yp * factor, quaternion.zp * factor);
    00257 }
    00258
    00259 inline const QQuaternion operator-(const QQuaternion &quaternion)
    00260 {
    00261     return QQuaternion(-quaternion.wp, -quaternion.xp, -quaternion.yp, -quaternion.zp);
    00262 }
    00263
    00264 inline const QQuaternion operator/(const QQuaternion &quaternion, qreal divisor)
    00265 {
    00266     return QQuaternion(quaternion.wp / divisor, quaternion.xp / divisor, quaternion.yp / divisor, quaternion.zp / divisor);
    00267 }
    00268
    00269 inline bool qFuzzyCompare(const QQuaternion& q1, const QQuaternion& q2)
    00270 {
    00271     return qFuzzyCompare(q1.xp, q2.xp) &&
    00272            qFuzzyCompare(q1.yp, q2.yp) &&
    00273            qFuzzyCompare(q1.zp, q2.zp) &&
    00274            qFuzzyCompare(q1.wp, q2.wp);
    00275 }
    00276
    00277 #ifndef QT_NO_VECTOR3D
    00278
    00279 inline QQuaternion::QQuaternion(qreal aScalar, const QVector3D& aVector)
    00280     : wp(aScalar), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
    00281
    00282 inline void QQuaternion::setVector(const QVector3D& aVector)
    00283 {
    00284     xp = aVector.x();
    00285     yp = aVector.y();
    00286     zp = aVector.z();
    00287 }
    00288
    00289 inline QVector3D QQuaternion::vector() const
    00290 {
    00291     return QVector3D(xp, yp, zp);
    00292 }
    00293
    00294 #endif
    00295
    00296 inline void QQuaternion::setVector(qreal aX, qreal aY, qreal aZ)
    00297 {
    00298     xp = aX;
    00299     yp = aY;
    00300     zp = aZ;
    00301 }
    00302
    00303 #ifndef QT_NO_VECTOR4D
    00304
    00305 inline QQuaternion::QQuaternion(const QVector4D& aVector)
    00306     : wp(aVector.w()), xp(aVector.x()), yp(aVector.y()), zp(aVector.z()) {}
    00307
    00308 inline QVector4D QQuaternion::toVector4D() const
    00309 {
    00310     return QVector4D(xp, yp, zp, wp);
    00311 }
    00312
    00313 #endif
    00314
    00315 #ifndef QT_NO_DEBUG_STREAM
    00316 Q_GUI_EXPORT QDebug operator<<(QDebug dbg, const QQuaternion &q);
    00317 #endif
    00318
    00319 #ifndef QT_NO_DATASTREAM
    00320 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QQuaternion &);
    00321 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QQuaternion &);
    00322 #endif
    00323
    00324 #endif
    00325
    00326 QT_END_NAMESPACE
    00327
    00328 QT_END_HEADER
    00329
    00330 #endif