[5] | 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-2006 Torus Knot Software Ltd |
---|
| 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 | You may alternatively use this source under the terms of a specific version of |
---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
| 26 | Torus Knot Software Ltd. |
---|
| 27 | ----------------------------------------------------------------------------- |
---|
| 28 | */ |
---|
| 29 | // NOTE THAT THIS FILE IS BASED ON MATERIAL FROM: |
---|
| 30 | |
---|
| 31 | // Magic Software, Inc. |
---|
| 32 | // http://www.geometrictools.com/ |
---|
| 33 | // Copyright (c) 2000, All Rights Reserved |
---|
| 34 | // |
---|
| 35 | // Source code from Magic Software is supplied under the terms of a license |
---|
| 36 | // agreement and may not be copied or disclosed except in accordance with the |
---|
| 37 | // terms of that agreement. The various license agreements may be found at |
---|
| 38 | // the Magic Software web site. This file is subject to the license |
---|
| 39 | // |
---|
| 40 | // FREE SOURCE CODE |
---|
| 41 | // http://www.geometrictools.com/License/WildMagic3License.pdf |
---|
| 42 | |
---|
| 43 | #ifndef __Quaternion_H__ |
---|
| 44 | #define __Quaternion_H__ |
---|
| 45 | |
---|
| 46 | #include "OgrePrerequisites.h" |
---|
| 47 | #include "OgreMath.h" |
---|
| 48 | |
---|
| 49 | namespace Ogre { |
---|
| 50 | |
---|
| 51 | /** Implementation of a Quaternion, i.e. a rotation around an axis. |
---|
| 52 | */ |
---|
| 53 | class _OgreExport Quaternion |
---|
| 54 | { |
---|
| 55 | public: |
---|
| 56 | inline Quaternion ( |
---|
| 57 | Real fW = 1.0, |
---|
| 58 | Real fX = 0.0, Real fY = 0.0, Real fZ = 0.0) |
---|
| 59 | { |
---|
| 60 | w = fW; |
---|
| 61 | x = fX; |
---|
| 62 | y = fY; |
---|
| 63 | z = fZ; |
---|
| 64 | } |
---|
| 65 | inline Quaternion (const Quaternion& rkQ) |
---|
| 66 | { |
---|
| 67 | w = rkQ.w; |
---|
| 68 | x = rkQ.x; |
---|
| 69 | y = rkQ.y; |
---|
| 70 | z = rkQ.z; |
---|
| 71 | } |
---|
| 72 | /// Construct a quaternion from a rotation matrix |
---|
| 73 | inline Quaternion(const Matrix3& rot) |
---|
| 74 | { |
---|
| 75 | this->FromRotationMatrix(rot); |
---|
| 76 | } |
---|
| 77 | /// Construct a quaternion from an angle/axis |
---|
| 78 | inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis) |
---|
| 79 | { |
---|
| 80 | this->FromAngleAxis(rfAngle, rkAxis); |
---|
| 81 | } |
---|
| 82 | #ifndef OGRE_FORCE_ANGLE_TYPES |
---|
| 83 | inline Quaternion(const Real& rfAngle, const Vector3& rkAxis) |
---|
| 84 | { |
---|
| 85 | this->FromAngleAxis(rfAngle, rkAxis); |
---|
| 86 | } |
---|
| 87 | #endif//OGRE_FORCE_ANGLE_TYPES |
---|
| 88 | /// Construct a quaternion from 3 orthonormal local axes |
---|
| 89 | inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis) |
---|
| 90 | { |
---|
| 91 | this->FromAxes(xaxis, yaxis, zaxis); |
---|
| 92 | } |
---|
| 93 | /// Construct a quaternion from 3 orthonormal local axes |
---|
| 94 | inline Quaternion(const Vector3* akAxis) |
---|
| 95 | { |
---|
| 96 | this->FromAxes(akAxis); |
---|
| 97 | } |
---|
| 98 | /// Construct a quaternion from 4 manual w/x/y/z values |
---|
| 99 | inline Quaternion(Real* valptr) |
---|
| 100 | { |
---|
| 101 | memcpy(&w, valptr, sizeof(Real)*4); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /// Array accessor operator |
---|
| 105 | inline Real operator [] ( const size_t i ) const |
---|
| 106 | { |
---|
| 107 | assert( i < 4 ); |
---|
| 108 | |
---|
| 109 | return *(&w+i); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /// Array accessor operator |
---|
| 113 | inline Real& operator [] ( const size_t i ) |
---|
| 114 | { |
---|
| 115 | assert( i < 4 ); |
---|
| 116 | |
---|
| 117 | return *(&w+i); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /// Pointer accessor for direct copying |
---|
| 121 | inline Real* ptr() |
---|
| 122 | { |
---|
| 123 | return &w; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /// Pointer accessor for direct copying |
---|
| 127 | inline const Real* ptr() const |
---|
| 128 | { |
---|
| 129 | return &w; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | void FromRotationMatrix (const Matrix3& kRot); |
---|
| 133 | void ToRotationMatrix (Matrix3& kRot) const; |
---|
| 134 | void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis); |
---|
| 135 | void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const; |
---|
| 136 | inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const { |
---|
| 137 | Radian rAngle; |
---|
| 138 | ToAngleAxis ( rAngle, rkAxis ); |
---|
| 139 | dAngle = rAngle; |
---|
| 140 | } |
---|
| 141 | #ifndef OGRE_FORCE_ANGLE_TYPES |
---|
| 142 | inline void FromAngleAxis (const Real& rfAngle, const Vector3& rkAxis) { |
---|
| 143 | FromAngleAxis ( Angle(rfAngle), rkAxis ); |
---|
| 144 | } |
---|
| 145 | inline void ToAngleAxis (Real& rfAngle, Vector3& rkAxis) const { |
---|
| 146 | Radian r; |
---|
| 147 | ToAngleAxis ( r, rkAxis ); |
---|
| 148 | rfAngle = r.valueAngleUnits(); |
---|
| 149 | } |
---|
| 150 | #endif//OGRE_FORCE_ANGLE_TYPES |
---|
| 151 | void FromAxes (const Vector3* akAxis); |
---|
| 152 | void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis); |
---|
| 153 | void ToAxes (Vector3* akAxis) const; |
---|
| 154 | void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const; |
---|
| 155 | /// Get the local x-axis |
---|
| 156 | Vector3 xAxis(void) const; |
---|
| 157 | /// Get the local y-axis |
---|
| 158 | Vector3 yAxis(void) const; |
---|
| 159 | /// Get the local z-axis |
---|
| 160 | Vector3 zAxis(void) const; |
---|
| 161 | |
---|
| 162 | inline Quaternion& operator= (const Quaternion& rkQ) |
---|
| 163 | { |
---|
| 164 | w = rkQ.w; |
---|
| 165 | x = rkQ.x; |
---|
| 166 | y = rkQ.y; |
---|
| 167 | z = rkQ.z; |
---|
| 168 | return *this; |
---|
| 169 | } |
---|
| 170 | Quaternion operator+ (const Quaternion& rkQ) const; |
---|
| 171 | Quaternion operator- (const Quaternion& rkQ) const; |
---|
| 172 | Quaternion operator* (const Quaternion& rkQ) const; |
---|
| 173 | Quaternion operator* (Real fScalar) const; |
---|
| 174 | _OgreExport friend Quaternion operator* (Real fScalar, |
---|
| 175 | const Quaternion& rkQ); |
---|
| 176 | Quaternion operator- () const; |
---|
| 177 | inline bool operator== (const Quaternion& rhs) const |
---|
| 178 | { |
---|
| 179 | return (rhs.x == x) && (rhs.y == y) && |
---|
| 180 | (rhs.z == z) && (rhs.w == w); |
---|
| 181 | } |
---|
| 182 | inline bool operator!= (const Quaternion& rhs) const |
---|
| 183 | { |
---|
| 184 | return !operator==(rhs); |
---|
| 185 | } |
---|
| 186 | // functions of a quaternion |
---|
| 187 | Real Dot (const Quaternion& rkQ) const; // dot product |
---|
| 188 | Real Norm () const; // squared-length |
---|
| 189 | /// Normalises this quaternion, and returns the previous length |
---|
| 190 | Real normalise(void); |
---|
| 191 | Quaternion Inverse () const; // apply to non-zero quaternion |
---|
| 192 | Quaternion UnitInverse () const; // apply to unit-length quaternion |
---|
| 193 | Quaternion Exp () const; |
---|
| 194 | Quaternion Log () const; |
---|
| 195 | |
---|
| 196 | // rotation of a vector by a quaternion |
---|
| 197 | Vector3 operator* (const Vector3& rkVector) const; |
---|
| 198 | |
---|
| 199 | /** Calculate the local roll element of this quaternion. |
---|
| 200 | @param reprojectAxis By default the method returns the 'intuitive' result |
---|
| 201 | that is, if you projected the local Y of the quaterion onto the X and |
---|
| 202 | Y axes, the angle between them is returned. If set to false though, the |
---|
| 203 | result is the actual yaw that will be used to implement the quaternion, |
---|
| 204 | which is the shortest possible path to get to the same orientation and |
---|
| 205 | may involve less axial rotation. |
---|
| 206 | */ |
---|
| 207 | Radian getRoll(bool reprojectAxis = true) const; |
---|
| 208 | /** Calculate the local pitch element of this quaternion |
---|
| 209 | @param reprojectAxis By default the method returns the 'intuitive' result |
---|
| 210 | that is, if you projected the local Z of the quaterion onto the X and |
---|
| 211 | Y axes, the angle between them is returned. If set to true though, the |
---|
| 212 | result is the actual yaw that will be used to implement the quaternion, |
---|
| 213 | which is the shortest possible path to get to the same orientation and |
---|
| 214 | may involve less axial rotation. |
---|
| 215 | */ |
---|
| 216 | Radian getPitch(bool reprojectAxis = true) const; |
---|
| 217 | /** Calculate the local yaw element of this quaternion |
---|
| 218 | @param reprojectAxis By default the method returns the 'intuitive' result |
---|
| 219 | that is, if you projected the local Z of the quaterion onto the X and |
---|
| 220 | Z axes, the angle between them is returned. If set to true though, the |
---|
| 221 | result is the actual yaw that will be used to implement the quaternion, |
---|
| 222 | which is the shortest possible path to get to the same orientation and |
---|
| 223 | may involve less axial rotation. |
---|
| 224 | */ |
---|
| 225 | Radian getYaw(bool reprojectAxis = true) const; |
---|
| 226 | /// Equality with tolerance (tolerance is max angle difference) |
---|
| 227 | bool equals(const Quaternion& rhs, const Radian& tolerance) const; |
---|
| 228 | |
---|
| 229 | // spherical linear interpolation |
---|
| 230 | static Quaternion Slerp (Real fT, const Quaternion& rkP, |
---|
| 231 | const Quaternion& rkQ, bool shortestPath = false); |
---|
| 232 | |
---|
| 233 | static Quaternion SlerpExtraSpins (Real fT, |
---|
| 234 | const Quaternion& rkP, const Quaternion& rkQ, |
---|
| 235 | int iExtraSpins); |
---|
| 236 | |
---|
| 237 | // setup for spherical quadratic interpolation |
---|
| 238 | static void Intermediate (const Quaternion& rkQ0, |
---|
| 239 | const Quaternion& rkQ1, const Quaternion& rkQ2, |
---|
| 240 | Quaternion& rka, Quaternion& rkB); |
---|
| 241 | |
---|
| 242 | // spherical quadratic interpolation |
---|
| 243 | static Quaternion Squad (Real fT, const Quaternion& rkP, |
---|
| 244 | const Quaternion& rkA, const Quaternion& rkB, |
---|
| 245 | const Quaternion& rkQ, bool shortestPath = false); |
---|
| 246 | |
---|
| 247 | // normalised linear interpolation - faster but less accurate (non-constant rotation velocity) |
---|
| 248 | static Quaternion nlerp(Real fT, const Quaternion& rkP, |
---|
| 249 | const Quaternion& rkQ, bool shortestPath = false); |
---|
| 250 | |
---|
| 251 | // cutoff for sine near zero |
---|
| 252 | static const Real ms_fEpsilon; |
---|
| 253 | |
---|
| 254 | // special values |
---|
| 255 | static const Quaternion ZERO; |
---|
| 256 | static const Quaternion IDENTITY; |
---|
| 257 | |
---|
| 258 | Real w, x, y, z; |
---|
| 259 | |
---|
| 260 | /** Function for writing to a stream. Outputs "Quaternion(w, x, y, z)" with w,x,y,z |
---|
| 261 | being the member values of the quaternion. |
---|
| 262 | */ |
---|
| 263 | inline _OgreExport friend std::ostream& operator << |
---|
| 264 | ( std::ostream& o, const Quaternion& q ) |
---|
| 265 | { |
---|
| 266 | o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")"; |
---|
| 267 | return o; |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | }; |
---|
| 271 | |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | #endif |
---|