Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Public Types | Public Member Functions | Public Attributes | Static Public Attributes | Related Functions | List of all members
gf::Matrix< T, ROWS, COLS > Struct Template Reference

General purpose math matrix. More...

#include <gf/Matrix.h>

Public Types

using value_type = T
 The value type of the elements of the matrix. More...
 

Public Member Functions

 Matrix ()=default
 Default constructor. More...
 
 Matrix (T val)
 Constructor that fills the matrix with a value. More...
 

Public Attributes

union {
   T   grid [ROWS][COLS]
 Grid representation. More...
 
   T   data [ROWS *COLS]
 Array representation. More...
 
}; 
 

Static Public Attributes

static constexpr std::size_t Rows = ROWS
 The number of rows in the matrix. More...
 
static constexpr std::size_t Cols = COLS
 The number of columns in the matrix. More...
 

Related Functions

(Note that these are not member functions.)

template<typename T , std::size_t ROWS, std::size_t COLS>
bool operator== (const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
 Equality operator between two matrices. More...
 
template<typename T , std::size_t ROWS, std::size_t COLS>
bool operator!= (const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
 Inequality operator between two matrices. More...
 
template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > operator- (const Matrix< T, ROWS, COLS > &val)
 Component-wise unary minus. More...
 
template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > operator+ (const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
 Component-wise addition. More...
 
template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > & operator+= (Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
 Component-wise addition and assignment. More...
 
template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > operator- (const Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
 Component-wise substraction. More...
 
template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > & operator-= (Matrix< T, ROWS, COLS > &lhs, const Matrix< T, ROWS, COLS > &rhs)
 Component-wise substraction and assignment. More...
 
template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > operator* (const Matrix< T, ROWS, COLS > &lhs, T rhs)
 Right scalar multiplication. More...
 
template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > & operator*= (Matrix< T, ROWS, COLS > &lhs, T rhs)
 Right scalar multiplication and assignment. More...
 
template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > operator* (T lhs, const Matrix< T, ROWS, COLS > &rhs)
 Left scalar multiplication. More...
 
template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > operator/ (const Matrix< T, ROWS, COLS > &lhs, T rhs)
 Right scalar division. More...
 
template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > & operator/= (Matrix< T, ROWS, COLS > &lhs, T rhs)
 Right scalar division and assignment. More...
 
template<typename T , std::size_t S1, std::size_t S2>
Vector< T, S1 > operator* (const Matrix< T, S1, S2 > &lhs, const Vector< T, S2 > &rhs)
 Matrix-vector multiplication. More...
 
template<typename T , std::size_t S1, std::size_t S2>
Vector< T, S2 > operator* (const Vector< T, S1 > &lhs, const Matrix< T, S1, S2 > &rhs)
 Vector-matrix multiplication. More...
 
template<typename T , std::size_t S1, std::size_t S2, std::size_t S3>
Matrix< T, S1, S3 > operator* (const Matrix< T, S1, S2 > &lhs, const Matrix< T, S2, S3 > &rhs)
 Matrix-matrix multiplication. More...
 
template<typename T , std::size_t N>
Matrix< T, N, N > & operator*= (Matrix< T, N, N > &lhs, const Matrix< T, N, N > &rhs)
 Matrix-matrix multiplication and assignment. More...
 
template<typename MatrixType >
MatrixType identity ()
 Identity matrix. More...
 
template<typename T , std::size_t S1, std::size_t S2>
Matrix< T, S2, S1 > transpose (const Matrix< T, S1, S2 > &mat)
 Transposition of a matrix. More...
 
template<typename T >
Matrix< T, 2, 2 > invert (const Matrix< T, 2, 2 > &mat)
 Inversion of a 2x2 matrix. More...
 
template<typename T >
Matrix< T, 3, 3 > invert (const Matrix< T, 3, 3 > &mat)
 Inversion of a 3x3 matrix. More...
 

Detailed Description

template<typename T, std::size_t ROWS, std::size_t COLS>
struct gf::Matrix< T, ROWS, COLS >

General purpose math matrix.

gf::Matrix represents a matrix with ROWS rows and COLS columns. The internal representation uses a row-major order.

The template parameter T is the type of coordinates. . It can be any type that supports arithmetic operations (+, -, *, /).

Several specializations are defined for common use cases, especially for square matrices:

This class was designed according to the article On Vector Math Libraries by Nathan Reed.

See also
gf::Array2D

Member Typedef Documentation

template<typename T, std::size_t ROWS, std::size_t COLS>
using gf::Matrix< T, ROWS, COLS >::value_type = T

The value type of the elements of the matrix.

Constructor & Destructor Documentation

template<typename T, std::size_t ROWS, std::size_t COLS>
gf::Matrix< T, ROWS, COLS >::Matrix ( )
default

Default constructor.

template<typename T, std::size_t ROWS, std::size_t COLS>
gf::Matrix< T, ROWS, COLS >::Matrix ( T  val)
inlineexplicit

Constructor that fills the matrix with a value.

Friends And Related Function Documentation

template<typename MatrixType >
MatrixType identity ( )
related

Identity matrix.

See also
gf::identityTransform()
template<typename T >
Matrix< T, 2, 2 > invert ( const Matrix< T, 2, 2 > &  mat)
related

Inversion of a 2x2 matrix.

template<typename T >
Matrix< T, 3, 3 > invert ( const Matrix< T, 3, 3 > &  mat)
related

Inversion of a 3x3 matrix.

template<typename T , std::size_t ROWS, std::size_t COLS>
bool operator!= ( const Matrix< T, ROWS, COLS > &  lhs,
const Matrix< T, ROWS, COLS > &  rhs 
)
related

Inequality operator between two matrices.

template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > operator* ( const Matrix< T, ROWS, COLS > &  lhs,
T  rhs 
)
related

Right scalar multiplication.

template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > operator* ( T  lhs,
const Matrix< T, ROWS, COLS > &  rhs 
)
related

Left scalar multiplication.

template<typename T , std::size_t S1, std::size_t S2>
Vector< T, S1 > operator* ( const Matrix< T, S1, S2 > &  lhs,
const Vector< T, S2 > &  rhs 
)
related

Matrix-vector multiplication.

template<typename T , std::size_t S1, std::size_t S2>
Vector< T, S2 > operator* ( const Vector< T, S1 > &  lhs,
const Matrix< T, S1, S2 > &  rhs 
)
related

Vector-matrix multiplication.

template<typename T , std::size_t S1, std::size_t S2, std::size_t S3>
Matrix< T, S1, S3 > operator* ( const Matrix< T, S1, S2 > &  lhs,
const Matrix< T, S2, S3 > &  rhs 
)
related

Matrix-matrix multiplication.

template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > & operator*= ( Matrix< T, ROWS, COLS > &  lhs,
T  rhs 
)
related

Right scalar multiplication and assignment.

template<typename T , std::size_t N>
Matrix< T, N, N > & operator*= ( Matrix< T, N, N > &  lhs,
const Matrix< T, N, N > &  rhs 
)
related

Matrix-matrix multiplication and assignment.

This operation is only available for square matrices.

template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > operator+ ( const Matrix< T, ROWS, COLS > &  lhs,
const Matrix< T, ROWS, COLS > &  rhs 
)
related

Component-wise addition.

template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > & operator+= ( Matrix< T, ROWS, COLS > &  lhs,
const Matrix< T, ROWS, COLS > &  rhs 
)
related

Component-wise addition and assignment.

template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > operator- ( const Matrix< T, ROWS, COLS > &  val)
related

Component-wise unary minus.

template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > operator- ( const Matrix< T, ROWS, COLS > &  lhs,
const Matrix< T, ROWS, COLS > &  rhs 
)
related

Component-wise substraction.

template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > & operator-= ( Matrix< T, ROWS, COLS > &  lhs,
const Matrix< T, ROWS, COLS > &  rhs 
)
related

Component-wise substraction and assignment.

template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > operator/ ( const Matrix< T, ROWS, COLS > &  lhs,
T  rhs 
)
related

Right scalar division.

template<typename T , std::size_t ROWS, std::size_t COLS>
Matrix< T, ROWS, COLS > & operator/= ( Matrix< T, ROWS, COLS > &  lhs,
T  rhs 
)
related

Right scalar division and assignment.

template<typename T , std::size_t ROWS, std::size_t COLS>
bool operator== ( const Matrix< T, ROWS, COLS > &  lhs,
const Matrix< T, ROWS, COLS > &  rhs 
)
related

Equality operator between two matrices.

template<typename T , std::size_t S1, std::size_t S2>
Matrix< T, S2, S1 > transpose ( const Matrix< T, S1, S2 > &  mat)
related

Transposition of a matrix.

Member Data Documentation

union { ... }

An anonymous union to handle the various representations

template<typename T, std::size_t ROWS, std::size_t COLS>
constexpr std::size_t gf::Matrix< T, ROWS, COLS >::Cols = COLS
static

The number of columns in the matrix.

template<typename T, std::size_t ROWS, std::size_t COLS>
T gf::Matrix< T, ROWS, COLS >::data[ROWS *COLS]

Array representation.

template<typename T, std::size_t ROWS, std::size_t COLS>
T gf::Matrix< T, ROWS, COLS >::grid[ROWS][COLS]

Grid representation.

template<typename T, std::size_t ROWS, std::size_t COLS>
constexpr std::size_t gf::Matrix< T, ROWS, COLS >::Rows = ROWS
static

The number of rows in the matrix.