proj home

Files   Classes   Functions   Hierarchy  

Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
zprGLmatrix Class Reference

Support for basic operations on OpenGL matrix. More...

#include <zpr.h>

Collaboration diagram for zprGLmatrix:
Collaboration graph

Public Member Functions

 zprGLmatrix (GLdouble *matrix_)
 The class needs a pointer to an existing OpenGL matrix.
GLdouble access (uintc r, uintc k) const
 The OpenGL matrix is stored as a transpose.
void print () const
 Print the matrix as stored to cout.
void printTranspose () const
 Print the transpose matrix to cout.

Static Public Member Functions

static void invertMatrix (GLdouble *out, GLdouble const *m)
 Compute the inverse of a 4x4 matrix.

Public Attributes

GLdouble * matrix
 Client must ensure that this is not null.

Static Public Attributes

static GLdouble zero = 1.0E-15
 The smallest positive value used in singularity test.

Detailed Description

Support for basic operations on OpenGL matrix.

This is a helper class for zrp.

OpenGL stores a matrix as a transpose so its rows are stored as columns. The access function adjusts for this. If you want to print meaningfully use printTranspose().

Definition at line 195 of file zpr.h.

Constructor & Destructor Documentation

zprGLmatrix::zprGLmatrix ( GLdouble *  matrix_)
inline

The class needs a pointer to an existing OpenGL matrix.

Definition at line 203 of file zpr.h.

: matrix(matrix_) {}

Member Function Documentation

GLdouble zprGLmatrix::access ( uintc  r,
uintc  k 
) const
inline

The OpenGL matrix is stored as a transpose.

Definition at line 207 of file zpr.h.

References r.

Referenced by zpr::readProjection(), and zprtest::test02().

{ return matrix[4*k+r]; }
void zprGLmatrix::invertMatrix ( GLdouble *  out,
GLdouble const *  m 
)
static

Compute the inverse of a 4x4 matrix.

Definition at line 33 of file zpr.cpp.

References m11, m12, m13, m14, m21, m22, m23, m24, m31, m32, m33, m34, m41, m42, m43, and m44.

Referenced by zpr::readModelView().

{
/* NB. OpenGL Matrices are COLUMN major. */
#define MAT(m,r,c) (m)[(c)*4+(r)]
// <TODO> Renaming the indexes??? Do they have a problem counting from 0? Why?
/* Here's some shorthand converting standard (row,column) to index. */
#define m11 MAT(m,0,0)
#define m12 MAT(m,0,1)
#define m13 MAT(m,0,2)
#define m14 MAT(m,0,3)
#define m21 MAT(m,1,0)
#define m22 MAT(m,1,1)
#define m23 MAT(m,1,2)
#define m24 MAT(m,1,3)
#define m31 MAT(m,2,0)
#define m32 MAT(m,2,1)
#define m33 MAT(m,2,2)
#define m34 MAT(m,2,3)
#define m41 MAT(m,3,0)
#define m42 MAT(m,3,1)
#define m43 MAT(m,3,2)
#define m44 MAT(m,3,3)
static GLdouble temporary[16]; /* Allow out == in. */
GLdouble * tmp = out;
bool const outin(m==out);
if (outin)
tmp = & temporary[0];
/* Inverse = adjoint / det. (See linear algebra texts.)*/
/* pre-compute 2x2 dets for last two rows when computing */
/* cofactors of first two rows. */
GLdouble d12(m31*m42-m41*m32);
GLdouble d13(m31*m43-m41*m33);
GLdouble d23(m32*m43-m42*m33);
GLdouble d24(m32*m44-m42*m34);
GLdouble d34(m33*m44-m43*m34);
GLdouble d41(m34*m41-m44*m31);
tmp[0] = (m22 * d34 - m23 * d24 + m24 * d23);
tmp[1] = -(m21 * d34 + m23 * d41 + m24 * d13);
tmp[2] = (m21 * d24 + m22 * d41 + m24 * d12);
tmp[3] = -(m21 * d23 - m22 * d13 + m23 * d12);
/* Compute determinant as early as possible using these cofactors. */
GLdouble det(m11 * tmp[0] + m12 * tmp[1] + m13 * tmp[2] + m14 * tmp[3]);
/* Singularity test. */
if (abs(det)>zero)
{
GLdouble invDet(1.0 / det);
/* Compute rest of inverse. */
tmp[0] *= invDet;
tmp[1] *= invDet;
tmp[2] *= invDet;
tmp[3] *= invDet;
tmp[4] = -(m12 * d34 - m13 * d24 + m14 * d23) * invDet;
tmp[5] = (m11 * d34 + m13 * d41 + m14 * d13) * invDet;
tmp[6] = -(m11 * d24 + m12 * d41 + m14 * d12) * invDet;
tmp[7] = (m11 * d23 - m12 * d13 + m13 * d12) * invDet;
/* Pre-compute 2x2 dets for first two rows when computing */
/* cofactors of last two rows. */
d12 = m11*m22-m21*m12;
d13 = m11*m23-m21*m13;
d23 = m12*m23-m22*m13;
d24 = m12*m24-m22*m14;
d34 = m13*m24-m23*m14;
d41 = m14*m21-m24*m11;
tmp[8] = (m42 * d34 - m43 * d24 + m44 * d23) * invDet;
tmp[9] = -(m41 * d34 + m43 * d41 + m44 * d13) * invDet;
tmp[10] = (m41 * d24 + m42 * d41 + m44 * d12) * invDet;
tmp[11] = -(m41 * d23 - m42 * d13 + m43 * d12) * invDet;
tmp[12] = -(m32 * d34 - m33 * d24 + m34 * d23) * invDet;
tmp[13] = (m31 * d34 + m33 * d41 + m34 * d13) * invDet;
tmp[14] = -(m31 * d24 + m32 * d41 + m34 * d12) * invDet;
tmp[15] = (m31 * d23 - m32 * d13 + m33 * d12) * invDet;
if (outin)
memcpy(out, tmp, 16*sizeof(GLdouble));
}
#undef m11
#undef m12
#undef m13
#undef m14
#undef m21
#undef m22
#undef m23
#undef m24
#undef m31
#undef m32
#undef m33
#undef m34
#undef m41
#undef m42
#undef m43
#undef m44
#undef MAT
}
void zprGLmatrix::print ( ) const

Print the matrix as stored to cout.

Definition at line 439 of file zpr.cpp.

Referenced by zprtest::test02().

{
uint i;
uint k;
uint i2=0;
for ( i=0; i<4; ++i)
{
for (k=0; k<4; ++k)
{
cout << matrix[i2] << " ";
++i2;
}
cout << endl;
}
}
void zprGLmatrix::printTranspose ( ) const

Print the transpose matrix to cout.

Definition at line 455 of file zpr.cpp.

Referenced by zprtest::test02().

{
uint i;
uint k;
for ( i=0; i<4; ++i)
{
for (k=0; k<4; ++k)
{
cout << access(i,k) << " ";
}
cout << endl;
}
}

Member Data Documentation

GLdouble* zprGLmatrix::matrix

Client must ensure that this is not null.

Definition at line 200 of file zpr.h.

GLdouble zprGLmatrix::zero = 1.0E-15
static

The smallest positive value used in singularity test.

A measure of zero for singularity test.

Definition at line 214 of file zpr.h.


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