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

Matrix.h

Go to the documentation of this file.
00001 /************************************************************** ggt-head beg
00002  *
00003  * GGT: Generic Graphics Toolkit
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum
00007  *
00008  * -----------------------------------------------------------------
00009  * File:          $RCSfile: Matrix.h,v $
00010  * Date modified: $Date: 2003/03/29 22:02:19 $
00011  * Version:       $Revision: 1.26 $
00012  * -----------------------------------------------------------------
00013  *
00014  *********************************************************** ggt-head end */
00015 /*************************************************************** ggt-cpr beg
00016 *
00017 * GGT: The Generic Graphics Toolkit
00018 * Copyright (C) 2001,2002 Allen Bierbaum
00019 *
00020 * This library is free software; you can redistribute it and/or
00021 * modify it under the terms of the GNU Lesser General Public
00022 * License as published by the Free Software Foundation; either
00023 * version 2.1 of the License, or (at your option) any later version.
00024 *
00025 * This library is distributed in the hope that it will be useful,
00026 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00028 * Lesser General Public License for more details.
00029 *
00030 * You should have received a copy of the GNU Lesser General Public
00031 * License along with this library; if not, write to the Free Software
00032 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033 *
00034  ************************************************************ ggt-cpr end */
00035 #ifndef _GMTL_MATRIX_H_
00036 #define _GMTL_MATRIX_H_
00037 
00038 #include <gmtl/Defines.h>
00039 #include <gmtl/Math.h>
00040 #include <gmtl/Util/Assert.h>
00041 
00042 namespace gmtl
00043 {
00044 
00073 template <typename DATA_TYPE, unsigned ROWS, unsigned COLS>
00074 class Matrix
00075 {
00076 public:
00079    typedef DATA_TYPE DataType;
00080    enum Params
00081    {
00082       Rows = ROWS, Cols = COLS
00083    };
00084 
00089    class RowAccessor
00090    {
00091    public:
00092       RowAccessor(Matrix<DATA_TYPE,ROWS,COLS>* mat, const unsigned row)
00093          : mMat(mat), mRow(row)
00094       {
00095          gmtlASSERT(row < ROWS);
00096          gmtlASSERT(NULL != mat);
00097       }
00098 
00099       DATA_TYPE& operator[](const unsigned column)
00100       {
00101          gmtlASSERT(column < COLS);
00102          return (*mMat)(mRow,column);
00103       }
00104 
00105       Matrix<DATA_TYPE,ROWS,COLS>*  mMat;
00106       unsigned                      mRow;    
00107    };
00108 
00110    enum XformState
00111    {
00112       IDENTITY = 1,
00113       ORTHOGONAL = 2,
00114       ORTHONORMAL = 4,
00115       AFFINE = 8,
00116       FULL = 16,
00117       XFORM_ERROR = 32 // error bit
00118    };
00119 
00121    Matrix()
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    };
00135 
00137    Matrix( const Matrix<DATA_TYPE, ROWS, COLS>& matrix )
00138    {
00139       this->set( matrix.getData() );
00140       mState = matrix.mState;
00141    }
00142 
00147    void set( DATA_TYPE v00, DATA_TYPE v01,
00148              DATA_TYPE v10, DATA_TYPE v11 )
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    }
00157 
00161    void set( DATA_TYPE v00, DATA_TYPE v01, DATA_TYPE v02,
00162              DATA_TYPE v10, DATA_TYPE v11, DATA_TYPE v12  )
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    }
00173 
00177    void set( DATA_TYPE v00, DATA_TYPE v01, DATA_TYPE v02,
00178              DATA_TYPE v10, DATA_TYPE v11, DATA_TYPE v12,
00179              DATA_TYPE v20, DATA_TYPE v21, DATA_TYPE v22)
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    }
00195 
00199    void set( DATA_TYPE v00, DATA_TYPE v01, DATA_TYPE v02, DATA_TYPE v03,
00200              DATA_TYPE v10, DATA_TYPE v11, DATA_TYPE v12, DATA_TYPE v13,
00201              DATA_TYPE v20, DATA_TYPE v21, DATA_TYPE v22, DATA_TYPE v23)
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    }
00220 
00224    void set( DATA_TYPE v00, DATA_TYPE v01, DATA_TYPE v02, DATA_TYPE v03,
00225              DATA_TYPE v10, DATA_TYPE v11, DATA_TYPE v12, DATA_TYPE v13,
00226              DATA_TYPE v20, DATA_TYPE v21, DATA_TYPE v22, DATA_TYPE v23,
00227              DATA_TYPE v30, DATA_TYPE v31, DATA_TYPE v32, DATA_TYPE v33 )
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    }
00252 
00256    //void operator,()( DATA_TYPE b ) {}
00257 
00276    void set( const DATA_TYPE* data )
00277    {
00279       for (unsigned int x = 0; x < ROWS * COLS; ++x)
00280          mData[x] = data[x];
00281       mState = FULL;
00282    }
00283 
00306    void setTranspose( const DATA_TYPE* data )
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    }
00314 
00316    DATA_TYPE& operator()( const unsigned row, const unsigned column )
00317    {
00318       gmtlASSERT( (row < ROWS) && (column < COLS) );
00319       return mData[column*ROWS + row];
00320    }
00321 
00323    const DATA_TYPE&  operator()( const unsigned row, const unsigned column ) const
00324    {
00325       gmtlASSERT( (row < ROWS) && (column < COLS) );
00326       return mData[column*ROWS + row];
00327    }
00328 
00330    RowAccessor operator[]( const unsigned row )
00331    {
00332       return RowAccessor(this, row);
00333    }
00334 
00335    /*
00336    // racket operator
00337    const DATA_TYPE& operator[]( const unsigned i ) const
00338    {
00339       gmtlASSERT( i < (ROWS*COLS) );
00340       return mData[i];
00341    }
00342    */
00343 
00347    const DATA_TYPE*  getData() const { return (DATA_TYPE*)mData; }
00348 
00349    bool isError()
00350    {
00351       return mState & XFORM_ERROR;
00352    }
00353    void setError()
00354    {
00355       mState |= XFORM_ERROR;
00356    }
00357 
00358 public:
00362    DATA_TYPE mData[COLS*ROWS];
00363 
00365    char mState;
00366 };
00367 
00368 typedef Matrix<float, 2, 2> Matrix22f;
00369 typedef Matrix<double, 2, 2> Matrix22d;
00370 typedef Matrix<float, 2, 3> Matrix23f;
00371 typedef Matrix<double, 2, 3> Matrix23d;
00372 typedef Matrix<float, 3, 3> Matrix33f;
00373 typedef Matrix<double, 3, 3> Matrix33d;
00374 typedef Matrix<float, 3, 4> Matrix34f;
00375 typedef Matrix<double, 3, 4> Matrix34d;
00376 typedef Matrix<float, 4, 4> Matrix44f;
00377 typedef Matrix<double, 4, 4> Matrix44d;
00378 
00380 const Matrix22f MAT_IDENTITY22F = Matrix22f();
00381 
00383 const Matrix22d MAT_IDENTITY22D = Matrix22d();
00384 
00386 const Matrix23f MAT_IDENTITY23F = Matrix23f();
00387 
00389 const Matrix23d MAT_IDENTITY23D = Matrix23d();
00390 
00392 const Matrix33f MAT_IDENTITY33F = Matrix33f();
00393 
00395 const Matrix33d MAT_IDENTITY33D = Matrix33d();
00396 
00398 const Matrix34f MAT_IDENTITY34F = Matrix34f();
00399 
00401 const Matrix34d MAT_IDENTITY34D = Matrix34d();
00402 
00404 const Matrix44f MAT_IDENTITY44F = Matrix44f();
00405 
00407 const Matrix44d MAT_IDENTITY44D = Matrix44d();
00408 
00409 
00410 } // end namespace gmtl
00411 
00412 
00413 
00414 #endif

Generated on Mon Apr 7 15:28:55 2003 for GenericMathTemplateLibrary by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002