Files Class List
Matrix4< T > Class Template Reference

Detailed Description

template<typename T>
class cy::Matrix4< T >

4x4 matrix class.

Its data stores 16-value array of column-major matrix elements. I chose column-major format to be compatible with OpenGL You can use Matrix4 with Vec3<T> and Vec4<T> to transform 3D and 4D points.

#include <cyMatrix.h>

Public Member Functions

Constructors
 Matrix4 ()
 Default constructor.
 
template<typename TT >
 Matrix4 (Matrix4< TT > const &matrix)
 Copy constructor for different types.
 
 Matrix4 (T const *values)
 Initialize the matrix using an array of 9 values.
 
 Matrix4 (T v)
 Initialize the matrix as identity scaled by v.
 
 Matrix4 (Vec3< T > const &x, Vec3< T > const &y, Vec3< T > const &z, Vec3< T > const &pos)
 Initialize the matrix using x,y,z vectors and coordinate center.
 
 Matrix4 (Vec4< T > const &x, Vec4< T > const &y, Vec4< T > const &z, Vec4< T > const &w)
 Initialize the matrix using x,y,z vectors as columns.
 
 Matrix4 (Matrix34< T > const &m)
 Default constructor.
 
 Matrix4 (Matrix3< T > const &m)
 Default constructor.
 
 Matrix4 (Matrix2< T > const &m)
 Default constructor.
 
 Matrix4 (Matrix3< T > const &m, Vec3< T > const &pos)
 Default constructor.
 
 Matrix4 (T c00, T c01, T c02, T c03, T c10, T c11, T c12, T c13, T c20, T c21, T c22, T c23, T c30, T c31, T c32, T c33)
 Constructor using row-major order for initialization.
 
Set & Get Methods
void Zero ()
 Set all the values as zero.
 
bool IsZero () const
 Returns true if the matrix is exactly zero.
 
bool IsFinite () const
 Returns true if all components are finite real numbers.
 
void Get (T *values)
 Copies the matrix cell to the given values array of size 16.
 
void Set (T const *values)
 Set Matrix using an array of 16 values.
 
void Set (Vec3< T > const &x, Vec3< T > const &y, Vec3< T > const &z, Vec3< T > const &pos)
 Set matrix using x,y,z column vectors and coordinate center.
 
void Set (Vec4< T > const &x, Vec4< T > const &y, Vec4< T > const &z, Vec4< T > const &w)
 Set matrix using x,y,z,w column vectors.
 
void SetIdentity ()
 Converts the matrix to an identity matrix.
 
void SetTensorProduct (Vec4< T > const &v0, Vec4< T > const &v1)
 Set all the values as zero.
 
Affine transformations
void SetScale (T uniformScale)
 Sets a uniform scale matrix.
 
void SetScale (T scaleX, T scaleY, T scaleZ, T scaleW=T(1))
 Sets a scale matrix.
 
void SetScale (Vec3< T > const &scale)
 Sets a scale matrix.
 
void SetRotationX (T angle)
 Set as rotation matrix around x axis.
 
void SetRotationX (T sinAngle, T cosAngle)
 Set as rotation matrix around x axis by cos and sin of angle.
 
void SetRotationY (T angle)
 Set as rotation matrix around y axis.
 
void SetRotationY (T sinAngle, T cosAngle)
 Set as rotation matrix around y axis by cos and sin of angle.
 
void SetRotationZ (T angle)
 Set as rotation matrix around z axis.
 
void SetRotationZ (T sinAngle, T cosAngle)
 Set as rotation matrix around z axis by cos and sin of angle.
 
void SetRotationXYZ (T angleX, T angleY, T angleZ)
 Set as rotation matrix around x, y, and then z axes ( Rz * Ry * Rx )
 
void SetRotationZYX (T angleX, T angleY, T angleZ)
 Set as rotation matrix around z, y, and then x axes ( Rx * Ry * Rz )
 
void SetRotation (Vec3< T > const &axis, T angle)
 Set a rotation matrix about the given axis by angle.
 
void SetRotation (Vec3< T > const &axis, T sinAngle, T cosAngle)
 Set a rotation matrix about the given axis by cos and sin of angle.
 
void SetRotation (Vec3< T > const &from, Vec3< T > const &to)
 Set a rotation matrix that sets [from] unit vector to [to] unit vector.
 
void SetTranslation (Vec3< T > const &move)
 Sets a translation matrix with no rotation or scale.
 
void AddTranslation (Vec3< T > const &move)
 Adds a translation to the matrix.
 
void SetTranslationComponent (Vec3< T > const &move)
 Sets the translation component of the matrix.
 
void SetNoTranslation ()
 Sets the translation component of the matrix to zero.
 
void SetView (Vec3< T > const &pos, Vec3< T > const &target, Vec3< T > const &up)
 Set view matrix using position, target and approximate up vector.
 
void SetCartesianFrameXY (Vec3< T > const &x, Vec3< T > const &y_approx, Vec3< T > const &trans=Vec3< T >(T(0), T(0), T(0)))
 Sets a Cartesian coordinate frame using the given x direction, an approximate y direction, and a translation. x must be a unit vector.
 
void SetCartesianFrameXZ (Vec3< T > const &x, Vec3< T > const &z_approx, Vec3< T > const &trans=Vec3< T >(T(0), T(0), T(0)))
 Sets a Cartesian coordinate frame using the given x direction, an approximate z direction, and a translation. x must be a unit vector.
 
void SetCartesianFrameYX (Vec3< T > const &y, Vec3< T > const &x_approx, Vec3< T > const &trans=Vec3< T >(T(0), T(0), T(0)))
 Sets a Cartesian coordinate frame using the given y direction, an approximate x direction, and a translation. y must be a unit vector.
 
void SetCartesianFrameYZ (Vec3< T > const &y, Vec3< T > const &z_approx, Vec3< T > const &trans=Vec3< T >(T(0), T(0), T(0)))
 Sets a Cartesian coordinate frame using the given y direction, an approximate z direction, and a translation. y must be a unit vector.
 
void SetCartesianFrameZX (Vec3< T > const &z, Vec3< T > const &x_approx, Vec3< T > const &trans=Vec3< T >(T(0), T(0), T(0)))
 Sets a Cartesian coordinate frame using the given z direction, an approximate x direction, and a translation. z must be a unit vector.
 
void SetCartesianFrameZY (Vec3< T > const &z, Vec3< T > const &y_approx, Vec3< T > const &trans=Vec3< T >(T(0), T(0), T(0)))
 Sets a Cartesian coordinate frame using the given z direction, an approximate y direction, and a translation. z must be a unit vector.
 
void SetPerspective (T fov, T aspect, T znear, T zfar)
 Set a project matrix with field of view in radians.
 
void SetPerspectiveTan (T tan_fov_2, T aspect, T znear, T zfar)
 Set a project matrix with the tangent of the half field of view (tan_fov_2)
 
Set Row, Column, or Diagonal
void SetRow (int ri, T x, T y, T z, T w)
 Sets a row of the matrix.
 
void SetRow (int ri, Vec4< T > const &v)
 Sets a row of the matrix.
 
void SetColumn (int ci, T x, T y, T z, T w)
 Sets a column of the matrix.
 
void SetColumn (int ci, Vec4< T > const &v)
 Sets a column of the matrix.
 
void SetDiagonal (T xx, T yy, T zz, T ww=1)
 Sets the diagonal values of the matrix.
 
void SetDiagonal (Vec4< T > const &p)
 Sets the diagonal values of the matrix.
 
void SetDiagonal (Vec3< T > const &p)
 Sets the diagonal values of the matrix.
 
void SetDiagonal (T const *values)
 Sets the 4 diagonal values of the matrix.
 
Get Row, Column, or Diagonal
Vec4< T > * Columns ()
 Returns a row of the matrix.
 
Vec4< T > const * Columns () const
 Returns a row of the matrix.
 
Vec4< T > & Column (int ci)
 Returns a row of the matrix.
 
Vec4< T > const & Column (int ci) const
 Returns a row of the matrix.
 
Vec3< T > & Column3 (int ci)
 Returns a row of the matrix.
 
Vec3< T > const & Column3 (int ci) const
 Returns a row of the matrix.
 
Vec4< T > GetRow (int ri) const
 Returns a row of the matrix.
 
Vec4< T > GetDiagonal () const
 Returns the diagonal of the matrix.
 
Get Sub-matrix cell
Matrix34< T > GetSubMatrix34 () const
 Returns the 3x4 portion of the matrix.
 
Matrix3< T > GetSubMatrix3 () const
 Returns the 3x3 portion of the matrix.
 
Matrix2< T > GetSubMatrix2 () const
 Returns the 2x2 portion of the matrix.
 
Vec3< T > GetTranslation () const
 Returns the translation component of the matrix.
 
Matrix3< T > GetRotation () const
 Returns the rotation portion of the transformation.
 
Matrix3< T > GetScale () const
 Returns the scale portion of the transformation.
 
GetAvrgScale () const
 Returns the average scale factor of the 3 by 3 sub-matrix.
 
void GetComponents (Matrix3< T > &scale, Matrix3< T > &rotation, Vec3< T > &translation) const
 Returns separate transformation components.
 
Comparison Operators
bool operator== (Matrix4 const &right) const
 compare equal
 
bool operator!= (Matrix4 const &right) const
 compare not equal
 
Access Operators
T & operator() (int ri, int ci)
 subscript operator
 
T const & operator() (int ri, int ci) const
 constant subscript operator
 
T & operator[] (int i)
 subscript operator
 
T const & operator[] (int i) const
 constant subscript operator
 
Unary and Binary Operators
Matrix4 operator- () const
 negative matrix
 
Matrix4 operator* (T const value) const
 multiply matrix by a value
 
Matrix4 operator/ (T const value) const
 divide matrix by a value
 
Matrix4 operator+ (Matrix4 const &right) const
 add two Matrices
 
Matrix4 operator- (Matrix4 const &right) const
 subtract one Matrix4 from another
 
Matrix4 operator* (Matrix4 const &right) const
 < multiply a matrix with another
 
Matrix4 operator* (Matrix34< T > const &right) const
 < multiply a matrix with another
 
Matrix4 operator* (Matrix3< T > const &right) const
 < multiply a matrix with another
 
Vec4< T > operator* (Vec3< T > const &p) const
 negative matrix
 
Vec4< T > operator* (Vec4< T > const &p) const
 negative matrix
 
Transform Methods
void Transform (Vec4< T > &p) const
 Transforms the given vector by multiplying it with the matrix.
 
Vec4< T > VectorTransform (Vec3< T > const &p) const
 Transforms the vector by multiplying it with the matrix, ignoring the translation component.
 
Assignment Operators
Matrix4 const & operator+= (Matrix4 const &right)
 add two Matrices modify this
 
Matrix4 const & operator-= (Matrix4 const &right)
 subtract one Matrix4 from another matrix and modify this matrix
 
Matrix4 const & operator*= (Matrix4 const &right)
 multiply a matrix with another matrix and modify this matrix
 
Matrix4 const & operator*= (Matrix34< T > const &right)
 multiply a matrix with another matrix and modify this matrix
 
Matrix4 const & operator*= (Matrix3< T > const &right)
 multiply a matrix with another matrix and modify this matrix
 
Matrix4 const & operator*= (T const value)
 multiply a matrix with a value modify this matrix
 
Matrix4 const & operator/= (T const value)
 divide the matrix by a value modify the this matrix
 
Other Methods
void Transpose ()
 < Return the transpose of this matrix
 
Matrix4 GetTranspose () const
 < Return the transpose of this matrix
 
Vec4< T > TransposeMult (Vec3< T > const &p) const
 Multiply the give vector with the transpose of the matrix.
 
Vec4< T > TransposeMult (Vec4< T > const &p) const
 Multiply the give vector with the transpose of the matrix.
 
Matrix4 TransposeMult (Matrix4 const &right) const
 < Multiply a matrix by the transpose of this one (i.e. this^T * right).
 
Matrix4 MultTranspose (Matrix4 const &right) const
 < Multiply the transpose of a matrix by this one (i.e. this * right^T).
 
Matrix4 TransposeMultSelf () const
 Multiply the transpose of this matrix with itself (i.e. this^T * this).
 
Matrix4 MultSelfTranspose () const
 Multiply the matrix with its transpose (i.e. this * this^T).
 
GetTrace () const
 < Return the transpose of this matrix
 
GetDeterminant () const
 < Get the determinant of this matrix
 
void Invert ()
 Invert this matrix.
 
Matrix4 GetInverse () const
 < Get the inverse of this matrix
 
void Normalize ()
 Removes the scale component of the matrix by normalizing each column of the 3x3 sub-matrix. The resulting matrix can contain shear, if it originally contained non-uniform scale and rotation.
 
void OrthogonalizeX ()
 Orthogonalizes the matrix and removes the scale component, preserving the x direction.
 
void OrthogonalizeY ()
 Orthogonalizes the matrix and removes the scale component, preserving the y direction.
 
void OrthogonalizeZ ()
 Orthogonalizes the matrix and removes the scale component, preserving the z direction.
 
bool IsIdentity (T tollerance=T(0.0001)) const
 Returns if the matrix is identity within the given error tollerance.
 
bool IsSymmetric (T tollerance=T(0.0001)) const
 Returns if the matrix is symmetric within the given error tollerance.
 
bool IsDiagonal (T tollerance=T(0.0001)) const
 Returns if the matrix is diagonal.
 

Static Public Member Functions

Static Methods
static Matrix4 Identity ()
 Returns an identity matrix.
 
static Matrix4 View (Vec3< T > const &pos, Vec3< T > const &target, Vec3< T > const &up)
 Returns a view matrix using position, target and approximate up vector.
 
static Matrix4 RotationX (T angle)
 Returns a rotation matrix around x axis by angle in radians.
 
static Matrix4 RotationY (T angle)
 Returns a rotation matrix around y axis by angle in radians.
 
static Matrix4 RotationZ (T angle)
 Returns a rotation matrix around z axis by angle in radians.
 
static Matrix4 Rotation (Vec3< T > const &axis, T angle)
 Returns a rotation matrix about the given axis by angle in radians.
 
static Matrix4 Rotation (Vec3< T > const &axis, T cosAngle, T sinAngle)
 Returns a rotation matrix about the given axis by cos and sin of the rotation angle.
 
static Matrix4 Rotation (Vec3< T > const &from, Vec3< T > const &to)
 Returns a rotation matrix that sets [from] unit vector to [to] unit vector.
 
static Matrix4 RotationXYZ (T angleX, T angleY, T angleZ)
 Returns a rotation matrix around x, y, and then z axes by angle in radians (Rz * Ry * Rx)
 
static Matrix4 RotationZYX (T angleX, T angleY, T angleZ)
 Returns a rotation matrix around z, y, and then x axes by angle in radians (Rx * Ry * Rz)
 
static Matrix4 Scale (T uniformScale)
 Returns a uniform scale matrix.
 
static Matrix4 Scale (T scaleX, T scaleY, T scaleZ, T scaleW=T(1))
 Returns a scale matrix.
 
static Matrix4 Scale (Vec3< T > const &scale)
 Returns a scale matrix.
 
static Matrix4 Translation (Vec3< T > const &move)
 Returns a translation matrix with no rotation or scale.
 
static Matrix4 Perspective (T fov, T aspect, T znear, T zfar)
 Returns a project matrix with field of view in radians.
 
static Matrix4 PerspectiveTan (T tan_fov_2, T aspect, T znear, T zfar)
 Returns a project matrix with the tangent of the half field of view (tan_fov_2)
 
static Matrix4 TensorProduct (Vec4< T > const &v0, Vec4< T > const &v1)
 Returns the tensor product (outer product) matrix of two vectors.
 

Public Attributes

cell [16]
 Elements of the matrix are column-major:
| 0 4 8 12 |
| 1 5 9 13 |
| 2 6 10 14 |
| 3 7 11 15 |
.
 

Friends

Matrix4 operator* (T value, Matrix4 const &right)
 multiply matrix by a value
 
Matrix4 Inverse (Matrix4 const &m)
 return the inverse of the matrix
 
Matrix4 operator* (Matrix34< T > const &left, Matrix4 const &right)
 multiply a 4x4 matrix with a 3x4 matrix, treating it as a 4x4 matrix with last row 0,0,0,1
 

Member Function Documentation

◆ SetTensorProduct()

template<typename T >
void SetTensorProduct ( Vec4< T > const & v0,
Vec4< T > const & v1 )

Set all the values as zero.

Parameters
v1Sets the matrix as the tensor product (outer product) of two vectors