Files Class List
Matrix3< T > Class Template Reference

Detailed Description

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

3x3 matrix class.

Its data stores 9-value array of column-major matrix elements. You can use Matrix3 with Vec3<T> to transform 3D points.

#include <cyMatrix.h>

Public Member Functions

Constructors
 Matrix3 ()
 Default constructor.
 
template<typename TT >
 Matrix3 (Matrix3< TT > const &matrix)
 Copy constructor for different types.
 
 Matrix3 (T const *values)
 Initialize the matrix using an array of 9 values.
 
 Matrix3 (T v)
 Initialize the matrix as identity scaled by v.
 
 Matrix3 (Vec3< T > const &x, Vec3< T > const &y, Vec3< T > const &z)
 Initialize the matrix using x,y,z vectors as columns.
 
 Matrix3 (Matrix2< T > const &m)
 Default constructor.
 
 Matrix3 (Matrix34< T > const &m)
 Default constructor.
 
 Matrix3 (Matrix4< T > const &m)
 Default constructor.
 
 Matrix3 (T c00, T c01, T c02, T c10, T c11, T c12, T c20, T c21, T c22)
 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 9.
 
void Set (T const *values)
 Set matrix using an array of 9 values.
 
void Set (Vec3< T > const &x, Vec3< T > const &y, Vec3< T > const &z)
 Set matrix using x,y,z vectors as columns.
 
void SetIdentity ()
 Converts the matrix to an identity matrix.
 
void SetTensorProduct (Vec3< T > const &v0, Vec3< T > const &v1)
 Set all the values as zero.
 
void SetCrossProd (Vec3< T > const &p)
 Matrix representation of the cross product ( a x b)
 
Affine transformations
void SetScale (T uniformScale)
 Sets a uniform scale matrix.
 
void SetScale (T scaleX, T scaleY, T scaleZ)
 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 SetView (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)
 Sets a Cartesian coordinate frame using the given x direction and an approximate y direction. x must be a unit vector.
 
void SetCartesianFrameXZ (Vec3< T > const &x, Vec3< T > const &z_approx)
 Sets a Cartesian coordinate frame using the given x direction and an approximate z direction. x must be a unit vector.
 
void SetCartesianFrameYX (Vec3< T > const &y, Vec3< T > const &x_approx)
 Sets a Cartesian coordinate frame using the given y direction and an approximate x direction. y must be a unit vector.
 
void SetCartesianFrameYZ (Vec3< T > const &y, Vec3< T > const &z_approx)
 Sets a Cartesian coordinate frame using the given y direction and an approximate z direction. y must be a unit vector.
 
void SetCartesianFrameZX (Vec3< T > const &z, Vec3< T > const &x_approx)
 Sets a Cartesian coordinate frame using the given z direction and an approximate x direction. z must be a unit vector.
 
void SetCartesianFrameZY (Vec3< T > const &z, Vec3< T > const &y_approx)
 Sets a Cartesian coordinate frame using the given z direction and an approximate y direction. z must be a unit vector.
 
Set Row, Column, or Diagonal
void SetRow (int ri, T x, T y, T z)
 Sets a row of the matrix.
 
void SetRow (int ri, Vec3< T > const &v)
 Sets a row of the matrix.
 
void SetColumn (int ci, T x, T y, T z)
 Sets a column of the matrix.
 
void SetColumn (int ci, Vec3< T > const &v)
 Sets a column of the matrix.
 
void SetDiagonal (T xx, T yy, T zz)
 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 diagonal values of the matrix.
 
Get Row, Column, or Diagonal
Vec3< T > * Columns ()
 Returns a row of the matrix.
 
Vec3< T > const * Columns () const
 Returns a row of the matrix.
 
Vec3< T > & Column (int ci)
 Returns a row of the matrix.
 
Vec3< T > const & Column (int ci) const
 Returns a row of the matrix.
 
Vec3< T > GetRow (int ri) const
 Returns a row of the matrix.
 
Vec3< T > GetDiagonal () const
 Returns the diagonal of the matrix.
 
Matrix3 GetRotation () const
 Returns the rotation portion of the transformation.
 
Matrix3 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 (Matrix3< T > &scale, Matrix3< T > &rotation) const
 Returns separate transformation components.
 
Get Sub-matrix cell
Matrix2< T > GetSubMatrix2 () const
 Returns the 2x2 portion of the matrix.
 
Comparison Operators
bool operator== (Matrix3 const &right) const
 compare equal
 
bool operator!= (Matrix3 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
Matrix3 operator- () const
 negative matrix
 
Matrix3 operator* (T const value) const
 multiply matrix by a value
 
Matrix3 operator/ (T const value) const
 divide matrix by a value
 
Matrix3 operator+ (Matrix3 const &right) const
 add two Matrices
 
Matrix3 operator- (Matrix3 const &right) const
 subtract one Matrix3 from another
 
Matrix3 operator* (Matrix3 const &right) const
 < multiply a matrix with another
 
Vec3< T > operator* (Vec3< T > const &p) const
 negative matrix
 
Matrix3 operator+ (T value) const
 add a value times identity matrix
 
Matrix3 operator- (T value) const
 subtract a value times identity matrix
 
3D Transform Methods
void Transform (Vec3< T > &p) const
 Transforms the given vector by multiplying it with the matrix.
 
Assignment Operators
Matrix3 const & operator*= (Matrix3 const &right)
 multiply a matrix with another matrix and modify this matrix
 
Matrix3 const & operator+= (Matrix3 const &right)
 add two Matrices modify this
 
Matrix3 const & operator-= (Matrix3 const &right)
 subtract one Matrix3 from another matrix and modify this matrix
 
Matrix3 const & operator*= (T const value)
 multiply a matrix with a value modify this matrix
 
Matrix3 const & operator/= (T const value)
 divide the matrix by a value modify the this matrix
 
Matrix3 const & operator+= (T const value)
 add a value times identity matrix
 
Matrix3 const & operator-= (T const value)
 subtract a value times identity matrix
 
Other Methods
Matrix3 AddDiagonal (T xx, T yy, T zz) const
 Adds a diagonal matrix to this matrix and returns the result.
 
Matrix3 AddDiagonal (Vec3< T > const &diag) const
 Adds a diagonal matrix to this matrix and returns the result.
 
Matrix3 AddIdentity (T scale=T(1)) const
 Adds a scaled identity matrix to this matrix and returns the result.
 
void Transpose ()
 Adds a diagonal matrix to this matrix and returns the result.
 
Matrix3 GetTranspose () const
 < Return the transpose of this matrix
 
Vec3< T > TransposeMult (Vec3< T > const &p) const
 Multiply the give vector with the transpose of the matrix.
 
Matrix3 TransposeMult (Matrix3 const &right) const
 < Multiply a matrix by the transpose of this one (i.e. this^T * right).
 
Matrix3 MultTranspose (Matrix3 const &right) const
 < Multiply the transpose of a matrix by this one (i.e. this * right^T).
 
Matrix3 TransposeMultSelf () const
 Multiply the transpose of this matrix with itself (i.e. this^T * this).
 
Matrix3 MultSelfTranspose () const
 Multiply the matrix with its transpose (i.e. this * this^T).
 
GetTrace () const
 Adds a diagonal matrix to this matrix and returns the result.
 
GetDeterminant () const
 
void Invert ()
 Invert this matrix.
 
Matrix3 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.
 
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.
 
Vec3< T > GetEigenvalues (T tollerance=T(0.0001)) const
 Returns the eigenvalues of the matrix. The eigenvalues are ordered, such that the first one is the largest. The given tollerance value is used for checking whether the matrix is diagonal.
 
Vec3< T > GetEigenvectors (Vec3< T > &evec0, Vec3< T > &evec1, Vec3< T > &evec2, T tollerance=T(0.0001)) const
 Returns the eigenvalues and sets the given vector as the eigenvectors of the matrix. The eigenvalues are ordered, such that the first one is the largest. The given tollerance is used for checking whether the eigenvalues are the same.
 
void SingularValueDecomposition (Matrix3< T > &U, Vec3< T > &S, Matrix3< T > &V, T tollerance=T(0.0001))
 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. The given tollerance is used for checking whether the eigenvalues are the same.
 

Static Public Member Functions

Static Methods
static Matrix3 Identity ()
 Returns an identity matrix.
 
static Matrix3 View (Vec3< T > const &target, Vec3< T > const &up)
 Returns a view matrix using position, target and approximate up vector.
 
static Matrix3 RotationX (T angle)
 Returns a rotation matrix around x axis by angle in radians.
 
static Matrix3 RotationY (T angle)
 Returns a rotation matrix around y axis by angle in radians.
 
static Matrix3 RotationZ (T angle)
 Returns a rotation matrix around z axis by angle in radians.
 
static Matrix3 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 Matrix3 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 Matrix3 Rotation (Vec3< T > const &axis, T angle)
 Returns a rotation matrix about the given axis by angle in radians.
 
static Matrix3 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 Matrix3 Rotation (Vec3< T > const &from, Vec3< T > const &to)
 Returns a rotation matrix that sets [from] unit vector to [to] unit vector.
 
static Matrix3 Scale (T uniformScale)
 Returns a uniform scale matrix.
 
static Matrix3 Scale (T scaleX, T scaleY, T scaleZ)
 Returns a scale matrix.
 
static Matrix3 Scale (Vec3< T > const &scale)
 Returns a scale matrix.
 
static Matrix3 TensorProduct (Vec3< T > const &v0, Vec3< T > const &v1)
 Returns the tensor product (outer product) matrix of two vectors.
 
static Matrix3 MatrixCrossProd (Vec3< T > const &a)
 Returns the matrix representation of cross product ( a x b )
 

Public Attributes

cell [9]
 Elements of the matrix are column-major:
| 0 3 6 |
| 1 4 7 |
| 2 5 8 |
.
 

Friends

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

Member Function Documentation

◆ SetTensorProduct()

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

Set all the values as zero.

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

◆ GetDeterminant()

template<typename T >
T GetDeterminant ( ) const

< Get the determinant of this matrix