Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

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-2005 The OGRE Team
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 */
00025 // NOTE THAT THIS FILE IS BASED ON MATERIAL FROM:
00026 
00027 // Magic Software, Inc.
00028 // http://www.geometrictools.com/
00029 // Copyright (c) 2000, All Rights Reserved
00030 //
00031 // Source code from Magic Software is supplied under the terms of a license
00032 // agreement and may not be copied or disclosed except in accordance with the
00033 // terms of that agreement.  The various license agreements may be found at
00034 // the Magic Software web site.  This file is subject to the license
00035 //
00036 // FREE SOURCE CODE
00037 // http://www.geometrictools.com/License/WildMagic3License.pdf
00038 
00039 #ifndef __Quaternion_H__
00040 #define __Quaternion_H__
00041 
00042 #include "OgrePrerequisites.h"
00043 #include "OgreMath.h"
00044 
00045 namespace Ogre {
00046 
00049     class _OgreExport Quaternion
00050     {
00051     public:
00052         inline Quaternion (
00053             Real fW = 1.0,
00054             Real fX = 0.0, Real fY = 0.0, Real fZ = 0.0)
00055         {
00056             w = fW;
00057             x = fX;
00058             y = fY;
00059             z = fZ;
00060         }
00061         inline Quaternion (const Quaternion& rkQ)
00062         {
00063             w = rkQ.w;
00064             x = rkQ.x;
00065             y = rkQ.y;
00066             z = rkQ.z;
00067         }
00069         inline Quaternion(const Matrix3& rot)
00070         {
00071             this->FromRotationMatrix(rot);
00072         }
00074         inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis)
00075         {
00076             this->FromAngleAxis(rfAngle, rkAxis);
00077         }
00078 #ifndef OGRE_FORCE_ANGLE_TYPES
00079         inline Quaternion(const Real& rfAngle, const Vector3& rkAxis)
00080         {
00081             this->FromAngleAxis(rfAngle, rkAxis);
00082         }
00083 #endif//OGRE_FORCE_ANGLE_TYPES
00084 
00085         inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
00086         {
00087             this->FromAxes(xaxis, yaxis, zaxis);
00088         }
00090         inline Quaternion(const Vector3* akAxis)
00091         {
00092             this->FromAxes(akAxis);
00093         }
00095         inline Quaternion(Real* valptr)
00096         {
00097             memcpy(val, valptr, sizeof(Real)*4);
00098         }
00099 
00100         void FromRotationMatrix (const Matrix3& kRot);
00101         void ToRotationMatrix (Matrix3& kRot) const;
00102         void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis);
00103         void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const;
00104         inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
00105             Radian rAngle;
00106             ToAngleAxis ( rAngle, rkAxis );
00107             dAngle = rAngle;
00108         }
00109 #ifndef OGRE_FORCE_ANGLE_TYPES
00110         inline void FromAngleAxis (const Real& rfAngle, const Vector3& rkAxis) {
00111             FromAngleAxis ( Angle(rfAngle), rkAxis );
00112         }
00113         inline void ToAngleAxis (Real& rfAngle, Vector3& rkAxis) const {
00114             Radian r;
00115             ToAngleAxis ( r, rkAxis );
00116             rfAngle = r.valueAngleUnits();
00117         }
00118 #endif//OGRE_FORCE_ANGLE_TYPES
00119         void FromAxes (const Vector3* akAxis);
00120         void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
00121         void ToAxes (Vector3* akAxis) const;
00122         void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
00124         Vector3 xAxis(void) const;
00126         Vector3 yAxis(void) const;
00128         Vector3 zAxis(void) const;
00129 
00130         inline Quaternion& operator= (const Quaternion& rkQ)
00131         {
00132             w = rkQ.w;
00133             x = rkQ.x;
00134             y = rkQ.y;
00135             z = rkQ.z;
00136             return *this;
00137         }
00138         Quaternion operator+ (const Quaternion& rkQ) const;
00139         Quaternion operator- (const Quaternion& rkQ) const;
00140         Quaternion operator* (const Quaternion& rkQ) const;
00141         Quaternion operator* (Real fScalar) const;
00142         _OgreExport friend Quaternion operator* (Real fScalar,
00143             const Quaternion& rkQ);
00144         Quaternion operator- () const;
00145         inline bool operator== (const Quaternion& rhs) const
00146         {
00147             return (rhs.x == x) && (rhs.y == y) &&
00148                 (rhs.z == z) && (rhs.w == w);
00149         }
00150         inline bool operator!= (const Quaternion& rhs) const
00151         {
00152             return !operator==(rhs);
00153         }
00154         // functions of a quaternion
00155         Real Dot (const Quaternion& rkQ) const;  // dot product
00156         Real Norm () const;  // squared-length
00158         Real normalise(void); 
00159         Quaternion Inverse () const;  // apply to non-zero quaternion
00160         Quaternion UnitInverse () const;  // apply to unit-length quaternion
00161         Quaternion Exp () const;
00162         Quaternion Log () const;
00163 
00164         // rotation of a vector by a quaternion
00165         Vector3 operator* (const Vector3& rkVector) const;
00166 
00168         Radian getRoll(void) const;
00170         Radian getPitch(void) const;
00172         Radian getYaw(void) const;      
00174         bool equals(const Quaternion& rhs, const Radian& tolerance) const;
00175         
00176         // spherical linear interpolation
00177         static Quaternion Slerp (Real fT, const Quaternion& rkP,
00178             const Quaternion& rkQ, bool shortestPath = false);
00179 
00180         static Quaternion SlerpExtraSpins (Real fT,
00181             const Quaternion& rkP, const Quaternion& rkQ,
00182             int iExtraSpins);
00183 
00184         // setup for spherical quadratic interpolation
00185         static void Intermediate (const Quaternion& rkQ0,
00186             const Quaternion& rkQ1, const Quaternion& rkQ2,
00187             Quaternion& rka, Quaternion& rkB);
00188 
00189         // spherical quadratic interpolation
00190         static Quaternion Squad (Real fT, const Quaternion& rkP,
00191             const Quaternion& rkA, const Quaternion& rkB,
00192             const Quaternion& rkQ, bool shortestPath = false);
00193 
00194         // normalised linear interpolation - faster but less accurate (non-constant rotation velocity)
00195         static Quaternion nlerp(Real fT, const Quaternion& rkP, 
00196             const Quaternion& rkQ, bool shortestPath = false);
00197 
00198         // cutoff for sine near zero
00199         static const Real ms_fEpsilon;
00200 
00201         // special values
00202         static const Quaternion ZERO;
00203         static const Quaternion IDENTITY;
00204 
00205         union
00206         {
00207             struct {
00208                 Real w, x, y, z;
00209             };
00210             Real val[4];
00211         };
00212 
00216         inline _OgreExport friend std::ostream& operator <<
00217             ( std::ostream& o, const Quaternion& q )
00218         {
00219             o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
00220             return o;
00221         }
00222 
00223     };
00224 
00225 }
00226 
00227 
00228 
00229 
00230 #endif 

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Mar 12 14:37:47 2006