RoboidControl
Loading...
Searching...
No Matches
Quaternion.h
Go to the documentation of this file.
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0.If a copy of the MPL was not distributed with this
3// file, You can obtain one at https ://mozilla.org/MPL/2.0/.
4
5#ifndef QUATERNION_H
6#define QUATERNION_H
7
8#include "Vector3.h"
9
10namespace Passer {
11namespace LinearAlgebra {
12
13extern "C" {
18typedef struct Quat {
22 float x;
26 float y;
30 float z;
34 float w;
36}
37
41struct Quaternion : Quat {
42public:
46 Quaternion();
54 Quaternion(float _x, float _y, float _z, float _w);
64
68 const static Quaternion identity;
69
75 void Normalize();
83 static Quaternion Normalize(const Quaternion &q);
84
91 static Vector3 ToAngles(const Quaternion &q);
92
98 Vector3 operator*(const Vector3 &vector) const;
106 Quaternion operator*(const Quaternion &rotation) const;
107
117 bool operator==(const Quaternion &quaternion);
118
124 static Quaternion Inverse(Quaternion quaternion);
125
132 static Quaternion LookRotation(const Vector3 &forward,
133 const Vector3 &upwards);
143 static Quaternion LookRotation(const Vector3 &forward);
144
151 static Quaternion FromToRotation(Vector3 fromDirection, Vector3 toDirection);
152
160 static Quaternion RotateTowards(const Quaternion &from, const Quaternion &to,
161 float maxDegreesDelta);
162
169 static Quaternion AngleAxis(float angle, const Vector3 &axis);
175 void ToAngleAxis(float *angle, Vector3 *axis);
176
184 static float Angle(Quaternion orientation1, Quaternion orientation2);
193 static Quaternion Slerp(const Quaternion &rotation1,
194 const Quaternion &rotation2, float factor);
204 static Quaternion SlerpUnclamped(const Quaternion &rotation1,
205 const Quaternion &rotation2, float factor);
206
215 static Quaternion Euler(float x, float y, float z);
222 static Quaternion Euler(Vector3 eulerAngles);
223
232 static Quaternion EulerXYZ(float x, float y, float z);
239 static Quaternion EulerXYZ(Vector3 eulerAngles);
240
247 static float GetAngleAround(Vector3 axis, Quaternion rotation);
254 static Quaternion GetRotationAround(Vector3 axis, Quaternion rotation);
263 static void GetSwingTwist(Vector3 axis, Quaternion rotation,
264 Quaternion *swing, Quaternion *twist);
265
272 static float Dot(Quaternion rotation1, Quaternion rotation2);
273
274private:
275 float GetLength() const;
276 float GetLengthSquared() const;
277 static float GetLengthSquared(const Quaternion &q);
278
279 void ToAxisAngleRad(const Quaternion &q, Vector3 *const axis, float *angle);
280 static Quaternion FromEulerRad(Vector3 euler);
281 static Quaternion FromEulerRadXYZ(Vector3 euler);
282
283 Vector3 xyz() const;
284};
285
286} // namespace LinearAlgebra
287} // namespace Passer
288using namespace Passer::LinearAlgebra;
289
290#endif
Definition AngleAxis.h:14
Definition Angle.h:9
AngleOf< float > Angle
Definition Angle.h:42
Definition AbsoluteEncoder.h:5
A quaternion.
Definition Quaternion.h:18
float x
The x component.
Definition Quaternion.h:22
float y
The y component.
Definition Quaternion.h:26
float w
The w component.
Definition Quaternion.h:34
float z
The z component.
Definition Quaternion.h:30
A quaternion.
Definition Quaternion.h:41
static Quaternion Inverse(Quaternion quaternion)
The inverse of quaterion.
Definition Quaternion.cpp:140
static Quaternion GetRotationAround(Vector3 axis, Quaternion rotation)
Returns the rotation limited around the given axis.
Definition Quaternion.cpp:397
static Quaternion EulerXYZ(float x, float y, float z)
Create a rotation from euler angles.
Definition Quaternion.cpp:353
static void GetSwingTwist(Vector3 axis, Quaternion rotation, Quaternion *swing, Quaternion *twist)
Swing-twist decomposition of a rotation.
Definition Quaternion.cpp:406
static Vector3 ToAngles(const Quaternion &q)
Convert to euler angles.
Definition Quaternion.cpp:77
static Quaternion FromToRotation(Vector3 fromDirection, Vector3 toDirection)
Calculat the rotation from on vector to another.
Definition Quaternion.cpp:201
void Normalize()
Convert to unit quaternion.
Definition Quaternion.cpp:58
~Quaternion()
Quaternion destructor.
Definition Quaternion.cpp:41
Vector3 operator*(const Vector3 &vector) const
Rotate a vector using this quaterion.
void ToAngleAxis(float *angle, Vector3 *axis)
Convert this quaternion to angle/axis representation.
Definition Quaternion.cpp:243
static float GetAngleAround(Vector3 axis, Quaternion rotation)
Returns the angle of around the give axis for a rotation.
Definition Quaternion.cpp:384
static const Quaternion identity
An identity quaternion.
Definition Quaternion.h:68
static Quaternion SlerpUnclamped(const Quaternion &rotation1, const Quaternion &rotation2, float factor)
Unclamped sherical lerp between two rotations.
Definition Quaternion.cpp:261
static Quaternion Euler(Vector3 eulerAngles)
Create a rotation from a vector containing euler angles.
static Quaternion LookRotation(const Vector3 &forward, const Vector3 &upwards)
A rotation which looks in the given direction.
static Quaternion EulerXYZ(Vector3 eulerAngles)
Create a rotation from a vector containing euler angles.
Quaternion()
Create a new identity quaternion.
Definition Quaternion.cpp:20
static Quaternion Slerp(const Quaternion &rotation1, const Quaternion &rotation2, float factor)
Sherical lerp between two rotations.
Definition Quaternion.cpp:312
Quaternion operator*(const Quaternion &rotation) const
Multiply this quaternion with another quaternion.
bool operator==(const Quaternion &quaternion)
Check the equality of two quaternions.
Definition Quaternion.cpp:136
static float Dot(Quaternion rotation1, Quaternion rotation2)
Calculate the dot product of two quaternions.
Definition Quaternion.cpp:73
static Quaternion Euler(float x, float y, float z)
Create a rotation from euler angles.
Definition Quaternion.cpp:321
static Quaternion RotateTowards(const Quaternion &from, const Quaternion &to, float maxDegreesDelta)
Rotate form one orientation to anther with a maximum amount of degrees.
Definition Quaternion.cpp:210
A 3-dimensional vector.
Definition Vector3.h:42