Raven Engine v0.1
A modern 3D Game Engine
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<f32, 2, 2>
 2x2 single-precision f32ing-point matrix.
using Crux::mat3 = Matrix<f32, 3, 3>
 3x3 single-precision f32ing-point matrix.
using Crux::mat4 = Matrix<f32, 4, 4>
 4x4 single-precision f32ing-point matrix.
using Crux::dmat2 = Matrix<double, 2, 2>
 2x2 double-precision f32ing-point matrix.
using Crux::dmat3 = Matrix<double, 3, 3>
 3x3 double-precision f32ing-point matrix.
using Crux::dmat4 = Matrix<double, 4, 4>
 4x4 double-precision f32ing-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::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, 4, 4 > Crux::Rotate (T angle, const Vector< T, 3 > &axis)
 Creates a 4x4 rotation matrix around an arbitrary 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< f32, 4, 4 > Crux::Translate (f32 tx, f32 ty, f32 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::ToMat4 (const Matrix< T, 3, 3 > &m)
 Converts a 3x3 Matrix into a 4x4 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.
Matrix< f32, 4, 4 > Crux::Perspective (f32 yFov, f32 aspect, f32 zNear, f32 zFar)
 Constructs a perspective projection matrix.
template<typename T>
constexpr Matrix< T, 4, 4 > Crux::Ortho (T left, T right, T bottom, T top, T zNear, T zFar)
 Constructs an orthographic projection matrix.

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 f32ing-point matrix.

◆ dmat3

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

3x3 double-precision f32ing-point matrix.

◆ dmat4

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

4x4 double-precision f32ing-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<f32, 2, 2>

2x2 single-precision f32ing-point matrix.

◆ mat3

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

3x3 single-precision f32ing-point matrix.

◆ mat4

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

4x4 single-precision f32ing-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. f32, 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.

◆ Ortho()

template<typename T>
Matrix< T, 4, 4 > Crux::Ortho ( T left,
T right,
T bottom,
T top,
T zNear,
T zFar )
constexpr

Constructs an orthographic projection matrix.

Maps a cuboid volume defined by left/right, bottom/top, near/far planes into normalized device coordinates.

Parameters
leftLeft boundary of the view volume.
rightRight boundary of the view volume.
bottomBottom boundary of the view volume.
topTop boundary of the view volume.
zNearNear clipping plane (positive).
zFarFar clipping plane (positive).
Returns
Matrix<T, 4, 4> An orthographic projection matrix.

◆ Perspective()

Matrix< f32, 4, 4 > Crux::Perspective ( f32 yFov,
f32 aspect,
f32 zNear,
f32 zFar )
inline

Constructs a perspective projection matrix.

Creates a projection matrix for a perspective camera based on vertical field of view, aspect ratio, and near/far clipping planes.

Note: The Y axis is flipped (inverted) to accommodate Vulkan's coordinate conventions where the Y coordinate in clip space points downwards.

Parameters
yFovVertical field of view in radians.
aspectAspect ratio defined as width / height.
zNearNear clipping plane distance (positive).
zFarFar clipping plane distance (positive).
Returns
Matrix<f32, 4, 4> A perspective projection matrix.

◆ Rotate()

template<typename T>
Matrix< T, 4, 4 > Crux::Rotate ( T angle,
const Vector< T, 3 > & axis )
constexpr

Creates a 4x4 rotation matrix around an arbitrary axis.

Constructs a rotation matrix for rotating points around the given axis by the specified angle using the Rodrigues rotation formula.

Parameters
angleRotation angle in radians.
axisRotation axis (does not need to be normalized).
Returns
Matrix<T, 4, 4> Rotation 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

◆ ToMat4()

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

Converts a 3x3 Matrix into a 4x4 Matrix.

Places the 3x3 into the upper-left corner of the 4x4. The last row and column are set to (0,0,0,1) so the result is a valid homogeneous transform.

Returns
Matrix<T,4,4> 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< f32, 4, 4 > Crux::Translate ( f32 tx,
f32 ty,
f32 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).

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
Matrix<T, Rows, Cols> Zeroed matrix.