Easy3D 2.6.1
Loading...
Searching...
No Matches
Mat< N, M, T > Class Template Reference

Base class for matrix types. More...

#include <easy3d/core/mat.h>

Inheritance diagram for Mat< N, M, T >:
Mat2< float > Mat2< double > Mat3< float > Mat3< double > Mat4< float > Mat4< double >

Public Member Functions

 Mat ()=default
 Default constructor.
 
 Mat (T s)
 Constructs a matrix with diagonal elements set to s and other elements set to zero.
 
template<size_t rN, size_t rM>
 Mat (const Mat< rN, rM, T > &rhs)
 Copy constructor for matrices with different dimensions (rN >= N, rM >= M).
 
 Mat (const T *m)
 Constructs a matrix from an array of elements.
 
Vec< M, T > row (size_t row) const
 Returns the specified row as a vector.
 
Vec< N, T > col (size_t col) const
 Returns the specified column as a vector.
 
const T & operator() (size_t row, size_t col) const
 Returns a constant reference to the element at the specified row and column.
 
T & operator() (size_t row, size_t col)
 Returns a reference to the element at the specified row and column.
 
 operator const T * () const
 Returns a constant pointer to the matrix elements.
 
 operator T* ()
 Returns a pointer to the matrix elements.
 
void load_zero ()
 Sets all elements to zero.
 
void load_identity (T s=T(1))
 Sets the diagonal elements to s and other elements to zero.
 
template<size_t vN>
void set_row (size_t row, const Vec< vN, T > &v)
 Sets the specified row from a vector.
 
template<size_t vN>
void set_col (size_t col, const Vec< vN, T > &v)
 Sets the specified column from a vector.
 
void swap_rows (size_t a, size_t b)
 Swaps the specified rows.
 
void swap_cols (size_t a, size_t b)
 Swaps the specified columns.
 
bool operator== (const Mat< N, M, T > &rhs) const
 Checks if the matrix is equal to another matrix.
 
bool operator!= (const Mat< N, M, T > &rhs) const
 Checks if the matrix is not equal to another matrix.
 
template<size_t rM>
Mat< N, rM, T > operator* (const Mat< M, rM, T > &rhs) const
 Multiplies the matrix by another matrix.
 
Mat< N, M, T > operator+ (const Mat< N, M, T > &rhs) const
 Adds the matrix to another matrix. Component-wise matrix-matrix addition.
 
Mat< N, M, T > operator- (const Mat< N, M, T > &rhs) const
 Subtracts another matrix from the matrix. Component-wise matrix-matrix subtraction.
 
Mat< N, M, T > operator- () const
 Negates the matrix. Component-wise matrix negation.
 
Vec< N, T > operator* (const Vec< M, T > &rhs) const
 Multiplies the matrix by a vector.
 
Mat< N, M, T > operator* (T rhs) const
 Multiplies the matrix by a scalar.
 
Mat< N, M, T > operator/ (T rhs) const
 Divides the matrix by a scalar.
 
Mat< N, M, T > & operator*= (const Mat< N, M, T > &rhs)
 Multiplies the matrix by another matrix and assigns the result to the matrix.
 
Mat< N, M, T > & operator+= (const Mat< N, M, T > &rhs)
 Adds another matrix to the matrix and assigns the result to the matrix.
 
Mat< N, M, T > & operator-= (const Mat< N, M, T > &rhs)
 Subtracts another matrix from the matrix and assigns the result to the matrix.
 
Mat< N, M, T > & operator*= (T rhs)
 Multiplies the matrix by a scalar and assigns the result to the matrix.
 
Mat< N, M, T > & operator/= (T rhs)
 Divides the matrix by a scalar and assigns the result to the matrix.
 
Mat< N, M, T > & operator+= (T rhs)
 Adds a scalar to the matrix and assigns the result to the matrix.
 
Mat< N, M, T > & operator-= (T rhs)
 Subtracts a scalar from the matrix and assigns the result to the matrix.
 

Static Public Member Functions

static Mat< N, M, T > identity ()
 Returns an identity matrix.
 
static size_t num_rows ()
 Returns the number of rows in the matrix.
 
static size_t num_columns ()
 Returns the number of columns in the matrix.
 

Detailed Description

template<size_t N, size_t M, typename T>
class easy3d::Mat< N, M, T >

Base class for matrix types.

Mat is a base matrix class that provides generic functionality for N by M matrices.

Template Parameters
NThe number of rows in this matrix.
MThe number of columns in this matrix.
TThe scalar type for matrix elements.
Note
Matrices are stored internally as column-major unless MATRIX_ROW_MAJOR is defined.
Todo
Add a transform() method or overload operator* to allow matrices to transform vectors that are M-1 in size, as vectors in homogeneous space.
See also
Mat2, Mat3, and Mat4

Constructor & Destructor Documentation

◆ Mat() [1/4]

template<size_t N, size_t M, typename T>
Mat ( )
default

Default constructor.

Note
The matrix elements are intentionally not initialized. This is efficient if the user assigns their values from subsequent computations. Use Mat(T s) to initialize the elements during construction.

◆ Mat() [2/4]

template<size_t N, size_t M, typename T>
Mat ( T s)
explicit

Constructs a matrix with diagonal elements set to s and other elements set to zero.

Parameters
sThe value to set the diagonal elements to.

◆ Mat() [3/4]

template<size_t N, size_t M, typename T>
template<size_t rN, size_t rM>
Mat ( const Mat< rN, rM, T > & rhs)
explicit

Copy constructor for matrices with different dimensions (rN >= N, rM >= M).

Template Parameters
rNThe number of rows in the source matrix.
rMThe number of columns in the source matrix.
Parameters
rhsThe source matrix to copy from.
Attention
This function does not support smaller incoming matrices (i.e, rN < N, rM < M ).
Todo
Can this ever actually get called? A templated constructor for a templated class seems dodgy!

◆ Mat() [4/4]

template<size_t N, size_t M, typename T>
Mat ( const T * m)
explicit

Constructs a matrix from an array of elements.

Parameters
mThe array of elements.

Member Function Documentation

◆ col()

template<size_t N, size_t M, typename T>
Vec< N, T > col ( size_t col) const

Returns the specified column as a vector.

Parameters
colThe column index.
Returns
The column as a vector.
Examples
Tutorial_207_RealCamera/main.cpp.

◆ identity()

template<size_t N, size_t M, typename T>
Mat< N, M, T > identity ( )
static

Returns an identity matrix.

Returns
An N x M identity matrix.
See also
load_identity()
Examples
Tutorial_203_Viewer_wxWidgets/main.cpp, and Tutorial_204_Viewer_Qt/main.cpp.

◆ load_identity()

template<size_t N, size_t M, typename T>
void load_identity ( T s = T(1))

Sets the diagonal elements to s and other elements to zero.

Parameters
sThe value to set the diagonal elements to.

◆ num_columns()

template<size_t N, size_t M, typename T>
static size_t num_columns ( )
inlinestatic

Returns the number of columns in the matrix.

Returns
The number of columns.

◆ num_rows()

template<size_t N, size_t M, typename T>
static size_t num_rows ( )
inlinestatic

Returns the number of rows in the matrix.

Returns
The number of rows.

◆ operator const T *()

template<size_t N, size_t M, typename T>
operator const T * ( ) const

Returns a constant pointer to the matrix elements.

Returns
A constant pointer to the elements.

◆ operator T*()

template<size_t N, size_t M, typename T>
operator T* ( )

Returns a pointer to the matrix elements.

Returns
A pointer to the elements.

◆ operator!=()

template<size_t N, size_t M, typename T>
bool operator!= ( const Mat< N, M, T > & rhs) const

Checks if the matrix is not equal to another matrix.

Parameters
rhsThe matrix to compare with.
Returns
True if the matrices are not equal, false otherwise.

◆ operator()() [1/2]

template<size_t N, size_t M, typename T>
T & operator() ( size_t row,
size_t col )

Returns a reference to the element at the specified row and column.

Parameters
rowThe row index.
colThe column index.
Returns
A reference to the element.

◆ operator()() [2/2]

template<size_t N, size_t M, typename T>
const T & operator() ( size_t row,
size_t col ) const

Returns a constant reference to the element at the specified row and column.

Parameters
rowThe row index.
colThe column index.
Returns
A constant reference to the element.

◆ operator*() [1/3]

template<size_t N, size_t M, typename T>
template<size_t rM>
Mat< N, rM, T > operator* ( const Mat< M, rM, T > & rhs) const

Multiplies the matrix by another matrix.

Template Parameters
rMThe number of columns in the right-hand side matrix.
Parameters
rhsThe right-hand side matrix. It must have the same number of rows as this matrix has columns.
Returns
The result of the multiplication, which is an N x rM matrix.

◆ operator*() [2/3]

template<size_t N, size_t M, typename T>
Vec< N, T > operator* ( const Vec< M, T > & rhs) const

Multiplies the matrix by a vector.

Parameters
rhsThe right-hand side vector. It must have the same number of elements as this matrix has columns.
Returns
The result of the multiplication.

◆ operator*() [3/3]

template<size_t N, size_t M, typename T>
Mat< N, M, T > operator* ( T rhs) const

Multiplies the matrix by a scalar.

Parameters
rhsThe scalar value.
Returns
The result of the multiplication.

◆ operator*=() [1/2]

template<size_t N, size_t M, typename T>
Mat< N, M, T > & operator*= ( const Mat< N, M, T > & rhs)

Multiplies the matrix by another matrix and assigns the result to the matrix.

Parameters
rhsThe right-hand side matrix.
Returns
A reference to the matrix.

◆ operator*=() [2/2]

template<size_t N, size_t M, typename T>
Mat< N, M, T > & operator*= ( T rhs)

Multiplies the matrix by a scalar and assigns the result to the matrix.

Parameters
rhsThe scalar value.
Returns
A reference to the matrix.

◆ operator+()

template<size_t N, size_t M, typename T>
Mat< N, M, T > operator+ ( const Mat< N, M, T > & rhs) const

Adds the matrix to another matrix. Component-wise matrix-matrix addition.

Parameters
rhsThe right-hand side matrix.
Returns
The result of the addition.

◆ operator+=() [1/2]

template<size_t N, size_t M, typename T>
Mat< N, M, T > & operator+= ( const Mat< N, M, T > & rhs)

Adds another matrix to the matrix and assigns the result to the matrix.

Parameters
rhsThe right-hand side matrix.
Returns
A reference to the matrix.

◆ operator+=() [2/2]

template<size_t N, size_t M, typename T>
Mat< N, M, T > & operator+= ( T rhs)

Adds a scalar to the matrix and assigns the result to the matrix.

Parameters
rhsThe scalar value.
Returns
A reference to the matrix.

◆ operator-() [1/2]

template<size_t N, size_t M, typename T>
Mat< N, M, T > operator- ( ) const

Negates the matrix. Component-wise matrix negation.

Returns
The negated matrix.

◆ operator-() [2/2]

template<size_t N, size_t M, typename T>
Mat< N, M, T > operator- ( const Mat< N, M, T > & rhs) const

Subtracts another matrix from the matrix. Component-wise matrix-matrix subtraction.

Parameters
rhsThe right-hand side matrix.
Returns
The result of the subtraction.

◆ operator-=() [1/2]

template<size_t N, size_t M, typename T>
Mat< N, M, T > & operator-= ( const Mat< N, M, T > & rhs)

Subtracts another matrix from the matrix and assigns the result to the matrix.

Parameters
rhsThe right-hand side matrix.
Returns
A reference to the matrix.

◆ operator-=() [2/2]

template<size_t N, size_t M, typename T>
Mat< N, M, T > & operator-= ( T rhs)

Subtracts a scalar from the matrix and assigns the result to the matrix.

Parameters
rhsThe scalar value.
Returns
A reference to the matrix.

◆ operator/()

template<size_t N, size_t M, typename T>
Mat< N, M, T > operator/ ( T rhs) const

Divides the matrix by a scalar.

Parameters
rhsThe scalar value.
Returns
The result of the division.

◆ operator/=()

template<size_t N, size_t M, typename T>
Mat< N, M, T > & operator/= ( T rhs)

Divides the matrix by a scalar and assigns the result to the matrix.

Parameters
rhsThe scalar value.
Returns
A reference to the matrix.

◆ operator==()

template<size_t N, size_t M, typename T>
bool operator== ( const Mat< N, M, T > & rhs) const

Checks if the matrix is equal to another matrix.

Parameters
rhsThe matrix to compare with.
Returns
True if the matrices are equal, false otherwise.

◆ row()

template<size_t N, size_t M, typename T>
Vec< M, T > row ( size_t row) const

Returns the specified row as a vector.

Parameters
rowThe row index.
Returns
The row as a vector.

◆ set_col()

template<size_t N, size_t M, typename T>
template<size_t vN>
void set_col ( size_t col,
const Vec< vN, T > & v )

Sets the specified column from a vector.

This function copies the first N components from v, so vN must be >= N.

Template Parameters
vNThe dimension of the vector.
Parameters
colThe column index.
vThe vector to set the column from.

◆ set_row()

template<size_t N, size_t M, typename T>
template<size_t vN>
void set_row ( size_t row,
const Vec< vN, T > & v )

Sets the specified row from a vector.

This function copies the first M components from v, so vN must be >= M.

Template Parameters
vNThe dimension of the vector.
Parameters
rowThe row index.
vThe vector to set the row from.

◆ swap_cols()

template<size_t N, size_t M, typename T>
void swap_cols ( size_t a,
size_t b )

Swaps the specified columns.

Parameters
aThe first column index.
bThe second column index.

◆ swap_rows()

template<size_t N, size_t M, typename T>
void swap_rows ( size_t a,
size_t b )

Swaps the specified rows.

Parameters
aThe first row index.
bThe second row index.

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