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-2006 Torus Knot Software Ltd
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 
00024 You may alternatively use this source under the terms of a specific version of
00025 the OGRE Unrestricted License provided you have obtained such a license from
00026 Torus Knot Software Ltd.
00027 -----------------------------------------------------------------------------
00028 */
00029 // NOTE THAT THIS FILE IS BASED ON MATERIAL FROM:
00030 
00031 // Magic Software, Inc.
00032 // http://www.geometrictools.com/
00033 // Copyright (c) 2000, All Rights Reserved
00034 //
00035 // Source code from Magic Software is supplied under the terms of a license
00036 // agreement and may not be copied or disclosed except in accordance with the
00037 // terms of that agreement.  The various license agreements may be found at
00038 // the Magic Software web site.  This file is subject to the license
00039 //
00040 // FREE SOURCE CODE
00041 // http://www.geometrictools.com/License/WildMagic3License.pdf
00042 
00043 #ifndef __Quaternion_H__
00044 #define __Quaternion_H__
00045 
00046 #include "OgrePrerequisites.h"
00047 #include "OgreMath.h"
00048 
00049 namespace Ogre {
00050 
00053     class _OgreExport Quaternion
00054     {
00055     public:
00056         inline Quaternion (
00057             Real fW = 1.0,
00058             Real fX = 0.0, Real fY = 0.0, Real fZ = 0.0)
00059         {
00060             w = fW;
00061             x = fX;
00062             y = fY;
00063             z = fZ;
00064         }
00066         inline Quaternion(const Matrix3& rot)
00067         {
00068             this->FromRotationMatrix(rot);
00069         }
00071         inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis)
00072         {
00073             this->FromAngleAxis(rfAngle, rkAxis);
00074         }
00076         inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
00077         {
00078             this->FromAxes(xaxis, yaxis, zaxis);
00079         }
00081         inline Quaternion(const Vector3* akAxis)
00082         {
00083             this->FromAxes(akAxis);
00084         }
00086         inline Quaternion(Real* valptr)
00087         {
00088             memcpy(&w, valptr, sizeof(Real)*4);
00089         }
00090 
00092         inline Real operator [] ( const size_t i ) const
00093         {
00094             assert( i < 4 );
00095 
00096             return *(&w+i);
00097         }
00098 
00100         inline Real& operator [] ( const size_t i )
00101         {
00102             assert( i < 4 );
00103 
00104             return *(&w+i);
00105         }
00106 
00108         inline Real* ptr()
00109         {
00110             return &w;
00111         }
00112 
00114         inline const Real* ptr() const
00115         {
00116             return &w;
00117         }
00118 
00119         void FromRotationMatrix (const Matrix3& kRot);
00120         void ToRotationMatrix (Matrix3& kRot) const;
00121         void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis);
00122         void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const;
00123         inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
00124             Radian rAngle;
00125             ToAngleAxis ( rAngle, rkAxis );
00126             dAngle = rAngle;
00127         }
00128         void FromAxes (const Vector3* akAxis);
00129         void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
00130         void ToAxes (Vector3* akAxis) const;
00131         void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
00133         Vector3 xAxis(void) const;
00135         Vector3 yAxis(void) const;
00137         Vector3 zAxis(void) const;
00138 
00139         inline Quaternion& operator= (const Quaternion& rkQ)
00140         {
00141             w = rkQ.w;
00142             x = rkQ.x;
00143             y = rkQ.y;
00144             z = rkQ.z;
00145             return *this;
00146         }
00147         Quaternion operator+ (const Quaternion& rkQ) const;
00148         Quaternion operator- (const Quaternion& rkQ) const;
00149         Quaternion operator* (const Quaternion& rkQ) const;
00150         Quaternion operator* (Real fScalar) const;
00151         _OgreExport friend Quaternion operator* (Real fScalar,
00152             const Quaternion& rkQ);
00153         Quaternion operator- () const;
00154         inline bool operator== (const Quaternion& rhs) const
00155         {
00156             return (rhs.x == x) && (rhs.y == y) &&
00157                 (rhs.z == z) && (rhs.w == w);
00158         }
00159         inline bool operator!= (const Quaternion& rhs) const
00160         {
00161             return !operator==(rhs);
00162         }
00163         // functions of a quaternion
00164         Real Dot (const Quaternion& rkQ) const;  // dot product
00165         Real Norm () const;  // squared-length
00167         Real normalise(void); 
00168         Quaternion Inverse () const;  // apply to non-zero quaternion
00169         Quaternion UnitInverse () const;  // apply to unit-length quaternion
00170         Quaternion Exp () const;
00171         Quaternion Log () const;
00172 
00173         // rotation of a vector by a quaternion
00174         Vector3 operator* (const Vector3& rkVector) const;
00175 
00184         Radian getRoll(bool reprojectAxis = true) const;
00193         Radian getPitch(bool reprojectAxis = true) const;
00202         Radian getYaw(bool reprojectAxis = true) const;     
00204         bool equals(const Quaternion& rhs, const Radian& tolerance) const;
00205         
00206         // spherical linear interpolation
00207         static Quaternion Slerp (Real fT, const Quaternion& rkP,
00208             const Quaternion& rkQ, bool shortestPath = false);
00209 
00210         static Quaternion SlerpExtraSpins (Real fT,
00211             const Quaternion& rkP, const Quaternion& rkQ,
00212             int iExtraSpins);
00213 
00214         // setup for spherical quadratic interpolation
00215         static void Intermediate (const Quaternion& rkQ0,
00216             const Quaternion& rkQ1, const Quaternion& rkQ2,
00217             Quaternion& rka, Quaternion& rkB);
00218 
00219         // spherical quadratic interpolation
00220         static Quaternion Squad (Real fT, const Quaternion& rkP,
00221             const Quaternion& rkA, const Quaternion& rkB,
00222             const Quaternion& rkQ, bool shortestPath = false);
00223 
00224         // normalised linear interpolation - faster but less accurate (non-constant rotation velocity)
00225         static Quaternion nlerp(Real fT, const Quaternion& rkP, 
00226             const Quaternion& rkQ, bool shortestPath = false);
00227 
00228         // cutoff for sine near zero
00229         static const Real ms_fEpsilon;
00230 
00231         // special values
00232         static const Quaternion ZERO;
00233         static const Quaternion IDENTITY;
00234 
00235         Real w, x, y, z;
00236 
00240         inline _OgreExport friend std::ostream& operator <<
00241             ( std::ostream& o, const Quaternion& q )
00242         {
00243             o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
00244             return o;
00245         }
00246 
00247     };
00248 
00249 }
00250 
00251 
00252 
00253 
00254 #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 Sun Sep 27 22:02:25 2009