OGRE  1.9.0
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-2014 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 
57  {
58  public:
60  inline Quaternion ()
61  : w(1), x(0), y(0), z(0)
62  {
63  }
65  inline Quaternion (
66  Real fW,
67  Real fX, Real fY, Real fZ)
68  : w(fW), x(fX), y(fY), z(fZ)
69  {
70  }
72  inline Quaternion(const Matrix3& rot)
73  {
74  this->FromRotationMatrix(rot);
75  }
77  inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis)
78  {
79  this->FromAngleAxis(rfAngle, rkAxis);
80  }
82  inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
83  {
84  this->FromAxes(xaxis, yaxis, zaxis);
85  }
87  inline Quaternion(const Vector3* akAxis)
88  {
89  this->FromAxes(akAxis);
90  }
92  inline Quaternion(Real* valptr)
93  {
94  memcpy(&w, valptr, sizeof(Real)*4);
95  }
96 
99  inline void swap(Quaternion& other)
100  {
101  std::swap(w, other.w);
102  std::swap(x, other.x);
103  std::swap(y, other.y);
104  std::swap(z, other.z);
105  }
106 
108  inline Real operator [] ( const size_t i ) const
109  {
110  assert( i < 4 );
111 
112  return *(&w+i);
113  }
114 
116  inline Real& operator [] ( const size_t i )
117  {
118  assert( i < 4 );
119 
120  return *(&w+i);
121  }
122 
124  inline Real* ptr()
125  {
126  return &w;
127  }
128 
130  inline const Real* ptr() const
131  {
132  return &w;
133  }
134 
135  void FromRotationMatrix (const Matrix3& kRot);
136  void ToRotationMatrix (Matrix3& kRot) const;
140  void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis);
141  void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const;
142  inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
143  Radian rAngle;
144  ToAngleAxis ( rAngle, rkAxis );
145  dAngle = rAngle;
146  }
150  void FromAxes (const Vector3* akAxis);
151  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 
159  Vector3 xAxis(void) const;
160 
164  Vector3 yAxis(void) const;
165 
169  Vector3 zAxis(void) const;
170 
171  inline Quaternion& operator= (const Quaternion& rkQ)
172  {
173  w = rkQ.w;
174  x = rkQ.x;
175  y = rkQ.y;
176  z = rkQ.z;
177  return *this;
178  }
179  Quaternion operator+ (const Quaternion& rkQ) const;
180  Quaternion operator- (const Quaternion& rkQ) const;
181  Quaternion operator* (const Quaternion& rkQ) const;
182  Quaternion operator* (Real fScalar) const;
183  _OgreExport friend Quaternion operator* (Real fScalar,
184  const Quaternion& rkQ);
185  Quaternion operator- () const;
186  inline bool operator== (const Quaternion& rhs) const
187  {
188  return (rhs.x == x) && (rhs.y == y) &&
189  (rhs.z == z) && (rhs.w == w);
190  }
191  inline bool operator!= (const Quaternion& rhs) const
192  {
193  return !operator==(rhs);
194  }
195  // functions of a quaternion
197  Real Dot (const Quaternion& rkQ) const;
198  /* Returns the normal length of this quaternion.
199  @note This does <b>not</b> alter any values.
200  */
201  Real Norm () const;
203  Real normalise(void);
204  Quaternion Inverse () const;
205  Quaternion UnitInverse () const;
206  Quaternion Exp () const;
207  Quaternion Log () const;
208 
210  Vector3 operator* (const Vector3& rkVector) const;
211 
221  Radian getRoll(bool reprojectAxis = true) const;
231  Radian getPitch(bool reprojectAxis = true) const;
241  Radian getYaw(bool reprojectAxis = true) const;
242 
247  bool equals(const Quaternion& rhs, const Radian& tolerance) const;
248 
254  inline bool orientationEquals( const Quaternion& other, Real tolerance = 1e-3 ) const
255  {
256  Real d = this->Dot(other);
257  return 1 - d*d < tolerance;
258  }
259 
272  static Quaternion Slerp (Real fT, const Quaternion& rkP,
273  const Quaternion& rkQ, bool shortestPath = false);
274 
279  static Quaternion SlerpExtraSpins (Real fT,
280  const Quaternion& rkP, const Quaternion& rkQ,
281  int iExtraSpins);
282 
284  static void Intermediate (const Quaternion& rkQ0,
285  const Quaternion& rkQ1, const Quaternion& rkQ2,
286  Quaternion& rka, Quaternion& rkB);
287 
289  static Quaternion Squad (Real fT, const Quaternion& rkP,
290  const Quaternion& rkA, const Quaternion& rkB,
291  const Quaternion& rkQ, bool shortestPath = false);
292 
307  static Quaternion nlerp(Real fT, const Quaternion& rkP,
308  const Quaternion& rkQ, bool shortestPath = false);
309 
311  static const Real msEpsilon;
312 
313  // special values
314  static const Quaternion ZERO;
315  static const Quaternion IDENTITY;
316 
317  Real w, x, y, z;
318 
320  inline bool isNaN() const
321  {
322  return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
323  }
324 
328  inline _OgreExport friend std::ostream& operator <<
329  ( std::ostream& o, const Quaternion& q )
330  {
331  o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
332  return o;
333  }
334 
335  };
339 }
340 
341 
342 
343 
344 #endif
#define _OgreExport
Definition: OgrePlatform.h:260
Real * ptr()
Pointer accessor for direct copying.
Implementation of a Quaternion, i.e.
A 3x3 matrix which can represent rotations around axes.
Definition: OgreMatrix3.h:68
Quaternion(const Radian &rfAngle, const Vector3 &rkAxis)
Construct a quaternion from an angle/axis.
static bool isNaN(Real f)
Definition: OgreMath.h:305
float Real
Software floating point type.
static const Real msEpsilon
Cutoff for sine near zero.
Quaternion(const Vector3 &xaxis, const Vector3 &yaxis, const Vector3 &zaxis)
Construct a quaternion from 3 orthonormal local axes.
Standard 3-dimensional vector.
Definition: OgreVector3.h:51
void ToAngleAxis(Degree &dAngle, Vector3 &rkAxis) const
static const Quaternion ZERO
const Real * ptr() const
Pointer accessor for direct copying.
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator,...
Quaternion(const Matrix3 &rot)
Construct a quaternion from a rotation matrix.
Quaternion(Real fW, Real fX, Real fY, Real fZ)
Construct from an explicit list of values.
Radian operator *(Real a, const Radian &b)
Definition: OgreMath.h:747
bool orientationEquals(const Quaternion &other, Real tolerance=1e-3) const
Compare two quaternions which are assumed to be used as orientations.
void swap(Ogre::SmallVectorImpl< T > &LHS, Ogre::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
bool isNaN() const
Check whether this quaternion contains valid values.
Quaternion()
Default constructor, initializes to identity rotation (aka 0°)
Quaternion(Real *valptr)
Construct a quaternion from 4 manual w/x/y/z values.
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator,...
void swap(Quaternion &other)
Exchange the contents of this quaternion with another.
Wrapper class which indicates a given angle value is in Degrees.
Definition: OgreMath.h:98
static const Quaternion IDENTITY
Quaternion(const Vector3 *akAxis)
Construct a quaternion from 3 orthonormal local axes.
Wrapper class which indicates a given angle value is in Radians.
Definition: OgreMath.h:47