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::Mat3D Class Reference

Three-by-three matrix with multiplication, vector transform, inverse, and solve helpers. More...

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

Public Member Functions

float Determinate () const
 Compute the matrix determinant.
float Determinate () const
 Compute the matrix determinant.
bool Inverse (Mat3D &out) const
 Compute the inverse matrix.
bool Inverse (Mat3D &out) const
 Compute the inverse matrix.
void LoadIdentity ()
 Set this matrix to the identity matrix.
void LoadIdentity ()
 Set this matrix to the identity matrix.
 Mat3D ()=default
 Construct a zero-initialized matrix.
 Mat3D ()=default
 Construct a zero-initialized matrix.
 Mat3D (float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
 Construct from explicit row-major elements.
 Mat3D (float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
 Construct from explicit row-major elements.
vec3D MulVec (const vec3D &in) const
 Transform a 3D vector by this matrix.
vec3D MulVec (const vec3D &in) const
 Transform a 3D vector by this matrix.
void MulVec (const vec3D &in, vec3D &out) const
 Transform a 3D vector and write the result to out.
void MulVec (const vec3D &in, vec3D &out) const
 Transform a 3D vector and write the result to out.
Mat3D operator* (const Mat3D &m) const
 Multiply two 3x3 matrices.
Mat3D operator* (const Mat3D &m) const
 Multiply two 3x3 matrices.
void Set (float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
 Set all matrix elements in row-major order.
void Set (float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
 Set all matrix elements in row-major order.
bool Solve3x3 (const Mat3D &a, Mat1x3D &out, const Mat1x3D &b) const
 Solve a 3x3 linear system.

Static Public Member Functions

static bool Solve3x3 (const Mat3D &a, Mat1x3D &out, const Mat1x3D &b)
 Solve a 3x3 linear system.

Public Attributes

float mat [3][3] {}
 Matrix elements indexed as row, column.

Detailed Description

Three-by-three matrix with multiplication, vector transform, inverse, and solve helpers.

Definition at line 707 of file mxvk_math.h.

Constructor & Destructor Documentation

◆ Mat3D() [1/4]

mxvk::Mat3D::Mat3D ( )
default

Construct a zero-initialized matrix.

◆ Mat3D() [2/4]

mxvk::Mat3D::Mat3D ( float m00,
float m01,
float m02,
float m10,
float m11,
float m12,
float m20,
float m21,
float m22 )
inline

Construct from explicit row-major elements.

Definition at line 716 of file mxvk_math.h.

716 {
717 Set(m00, m01, m02, m10, m11, m12, m20, m21, m22);
718 }
void Set(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
Set all matrix elements in row-major order.
Definition mxvk_math.h:721

◆ Mat3D() [3/4]

mxvk::Mat3D::Mat3D ( )
default

Construct a zero-initialized matrix.

◆ Mat3D() [4/4]

mxvk::Mat3D::Mat3D ( float m00,
float m01,
float m02,
float m10,
float m11,
float m12,
float m20,
float m21,
float m22 )
inline

Construct from explicit row-major elements.

Definition at line 763 of file mxvk_math_eigen.hpp.

763 {
764 Set(m00, m01, m02, m10, m11, m12, m20, m21, m22);
765 }

Member Function Documentation

◆ Determinate() [1/2]

float mxvk::Mat3D::Determinate ( ) const
inlinenodiscard

Compute the matrix determinant.

Definition at line 764 of file mxvk_math.h.

764 {
765 return mat[0][0] * (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]) - mat[0][1] * (mat[1][0] * mat[2][2] - mat[1][2] * mat[2][0]) + mat[0][2] * (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]);
766 }
float mat[3][3]
Matrix elements indexed as row, column.
Definition mxvk_math.h:710

◆ Determinate() [2/2]

float mxvk::Mat3D::Determinate ( ) const
inlinenodiscard

Compute the matrix determinant.

Definition at line 794 of file mxvk_math_eigen.hpp.

794 {
795 return ToEigen().determinant();
796 }

◆ Inverse() [1/2]

bool mxvk::Mat3D::Inverse ( Mat3D & out) const
inline

Compute the inverse matrix.

Parameters
outReceives the inverse on success.
Returns
True when the matrix is invertible.

Definition at line 773 of file mxvk_math.h.

773 {
774 const float d = Determinate();
775 if (std::fabs(d) <= EPSILON) {
776 return false;
777 }
778 const float inv = 1.0f / d;
779 out.mat[0][0] = (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]) * inv;
780 out.mat[0][1] = (mat[0][2] * mat[2][1] - mat[0][1] * mat[2][2]) * inv;
781 out.mat[0][2] = (mat[0][1] * mat[1][2] - mat[0][2] * mat[1][1]) * inv;
782 out.mat[1][0] = (mat[1][2] * mat[2][0] - mat[1][0] * mat[2][2]) * inv;
783 out.mat[1][1] = (mat[0][0] * mat[2][2] - mat[0][2] * mat[2][0]) * inv;
784 out.mat[1][2] = (mat[0][2] * mat[1][0] - mat[0][0] * mat[1][2]) * inv;
785 out.mat[2][0] = (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]) * inv;
786 out.mat[2][1] = (mat[0][1] * mat[2][0] - mat[0][0] * mat[2][1]) * inv;
787 out.mat[2][2] = (mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0]) * inv;
788 return true;
789 }
float Determinate() const
Compute the matrix determinant.
Definition mxvk_math.h:764
constexpr float EPSILON
Default tolerance used for floating-point singularity and zero-length checks.
Definition mxvk_math.h:37

◆ Inverse() [2/2]

bool mxvk::Mat3D::Inverse ( Mat3D & out) const
inline

Compute the inverse matrix.

Parameters
outReceives the inverse on success.
Returns
True when the matrix is invertible.

Definition at line 803 of file mxvk_math_eigen.hpp.

803 {
804 const float d = Determinate();
805 if (std::fabs(d) <= EPSILON) {
806 return false;
807 }
808 out = FromEigen(ToEigen().inverse());
809 return true;
810 }

◆ LoadIdentity() [1/2]

void mxvk::Mat3D::LoadIdentity ( )
inline

Set this matrix to the identity matrix.

Definition at line 734 of file mxvk_math.h.

734 {
735 Set(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f);
736 }

◆ LoadIdentity() [2/2]

void mxvk::Mat3D::LoadIdentity ( )
inline

Set this matrix to the identity matrix.

Definition at line 773 of file mxvk_math_eigen.hpp.

773 {
774 ToEigen().setIdentity();
775 }

◆ MulVec() [1/4]

vec3D mxvk::Mat3D::MulVec ( const vec3D & in) const
inlinenodiscard

Transform a 3D vector by this matrix.

Definition at line 752 of file mxvk_math.h.

752 {
753 return {in.x * mat[0][0] + in.y * mat[1][0] + in.z * mat[2][0],
754 in.x * mat[0][1] + in.y * mat[1][1] + in.z * mat[2][1],
755 in.x * mat[0][2] + in.y * mat[1][2] + in.z * mat[2][2]};
756 }

◆ MulVec() [2/4]

vec3D mxvk::Mat3D::MulVec ( const vec3D & in) const
inlinenodiscard

Transform a 3D vector by this matrix.

Definition at line 783 of file mxvk_math_eigen.hpp.

783 {
784 const Eigen::Vector3f result = ToEigen().transpose() * Eigen::Vector3f(in.x, in.y, in.z);
785 return {result.x(), result.y(), result.z()};
786 }

◆ MulVec() [3/4]

void mxvk::Mat3D::MulVec ( const vec3D & in,
vec3D & out ) const
inline

Transform a 3D vector and write the result to out.

Definition at line 759 of file mxvk_math.h.

759 {
760 out = MulVec(in);
761 }
vec3D MulVec(const vec3D &in) const
Transform a 3D vector by this matrix.
Definition mxvk_math.h:752

◆ MulVec() [4/4]

void mxvk::Mat3D::MulVec ( const vec3D & in,
vec3D & out ) const
inline

Transform a 3D vector and write the result to out.

Definition at line 789 of file mxvk_math_eigen.hpp.

789 {
790 out = MulVec(in);
791 }

◆ operator*() [1/2]

Mat3D mxvk::Mat3D::operator* ( const Mat3D & m) const
inlinenodiscard

Multiply two 3x3 matrices.

Definition at line 739 of file mxvk_math.h.

739 {
740 Mat3D out;
741 for (int r = 0; r < 3; ++r) {
742 for (int c = 0; c < 3; ++c) {
743 for (int k = 0; k < 3; ++k) {
744 out.mat[r][c] += mat[r][k] * m.mat[k][c];
745 }
746 }
747 }
748 return out;
749 }
Mat3D()=default
Construct a zero-initialized matrix.

◆ operator*() [2/2]

Mat3D mxvk::Mat3D::operator* ( const Mat3D & m) const
inlinenodiscard

Multiply two 3x3 matrices.

Definition at line 778 of file mxvk_math_eigen.hpp.

778 {
779 return FromEigen(ToEigen() * m.ToEigen());
780 }

◆ Set() [1/2]

void mxvk::Mat3D::Set ( float m00,
float m01,
float m02,
float m10,
float m11,
float m12,
float m20,
float m21,
float m22 )
inline

Set all matrix elements in row-major order.

Definition at line 721 of file mxvk_math.h.

721 {
722 mat[0][0] = m00;
723 mat[0][1] = m01;
724 mat[0][2] = m02;
725 mat[1][0] = m10;
726 mat[1][1] = m11;
727 mat[1][2] = m12;
728 mat[2][0] = m20;
729 mat[2][1] = m21;
730 mat[2][2] = m22;
731 }

◆ Set() [2/2]

void mxvk::Mat3D::Set ( float m00,
float m01,
float m02,
float m10,
float m11,
float m12,
float m20,
float m21,
float m22 )
inline

Set all matrix elements in row-major order.

Definition at line 768 of file mxvk_math_eigen.hpp.

768 {
769 ToEigen() << m00, m01, m02, m10, m11, m12, m20, m21, m22;
770 }

◆ Solve3x3() [1/2]

bool mxvk::Mat3D::Solve3x3 ( const Mat3D & a,
Mat1x3D & out,
const Mat1x3D & b )
inlinestatic

Solve a 3x3 linear system.

Parameters
aCoefficient matrix.
outReceives the solution vector.
bRight-hand-side vector.
Returns
True when the system has a unique solution.

Definition at line 819 of file mxvk_math_eigen.hpp.

819 {
820 if (std::fabs(a.Determinate()) <= EPSILON) {
821 return false;
822 }
823 const Eigen::Vector3f rhs(b.mat[0], b.mat[1], b.mat[2]);
824 const Eigen::Vector3f result = a.ToEigen().transpose().partialPivLu().solve(rhs);
825 Eigen::Map<Eigen::Vector3f>(out.mat) = result;
826 return true;
827 }

◆ Solve3x3() [2/2]

bool mxvk::Mat3D::Solve3x3 ( const Mat3D & a,
Mat1x3D & out,
const Mat1x3D & b ) const
inline

Solve a 3x3 linear system.

Parameters
aCoefficient matrix.
outReceives the solution vector.
bRight-hand-side vector.
Returns
True when the system has a unique solution.

Definition at line 798 of file mxvk_math.h.

798 {
799 Mat3D inv;
800 if (!a.Inverse(inv)) {
801 return false;
802 }
803 const vec3D r = inv.MulVec({b.mat[0], b.mat[1], b.mat[2]});
804 out.mat[0] = r.x;
805 out.mat[1] = r.y;
806 out.mat[2] = r.z;
807 return true;
808 }

Member Data Documentation

◆ mat

float mxvk::Mat3D::mat {}

Matrix elements indexed as row, column.

Definition at line 710 of file mxvk_math.h.

710{};

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