[657] | 1 | /*
|
---|
| 2 | -----------------------------------------------------------------------------
|
---|
| 3 | This source file is part of OGRE
|
---|
| 4 | (Object-oriented Graphics Rendering Engine)
|
---|
| 5 | For the latest info, see http://www.ogre3d.org/
|
---|
| 6 |
|
---|
| 7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
| 8 | Also see acknowledgements in Readme.html
|
---|
| 9 |
|
---|
| 10 | This program is free software; you can redistribute it and/or modify it under
|
---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
| 13 | version.
|
---|
| 14 |
|
---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
| 18 |
|
---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
| 22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
| 23 | -----------------------------------------------------------------------------
|
---|
| 24 | */
|
---|
| 25 | // NOTE THAT THIS FILE IS BASED ON MATERIAL FROM:
|
---|
| 26 |
|
---|
| 27 | // Magic Software, Inc.
|
---|
| 28 | // http://www.magic-software.com
|
---|
| 29 | // Copyright (c) 2000, All Rights Reserved
|
---|
| 30 | //
|
---|
| 31 | // Source code from Magic Software is supplied under the terms of a license
|
---|
| 32 | // agreement and may not be copied or disclosed except in accordance with the
|
---|
| 33 | // terms of that agreement. The various license agreements may be found at
|
---|
| 34 | // the Magic Software web site. This file is subject to the license
|
---|
| 35 | //
|
---|
| 36 | // FREE SOURCE CODE
|
---|
| 37 | // http://www.magic-software.com/License/free.pdf
|
---|
| 38 |
|
---|
| 39 | #ifndef __Quaternion_H__
|
---|
| 40 | #define __Quaternion_H__
|
---|
| 41 |
|
---|
| 42 | #include "OgrePrerequisites.h"
|
---|
| 43 | #include "OgreMath.h"
|
---|
| 44 |
|
---|
| 45 | namespace Ogre {
|
---|
| 46 |
|
---|
| 47 | /** Implementation of a Quaternion, i.e. a rotation around an axis.
|
---|
| 48 | */
|
---|
| 49 | class _OgreExport Quaternion
|
---|
| 50 | {
|
---|
| 51 | public:
|
---|
| 52 | inline Quaternion (
|
---|
| 53 | Real fW = 1.0,
|
---|
| 54 | Real fX = 0.0, Real fY = 0.0, Real fZ = 0.0)
|
---|
| 55 | {
|
---|
| 56 | w = fW;
|
---|
| 57 | x = fX;
|
---|
| 58 | y = fY;
|
---|
| 59 | z = fZ;
|
---|
| 60 | }
|
---|
| 61 | inline Quaternion (const Quaternion& rkQ)
|
---|
| 62 | {
|
---|
| 63 | w = rkQ.w;
|
---|
| 64 | x = rkQ.x;
|
---|
| 65 | y = rkQ.y;
|
---|
| 66 | z = rkQ.z;
|
---|
| 67 | }
|
---|
| 68 | /// Construct a quaternion from a rotation matrix
|
---|
| 69 | inline Quaternion(const Matrix3& rot)
|
---|
| 70 | {
|
---|
| 71 | this->FromRotationMatrix(rot);
|
---|
| 72 | }
|
---|
| 73 | /// Construct a quaternion from an angle/axis
|
---|
| 74 | inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis)
|
---|
| 75 | {
|
---|
| 76 | this->FromAngleAxis(rfAngle, rkAxis);
|
---|
| 77 | }
|
---|
| 78 | #ifndef OGRE_FORCE_ANGLE_TYPES
|
---|
| 79 | inline Quaternion(const Real& rfAngle, const Vector3& rkAxis)
|
---|
| 80 | {
|
---|
| 81 | this->FromAngleAxis(rfAngle, rkAxis);
|
---|
| 82 | }
|
---|
| 83 | #endif//OGRE_FORCE_ANGLE_TYPES
|
---|
| 84 | /// Construct a quaternion from 3 orthonormal local axes
|
---|
| 85 | inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
|
---|
| 86 | {
|
---|
| 87 | this->FromAxes(xaxis, yaxis, zaxis);
|
---|
| 88 | }
|
---|
| 89 | /// Construct a quaternion from 3 orthonormal local axes
|
---|
| 90 | inline Quaternion(const Vector3* akAxis)
|
---|
| 91 | {
|
---|
| 92 | this->FromAxes(akAxis);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | void FromRotationMatrix (const Matrix3& kRot);
|
---|
| 96 | void ToRotationMatrix (Matrix3& kRot) const;
|
---|
| 97 | void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis);
|
---|
| 98 | void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const;
|
---|
| 99 | inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
|
---|
| 100 | Radian rAngle;
|
---|
| 101 | ToAngleAxis ( rAngle, rkAxis );
|
---|
| 102 | dAngle = rAngle;
|
---|
| 103 | }
|
---|
| 104 | #ifndef OGRE_FORCE_ANGLE_TYPES
|
---|
| 105 | inline void FromAngleAxis (const Real& rfAngle, const Vector3& rkAxis) {
|
---|
| 106 | FromAngleAxis ( Angle(rfAngle), rkAxis );
|
---|
| 107 | }
|
---|
| 108 | inline void ToAngleAxis (Real& rfAngle, Vector3& rkAxis) const {
|
---|
| 109 | Radian r;
|
---|
| 110 | ToAngleAxis ( r, rkAxis );
|
---|
| 111 | rfAngle = r.valueAngleUnits();
|
---|
| 112 | }
|
---|
| 113 | #endif//OGRE_FORCE_ANGLE_TYPES
|
---|
| 114 | void FromAxes (const Vector3* akAxis);
|
---|
| 115 | void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
|
---|
| 116 | void ToAxes (Vector3* akAxis) const;
|
---|
| 117 | void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
|
---|
| 118 | /// Get the local x-axis
|
---|
| 119 | Vector3 xAxis(void) const;
|
---|
| 120 | /// Get the local y-axis
|
---|
| 121 | Vector3 yAxis(void) const;
|
---|
| 122 | /// Get the local z-axis
|
---|
| 123 | Vector3 zAxis(void) const;
|
---|
| 124 |
|
---|
| 125 | inline Quaternion& operator= (const Quaternion& rkQ)
|
---|
| 126 | {
|
---|
| 127 | w = rkQ.w;
|
---|
| 128 | x = rkQ.x;
|
---|
| 129 | y = rkQ.y;
|
---|
| 130 | z = rkQ.z;
|
---|
| 131 | return *this;
|
---|
| 132 | }
|
---|
| 133 | Quaternion operator+ (const Quaternion& rkQ) const;
|
---|
| 134 | Quaternion operator- (const Quaternion& rkQ) const;
|
---|
| 135 | Quaternion operator* (const Quaternion& rkQ) const;
|
---|
| 136 | Quaternion operator* (Real fScalar) const;
|
---|
| 137 | _OgreExport friend Quaternion operator* (Real fScalar,
|
---|
| 138 | const Quaternion& rkQ);
|
---|
| 139 | Quaternion operator- () const;
|
---|
| 140 | inline bool operator== (const Quaternion& rhs) const
|
---|
| 141 | {
|
---|
| 142 | return (rhs.x == x) && (rhs.y == y) &&
|
---|
| 143 | (rhs.z == z) && (rhs.w == w);
|
---|
| 144 | }
|
---|
| 145 | inline bool operator!= (const Quaternion& rhs) const
|
---|
| 146 | {
|
---|
| 147 | return !operator==(rhs);
|
---|
| 148 | }
|
---|
| 149 | // functions of a quaternion
|
---|
| 150 | Real Dot (const Quaternion& rkQ) const; // dot product
|
---|
| 151 | Real Norm () const; // squared-length
|
---|
| 152 | /// Normalises this quaternion, and returns the previous length
|
---|
| 153 | Real normalise(void);
|
---|
| 154 | Quaternion Inverse () const; // apply to non-zero quaternion
|
---|
| 155 | Quaternion UnitInverse () const; // apply to unit-length quaternion
|
---|
| 156 | Quaternion Exp () const;
|
---|
| 157 | Quaternion Log () const;
|
---|
| 158 |
|
---|
| 159 | // rotation of a vector by a quaternion
|
---|
| 160 | Vector3 operator* (const Vector3& rkVector) const;
|
---|
| 161 |
|
---|
| 162 | /// Calculate the local roll element of this quaternion
|
---|
| 163 | Radian getRoll(void) const;
|
---|
| 164 | /// Calculate the local pitch element of this quaternion
|
---|
| 165 | Radian getPitch(void) const;
|
---|
| 166 | /// Calculate the local yaw element of this quaternion
|
---|
| 167 | Radian getYaw(void) const;
|
---|
| 168 | /// Equality with tolerance (tolerance is max angle difference)
|
---|
| 169 | bool equals(const Quaternion& rhs, const Radian& tolerance) const;
|
---|
| 170 |
|
---|
| 171 | // spherical linear interpolation
|
---|
| 172 | static Quaternion Slerp (Real fT, const Quaternion& rkP,
|
---|
| 173 | const Quaternion& rkQ, bool shortestPath = false);
|
---|
| 174 |
|
---|
| 175 | static Quaternion SlerpExtraSpins (Real fT,
|
---|
| 176 | const Quaternion& rkP, const Quaternion& rkQ,
|
---|
| 177 | int iExtraSpins);
|
---|
| 178 |
|
---|
| 179 | // setup for spherical quadratic interpolation
|
---|
| 180 | static void Intermediate (const Quaternion& rkQ0,
|
---|
| 181 | const Quaternion& rkQ1, const Quaternion& rkQ2,
|
---|
| 182 | Quaternion& rka, Quaternion& rkB);
|
---|
| 183 |
|
---|
| 184 | // spherical quadratic interpolation
|
---|
| 185 | static Quaternion Squad (Real fT, const Quaternion& rkP,
|
---|
| 186 | const Quaternion& rkA, const Quaternion& rkB,
|
---|
| 187 | const Quaternion& rkQ, bool shortestPath = false);
|
---|
| 188 |
|
---|
| 189 | // normalised linear interpolation - faster but less accurate (non-constant rotation velocity)
|
---|
| 190 | static Quaternion nlerp(Real fT, const Quaternion& rkP,
|
---|
| 191 | const Quaternion& rkQ, bool shortestPath = false);
|
---|
| 192 |
|
---|
| 193 | // cutoff for sine near zero
|
---|
| 194 | static const Real ms_fEpsilon;
|
---|
| 195 |
|
---|
| 196 | // special values
|
---|
| 197 | static const Quaternion ZERO;
|
---|
| 198 | static const Quaternion IDENTITY;
|
---|
| 199 |
|
---|
| 200 | Real w, x, y, z;
|
---|
| 201 |
|
---|
| 202 | /** Function for writing to a stream. Outputs "Quaternion(w, x, y, z)" with w,x,y,z
|
---|
| 203 | being the member values of the quaternion.
|
---|
| 204 | */
|
---|
| 205 | inline _OgreExport friend std::ostream& operator <<
|
---|
| 206 | ( std::ostream& o, const Quaternion& q )
|
---|
| 207 | {
|
---|
| 208 | o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
|
---|
| 209 | return o;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | };
|
---|
| 213 |
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 |
|
---|
| 218 |
|
---|
| 219 | #endif
|
---|