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-2012 Torus Knot Software Ltd 00008 00009 Permission is hereby granted, free of charge, to any person obtaining a copy 00010 of this software and associated documentation files (the "Software"), to deal 00011 in the Software without restriction, including without limitation the rights 00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00013 copies of the Software, and to permit persons to whom the Software is 00014 furnished to do so, subject to the following conditions: 00015 00016 The above copyright notice and this permission notice shall be included in 00017 all copies or substantial portions of the Software. 00018 00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00025 THE SOFTWARE. 00026 ----------------------------------------------------------------------------- 00027 */ 00028 // This file is based on material originally from: 00029 // Geometric Tools, LLC 00030 // Copyright (c) 1998-2010 00031 // Distributed under the Boost Software License, Version 1.0. 00032 // http://www.boost.org/LICENSE_1_0.txt 00033 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt 00034 00035 00036 #ifndef __Quaternion_H__ 00037 #define __Quaternion_H__ 00038 00039 #include "OgrePrerequisites.h" 00040 #include "OgreMath.h" 00041 00042 namespace Ogre { 00043 00057 class _OgreExport Quaternion 00058 { 00059 public: 00061 inline Quaternion () 00062 : w(1), x(0), y(0), z(0) 00063 { 00064 } 00066 inline Quaternion ( 00067 Real fW, 00068 Real fX, Real fY, Real fZ) 00069 : w(fW), x(fX), y(fY), z(fZ) 00070 { 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 } 00083 inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis) 00084 { 00085 this->FromAxes(xaxis, yaxis, zaxis); 00086 } 00088 inline Quaternion(const Vector3* akAxis) 00089 { 00090 this->FromAxes(akAxis); 00091 } 00093 inline Quaternion(Real* valptr) 00094 { 00095 memcpy(&w, valptr, sizeof(Real)*4); 00096 } 00097 00100 inline void swap(Quaternion& other) 00101 { 00102 std::swap(w, other.w); 00103 std::swap(x, other.x); 00104 std::swap(y, other.y); 00105 std::swap(z, other.z); 00106 } 00107 00109 inline Real operator [] ( const size_t i ) const 00110 { 00111 assert( i < 4 ); 00112 00113 return *(&w+i); 00114 } 00115 00117 inline Real& operator [] ( const size_t i ) 00118 { 00119 assert( i < 4 ); 00120 00121 return *(&w+i); 00122 } 00123 00125 inline Real* ptr() 00126 { 00127 return &w; 00128 } 00129 00131 inline const Real* ptr() const 00132 { 00133 return &w; 00134 } 00135 00136 void FromRotationMatrix (const Matrix3& kRot); 00137 void ToRotationMatrix (Matrix3& kRot) const; 00141 void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis); 00142 void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const; 00143 inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const { 00144 Radian rAngle; 00145 ToAngleAxis ( rAngle, rkAxis ); 00146 dAngle = rAngle; 00147 } 00151 void FromAxes (const Vector3* akAxis); 00152 void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis); 00154 void ToAxes (Vector3* akAxis) const; 00155 void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const; 00156 00160 Vector3 xAxis(void) const; 00161 00165 Vector3 yAxis(void) const; 00166 00170 Vector3 zAxis(void) const; 00171 00172 inline Quaternion& operator= (const Quaternion& rkQ) 00173 { 00174 w = rkQ.w; 00175 x = rkQ.x; 00176 y = rkQ.y; 00177 z = rkQ.z; 00178 return *this; 00179 } 00180 Quaternion operator+ (const Quaternion& rkQ) const; 00181 Quaternion operator- (const Quaternion& rkQ) const; 00182 Quaternion operator* (const Quaternion& rkQ) const; 00183 Quaternion operator* (Real fScalar) const; 00184 _OgreExport friend Quaternion operator* (Real fScalar, 00185 const Quaternion& rkQ); 00186 Quaternion operator- () const; 00187 inline bool operator== (const Quaternion& rhs) const 00188 { 00189 return (rhs.x == x) && (rhs.y == y) && 00190 (rhs.z == z) && (rhs.w == w); 00191 } 00192 inline bool operator!= (const Quaternion& rhs) const 00193 { 00194 return !operator==(rhs); 00195 } 00196 // functions of a quaternion 00198 Real Dot (const Quaternion& rkQ) const; 00199 /* Returns the normal length of this quaternion. 00200 @note This does <b>not</b> alter any values. 00201 */ 00202 Real Norm () const; 00204 Real normalise(void); 00205 Quaternion Inverse () const; // apply to non-zero quaternion 00206 Quaternion UnitInverse () const; // apply to unit-length quaternion 00207 Quaternion Exp () const; 00208 Quaternion Log () const; 00209 00211 Vector3 operator* (const Vector3& rkVector) const; 00212 00222 Radian getRoll(bool reprojectAxis = true) const; 00232 Radian getPitch(bool reprojectAxis = true) const; 00242 Radian getYaw(bool reprojectAxis = true) const; 00244 bool equals(const Quaternion& rhs, const Radian& tolerance) const; 00245 00258 static Quaternion Slerp (Real fT, const Quaternion& rkP, 00259 const Quaternion& rkQ, bool shortestPath = false); 00260 00265 static Quaternion SlerpExtraSpins (Real fT, 00266 const Quaternion& rkP, const Quaternion& rkQ, 00267 int iExtraSpins); 00268 00269 // setup for spherical quadratic interpolation 00270 static void Intermediate (const Quaternion& rkQ0, 00271 const Quaternion& rkQ1, const Quaternion& rkQ2, 00272 Quaternion& rka, Quaternion& rkB); 00273 00274 // spherical quadratic interpolation 00275 static Quaternion Squad (Real fT, const Quaternion& rkP, 00276 const Quaternion& rkA, const Quaternion& rkB, 00277 const Quaternion& rkQ, bool shortestPath = false); 00278 00293 static Quaternion nlerp(Real fT, const Quaternion& rkP, 00294 const Quaternion& rkQ, bool shortestPath = false); 00295 00297 static const Real msEpsilon; 00298 00299 // special values 00300 static const Quaternion ZERO; 00301 static const Quaternion IDENTITY; 00302 00303 Real w, x, y, z; 00304 00306 inline bool isNaN() const 00307 { 00308 return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w); 00309 } 00310 00314 inline _OgreExport friend std::ostream& operator << 00315 ( std::ostream& o, const Quaternion& q ) 00316 { 00317 o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")"; 00318 return o; 00319 } 00320 00321 }; 00325 } 00326 00327 00328 00329 00330 #endif
Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Fri May 25 23:36:25 2012