MXVK Vulkan Framework 0.24.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.
QuatTypeoperator*= (const QuatType &q)
 Multiply this quaternion by another quaternion in place.
QuatTypeoperator*= (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 1170 of file mxvk_math.h.

Constructor & Destructor Documentation

◆ QuatType() [1/4]

mxvk::QuatType::QuatType ( )
inlineconstexpr

Construct the identity quaternion.

Definition at line 1185 of file mxvk_math.h.

1185: x(0.0f), y(0.0f), z(0.0f), w(1.0f) {}
float w
Scalar component.
Definition mxvk_math.h:1182
float z
Z component of the vector part.
Definition mxvk_math.h:1179
float x
X component of the vector part.
Definition mxvk_math.h:1173
float y
Y component of the vector part.
Definition mxvk_math.h:1176

◆ 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 1188 of file mxvk_math.h.

1188: 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 1185 of file mxvk_math_eigen.hpp.

1185: 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 1188 of file mxvk_math_eigen.hpp.

1188: 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 1215 of file mxvk_math.h.

1215 {
1216 x = -x;
1217 y = -y;
1218 z = -z;
1219 }

◆ Conj() [2/2]

void mxvk::QuatType::Conj ( )
inline

Conjugate this quaternion in place.

Definition at line 1215 of file mxvk_math_eigen.hpp.

1215 {
1216 x = -x;
1217 y = -y;
1218 z = -z;
1219 }

◆ 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 1293 of file mxvk_math.h.

1293 {
1294 const float hx = deg2rad(theta_x) * 0.5f;
1295 const float hy = deg2rad(theta_y) * 0.5f;
1296 const float hz = deg2rad(theta_z) * 0.5f;
1297 const float cx = std::cos(hx);
1298 const float sx = std::sin(hx);
1299 const float cy = std::cos(hy);
1300 const float sy = std::sin(hy);
1301 const float cz = std::cos(hz);
1302 const float sz = std::sin(hz);
1303 w = cz * cy * cx + sz * sy * sx;
1304 x = cz * cy * sx - sz * sy * cx;
1305 y = cz * sy * cx + sz * cy * sx;
1306 z = sz * cy * cx - cz * sy * sx;
1307 }
float deg2rad(float ang)
Convert degrees to radians.
Definition mxvk_math.h:124

◆ 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 1293 of file mxvk_math_eigen.hpp.

1293 {
1294 const float hx = deg2rad(theta_x) * 0.5f;
1295 const float hy = deg2rad(theta_y) * 0.5f;
1296 const float hz = deg2rad(theta_z) * 0.5f;
1297 const float cx = std::cos(hx);
1298 const float sx = std::sin(hx);
1299 const float cy = std::cos(hy);
1300 const float sy = std::sin(hy);
1301 const float cz = std::cos(hz);
1302 const float sz = std::sin(hz);
1303 w = cz * cy * cx + sz * sy * sx;
1304 x = cz * cy * sx - sz * sy * cx;
1305 y = cz * sy * cx + sz * cy * sx;
1306 z = sz * cy * cx - cz * sy * sx;
1307 }

◆ Inverse() [1/2]

void mxvk::QuatType::Inverse ( )
inline

Invert this quaternion in place.

Definition at line 1251 of file mxvk_math.h.

1251 {
1252 const float n2 = Norm2();
1253 if (n2 <= EPSILON) {
1254 x = y = z = 0.0f;
1255 w = 1.0f;
1256 return;
1257 }
1258 x = -x / n2;
1259 y = -y / n2;
1260 z = -z / n2;
1261 w = w / n2;
1262 }
float Norm2() const
Compute the squared norm.
Definition mxvk_math.h:1230
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 1251 of file mxvk_math_eigen.hpp.

1251 {
1252 const float n2 = Norm2();
1253 if (n2 <= EPSILON) {
1254 x = y = z = 0.0f;
1255 w = 1.0f;
1256 return;
1257 }
1258 x = -x / n2;
1259 y = -y / n2;
1260 z = -z / n2;
1261 w = w / n2;
1262 }

◆ InverseNormal() [1/2]

void mxvk::QuatType::InverseNormal ( )
inline

Invert a unit quaternion by conjugating it.

Definition at line 1265 of file mxvk_math.h.

1265 {
1266 Conj();
1267 }
void Conj()
Conjugate this quaternion in place.
Definition mxvk_math.h:1215

◆ InverseNormal() [2/2]

void mxvk::QuatType::InverseNormal ( )
inline

Invert a unit quaternion by conjugating it.

Definition at line 1265 of file mxvk_math_eigen.hpp.

1265 {
1266 Conj();
1267 }

◆ Norm() [1/2]

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

Compute the norm.

Definition at line 1235 of file mxvk_math.h.

1235 {
1236 return std::sqrt(Norm2());
1237 }

◆ Norm() [2/2]

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

Compute the norm.

Definition at line 1235 of file mxvk_math_eigen.hpp.

1235 {
1236 return std::sqrt(Norm2());
1237 }

◆ Norm2() [1/2]

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

Compute the squared norm.

Definition at line 1230 of file mxvk_math.h.

1230 {
1231 return w * w + x * x + y * y + z * z;
1232 }

◆ Norm2() [2/2]

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

Compute the squared norm.

Definition at line 1230 of file mxvk_math_eigen.hpp.

1230 {
1231 return w * w + x * x + y * y + z * z;
1232 }

◆ 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 1240 of file mxvk_math.h.

1240 {
1241 const float norm = Norm();
1242 if (norm <= EPSILON) {
1243 x = y = z = 0.0f;
1244 w = 1.0f;
1245 return;
1246 }
1247 Scale(1.0f / norm);
1248 }
float Norm() const
Compute the norm.
Definition mxvk_math.h:1235
void Scale(float f)
Scale all quaternion components in place.
Definition mxvk_math.h:1222

◆ 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 1240 of file mxvk_math_eigen.hpp.

1240 {
1241 const float norm = Norm();
1242 if (norm <= EPSILON) {
1243 x = y = z = 0.0f;
1244 w = 1.0f;
1245 return;
1246 }
1247 Scale(1.0f / norm);
1248 }

◆ operator*() [1/2]

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

Multiply two quaternions.

Definition at line 1201 of file mxvk_math.h.

1201 {
1202 return {w * q.x + x * q.w + y * q.z - z * q.y,
1203 w * q.y - x * q.z + y * q.w + z * q.x,
1204 w * q.z + x * q.y - y * q.x + z * q.w,
1205 w * q.w - x * q.x - y * q.y - z * q.z};
1206 }

◆ operator*() [2/2]

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

Multiply two quaternions.

Definition at line 1201 of file mxvk_math_eigen.hpp.

1201 {
1202 return {w * q.x + x * q.w + y * q.z - z * q.y,
1203 w * q.y - x * q.z + y * q.w + z * q.x,
1204 w * q.z + x * q.y - y * q.x + z * q.w,
1205 w * q.w - x * q.x - y * q.y - z * q.z};
1206 }

◆ operator*=() [1/2]

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

Multiply this quaternion by another quaternion in place.

Definition at line 1209 of file mxvk_math.h.

1209 {
1210 *this = *this * q;
1211 return *this;
1212 }

◆ operator*=() [2/2]

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

Multiply this quaternion by another quaternion in place.

Definition at line 1209 of file mxvk_math_eigen.hpp.

1209 {
1210 *this = *this * q;
1211 return *this;
1212 }

◆ operator+() [1/2]

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

Add two quaternions component-wise.

Definition at line 1191 of file mxvk_math.h.

1191 {
1192 return {x + q.x, y + q.y, z + q.z, w + q.w};
1193 }

◆ operator+() [2/2]

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

Add two quaternions component-wise.

Definition at line 1191 of file mxvk_math_eigen.hpp.

1191 {
1192 return {x + q.x, y + q.y, z + q.z, w + q.w};
1193 }

◆ operator-() [1/2]

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

Subtract two quaternions component-wise.

Definition at line 1196 of file mxvk_math.h.

1196 {
1197 return {x - q.x, y - q.y, z - q.z, w - q.w};
1198 }

◆ operator-() [2/2]

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

Subtract two quaternions component-wise.

Definition at line 1196 of file mxvk_math_eigen.hpp.

1196 {
1197 return {x - q.x, y - q.y, z - q.z, w - q.w};
1198 }

◆ QuatToVec3D() [1/2]

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

Convert this quaternion to axis-angle form.

Definition at line 1310 of file mxvk_math.h.

1310 {
1311 QuatType q = *this;
1312 q.Normalize();
1313 const float s = std::sqrt(std::max(0.0f, 1.0f - q.w * q.w));
1314 if (s <= EPSILON) {
1315 axis.Set(1.0f, 0.0f, 0.0f);
1316 } else {
1317 axis.Set(q.x / s, q.y / s, q.z / s);
1318 }
1319 if (theta_degrees != nullptr) {
1320 *theta_degrees = rad2deg(2.0f * std::acos(std::clamp(q.w, -1.0f, 1.0f)));
1321 }
1322 }
constexpr QuatType()
Construct the identity quaternion.
Definition mxvk_math.h:1185
float rad2deg(float rad)
Convert radians to degrees.
Definition mxvk_math.h:129

◆ QuatToVec3D() [2/2]

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

Convert this quaternion to axis-angle form.

Definition at line 1310 of file mxvk_math_eigen.hpp.

1310 {
1311 QuatType q = *this;
1312 q.Normalize();
1313 const float s = std::sqrt(std::max(0.0f, 1.0f - q.w * q.w));
1314 if (s <= EPSILON) {
1315 axis.Set(1.0f, 0.0f, 0.0f);
1316 } else {
1317 axis.Set(q.x / s, q.y / s, q.z / s);
1318 }
1319 if (theta_degrees != nullptr) {
1320 *theta_degrees = rad2deg(2.0f * std::acos(std::clamp(q.w, -1.0f, 1.0f)));
1321 }
1322 }

◆ Scale() [1/2]

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

Scale all quaternion components in place.

Definition at line 1222 of file mxvk_math.h.

1222 {
1223 x *= f;
1224 y *= f;
1225 z *= f;
1226 w *= f;
1227 }

◆ Scale() [2/2]

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

Scale all quaternion components in place.

Definition at line 1222 of file mxvk_math_eigen.hpp.

1222 {
1223 x *= f;
1224 y *= f;
1225 z *= f;
1226 w *= f;
1227 }

◆ 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 1270 of file mxvk_math.h.

1270 {
1271 return (*this * p1) * p2;
1272 }

◆ 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 1270 of file mxvk_math_eigen.hpp.

1270 {
1271 return (*this * p1) * p2;
1272 }

◆ 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 1275 of file mxvk_math.h.

1275 {
1276 vec3D n = axis;
1277 n.Normalize();
1278 const float half = deg2rad(theta_degrees) * 0.5f;
1279 const float s = std::sin(half);
1280 x = s * n.x;
1281 y = s * n.y;
1282 z = s * n.z;
1283 w = std::cos(half);
1284 }

◆ 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 1275 of file mxvk_math_eigen.hpp.

1275 {
1276 vec3D n = axis;
1277 n.Normalize();
1278 const float half = deg2rad(theta_degrees) * 0.5f;
1279 const float s = std::sin(half);
1280 x = s * n.x;
1281 y = s * n.y;
1282 z = s * n.z;
1283 w = std::cos(half);
1284 }

◆ 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 1287 of file mxvk_math.h.

1287 {
1288 vec3D n(axis.x, axis.y, axis.z);
1289 vec3DthetaQuat(theta_degrees, n);
1290 }
void vec3DthetaQuat(float theta_degrees, const vec3D &axis)
Build a quaternion from an axis and angle in degrees.
Definition mxvk_math.h:1275

◆ 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 1287 of file mxvk_math_eigen.hpp.

1287 {
1288 vec3D n(axis.x, axis.y, axis.z);
1289 vec3DthetaQuat(theta_degrees, n);
1290 }

Member Data Documentation

◆ w

float mxvk::QuatType::w = 1.0f

Scalar component.

Definition at line 1182 of file mxvk_math.h.

◆ x

float mxvk::QuatType::x = 0.0f

X component of the vector part.

Definition at line 1173 of file mxvk_math.h.

◆ y

float mxvk::QuatType::y = 0.0f

Y component of the vector part.

Definition at line 1176 of file mxvk_math.h.

◆ z

float mxvk::QuatType::z = 0.0f

Z component of the vector part.

Definition at line 1179 of file mxvk_math.h.


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