Files Class List
Matrix2< T > Class Template Reference

Detailed Description

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

2x2 matrix class.

Its data stores 4-value array of column-major matrix elements. You can use Matrix2 with Vec2<T> to transform 2D points.

#include <cyMatrix.h>

Public Member Functions

Constructors
 Matrix2 () CY_CLASS_FUNCTION_DEFAULT template< typename TT > explicit Matrix2< T >(const Matrix2< TT > &matrix)
 < Default constructor More...
 
 Matrix2 (T const *values)
 Initialize the matrix using an array of 4 values.
 
 Matrix2 (T const v)
 Initialize the matrix as identity scaled by v.
 
 Matrix2 (Vec2< T > const &x, Vec2< T > const &y)
 Initialize the matrix using two vectors as columns.
 
 Matrix2 (Matrix3< T > const &m)
 < Default constructor More...
 
 Matrix2 (Matrix34< T > const &m)
 < Default constructor More...
 
 Matrix2 (Matrix4< T > const &m)
 < Default constructor More...
 
 Matrix2 (T c00, T c01, T c10, T c11)
 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 4.
 
void Set (T const *values)
 Set Matrix using an array of 4 values.
 
void Set (Vec2< T > const &x, Vec2< T > const &y)
 Set Matrix using two vectors as columns.
 
void SetIdentity ()
 Converts the matrix to an identity matrix.
 
void SetIdentity (T v)
 Converts the matrix to an identity matrix scaled by a scalar.
 
void SetTensorProduct (Vec2< T > const &v0, Vec2< T > const &v1)
 Set all the values as zero. More...
 
Affine transformations
void SetScale (T uniformScale)
 Sets a uniform scale matrix.
 
void SetScale (T scaleX, T scaleY)
 Sets a scale matrix.
 
void SetScale (Vec2< T > const &scale)
 Sets a scale matrix.
 
void SetRotation (T angle)
 Set a rotation matrix by angle.
 
void SetRotation (T sinAngle, T cosAngle)
 Set a rotation matrix by cos and sin of angle.
 
void SetCartesianFrameX (Vec2< T > const &x)
 Sets a Cartesian coordinate frame using the given x direction. x must be a unit vector.
 
void SetCartesianFrameY (Vec2< T > const &y)
 Sets a Cartesian coordinate frame using the given y direction. y must be a unit vector.
 
Set Row, Column, or Diagonal
void SetRow (int row, T x, T y)
 Sets a row of the matrix.
 
void SetRow (int row, Vec2< T > const &v)
 Sets a row of the matrix.
 
void SetColumn (int col, T x, T y)
 Sets a column of the matrix.
 
void SetColumn (int col, Vec2< T > const &v)
 Sets a column of the matrix.
 
void SetDiagonal (T const &xx, T const &yy)
 Sets the diagonal values of the matrix.
 
void SetDiagonal (Vec2< T > const &p)
 Sets the diagonal values of the matrix.
 
void SetDiagonal (T const *values)
 Sets the diagonal values of the matrix.
 
Get Row, Column, or Diagonal
Vec2< T > * Columns ()
 Returns a row of the matrix.
 
Vec2< T > const * Columns () const
 Returns a row of the matrix.
 
Vec2< T > & Column (int col)
 Returns a row of the matrix.
 
Vec2< T > const & Column (int col) const
 Returns a row of the matrix.
 
Vec2< T > * Columns ()
 Returns a row of the matrix.
 
Vec2< T > const * Columns () const
 Returns a row of the matrix.
 
Vec2< T > & Column (int col)
 Returns a row of the matrix.
 
Vec2< T > const & Column (int col) const
 Returns a row of the matrix.
 
Vec2< T > GetRow (int row) const
 Returns a row of the matrix.
 
Vec2< T > GetColumn (int col) const
 Returns a column of the matrix.
 
Vec2< T > GetDiagonal () const
 Returns the diagonal of the matrix.
 
Matrix2 GetRotation () const
 Returns the rotation portion of the transformation.
 
Matrix2 GetScale () const
 Returns the scale portion of the transformation. The returned matrix is symmetric, but not necessarily diagonal, and it can include non-uniform scale.
 
GetAvrgScale () const
 Returns the average scale factor.
 
void GetComponents (Matrix2< T > &scale, Matrix2< T > &rotation) const
 Returns separate transformation components.
 
Comparison Operators
bool operator== (Matrix2 const &right) const
 compare equal
 
bool operator!= (Matrix2 const &right) const
 compare not equal
 
Access Operators
T & operator() (int row, int col)
 subscript operator
 
T const & operator() (int row, int col) const
 constant subscript operator
 
T & operator[] (int i)
 subscript operator
 
T const & operator[] (int i) const
 constant subscript operator
 
Unary and Binary Operators
Matrix2 operator- () const
 negative matrix
 
Matrix2 operator* (T const value) const
 multiply matrix by a value
 
Matrix2 operator/ (T const value) const
 divide matrix by a value
 
Matrix2 operator+ (Matrix2 const &right) const
 add two Matrices
 
Matrix2 operator- (Matrix2 const &right) const
 subtract one Matrix2 from another
 
Matrix2 operator* (Matrix2 const &right) const
 < multiply a matrix with another
 
Vec2< T > operator* (Vec2< T > const &p) const
 negative matrix
 
Matrix2 operator+ (T const value) const
 add a value times identity matrix
 
Matrix2 operator- (T const value) const
 subtract a value times identity matrix
 
Assignment Operators
Matrix2 const & operator+= (Matrix2 const &right)
 add two Matrices modify this
 
Matrix2 const & operator-= (Matrix2 const &right)
 subtract one Matrix2 from another matrix and modify this matrix
 
Matrix2 const & operator*= (Matrix2 const &right)
 multiply a matrix with another matrix and modify this matrix
 
Matrix2 const & operator*= (T const value)
 multiply a matrix with a value modify this matrix
 
Matrix2 const & operator/= (T const value)
 divide the matrix by a value modify the this matrix
 
Matrix2 const & operator+= (T const value)
 add a value times identity matrix
 
Matrix2 const & operator-= (T const value)
 subtract a value times identity matrix
 
Other Methods
void Transpose ()
 Transpose this matrix.
 
void GetTranspose (Matrix2 &m) const
 < return Transpose of this matrix
 
Matrix2 GetTranspose () const
 return Transpose of this matrix
 
Vec2< T > TransposeMult (Vec2< T > const &p) const
 Multiply the give vector with the transpose of the matrix.
 
Matrix2 TransposeMult (Matrix2 const &right) const
 < Multiply a matrix by the transpose of this one (i.e. this^T * right).
 
Matrix2 MultTranspose (Matrix2 const &right) const
 < Multiply the transpose of a matrix by this one (i.e. this * right^T).
 
Matrix2 TransposeMultSelf () const
 Multiply the transpose of this matrix with itself (i.e. this^T * this).
 
Matrix2 MultSelfTranspose () const
 Multiply the matrix with its transpose (i.e. this * this^T).
 
GetTrace () const
 return the Trace of this matrix
 
GetDeterminant () const
 Get the determinant of this matrix.
 
void Invert ()
 Transpose this matrix.
 
void GetInverse (Matrix2 &inverse) const
 Get the inverse of this matrix.
 
Matrix2 GetInverse () const
 Get the inverse of this matrix.
 
void Normalize ()
 Removes the scale component of the matrix by normalizing each column. 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.
 
bool IsIdentity (T tollerance=T(_CY_VEC_DEFAULT_ERROR_TOLERANCE)) const
 Returns if the matrix is identity within the given error tollerance.
 
bool IsSymmetric (T tollerance=T(_CY_VEC_DEFAULT_ERROR_TOLERANCE)) const
 Returns if the matrix is symmetric within the given error tollerance.
 
bool IsDiagonal (T tollerance=T(_CY_VEC_DEFAULT_ERROR_TOLERANCE)) const
 Returns if the matrix is diagonal.
 
Vec2< T > GetEigenvalues () const
 Returns the eigenvalues of the matrix. The eigenvalues are ordered, such that the first one is larger.
 
Vec2< T > GetEigenvectors (Vec2< T > &evec0, Vec2< T > &evec1, T tollerance=T(_CY_VEC_DEFAULT_ERROR_TOLERANCE)) const
 Returns the eigenvalues and sets the given vectors as the eigenvectors of the matrix. The eigenvalues are ordered, such that the first one is larger. The given tollerance is used for checking whether the eigenvalues are the same.
 
void SingularValueDecomposition (Matrix2< T > &U, Vec2< T > &S, Matrix2< T > &V)
 Singular value decomposition (SVD). Returns the SVD of the matrix, where U and V are orthogonal matrices and S is the diagonal elements of a diagonal matrix (including zeros), such that this matrix A = U S V^T.
 

Static Public Member Functions

Static Methods
static Matrix2 Identity ()
 Returns an identity matrix.
 
static Matrix2 Rotation (T angle)
 Returns a rotation matrix about the given axis by angle in radians.
 
static Matrix2 Scale (T uniformScale)
 Returns a uniform scale matrix.
 
static Matrix2 Scale (T scaleX, T scaleY)
 Returns a scale matrix.
 
static Matrix2 Scale (Vec2< T > const &scale)
 Returns a scale matrix.
 
static Matrix2 TensorProduct (Vec2< T > const &v0, Vec2< T > const &v1)
 Returns the tensor product (outer product) matrix of two vectors.
 

Public Attributes

union {
   T   cell [4]
 
   Vec2< T >   column [2]
 
}; 
 Elements of the matrix are column-major:
| 0 2 |
| 1 3 |
.
 

Friends

Matrix2 operator* (T const value, Matrix2 const &right)
 multiply matrix by a value
 
Matrix2 operator+ (T const value, Matrix2 const &right)
 add a value times identity matrix to a matrix
 
Matrix2 operator- (T const value, Matrix2 const &right)
 subtract matrix from a value times identity matrix
 
Matrix2 Inverse (Matrix2 const &m)
 return the inverse of the matrix
 

Constructor & Destructor Documentation

◆ Matrix2() [1/4]

Matrix2 ( ) const &

< Default constructor

Copy constructor for different types

◆ Matrix2() [2/4]

Matrix2 ( Matrix3< T > const &  m)
explicit

< Default constructor

Copy constructor for different types

◆ Matrix2() [3/4]

Matrix2 ( Matrix34< T > const &  m)
explicit

< Default constructor

Copy constructor for different types

◆ Matrix2() [4/4]

Matrix2 ( Matrix4< T > const &  m)
explicit

< Default constructor

Copy constructor for different types

Member Function Documentation

◆ SetTensorProduct()

void SetTensorProduct ( Vec2< T > const &  v0,
Vec2< T > const &  v1 
)

Set all the values as zero.

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