# OgreQuaternion.h

Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004     (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org/
    00006
    00007 Copyright (c) 2000-2009 Torus Knot Software Ltd
    00008
    00009 Permission is hereby granted, free of charge, to any person obtaining a copy
    00010 of this software and associated documentation files (the "Software"), to deal
    00011 in the Software without restriction, including without limitation the rights
    00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00013 copies of the Software, and to permit persons to whom the Software is
    00014 furnished to do so, subject to the following conditions:
    00015
    00016 The above copyright notice and this permission notice shall be included in
    00017 all copies or substantial portions of the Software.
    00018
    00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00025 THE SOFTWARE.
    00026 -----------------------------------------------------------------------------
    00027 */
    00028 // This file is based on material originally from:
    00029 // Geometric Tools, LLC
    00030 // Copyright (c) 1998-2010
    00031 // Distributed under the Boost Software License, Version 1.0.
    00032 // http://www.boost.org/LICENSE_1_0.txt
    00033 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
    00034
    00035
    00036 #ifndef __Quaternion_H__
    00037 #define __Quaternion_H__
    00038
    00039 #include "OgrePrerequisites.h"
    00040 #include "OgreMath.h"
    00041
    00042 namespace Ogre {
    00043
    00052     class _OgreExport Quaternion
    00053     {
    00054     public:
    00055         inline Quaternion (
    00056             Real fW = 1.0,
    00057             Real fX = 0.0, Real fY = 0.0, Real fZ = 0.0)
    00058         {
    00059             w = fW;
    00060             x = fX;
    00061             y = fY;
    00062             z = fZ;
    00063         }
    00065         inline Quaternion(const Matrix3& rot)
    00066         {
    00067             this->FromRotationMatrix(rot);
    00068         }
    00070         inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis)
    00071         {
    00072             this->FromAngleAxis(rfAngle, rkAxis);
    00073         }
    00075         inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
    00076         {
    00077             this->FromAxes(xaxis, yaxis, zaxis);
    00078         }
    00080         inline Quaternion(const Vector3* akAxis)
    00081         {
    00082             this->FromAxes(akAxis);
    00083         }
    00085         inline Quaternion(Real* valptr)
    00086         {
    00087             memcpy(&w, valptr, sizeof(Real)*4);
    00088         }
    00089
    00092         inline void swap(Quaternion& other)
    00093         {
    00094             std::swap(w, other.w);
    00095             std::swap(x, other.x);
    00096             std::swap(y, other.y);
    00097             std::swap(z, other.z);
    00098         }
    00099
    00101         inline Real operator [] ( const size_t i ) const
    00102         {
    00103             assert( i < 4 );
    00104
    00105             return *(&w+i);
    00106         }
    00107
    00109         inline Real& operator [] ( const size_t i )
    00110         {
    00111             assert( i < 4 );
    00112
    00113             return *(&w+i);
    00114         }
    00115
    00117         inline Real* ptr()
    00118         {
    00119             return &w;
    00120         }
    00121
    00123         inline const Real* ptr() const
    00124         {
    00125             return &w;
    00126         }
    00127
    00128         void FromRotationMatrix (const Matrix3& kRot);
    00129         void ToRotationMatrix (Matrix3& kRot) const;
    00130         void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis);
    00131         void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const;
    00132         inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
    00133             Radian rAngle;
    00134             ToAngleAxis ( rAngle, rkAxis );
    00135             dAngle = rAngle;
    00136         }
    00137         void FromAxes (const Vector3* akAxis);
    00138         void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
    00139         void ToAxes (Vector3* akAxis) const;
    00140         void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
    00142         Vector3 xAxis(void) const;
    00144         Vector3 yAxis(void) const;
    00146         Vector3 zAxis(void) const;
    00147
    00148         inline Quaternion& operator= (const Quaternion& rkQ)
    00149         {
    00150             w = rkQ.w;
    00151             x = rkQ.x;
    00152             y = rkQ.y;
    00153             z = rkQ.z;
    00154             return *this;
    00155         }
    00156         Quaternion operator+ (const Quaternion& rkQ) const;
    00157         Quaternion operator- (const Quaternion& rkQ) const;
    00158         Quaternion operator* (const Quaternion& rkQ) const;
    00159         Quaternion operator* (Real fScalar) const;
    00160         _OgreExport friend Quaternion operator* (Real fScalar,
    00161             const Quaternion& rkQ);
    00162         Quaternion operator- () const;
    00163         inline bool operator== (const Quaternion& rhs) const
    00164         {
    00165             return (rhs.x == x) && (rhs.y == y) &&
    00166                 (rhs.z == z) && (rhs.w == w);
    00167         }
    00168         inline bool operator!= (const Quaternion& rhs) const
    00169         {
    00170             return !operator==(rhs);
    00171         }
    00172         // functions of a quaternion
    00173         Real Dot (const Quaternion& rkQ) const;  // dot product
    00174         Real Norm () const;  // squared-length
    00176         Real normalise(void);
    00177         Quaternion Inverse () const;  // apply to non-zero quaternion
    00178         Quaternion UnitInverse () const;  // apply to unit-length quaternion
    00179         Quaternion Exp () const;
    00180         Quaternion Log () const;
    00181
    00182         // rotation of a vector by a quaternion
    00183         Vector3 operator* (const Vector3& rkVector) const;
    00184
    00193         Radian getRoll(bool reprojectAxis = true) const;
    00202         Radian getPitch(bool reprojectAxis = true) const;
    00211         Radian getYaw(bool reprojectAxis = true) const;
    00213         bool equals(const Quaternion& rhs, const Radian& tolerance) const;
    00214
    00215         // spherical linear interpolation
    00216         static Quaternion Slerp (Real fT, const Quaternion& rkP,
    00217             const Quaternion& rkQ, bool shortestPath = false);
    00218
    00219         static Quaternion SlerpExtraSpins (Real fT,
    00220             const Quaternion& rkP, const Quaternion& rkQ,
    00221             int iExtraSpins);
    00222
    00223         // setup for spherical quadratic interpolation
    00224         static void Intermediate (const Quaternion& rkQ0,
    00225             const Quaternion& rkQ1, const Quaternion& rkQ2,
    00226             Quaternion& rka, Quaternion& rkB);
    00227
    00228         // spherical quadratic interpolation
    00229         static Quaternion Squad (Real fT, const Quaternion& rkP,
    00230             const Quaternion& rkA, const Quaternion& rkB,
    00231             const Quaternion& rkQ, bool shortestPath = false);
    00232
    00233         // normalised linear interpolation - faster but less accurate (non-constant rotation velocity)
    00234         static Quaternion nlerp(Real fT, const Quaternion& rkP,
    00235             const Quaternion& rkQ, bool shortestPath = false);
    00236
    00237         // cutoff for sine near zero
    00238         static const Real ms_fEpsilon;
    00239
    00240         // special values
    00241         static const Quaternion ZERO;
    00242         static const Quaternion IDENTITY;
    00243
    00244         Real w, x, y, z;
    00245
    00247         inline bool isNaN() const
    00248         {
    00249             return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
    00250         }
    00251
    00255         inline _OgreExport friend std::ostream& operator <<
    00256             ( std::ostream& o, const Quaternion& q )
    00257         {
    00258             o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
    00259             return o;
    00260         }
    00261
    00262     };
    00266 }
    00267
    00268
    00269
    00270
    00271 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun May 8 2011 20:28:08