#include <muu/quaternion.h>
template <typename Scalar>
quaternion struct
A quaternion.
Template parameters | |
---|---|
Scalar | The quaternion's scalar component type. Must be a floating-point type. |
Public types
-
using axis_angle_type = axis_
angle<scalar_ type> - The muu::
axis_angle with the same scalar_ type as this quaternion. -
using constants = muu::
constants<quaternion> - Compile-time constants for this quaternion.
-
using euler_type = euler_
angles<scalar_ type> - The muu::
euler_angles with the same scalar_ type as this quaternion. - using scalar_type = Scalar
- The type of each scalar component stored in this quaternion.
-
using vector_type = vector<scalar_
type, 3> - The three-dimensional muu::
vector with the same scalar_ type as this quaternion.
Public static functions
-
static auto slerp(const quaternion& start,
const quaternion& finish,
scalar_
type alpha) -> quaternion constexpr noexcept - Performs a spherical-linear interpolation between two quaternion orientations.
Constructors, destructors, conversion operators
- quaternion() defaulted noexcept
- Default constructor. Values are not initialized.
- quaternion(const quaternion&) defaulted constexpr noexcept
- Copy constructor.
-
quaternion(scalar_
type s, scalar_ type vx, scalar_ type vy, scalar_ type vz) constexpr noexcept - Constructs a quaternion from raw scalar values.
-
quaternion(scalar_
type s, const vector_ type& v) constexpr noexcept - Constructs a quaternion from a scalar and a vector.
-
template <typename T>quaternion(const quaternion<T>& quat) explicit constexpr noexcept
- Converting constructor.
-
quaternion(const axis_
angle_ type& aa) explicit constexpr noexcept - Constructs a quaternion from an axis-angle rotation.
-
quaternion(const euler_
type& euler) explicit constexpr noexcept - Constructs a quaternion from a set of euler angles.
-
quaternion(const matrix<scalar_
type, 3, 3>& rot) explicit constexpr noexcept - Constructs a quaternion from a 3x3 3D rotation matrix.
-
quaternion(const matrix<scalar_
type, 3, 4>& rot) explicit constexpr noexcept - Constructs a quaternion from the lower 3x3 part of a 3x4 3D transform matrix.
-
quaternion(const matrix<scalar_
type, 4, 4>& rot) explicit constexpr noexcept - Constructs a quaternion from the lower 3x3 part of a 4x4 3D transform matrix.
-
template <typename T>quaternion(const T& obj) constexpr noexcept
- Constructs a quaternion from an implicitly bit-castable type.
Public functions
-
auto data() const -> constscalar_
type* constexpr noexcept - Returns a pointer to the first scalar component in the quaternion.
-
auto data() -> scalar_
type* constexpr noexcept - Returns a pointer to the first scalar component in the quaternion.
-
template <size_t Index>auto get() const -> const auto& constexpr noexcept
- Gets a reference to the value at a specific index.
-
template <size_t Index>auto get() -> auto& constexpr noexcept
- Gets a reference to the value at a specific index.
- auto operator=(const quaternion&) -> quaternion& defaulted constexpr noexcept
- Copy-assigment operator.
-
auto slerp(const quaternion& target,
scalar_
type alpha) -> quaternion& constexpr noexcept - Performs a spherical-linear interpolation on this quaternion (in-place).
Public variables
-
scalar_
type s - The quaternion's scalar (real) part.
-
vector_
type v - The quaternion's vector (imaginary) part.
Conjugate
- static auto conjugate(const quaternion& q) -> quaternion constexpr noexcept
- Returns the conjugate of a quaternion.
- auto conjugate() const -> quaternion constexpr noexcept
- Returns the conjugate of the quaternion.
Dot product
-
static auto dot(const quaternion& q1,
const quaternion& q2) -> scalar_
type constexpr noexcept - Returns the dot product of two quaternions.
-
auto dot(const quaternion& q) const -> scalar_
type constexpr noexcept - Returns the dot product of this and another quaternion.
Equality (approximate)
-
template <typename T>static auto approx_equal(const quaternion& q1, const quaternion<T>& q2, epsilon_
type<scalar_ type, T> epsilon = default_ epsilon<scalar_ type, T>) -> bool constexpr noexcept - Returns true if two quaternions are approximately equal.
-
static auto approx_zero(const quaternion& q,
scalar_
type epsilon = default_ epsilon<scalar_ type>) -> bool constexpr noexcept - Returns true if all the scalar components in a quaternion are approximately equal to zero.
-
template <typename T>auto approx_equal(const quaternion<T>& q, epsilon_
type<scalar_ type, T> epsilon = default_ epsilon<scalar_ type, T>) const -> bool constexpr noexcept - Returns true if the quaternion is approximately equal to another.
-
auto approx_zero(scalar_
type epsilon = default_ epsilon<scalar_ type>) const -> bool constexpr noexcept - Returns true if all the scalar components in the quaternion are approximately equal to zero.
Equality (exact)
- static auto infinity_or_nan(const quaternion& q) -> bool constexpr noexcept
- Returns true if any of the scalar components of a quaternion are infinity or NaN.
- static auto zero(const quaternion& q) -> bool constexpr noexcept
- Returns true if all the scalar components of a quaternion are exactly zero.
- auto infinity_or_nan() const -> bool constexpr noexcept
- Returns true if any of the scalar components of the quaternion are infinity or NaN.
- auto zero() const -> bool constexpr noexcept
- Returns true if all the scalar components of the quaternion are exactly zero.
-
template <typename T>auto operator!=(const quaternion& lhs, const quaternion<T>& rhs) -> bool constexpr noexcept
- Returns true if two quaternions are not exactly equal.
-
template <typename T>auto operator==(const quaternion& lhs, const quaternion<T>& rhs) -> bool constexpr noexcept
- Returns true if two quaternions are exactly equal.
Multiplication
- auto operator*=(const quaternion& rhs) -> quaternion& constexpr noexcept
- Multiplies this quaternion with another.
-
auto operator*=(scalar_
type rhs) -> quaternion& constexpr noexcept - Scales the shortest-path rotation equivalent of this quaternion by a scalar (in-place).
- auto operator*(const quaternion& lhs, const quaternion& rhs) -> quaternion constexpr noexcept
- Multiplies two quaternions.
-
auto operator*(const quaternion& lhs,
const vector_
type& rhs) -> vector_ type constexpr noexcept - Rotates a three-dimensional vector by the rotation encoded in a quaternion.
-
auto operator*(const vector_
type& lhs, const quaternion& rhs) -> vector_ type constexpr noexcept - Rotates a three-dimensional vector by the rotation encoded in a quaternion.
-
auto operator*(const quaternion& lhs,
scalar_
type rhs) -> quaternion constexpr noexcept - Scales the shortest-path rotation equivalent of a quaternion by a scalar.
-
auto operator*(scalar_
type lhs, const quaternion& rhs) -> quaternion constexpr noexcept - Scales the shortest-path rotation equivalent of a quaternion by a scalar.
Normalization
- static auto normalize(const quaternion& q) -> quaternion constexpr noexcept
- Normalizes a quaternion.
- static auto normalized(const quaternion& q) -> bool constexpr noexcept
- Returns true if a quaternion is normalized (i.e. has a length of 1).
- auto normalize() -> quaternion& constexpr noexcept
- Normalizes the quaternion (in-place).
- auto normalized() const -> bool constexpr noexcept
- Returns true if the quaternion is normalized (i.e. has a length of 1).
Rotations
-
static auto from_axis_angle(const vector_
type& axis, scalar_ type angle) -> quaternion constexpr noexcept - Creates a quaternion from an axis-angle rotation.
-
static auto from_axis_angle(const axis_
angle_ type& aa) -> quaternion constexpr noexcept - Creates a quaternion from an axis-angle rotation.
-
static auto from_euler(scalar_
type yaw, scalar_ type pitch, scalar_ type roll) -> quaternion constexpr noexcept - Creates a quaternion from a set of euler angles.
-
static auto from_euler(const euler_
type& euler) -> quaternion constexpr noexcept - Creates a quaternion from an axis-angle rotation.
-
static auto from_rotation(const vector_
type& from, const vector_ type& to) -> quaternion constexpr noexcept - Creates a quaternion encoding a rotation from one direction vector to another.
- static auto from_rotation(const matrix<Scalar, 3, 3>& rot) -> quaternion constexpr noexcept
- Creates a quaternion from a 3x3 3D rotation matrix.
- static auto from_rotation(const matrix<Scalar, 3, 4>& rot) -> quaternion constexpr noexcept
- Creates a quaternion from the lower 3x3 part of a 3x4 3D transform matrix.
- static auto from_rotation(const matrix<Scalar, 4, 4>& rot) -> quaternion constexpr noexcept
- Creates a quaternion from the lower 3x3 part of a 4x4 3D transform matrix.
-
static auto to_axis_angle(const quaternion& quat,
bool shortest_path = true) -> axis_
angle_ type constexpr noexcept - Extracts an axis-angle rotation from a quaternion.
-
static auto to_euler(const quaternion& q) -> euler_
type constexpr noexcept - Extracts a set of euler angles from a quaternion.
-
auto to_axis_angle(bool shortest_path = true) const -> axis_
angle_ type constexpr noexcept - Extracts an axis-angle rotation from this quaternion.
-
auto to_euler() const -> euler_
type constexpr noexcept - Extracts a set of euler angles from this quaternion.
Friends
-
template <typename Char, typename Traits>auto operator<<(std::
basic_ostream<Char, Traits>& os, const quaternion& q) -> std:: basic_ostream<Char, Traits>& - Writes a quaternion out to a text stream.
Function documentation
template <typename Scalar>
static quaternion muu::quaternion::slerp(const quaternion& start,
const quaternion& finish,
scalar_ type alpha) constexpr noexcept
Performs a spherical-linear interpolation between two quaternion orientations.
Parameters | |
---|---|
start | The start orientation. |
finish | The finish orientation. |
alpha | The blend factor. |
Returns | A quaternion orientation derived from a spherical-linear interpolation between start and finish . |
template <typename Scalar>
muu::quaternion::quaternion(scalar_ type s,
scalar_ type vx,
scalar_ type vy,
scalar_ type vz) constexpr noexcept
Constructs a quaternion from raw scalar values.
Parameters | |
---|---|
s | Initial value for the scalar (real) part. |
vx | Initial value for the vector (imaginary) part's X component. |
vy | Initial value for the vector (imaginary) part's Y component. |
vz | Initial value for the vector (imaginary) part's Z component. |
template <typename Scalar>
muu::quaternion::quaternion(scalar_ type s,
const vector_ type& v) constexpr noexcept
Constructs a quaternion from a scalar and a vector.
Parameters | |
---|---|
s | Initial value for the scalar (real) part. |
v | Initial value for the vector (imaginary) part. |
template <typename Scalar>
template <typename T>
muu::quaternion::quaternion(const T& obj) constexpr noexcept
template <typename T>
Constructs a quaternion from an implicitly bit-castable type.
Template parameters | |
---|---|
T | A bit-castable type. |
template <typename Scalar>
quaternion& muu::quaternion::slerp(const quaternion& target,
scalar_ type alpha) constexpr noexcept
Performs a spherical-linear interpolation on this quaternion (in-place).
Parameters | |
---|---|
target | The 'target' orientation. |
alpha | The blend factor. |
Returns | A reference to the quaternion. |
template <typename Scalar>
static bool muu::quaternion::zero(const quaternion& q) constexpr noexcept
Returns true if all the scalar components of a quaternion are exactly zero.
template <typename Scalar>
bool muu::quaternion::zero() const constexpr noexcept
Returns true if all the scalar components of the quaternion are exactly zero.
template <typename Scalar>
template <typename T>
bool muu::quaternion::operator!=(const quaternion& lhs,
const quaternion<T>& rhs) constexpr noexcept
template <typename T>
Returns true if two quaternions are not exactly equal.
template <typename Scalar>
template <typename T>
bool muu::quaternion::operator==(const quaternion& lhs,
const quaternion<T>& rhs) constexpr noexcept
template <typename T>
Returns true if two quaternions are exactly equal.
template <typename Scalar>
static quaternion muu::quaternion::normalize(const quaternion& q) constexpr noexcept
Normalizes a quaternion.
Parameters | |
---|---|
q | The quaternion to normalize. |
Returns | A normalized copy of the input quaternion. |
template <typename Scalar>
quaternion& muu::quaternion::normalize() constexpr noexcept
Normalizes the quaternion (in-place).
Returns | A reference to the quaternion. |
---|
template <typename Scalar>
static quaternion muu::quaternion::from_axis_angle(const vector_ type& axis,
scalar_ type angle) constexpr noexcept
Creates a quaternion from an axis-angle rotation.
Parameters | |
---|---|
axis | Axis to rotate around. Must be normalized. |
angle | Angle to rotate by (in radians). |
Returns | A quaternion encoding the given axis-angle rotation. |
template <typename Scalar>
static quaternion muu::quaternion::from_euler(scalar_ type yaw,
scalar_ type pitch,
scalar_ type roll) constexpr noexcept
Creates a quaternion from a set of euler angles.
Parameters | |
---|---|
yaw | The rotation around the local up axis (in radians). |
pitch | The rotation around the local right axis (in radians). |
roll | The rotation around the local forward axis (in radians). |
Returns | A quaternion encoding the given axis-angle rotation. |
template <typename Scalar>
static quaternion muu::quaternion::from_rotation(const vector_ type& from,
const vector_ type& to) constexpr noexcept
Creates a quaternion encoding a rotation from one direction vector to another.
Parameters | |
---|---|
from | The source direction. |
to | The destination direction. |
Returns | A quaternion encoding a rotation between the two direction vectors. |
template <typename Scalar>
static axis_ angle_ type muu::quaternion::to_axis_angle(const quaternion& quat,
bool shortest_path = true) constexpr noexcept
Extracts an axis-angle rotation from a quaternion.
Parameters | |
---|---|
quat | The quaternion to convert. Must be normalized. |
shortest_path | Should the returned values be derived from the shortest path? true is more intuitive but means that the returned values may not convert back to the original quaternion if used with from_ |
Returns | An axis-angle rotation representing the rotation stored in the given quaternion. |
template <typename Scalar>
static euler_ type muu::quaternion::to_euler(const quaternion& q) constexpr noexcept
Extracts a set of euler angles from a quaternion.
Parameters | |
---|---|
q | The quaternion to convert. |
Returns | A set of euler angles representing the rotation stored in the given quaternion. |
template <typename Scalar>
axis_ angle_ type muu::quaternion::to_axis_angle(bool shortest_path = true) const constexpr noexcept
Extracts an axis-angle rotation from this quaternion.
Parameters | |
---|---|
shortest_path | Should the returned values be derived from the shortest path? true is more intuitive but means that the returned values may not convert back to the original quaternion if used with from_ |
Returns | An axis-angle rotation representing the rotation stored in the quaternion. |
template <typename Scalar>
template <typename S>
quaternion<S> normalize(const quaternion<S>& q) constexpr noexcept
template <typename S>
Normalizes a quaternion.
Parameters | |
---|---|
q | The quaternion to normalize. |
Returns | A normalized copy of the input quaternion. |