API Documentation
Public Member Functions | Public Attributes | List of all members
nkMaths::Matrix Class Reference

Represents a 4x4 float matrix. More...

Public Member Functions

 Matrix () noexcept
 
 Matrix (float m00, float m01, float m10, float m11) noexcept
 
 Matrix (float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22) noexcept
 
 Matrix (float m00, float m01, float m02, float m03, float m10, float m11, float m12, float m13, float m20, float m21, float m22, float m23, float m30, float m31, float m32, float m33) noexcept
 
 Matrix (const Matrix &other) noexcept
 
Vector getRow0 () const
 
Vector getRow1 () const
 
Vector getRow2 () const
 
Vector getRow3 () const
 
Vector getCol0 () const
 
Vector getCol1 () const
 
Vector getCol2 () const
 
Vector getCol3 () const
 
float get (unsigned int row, unsigned int col) const
 
Matrixset (float val, unsigned int row, unsigned int col)
 
Matrix getInverseMat2 () const
 
Matrix getInverseMat3 () const
 
Matrix getInverseMat4 () const
 
Matrix getTranspose () const
 
MatrixinverseMat2 ()
 
MatrixinverseMat3 ()
 
MatrixinverseMat4 ()
 
Matrixtranspose ()
 
Vector getPositionComponent () const
 
Matrix getOrientationComponent () const
 
Vector getScaleComponent () const
 
void getDecomposed (Vector &position, Matrix &orientation, Vector &scale) const
 
MatrixsetToViewMatrixDirection (const Vector &position, const Vector &direction, const Vector &upDirection)
 
MatrixsetToPerspectiveMatrix (float fov, float aspect, float nearVal, float farVal)
 
MatrixsetToOrthographicMatrix (float widthNearPlane, float heightNearPlane, float nearVal, float farVal)
 
MatrixsetPositionComponent (const Vector &position)
 
MatrixsetOrientationComponent (const Quaternion &rot)
 
MatrixsetScaleComponent (const Vector &scale)
 
MatrixsetToTransformation (const Vector &position, const Quaternion &orientation, const Vector &scale)
 
float getDeterminantMat2 () const
 
float getDeterminantMat3 () const
 
float getDeterminantMat4 () const
 
float getTraceMat2 () const
 
float getTraceMat3 () const
 
float getTraceMat4 () const
 
nkMemory::String toString () const
 
MatrixfromString (nkMemory::StringView str)
 
Matrixoperator= (const Matrix &other) noexcept
 
bool operator== (const Matrix &other)
 
bool operator!= (const Matrix &other)
 
Matrix operator+ (const Matrix &other) const
 
void operator+= (const Matrix &other)
 
Matrix operator- (const Matrix &other) const
 
void operator-= (const Matrix &other)
 
Matrix operator* (const Matrix &other) const
 
Vector operator* (const Vector &vec) const
 
Quaternion operator* (const Quaternion &q) const
 
Matrix operator* (float scalar) const
 
void operator*= (const Matrix &other)
 
void operator*= (float scalar)
 
Matrix operator/ (float scalar) const
 
void operator/= (float scalar)
 

Public Attributes

float _m [4][4]
 The matrix memory, considered in row-major order (_m[row][col]).
 

Detailed Description

Represents a 4x4 float matrix.

The matrix is considered as row-major.

Constructor & Destructor Documentation

◆ Matrix() [1/5]

nkMaths::Matrix::Matrix ( )
noexcept

Default constructor. Defaults to an identity matrix.

◆ Matrix() [2/5]

nkMaths::Matrix::Matrix ( float  m00,
float  m01,
float  m10,
float  m11 
)
noexcept

Matrix 2x2 constructor.

Parameters
m00The value for _m[0][0].
m01The value for _m[0][1].
m10The value for _m[1][0].
m11The value for _m[1][1].
Remarks
Parameters are given row per row. Other members will default to identity.

◆ Matrix() [3/5]

nkMaths::Matrix::Matrix ( float  m00,
float  m01,
float  m02,
float  m10,
float  m11,
float  m12,
float  m20,
float  m21,
float  m22 
)
noexcept

Matrix 3x3 constructor.

Parameters
m00The value for _m[0][0].
m01The value for _m[0][1].
m02The value for _m[0][2].
m10The value for _m[1][0].
m11The value for _m[1][1].
m12The value for _m[1][2].
m20The value for _m[2][0].
m21The value for _m[2][1].
m22The value for _m[2][2].
Remarks
Parameters are given row per row. Other members will default to identity.

◆ Matrix() [4/5]

nkMaths::Matrix::Matrix ( float  m00,
float  m01,
float  m02,
float  m03,
float  m10,
float  m11,
float  m12,
float  m13,
float  m20,
float  m21,
float  m22,
float  m23,
float  m30,
float  m31,
float  m32,
float  m33 
)
noexcept

Matrix 4x4 constructor.

Parameters
m00The value for _m[0][0].
m01The value for _m[0][1].
m02The value for _m[0][2].
m03The value for _m[0][3].
m10The value for _m[1][0].
m11The value for _m[1][1].
m12The value for _m[1][2].
m13The value for _m[1][3].
m20The value for _m[2][0].
m21The value for _m[2][1].
m22The value for _m[2][2].
m23The value for _m[2][3].
m30The value for _m[3][0].
m31The value for _m[3][1].
m32The value for _m[3][2].
m33The value for _m[3][3].
Remarks
Parameters are given row per row.

◆ Matrix() [5/5]

nkMaths::Matrix::Matrix ( const Matrix other)
noexcept

Copy constructor.

Parameters
otherThe matrix to copy from.

Member Function Documentation

◆ getRow0()

Vector nkMaths::Matrix::getRow0 ( ) const
Returns
The first row of the matrix, as a vector.

◆ getRow1()

Vector nkMaths::Matrix::getRow1 ( ) const
Returns
The second row of the matrix, as a vector.

◆ getRow2()

Vector nkMaths::Matrix::getRow2 ( ) const
Returns
The third row of the matrix, as a vector.

◆ getRow3()

Vector nkMaths::Matrix::getRow3 ( ) const
Returns
The fourth row of the matrix, as a vector.

◆ getCol0()

Vector nkMaths::Matrix::getCol0 ( ) const
Returns
The first column of the matrix, as a vector.

◆ getCol1()

Vector nkMaths::Matrix::getCol1 ( ) const
Returns
The second column of the matrix, as a vector.

◆ getCol2()

Vector nkMaths::Matrix::getCol2 ( ) const
Returns
The third column of the matrix, as a vector.

◆ getCol3()

Vector nkMaths::Matrix::getCol3 ( ) const
Returns
The fourth column of the matrix, as a vector.

◆ get()

float nkMaths::Matrix::get ( unsigned int  row,
unsigned int  col 
) const
Parameters
rowThe row coordinate of the member to retrieve, between [0, 3].
colThe col coordinate of the member to retrieve, between [0, 3].
Returns
The component requested (_m[row][col]) if available, 0 in all other cases.
Remarks
This function does a bound check. If you are in a safe environment and can index the array directly, it is advised to do so.

◆ set()

Matrix& nkMaths::Matrix::set ( float  val,
unsigned int  row,
unsigned int  col 
)
Parameters
valThe value to set in the matrix.
rowThe row coordinate to override, between [0, 3].
colThe col coordinate to override, between [0, 3].
Returns
Reference over caller.
Remarks
This function does a bound check. If you are in a safe environment and can index the array directly, it is advised to do so.

◆ getInverseMat2()

Matrix nkMaths::Matrix::getInverseMat2 ( ) const
Returns
The matrix's inverse, as if the matrix was a 2x2 matrix.

◆ getInverseMat3()

Matrix nkMaths::Matrix::getInverseMat3 ( ) const
Returns
The matrix's inverse, as if the matrix was a 3x3 matrix.

◆ getInverseMat4()

Matrix nkMaths::Matrix::getInverseMat4 ( ) const
Returns
The matrix's inverse, as if the matrix was a 4x4 matrix.

◆ getTranspose()

Matrix nkMaths::Matrix::getTranspose ( ) const
Returns
The transposed matrix.

◆ inverseMat2()

Matrix& nkMaths::Matrix::inverseMat2 ( )

Inverses the matrix, as if it was a 2x2 matrix.

Returns
The matrix the function has been called on, for operation chaining.

◆ inverseMat3()

Matrix& nkMaths::Matrix::inverseMat3 ( )

Inverses the matrix, as if it was a 3x3 matrix.

Returns
The matrix the function has been called on, for operation chaining.

◆ inverseMat4()

Matrix& nkMaths::Matrix::inverseMat4 ( )

Inverses the matrix, as if it was a 4x4 matrix.

Returns
The matrix the function has been called on, for operation chaining.

◆ transpose()

Matrix& nkMaths::Matrix::transpose ( )

Transposes the matrix.

Returns
The matrix the function has been called on, for operation chaining.

◆ getPositionComponent()

Vector nkMaths::Matrix::getPositionComponent ( ) const
Returns
The position component, considering the matrix as a 4x4 transformation matrix.
Remarks
The matrix being row major, this corresponds to the third column's first 3 members.

◆ getOrientationComponent()

Matrix nkMaths::Matrix::getOrientationComponent ( ) const
Returns
The orientation component, considering the matrix as a 4x4 transformation matrix.
Remarks
The matrix being row major, this corresponds to the internals 3x3 matrix, once the scale has been stripped out.

◆ getScaleComponent()

Vector nkMaths::Matrix::getScaleComponent ( ) const
Returns
The scale component, considering the matrix as a 4x4 transformation matrix.
Remarks
The matrix being row major, this corresponds to the lengths of each of the 3 first columns.

◆ getDecomposed()

void nkMaths::Matrix::getDecomposed ( Vector position,
Matrix orientation,
Vector scale 
) const

Decomposes the matrix into a set of translation, orientation, and scale, considering the matrix as a 4x4 transformation matrix.

Parameters
positionThe position, translation component to fill.
orientationThe orientation component to fill.
scaleThe scale component to fill.

◆ setToViewMatrixDirection()

Matrix& nkMaths::Matrix::setToViewMatrixDirection ( const Vector position,
const Vector direction,
const Vector upDirection 
)

Sets the matrix as a view matrix.

Parameters
positionThe eye position.
directionThe eye front direction.
upDirectionThe eye up direction.
Returns
Reference over caller.

◆ setToPerspectiveMatrix()

Matrix& nkMaths::Matrix::setToPerspectiveMatrix ( float  fov,
float  aspect,
float  nearVal,
float  farVal 
)

Sets the matrix as a perspective matrix.

Parameters
fovThe field of view, vertical, in radians.
aspectThe aspect ratio.
nearValThe near plane distance.
farValThe far plane distance.
Returns
Reference over caller.
Remarks
The function will map the z component to [-1, 1], in DirectX's style. Beware that OpenGL usually expects the projection's z to end between [0, 1].

◆ setToOrthographicMatrix()

Matrix& nkMaths::Matrix::setToOrthographicMatrix ( float  widthNearPlane,
float  heightNearPlane,
float  nearVal,
float  farVal 
)

Sets the matrix as an orthographic matrix.

Parameters
widthNearPlaneThe width of the matrix, in eye space distance.
heightNearPlaneThe height of the matrix, in eye space distance.
nearValThe near plane distance.
farValThe far plane distance.
Returns
Reference over caller.
Remarks
The function will map the z component to [-1, 1], in DirectX's style. Beware that OpenGL usually expects the projection's z to end between [0, 1].

◆ setPositionComponent()

Matrix& nkMaths::Matrix::setPositionComponent ( const Vector position)

Sets the position component in the matrix.

Parameters
positionThe position to set in the matrix.
Returns
Reference over caller.

◆ setOrientationComponent()

Matrix& nkMaths::Matrix::setOrientationComponent ( const Quaternion rot)

Sets the rotation component in the matrix.

Parameters
rotThe quaternion to use as orientation.
Returns
Reference over caller.

◆ setScaleComponent()

Matrix& nkMaths::Matrix::setScaleComponent ( const Vector scale)

Sets the scale component in the matrix.

Parameters
scaleThe scale on each axis.
Returns
Reference over caller.

◆ setToTransformation()

Matrix& nkMaths::Matrix::setToTransformation ( const Vector position,
const Quaternion orientation,
const Vector scale 
)

Sets the matrix as a full transformation matrix.

Parameters
positionThe position, translation component to bake.
orientationThe orientation component to bake.
scaleThe scale component to bake.
Returns
Reference over caller.

◆ getDeterminantMat2()

float nkMaths::Matrix::getDeterminantMat2 ( ) const
Returns
The determinant of the matrix, considering it as a 2x2 matrix.

◆ getDeterminantMat3()

float nkMaths::Matrix::getDeterminantMat3 ( ) const
Returns
The determinant of the matrix, considering it as a 3x3 matrix.

◆ getDeterminantMat4()

float nkMaths::Matrix::getDeterminantMat4 ( ) const
Returns
The determinant of the matrix, considering it as a 4x4 matrix.

◆ getTraceMat2()

float nkMaths::Matrix::getTraceMat2 ( ) const
Returns
The trace of the matrix, considering it as a 2x2 matrix.

◆ getTraceMat3()

float nkMaths::Matrix::getTraceMat3 ( ) const
Returns
The trace of the matrix, considering it as a 3x3 matrix.

◆ getTraceMat4()

float nkMaths::Matrix::getTraceMat4 ( ) const
Returns
The trace of the matrix, considering it as a 4x4 matrix.

◆ toString()

nkMemory::String nkMaths::Matrix::toString ( ) const
Returns
The matrix expressed as a string, formatted as "m00, m01, m02, m03, m10, ...".

◆ fromString()

Matrix& nkMaths::Matrix::fromString ( nkMemory::StringView  str)

Interprets a string to set the components of the matrix. Format is m00,m01,m02,m03,...

Parameters
strThe string to parse.
Returns
The updated matrix instance.

◆ operator=()

Matrix& nkMaths::Matrix::operator= ( const Matrix other)
noexcept

Assignment operator.

Parameters
otherThe matrix to assign.
Returns
The calling matrix, once assigned.

◆ operator==()

bool nkMaths::Matrix::operator== ( const Matrix other)

Equality operator.

Parameters
otherThe matrix to check against.
Returns
If the matrices are equal (true) or not (false).

◆ operator!=()

bool nkMaths::Matrix::operator!= ( const Matrix other)

Inequality operator.

Parameters
otherThe matrix to check against.
Returns
If the matrices are different (true) or not (false).

◆ operator+()

Matrix nkMaths::Matrix::operator+ ( const Matrix other) const

Addition operator.

Parameters
otherThe matrix to operate with.
Returns
A matrix resulting from the operation.

◆ operator+=()

void nkMaths::Matrix::operator+= ( const Matrix other)

Addition and assignment operator.

Parameters
otherThe matrix to operate with.

◆ operator-()

Matrix nkMaths::Matrix::operator- ( const Matrix other) const

Subtraction operator.

Parameters
otherThe matrix to operate with.
Returns
A matrix resulting from the operation.

◆ operator-=()

void nkMaths::Matrix::operator-= ( const Matrix other)

Subtraction and assignment operator.

Parameters
otherThe matrix to operate with.

◆ operator*() [1/4]

Matrix nkMaths::Matrix::operator* ( const Matrix other) const

Transformation operator.

Parameters
otherThe matrix to operate with.
Returns
A matrix resulting from the operation.

◆ operator*() [2/4]

Vector nkMaths::Matrix::operator* ( const Vector vec) const

Transformation operator.

Parameters
vecThe vector to apply to.
Returns
A vector resulting from the operation.

◆ operator*() [3/4]

Quaternion nkMaths::Matrix::operator* ( const Quaternion q) const

Transformation operator.

Parameters
qThe quaternion to apply to.
Returns
A quaternion resulting from the operation.
Remarks
This operation will only use the rotation component from the matrix to compose both rotations.

◆ operator*() [4/4]

Matrix nkMaths::Matrix::operator* ( float  scalar) const

Multiplication operator.

Parameters
scalarThe scalar to operate with.
Returns
A matrix resulting from the operation.

◆ operator*=() [1/2]

void nkMaths::Matrix::operator*= ( const Matrix other)

Transformation and assignment operator.

Parameters
otherThe matrix to operate with.

◆ operator*=() [2/2]

void nkMaths::Matrix::operator*= ( float  scalar)

Multiplication and assignment operator.

Parameters
scalarThe scalar to operate with.

◆ operator/()

Matrix nkMaths::Matrix::operator/ ( float  scalar) const

Division operator.

Parameters
scalarThe scalar to operate with.
Returns
A matrix resulting from the operation.

◆ operator/=()

void nkMaths::Matrix::operator/= ( float  scalar)

Addition and assignment operator.

Parameters
scalarThe scalar to operate with.

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