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-2013 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
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;
183 Quaternion operator* (Real fScalar) const;
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;
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
266 const Quaternion& rkP, const Quaternion& rkQ,
267 int iExtraSpins);
268
270 static void Intermediate (const Quaternion& rkQ0,
271 const Quaternion& rkQ1, const Quaternion& rkQ2,
272 Quaternion& rka, Quaternion& rkB);
273
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
#define _OgreExport
Definition: OgrePlatform.h:257
Wrapper class which indicates a given angle value is in Degrees.
Definition: OgreMath.h:99
static bool isNaN(Real f)
Definition: OgreMath.h:305
A 3x3 matrix which can represent rotations around axes.
Definition: OgreMatrix3.h:69
Implementation of a Quaternion, i.e.
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
Standard 3-dimensional vector.
Definition: OgreVector3.h:52
Radian operator*(Real a, const Radian &b)
Definition: OgreMath.h:747
bool operator!=(STLAllocator< T, P > const &, STLAllocator< T2, P > const &)
determine equality, can memory from another allocator be released by this allocator,...
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.

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.