OGRE  1.7
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OgreQuaternion.h
Go to the documentation of this file.
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-2011 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 // This file is based on material originally from:
29 // Geometric Tools, LLC
30 // Copyright (c) 1998-2010
31 // Distributed under the Boost Software License, Version 1.0.
32 // http://www.boost.org/LICENSE_1_0.txt
33 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
34 
35 
36 #ifndef __Quaternion_H__
37 #define __Quaternion_H__
38 
39 #include "OgrePrerequisites.h"
40 #include "OgreMath.h"
41 
42 namespace Ogre {
43 
53  {
54  public:
55  inline Quaternion (
56  Real fW = 1.0,
57  Real fX = 0.0, Real fY = 0.0, Real fZ = 0.0)
58  {
59  w = fW;
60  x = fX;
61  y = fY;
62  z = fZ;
63  }
65  inline Quaternion(const Matrix3& rot)
66  {
67  this->FromRotationMatrix(rot);
68  }
70  inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis)
71  {
72  this->FromAngleAxis(rfAngle, rkAxis);
73  }
75  inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
76  {
77  this->FromAxes(xaxis, yaxis, zaxis);
78  }
80  inline Quaternion(const Vector3* akAxis)
81  {
82  this->FromAxes(akAxis);
83  }
85  inline Quaternion(Real* valptr)
86  {
87  memcpy(&w, valptr, sizeof(Real)*4);
88  }
89 
92  inline void swap(Quaternion& other)
93  {
94  std::swap(w, other.w);
95  std::swap(x, other.x);
96  std::swap(y, other.y);
97  std::swap(z, other.z);
98  }
99 
101  inline Real operator [] ( const size_t i ) const
102  {
103  assert( i < 4 );
104 
105  return *(&w+i);
106  }
107 
109  inline Real& operator [] ( const size_t i )
110  {
111  assert( i < 4 );
112 
113  return *(&w+i);
114  }
115 
117  inline Real* ptr()
118  {
119  return &w;
120  }
121 
123  inline const Real* ptr() const
124  {
125  return &w;
126  }
127 
128  void FromRotationMatrix (const Matrix3& kRot);
129  void ToRotationMatrix (Matrix3& kRot) const;
130  void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis);
131  void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const;
132  inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
133  Radian rAngle;
134  ToAngleAxis ( rAngle, rkAxis );
135  dAngle = rAngle;
136  }
137  void FromAxes (const Vector3* akAxis);
138  void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
139  void ToAxes (Vector3* akAxis) const;
140  void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
142  Vector3 xAxis(void) const;
144  Vector3 yAxis(void) const;
146  Vector3 zAxis(void) const;
147 
148  inline Quaternion& operator= (const Quaternion& rkQ)
149  {
150  w = rkQ.w;
151  x = rkQ.x;
152  y = rkQ.y;
153  z = rkQ.z;
154  return *this;
155  }
156  Quaternion operator+ (const Quaternion& rkQ) const;
157  Quaternion operator- (const Quaternion& rkQ) const;
158  Quaternion operator* (const Quaternion& rkQ) const;
159  Quaternion operator* (Real fScalar) const;
160  _OgreExport friend Quaternion operator* (Real fScalar,
161  const Quaternion& rkQ);
162  Quaternion operator- () const;
163  inline bool operator== (const Quaternion& rhs) const
164  {
165  return (rhs.x == x) && (rhs.y == y) &&
166  (rhs.z == z) && (rhs.w == w);
167  }
168  inline bool operator!= (const Quaternion& rhs) const
169  {
170  return !operator==(rhs);
171  }
172  // functions of a quaternion
173  Real Dot (const Quaternion& rkQ) const; // dot product
174  Real Norm () const; // squared-length
176  Real normalise(void);
177  Quaternion Inverse () const; // apply to non-zero quaternion
178  Quaternion UnitInverse () const; // apply to unit-length quaternion
179  Quaternion Exp () const;
180  Quaternion Log () const;
181 
182  // rotation of a vector by a quaternion
183  Vector3 operator* (const Vector3& rkVector) const;
184 
193  Radian getRoll(bool reprojectAxis = true) const;
202  Radian getPitch(bool reprojectAxis = true) const;
211  Radian getYaw(bool reprojectAxis = true) const;
213  bool equals(const Quaternion& rhs, const Radian& tolerance) const;
214 
215  // spherical linear interpolation
216  static Quaternion Slerp (Real fT, const Quaternion& rkP,
217  const Quaternion& rkQ, bool shortestPath = false);
218 
219  static Quaternion SlerpExtraSpins (Real fT,
220  const Quaternion& rkP, const Quaternion& rkQ,
221  int iExtraSpins);
222 
223  // setup for spherical quadratic interpolation
224  static void Intermediate (const Quaternion& rkQ0,
225  const Quaternion& rkQ1, const Quaternion& rkQ2,
226  Quaternion& rka, Quaternion& rkB);
227 
228  // spherical quadratic interpolation
229  static Quaternion Squad (Real fT, const Quaternion& rkP,
230  const Quaternion& rkA, const Quaternion& rkB,
231  const Quaternion& rkQ, bool shortestPath = false);
232 
233  // normalised linear interpolation - faster but less accurate (non-constant rotation velocity)
234  static Quaternion nlerp(Real fT, const Quaternion& rkP,
235  const Quaternion& rkQ, bool shortestPath = false);
236 
237  // cutoff for sine near zero
238  static const Real ms_fEpsilon;
239 
240  // special values
241  static const Quaternion ZERO;
242  static const Quaternion IDENTITY;
243 
244  Real w, x, y, z;
245 
247  inline bool isNaN() const
248  {
249  return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
250  }
251 
255  inline _OgreExport friend std::ostream& operator <<
256  ( std::ostream& o, const Quaternion& q )
257  {
258  o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
259  return o;
260  }
261 
262  };
266 }
267 
268 
269 
270 
271 #endif
Quaternion(const Radian &rfAngle, const Vector3 &rkAxis)
Construct a quaternion from an angle/axis.
Real * ptr()
Pointer accessor for direct copying.
void swap(Quaternion &other)
Exchange the contents of this quaternion with another.
float Real
Software floating point type.
#define _OgreExport
Definition: OgrePlatform.h:203
static const Real ms_fEpsilon
void ToAngleAxis(Degree &dAngle, Vector3 &rkAxis) const
A 3x3 matrix which can represent rotations around axes.
Definition: OgreMatrix3.h:68
Quaternion(const Matrix3 &rot)
Construct a quaternion from a rotation matrix.
Quaternion(Real fW=1.0, Real fX=0.0, Real fY=0.0, Real fZ=0.0)
bool isNaN() const
Check whether this quaternion contains valid values.
Quaternion(const Vector3 &xaxis, const Vector3 &yaxis, const Vector3 &zaxis)
Construct a quaternion from 3 orthonormal local axes.
Radian operator*(Real a, const Radian &b)
Definition: OgreMath.h:633
const Real * ptr() const
Pointer accessor for direct copying.
Implementation of a Quaternion, i.e.
Quaternion(Real *valptr)
Construct a quaternion from 4 manual w/x/y/z values.
Quaternion(const Vector3 *akAxis)
Construct a quaternion from 3 orthonormal local axes.
static const Quaternion IDENTITY
static const Quaternion ZERO
Wrapper class which indicates a given angle value is in Degrees.
Definition: OgreMath.h:97
Standard 3-dimensional vector.
Definition: OgreVector3.h:51
Wrapper class which indicates a given angle value is in Radians.
Definition: OgreMath.h:46
static bool isNaN(Real f)
Definition: OgreMath.h:247
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator, (ISO C++)