DirectZ
Loading...
Searching...
No Matches
dz::mat< T, C, R > Struct Template Reference

A generic fixed-size matrix template supporting common matrix operations. More...

#include <math.hpp>

Public Member Functions

 mat (T initial=T())
 Constructs a matrix initialized to zero with optional diagonal initialization.
matoperator= (const mat &other)
 Copy assignment operator.
vec< T, R > & operator[] (size_t i)
 Provides access to the column vector at index i (non-const).
const vec< T, R > & operator[] (size_t i) const
 Provides access to the column vector at index i (const).
template<size_t N>
mattranslate (const vec< T, N > &v)
 Applies a translation to a 4x4 matrix by adding a vector to its position component.
template<size_t N>
matscale (const vec< T, N > &v)
 Applies a component-wise scale to the matrix columns.
template<size_t N>
matrotate (T angle, const vec< T, N > &axis)
 Rotates the matrix in-place around the specified axis by the given angle.
template<size_t N>
matrotateAround (T angle, const vec< T, N > &axis, const vec< T, N > &point)
 Rotates the matrix around a specified pivot point.
mat< T, C, Rinverse () const
 Computes the inverse of a square matrix.
template<size_t C2, size_t R2>
mat< T, C, R > & operator*= (const mat< T, C2, R2 > &rhs)
 Performs in-place multiplication by another matrix.
template<size_t C2, size_t R2>
mat< T, C2, Roperator* (const mat< T, C2, R2 > &rhs) const
 Returns a new matrix which is the product of this matrix and rhs.
constexpr mat< T, R, Ctranspose ()

Static Public Member Functions

template<size_t N>
static mat translate_static (const vec< T, N > &v)
template<size_t N>
static mat scale_static (const vec< T, N > &v)
template<size_t N>
static mat rotate_static (T angle, const vec< T, N > &axis)

Public Attributes

T matrix [C][R]
 Raw matrix storage, column-major order.

Detailed Description

template<typename T, size_t C, size_t R>
struct dz::mat< T, C, R >

A generic fixed-size matrix template supporting common matrix operations.

This template represents a matrix with dimensions C columns by R rows, where each element is of type T. It supports initialization, assignment, indexing, transformation (translation, scaling, rotation), inversion, and multiplication.

The internal storage is column-major, meaning matrix[c][r] accesses the element in the c-th column and r-th row.

Template Parameters
TThe scalar numeric type used in the matrix (e.g., float, double).
CThe number of columns in the matrix.
RThe number of rows in the matrix.

Constructor & Destructor Documentation

◆ mat()

template<typename T, size_t C, size_t R>
dz::mat< T, C, R >::mat ( T initial = T())
inline

Constructs a matrix initialized to zero with optional diagonal initialization.

The matrix elements are zeroed. If an initial value is provided, it is assigned to the diagonal elements (for square or rectangular matrices where diagonal exists).

Parameters
initialThe value to initialize the diagonal elements with. Default is zero.

Member Function Documentation

◆ inverse()

template<typename T, size_t C, size_t R>
mat< T, C, R > dz::mat< T, C, R >::inverse ( ) const
inline

Computes the inverse of a square matrix.

Uses Gaussian elimination with partial pivoting to invert the matrix.

Returns
The inverse matrix.
Exceptions
Assertionfailure if the matrix is singular (non-invertible).

◆ operator*()

template<typename T, size_t C, size_t R>
template<size_t C2, size_t R2>
mat< T, C2, R > dz::mat< T, C, R >::operator* ( const mat< T, C2, R2 > & rhs) const
inline

Returns a new matrix which is the product of this matrix and rhs.

Does not modify the current matrix.

Template Parameters
C2Number of columns in the right-hand matrix.
R2Number of rows in the right-hand matrix.
Parameters
rhsThe matrix to multiply by.
Returns
The resulting matrix product.

◆ operator*=()

template<typename T, size_t C, size_t R>
template<size_t C2, size_t R2>
mat< T, C, R > & dz::mat< T, C, R >::operator*= ( const mat< T, C2, R2 > & rhs)
inline

Performs in-place multiplication by another matrix.

The matrix is multiplied by rhs, and the result replaces the current matrix.

Template Parameters
C2Number of columns in the right-hand matrix.
R2Number of rows in the right-hand matrix.
Parameters
rhsThe matrix to multiply by.
Returns
Reference to the resulting matrix (this).
Note
Requires the dimensions to satisfy C == R2, and output size matches current matrix.

◆ operator=()

template<typename T, size_t C, size_t R>
mat & dz::mat< T, C, R >::operator= ( const mat< T, C, R > & other)
inline

Copy assignment operator.

Copies all elements from another matrix of the same type and dimensions.

Parameters
otherThe source matrix to copy from.
Returns
Reference to this matrix after assignment.

◆ operator[]() [1/2]

template<typename T, size_t C, size_t R>
vec< T, R > & dz::mat< T, C, R >::operator[] ( size_t i)
inline

Provides access to the column vector at index i (non-const).

This returns a reference to the i-th column as a vector of length R.

Parameters
iThe column index to access (0-based).
Returns
Reference to the column vector.

◆ operator[]() [2/2]

template<typename T, size_t C, size_t R>
const vec< T, R > & dz::mat< T, C, R >::operator[] ( size_t i) const
inline

Provides access to the column vector at index i (const).

Parameters
iThe column index to access (0-based).
Returns
Const reference to the column vector.

◆ rotate()

template<typename T, size_t C, size_t R>
template<size_t N>
mat & dz::mat< T, C, R >::rotate ( T angle,
const vec< T, N > & axis )
inline

Rotates the matrix in-place around the specified axis by the given angle.

Supports 2D rotation around a 2D axis (angle in radians) or 3D rotation around an arbitrary 3D axis using Rodrigues' rotation formula.

The rotation is applied directly to each column of the matrix.

Template Parameters
NDimension of the rotation axis (2 or 3).
Parameters
angleThe angle to rotate by, in radians.
axisThe axis of rotation (2D or 3D vector).
Returns
Reference to the rotated matrix (this).
Note
For 2D rotations, the third component is assumed zero.

◆ rotate_static()

template<typename T, size_t C, size_t R>
template<size_t N>
mat dz::mat< T, C, R >::rotate_static ( T angle,
const vec< T, N > & axis )
inlinestatic

◆ rotateAround()

template<typename T, size_t C, size_t R>
template<size_t N>
mat & dz::mat< T, C, R >::rotateAround ( T angle,
const vec< T, N > & axis,
const vec< T, N > & point )
inline

Rotates the matrix around a specified pivot point.

Performs an in-place rotation around the point by:

  • Translating the matrix so that the pivot moves to the origin,
  • Rotating by the given angle around the axis,
  • Translating back to the original pivot location.
Template Parameters
NDimension of the rotation axis (2 or 3).
Parameters
angleThe angle of rotation in radians.
axisThe rotation axis vector.
pointThe pivot point around which to rotate.
Returns
Reference to the rotated matrix (this).

◆ scale()

template<typename T, size_t C, size_t R>
template<size_t N>
mat & dz::mat< T, C, R >::scale ( const vec< T, N > & v)
inline

Applies a component-wise scale to the matrix columns.

Multiplies each of the first N columns of the matrix by the corresponding scalar value from the vector v.

Template Parameters
NThe number of scale components (usually matching matrix dimension).
Parameters
vVector containing scaling factors.
Returns
Reference to the scaled matrix (this).

◆ scale_static()

template<typename T, size_t C, size_t R>
template<size_t N>
mat dz::mat< T, C, R >::scale_static ( const vec< T, N > & v)
inlinestatic

◆ translate()

template<typename T, size_t C, size_t R>
template<size_t N>
mat & dz::mat< T, C, R >::translate ( const vec< T, N > & v)
inline

Applies a translation to a 4x4 matrix by adding a vector to its position component.

This function assumes a 4x4 matrix representing an affine transform and translates it by the vector v applied to the fourth column (position).

Template Parameters
NThe dimension of the translation vector (must be 3).
Parameters
vThe translation vector to add.
Returns
Reference to the translated matrix (this).

◆ translate_static()

template<typename T, size_t C, size_t R>
template<size_t N>
mat dz::mat< T, C, R >::translate_static ( const vec< T, N > & v)
inlinestatic

◆ transpose()

template<typename T, size_t C, size_t R>
mat< T, R, C > dz::mat< T, C, R >::transpose ( )
inlineconstexpr

Member Data Documentation

◆ matrix

template<typename T, size_t C, size_t R>
T dz::mat< T, C, R >::matrix[C][R]

Raw matrix storage, column-major order.

Access elements as matrix[column][row].


The documentation for this struct was generated from the following file: