proj home

Files   Classes   Functions   Hierarchy  

zprGLmatrix Class Reference

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

#include <zpr.h>

Collaboration diagram for zprGLmatrix:
Collaboration graph

List of all members.

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.

00204     : 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 matrix.

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

00208     { 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().

00034 {
00035 
00036 /* NB. OpenGL Matrices are COLUMN major. */
00037 #define MAT(m,r,c) (m)[(c)*4+(r)]
00038 
00039 // <TODO> Renaming the indexes??? Do they have a problem counting from 0? Why?
00040 /* Here's some shorthand converting standard (row,column) to index. */
00041 #define m11 MAT(m,0,0)
00042 #define m12 MAT(m,0,1)
00043 #define m13 MAT(m,0,2)
00044 #define m14 MAT(m,0,3)
00045 #define m21 MAT(m,1,0)
00046 #define m22 MAT(m,1,1)
00047 #define m23 MAT(m,1,2)
00048 #define m24 MAT(m,1,3)
00049 #define m31 MAT(m,2,0)
00050 #define m32 MAT(m,2,1)
00051 #define m33 MAT(m,2,2)
00052 #define m34 MAT(m,2,3)
00053 #define m41 MAT(m,3,0)
00054 #define m42 MAT(m,3,1)
00055 #define m43 MAT(m,3,2)
00056 #define m44 MAT(m,3,3)
00057 
00058   static GLdouble temporary[16]; /* Allow out == in. */
00059   GLdouble * tmp = out;
00060 
00061   bool const outin(m==out);
00062   if (outin)
00063     tmp = & temporary[0];
00064 
00065   /* Inverse = adjoint / det. (See linear algebra texts.)*/
00066 
00067   /* pre-compute 2x2 dets for last two rows when computing */
00068   /* cofactors of first two rows. */
00069   GLdouble d12(m31*m42-m41*m32);
00070   GLdouble d13(m31*m43-m41*m33);
00071   GLdouble d23(m32*m43-m42*m33);
00072   GLdouble d24(m32*m44-m42*m34);
00073   GLdouble d34(m33*m44-m43*m34);
00074   GLdouble d41(m34*m41-m44*m31);
00075 
00076   tmp[0] =  (m22 * d34 - m23 * d24 + m24 * d23);
00077   tmp[1] = -(m21 * d34 + m23 * d41 + m24 * d13);
00078   tmp[2] =  (m21 * d24 + m22 * d41 + m24 * d12);
00079   tmp[3] = -(m21 * d23 - m22 * d13 + m23 * d12);
00080 
00081   /* Compute determinant as early as possible using these cofactors. */
00082   GLdouble det(m11 * tmp[0] + m12 * tmp[1] + m13 * tmp[2] + m14 * tmp[3]);
00083 
00084   /* Singularity test. */
00085   if (abs(det)>zero)
00086   {
00087     GLdouble invDet(1.0 / det);
00088     /* Compute rest of inverse. */
00089     tmp[0] *= invDet;
00090     tmp[1] *= invDet;
00091     tmp[2] *= invDet;
00092     tmp[3] *= invDet;
00093 
00094     tmp[4] = -(m12 * d34 - m13 * d24 + m14 * d23) * invDet;
00095     tmp[5] =  (m11 * d34 + m13 * d41 + m14 * d13) * invDet;
00096     tmp[6] = -(m11 * d24 + m12 * d41 + m14 * d12) * invDet;
00097     tmp[7] =  (m11 * d23 - m12 * d13 + m13 * d12) * invDet;
00098 
00099     /* Pre-compute 2x2 dets for first two rows when computing */
00100     /* cofactors of last two rows. */
00101     d12 = m11*m22-m21*m12;
00102     d13 = m11*m23-m21*m13;
00103     d23 = m12*m23-m22*m13;
00104     d24 = m12*m24-m22*m14;
00105     d34 = m13*m24-m23*m14;
00106     d41 = m14*m21-m24*m11;
00107 
00108     tmp[8] =  (m42 * d34 - m43 * d24 + m44 * d23) * invDet;
00109     tmp[9] = -(m41 * d34 + m43 * d41 + m44 * d13) * invDet;
00110     tmp[10] =  (m41 * d24 + m42 * d41 + m44 * d12) * invDet;
00111     tmp[11] = -(m41 * d23 - m42 * d13 + m43 * d12) * invDet;
00112     tmp[12] = -(m32 * d34 - m33 * d24 + m34 * d23) * invDet;
00113     tmp[13] =  (m31 * d34 + m33 * d41 + m34 * d13) * invDet;
00114     tmp[14] = -(m31 * d24 + m32 * d41 + m34 * d12) * invDet;
00115     tmp[15] =  (m31 * d23 - m32 * d13 + m33 * d12) * invDet;
00116 
00117     if (outin)
00118       memcpy(out, tmp, 16*sizeof(GLdouble));
00119   }
00120 
00121 #undef m11
00122 #undef m12
00123 #undef m13
00124 #undef m14
00125 #undef m21
00126 #undef m22
00127 #undef m23
00128 #undef m24
00129 #undef m31
00130 #undef m32
00131 #undef m33
00132 #undef m34
00133 #undef m41
00134 #undef m42
00135 #undef m43
00136 #undef m44
00137 #undef MAT
00138 }

void zprGLmatrix::print (  )  const

Print the matrix as stored to cout.

Definition at line 439 of file zpr.cpp.

References matrix.

00440 {
00441   uint i;
00442   uint k;
00443   uint i2=0;
00444   for ( i=0; i<4; ++i)
00445   {
00446     for (k=0; k<4; ++k)
00447     {
00448       cout << matrix[i2] << " ";
00449       ++i2;
00450     }
00451     cout << endl;
00452   }
00453 }

void zprGLmatrix::printTranspose (  )  const

Print the transpose matrix to cout.

Definition at line 455 of file zpr.cpp.

References access().

00456 {
00457   uint i;
00458   uint k;
00459   for ( i=0; i<4; ++i)
00460   {
00461     for (k=0; k<4; ++k)
00462     {
00463       cout << access(i,k) << " ";
00464     }
00465     cout << endl;
00466   }
00467 }


Member Data Documentation

Client must ensure that this is not null.

Definition at line 200 of file zpr.h.

Referenced by access(), and print().

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:

Generated on 27 Nov 2012 for Chelton Evans Source by  doxygen 1.6.1