vmath vmath-0.13

Public Member Functions | Static Public Member Functions | Public Attributes | Friends | List of all members
Matrix4< T > Class Template Reference

Class for matrix 4x4. More...

`#include <vmath.h>`

Collaboration diagram for Matrix4< T >:
[legend]

## Public Member Functions
| Matrix4 ()
| Creates identity matrix. More...
| Matrix4 (const T *dt)
| Copy matrix values from array (these data must be in column major order!) More...
| Matrix4 (const Matrix4< T > &src)
| Copy constructor. More...
template<class FromT >
| Matrix4 (const Matrix4< FromT > &src)
| Copy casting constructor. More...
void | identity ()
| Resets matrix to be identity matrix. More...
bool | operator== (const Matrix4< T > &rhs) const
| Equality test operator. More...
bool | operator!= (const Matrix4< T > &rhs) const
| Inequality test operator. More...
T & | at (int x, int y)
| Get reference to element at postion (x,y). More...
const T & | at (int x, int y) const
| Get constant reference to element at position (x,y). More...
T & | operator() (int i, int j)
| Get reference to element at position (i,j), with math matrix notation. More...
const T & | operator() (int i, int j) const
| Get constant reference to element at position (i,j), with math matrix notation. More...
void | setTranslation (const Vector3< T > &v)
| Sets translation part of matrix. More...
Vector3< T > | getTranslation () const
void | setRotation (const Matrix3< T > &m)
| Sets rotation part (matrix 3x3) of matrix. More...
Vector3< T > | getScale () const
| Gets matrix scale. More...
void | setScale (T s)
| Sets matrix uniform scale values. More...
void | setScale (T sx, T sy, T sz)
| Sets matrix scale for all axes. More...
void | setScale (const Vector3< T > &s)
| Sets matrix scale for all axes. More...
Matrix4< T > & | operator= (const Matrix4< T > &rhs)
| Copy operator. More...
template<class FromT >
Matrix4< T > & | operator= (const Matrix4< FromT > &rhs)
| Copy casting operator. More...
Matrix4< T > & | operator= (const T *rhs)
| Copy operator. More...
Matrix4< T > | operator+ (const Matrix4< T > &rhs) const
| Addition operator. More...
Matrix4< T > | operator- (const Matrix4< T > &rhs) const
| Subtraction operator. More...
Matrix4< T > | operator+ (T rhs) const
| Addition operator. More...
Matrix4< T > | operator- (T rhs) const
| Subtraction operator. More...
Matrix4< T > | operator* (T rhs) const
| Multiplication operator. More...
Matrix4< T > | operator/ (T rhs) const
| Division operator. More...
Vector4< T > | operator* (const Vector4< T > &rhs) const
| Multiplication operator. More...
Vector3< T > | operator* (const Vector3< T > &rhs) const
| Multiplication operator. More...
Matrix4< T > | operator* (Matrix4< T > rhs) const
| Multiplication operator. More...
T | det ()
| Computes determinant of matrix. More...
Matrix4< T > | inverse ()
| Computes inverse matrix. More...
Matrix4< T > | transpose ()
| Transpose matrix. More...
Matrix4< T > | lerp (T fact, const Matrix4< T > &rhs) const
| Linear interpolation of two matrices. More...
| operator T* ()
| Conversion to pointer operator. More...
| operator const T * () const
| Conversion to pointer operator. More...
std::string | toString () const
| Gets string representation. More...

## Static Public Member Functions
static Matrix4< T > | createRotationAroundAxis (T xDeg, T yDeg, T zDeg)
| Creates rotation matrix by rotation around axis. More...
static Matrix4< T > | createTranslation (T x, T y, T z, T w=1)
| Creates translation matrix. More...
static Matrix4< T > | createScale (T sx, T sy, T sz)
| Create scale matrix with sx, sy, and sz being values of matrix main diagonal. More...
static Matrix4< T > | createLookAt (const Vector3< T > &eyePos, const Vector3< T > &centerPos, const Vector3< T > &upDir)
| Creates new view matrix to look from specified position eyePos to specified position centerPos. More...
static Matrix4< T > | createFrustum (T left, T right, T bottom, T top, T zNear, T zFar)
| Creates OpenGL compatible perspective projection according specified frustum parameters. More...
static Matrix4< T > | createOrtho (T left, T right, T bottom, T top, T zNear, T zFar)
| Creates OpenGL compatible orthographic projection matrix. More...
template<class FromT >
static Matrix4< T > | fromRowMajorArray (const FromT *arr)
| Creates new matrix 4x4 from array that represents such matrix 4x4 as array of tightly packed elements in row major order. More...
template<class FromT >
static Matrix4< T > | fromColumnMajorArray (const FromT *arr)
| Creates new matrix 4x4 from array that represents such matrix 4x4 as array of tightly packed elements in column major order. More...

## Public Attributes
T | data [16]
| Data stored in column major order. More...

## Friends
std::ostream & | operator<< (std::ostream &lhs, const Matrix4< T > &rhs)
| Output to stream operator. More...

## Detailed Description

### template<class T>
class Matrix4< T >

Class for matrix 4x4.

Note
  Data stored in this matrix are in column major order. This arrangement suits OpenGL. If you're using row major matrix, consider using fromRowMajorArray as way for construction Matrix4<T> instance.

## Constructor & Destructor Documentation

## ◆ Matrix4() [1/4]

template<class T>

Matrix4< T >::Matrix4 | ( |  | ) |
| inline

Creates identity matrix.

## ◆ Matrix4() [2/4]

template<class T>

Matrix4< T >::Matrix4 | ( | const T * | dt | ) |
| inline

Copy matrix values from array (these data must be in column major order!)

## ◆ Matrix4() [3/4]

template<class T>

Matrix4< T >::Matrix4 | ( | const Matrix4< T > & | src | ) |
| inline

Copy constructor.

Parameters

  src | Data source for new created instance of Matrix4.

## ◆ Matrix4() [4/4]

template<class T>
template<class FromT >

Matrix4< T >::Matrix4 | ( | const Matrix4< FromT > & | src | ) |
| inline

Copy casting constructor.

Parameters

  src | Data source for new created instance of Matrix4.

## Member Function Documentation

## ◆ at() [1/2]

template<class T>

T& Matrix4< T >::at | ( | int | x,
|  | int | y
| ) |  |
| inline

Get reference to element at postion (x,y).

Parameters

  x | Number of column (0..3)
  y | Number of row (0..3)

## ◆ at() [2/2]

template<class T>

const T& Matrix4< T >::at | ( | int | x,
|  | int | y
| ) |  | const
| inline

Get constant reference to element at position (x,y).

Parameters

  x | Number of column (0..3)
  y | Number of row (0..3)

## ◆ createFrustum()

template<class T>

static Matrix4<T> Matrix4< T >::createFrustum | ( | T | left,
|  | T | right,
|  | T | bottom,
|  | T | top,
|  | T | zNear,
|  | T | zFar
| ) |  |
| inline static

Creates OpenGL compatible perspective projection according specified frustum parameters.

Parameters

  left | Specify the coordinate for the left vertical clipping plane,
  right | Specify the coordinate for the right vertical clipping plane.
  bottom | Specify the coordinate for the bottom horizontal clipping plane,
  top | Specify the coordinate for the top horizontal clipping plane.
  zNear | Specify the distance to the near clipping plane. Distance must be positive.
  zFar | Specify the distance to the far depth clipping plane. Distance must be positive.

Returns
  Projection matrix for specified frustum.

## ◆ createLookAt()

template<class T>

static Matrix4<T> Matrix4< T >::createLookAt | ( | const Vector3< T > & | eyePos,
|  | const Vector3< T > & | centerPos,
|  | const Vector3< T > & | upDir
| ) |  |
| inline static

Creates new view matrix to look from specified position eyePos to specified position centerPos.

Parameters

  eyePos | A position of camera
  centerPos | A position where camera looks-at
  upDir | Direction of up vector

Returns
  Resulting view matrix that looks from and at specific position.

## ◆ createOrtho()

template<class T>

static Matrix4<T> Matrix4< T >::createOrtho | ( | T | left,
|  | T | right,
|  | T | bottom,
|  | T | top,
|  | T | zNear,
|  | T | zFar
| ) |  |
| inline static

Creates OpenGL compatible orthographic projection matrix.

Parameters

  left | Specify the coordinate for the left vertical clipping plane,
  right | Specify the coordinate for the right vertical clipping plane.
  bottom | Specify the coordinate for the bottom horizontal clipping plane,
  top | Specify the coordinate for the top horizontal clipping plane.
  zNear | Specify the distance to the nearer depth clipping plane. This value is negative if the plane is to be behind the viewer,
  zFar | Specify the distance to the farther depth clipping plane. This value is negative if the plane is to be behind the viewer.

Returns
  Othrographic projection matrix.

## ◆ createRotationAroundAxis()

template<class T>

static Matrix4<T> Matrix4< T >::createRotationAroundAxis | ( | T | xDeg,
|  | T | yDeg,
|  | T | zDeg
| ) |  |
| inline static

Creates rotation matrix by rotation around axis.

Parameters

  xDeg | Angle (in degrees) of rotation around axis X.
  yDeg | Angle (in degrees) of rotation around axis Y.
  zDeg | Angle (in degrees) of rotation around axis Z.

## ◆ createScale()

template<class T>

static Matrix4<T> Matrix4< T >::createScale | ( | T | sx,
|  | T | sy,
|  | T | sz
| ) |  |
| inline static

Create scale matrix with sx, sy, and sz being values of matrix main diagonal.

Parameters

  sx | Scale in X-axis
  sy | Scale in Y-axis
  sz | Scale in Z-axis

Returns
  Transform matrix 4x4 with scale transformation.

## ◆ createTranslation()

template<class T>

static Matrix4<T> Matrix4< T >::createTranslation | ( | T | x,
|  | T | y,
|  | T | z,
|  | T | w = `1`
| ) |  |
| inline static

Creates translation matrix.

Creates translation matrix.

Parameters

  x | X-direction translation
  y | Y-direction translation
  z | Z-direction translation
  w | for W-coordinate translation (implicitly set to 1)

## ◆ det()

template<class T>

T Matrix4< T >::det | ( |  | ) |
| inline

Computes determinant of matrix.

Returns
  Determinant of matrix

Note
  This function does 3 * 4 * 6 muls, 3 * 6 adds.

## ◆ fromColumnMajorArray()

template<class T>
template<class FromT >

static Matrix4<T> Matrix4< T >::fromColumnMajorArray | ( | const FromT * | arr | ) |
| inline static

Creates new matrix 4x4 from array that represents such matrix 4x4 as array of tightly packed elements in column major order.

Parameters

  arr | An array of elements for 4x4 matrix in column major order.

Returns
  An instance of Matrix4<T> representing arr

## ◆ fromRowMajorArray()

template<class T>
template<class FromT >

static Matrix4<T> Matrix4< T >::fromRowMajorArray | ( | const FromT * | arr | ) |
| inline static

Creates new matrix 4x4 from array that represents such matrix 4x4 as array of tightly packed elements in row major order.

Parameters

  arr | An array of elements for 4x4 matrix in row major order.

Returns
  An instance of Matrix4<T> representing arr

## ◆ getScale()

template<class T>

Vector3<T> Matrix4< T >::getScale | ( |  | ) | const
| inline

Gets matrix scale.

Returns
  Scales (i.e. first three values from matrix diagonal.

## ◆ getTranslation()

template<class T>

Vector3<T> Matrix4< T >::getTranslation | ( |  | ) | const
| inline

## ◆ identity()

template<class T>

void Matrix4< T >::identity | ( |  | ) |
| inline

Resets matrix to be identity matrix.

## ◆ inverse()

template<class T>

Matrix4<T> Matrix4< T >::inverse | ( |  | ) |
| inline

Computes inverse matrix.

Returns
  Inverse matrix of this matrix.

Note
  This is a little bit time consuming operation (16 * 6 * 3 muls, 16 * 5 adds + det() + mul() functions)

## ◆ lerp()

template<class T>

Matrix4<T> Matrix4< T >::lerp | ( | T | fact,
|  | const Matrix4< T > & | rhs
| ) |  | const
| inline

Linear interpolation of two matrices.

Parameters

  fact | Factor of interpolation. For translation from positon of this matrix (lhs) to matrix rhs, values of factor goes from 0.0 to 1.0.
  rhs | Second Matrix for interpolation

Note
  However values of fact parameter are reasonable only in interval [0.0 , 1.0], you can pass also values outside of this interval and you can get result (extrapolation?)

## ◆ operator const T *()

template<class T>

Matrix4< T >::operator const T * | ( |  | ) | const
| inline

Conversion to pointer operator.

Returns
  Constant Pointer to internally stored (in management of class Matrix4<T>) used for passing Matrix4<T> values to gl*[fd]v functions.

## ◆ operator T*()

template<class T>

Matrix4< T >::operator T* | ( |  | ) |
| inline

Conversion to pointer operator.

Returns
  Pointer to internally stored (in management of class Matrix4<T>) used for passing Matrix4<T> values to gl*[fd]v functions.

## ◆ operator!=()

template<class T>

bool Matrix4< T >::operator!= | ( | const Matrix4< T > & | rhs | ) | const
| inline

Inequality test operator.

Parameters

  rhs | Right hand side argument of binary operator.

Returns
  not (lhs == rhs) :-P

## ◆ operator()() [1/2]

template<class T>

T& Matrix4< T >::operator() | ( | int | i,
|  | int | j
| ) |  |
| inline

Get reference to element at position (i,j), with math matrix notation.

Parameters

  i | Number of row (1..4)
  j | Number of column (1..4)

## ◆ operator()() [2/2]

template<class T>

const T& Matrix4< T >::operator() | ( | int | i,
|  | int | j
| ) |  | const
| inline

Get constant reference to element at position (i,j), with math matrix notation.

Parameters

  i | Number of row (1..4)
  j | Number of column (1..4)

## ◆ operator*() [1/4]

template<class T>

Matrix4<T> Matrix4< T >::operator* | ( | T | rhs | ) | const
| inline

Multiplication operator.

Parameters

  rhs | Right hand side argument of binary operator.

## ◆ operator*() [2/4]

template<class T>

Vector4<T> Matrix4< T >::operator* | ( | const Vector4< T > & | rhs | ) | const
| inline

Multiplication operator.

Parameters

  rhs | Right hand side argument of binary operator.

## ◆ operator*() [3/4]

template<class T>

Vector3<T> Matrix4< T >::operator* | ( | const Vector3< T > & | rhs | ) | const
| inline

Multiplication operator.

Parameters

  rhs | Right hand side argument of binary operator.

## ◆ operator*() [4/4]

template<class T>

Matrix4<T> Matrix4< T >::operator* | ( | Matrix4< T > | rhs | ) | const
| inline

Multiplication operator.

Parameters

  rhs | Right hand side argument of binary operator.

## ◆ operator+() [1/2]

template<class T>

Matrix4<T> Matrix4< T >::operator+ | ( | const Matrix4< T > & | rhs | ) | const
| inline

Addition operator.

Parameters

  rhs | Right hand side argument of binary operator.

## ◆ operator+() [2/2]

template<class T>

Matrix4<T> Matrix4< T >::operator+ | ( | T | rhs | ) | const
| inline

Addition operator.

Parameters

  rhs | Right hand side argument of binary operator.

## ◆ operator-() [1/2]

template<class T>

Matrix4<T> Matrix4< T >::operator- | ( | const Matrix4< T > & | rhs | ) | const
| inline

Subtraction operator.

Parameters

  rhs | Right hand side argument of binary operator.

## ◆ operator-() [2/2]

template<class T>

Matrix4<T> Matrix4< T >::operator- | ( | T | rhs | ) | const
| inline

Subtraction operator.

Parameters

  rhs | Right hand side argument of binary operator.

## ◆ operator/()

template<class T>

Matrix4<T> Matrix4< T >::operator/ | ( | T | rhs | ) | const
| inline

Division operator.

Parameters

  rhs | Right hand side argument of binary operator.

## ◆ operator=() [1/3]

template<class T>

Matrix4<T>& Matrix4< T >::operator= | ( | const Matrix4< T > & | rhs | ) |
| inline

Copy operator.

Parameters

  rhs | Right hand side argument of binary operator.

## ◆ operator=() [2/3]

template<class T>
template<class FromT >

Matrix4<T>& Matrix4< T >::operator= | ( | const Matrix4< FromT > & | rhs | ) |
| inline

Copy casting operator.

Parameters

  rhs | Right hand side argument of binary operator.

## ◆ operator=() [3/3]

template<class T>

Matrix4<T>& Matrix4< T >::operator= | ( | const T * | rhs | ) |
| inline

Copy operator.

Parameters

  rhs | Right hand side argument of binary operator.

## ◆ operator==()

template<class T>

bool Matrix4< T >::operator== | ( | const Matrix4< T > & | rhs | ) | const
| inline

Equality test operator.

Parameters

  rhs | Right hand side argument of binary operator.

Note
  Test of equality is based of threshold EPSILON value. To be two values equal, must satisfy this condition all elements of matrix | lhs[i] - rhs[i] | < EPSILON, same for y-coordinate, z-coordinate, and w-coordinate.

## ◆ setRotation()

template<class T>

void Matrix4< T >::setRotation | ( | const Matrix3< T > & | m | ) |
| inline

Sets rotation part (matrix 3x3) of matrix.

Parameters

  m | Rotation part of matrix

## ◆ setScale() [1/3]

template<class T>

void Matrix4< T >::setScale | ( | T | s | ) |
| inline

Sets matrix uniform scale values.

Parameters

  s | Uniform scale value

## ◆ setScale() [2/3]

template<class T>

void Matrix4< T >::setScale | ( | T | sx,
|  | T | sy,
|  | T | sz
| ) |  |
| inline

Sets matrix scale for all axes.

Parameters

  sx | X-axis scale factor
  sy | Y-axis scale factor
  sz | Z-axis scale factor

## ◆ setScale() [3/3]

template<class T>

void Matrix4< T >::setScale | ( | const Vector3< T > & | s | ) |
| inline

Sets matrix scale for all axes.

Parameters

  s | Scale factors for X, Y, and Z coordinate.

## ◆ setTranslation()

template<class T>

void Matrix4< T >::setTranslation | ( | const Vector3< T > & | v | ) |
| inline

Sets translation part of matrix.

Parameters

  v | Vector of translation to be set.

## ◆ toString()

template<class T>

std::string Matrix4< T >::toString | ( |  | ) | const
| inline

Gets string representation.

## ◆ transpose()

template<class T>

Matrix4<T> Matrix4< T >::transpose | ( |  | ) |
| inline

Transpose matrix.

## Friends And Related Function Documentation

## ◆ operator<<

template<class T>

std::ostream& operator<< | ( | std::ostream & | lhs,
|  | const Matrix4< T > & | rhs
| ) |  |
| friend

Output to stream operator.

Parameters

  lhs | Left hand side argument of operator (commonly ostream instance).
  rhs | Right hand side argument of operator.

Returns
  Left hand side argument - the ostream object passed to operator.

## Member Data Documentation

## ◆ data

template<class T>

T Matrix4< T >::data[16]

Data stored in column major order.

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

- src/vmath.h