AaronCameron.net
Not Left, nor right. Just correct.
Not a Member? - Login or Create an Account
Tuesday the 22nd of May 2012 @ 01:25am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

cMatrix.h

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2002  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 
00026 #ifndef _n2l4_cMatrix_h
00027 #define _n2l4_cMatrix_h
00028 
00029 #include "n2l/n2l.h"
00030 
00031 namespace n2l
00032 {
00033 
00077     template <class TComponent, tUint Rows, tUint Cols >
00078     class cMatrix
00079     {
00080     public:
00081         typedef cMatrix<TComponent,Rows,Cols>       tThisMatrix;
00082         typedef TComponent                          tComponent;
00083 
00084         enum { NumComps = Rows*Cols };
00085 
00086         // Constructors
00087         /* ************************************************************ */
00088         cMatrix() :
00089             mMatrix( new tComponent[Rows*Cols] )
00090         {
00091         }
00092 
00093         /* ************************************************************ */
00094         cMatrix( const tThisMatrix & iMatrix ) :
00095             mMatrix( new tComponent[Rows*Cols] )
00096         {
00097             memcpy( (void*)mMatrix,
00098                     (void*)(iMatrix.mMatrix),
00099                     sizeof(tComponent)*Rows*Cols );
00100         }
00101 
00102         /* ************************************************************ */
00103         ~cMatrix()
00104         {
00105             delete []mMatrix;
00106         }
00107 
00108 
00109         /* ************************************************************ */
00110         void setRow( const tUint iRow,
00111             const tComponent &i1, 
00112             const tComponent &i2 = 0, 
00113             const tComponent &i3 = 0, 
00114             const tComponent &i4 = 0, 
00115             const tComponent &i5 = 0, 
00116             const tComponent &i6 = 0, 
00117             const tComponent &i7 = 0, 
00118             const tComponent &i8 = 0, 
00119             const tComponent &i9 = 0, 
00120             const tComponent &i10 = 0 )
00121         {
00122             switch (Cols)
00123             {
00124                 case 10:
00125                     at(iRow,9) = i10;
00126                 case 9:
00127                     at(iRow,8) = i9;
00128                 case 8:
00129                     at(iRow,7) = i8;
00130                 case 7:
00131                     at(iRow,6) = i7;
00132                 case 6:
00133                     at(iRow,5) = i6;
00134                 case 5:
00135                     at(iRow,4) = i5;
00136                 case 4:
00137                     at(iRow,3) = i4;
00138                 case 3:
00139                     at(iRow,2) = i3;
00140                 case 2:
00141                     at(iRow,1) = i2;
00142                 case 1:
00143                     at(iRow,0) = i1;
00144                     break;
00145 
00146                 default:
00147                     throw cOutOfBoundsException( "cMatrix::setRow",
00148                         "Too many columns to use this method" );
00149                     break;
00150             }
00151         }
00152 
00153 
00154         /* ************************************************************ */
00155         void setColumn( const tUint iCol,
00156             const tComponent &i1, 
00157             const tComponent &i2 = 0, 
00158             const tComponent &i3 = 0, 
00159             const tComponent &i4 = 0, 
00160             const tComponent &i5 = 0, 
00161             const tComponent &i6 = 0, 
00162             const tComponent &i7 = 0, 
00163             const tComponent &i8 = 0, 
00164             const tComponent &i9 = 0, 
00165             const tComponent &i10 = 0 )
00166         {
00167             switch (Rows)
00168             {
00169                 case 10:
00170                     at(9,iCol) = i10;
00171                 case 9:
00172                     at(8,iCol) = i9;
00173                 case 8:
00174                     at(7,iCol) = i8;
00175                 case 7:
00176                     at(6,iCol) = i7;
00177                 case 6:
00178                     at(5,iCol) = i6;
00179                 case 5:
00180                     at(4,iCol) = i5;
00181                 case 4:
00182                     at(3,iCol) = i4;
00183                 case 3:
00184                     at(2,iCol) = i3;
00185                 case 2:
00186                     at(1,iCol) = i2;
00187                 case 1:
00188                     at(0,iCol) = i1;
00189                     break;
00190 
00191                 default:
00192                     throw cOutOfBoundsException( "cMatrix::setCol",
00193                         "Too many rows to use this method" );
00194                     break;
00195             }
00196         }
00197 
00198         /* ************************************************************ */
00199         void transposed( cMatrix<tComponent, Cols,Rows> &oM ) const
00200         {
00201             for (tUint r=0; r<Rows; ++r)
00202                 for (tUint c=0; c<Cols; ++c)
00203                     oM.at(c,r) = at(r,c);
00204         }
00205 
00206         /* ************************************************************ */
00207         const cMatrix<tComponent, Cols,Rows> transposed() const
00208         {
00209             cMatrix<tComponent, Cols,Rows> oM;
00210             for (tUint r=0; r<Rows; ++r)
00211                 for (tUint c=0; c<Cols; ++c)
00212                     oM.at(c,r) = at(r,c);
00213             return oM;
00214         }
00215 
00216         /* ************************************************************ */
00217         inline const tUint rows() const { return Rows; };
00218         
00219         /* ************************************************************ */
00220         inline const tUint cols() const { return Cols; };
00221 
00222         /* ************************************************************ */
00223         tThisMatrix & operator =( const tThisMatrix & iMatrix )
00224         {
00225             if (&iMatrix!=this)
00226                 memcpy( (void*)mMatrix,
00227                         (void*)(iMatrix.mMatrix),
00228                         sizeof(tComponent)*Rows*Cols );
00229             return *this;
00230         }
00231 
00232         /* ************************************************************ */
00233         const tBool operator ==( const tThisMatrix &iM ) const
00234         {
00235             if (iM.rows()!=Rows || iM.cols()!=Cols) return false;
00236             for (tUint i=0; i!=NumComps; ++i)
00237                 if (mMatrix[i]!=iM.mMatrix[i])
00238                     return false;
00239             return true;
00240         }
00241 
00242         /* ************************************************************ */
00243         const tThisMatrix operator +( const tThisMatrix &iM ) const
00244         {
00245             if (iM.rows()!=Rows || iM.cols()!=Cols)
00246                 throw cBadDataUseException( "cMatrix::operator +()",
00247                     "A and B are of different size" );
00248             tThisMatrix s;
00249             s.zero();
00250             for (tUint i=0; i!=NumComps; ++i)
00251                 s[i] = mMatrix[i]+iM.mMatrix[i];
00252             return s;
00253         }
00254 
00255         /* ************************************************************ */
00256         const tThisMatrix operator *( const tComponent &iComp ) const
00257         {
00258             tThisMatrix s;
00259             s.zero();
00260             for (tUint i=0; i!=NumComps; ++i)
00261                 s[i] = mMatrix[i]*iComp;
00262             return s;
00263         }
00264 
00265         /* ************************************************************ */
00266         inline const tBool operator !=( const tThisMatrix &iM ) const
00267         {
00268             return !(*this==iM);
00269         }
00270 
00271         /* ************************************************************ */
00272         inline const tComponent &operator []( const tSint iOffset ) const
00273         {
00274             return mMatrix[iOffset];
00275         }
00276 
00277         /* ************************************************************ */
00278         inline tComponent &operator []( const tSint iOffset )
00279         {
00280             return mMatrix[iOffset];
00281         }
00282 
00283         /* ************************************************************ */
00284         template <class TRMatrix, class TOMatrix>
00285         void multiply( TRMatrix &oC, const TOMatrix &iB ) const
00286         {
00287             assert( oC.rows() == Rows );
00288             assert( oC.cols() == iB.cols() );
00289             assert( Cols == iB.rows() );
00290             for (tUint cR=0; cR<Rows; ++cR)
00291                 for (tUint cC=0; cC<iB.cols(); ++cC) {
00292                     tComponent sum(0);
00293                     for (tUint aC=0; aC<Cols; ++aC)
00294                         sum += at(cR,aC) * iB.at(aC,cC);
00295                     oC.at(cR,cC) = sum;
00296                 }
00297         }
00298 
00299         /* ************************************************************ */
00302         tComponent & at( const tUint iRow, const tUint iCol )
00303         {
00304             return mMatrix[ iCol*Rows+iRow ];
00305         }
00306 
00307         /* ************************************************************ */
00310         const tComponent & at( const tUint iRow, const tUint iCol ) const
00311         {
00312             return mMatrix[ iCol*Rows+iRow ];
00313         }
00314 
00315 
00316         /* ************************************************************ */
00319         tComponent * data()
00320         {
00321             return mMatrix;
00322         }
00323 
00324 
00325         /* ************************************************************ */
00328         void identity()
00329         {
00330             // Clear the matrix
00331             memset( (void*)(mMatrix), 0, sizeof(tComponent)*Rows*Cols );
00332             // Set the identity
00333             const tUint Num( n2lMin(Rows,Cols) );
00334             for (tUint i=0; i<Num; ++i) at(i,i) = 1;
00335         }
00336 
00337         /* ************************************************************ */
00338         inline void zero()
00339         {
00340             memset( (void*)(mMatrix), 0, sizeof(tComponent)*NumComps );
00341         }
00342 
00343         /* ************************************************************ */
00344         const tString dump()
00345         {
00346             tString temp;
00347             for (tUint r=0; r<Rows; ++r) {
00348                 temp += "[";
00349                 for (tUint c=0; c<Cols; ++c) {
00350                     temp += asString(at(r,c));
00351                     temp += (c+1==Cols?" ]\n":",");
00352                 }
00353             }
00354             return temp;
00355         }
00356 
00357     private:
00358         tComponent  *mMatrix;
00359 
00360     }; // class
00361 
00362 } // namespace n2l
00363 
00364 #endif
©2012 Aaron Cameron