LinearAlgebra
Loading...
Searching...
No Matches
Quaternion.h
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
10extern "C" {
15typedef struct Quat {
19 float x;
23 float y;
27 float z;
31 float w;
32} Quat;
33}
34
35namespace Passer {
36namespace LinearAlgebra {
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
107
117 bool operator==(const Quaternion &quaternion) const;
118
125
132 static Quaternion LookRotation(const Vector3 &forward,
133 const Vector3 &upwards);
143 static Quaternion LookRotation(const Vector3 &forward);
144
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
193 static Quaternion Slerp(const Quaternion &rotation1,
194 const Quaternion &rotation2, float factor);
205 const Quaternion &rotation2, float factor);
206
215 static Quaternion Euler(float x, float y, float z);
223
232 static Quaternion EulerXYZ(float x, float y, float z);
240
264 Quaternion *swing, Quaternion *twist);
265
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
An angle in various representations.
Definition Angle.h:21
A quaternion.
Definition Quaternion.h:41
static float Angle(Quaternion orientation1, Quaternion orientation2)
Get the angle between two orientations.
Definition Quaternion.cpp:241
static Quaternion Inverse(Quaternion quaternion)
The inverse of quaterion.
Definition Quaternion.cpp:143
static Quaternion GetRotationAround(Vector3 axis, Quaternion rotation)
Returns the rotation limited around the given axis.
Definition Quaternion.cpp:403
static Quaternion AngleAxis(float angle, const Vector3 &axis)
Convert an angle/axis representation to a quaternion.
Definition Quaternion.cpp:224
static Quaternion EulerXYZ(float x, float y, float z)
Create a rotation from euler angles.
Definition Quaternion.cpp:359
static void GetSwingTwist(Vector3 axis, Quaternion rotation, Quaternion *swing, Quaternion *twist)
Swing-twist decomposition of a rotation.
Definition Quaternion.cpp:412
static Vector3 ToAngles(const Quaternion &q)
Convert to euler angles.
Definition Quaternion.cpp:80
bool operator==(const Quaternion &quaternion) const
Check the equality of two quaternions.
Definition Quaternion.cpp:139
static Quaternion FromToRotation(Vector3 fromDirection, Vector3 toDirection)
Calculat the rotation from on vector to another.
Definition Quaternion.cpp:204
void Normalize()
Convert to unit quaternion.
Definition Quaternion.cpp:61
~Quaternion()
Quaternion destructor.
Definition Quaternion.cpp:42
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:246
static float GetAngleAround(Vector3 axis, Quaternion rotation)
Returns the angle of around the give axis for a rotation.
Definition Quaternion.cpp:390
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:265
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:21
static Quaternion Slerp(const Quaternion &rotation1, const Quaternion &rotation2, float factor)
Sherical lerp between two rotations.
Definition Quaternion.cpp:317
Quaternion operator*(const Quaternion &rotation) const
Multiply this quaternion with another quaternion.
static float Dot(Quaternion rotation1, Quaternion rotation2)
Calculate the dot product of two quaternions.
Definition Quaternion.cpp:76
static Quaternion Euler(float x, float y, float z)
Create a rotation from euler angles.
Definition Quaternion.cpp:327
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:213
A 3-dimensional vector.
Definition Vector3.h:42
A quaternion (C-style)
Definition Quaternion.h:15
float x
The x component.
Definition Quaternion.h:19
float z
The z component.
Definition Quaternion.h:27
float y
The y component.
Definition Quaternion.h:23
float w
The w component.
Definition Quaternion.h:31