Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

gmtl::Matrix Class Template Reference
[Abstract Data Types: Matrix, Vec, Quat, Coord, Sphere, Plane]

Matrix: 4x4 Matrix class (OpenGL ordering). More...

#include <Matrix.h>

Collaboration diagram for gmtl::Matrix:

Collaboration graph
[legend]
List of all members.

Public Types

typedef DATA_TYPE DataType
 use this to declare single value types of the same type as this matrix. More...

enum  Params { Rows = ROWS, Cols = COLS }
enum  XformState {
  IDENTITY = 1, ORTHOGONAL = 2, ORTHONORMAL = 4, AFFINE = 8,
  FULL = 16, XFORM_ERROR = 32
}
 describes the xforms that this matrix has been through. More...


Public Methods

 Matrix ()
 Default Constructor (Identity constructor). More...

 Matrix (const Matrix< DATA_TYPE, ROWS, COLS > &matrix)
 copy constructor. More...

void set (DATA_TYPE v00, DATA_TYPE v01, DATA_TYPE v10, DATA_TYPE v11)
 element wise setter for 2x2. More...

void set (DATA_TYPE v00, DATA_TYPE v01, DATA_TYPE v02, DATA_TYPE v10, DATA_TYPE v11, DATA_TYPE v12)
 element wise setter for 2x3. More...

void set (DATA_TYPE v00, DATA_TYPE v01, DATA_TYPE v02, DATA_TYPE v10, DATA_TYPE v11, DATA_TYPE v12, DATA_TYPE v20, DATA_TYPE v21, DATA_TYPE v22)
 element wise setter for 3x3. More...

void set (DATA_TYPE v00, DATA_TYPE v01, DATA_TYPE v02, DATA_TYPE v03, DATA_TYPE v10, DATA_TYPE v11, DATA_TYPE v12, DATA_TYPE v13, DATA_TYPE v20, DATA_TYPE v21, DATA_TYPE v22, DATA_TYPE v23)
 element wise setter for 3x4. More...

void set (DATA_TYPE v00, DATA_TYPE v01, DATA_TYPE v02, DATA_TYPE v03, DATA_TYPE v10, DATA_TYPE v11, DATA_TYPE v12, DATA_TYPE v13, DATA_TYPE v20, DATA_TYPE v21, DATA_TYPE v22, DATA_TYPE v23, DATA_TYPE v30, DATA_TYPE v31, DATA_TYPE v32, DATA_TYPE v33)
 element wise setter for 4x4. More...

void set (const DATA_TYPE *data)
 set the matrix to the given data. More...

void setTranspose (const DATA_TYPE *data)
 set the matrix to the transpose of the given data. More...

DATA_TYPE & operator() (const unsigned row, const unsigned column)
 access [row, col] in the matrix. More...

const DATA_TYPE & operator() (const unsigned row, const unsigned column) const
 access [row, col] in the matrix (const version). More...

RowAccessor operator[] (const unsigned row)
 bracket operator. More...

const DATA_TYPE * getData () const
 Get a DATA_TYPE pointer to the matrix data RETVAL: Returns a ptr to the head of the matrix data. More...

bool isError ()
void setError ()

Public Attributes

DATA_TYPE mData [COLS *ROWS]
 Column major. More...

char mState
 describes what xforms are in this matrix. More...


Detailed Description

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
class gmtl::Matrix< DATA_TYPE, ROWS, COLS >

Matrix: 4x4 Matrix class (OpenGL ordering).

C/C++ uses matrices in row major order. In other words the access indices look like: mat[row][col]
(0,0) (0,1) (0,2) (0,3) <=== Array
(1,0) (1,1) (1,2) (1,3) <=== Array
(2,0) (2,1) (2,2) (2,3) <=== Array
(3,0) (3,1) (3,2) (3,3) <=== Array

OpenGL ordering specifies that the matrix has to be column major in memory, so we need to access it more like:
NOTE: The given indexes are what the cells have to be called in C/C++ notation. Since we are putting the columns into memory back-to-back.
(0,0) (1,0) (2,0) (3,0)
(0,1) (1,1) (2,1) (3,1)
(0,2) (1,2) (2,2) (3,2)
(0,3) (1,3) (2,3) (3,3)
^ ^ ^ ^
====================== Arrays

So basically OpenGL ordering is the Transpose of the way C++ accesses the array

See also:
Matrix44f , Matrix44d

Definition at line 74 of file Matrix.h.


Member Typedef Documentation

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
typedef DATA_TYPE gmtl::Matrix::DataType
 

use this to declare single value types of the same type as this matrix.

Definition at line 79 of file Matrix.h.


Member Enumeration Documentation

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
enum gmtl::Matrix::Params
 

Enumeration values:
Rows 
Cols 

Definition at line 80 of file Matrix.h.

00081    {
00082       Rows = ROWS, Cols = COLS
00083    };

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
enum gmtl::Matrix::XformState
 

describes the xforms that this matrix has been through.

Enumeration values:
IDENTITY 
ORTHOGONAL 
ORTHONORMAL 
AFFINE 
FULL 
XFORM_ERROR 

Definition at line 110 of file Matrix.h.

00111    {
00112       IDENTITY = 1,
00113       ORTHOGONAL = 2,
00114       ORTHONORMAL = 4,
00115       AFFINE = 8,
00116       FULL = 16,
00117       XFORM_ERROR = 32 // error bit
00118    };


Constructor & Destructor Documentation

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
gmtl::Matrix< DATA_TYPE, ROWS, COLS >::Matrix   [inline]
 

Default Constructor (Identity constructor).

Definition at line 121 of file Matrix.h.

References FULL, gmtl::Math::Min, mState, and operator().

00122    {
00124       for (unsigned int r = 0; r < ROWS; ++r)
00125       for (unsigned int c = 0; c < COLS; ++c)
00126          this->operator()( r, c ) = (DATA_TYPE)0.0;
00127 
00129       for (unsigned int x = 0; x < Math::Min( COLS, ROWS ); ++x)
00130          this->operator()( x, x ) = (DATA_TYPE)1.0;
00131 
00133       mState = FULL;
00134    };

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
gmtl::Matrix< DATA_TYPE, ROWS, COLS >::Matrix const Matrix< DATA_TYPE, ROWS, COLS > &    matrix [inline]
 

copy constructor.

Definition at line 137 of file Matrix.h.

References mState, and set.

00138    {
00139       this->set( matrix.getData() );
00140       mState = matrix.mState;
00141    }


Member Function Documentation

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
const DATA_TYPE* gmtl::Matrix< DATA_TYPE, ROWS, COLS >::getData   const [inline]
 

Get a DATA_TYPE pointer to the matrix data RETVAL: Returns a ptr to the head of the matrix data.

Definition at line 347 of file Matrix.h.

References mData.

00347 { return (DATA_TYPE*)mData; }

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
bool gmtl::Matrix< DATA_TYPE, ROWS, COLS >::isError   [inline]
 

Definition at line 349 of file Matrix.h.

References mState, and XFORM_ERROR.

00350    {
00351       return mState & XFORM_ERROR;
00352    }

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
const DATA_TYPE& gmtl::Matrix< DATA_TYPE, ROWS, COLS >::operator() const unsigned    row,
const unsigned    column
const [inline]
 

access [row, col] in the matrix (const version).

Definition at line 323 of file Matrix.h.

References gmtlASSERT, and mData.

00324    {
00325       gmtlASSERT( (row < ROWS) && (column < COLS) );
00326       return mData[column*ROWS + row];
00327    }

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
DATA_TYPE& gmtl::Matrix< DATA_TYPE, ROWS, COLS >::operator() const unsigned    row,
const unsigned    column
[inline]
 

access [row, col] in the matrix.

Definition at line 316 of file Matrix.h.

References gmtlASSERT, and mData.

Referenced by Matrix, and setTranspose.

00317    {
00318       gmtlASSERT( (row < ROWS) && (column < COLS) );
00319       return mData[column*ROWS + row];
00320    }

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
RowAccessor gmtl::Matrix< DATA_TYPE, ROWS, COLS >::operator[] const unsigned    row [inline]
 

bracket operator.

Definition at line 330 of file Matrix.h.

00331    {
00332       return RowAccessor(this, row);
00333    }

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
void gmtl::Matrix< DATA_TYPE, ROWS, COLS >::set const DATA_TYPE *    data [inline]
 

set the matrix to the given data.

This function is useful to copy matrix data from another math library.

"Example (to a matrix using an external math library):"

    pfMatrix other_matrix;
    other_matrix.setRot( 90, 1, 0, 0 );

    gmtl::Matrix44f mat;
    mat.set( other_matrix.getFloatPtr() );

WARNING: this isn't really safe, size and datatype are not enforced by the compiler.

Precondition:
data is in the native format of the gmtl::Matrix class, if not, then you might be able to use the setTranspose function.
i.e. in a 4x4 data[0-3] is the 1st column, data[4-7] is 2nd, etc...

Definition at line 276 of file Matrix.h.

References FULL, mData, and mState.

00277    {
00279       for (unsigned int x = 0; x < ROWS * COLS; ++x)
00280          mData[x] = data[x];
00281       mState = FULL;
00282    }

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
void gmtl::Matrix< DATA_TYPE, ROWS, COLS >::set DATA_TYPE    v00,
DATA_TYPE    v01,
DATA_TYPE    v02,
DATA_TYPE    v03,
DATA_TYPE    v10,
DATA_TYPE    v11,
DATA_TYPE    v12,
DATA_TYPE    v13,
DATA_TYPE    v20,
DATA_TYPE    v21,
DATA_TYPE    v22,
DATA_TYPE    v23,
DATA_TYPE    v30,
DATA_TYPE    v31,
DATA_TYPE    v32,
DATA_TYPE    v33
[inline]
 

element wise setter for 4x4.

Todo:
needs mp!! currently no way for a 4x3, ....

Definition at line 224 of file Matrix.h.

References FULL, gmtlASSERT, mData, and mState.

00228    {
00229       gmtlASSERT( ROWS == 4 && COLS == 4 );// could be compile time...
00230       mData[0]  = v00;
00231       mData[1]  = v10;
00232       mData[2]  = v20;
00233       mData[4]  = v01;
00234       mData[5]  = v11;
00235       mData[6]  = v21;
00236       mData[8]  = v02;
00237       mData[9]  = v12;
00238       mData[10] = v22;
00239 
00240       // right row
00241       mData[12] = v03;
00242       mData[13] = v13;
00243       mData[14] = v23;
00244 
00245       // bottom row
00246       mData[3]  = v30;
00247       mData[7]  = v31;
00248       mData[11] = v32;
00249       mData[15] = v33;
00250       mState = FULL;
00251    }

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
void gmtl::Matrix< DATA_TYPE, ROWS, COLS >::set DATA_TYPE    v00,
DATA_TYPE    v01,
DATA_TYPE    v02,
DATA_TYPE    v03,
DATA_TYPE    v10,
DATA_TYPE    v11,
DATA_TYPE    v12,
DATA_TYPE    v13,
DATA_TYPE    v20,
DATA_TYPE    v21,
DATA_TYPE    v22,
DATA_TYPE    v23
[inline]
 

element wise setter for 3x4.

Todo:
needs mp!! currently no way for a 4x3, ....

Definition at line 199 of file Matrix.h.

References FULL, gmtlASSERT, mData, and mState.

00202    {
00203       gmtlASSERT( ROWS == 3 && COLS == 4 );// could be compile time...
00204       mData[0] = v00;
00205       mData[1] = v10;
00206       mData[2] = v20;
00207       mData[3] = v01;
00208       mData[4] = v11;
00209       mData[5] = v21;
00210       mData[6] = v02;
00211       mData[7] = v12;
00212       mData[8] = v22;
00213 
00214       // right row
00215       mData[9]  = v03;
00216       mData[10] = v13;
00217       mData[11] = v23;
00218       mState = FULL;
00219    }

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
void gmtl::Matrix< DATA_TYPE, ROWS, COLS >::set DATA_TYPE    v00,
DATA_TYPE    v01,
DATA_TYPE    v02,
DATA_TYPE    v10,
DATA_TYPE    v11,
DATA_TYPE    v12,
DATA_TYPE    v20,
DATA_TYPE    v21,
DATA_TYPE    v22
[inline]
 

element wise setter for 3x3.

Todo:
needs mp!!

Definition at line 177 of file Matrix.h.

References FULL, gmtlASSERT, mData, and mState.

00180    {
00181       gmtlASSERT( ROWS == 3 && COLS == 3 ); // could be at compile time...
00182       mData[0] = v00;
00183       mData[1] = v10;
00184       mData[2] = v20;
00185 
00186       mData[3] = v01;
00187       mData[4] = v11;
00188       mData[5] = v21;
00189 
00190       mData[6] = v02;
00191       mData[7] = v12;
00192       mData[8] = v22;
00193       mState = FULL;
00194    }

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
void gmtl::Matrix< DATA_TYPE, ROWS, COLS >::set DATA_TYPE    v00,
DATA_TYPE    v01,
DATA_TYPE    v02,
DATA_TYPE    v10,
DATA_TYPE    v11,
DATA_TYPE    v12
[inline]
 

element wise setter for 2x3.

Todo:
needs mp!!

Definition at line 161 of file Matrix.h.

References FULL, gmtlASSERT, mData, and mState.

00163    {
00164       gmtlASSERT( ROWS == 2 && COLS == 3 ); // could be at compile time...
00165       mData[0] = v00;
00166       mData[1] = v10;
00167       mData[2] = v01;
00168       mData[3] = v11;
00169       mData[4] = v02;
00170       mData[5] = v12;
00171       mState = FULL;
00172    }

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
void gmtl::Matrix< DATA_TYPE, ROWS, COLS >::set DATA_TYPE    v00,
DATA_TYPE    v01,
DATA_TYPE    v10,
DATA_TYPE    v11
[inline]
 

element wise setter for 2x2.

Note:
variable names specify the row,column number to put the data into
Todo:
needs mp!!

Definition at line 147 of file Matrix.h.

References FULL, gmtlASSERT, mData, and mState.

Referenced by Matrix.

00149    {
00150       gmtlASSERT( ROWS == 2 && COLS == 2 ); // could be at compile time...
00151       mData[0] = v00;
00152       mData[1] = v10;
00153       mData[2] = v01;
00154       mData[3] = v11;
00155       mState = FULL;
00156    }

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
void gmtl::Matrix< DATA_TYPE, ROWS, COLS >::setError   [inline]
 

Definition at line 353 of file Matrix.h.

References mState, and XFORM_ERROR.

00354    {
00355       mState |= XFORM_ERROR;
00356    }

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
void gmtl::Matrix< DATA_TYPE, ROWS, COLS >::setTranspose const DATA_TYPE *    data [inline]
 

set the matrix to the transpose of the given data.

normally set() takes raw matrix data in column by column order, this function allows you to pass in row by row data.

Normally you'll use this function if you want to use a float array to init the matrix (see code example).

"Example (to set a [15 -4 20] translation using float array):"

    float data[] = { 1, 0, 0, 15,
                     0, 1, 0, -4,
                     0, 0, 1, 20,
                     0, 0, 0, 1   };
    gmtl::Matrix44f mat;
    mat.setTranspose( data );

WARNING: this isn't really safe, size and datatype are not enforced by the compiler.

Precondition:
ptr is in the transpose of the native format of the Matrix class
i.e. in a 4x4 data[0-3] is the 1st row, data[4-7] is 2nd, etc...

Definition at line 306 of file Matrix.h.

References FULL, mState, and operator().

00307    {
00309       for (unsigned int r = 0; r < ROWS; ++r)
00310       for (unsigned int c = 0; c < COLS; ++c)
00311          this->operator()( r, c ) = data[(r * COLS) + c];
00312       mState = FULL;
00313    }


Member Data Documentation

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
DATA_TYPE gmtl::Matrix::mData[COLS*ROWS]
 

Column major.

In other words {Column1, Column2, Column3, Column4} in memory access element mData[column][row]

Definition at line 362 of file Matrix.h.

Referenced by getData, operator(), and set.

template<typename DATA_TYPE, unsigned ROWS, unsigned COLS>
char gmtl::Matrix::mState
 

describes what xforms are in this matrix.

Definition at line 365 of file Matrix.h.

Referenced by isError, Matrix, set, setError, and setTranspose.


The documentation for this class was generated from the following file:
Generated on Mon Apr 7 15:29:28 2003 for GenericMathTemplateLibrary by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002