Raven Engine v0.1
Loading...
Searching...
No Matches
Matrix

Matrix types and operations. More...

Classes

struct  Crux::Matrix< T, Rows, Cols >
 Generic fixed-size matrix container. More...

Typedefs

using Crux::mat2 = Matrix<float, 2, 2>
 2x2 single-precision floating-point matrix.
using Crux::mat3 = Matrix<float, 3, 3>
 3x3 single-precision floating-point matrix.
using Crux::mat4 = Matrix<float, 4, 4>
 4x4 single-precision floating-point matrix.
using Crux::dmat2 = Matrix<double, 2, 2>
 2x2 double-precision floating-point matrix.
using Crux::dmat3 = Matrix<double, 3, 3>
 3x3 double-precision floating-point matrix.
using Crux::dmat4 = Matrix<double, 4, 4>
 4x4 double-precision floating-point matrix.
using Crux::imat2 = Matrix<int, 2, 2>
 2x2 integer matrix.
using Crux::imat3 = Matrix<int, 3, 3>
 3x3 integer matrix.
using Crux::imat4 = Matrix<int, 4, 4>
 4x4 integer matrix.

Functions

template<typename T, size_t Rows, size_t Cols>
constexpr Matrix< T, Rows, Cols > Crux::operator+ (const Matrix< T, Rows, Cols > &a, const Matrix< T, Rows, Cols > &b)
 Adds two matrices element-wise.
template<typename T, size_t Rows, size_t Cols>
constexpr Matrix< T, Rows, Cols > Crux::operator- (const Matrix< T, Rows, Cols > &a, const Matrix< T, Rows, Cols > &b)
 Subtracts two matrices element-wise.
template<typename T, size_t Rows, size_t Cols>
constexpr Matrix< T, Rows, Cols > Crux::operator* (const Matrix< T, Rows, Cols > &a, const T &scalar)
 Multiplies each matrix element by a scalar.
template<typename T, size_t Rows, size_t Cols>
constexpr Matrix< T, Rows, Cols > Crux::operator* (const T &scalar, const Matrix< T, Rows, Cols > &a)
 Multiplies each matrix element by a scalar.
template<typename T, size_t Rows, size_t Cols>
constexpr Matrix< T, Rows, Cols > Crux::operator/ (const Matrix< T, Rows, Cols > &a, const T &scalar)
 Divides each matrix element by a scalar.
template<typename T, size_t RowsA, size_t ColsA, size_t ColsB>
constexpr Matrix< T, RowsA, ColsB > Crux::operator* (const Matrix< T, RowsA, ColsA > &a, const Matrix< T, ColsA, ColsB > &b)
 Performs matrix multiplication (dot product).
template<typename T, size_t Rows, size_t Cols>
constexpr Matrix< T, Rows, Cols > Crux::Zero ()
 Generates a zero matrix of size Rows x Cols.
template<typename T, size_t Rows, size_t Cols>
constexpr Matrix< T, Cols, Rows > Crux::Transpose (const Matrix< T, Rows, Cols > &a)
 Transposes the given matrix (rows become columns).
template<typename T>
constexpr Matrix< T, 4, 4 > Crux::RotateX (T angle)
 Creates a 4x4 rotation matrix around the X axis.
template<typename T>
constexpr Matrix< T, 4, 4 > Crux::RotateY (T angle)
 Creates a 4x4 rotation matrix around the Y axis.
template<typename T>
constexpr Matrix< T, 4, 4 > Crux::RotateZ (T angle)
 Creates a 4x4 rotation matrix around the Z axis.
template<typename T>
constexpr Matrix< T, 2, 2 > Crux::Scale (T tx, T ty)
 Constructs a 2D scaling matrix.
template<typename T>
constexpr Matrix< T, 4, 4 > Crux::Scale (T sx, T sy, T sz)
 Constructs a 4x4 3D non-uniform scaling matrix.
template<typename T>
constexpr Matrix< T, 4, 4 > Crux::Scale (const Vector< T, 3 > &s)
 Constructs a 4x4 3D non-uniform scaling matrix from a vector.
template<typename T, size_t N>
constexpr Matrix< T, N, N > Crux::Scale (T s)
 Constructs a uniform scaling matrix of size N x N.
template<typename T>
constexpr Matrix< T, 3, 3 > Crux::Translate (T tx, T ty)
 Constructs a 3x3 2D translation matrix.
constexpr Matrix< float, 4, 4 > Crux::Translate (float tx, float ty, float tz)
 Constructs a 4x4 3D translation matrix.
template<typename T>
constexpr Matrix< T, 4, 4 > Crux::Translate (const Matrix< T, 4, 4 > &m, const Vector< T, 3 > &v)
 Applies a 3D translation to a 4x4 matrix using a vector.
template<typename T, size_t N>
constexpr Matrix< T, N, N > Crux::Identity ()
 Generates an identity matrix of size NxN.
template<typename T, size_t N>
constexpr T Crux::Trace (const Matrix< T, N, N > &a)
 Computes the trace (sum of diagonal elements) of a square matrix.
template<typename T, size_t Rows, size_t Cols>
constexpr T Crux::Norm (const Matrix< T, Rows, Cols > &a)
 Computes the Frobenius norm of the matrix.
template<typename T, size_t Rows, size_t Cols>
constexpr Matrix< T, Rows, Cols > Crux::Normalize (const Matrix< T, Rows, Cols > &a)
 Normalizes the matrix by dividing each element by the Frobenius norm.
template<typename T>
constexpr T Crux::Determinant (const Matrix< T, 2, 2 > &a)
 Computes the determinant of a 2x2 matrix.
template<typename T>
constexpr T Crux::Determinant (const Matrix< T, 3, 3 > &a)
 Computes the determinant of a 3x3 matrix.
template<typename T>
constexpr T Crux::Determinant (const Matrix< T, 4, 4 > &a)
 Computes the determinant of a 4x4 matrix.
template<typename T>
constexpr Matrix< T, 2, 2 > Crux::Inverse (const Matrix< T, 2, 2 > &a)
 Computes the inverse of a 2x2 matrix.
template<typename T>
constexpr Matrix< T, 3, 3 > Crux::Inverse (const Matrix< T, 3, 3 > &a)
 Computes the inverse of a 3x3 matrix.
template<typename T>
constexpr Matrix< T, 4, 4 > Crux::Inverse (const Matrix< T, 4, 4 > &a)
 Computes the inverse of a 4x4 matrix.
template<typename T>
constexpr Matrix< T, 3, 3 > Crux::ToMat3 (const Matrix< T, 4, 4 > &m)
 Converts a 4x4 Matrix into a 3x3 Matrix.
template<typename T>
constexpr Matrix< T, 4, 4 > Crux::LookAt (const Vector< T, 3 > &position, const Vector< T, 3 > &target, const Vector< T, 3 > &up)
 Creates a look-at view matrix using position, target, and up vectors.

Detailed Description

Matrix types and operations.

Contains matrix definitions, transformations, and related algorithms.

Typedef Documentation

◆ dmat2

using Crux::dmat2 = Matrix<double, 2, 2>

2x2 double-precision floating-point matrix.

◆ dmat3

using Crux::dmat3 = Matrix<double, 3, 3>

3x3 double-precision floating-point matrix.

◆ dmat4

using Crux::dmat4 = Matrix<double, 4, 4>

4x4 double-precision floating-point matrix.

◆ imat2

using Crux::imat2 = Matrix<int, 2, 2>

2x2 integer matrix.

◆ imat3

using Crux::imat3 = Matrix<int, 3, 3>

3x3 integer matrix.

◆ imat4

using Crux::imat4 = Matrix<int, 4, 4>

4x4 integer matrix.

◆ mat2

using Crux::mat2 = Matrix<float, 2, 2>

2x2 single-precision floating-point matrix.

◆ mat3

using Crux::mat3 = Matrix<float, 3, 3>

3x3 single-precision floating-point matrix.

◆ mat4

using Crux::mat4 = Matrix<float, 4, 4>

4x4 single-precision floating-point matrix.

Function Documentation

◆ Determinant() [1/3]

template<typename T>
T Crux::Determinant ( const Matrix< T, 2, 2 > & a)
constexpr

Computes the determinant of a 2x2 matrix.

Calculates the determinant using the formula: ad - bc.

Parameters
aThe matrix.
Returns
T Determinant of the matrix.

◆ Determinant() [2/3]

template<typename T>
T Crux::Determinant ( const Matrix< T, 3, 3 > & a)
constexpr

Computes the determinant of a 3x3 matrix.

Uses the rule of Sarrus or cofactor expansion.

Parameters
aThe matrix.
Returns
T Determinant of the matrix.

◆ Determinant() [3/3]

template<typename T>
T Crux::Determinant ( const Matrix< T, 4, 4 > & a)
constexpr

Computes the determinant of a 4x4 matrix.

Uses cofactor expansion along the first row.

Parameters
aThe matrix.
Returns
T Determinant of the matrix.

◆ Identity()

template<typename T, size_t N>
Matrix< T, N, N > Crux::Identity ( )
constexpr

Generates an identity matrix of size NxN.

Returns a square matrix with ones on the diagonal and zeros elsewhere.

Returns
Matrix<T, N, N> Identity matrix.

◆ Inverse() [1/3]

template<typename T>
Matrix< T, 2, 2 > Crux::Inverse ( const Matrix< T, 2, 2 > & a)
constexpr

Computes the inverse of a 2x2 matrix.

Returns the inverse if the matrix is invertible; otherwise, returns the original matrix.

Note
If the matrix is not invertible, the input matrix is returned unchanged.
Parameters
aThe matrix.
Returns
Matrix<T, 2, 2> Inverse of the matrix.

◆ Inverse() [2/3]

template<typename T>
Matrix< T, 3, 3 > Crux::Inverse ( const Matrix< T, 3, 3 > & a)
constexpr

Computes the inverse of a 3x3 matrix.

Returns the inverse if the matrix is invertible; otherwise, returns the original matrix.

Note
If the matrix is not invertible, the input matrix is returned unchanged.
Parameters
aThe matrix.
Returns
Matrix<T, 3, 3> Inverse of the matrix.

◆ Inverse() [3/3]

template<typename T>
Matrix< T, 4, 4 > Crux::Inverse ( const Matrix< T, 4, 4 > & a)
constexpr

Computes the inverse of a 4x4 matrix.

Returns the inverse if the matrix is invertible; otherwise, returns the original matrix.

Note
If the matrix is not invertible, the input matrix is returned unchanged.
Parameters
aThe matrix.
Returns
Matrix<T, 4, 4> Inverse of the matrix.

◆ LookAt()

template<typename T>
Matrix< T, 4, 4 > Crux::LookAt ( const Vector< T, 3 > & position,
const Vector< T, 3 > & target,
const Vector< T, 3 > & up )
constexpr

Creates a look-at view matrix using position, target, and up vectors.

Constructs a right-handed coordinate system for a camera transform, where:

  • The camera looks from position towards target.
  • up defines the world's up direction.

The matrix transforms world coordinates into view space.

Template Parameters
TNumeric type for the vector and matrix (e.g. float, double).
Parameters
positionThe camera position in world space.
targetThe point in world space the camera is looking at.
upThe world up vector, usually (0,0,1) or (0,1,0) depending on conventions.
Returns
Matrix<T, 4, 4> A view matrix that transforms world-space coordinates to view space.

◆ Norm()

template<typename T, size_t Rows, size_t Cols>
T Crux::Norm ( const Matrix< T, Rows, Cols > & a)
constexpr

Computes the Frobenius norm of the matrix.

Calculates the square root of the sum of the squares of all elements.

Parameters
aThe matrix.
Returns
T The Euclidean norm.

◆ Normalize()

template<typename T, size_t Rows, size_t Cols>
Matrix< T, Rows, Cols > Crux::Normalize ( const Matrix< T, Rows, Cols > & a)
constexpr

Normalizes the matrix by dividing each element by the Frobenius norm.

Scales the matrix so that its Frobenius norm is 1. If the norm is zero, returns the original matrix.

Parameters
aThe matrix to normalize.
Returns
Matrix<T, Rows, Cols> The normalized matrix.

◆ operator*() [1/3]

template<typename T, size_t Rows, size_t Cols>
Matrix< T, Rows, Cols > Crux::operator* ( const Matrix< T, Rows, Cols > & a,
const T & scalar )
constexpr

Multiplies each matrix element by a scalar.

Multiplies every element of the matrix by the given scalar value.

Parameters
aInput matrix.
scalarScalar value to multiply by.
Returns
Matrix<T, Rows, Cols> The scaled matrix.

◆ operator*() [2/3]

template<typename T, size_t RowsA, size_t ColsA, size_t ColsB>
Matrix< T, RowsA, ColsB > Crux::operator* ( const Matrix< T, RowsA, ColsA > & a,
const Matrix< T, ColsA, ColsB > & b )
constexpr

Performs matrix multiplication (dot product).

Multiplies two matrices using the standard matrix multiplication rule.

Parameters
aLeft matrix (RowsA x ColsA).
bRight matrix (ColsA x ColsB).
Returns
Matrix<T, RowsA, ColsB> Result of the matrix multiplication.

◆ operator*() [3/3]

template<typename T, size_t Rows, size_t Cols>
Matrix< T, Rows, Cols > Crux::operator* ( const T & scalar,
const Matrix< T, Rows, Cols > & a )
constexpr

Multiplies each matrix element by a scalar.

Multiplies every element of the matrix by the given scalar value.

Parameters
scalarScalar value to multiply by.
aInput matrix.
Returns
Matrix<T, Rows, Cols> The scaled matrix.

◆ operator+()

template<typename T, size_t Rows, size_t Cols>
Matrix< T, Rows, Cols > Crux::operator+ ( const Matrix< T, Rows, Cols > & a,
const Matrix< T, Rows, Cols > & b )
constexpr

Adds two matrices element-wise.

Adds corresponding elements of two matrices of the same size.

Parameters
aFirst matrix.
bSecond matrix.
Returns
Matrix<T, Rows, Cols> Resulting matrix after addition.

◆ operator-()

template<typename T, size_t Rows, size_t Cols>
Matrix< T, Rows, Cols > Crux::operator- ( const Matrix< T, Rows, Cols > & a,
const Matrix< T, Rows, Cols > & b )
constexpr

Subtracts two matrices element-wise.

Subtracts corresponding elements of matrix b from matrix a.

Parameters
aFirst matrix.
bSecond matrix.
Returns
Matrix<T, Rows, Cols> Resulting matrix after subtraction.

◆ operator/()

template<typename T, size_t Rows, size_t Cols>
Matrix< T, Rows, Cols > Crux::operator/ ( const Matrix< T, Rows, Cols > & a,
const T & scalar )
constexpr

Divides each matrix element by a scalar.

Divides every element of the matrix by the given scalar value.

Parameters
aInput matrix.
scalarScalar value to divide by.
Returns
Matrix<T, Rows, Cols> The scaled matrix.

◆ RotateX()

template<typename T>
Matrix< T, 4, 4 > Crux::RotateX ( T angle)
constexpr

Creates a 4x4 rotation matrix around the X axis.

Constructs a rotation matrix for rotating points around the X axis by the given angle.

Parameters
angleRotation angle in radians.
Returns
Matrix<T, 4, 4> Rotation matrix.

◆ RotateY()

template<typename T>
Matrix< T, 4, 4 > Crux::RotateY ( T angle)
constexpr

Creates a 4x4 rotation matrix around the Y axis.

Constructs a rotation matrix for rotating points around the Y axis by the given angle.

Parameters
angleRotation angle in radians.
Returns
Matrix<T, 4, 4> Rotation matrix.

◆ RotateZ()

template<typename T>
Matrix< T, 4, 4 > Crux::RotateZ ( T angle)
constexpr

Creates a 4x4 rotation matrix around the Z axis.

Constructs a rotation matrix for rotating points around the Z axis by the given angle.

Parameters
angleRotation angle in radians.
Returns
Matrix<T, 4, 4> Rotation matrix.

◆ Scale() [1/4]

template<typename T>
Matrix< T, 4, 4 > Crux::Scale ( const Vector< T, 3 > & s)
constexpr

Constructs a 4x4 3D non-uniform scaling matrix from a vector.

Unpacks the Vector<T,3> into sx, sy, sz.

Parameters
sScale vector (x, y, z).
Returns
Matrix<T, 4, 4> Scaling matrix.

◆ Scale() [2/4]

template<typename T, size_t N>
Matrix< T, N, N > Crux::Scale ( T s)
constexpr

Constructs a uniform scaling matrix of size N x N.

Puts s on every diagonal entry, zeros elsewhere.

Template Parameters
TNumeric type.
NDimension of the square matrix.
Parameters
sUniform scale factor.
Returns
Matrix<T, N, N> Diagonal matrix with s on the diagonal.

◆ Scale() [3/4]

template<typename T>
Matrix< T, 4, 4 > Crux::Scale ( T sx,
T sy,
T sz )
constexpr

Constructs a 4x4 3D non-uniform scaling matrix.

Scales X, Y, Z axes independently; leaves W = 1.

Parameters
sxScale in X direction.
syScale in Y direction.
szScale in Z direction.
Returns
Matrix<T, 4, 4> Scaling matrix.

◆ Scale() [4/4]

template<typename T>
Matrix< T, 2, 2 > Crux::Scale ( T tx,
T ty )
constexpr

Constructs a 2D scaling matrix.

Creates a 2x2 matrix that scales points in 2D space.

Parameters
txScale in X direction.
tyScale in Y direction.
Returns
Matrix<T, 2, 2> Scaling matrix.

◆ ToMat3()

template<typename T>
Matrix< T, 3, 3 > Crux::ToMat3 ( const Matrix< T, 4, 4 > & m)
constexpr

Converts a 4x4 Matrix into a 3x3 Matrix.

Constructs the 3x3 based on the top left components of the 4x4 Matrix. This causes the scale part of a Transformation to be left out, only leaving a Translation and Rotation Matrix behind

Returns
Matrix<T, 3, 3> the Converted Matrix

◆ Trace()

template<typename T, size_t N>
T Crux::Trace ( const Matrix< T, N, N > & a)
constexpr

Computes the trace (sum of diagonal elements) of a square matrix.

Sums the elements on the main diagonal of the matrix.

Parameters
aThe matrix.
Returns
T Sum of the diagonal elements.

◆ Translate() [1/3]

template<typename T>
Matrix< T, 4, 4 > Crux::Translate ( const Matrix< T, 4, 4 > & m,
const Vector< T, 3 > & v )
constexpr

Applies a 3D translation to a 4x4 matrix using a vector.

Multiplies the input matrix by a translation matrix constructed from the given vector.

Parameters
mThe matrix to transform.
vThe translation vector.
Returns
Matrix<T, 4, 4> Transformed matrix.

◆ Translate() [2/3]

Matrix< float, 4, 4 > Crux::Translate ( float tx,
float ty,
float tz )
constexpr

Constructs a 4x4 3D translation matrix.

Parameters
txTranslation along X axis.
tyTranslation along Y axis.
tzTranslation along Z axis.
Returns
Matrix<T, 4, 4> Translation matrix.

◆ Translate() [3/3]

template<typename T>
Matrix< T, 3, 3 > Crux::Translate ( T tx,
T ty )
constexpr

Constructs a 3x3 2D translation matrix.

Creates a 3x3 matrix for translating points in 2D homogeneous coordinates.

Parameters
txTranslation along X axis.
tyTranslation along Y axis.
Returns
Matrix<T, 3, 3> Translation matrix.

◆ Transpose()

template<typename T, size_t Rows, size_t Cols>
Matrix< T, Cols, Rows > Crux::Transpose ( const Matrix< T, Rows, Cols > & a)
constexpr

Transposes the given matrix (rows become columns).

Swaps the rows and columns of the input matrix.

Parameters
aThe matrix to transpose.
Returns
Matrix<T, Cols, Rows> The transposed matrix.

◆ Zero()

template<typename T, size_t Rows, size_t Cols>
Matrix< T, Rows, Cols > Crux::Zero ( )
constexpr

Generates a zero matrix of size Rows x Cols.

Returns a matrix filled with zeros.

Returns
Matrix<T, Rows, Cols> Zeroed matrix.