MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk::QuatType Class Reference

Quaternion used for 3D rotations. More...

#include <mxvk/include/mxvk/mxvk_math.h>

Public Member Functions

void Conj ()
 Conjugate this quaternion in place.
void Conj ()
 Conjugate this quaternion in place.
void EulerZYX (float theta_x, float theta_y, float theta_z)
 Build a quaternion from Euler angles in ZYX order, with angles in degrees.
void EulerZYX (float theta_x, float theta_y, float theta_z)
 Build a quaternion from Euler angles in ZYX order, with angles in degrees.
void Inverse ()
 Invert this quaternion in place.
void Inverse ()
 Invert this quaternion in place.
void InverseNormal ()
 Invert a unit quaternion by conjugating it.
void InverseNormal ()
 Invert a unit quaternion by conjugating it.
float Norm () const
 Compute the norm.
float Norm () const
 Compute the norm.
float Norm2 () const
 Compute the squared norm.
float Norm2 () const
 Compute the squared norm.
void Normalize ()
 Normalize this quaternion in place, or reset it to identity if it is too small.
void Normalize ()
 Normalize this quaternion in place, or reset it to identity if it is too small.
constexpr QuatType operator* (const QuatType &q) const
 Multiply two quaternions.
constexpr QuatType operator* (const QuatType &q) const
 Multiply two quaternions.
QuatType & operator*= (const QuatType &q)
 Multiply this quaternion by another quaternion in place.
QuatType & operator*= (const QuatType &q)
 Multiply this quaternion by another quaternion in place.
constexpr QuatType operator+ (const QuatType &q) const
 Add two quaternions component-wise.
constexpr QuatType operator+ (const QuatType &q) const
 Add two quaternions component-wise.
constexpr QuatType operator- (const QuatType &q) const
 Subtract two quaternions component-wise.
constexpr QuatType operator- (const QuatType &q) const
 Subtract two quaternions component-wise.
void QuatToVec3D (float *theta_degrees, vec3D &axis) const
 Convert this quaternion to axis-angle form.
void QuatToVec3D (float *theta_degrees, vec3D &axis) const
 Convert this quaternion to axis-angle form.
constexpr QuatType ()
 Construct the identity quaternion.
constexpr QuatType ()
 Construct the identity quaternion.
constexpr QuatType (float x_value, float y_value, float z_value, float w_value)
 Construct from explicit quaternion components.
constexpr QuatType (float x_value, float y_value, float z_value, float w_value)
 Construct from explicit quaternion components.
void Scale (float f)
 Scale all quaternion components in place.
void Scale (float f)
 Scale all quaternion components in place.
QuatType TripleProduct (const QuatType &p1, const QuatType &p2) const
 Return the quaternion product (*this * p1) * p2.
QuatType TripleProduct (const QuatType &p1, const QuatType &p2) const
 Return the quaternion product (*this * p1) * p2.
void vec3DthetaQuat (float theta_degrees, const vec3D &axis)
 Build a quaternion from an axis and angle in degrees.
void vec3DthetaQuat (float theta_degrees, const vec3D &axis)
 Build a quaternion from an axis and angle in degrees.
void vec4DthetaQuat (float theta_degrees, const vec4D &axis)
 Build a quaternion from a 4D axis vector and angle in degrees.
void vec4DthetaQuat (float theta_degrees, const vec4D &axis)
 Build a quaternion from a 4D axis vector and angle in degrees.

Public Attributes

float w = 1.0f
 Scalar component.
float x = 0.0f
 X component of the vector part.
float y = 0.0f
 Y component of the vector part.
float z = 0.0f
 Z component of the vector part.

Detailed Description

Quaternion used for 3D rotations.

Definition at line 1048 of file mxvk_math.h.

Constructor & Destructor Documentation

◆ QuatType() [1/4]

mxvk::QuatType::QuatType ( )
inlineconstexpr

Construct the identity quaternion.

Definition at line 1063 of file mxvk_math.h.

1063: x(0.0f), y(0.0f), z(0.0f), w(1.0f) {}
float w
Scalar component.
Definition mxvk_math.h:1060
float z
Z component of the vector part.
Definition mxvk_math.h:1057
float x
X component of the vector part.
Definition mxvk_math.h:1051
float y
Y component of the vector part.
Definition mxvk_math.h:1054

◆ QuatType() [2/4]

mxvk::QuatType::QuatType ( float x_value,
float y_value,
float z_value,
float w_value )
inlineconstexpr

Construct from explicit quaternion components.

Definition at line 1066 of file mxvk_math.h.

1066: x(x_value), y(y_value), z(z_value), w(w_value) {}

◆ QuatType() [3/4]

mxvk::QuatType::QuatType ( )
inlineconstexpr

Construct the identity quaternion.

Definition at line 1029 of file mxvk_math_eigen.hpp.

1029: x(0.0f), y(0.0f), z(0.0f), w(1.0f) {}

◆ QuatType() [4/4]

mxvk::QuatType::QuatType ( float x_value,
float y_value,
float z_value,
float w_value )
inlineconstexpr

Construct from explicit quaternion components.

Definition at line 1032 of file mxvk_math_eigen.hpp.

1032: x(x_value), y(y_value), z(z_value), w(w_value) {}

Member Function Documentation

◆ Conj() [1/2]

void mxvk::QuatType::Conj ( )
inline

Conjugate this quaternion in place.

Definition at line 1084 of file mxvk_math.h.

1084 {
1085 x = -x;
1086 y = -y;
1087 z = -z;
1088 }

◆ Conj() [2/2]

void mxvk::QuatType::Conj ( )
inline

Conjugate this quaternion in place.

Definition at line 1050 of file mxvk_math_eigen.hpp.

1050 {
1051 x = -x;
1052 y = -y;
1053 z = -z;
1054 }

◆ EulerZYX() [1/2]

void mxvk::QuatType::EulerZYX ( float theta_x,
float theta_y,
float theta_z )
inline

Build a quaternion from Euler angles in ZYX order, with angles in degrees.

Definition at line 1154 of file mxvk_math.h.

1154 {
1155 const float hx = deg2rad(theta_x) * 0.5f;
1156 const float hy = deg2rad(theta_y) * 0.5f;
1157 const float hz = deg2rad(theta_z) * 0.5f;
1158 const float cx = std::cos(hx);
1159 const float sx = std::sin(hx);
1160 const float cy = std::cos(hy);
1161 const float sy = std::sin(hy);
1162 const float cz = std::cos(hz);
1163 const float sz = std::sin(hz);
1164 w = cz * cy * cx + sz * sy * sx;
1165 x = cz * cy * sx - sz * sy * cx;
1166 y = cz * sy * cx + sz * cy * sx;
1167 z = sz * cy * cx - cz * sy * sx;
1168 }
float deg2rad(float ang)
Convert degrees to radians.
Definition mxvk_math.h:109

◆ EulerZYX() [2/2]

void mxvk::QuatType::EulerZYX ( float theta_x,
float theta_y,
float theta_z )
inline

Build a quaternion from Euler angles in ZYX order, with angles in degrees.

Definition at line 1120 of file mxvk_math_eigen.hpp.

1120 {
1121 const float hx = deg2rad(theta_x) * 0.5f;
1122 const float hy = deg2rad(theta_y) * 0.5f;
1123 const float hz = deg2rad(theta_z) * 0.5f;
1124 const float cx = std::cos(hx);
1125 const float sx = std::sin(hx);
1126 const float cy = std::cos(hy);
1127 const float sy = std::sin(hy);
1128 const float cz = std::cos(hz);
1129 const float sz = std::sin(hz);
1130 w = cz * cy * cx + sz * sy * sx;
1131 x = cz * cy * sx - sz * sy * cx;
1132 y = cz * sy * cx + sz * cy * sx;
1133 z = sz * cy * cx - cz * sy * sx;
1134 }

◆ Inverse() [1/2]

void mxvk::QuatType::Inverse ( )
inline

Invert this quaternion in place.

Definition at line 1116 of file mxvk_math.h.

1116 {
1117 const float n2 = Norm2();
1118 if (n2 <= EPSILON) {
1119 x = y = z = 0.0f;
1120 w = 1.0f;
1121 return;
1122 }
1123 x = -x / n2;
1124 y = -y / n2;
1125 z = -z / n2;
1126 w = w / n2;
1127 }
float Norm2() const
Compute the squared norm.
Definition mxvk_math.h:1099
constexpr float EPSILON
Default tolerance used for floating-point singularity and zero-length checks.
Definition mxvk_math.h:37

◆ Inverse() [2/2]

void mxvk::QuatType::Inverse ( )
inline

Invert this quaternion in place.

Definition at line 1082 of file mxvk_math_eigen.hpp.

1082 {
1083 const float n2 = Norm2();
1084 if (n2 <= EPSILON) {
1085 x = y = z = 0.0f;
1086 w = 1.0f;
1087 return;
1088 }
1089 x = -x / n2;
1090 y = -y / n2;
1091 z = -z / n2;
1092 w = w / n2;
1093 }

◆ InverseNormal() [1/2]

void mxvk::QuatType::InverseNormal ( )
inline

Invert a unit quaternion by conjugating it.

Definition at line 1130 of file mxvk_math.h.

1130{ Conj(); }
void Conj()
Conjugate this quaternion in place.
Definition mxvk_math.h:1084

◆ InverseNormal() [2/2]

void mxvk::QuatType::InverseNormal ( )
inline

Invert a unit quaternion by conjugating it.

Definition at line 1096 of file mxvk_math_eigen.hpp.

1096{ Conj(); }

◆ Norm() [1/2]

float mxvk::QuatType::Norm ( ) const
inlinenodiscard

Compute the norm.

Definition at line 1102 of file mxvk_math.h.

1102{ return std::sqrt(Norm2()); }

◆ Norm() [2/2]

float mxvk::QuatType::Norm ( ) const
inlinenodiscard

Compute the norm.

Definition at line 1068 of file mxvk_math_eigen.hpp.

1068{ return std::sqrt(Norm2()); }

◆ Norm2() [1/2]

float mxvk::QuatType::Norm2 ( ) const
inlinenodiscard

Compute the squared norm.

Definition at line 1099 of file mxvk_math.h.

1099{ return w * w + x * x + y * y + z * z; }

◆ Norm2() [2/2]

float mxvk::QuatType::Norm2 ( ) const
inlinenodiscard

Compute the squared norm.

Definition at line 1065 of file mxvk_math_eigen.hpp.

1065{ return w * w + x * x + y * y + z * z; }

◆ Normalize() [1/2]

void mxvk::QuatType::Normalize ( )
inline

Normalize this quaternion in place, or reset it to identity if it is too small.

Definition at line 1105 of file mxvk_math.h.

1105 {
1106 const float norm = Norm();
1107 if (norm <= EPSILON) {
1108 x = y = z = 0.0f;
1109 w = 1.0f;
1110 return;
1111 }
1112 Scale(1.0f / norm);
1113 }
float Norm() const
Compute the norm.
Definition mxvk_math.h:1102
void Scale(float f)
Scale all quaternion components in place.
Definition mxvk_math.h:1091

◆ Normalize() [2/2]

void mxvk::QuatType::Normalize ( )
inline

Normalize this quaternion in place, or reset it to identity if it is too small.

Definition at line 1071 of file mxvk_math_eigen.hpp.

1071 {
1072 const float norm = Norm();
1073 if (norm <= EPSILON) {
1074 x = y = z = 0.0f;
1075 w = 1.0f;
1076 return;
1077 }
1078 Scale(1.0f / norm);
1079 }

◆ operator*() [1/2]

QuatType mxvk::QuatType::operator* ( const QuatType & q) const
inlinenodiscardconstexpr

Multiply two quaternions.

Definition at line 1075 of file mxvk_math.h.

1075{ return {w * q.x + x * q.w + y * q.z - z * q.y, w * q.y - x * q.z + y * q.w + z * q.x, w * q.z + x * q.y - y * q.x + z * q.w, w * q.w - x * q.x - y * q.y - z * q.z}; }

◆ operator*() [2/2]

QuatType mxvk::QuatType::operator* ( const QuatType & q) const
inlinenodiscardconstexpr

Multiply two quaternions.

Definition at line 1041 of file mxvk_math_eigen.hpp.

1041{ return {w * q.x + x * q.w + y * q.z - z * q.y, w * q.y - x * q.z + y * q.w + z * q.x, w * q.z + x * q.y - y * q.x + z * q.w, w * q.w - x * q.x - y * q.y - z * q.z}; }

◆ operator*=() [1/2]

QuatType & mxvk::QuatType::operator*= ( const QuatType & q)
inline

Multiply this quaternion by another quaternion in place.

Definition at line 1078 of file mxvk_math.h.

1078 {
1079 *this = *this * q;
1080 return *this;
1081 }

◆ operator*=() [2/2]

QuatType & mxvk::QuatType::operator*= ( const QuatType & q)
inline

Multiply this quaternion by another quaternion in place.

Definition at line 1044 of file mxvk_math_eigen.hpp.

1044 {
1045 *this = *this * q;
1046 return *this;
1047 }

◆ operator+() [1/2]

QuatType mxvk::QuatType::operator+ ( const QuatType & q) const
inlinenodiscardconstexpr

Add two quaternions component-wise.

Definition at line 1069 of file mxvk_math.h.

1069{ return {x + q.x, y + q.y, z + q.z, w + q.w}; }

◆ operator+() [2/2]

QuatType mxvk::QuatType::operator+ ( const QuatType & q) const
inlinenodiscardconstexpr

Add two quaternions component-wise.

Definition at line 1035 of file mxvk_math_eigen.hpp.

1035{ return {x + q.x, y + q.y, z + q.z, w + q.w}; }

◆ operator-() [1/2]

QuatType mxvk::QuatType::operator- ( const QuatType & q) const
inlinenodiscardconstexpr

Subtract two quaternions component-wise.

Definition at line 1072 of file mxvk_math.h.

1072{ return {x - q.x, y - q.y, z - q.z, w - q.w}; }

◆ operator-() [2/2]

QuatType mxvk::QuatType::operator- ( const QuatType & q) const
inlinenodiscardconstexpr

Subtract two quaternions component-wise.

Definition at line 1038 of file mxvk_math_eigen.hpp.

1038{ return {x - q.x, y - q.y, z - q.z, w - q.w}; }

◆ QuatToVec3D() [1/2]

void mxvk::QuatType::QuatToVec3D ( float * theta_degrees,
vec3D & axis ) const
inline

Convert this quaternion to axis-angle form.

Definition at line 1171 of file mxvk_math.h.

1171 {
1172 QuatType q = *this;
1173 q.Normalize();
1174 const float s = std::sqrt(std::max(0.0f, 1.0f - q.w * q.w));
1175 if (s <= EPSILON) {
1176 axis.Set(1.0f, 0.0f, 0.0f);
1177 } else {
1178 axis.Set(q.x / s, q.y / s, q.z / s);
1179 }
1180 if (theta_degrees != nullptr) {
1181 *theta_degrees = rad2deg(2.0f * std::acos(std::clamp(q.w, -1.0f, 1.0f)));
1182 }
1183 }
constexpr QuatType()
Construct the identity quaternion.
Definition mxvk_math.h:1063
float rad2deg(float rad)
Convert radians to degrees.
Definition mxvk_math.h:112

◆ QuatToVec3D() [2/2]

void mxvk::QuatType::QuatToVec3D ( float * theta_degrees,
vec3D & axis ) const
inline

Convert this quaternion to axis-angle form.

Definition at line 1137 of file mxvk_math_eigen.hpp.

1137 {
1138 QuatType q = *this;
1139 q.Normalize();
1140 const float s = std::sqrt(std::max(0.0f, 1.0f - q.w * q.w));
1141 if (s <= EPSILON) {
1142 axis.Set(1.0f, 0.0f, 0.0f);
1143 } else {
1144 axis.Set(q.x / s, q.y / s, q.z / s);
1145 }
1146 if (theta_degrees != nullptr) {
1147 *theta_degrees = rad2deg(2.0f * std::acos(std::clamp(q.w, -1.0f, 1.0f)));
1148 }
1149 }

◆ Scale() [1/2]

void mxvk::QuatType::Scale ( float f)
inline

Scale all quaternion components in place.

Definition at line 1091 of file mxvk_math.h.

1091 {
1092 x *= f;
1093 y *= f;
1094 z *= f;
1095 w *= f;
1096 }

◆ Scale() [2/2]

void mxvk::QuatType::Scale ( float f)
inline

Scale all quaternion components in place.

Definition at line 1057 of file mxvk_math_eigen.hpp.

1057 {
1058 x *= f;
1059 y *= f;
1060 z *= f;
1061 w *= f;
1062 }

◆ TripleProduct() [1/2]

QuatType mxvk::QuatType::TripleProduct ( const QuatType & p1,
const QuatType & p2 ) const
inlinenodiscard

Return the quaternion product (*this * p1) * p2.

Definition at line 1133 of file mxvk_math.h.

1133{ return (*this * p1) * p2; }

◆ TripleProduct() [2/2]

QuatType mxvk::QuatType::TripleProduct ( const QuatType & p1,
const QuatType & p2 ) const
inlinenodiscard

Return the quaternion product (*this * p1) * p2.

Definition at line 1099 of file mxvk_math_eigen.hpp.

1099{ return (*this * p1) * p2; }

◆ vec3DthetaQuat() [1/2]

void mxvk::QuatType::vec3DthetaQuat ( float theta_degrees,
const vec3D & axis )
inline

Build a quaternion from an axis and angle in degrees.

Definition at line 1136 of file mxvk_math.h.

1136 {
1137 vec3D n = axis;
1138 n.Normalize();
1139 const float half = deg2rad(theta_degrees) * 0.5f;
1140 const float s = std::sin(half);
1141 x = s * n.x;
1142 y = s * n.y;
1143 z = s * n.z;
1144 w = std::cos(half);
1145 }

◆ vec3DthetaQuat() [2/2]

void mxvk::QuatType::vec3DthetaQuat ( float theta_degrees,
const vec3D & axis )
inline

Build a quaternion from an axis and angle in degrees.

Definition at line 1102 of file mxvk_math_eigen.hpp.

1102 {
1103 vec3D n = axis;
1104 n.Normalize();
1105 const float half = deg2rad(theta_degrees) * 0.5f;
1106 const float s = std::sin(half);
1107 x = s * n.x;
1108 y = s * n.y;
1109 z = s * n.z;
1110 w = std::cos(half);
1111 }

◆ vec4DthetaQuat() [1/2]

void mxvk::QuatType::vec4DthetaQuat ( float theta_degrees,
const vec4D & axis )
inline

Build a quaternion from a 4D axis vector and angle in degrees.

Definition at line 1148 of file mxvk_math.h.

1148 {
1149 vec3D n(axis.x, axis.y, axis.z);
1150 vec3DthetaQuat(theta_degrees, n);
1151 }
void vec3DthetaQuat(float theta_degrees, const vec3D &axis)
Build a quaternion from an axis and angle in degrees.
Definition mxvk_math.h:1136

◆ vec4DthetaQuat() [2/2]

void mxvk::QuatType::vec4DthetaQuat ( float theta_degrees,
const vec4D & axis )
inline

Build a quaternion from a 4D axis vector and angle in degrees.

Definition at line 1114 of file mxvk_math_eigen.hpp.

1114 {
1115 vec3D n(axis.x, axis.y, axis.z);
1116 vec3DthetaQuat(theta_degrees, n);
1117 }

Member Data Documentation

◆ w

float mxvk::QuatType::w = 1.0f

Scalar component.

Definition at line 1060 of file mxvk_math.h.

◆ x

float mxvk::QuatType::x = 0.0f

X component of the vector part.

Definition at line 1051 of file mxvk_math.h.

◆ y

float mxvk::QuatType::y = 0.0f

Y component of the vector part.

Definition at line 1054 of file mxvk_math.h.

◆ z

float mxvk::QuatType::z = 0.0f

Z component of the vector part.

Definition at line 1057 of file mxvk_math.h.


The documentation for this class was generated from the following files: