OGRE  1.8
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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-2013 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 
58  {
59  public:
61  inline Quaternion ()
62  : w(1), x(0), y(0), z(0)
63  {
64  }
66  inline Quaternion (
67  Real fW,
68  Real fX, Real fY, Real fZ)
69  : w(fW), x(fX), y(fY), z(fZ)
70  {
71  }
73  inline Quaternion(const Matrix3& rot)
74  {
75  this->FromRotationMatrix(rot);
76  }
78  inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis)
79  {
80  this->FromAngleAxis(rfAngle, rkAxis);
81  }
83  inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
84  {
85  this->FromAxes(xaxis, yaxis, zaxis);
86  }
88  inline Quaternion(const Vector3* akAxis)
89  {
90  this->FromAxes(akAxis);
91  }
93  inline Quaternion(Real* valptr)
94  {
95  memcpy(&w, valptr, sizeof(Real)*4);
96  }
97 
100  inline void swap(Quaternion& other)
101  {
102  std::swap(w, other.w);
103  std::swap(x, other.x);
104  std::swap(y, other.y);
105  std::swap(z, other.z);
106  }
107 
109  inline Real operator [] ( const size_t i ) const
110  {
111  assert( i < 4 );
112 
113  return *(&w+i);
114  }
115 
117  inline Real& operator [] ( const size_t i )
118  {
119  assert( i < 4 );
120 
121  return *(&w+i);
122  }
123 
125  inline Real* ptr()
126  {
127  return &w;
128  }
129 
131  inline const Real* ptr() const
132  {
133  return &w;
134  }
135 
136  void FromRotationMatrix (const Matrix3& kRot);
137  void ToRotationMatrix (Matrix3& kRot) const;
141  void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis);
142  void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const;
143  inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
144  Radian rAngle;
145  ToAngleAxis ( rAngle, rkAxis );
146  dAngle = rAngle;
147  }
151  void FromAxes (const Vector3* akAxis);
152  void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
154  void ToAxes (Vector3* akAxis) const;
155  void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
156 
160  Vector3 xAxis(void) const;
161 
165  Vector3 yAxis(void) const;
166 
170  Vector3 zAxis(void) const;
171 
172  inline Quaternion& operator= (const Quaternion& rkQ)
173  {
174  w = rkQ.w;
175  x = rkQ.x;
176  y = rkQ.y;
177  z = rkQ.z;
178  return *this;
179  }
180  Quaternion operator+ (const Quaternion& rkQ) const;
181  Quaternion operator- (const Quaternion& rkQ) const;
182  Quaternion operator* (const Quaternion& rkQ) const;
183  Quaternion operator* (Real fScalar) const;
184  _OgreExport friend Quaternion operator* (Real fScalar,
185  const Quaternion& rkQ);
186  Quaternion operator- () const;
187  inline bool operator== (const Quaternion& rhs) const
188  {
189  return (rhs.x == x) && (rhs.y == y) &&
190  (rhs.z == z) && (rhs.w == w);
191  }
192  inline bool operator!= (const Quaternion& rhs) const
193  {
194  return !operator==(rhs);
195  }
196  // functions of a quaternion
198  Real Dot (const Quaternion& rkQ) const;
199  /* Returns the normal length of this quaternion.
200  @note This does <b>not</b> alter any values.
201  */
202  Real Norm () const;
204  Real normalise(void);
205  Quaternion Inverse () const; // apply to non-zero quaternion
206  Quaternion UnitInverse () const; // apply to unit-length quaternion
207  Quaternion Exp () const;
208  Quaternion Log () const;
209 
211  Vector3 operator* (const Vector3& rkVector) const;
212 
222  Radian getRoll(bool reprojectAxis = true) const;
232  Radian getPitch(bool reprojectAxis = true) const;
242  Radian getYaw(bool reprojectAxis = true) const;
244  bool equals(const Quaternion& rhs, const Radian& tolerance) const;
245 
258  static Quaternion Slerp (Real fT, const Quaternion& rkP,
259  const Quaternion& rkQ, bool shortestPath = false);
260 
265  static Quaternion SlerpExtraSpins (Real fT,
266  const Quaternion& rkP, const Quaternion& rkQ,
267  int iExtraSpins);
268 
269  // setup for spherical quadratic interpolation
270  static void Intermediate (const Quaternion& rkQ0,
271  const Quaternion& rkQ1, const Quaternion& rkQ2,
272  Quaternion& rka, Quaternion& rkB);
273 
274  // spherical quadratic interpolation
275  static Quaternion Squad (Real fT, const Quaternion& rkP,
276  const Quaternion& rkA, const Quaternion& rkB,
277  const Quaternion& rkQ, bool shortestPath = false);
278 
293  static Quaternion nlerp(Real fT, const Quaternion& rkP,
294  const Quaternion& rkQ, bool shortestPath = false);
295 
297  static const Real msEpsilon;
298 
299  // special values
300  static const Quaternion ZERO;
301  static const Quaternion IDENTITY;
302 
303  Real w, x, y, z;
304 
306  inline bool isNaN() const
307  {
308  return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
309  }
310 
314  inline _OgreExport friend std::ostream& operator <<
315  ( std::ostream& o, const Quaternion& q )
316  {
317  o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
318  return o;
319  }
320 
321  };
325 }
326 
327 
328 
329 
330 #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:233
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.
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:720
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
Quaternion()
Default constructor, initializes to identity rotation (aka 0°)
static const Quaternion ZERO
Wrapper class which indicates a given angle value is in Degrees.
Definition: OgreMath.h:97
void swap(Ogre::SmallVectorImpl< T > &LHS, Ogre::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
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:290
Quaternion(Real fW, Real fX, Real fY, Real fZ)
Construct from an explicit list of values.
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++)
static const Real msEpsilon
Cutoff for sine near zero.