OGRE  1.9.0
OgreQuaternion.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2014 Torus Knot Software Ltd
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE 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
42namespace 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 }
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
142 inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
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
172 {
173 w = rkQ.w;
174 x = rkQ.x;
175 y = rkQ.y;
176 z = rkQ.z;
177 return *this;
178 }
184 const Quaternion& rkQ);
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;
206 Quaternion Exp () const;
207 Quaternion Log () const;
208
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
273 const Quaternion& rkQ, bool shortestPath = false);
274
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,
287
290 const Quaternion& rkA, const Quaternion& rkB,
291 const Quaternion& rkQ, bool shortestPath = false);
292
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
Wrapper class which indicates a given angle value is in Degrees.
Definition OgreMath.h:99
A 3x3 matrix which can represent rotations around axes.
Definition OgreMatrix3.h:69
Implementation of a Quaternion, i.e.
bool orientationEquals(const Quaternion &other, Real tolerance=1e-3) const
Compare two quaternions which are assumed to be used as orientations.
Quaternion(const Vector3 &xaxis, const Vector3 &yaxis, const Vector3 &zaxis)
Construct a quaternion from 3 orthonormal local axes.
Radian getYaw(bool reprojectAxis=true) const
Calculate the local yaw element of this quaternion.
static Quaternion nlerp(Real fT, const Quaternion &rkP, const Quaternion &rkQ, bool shortestPath=false)
Performs Normalised linear interpolation between two quaternions, and returns the result.
Quaternion(Real *valptr)
Construct a quaternion from 4 manual w/x/y/z values.
static Quaternion SlerpExtraSpins(Real fT, const Quaternion &rkP, const Quaternion &rkQ, int iExtraSpins)
void ToAngleAxis(Degree &dAngle, Vector3 &rkAxis) const
Real Norm() const
void FromAxes(const Vector3 &xAxis, const Vector3 &yAxis, const Vector3 &zAxis)
Vector3 zAxis(void) const
Returns the Z orthonormal axis defining the quaternion.
Quaternion(const Vector3 *akAxis)
Construct a quaternion from 3 orthonormal local axes.
static const Quaternion IDENTITY
static const Quaternion ZERO
Quaternion Log() const
void swap(Quaternion &other)
Exchange the contents of this quaternion with another.
Vector3 xAxis(void) const
Returns the X orthonormal axis defining the quaternion.
static Quaternion Squad(Real fT, const Quaternion &rkP, const Quaternion &rkA, const Quaternion &rkB, const Quaternion &rkQ, bool shortestPath=false)
Spherical quadratic interpolation.
void ToRotationMatrix(Matrix3 &kRot) const
static const Real msEpsilon
Cutoff for sine near zero.
void FromAxes(const Vector3 *akAxis)
Constructs the quaternion using 3 axes, the axes are assumed to be orthonormal.
Quaternion(const Radian &rfAngle, const Vector3 &rkAxis)
Construct a quaternion from an angle/axis.
void ToAngleAxis(Radian &rfAngle, Vector3 &rkAxis) const
Quaternion UnitInverse() const
Apply to non-zero quaternion.
Real * ptr()
Pointer accessor for direct copying.
static void Intermediate(const Quaternion &rkQ0, const Quaternion &rkQ1, const Quaternion &rkQ2, Quaternion &rka, Quaternion &rkB)
Setup for spherical quadratic interpolation.
Quaternion(Real fW, Real fX, Real fY, Real fZ)
Construct from an explicit list of values.
Real Dot(const Quaternion &rkQ) const
Returns the dot product of the quaternion.
Vector3 yAxis(void) const
Returns the Y orthonormal axis defining the quaternion.
bool equals(const Quaternion &rhs, const Radian &tolerance) const
Equality with tolerance (tolerance is max angle difference)
static Quaternion Slerp(Real fT, const Quaternion &rkP, const Quaternion &rkQ, bool shortestPath=false)
Performs Spherical linear interpolation between two quaternions, and returns the result.
Quaternion()
Default constructor, initializes to identity rotation (aka 0°)
void FromAngleAxis(const Radian &rfAngle, const Vector3 &rkAxis)
Setups the quaternion using the supplied vector, and "roll" around that vector by the specified radia...
bool isNaN() const
Check whether this quaternion contains valid values.
Quaternion(const Matrix3 &rot)
Construct a quaternion from a rotation matrix.
void ToAxes(Vector3 &xAxis, Vector3 &yAxis, Vector3 &zAxis) const
Radian getRoll(bool reprojectAxis=true) const
Calculate the local roll element of this quaternion.
Quaternion Inverse() const
Radian getPitch(bool reprojectAxis=true) const
Calculate the local pitch element of this quaternion.
void FromRotationMatrix(const Matrix3 &kRot)
void ToAxes(Vector3 *akAxis) const
Gets the 3 orthonormal axes defining the quaternion.
const Real * ptr() const
Pointer accessor for direct copying.
Quaternion Exp() const
Apply to unit-length quaternion.
Real normalise(void)
Normalises this quaternion, and returns the previous length.
Wrapper class which indicates a given angle value is in Radians.
Definition OgreMath.h:48
Reference-counted shared pointer, used for objects where implicit destruction is required.
Standard 3-dimensional vector.
Definition OgreVector3.h:52
bool operator==(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator,...
float Real
Software floating point type.
void swap(Ogre::SmallVectorImpl< T > &LHS, Ogre::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.