# 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         }
    00065         inline Quaternion (const Quaternion& rkQ)
    00066         {
    00067             w = rkQ.w;
    00068             x = rkQ.x;
    00069             y = rkQ.y;
    00070             z = rkQ.z;
    00071         }
    00073         inline Quaternion(const Matrix3& rot)
    00074         {
    00075             this->FromRotationMatrix(rot);
    00076         }
    00078         inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis)
    00079         {
    00080             this->FromAngleAxis(rfAngle, rkAxis);
    00081         }
    00082 #ifndef OGRE_FORCE_ANGLE_TYPES
    00083         inline Quaternion(const Real& rfAngle, const Vector3& rkAxis)
    00084         {
    00085             this->FromAngleAxis(rfAngle, rkAxis);
    00086         }
    00087 #endif//OGRE_FORCE_ANGLE_TYPES
    00089         inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
    00090         {
    00091             this->FromAxes(xaxis, yaxis, zaxis);
    00092         }
    00094         inline Quaternion(const Vector3* akAxis)
    00095         {
    00096             this->FromAxes(akAxis);
    00097         }
    00099         inline Quaternion(Real* valptr)
    00100         {
    00101             memcpy(&w, valptr, sizeof(Real)*4);
    00102         }
    00103
    00105         inline Real operator [] ( const size_t i ) const
    00106         {
    00107             assert( i < 4 );
    00108
    00109             return *(&w+i);
    00110         }
    00111
    00113         inline Real& operator [] ( const size_t i )
    00114         {
    00115             assert( i < 4 );
    00116
    00117             return *(&w+i);
    00118         }
    00119
    00121         inline Real* ptr()
    00122         {
    00123             return &w;
    00124         }
    00125
    00127         inline const Real* ptr() const
    00128         {
    00129             return &w;
    00130         }
    00131
    00132         void FromRotationMatrix (const Matrix3& kRot);
    00133         void ToRotationMatrix (Matrix3& kRot) const;
    00134         void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis);
    00135         void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const;
    00136         inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
    00137             Radian rAngle;
    00138             ToAngleAxis ( rAngle, rkAxis );
    00139             dAngle = rAngle;
    00140         }
    00141 #ifndef OGRE_FORCE_ANGLE_TYPES
    00142         inline void FromAngleAxis (const Real& rfAngle, const Vector3& rkAxis) {
    00143             FromAngleAxis ( Angle(rfAngle), rkAxis );
    00144         }
    00145         inline void ToAngleAxis (Real& rfAngle, Vector3& rkAxis) const {
    00146             Radian r;
    00147             ToAngleAxis ( r, rkAxis );
    00148             rfAngle = r.valueAngleUnits();
    00149         }
    00150 #endif//OGRE_FORCE_ANGLE_TYPES
    00151         void FromAxes (const Vector3* akAxis);
    00152         void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
    00153         void ToAxes (Vector3* akAxis) const;
    00154         void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
    00156         Vector3 xAxis(void) const;
    00158         Vector3 yAxis(void) const;
    00160         Vector3 zAxis(void) const;
    00161
    00162         inline Quaternion& operator= (const Quaternion& rkQ)
    00163         {
    00164             w = rkQ.w;
    00165             x = rkQ.x;
    00166             y = rkQ.y;
    00167             z = rkQ.z;
    00168             return *this;
    00169         }
    00170         Quaternion operator+ (const Quaternion& rkQ) const;
    00171         Quaternion operator- (const Quaternion& rkQ) const;
    00172         Quaternion operator* (const Quaternion& rkQ) const;
    00173         Quaternion operator* (Real fScalar) const;
    00174         _OgreExport friend Quaternion operator* (Real fScalar,
    00175             const Quaternion& rkQ);
    00176         Quaternion operator- () const;
    00177         inline bool operator== (const Quaternion& rhs) const
    00178         {
    00179             return (rhs.x == x) && (rhs.y == y) &&
    00180                 (rhs.z == z) && (rhs.w == w);
    00181         }
    00182         inline bool operator!= (const Quaternion& rhs) const
    00183         {
    00184             return !operator==(rhs);
    00185         }
    00186         // functions of a quaternion
    00187         Real Dot (const Quaternion& rkQ) const;  // dot product
    00188         Real Norm () const;  // squared-length
    00190         Real normalise(void);
    00191         Quaternion Inverse () const;  // apply to non-zero quaternion
    00192         Quaternion UnitInverse () const;  // apply to unit-length quaternion
    00193         Quaternion Exp () const;
    00194         Quaternion Log () const;
    00195
    00196         // rotation of a vector by a quaternion
    00197         Vector3 operator* (const Vector3& rkVector) const;
    00198
    00207         Radian getRoll(bool reprojectAxis = true) const;
    00216         Radian getPitch(bool reprojectAxis = true) const;
    00225         Radian getYaw(bool reprojectAxis = true) const;
    00227         bool equals(const Quaternion& rhs, const Radian& tolerance) const;
    00228
    00229         // spherical linear interpolation
    00230         static Quaternion Slerp (Real fT, const Quaternion& rkP,
    00231             const Quaternion& rkQ, bool shortestPath = false);
    00232
    00233         static Quaternion SlerpExtraSpins (Real fT,
    00234             const Quaternion& rkP, const Quaternion& rkQ,
    00235             int iExtraSpins);
    00236
    00237         // setup for spherical quadratic interpolation
    00238         static void Intermediate (const Quaternion& rkQ0,
    00239             const Quaternion& rkQ1, const Quaternion& rkQ2,
    00240             Quaternion& rka, Quaternion& rkB);
    00241
    00242         // spherical quadratic interpolation
    00243         static Quaternion Squad (Real fT, const Quaternion& rkP,
    00244             const Quaternion& rkA, const Quaternion& rkB,
    00245             const Quaternion& rkQ, bool shortestPath = false);
    00246
    00247         // normalised linear interpolation - faster but less accurate (non-constant rotation velocity)
    00248         static Quaternion nlerp(Real fT, const Quaternion& rkP,
    00249             const Quaternion& rkQ, bool shortestPath = false);
    00250
    00251         // cutoff for sine near zero
    00252         static const Real ms_fEpsilon;
    00253
    00254         // special values
    00255         static const Quaternion ZERO;
    00256         static const Quaternion IDENTITY;
    00257
    00258         Real w, x, y, z;
    00259
    00263         inline _OgreExport friend std::ostream& operator <<
    00264             ( std::ostream& o, const Quaternion& q )
    00265         {
    00266             o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
    00267             return o;
    00268         }
    00269
    00270     };
    00271
    00272 }
    00273
    00274
    00275
    00276
    00277 #endif

Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sat May 10 16:25:02 2008