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

Two-by-two matrix with arithmetic, determinant, 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 (Mat2D &out) const
 Compute the inverse matrix.
bool Inverse (Mat2D &out) const
 Compute the inverse matrix.
void LoadIdentity ()
 Set this matrix to the identity matrix.
void LoadIdentity ()
 Set this matrix to the identity matrix.
 Mat2D ()=default
 Construct a zero-initialized matrix.
 Mat2D ()=default
 Construct a zero-initialized matrix.
 Mat2D (float m00, float m01, float m10, float m11)
 Construct from explicit row-major elements.
 Mat2D (float m00, float m01, float m10, float m11)
 Construct from explicit row-major elements.
Mat2D operator* (const Mat2D &m) const
 Multiply two 2x2 matrices.
Mat2D operator* (const Mat2D &m) const
 Multiply two 2x2 matrices.
Mat2D operator+ (const Mat2D &m) const
 Add two matrices component-wise.
Mat2D operator+ (const Mat2D &m) const
 Add two matrices component-wise.
Mat2D operator- (const Mat2D &m) const
 Subtract two matrices component-wise.
Mat2D operator- (const Mat2D &m) const
 Subtract two matrices component-wise.
void Set (float m00, float m01, float m10, float m11)
 Set all matrix elements in row-major order.
void Set (float m00, float m01, float m10, float m11)
 Set all matrix elements in row-major order.
bool Solve2x2 (const Mat2D &a, Mat1D &out, const Mat1D &b) const
 Solve a 2x2 linear system.

Static Public Member Functions

static bool Solve2x2 (const Mat2D &a, Mat1D &out, const Mat1D &b)
 Solve a 2x2 linear system.

Public Attributes

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

Detailed Description

Two-by-two matrix with arithmetic, determinant, inverse, and solve helpers.

Definition at line 624 of file mxvk_math.h.

Constructor & Destructor Documentation

◆ Mat2D() [1/4]

mxvk::Mat2D::Mat2D ( )
default

Construct a zero-initialized matrix.

◆ Mat2D() [2/4]

mxvk::Mat2D::Mat2D ( float m00,
float m01,
float m10,
float m11 )
inline

Construct from explicit row-major elements.

Definition at line 633 of file mxvk_math.h.

633 {
634 Set(m00, m01, m10, m11);
635 }
void Set(float m00, float m01, float m10, float m11)
Set all matrix elements in row-major order.
Definition mxvk_math.h:638

◆ Mat2D() [3/4]

mxvk::Mat2D::Mat2D ( )
default

Construct a zero-initialized matrix.

◆ Mat2D() [4/4]

mxvk::Mat2D::Mat2D ( float m00,
float m01,
float m10,
float m11 )
inline

Construct from explicit row-major elements.

Definition at line 668 of file mxvk_math_eigen.hpp.

668 {
669 Set(m00, m01, m10, m11);
670 }

Member Function Documentation

◆ Determinate() [1/2]

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

Compute the matrix determinant.

Definition at line 669 of file mxvk_math.h.

669 {
670 return mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];
671 }
float mat[2][2]
Matrix elements indexed as row, column.
Definition mxvk_math.h:627

◆ Determinate() [2/2]

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

Compute the matrix determinant.

Definition at line 698 of file mxvk_math_eigen.hpp.

698 {
699 return ToEigen().determinant();
700 }

◆ Inverse() [1/2]

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

Compute the inverse matrix.

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

Definition at line 678 of file mxvk_math.h.

678 {
679 const float d = Determinate();
680 if (std::fabs(d) <= EPSILON) {
681 return false;
682 }
683 const float inv = 1.0f / d;
684 out.Set(mat[1][1] * inv, -mat[0][1] * inv, -mat[1][0] * inv, mat[0][0] * inv);
685 return true;
686 }
float Determinate() const
Compute the matrix determinant.
Definition mxvk_math.h:669
constexpr float EPSILON
Default tolerance used for floating-point singularity and zero-length checks.
Definition mxvk_math.h:37

◆ Inverse() [2/2]

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

Compute the inverse matrix.

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

Definition at line 707 of file mxvk_math_eigen.hpp.

707 {
708 const float d = Determinate();
709 if (std::fabs(d) <= EPSILON) {
710 return false;
711 }
712 out = FromEigen(ToEigen().inverse());
713 return true;
714 }

◆ LoadIdentity() [1/2]

void mxvk::Mat2D::LoadIdentity ( )
inline

Set this matrix to the identity matrix.

Definition at line 646 of file mxvk_math.h.

646 {
647 Set(1.0f, 0.0f, 0.0f, 1.0f);
648 }

◆ LoadIdentity() [2/2]

void mxvk::Mat2D::LoadIdentity ( )
inline

Set this matrix to the identity matrix.

Definition at line 678 of file mxvk_math_eigen.hpp.

678 {
679 ToEigen().setIdentity();
680 }

◆ operator*() [1/2]

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

Multiply two 2x2 matrices.

Definition at line 661 of file mxvk_math.h.

661 {
662 return {mat[0][0] * m.mat[0][0] + mat[0][1] * m.mat[1][0],
663 mat[0][0] * m.mat[0][1] + mat[0][1] * m.mat[1][1],
664 mat[1][0] * m.mat[0][0] + mat[1][1] * m.mat[1][0],
665 mat[1][0] * m.mat[0][1] + mat[1][1] * m.mat[1][1]};
666 }

◆ operator*() [2/2]

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

Multiply two 2x2 matrices.

Definition at line 693 of file mxvk_math_eigen.hpp.

693 {
694 return FromEigen(ToEigen() * m.ToEigen());
695 }

◆ operator+() [1/2]

Mat2D mxvk::Mat2D::operator+ ( const Mat2D & m) const
inlinenodiscard

Add two matrices component-wise.

Definition at line 651 of file mxvk_math.h.

651 {
652 return {mat[0][0] + m.mat[0][0], mat[0][1] + m.mat[0][1], mat[1][0] + m.mat[1][0], mat[1][1] + m.mat[1][1]};
653 }

◆ operator+() [2/2]

Mat2D mxvk::Mat2D::operator+ ( const Mat2D & m) const
inlinenodiscard

Add two matrices component-wise.

Definition at line 683 of file mxvk_math_eigen.hpp.

683 {
684 return FromEigen(ToEigen() + m.ToEigen());
685 }

◆ operator-() [1/2]

Mat2D mxvk::Mat2D::operator- ( const Mat2D & m) const
inlinenodiscard

Subtract two matrices component-wise.

Definition at line 656 of file mxvk_math.h.

656 {
657 return {mat[0][0] - m.mat[0][0], mat[0][1] - m.mat[0][1], mat[1][0] - m.mat[1][0], mat[1][1] - m.mat[1][1]};
658 }

◆ operator-() [2/2]

Mat2D mxvk::Mat2D::operator- ( const Mat2D & m) const
inlinenodiscard

Subtract two matrices component-wise.

Definition at line 688 of file mxvk_math_eigen.hpp.

688 {
689 return FromEigen(ToEigen() - m.ToEigen());
690 }

◆ Set() [1/2]

void mxvk::Mat2D::Set ( float m00,
float m01,
float m10,
float m11 )
inline

Set all matrix elements in row-major order.

Definition at line 638 of file mxvk_math.h.

638 {
639 mat[0][0] = m00;
640 mat[0][1] = m01;
641 mat[1][0] = m10;
642 mat[1][1] = m11;
643 }

◆ Set() [2/2]

void mxvk::Mat2D::Set ( float m00,
float m01,
float m10,
float m11 )
inline

Set all matrix elements in row-major order.

Definition at line 673 of file mxvk_math_eigen.hpp.

673 {
674 ToEigen() << m00, m01, m10, m11;
675 }

◆ Solve2x2() [1/2]

bool mxvk::Mat2D::Solve2x2 ( const Mat2D & a,
Mat1D & out,
const Mat1D & b )
inlinestatic

Solve a 2x2 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 723 of file mxvk_math_eigen.hpp.

723 {
724 const float d = a.Determinate();
725 if (std::fabs(d) <= EPSILON) {
726 return false;
727 }
728 const Eigen::Vector2f rhs(b.mat[0], b.mat[1]);
729 const Eigen::Vector2f result = a.ToEigen().transpose().partialPivLu().solve(rhs);
730 out.Set(result.x(), result.y());
731 return true;
732 }

◆ Solve2x2() [2/2]

bool mxvk::Mat2D::Solve2x2 ( const Mat2D & a,
Mat1D & out,
const Mat1D & b ) const
inline

Solve a 2x2 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 695 of file mxvk_math.h.

695 {
696 const float d = a.Determinate();
697 if (std::fabs(d) <= EPSILON) {
698 return false;
699 }
700 out.mat[0] = (b.mat[0] * a.mat[1][1] - a.mat[0][1] * b.mat[1]) / d;
701 out.mat[1] = (a.mat[0][0] * b.mat[1] - b.mat[0] * a.mat[1][0]) / d;
702 return true;
703 }

Member Data Documentation

◆ mat

float mxvk::Mat2D::mat {}

Matrix elements indexed as row, column.

Definition at line 627 of file mxvk_math.h.

627{};

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