LiteFX 0.4.1.2025
Computer Graphics Engine
Loading...
Searching...
No Matches
LiteFX::Math::Matrix< T, ROWS, COLS > Struct Template Referencefinal

An algebraic matrix type. More...

#include <matrix.hpp>

Public Types

using scalar_type = T
 The type of the matrix elements.
 
using mat_type = Matrix<scalar_type, mat_rows, mat_cols>
 The type of the matrix itself.
 
template<unsigned rows, unsigned cols>
using generic_mat_type = Matrix<scalar_type, rows, cols>
 The type of the matrix, but without pre-defined dimensions.
 

Public Member Functions

constexpr Matrix () noexcept=default
 Initializes an empty matrix.
 
constexpr Matrix (T val) noexcept
 Initializes a matrix where all elements take the value provided by val .
 
constexpr Matrix (array_type &&array) noexcept
 Initializes a matrix with an array of values.
 
constexpr Matrix (std::initializer_list< scalar_type > elements) noexcept
 Initializes the matrix with a set of values.
 
template<unsigned rows, unsigned cols>
constexpr Matrix (const Matrix< scalar_type, rows, cols > &_other)
 Initializes a copy from another matrix, that might have different dimensions.
 
constexpr Matrix (Matrix &&_other) noexcept=default
 Initializes a matrix by taking over another matrix.
 
constexpr Matrix (const Matrix &_other)=default
 Initializes a matrix with the values provided by another matrix.
 
constexpr Matrixoperator= (Matrix &&_other) noexcept=default
 Moves the elements of the other matrix to the current matrix.
 
constexpr Matrixoperator= (const Matrix &_other)=default
 Copies the elements of another matrix into the current matrix.
 
constexpr ~Matrix () noexcept=default
 Destroys the matrix instance.
 
constexpr const scalar_typeelements () const noexcept
 Returns a pointer to the raw data of the matrix.
 
constexpr scalar_typeelements () noexcept
 Returns a pointer to the raw data of the matrix.
 
consteval size_t size () const noexcept
 Returns the number of elements of the matrix.
 
constexpr auto begin () noexcept
 Returns an iterator for that addresses the begin of the matrix elements.
 
constexpr auto end () noexcept
 Returns an iterator for that addresses the end of the matrix elements.
 
constexpr auto cbegin () const noexcept
 Returns a constant iterator for that addresses the begin of the matrix elements.
 
constexpr auto cend () const noexcept
 Returns a constant iterator for that addresses the end of the matrix elements.
 
constexpr scalar_type at (size_t row, size_t col) const noexcept
 Returns the element at a specified position.
 
constexpr scalar_typeat (size_t row, size_t col) noexcept
 Returns the element at a specified position.
 
constexpr std::span< const scalar_typerow (size_t row) const noexcept
 Returns a view over a row of the matrix.
 
constexpr std::span< scalar_typerow (size_t row) noexcept
 Returns a view over a row of the matrix.
 
constexpr std::array< scalar_type, mat_colscolumn (size_t col) const noexcept
 Returns a copy of a column over the matrix.
 
constexpr scalar_type operator[] (std::pair< size_t, size_t > position) const noexcept
 Returns an element of the matrix.
 
constexpr scalar_typeoperator[] (std::pair< size_t, size_t > position) noexcept
 Returns an element of the matrix.
 
constexpr operator std::array< T, mat_rows *mat_cols > () const noexcept
 Converts the matrix to an instance of std::array.
 
constexpr operator std::vector< T > () const noexcept
 Converts the matrix into an instance of type std::vector.
 
constexpr operator std::span< const scalar_type > () const noexcept
 Converts the matrix into a linear view over the elements.
 
constexpr operator std::span< scalar_type > () noexcept
 Converts the matrix into a linear view over the elements.
 
constexpr generic_mat_type< mat_cols, mat_rowstranspose () const noexcept
 Returns a copy of the matrix where the elements are transposed.
 
consteval bool symmetric () const noexcept
 Returns whether or not the matrix is symmetric, that is the number of rows and columns are equal.
 

Static Public Member Functions

static constexpr mat_type identity () noexcept
 Returns an identity matrix.
 

Static Public Attributes

static constexpr size_t mat_rows = ROWS
 Stores the number of rows of the matrix.
 
static constexpr size_t mat_cols = COLS
 Stores the number of columns of the matrix.
 

Protected Types

using array_type = std::array<scalar_type, mat_rows * mat_cols>
 

Protected Attributes

array_type m_elements = { }
 

Detailed Description

template<typename T, unsigned ROWS, unsigned COLS>
requires (ROWS >= 2 && COLS >= 2) && std::is_standard_layout_v<T> && std::is_trivially_copyable_v<T>
struct LiteFX::Math::Matrix< T, ROWS, COLS >

An algebraic matrix type.

Note that matrices in the engine are row-major by convention.

Matrices act as optimized storage containers only. All algebraic operations are not part of the library itself, but rather covered by supported linear algebra libraries.

Template Parameters
TThe type of the matrix scalar elements. Must be in standard layout (i.e., std::is_standard_layout_v<T> must evaluate to true).
ROWSThe number of rows of the matrix. Must be greater than 1.
COLSThe number of columns of the matrix. Must be greater than 1.

Member Typedef Documentation

◆ array_type

template<typename T , unsigned ROWS, unsigned COLS>
using LiteFX::Math::Matrix< T, ROWS, COLS >::array_type = std::array<scalar_type, mat_rows * mat_cols>
protected

◆ generic_mat_type

template<typename T , unsigned ROWS, unsigned COLS>
template<unsigned rows, unsigned cols>
using LiteFX::Math::Matrix< T, ROWS, COLS >::generic_mat_type = Matrix<scalar_type, rows, cols>

The type of the matrix, but without pre-defined dimensions.

Template Parameters
rowsThe number of rows of the matrix.
colsThe number of columns of the matrix.

◆ mat_type

template<typename T , unsigned ROWS, unsigned COLS>
using LiteFX::Math::Matrix< T, ROWS, COLS >::mat_type = Matrix<scalar_type, mat_rows, mat_cols>

The type of the matrix itself.

◆ scalar_type

template<typename T , unsigned ROWS, unsigned COLS>
using LiteFX::Math::Matrix< T, ROWS, COLS >::scalar_type = T

The type of the matrix elements.

Constructor & Destructor Documentation

◆ Matrix() [1/7]

template<typename T , unsigned ROWS, unsigned COLS>
LiteFX::Math::Matrix< T, ROWS, COLS >::Matrix ( )
constexprdefaultnoexcept

Initializes an empty matrix.

◆ Matrix() [2/7]

template<typename T , unsigned ROWS, unsigned COLS>
LiteFX::Math::Matrix< T, ROWS, COLS >::Matrix ( T val)
inlineconstexprnoexcept

Initializes a matrix where all elements take the value provided by val .

Parameters
valThe value to initialize all elements of the matrix with.

◆ Matrix() [3/7]

template<typename T , unsigned ROWS, unsigned COLS>
LiteFX::Math::Matrix< T, ROWS, COLS >::Matrix ( array_type && array)
inlineconstexprnoexcept

Initializes a matrix with an array of values.

Parameters
arrayThe array of values to take over by the matrix.

◆ Matrix() [4/7]

template<typename T , unsigned ROWS, unsigned COLS>
LiteFX::Math::Matrix< T, ROWS, COLS >::Matrix ( std::initializer_list< scalar_type > elements)
inlineconstexprnoexcept

Initializes the matrix with a set of values.

Parameters
elementsThe values to initialize the matrix with.

◆ Matrix() [5/7]

template<typename T , unsigned ROWS, unsigned COLS>
template<unsigned rows, unsigned cols>
LiteFX::Math::Matrix< T, ROWS, COLS >::Matrix ( const Matrix< scalar_type, rows, cols > & _other)
inlineconstexpr

Initializes a copy from another matrix, that might have different dimensions.

Template Parameters
rowsThe rows of the other matrix.
colsThe columns of the other matrix.
Parameters
_otherThe other matrix.

◆ Matrix() [6/7]

template<typename T , unsigned ROWS, unsigned COLS>
LiteFX::Math::Matrix< T, ROWS, COLS >::Matrix ( Matrix< T, ROWS, COLS > && _other)
constexprdefaultnoexcept

Initializes a matrix by taking over another matrix.

Parameters
_otherThe matrix to take over.

◆ Matrix() [7/7]

template<typename T , unsigned ROWS, unsigned COLS>
LiteFX::Math::Matrix< T, ROWS, COLS >::Matrix ( const Matrix< T, ROWS, COLS > & _other)
constexprdefault

Initializes a matrix with the values provided by another matrix.

Parameters
_otherThe other matrix to copy the values from.

◆ ~Matrix()

template<typename T , unsigned ROWS, unsigned COLS>
LiteFX::Math::Matrix< T, ROWS, COLS >::~Matrix ( )
constexprdefaultnoexcept

Destroys the matrix instance.

Member Function Documentation

◆ at() [1/2]

template<typename T , unsigned ROWS, unsigned COLS>
scalar_type LiteFX::Math::Matrix< T, ROWS, COLS >::at ( size_t row,
size_t col ) const
inlineconstexprnoexcept

Returns the element at a specified position.

Parameters
rowThe row of the element.
colThe column of the element.
Returns
The scalar value at the provided row and column.

◆ at() [2/2]

template<typename T , unsigned ROWS, unsigned COLS>
scalar_type & LiteFX::Math::Matrix< T, ROWS, COLS >::at ( size_t row,
size_t col )
inlineconstexprnoexcept

Returns the element at a specified position.

Parameters
rowThe row of the element.
colThe column of the element.
Returns
A reference of the scalar value at the provided row and column.

◆ begin()

template<typename T , unsigned ROWS, unsigned COLS>
auto LiteFX::Math::Matrix< T, ROWS, COLS >::begin ( )
inlineconstexprnoexcept

Returns an iterator for that addresses the begin of the matrix elements.

Returns
An iterator for that addresses the begin of the matrix elements.

◆ cbegin()

template<typename T , unsigned ROWS, unsigned COLS>
auto LiteFX::Math::Matrix< T, ROWS, COLS >::cbegin ( ) const
inlineconstexprnoexcept

Returns a constant iterator for that addresses the begin of the matrix elements.

Returns
A constant iterator for that addresses the begin of the matrix elements.

◆ cend()

template<typename T , unsigned ROWS, unsigned COLS>
auto LiteFX::Math::Matrix< T, ROWS, COLS >::cend ( ) const
inlineconstexprnoexcept

Returns a constant iterator for that addresses the end of the matrix elements.

Returns
A constant iterator for that addresses the end of the matrix elements.

◆ column()

template<typename T , unsigned ROWS, unsigned COLS>
std::array< scalar_type, mat_cols > LiteFX::Math::Matrix< T, ROWS, COLS >::column ( size_t col) const
inlineconstexprnoexcept

Returns a copy of a column over the matrix.

Note that this call involves a copy, which may be inefficient if done frequently. Prefer converting the matrix into an std::mdspan instead, if supported.

Parameters
colThe index of the column of the matrix.
Returns
An array containing a copy of the specified column.

◆ elements() [1/2]

template<typename T , unsigned ROWS, unsigned COLS>
const scalar_type * LiteFX::Math::Matrix< T, ROWS, COLS >::elements ( ) const
inlineconstexprnoexcept

Returns a pointer to the raw data of the matrix.

Returns
A pointer to the raw data of the matrix.

◆ elements() [2/2]

template<typename T , unsigned ROWS, unsigned COLS>
scalar_type * LiteFX::Math::Matrix< T, ROWS, COLS >::elements ( )
inlineconstexprnoexcept

Returns a pointer to the raw data of the matrix.

Returns
A pointer to the raw data of the matrix.

◆ end()

template<typename T , unsigned ROWS, unsigned COLS>
auto LiteFX::Math::Matrix< T, ROWS, COLS >::end ( )
inlineconstexprnoexcept

Returns an iterator for that addresses the end of the matrix elements.

Returns
An iterator for that addresses the end of the matrix elements.

◆ identity()

template<typename T , unsigned ROWS, unsigned COLS>
static constexpr mat_type LiteFX::Math::Matrix< T, ROWS, COLS >::identity ( )
inlinestaticconstexprnoexcept

Returns an identity matrix.

Returns
An identity matrix instance.

◆ operator std::array< T, mat_rows *mat_cols >()

template<typename T , unsigned ROWS, unsigned COLS>
LiteFX::Math::Matrix< T, ROWS, COLS >::operator std::array< T, mat_rows *mat_cols > ( ) const
inlineconstexprnoexcept

Converts the matrix to an instance of std::array.

◆ operator std::span< const scalar_type >()

template<typename T , unsigned ROWS, unsigned COLS>
LiteFX::Math::Matrix< T, ROWS, COLS >::operator std::span< const scalar_type > ( ) const
inlineconstexprnoexcept

Converts the matrix into a linear view over the elements.

◆ operator std::span< scalar_type >()

template<typename T , unsigned ROWS, unsigned COLS>
LiteFX::Math::Matrix< T, ROWS, COLS >::operator std::span< scalar_type > ( )
inlineconstexprnoexcept

Converts the matrix into a linear view over the elements.

◆ operator std::vector< T >()

template<typename T , unsigned ROWS, unsigned COLS>
LiteFX::Math::Matrix< T, ROWS, COLS >::operator std::vector< T > ( ) const
inlineconstexprnoexcept

Converts the matrix into an instance of type std::vector.

◆ operator=() [1/2]

template<typename T , unsigned ROWS, unsigned COLS>
Matrix & LiteFX::Math::Matrix< T, ROWS, COLS >::operator= ( const Matrix< T, ROWS, COLS > & _other)
constexprdefault

Copies the elements of another matrix into the current matrix.

Parameters
_otherThe matrix to copy the elements from.
Returns
A reference to the current matrix instance.

◆ operator=() [2/2]

template<typename T , unsigned ROWS, unsigned COLS>
Matrix & LiteFX::Math::Matrix< T, ROWS, COLS >::operator= ( Matrix< T, ROWS, COLS > && _other)
constexprdefaultnoexcept

Moves the elements of the other matrix to the current matrix.

Parameters
_otherThe matrix to take over.
Returns
A reference to the current matrix instance.

◆ operator[]() [1/2]

template<typename T , unsigned ROWS, unsigned COLS>
scalar_type LiteFX::Math::Matrix< T, ROWS, COLS >::operator[] ( std::pair< size_t, size_t > position) const
inlineconstexprnoexcept

Returns an element of the matrix.

Parameters
positionThe row and column position of the matrix element.
Returns
The scalar value at the provided position.

◆ operator[]() [2/2]

template<typename T , unsigned ROWS, unsigned COLS>
scalar_type & LiteFX::Math::Matrix< T, ROWS, COLS >::operator[] ( std::pair< size_t, size_t > position)
inlineconstexprnoexcept

Returns an element of the matrix.

Parameters
positionThe row and column position of the matrix element.
Returns
A reference of the scalar value at the provided position.

◆ row() [1/2]

template<typename T , unsigned ROWS, unsigned COLS>
std::span< const scalar_type > LiteFX::Math::Matrix< T, ROWS, COLS >::row ( size_t row) const
inlineconstexprnoexcept

Returns a view over a row of the matrix.

Parameters
rowThe index of the row to view.
Returns
A view over the specified matrix row.

◆ row() [2/2]

template<typename T , unsigned ROWS, unsigned COLS>
std::span< scalar_type > LiteFX::Math::Matrix< T, ROWS, COLS >::row ( size_t row)
inlineconstexprnoexcept

Returns a view over a row of the matrix.

Parameters
rowThe index of the row to view.
Returns
A view over the specified matrix row.

◆ size()

template<typename T , unsigned ROWS, unsigned COLS>
size_t LiteFX::Math::Matrix< T, ROWS, COLS >::size ( ) const
inlineconstevalnoexcept

Returns the number of elements of the matrix.

Returns
The number of elements of the matrix.

◆ symmetric()

template<typename T , unsigned ROWS, unsigned COLS>
bool LiteFX::Math::Matrix< T, ROWS, COLS >::symmetric ( ) const
inlineconstevalnoexcept

Returns whether or not the matrix is symmetric, that is the number of rows and columns are equal.

Returns
true, if the matrix is symmetric and false otherwise.

◆ transpose()

template<typename T , unsigned ROWS, unsigned COLS>
generic_mat_type< mat_cols, mat_rows > LiteFX::Math::Matrix< T, ROWS, COLS >::transpose ( ) const
inlineconstexprnoexcept

Returns a copy of the matrix where the elements are transposed.

You can use this operations, if you want to iterate all columns of the matrix in a more efficient way. Transposing effectively turns a row-major matrix into a column-major one.

Returns
A copy of the matrix where the elements are transposed.

Member Data Documentation

◆ m_elements

template<typename T , unsigned ROWS, unsigned COLS>
array_type LiteFX::Math::Matrix< T, ROWS, COLS >::m_elements = { }
protected

◆ mat_cols

template<typename T , unsigned ROWS, unsigned COLS>
size_t LiteFX::Math::Matrix< T, ROWS, COLS >::mat_cols = COLS
staticconstexpr

Stores the number of columns of the matrix.

◆ mat_rows

template<typename T , unsigned ROWS, unsigned COLS>
size_t LiteFX::Math::Matrix< T, ROWS, COLS >::mat_rows = ROWS
staticconstexpr

Stores the number of rows of the matrix.