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 // NOTE THAT THIS FILE IS BASED ON MATERIAL FROM:
00029 
00030 // Magic Software, Inc.
00031 // http://www.geometrictools.com/
00032 // Copyright (c) 2000, All Rights Reserved
00033 //
00034 // Source code from Magic Software is supplied under the terms of a license
00035 // agreement and may not be copied or disclosed except in accordance with the
00036 // terms of that agreement.  The various license agreements may be found at
00037 // the Magic Software web site.  This file is subject to the license
00038 //
00039 // FREE SOURCE CODE
00040 // http://www.geometrictools.com/License/WildMagic3License.pdf
00041 
00042 #ifndef __Quaternion_H__
00043 #define __Quaternion_H__
00044 
00045 #include "OgrePrerequisites.h"
00046 #include "OgreMath.h"
00047 
00048 namespace Ogre {
00049 
00058     class _OgreExport Quaternion
00059     {
00060     public:
00061         inline Quaternion (
00062             Real fW = 1.0,
00063             Real fX = 0.0, Real fY = 0.0, Real fZ = 0.0)
00064         {
00065             w = fW;
00066             x = fX;
00067             y = fY;
00068             z = fZ;
00069         }
00071         inline Quaternion(const Matrix3& rot)
00072         {
00073             this->FromRotationMatrix(rot);
00074         }
00076         inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis)
00077         {
00078             this->FromAngleAxis(rfAngle, rkAxis);
00079         }
00081         inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
00082         {
00083             this->FromAxes(xaxis, yaxis, zaxis);
00084         }
00086         inline Quaternion(const Vector3* akAxis)
00087         {
00088             this->FromAxes(akAxis);
00089         }
00091         inline Quaternion(Real* valptr)
00092         {
00093             memcpy(&w, valptr, sizeof(Real)*4);
00094         }
00095 
00098         inline void swap(Quaternion& other)
00099         {
00100             std::swap(w, other.w);
00101             std::swap(x, other.x);
00102             std::swap(y, other.y);
00103             std::swap(z, other.z);
00104         }
00105 
00107         inline Real operator [] ( const size_t i ) const
00108         {
00109             assert( i < 4 );
00110 
00111             return *(&w+i);
00112         }
00113 
00115         inline Real& operator [] ( const size_t i )
00116         {
00117             assert( i < 4 );
00118 
00119             return *(&w+i);
00120         }
00121 
00123         inline Real* ptr()
00124         {
00125             return &w;
00126         }
00127 
00129         inline const Real* ptr() const
00130         {
00131             return &w;
00132         }
00133 
00134         void FromRotationMatrix (const Matrix3& kRot);
00135         void ToRotationMatrix (Matrix3& kRot) const;
00136         void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis);
00137         void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const;
00138         inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
00139             Radian rAngle;
00140             ToAngleAxis ( rAngle, rkAxis );
00141             dAngle = rAngle;
00142         }
00143         void FromAxes (const Vector3* akAxis);
00144         void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
00145         void ToAxes (Vector3* akAxis) const;
00146         void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
00148         Vector3 xAxis(void) const;
00150         Vector3 yAxis(void) const;
00152         Vector3 zAxis(void) const;
00153 
00154         inline Quaternion& operator= (const Quaternion& rkQ)
00155         {
00156             w = rkQ.w;
00157             x = rkQ.x;
00158             y = rkQ.y;
00159             z = rkQ.z;
00160             return *this;
00161         }
00162         Quaternion operator+ (const Quaternion& rkQ) const;
00163         Quaternion operator- (const Quaternion& rkQ) const;
00164         Quaternion operator* (const Quaternion& rkQ) const;
00165         Quaternion operator* (Real fScalar) const;
00166         _OgreExport friend Quaternion operator* (Real fScalar,
00167             const Quaternion& rkQ);
00168         Quaternion operator- () const;
00169         inline bool operator== (const Quaternion& rhs) const
00170         {
00171             return (rhs.x == x) && (rhs.y == y) &&
00172                 (rhs.z == z) && (rhs.w == w);
00173         }
00174         inline bool operator!= (const Quaternion& rhs) const
00175         {
00176             return !operator==(rhs);
00177         }
00178         // functions of a quaternion
00179         Real Dot (const Quaternion& rkQ) const;  // dot product
00180         Real Norm () const;  // squared-length
00182         Real normalise(void); 
00183         Quaternion Inverse () const;  // apply to non-zero quaternion
00184         Quaternion UnitInverse () const;  // apply to unit-length quaternion
00185         Quaternion Exp () const;
00186         Quaternion Log () const;
00187 
00188         // rotation of a vector by a quaternion
00189         Vector3 operator* (const Vector3& rkVector) const;
00190 
00199         Radian getRoll(bool reprojectAxis = true) const;
00208         Radian getPitch(bool reprojectAxis = true) const;
00217         Radian getYaw(bool reprojectAxis = true) const;     
00219         bool equals(const Quaternion& rhs, const Radian& tolerance) const;
00220         
00221         // spherical linear interpolation
00222         static Quaternion Slerp (Real fT, const Quaternion& rkP,
00223             const Quaternion& rkQ, bool shortestPath = false);
00224 
00225         static Quaternion SlerpExtraSpins (Real fT,
00226             const Quaternion& rkP, const Quaternion& rkQ,
00227             int iExtraSpins);
00228 
00229         // setup for spherical quadratic interpolation
00230         static void Intermediate (const Quaternion& rkQ0,
00231             const Quaternion& rkQ1, const Quaternion& rkQ2,
00232             Quaternion& rka, Quaternion& rkB);
00233 
00234         // spherical quadratic interpolation
00235         static Quaternion Squad (Real fT, const Quaternion& rkP,
00236             const Quaternion& rkA, const Quaternion& rkB,
00237             const Quaternion& rkQ, bool shortestPath = false);
00238 
00239         // normalised linear interpolation - faster but less accurate (non-constant rotation velocity)
00240         static Quaternion nlerp(Real fT, const Quaternion& rkP, 
00241             const Quaternion& rkQ, bool shortestPath = false);
00242 
00243         // cutoff for sine near zero
00244         static const Real ms_fEpsilon;
00245 
00246         // special values
00247         static const Quaternion ZERO;
00248         static const Quaternion IDENTITY;
00249 
00250         Real w, x, y, z;
00251 
00253         inline bool isNaN() const
00254         {
00255             return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
00256         }
00257 
00261         inline _OgreExport friend std::ostream& operator <<
00262             ( std::ostream& o, const Quaternion& q )
00263         {
00264             o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
00265             return o;
00266         }
00267 
00268     };
00272 }
00273 
00274 
00275 
00276 
00277 #endif 

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Thu Dec 31 16:27:12 2009