Sparkle 0.0.1
Loading...
Searching...
No Matches
spk::IMatrix< SizeX, SizeY > Class Template Reference

Column-major matrix with basic arithmetic, transforms, and helpers. More...

#include <spk_matrix.hpp>

Collaboration diagram for spk::IMatrix< SizeX, SizeY >:
Collaboration graph

Classes

class  Column
 View of the values belonging to a single matrix column. More...

Public Member Functions

 IMatrix ()
 Builds an identity matrix.
 IMatrix (float *p_values)
 Builds a matrix from a contiguous column-major array.
 IMatrix (std::initializer_list< float > p_values)
 Builds from a flat initializer list (column-major).
Columnoperator[] (size_t p_index)
 Accesses a column.
const Columnoperator[] (size_t p_index) const
 Accesses a column.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
Vector3 operator* (const Vector3 &p_vec) const
 Transforms a 3D vector with a 3x3 matrix (homogeneous w=1).
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
Vector3 operator* (const Vector3 &p_vec) const
 Transforms a 3D vector with a 4x4 matrix (homogeneous projection).
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
Vector2 operator* (const Vector2 &p_vec) const
 Transforms a 2D vector with a 3x3 matrix (homogeneous).
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
Vector4 operator* (const Vector4 &p_vec) const
 Transforms a 4D vector with a 4x4 matrix.
IMatrix< SizeX, SizeY > operator* (const IMatrix< SizeX, SizeY > &p_other) const
 Multiplies two matrices of the same dimensions.
bool operator== (const IMatrix &p_other) const
 Compares two matrices with tolerant float comparison.
bool operator!= (const IMatrix &p_other) const
 Inequality helper.
std::wstring toWstring () const
 Converts the matrix to a wide string representation.
std::string toString () const
 Converts the matrix to a string representation.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==Y), int > = 0>
float determinant () const
 Computes determinant for square matrices.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==Y), int > = 0>
bool isInvertible () const
 Checks if the matrix is invertible (determinant non-zero).
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==Y), int > = 0>
IMatrix< SizeX, SizeY > inverse () const
 Returns the inverse of a square matrix.

Static Public Member Functions

static spk::IMatrix< SizeX, SizeY > identity ()
 Returns an identity matrix of the same dimension.
static IMatrix< 4, 4 > rotation (float p_angleX, float p_angleY, float p_angleZ)
 Builds a 4x4 rotation matrix from Euler angles in degrees.
static IMatrix< 4, 4 > rotation (spk::Vector3 p_angle)
 Builds a 4x4 rotation matrix from Euler components in degrees.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
static IMatrix< 4, 4 > rotation (const spk::Quaternion &p_q)
 Converts a quaternion to a 4x4 rotation matrix.
static IMatrix< 4, 4 > translation (float p_translateX, float p_translateY, float p_translateZ)
 Builds a translation matrix.
static IMatrix< 4, 4 > translation (spk::Vector3 p_translation)
 Builds a translation matrix from a vector.
static IMatrix< 4, 4 > scale (float p_scaleX, float p_scaleY, float p_scaleZ)
 Builds a scale matrix.
static IMatrix< 4, 4 > scale (spk::Vector3 p_scale)
 Builds a scale matrix from a vector.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
static IMatrix lookAt (const spk::Vector3 &p_from, const spk::Vector3 &p_to, const spk::Vector3 &p_up)
 Builds a view matrix looking from one point to another.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
static IMatrix rotateAroundAxis (const spk::Vector3 &p_axis, const float &p_rotationAngle)
 Builds a rotation matrix around an arbitrary axis.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
static IMatrix perspective (float p_fov, float p_aspectRatio, float p_nearPlane, float p_farPlane)
 Builds a perspective projection matrix.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
static IMatrix ortho (float p_left, float p_right, float p_bottom, float p_top, float p_nearPlane, float p_farPlane)
 Builds an orthographic projection matrix.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
static IMatrix< 3, 3 > translation (float p_translateX, float p_translateY)
 Creates a 2D translation matrix.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
static IMatrix< 3, 3 > translation (const spk::Vector2 &p_translation)
 Creates a 2D translation matrix from a vector.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
static IMatrix< 3, 3 > scale (float p_scaleX, float p_scaleY)
 Creates a 2D scale matrix.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
static IMatrix< 3, 3 > scale (const spk::Vector2 &p_scale)
 Creates a 2D scale matrix from a vector.
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
static IMatrix< 3, 3 > rotationZ (float p_angleDegrees)
 Creates a 2D rotation matrix around Z axis.

Friends

std::wostream & operator<< (std::wostream &p_os, const IMatrix &p_mat)
 Streams the matrix to a wide output stream.
std::ostream & operator<< (std::ostream &p_os, const IMatrix &p_mat)
 Streams the matrix to a narrow output stream.

Detailed Description

template<size_t SizeX, size_t SizeY>
class spk::IMatrix< SizeX, SizeY >

Column-major matrix with basic arithmetic, transforms, and helpers.

Template Parameters
SizeXNumber of columns.
SizeYNumber of rows.
See also
Matrix2x2, Matrix3x3, Matrix4x4
spk::Matrix4x4 proj = spk::IMatrix<4,4>::perspective(60.f, 16.f/9.f, 0.1f, 100.f);
spk::Vector3 world = proj * spk::Vector3{1,2,3};
static IMatrix perspective(float p_fov, float p_aspectRatio, float p_nearPlane, float p_farPlane)
Builds a perspective projection matrix.
Definition spk_matrix.hpp:592

Constructor & Destructor Documentation

◆ IMatrix() [1/2]

template<size_t SizeX, size_t SizeY>
spk::IMatrix< SizeX, SizeY >::IMatrix ( float * p_values)
inline

Builds a matrix from a contiguous column-major array.

Parameters
p_valuesPointer to SizeX*SizeY float values ordered by column.

◆ IMatrix() [2/2]

template<size_t SizeX, size_t SizeY>
spk::IMatrix< SizeX, SizeY >::IMatrix ( std::initializer_list< float > p_values)
inline

Builds from a flat initializer list (column-major).

Parameters
p_valuesValues of size SizeX*SizeY.

Member Function Documentation

◆ determinant()

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==Y), int > = 0>
float spk::IMatrix< SizeX, SizeY >::determinant ( ) const
inline

Computes determinant for square matrices.

Returns
Determinant value.
Here is the caller graph for this function:

◆ identity()

template<size_t SizeX, size_t SizeY>
spk::IMatrix< SizeX, SizeY > spk::IMatrix< SizeX, SizeY >::identity ( )
inlinestatic

Returns an identity matrix of the same dimension.

Returns
Matrix with ones on the diagonal and zeros elsewhere.
Here is the caller graph for this function:

◆ inverse()

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==Y), int > = 0>
IMatrix< SizeX, SizeY > spk::IMatrix< SizeX, SizeY >::inverse ( ) const
inline

Returns the inverse of a square matrix.

Returns
Inverted matrix.
Exceptions
spk::Exceptionwhen singular.

◆ isInvertible()

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==Y), int > = 0>
bool spk::IMatrix< SizeX, SizeY >::isInvertible ( ) const
inline

Checks if the matrix is invertible (determinant non-zero).

Returns
true when invertible.

◆ lookAt()

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
IMatrix spk::IMatrix< SizeX, SizeY >::lookAt ( const spk::Vector3 & p_from,
const spk::Vector3 & p_to,
const spk::Vector3 & p_up )
inlinestatic

Builds a view matrix looking from one point to another.

Template Parameters
XCompile-time column count (requires 4).
YCompile-time row count (requires 4).
Parameters
p_fromEye position.
p_toTarget position.
p_upUp direction (not parallel to view direction).
Returns
View transform aligning the forward axis toward the target.

◆ operator!=()

template<size_t SizeX, size_t SizeY>
bool spk::IMatrix< SizeX, SizeY >::operator!= ( const IMatrix< SizeX, SizeY > & p_other) const
inline

Inequality helper.

Parameters
p_otherMatrix to compare against.
Returns
true when at least one coefficient differs.
See also
operator==

◆ operator*() [1/5]

template<size_t SizeX, size_t SizeY>
IMatrix< SizeX, SizeY > spk::IMatrix< SizeX, SizeY >::operator* ( const IMatrix< SizeX, SizeY > & p_other) const
inline

Multiplies two matrices of the same dimensions.

Parameters
p_otherRight-hand matrix.
Returns
Product matrix.

◆ operator*() [2/5]

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
Vector2 spk::IMatrix< SizeX, SizeY >::operator* ( const Vector2 & p_vec) const
inline

Transforms a 2D vector with a 3x3 matrix (homogeneous).

Template Parameters
Xcompile-time column count (requires 3x3).
Ycompile-time row count (requires 3x3).
Parameters
p_vec2D vector (z assumed 1).
Returns
Transformed vector.

◆ operator*() [3/5]

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
Vector3 spk::IMatrix< SizeX, SizeY >::operator* ( const Vector3 & p_vec) const
inline

Transforms a 3D vector with a 3x3 matrix (homogeneous w=1).

Parameters
p_vecInput vector.
Returns
Transformed vector.

◆ operator*() [4/5]

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
Vector3 spk::IMatrix< SizeX, SizeY >::operator* ( const Vector3 & p_vec) const
inline

Transforms a 3D vector with a 4x4 matrix (homogeneous projection).

Parameters
p_vecInput vector.
Returns
Transformed vector after perspective divide.

◆ operator*() [5/5]

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
Vector4 spk::IMatrix< SizeX, SizeY >::operator* ( const Vector4 & p_vec) const
inline

Transforms a 4D vector with a 4x4 matrix.

Parameters
p_vecInput vector.
Returns
Transformed vector.

◆ operator==()

template<size_t SizeX, size_t SizeY>
bool spk::IMatrix< SizeX, SizeY >::operator== ( const IMatrix< SizeX, SizeY > & p_other) const
inline

Compares two matrices with tolerant float comparison.

Parameters
p_otherMatrix to compare against.
Returns
true when every coefficient matches within SafeComparand tolerance.

◆ operator[]() [1/2]

template<size_t SizeX, size_t SizeY>
Column & spk::IMatrix< SizeX, SizeY >::operator[] ( size_t p_index)
inline

Accesses a column.

Parameters
p_indexColumn index.
Returns
Mutable column.

◆ operator[]() [2/2]

template<size_t SizeX, size_t SizeY>
const Column & spk::IMatrix< SizeX, SizeY >::operator[] ( size_t p_index) const
inline

Accesses a column.

Parameters
p_indexColumn index.
Returns
Const column.

◆ ortho()

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
IMatrix spk::IMatrix< SizeX, SizeY >::ortho ( float p_left,
float p_right,
float p_bottom,
float p_top,
float p_nearPlane,
float p_farPlane )
inlinestatic

Builds an orthographic projection matrix.

Template Parameters
XCompile-time column count (requires 4).
YCompile-time row count (requires 4).
Parameters
p_leftLeft view plane.
p_rightRight view plane.
p_bottomBottom view plane.
p_topTop view plane.
p_nearPlaneNear clipping plane distance.
p_farPlaneFar clipping plane distance.
Returns
Orthographic projection matrix.

◆ perspective()

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
IMatrix spk::IMatrix< SizeX, SizeY >::perspective ( float p_fov,
float p_aspectRatio,
float p_nearPlane,
float p_farPlane )
inlinestatic

Builds a perspective projection matrix.

Template Parameters
XCompile-time column count (requires 4).
YCompile-time row count (requires 4).
Parameters
p_fovField of view in degrees.
p_aspectRatioViewport aspect ratio (width/height).
p_nearPlaneNear clipping plane distance.
p_farPlaneFar clipping plane distance.
Returns
4x4 projection matrix suitable for homogeneous coordinates.

◆ rotateAroundAxis()

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
IMatrix spk::IMatrix< SizeX, SizeY >::rotateAroundAxis ( const spk::Vector3 & p_axis,
const float & p_rotationAngle )
inlinestatic

Builds a rotation matrix around an arbitrary axis.

Template Parameters
XCompile-time column count (requires 4).
YCompile-time row count (requires 4).
Parameters
p_axisAxis to rotate around (normalized internally).
p_rotationAngleRotation angle in degrees.
Returns
4x4 matrix representing the axis/angle rotation.

◆ rotation() [1/3]

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==4 &&Y==4), int > = 0>
IMatrix< 4, 4 > spk::IMatrix< SizeX, SizeY >::rotation ( const spk::Quaternion & p_q)
inlinestatic

Converts a quaternion to a 4x4 rotation matrix.

Template Parameters
XCompile-time column count (requires 4).
YCompile-time row count (requires 4).
Parameters
p_qUnit quaternion describing the rotation.
Returns
Rotation matrix equivalent to the quaternion.
See also
spk::Quaternion

◆ rotation() [2/3]

template<size_t SizeX, size_t SizeY>
IMatrix< 4, 4 > spk::IMatrix< SizeX, SizeY >::rotation ( float p_angleX,
float p_angleY,
float p_angleZ )
inlinestatic

Builds a 4x4 rotation matrix from Euler angles in degrees.

Parameters
p_angleXRotation around X axis in degrees.
p_angleYRotation around Y axis in degrees.
p_angleZRotation around Z axis in degrees.
Returns
Composition of Z * Y * X rotations.
See also
rotation(spk::Vector3), rotation(const spk::Quaternion &)
Here is the caller graph for this function:

◆ rotation() [3/3]

template<size_t SizeX, size_t SizeY>
IMatrix< 4, 4 > spk::IMatrix< SizeX, SizeY >::rotation ( spk::Vector3 p_angle)
inlinestatic

Builds a 4x4 rotation matrix from Euler components in degrees.

Parameters
p_angleVector containing (x, y, z) Euler rotations in degrees.
Returns
Rotation matrix matching the provided angles.
See also
rotation(float, float, float)

◆ rotationZ()

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
IMatrix< 3, 3 > spk::IMatrix< SizeX, SizeY >::rotationZ ( float p_angleDegrees)
inlinestatic

Creates a 2D rotation matrix around Z axis.

Parameters
p_angleDegreesRotation angle in degrees.
Returns
3x3 rotation matrix.

◆ scale() [1/4]

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
IMatrix< 3, 3 > spk::IMatrix< SizeX, SizeY >::scale ( const spk::Vector2 & p_scale)
inlinestatic

Creates a 2D scale matrix from a vector.

Parameters
p_scaleScale vector.
Returns
3x3 scale matrix.

◆ scale() [2/4]

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
IMatrix< 3, 3 > spk::IMatrix< SizeX, SizeY >::scale ( float p_scaleX,
float p_scaleY )
inlinestatic

Creates a 2D scale matrix.

Parameters
p_scaleXScale factor on X axis.
p_scaleYScale factor on Y axis.
Returns
3x3 scale matrix.

◆ scale() [3/4]

template<size_t SizeX, size_t SizeY>
IMatrix< 4, 4 > spk::IMatrix< SizeX, SizeY >::scale ( float p_scaleX,
float p_scaleY,
float p_scaleZ )
inlinestatic

Builds a scale matrix.

Parameters
p_scaleXScale factor along X axis.
p_scaleYScale factor along Y axis.
p_scaleZScale factor along Z axis.
Returns
4x4 matrix scaling coordinates by the provided factors.
See also
scale(spk::Vector3)
Here is the caller graph for this function:

◆ scale() [4/4]

template<size_t SizeX, size_t SizeY>
IMatrix< 4, 4 > spk::IMatrix< SizeX, SizeY >::scale ( spk::Vector3 p_scale)
inlinestatic

Builds a scale matrix from a vector.

Parameters
p_scaleScale factors along each axis.
Returns
Scale matrix.
See also
scale(float, float, float)

◆ toString()

template<size_t SizeX, size_t SizeY>
std::string spk::IMatrix< SizeX, SizeY >::toString ( ) const
inline

Converts the matrix to a string representation.

Returns
Human-readable matrix layout.

◆ toWstring()

template<size_t SizeX, size_t SizeY>
std::wstring spk::IMatrix< SizeX, SizeY >::toWstring ( ) const
inline

Converts the matrix to a wide string representation.

Returns
Human-readable matrix layout using wide characters.

◆ translation() [1/4]

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
IMatrix< 3, 3 > spk::IMatrix< SizeX, SizeY >::translation ( const spk::Vector2 & p_translation)
inlinestatic

Creates a 2D translation matrix from a vector.

Parameters
p_translationTranslation vector.
Returns
3x3 translation matrix.

◆ translation() [2/4]

template<size_t SizeX, size_t SizeY>
template<size_t X = SizeX, size_t Y = SizeY, typename std::enable_if_t<(X==3 &&Y==3), int > = 0>
IMatrix< 3, 3 > spk::IMatrix< SizeX, SizeY >::translation ( float p_translateX,
float p_translateY )
inlinestatic

Creates a 2D translation matrix.

Parameters
p_translateXTranslation on X axis.
p_translateYTranslation on Y axis.
Returns
3x3 translation matrix.

◆ translation() [3/4]

template<size_t SizeX, size_t SizeY>
IMatrix< 4, 4 > spk::IMatrix< SizeX, SizeY >::translation ( float p_translateX,
float p_translateY,
float p_translateZ )
inlinestatic

Builds a translation matrix.

Parameters
p_translateXOffset along X axis.
p_translateYOffset along Y axis.
p_translateZOffset along Z axis.
Returns
4x4 matrix translating coordinates by the provided offsets.
See also
translation(spk::Vector3)
Here is the caller graph for this function:

◆ translation() [4/4]

template<size_t SizeX, size_t SizeY>
IMatrix< 4, 4 > spk::IMatrix< SizeX, SizeY >::translation ( spk::Vector3 p_translation)
inlinestatic

Builds a translation matrix from a vector.

Parameters
p_translationOffsets along each axis.
Returns
Translation matrix.
See also
translation(float, float, float)

◆ operator<< [1/2]

template<size_t SizeX, size_t SizeY>
std::ostream & operator<< ( std::ostream & p_os,
const IMatrix< SizeX, SizeY > & p_mat )
friend

Streams the matrix to a narrow output stream.

Parameters
p_osDestination stream.
p_matMatrix to print.
Returns
Stream reference after write.

◆ operator<< [2/2]

template<size_t SizeX, size_t SizeY>
std::wostream & operator<< ( std::wostream & p_os,
const IMatrix< SizeX, SizeY > & p_mat )
friend

Streams the matrix to a wide output stream.

Parameters
p_osDestination stream.
p_matMatrix to print.
Returns
Stream reference after write.

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