MathFu
An open source project by
FPL.
|
Matrix stores a set of "rows" by "columns" elements of type T and provides functions that operate on the set of elements. More...
#include <matrix.h>
Matrix stores a set of "rows" by "columns" elements of type T and provides functions that operate on the set of elements.
T | type of each element in the matrix. |
rows | Number of rows in the matrix. |
columns | Number of columns in the matrix. |
Public Member Functions | |
Matrix () | |
Construct a Matrix of uninitialized values. | |
Matrix (const Matrix< T, rows, columns > &m) | |
Construct a Matrix from another Matrix copying each element. /. More... | |
Matrix (const T &s) | |
Construct a Matrix from a single float. More... | |
Matrix (const T &s00, const T &s10, const T &s01, const T &s11) | |
Construct a Matrix from four floats. More... | |
Matrix (const T &s00, const T &s10, const T &s20, const T &s01, const T &s11, const T &s21, const T &s02, const T &s12, const T &s22) | |
Create a Matrix from nine floats. More... | |
Matrix (const T &s00, const T &s10, const T &s20, const T &s30, const T &s01, const T &s11, const T &s21, const T &s31, const T &s02, const T &s12, const T &s22, const T &s32) | |
Creates a Matrix from twelve floats. More... | |
Matrix (const T &s00, const T &s10, const T &s20, const T &s30, const T &s01, const T &s11, const T &s21, const T &s31, const T &s02, const T &s12, const T &s22, const T &s32, const T &s03, const T &s13, const T &s23, const T &s33) | |
Create a Matrix from sixteen floats. More... | |
Matrix (const Vector< T, 4 > &column0, const Vector< T, 4 > &column1, const Vector< T, 4 > &column2, const Vector< T, 4 > &column3) | |
Create 4x4 Matrix from 4, 4 element vectors. More... | |
Matrix (const T *const a) | |
Create a Matrix from the first row * column elements of an array. More... | |
Matrix (const VectorPacked< T, rows > *const vectors) | |
Create a Matrix from an array of "columns", "rows" element packed vectors. More... | |
const T & | operator() (const int row, const int column) const |
Access an element of the matrix. More... | |
T & | operator() (const int row, const int column) |
Access an element of the Matrix. More... | |
const T & | operator() (const int i) const |
Access an element of the Matrix. More... | |
T & | operator() (const int i) |
Access an element of the Matrix. More... | |
const T & | operator[] (const int i) const |
Access an element of the Matrix. More... | |
T & | operator[] (const int i) |
Access an element of the Matrix. More... | |
void | Pack (VectorPacked< T, rows > *const vector) const |
Pack the matrix to an array of "rows" element vectors, one vector per matrix column. More... | |
Matrix< T, rows, columns > | operator- () const |
Negate this Matrix. More... | |
Matrix< T, rows, columns > | operator+ (const Matrix< T, rows, columns > &m) const |
Add a Matrix to this Matrix. More... | |
Matrix< T, rows, columns > | operator- (const Matrix< T, rows, columns > &m) const |
Subtract a Matrix from this Matrix. More... | |
Matrix< T, rows, columns > | operator+ (const T &s) const |
Add a scalar to each element of this Matrix. More... | |
Matrix< T, rows, columns > | operator- (const T &s) const |
Subtract a scalar from each element of this Matrix. More... | |
Matrix< T, rows, columns > | operator* (const T &s) const |
Multiply each element of this Matrix with a scalar. More... | |
Matrix< T, rows, columns > | operator/ (const T &s) const |
Divide each element of this Matrix with a scalar. More... | |
Matrix< T, rows, columns > | operator* (const Matrix< T, rows, columns > &m) const |
Multiply this Matrix with another Matrix. More... | |
Matrix< T, rows, columns > & | operator+= (const Matrix< T, rows, columns > &m) |
Add a Matrix to this Matrix (in-place). More... | |
Matrix< T, rows, columns > & | operator-= (const Matrix< T, rows, columns > &m) |
Subtract a Matrix from this Matrix (in-place). More... | |
Matrix< T, rows, columns > & | operator+= (const T &s) |
Add a scalar to each element of this Matrix (in-place). More... | |
Matrix< T, rows, columns > & | operator-= (const T &s) |
Subtract a scalar from each element of this Matrix (in-place). More... | |
Matrix< T, rows, columns > & | operator*= (const T &s) |
Multiply each element of this Matrix with a scalar (in-place). More... | |
Matrix< T, rows, columns > & | operator/= (const T &s) |
Divide each element of this Matrix by a scalar (in-place). More... | |
Matrix< T, rows, columns > & | operator*= (const Matrix< T, rows, columns > &m) |
Multiply this Matrix with another Matrix (in-place). More... | |
Matrix< T, rows, columns > | Inverse () const |
Calculate the inverse of this Matrix. More... | |
bool | InverseWithDeterminantCheck (Matrix< T, rows, columns > *const inverse) const |
Calculate the inverse of this Matrix. More... | |
Matrix< T, columns, rows > | Transpose () const |
Calculate the transpose of this Matrix. More... | |
Vector< T, 2 > | TranslationVector2D () const |
Get the 2-dimensional translation of a 2-dimensional affine transform. More... | |
Vector< T, 3 > | TranslationVector3D () const |
Get the 3-dimensional translation of a 3-dimensional affine transform. More... | |
Static Public Member Functions | |
template<typename CompatibleT > | |
static Matrix< T, rows, columns > | FromType (const CompatibleT &compatible) |
Load from any byte-wise compatible external matrix. More... | |
template<typename CompatibleT > | |
static CompatibleT | ToType (const Matrix< T, rows, columns > &m) |
Load into any byte-wise compatible external matrix. More... | |
static Matrix< T, rows, columns > | OuterProduct (const Vector< T, rows > &v1, const Vector< T, columns > &v2) |
Calculate the outer product of two Vectors. More... | |
static Matrix< T, rows, columns > | HadamardProduct (const Matrix< T, rows, columns > &m1, const Matrix< T, rows, columns > &m2) |
Calculate the hadamard / component-wise product of two matrices. More... | |
static Matrix< T, rows, columns > | Identity () |
Calculate the identity Matrix. More... | |
static Matrix< T, 3 > | FromTranslationVector (const Vector< T, 2 > &v) |
Create a 3x3 translation Matrix from a 2-dimensional Vector. More... | |
static Matrix< T, 4 > | FromTranslationVector (const Vector< T, 3 > &v) |
Create a 4x4 translation Matrix from a 3-dimensional Vector. More... | |
static Matrix< T, rows > | FromScaleVector (const Vector< T, rows-1 > &v) |
Create a square Matrix with the diagonal component set to v. More... | |
static Matrix< T, 4 > | FromRotationMatrix (const Matrix< T, 3 > &m) |
Create a 4x4 Matrix from a 3x3 rotation Matrix. More... | |
static Matrix< T, 3 > | ToRotationMatrix (const Matrix< T, 4 > &m) |
Extracts the 3x3 rotation Matrix from a 4x4 Matrix. More... | |
static Matrix< T, 4 > | FromAffineTransform (const Matrix< T, 4, 3 > &affine) |
Constructs a Matrix<float, 4> from an AffineTransform. More... | |
static Matrix< T, 4, 3 > | ToAffineTransform (const Matrix< T, 4 > &m) |
Converts a Matrix<float, 4> into an AffineTransform. More... | |
static Matrix< T, 3 > | RotationX (const Vector< T, 2 > &v) |
Create a 3x3 rotation Matrix from a 2D normalized directional Vector around the X axis. More... | |
static Matrix< T, 3 > | RotationY (const Vector< T, 2 > &v) |
Create a 3x3 rotation Matrix from a 2D normalized directional Vector around the Y axis. More... | |
static Matrix< T, 3 > | RotationZ (const Vector< T, 2 > &v) |
Create a 3x3 rotation Matrix from a 2D normalized directional Vector around the Z axis. More... | |
static Matrix< T, 3 > | RotationX (T angle) |
Create a 3x3 rotation Matrix from an angle (in radians) around the X axis. More... | |
static Matrix< T, 3 > | RotationY (T angle) |
Create a 3x3 rotation Matrix from an angle (in radians) around the Y axis. More... | |
static Matrix< T, 3 > | RotationZ (T angle) |
Create a 3x3 rotation Matrix from an angle (in radians) around the Z axis. More... | |
static Matrix< T, 4, 4 > | Perspective (T fovy, T aspect, T znear, T zfar, T handedness=1) |
Create a 4x4 perspective Matrix. More... | |
static Matrix< T, 4, 4 > | Ortho (T left, T right, T bottom, T top, T znear, T zfar, T handedness=1) |
Create a 4x4 orthographic Matrix. More... | |
static Matrix< T, 4, 4 > | LookAt (const Vector< T, 3 > &at, const Vector< T, 3 > &eye, const Vector< T, 3 > &up, T handedness=-1) |
Create a 3-dimensional camera Matrix. More... | |
static Vector< T, 3 > | UnProject (const Vector< T, 3 > &window_coord, const Matrix< T, 4, 4 > &model_view, const Matrix< T, 4, 4 > &projection, const float window_width, const float window_height) |
Get the 3D position in object space from a window coordinate. More... | |
Static Public Attributes | |
static const int | kRows = rows |
Number of rows in the matrix. | |
static const int | kColumns = columns |
Number of columns in the matrix. | |
static const int | kElements = rows * columns |
Total number of elements in the matrix. | |
Friends | |
Vector< T, columns > | operator* (const Vector< T, rows > &v, const Matrix< T, rows, columns > &m) |
Multiply a Vector by a Matrix. More... | |
Related Functions | |
(Note that these are not member functions.) | |
template<class T , int rows, int columns> | |
Matrix< T, rows, columns > | operator* (const T &s, const Matrix< T, columns, rows > &m) |
Multiply each element of a Matrix by a scalar. More... | |
template<class T , int rows, int columns> | |
Vector< T, rows > | operator* (const Matrix< T, rows, columns > &m, const Vector< T, columns > &v) |
Multiply a Matrix by a Vector. More... | |
template<class T > | |
Vector< T, 3 > | operator* (const Matrix< T, 4, 4 > &m, const Vector< T, 3 > &v) |
Multiply a 4x4 Matrix by a 3-dimensional Vector. More... | |
|
inline |
|
inlineexplicit |
Construct a Matrix from a single float.
s | Scalar value used to initialize each element of the matrix. |
|
inline |
|
inline |
Create a Matrix from nine floats.
s00 | Value of the first row and column. |
s10 | Value of the second row, first column. |
s20 | Value of the third row, first column. |
s01 | Value of the first row, second column. |
s11 | Value of the second row and column. |
s21 | Value of the third row, second column. |
s02 | Value of the first row, third column. |
s12 | Value of the second row, third column. |
s22 | Value of the third row and column. |
|
inline |
Creates a Matrix from twelve floats.
s00 | Value of the first row and column. |
s10 | Value of the second row, first column. |
s20 | Value of the third row, first column. |
s30 | Value of the fourth row, first column. |
s01 | Value of the first row, second column. |
s11 | Value of the second row and column. |
s21 | Value of the third row, second column. |
s31 | Value of the fourth row, second column. |
s02 | Value of the first row, third column. |
s12 | Value of the second row, third column. |
s22 | Value of the third row and column. |
s32 | Value of the fourth row, third column. |
|
inline |
Create a Matrix from sixteen floats.
s00 | Value of the first row and column. |
s10 | Value of the second row, first column. |
s20 | Value of the third row, first column. |
s30 | Value of the fourth row, first column. |
s01 | Value of the first row, second column. |
s11 | Value of the second row and column. |
s21 | Value of the third row, second column. |
s31 | Value of the fourth row, second column. |
s02 | Value of the first row, third column. |
s12 | Value of the second row, third column. |
s22 | Value of the third row and column. |
s32 | Value of the fourth row, third column. |
s03 | Value of the first row, fourth column. |
s13 | Value of the second row, fourth column. |
s23 | Value of the third row, fourth column. |
s33 | Value of the fourth row and column. |
|
inlineexplicit |
Create a Matrix from the first row * column elements of an array.
a | Array of values that the matrix will be iniitlized to. |
|
inlineexplicit |
Create a Matrix from an array of "columns", "rows" element packed vectors.
vectors | Array of "columns", "rows" element packed vectors. |
|
inlinestatic |
Constructs a Matrix<float, 4> from an AffineTransform.
affine | An AffineTransform reference to be used to construct a Matrix<float, 4> by adding in the 'w' row of [0, 0, 0, 1]. |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
Load from any byte-wise compatible external matrix.
Format should be columns
vectors, each holding rows
values of type T.
Use this for safe conversion from external matrix classes. Often, external libraries will have their own matrix types that are, byte-for-byte, exactly the same as mathfu::Matrix. This function allows you to load a mathfu::Matrix from those external types, without potential aliasing bugs that are caused by casting.
compatible | reference to a byte-wise compatible matrix structure; array of columns x rows Ts. |
compatible
loaded as a mathfu::Matrix.
|
inlinestatic |
|
inlinestatic |
|
inline |
|
inline |
Calculate the inverse of this Matrix.
This calculates the inverse Matrix such that (m * m).Inverse()
is the identity. By contrast to Inverse() this returns whether the matrix is invertible.
The invertible check simply compares the calculated determinant with Constants<T>::GetDeterminantThreshold() to roughly determine whether the matrix is invertible. This simple check works in common cases but will fail for corner cases where the matrix is a combination of huge and tiny values that can't be accurately represented by the floating point datatype T. More extensive checks (relative to the input values) are possible but far more expensive, complicated and difficult to test.
|
inlinestatic |
Create a 3-dimensional camera Matrix.
at | The look-at target of the camera. |
eye | The position of the camera. |
up | The up vector in the world, for example (0, 1, 0) if the y-axis is up. |
handedness | 1.0f for RH, -1.0f for LH. |
|
inline |
Access an element of the matrix.
row | Index of the row to access. |
column | Index of the column to access. |
|
inline |
Access an element of the Matrix.
row | Index of the row to access. |
column | Index of the column to access. |
|
inline |
Access an element of the Matrix.
i | Index of the element to access in flattened memory. Where the column accessed is i / rows and the row is i % rows. |
|
inline |
Access an element of the Matrix.
i | Index of the element to access in flattened memory. Where the column accessed is i / rows and the row is i % rows. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Access an element of the Matrix.
i | Index of the element to access in flattened memory. Where the column accessed is i / rows and the row is i % rows. |
|
inline |
Access an element of the Matrix.
i | Index of the element to access in flattened memory. Where the column accessed is i / rows and the row is i % rows. |
|
inlinestatic |
|
inlinestatic |
Calculate the outer product of two Vectors.
|
inline |
Pack the matrix to an array of "rows" element vectors, one vector per matrix column.
vector | Array of "columns" entries to write to. |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
Converts a Matrix<float, 4> into an AffineTransform.
m | A Matrix<float, 4> reference to be converted into an AffineTransform by dropping the fixed 'w' row. |
|
inlinestatic |
|
inlinestatic |
Load into any byte-wise compatible external matrix.
Format should be columns
vectors, each holding rows
values of type T.
Use this for safe conversion to external matrix classes. Often, external libraries will have their own matrix types that are, byte-for-byte, exactly the same as mathfu::Matrix. This function allows you to load an external type from a mathfu::Matrix, without potential aliasing bugs that are caused by casting.
m | reference to mathfu::Matrix to convert. |
|
inline |
|
inline |
Get the 3-dimensional translation of a 3-dimensional affine transform.
|
inline |
|
inlinestatic |
Get the 3D position in object space from a window coordinate.
window_coord | The window coordinate. The z value is for depth. A window coordinate on the near plane will have 0 as the z value. And a window coordinate on the far plane will have 1 as the z value. z value should be with in [0, 1] here. |
model_view | The Model View matrix. |
projection | The projection matrix. |
window_width | Width of the window. |
window_height | Height of the window. |