K3dMatrix.cpp

Go to the documentation of this file.
00001 
00012 /***************************************************************************
00013  *   Copyright (C) 2007 by Jan Koci   *
00014  *   honza.koci@email.cz   *
00015  *   http://kengine.sourceforge.net/tutorial/
00016  *                                                                         *
00017  *   This program is free software; you can redistribute it and/or modify  *
00018  *   it under the terms of the GNU General Public License as published by  *
00019  *   the Free Software Foundation; either version 2 of the License, or     *
00020  *   (at your option) any later version.                                   *
00021  *                                                                         *
00022  *   This program is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00025  *   GNU General Public License for more details.                          *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU General Public License     *
00028  *   along with this program; if not, write to the                         *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00031  ***************************************************************************/
00032 
00033 #include "K3dMatrix.h"
00034 
00035 K3dMatrix::K3dMatrix()
00036 {
00037         // Reset matrix
00038         LoadIdentity();
00039 }
00040 
00041 K3dMatrix::K3dMatrix(const float afMatrix[16])
00042 {
00043         for( int i=0; i<16; i++)
00044         {
00045                 m_afMatrix[i] = afMatrix[i];
00046         }
00047 }
00048 
00049 K3dMatrix::~K3dMatrix()         
00050 {
00051 }
00052 
00053 
00054 
00058 void K3dMatrix::SetMatrix( const float *_pfMatrix, const int _iSize )
00059 { 
00060         for(int i=0; i<_iSize; i++)
00061         {
00062                 m_afMatrix[i] = _pfMatrix[i];
00063         }
00064 }
00065 
00066 
00067 
00068 
00072 void K3dMatrix::SetMatrix(const K3dMatrix &_rkMatrix, const int _iSize )
00073 {
00074         K3dMatrix kM = _rkMatrix;
00075         for(int i=0; i<_iSize; i++)
00076         {
00077                 m_afMatrix[i] = kM[i];
00078         }
00079 }
00080 
00081 
00082 
00083 
00084 void K3dMatrix::SetRow(const K3dVector4 &_rkRow, const int iRow)
00085 {
00086         K3dVector4 kV = _rkRow;
00087         if(!(0 <= iRow) && (iRow < 4) ) 
00088         {
00089                 cerr << "Error: K3dMatrix::SetRow() -- error index " << iRow << endl;
00090         }
00091 
00092         switch (iRow)
00093         {
00094         case 0:
00095                 m_afMatrix[0] = kV[0];
00096                 m_afMatrix[1] = kV[1];
00097                 m_afMatrix[2] = kV[2];
00098                 m_afMatrix[3] = kV[3];
00099                 break;
00100         case 1:
00101                 m_afMatrix[4] = kV[0];
00102                 m_afMatrix[5] = kV[1];
00103                 m_afMatrix[6] = kV[2];
00104                 m_afMatrix[7] = kV[3];
00105                 break;
00106         case 2:
00107                 m_afMatrix[8] = kV[0];
00108                 m_afMatrix[9] = kV[1];
00109                 m_afMatrix[10] = kV[2];
00110                 m_afMatrix[11] = kV[3];
00111                 break;
00112         case 3:
00113                 m_afMatrix[12] = kV[0];
00114                 m_afMatrix[13] = kV[1];
00115                 m_afMatrix[14] = kV[2];
00116                 m_afMatrix[15] = kV[3];
00117                 break;
00118         }
00119 }
00120 
00121 
00122 
00123 void K3dMatrix::GetRow(K3dVector4 &rkRow, const int iRow)
00124 {
00125         if(!(0 <= iRow) && (iRow < 4) ) 
00126         {
00127                 cerr << "Error - K3dMatrix::SetRow() -- error index " << iRow << endl;
00128         }
00129 
00130         switch (iRow)
00131         {
00132         case 0:
00133                 rkRow[0] = m_afMatrix[0];
00134                 rkRow[1] = m_afMatrix[1];
00135                 rkRow[2] = m_afMatrix[2];
00136                 rkRow[3] = m_afMatrix[3];
00137                 break;
00138         case 1:
00139                 rkRow[0] = m_afMatrix[4];
00140                 rkRow[1] = m_afMatrix[5];
00141                 rkRow[2] = m_afMatrix[6];
00142                 rkRow[3] = m_afMatrix[7];
00143                 break;
00144         case 2:
00145                 rkRow[0] = m_afMatrix[8];
00146                 rkRow[1] = m_afMatrix[9];
00147                 rkRow[2] = m_afMatrix[10];
00148                 rkRow[3] = m_afMatrix[11];
00149                 break;
00150         case 3:
00151                 rkRow[0] = m_afMatrix[12];
00152                 rkRow[1] = m_afMatrix[13];
00153                 rkRow[2] = m_afMatrix[14];
00154                 rkRow[3] = m_afMatrix[15];
00155                 break;
00156         }
00157 }
00158 
00159 
00160 
00161 void K3dMatrix::SetColumn(const K3dVector4 &_rkColumn, const int iColumn)
00162 {       
00163         if(!(0 <= iColumn) && (iColumn < 4) ) 
00164         {
00165                 cerr << "Error - K3dMatrix::SetColumn() -- error index " << iColumn << endl;
00166         }
00167         K3dVector4 kV = _rkColumn;
00168         switch (iColumn)
00169         {
00170         case 0:
00171                 m_afMatrix[0] = kV[0];
00172                 m_afMatrix[4] = kV[1];
00173                 m_afMatrix[8] = kV[2];
00174                 m_afMatrix[12] = kV[3];
00175                 break;
00176         case 1:
00177                 m_afMatrix[1] = kV[0];
00178                 m_afMatrix[5] = kV[1];
00179                 m_afMatrix[9] = kV[2];
00180                 m_afMatrix[13] = kV[3];
00181                 break;
00182         case 2:
00183                 m_afMatrix[2] = kV[0];
00184                 m_afMatrix[6] = kV[1];
00185                 m_afMatrix[10] = kV[2];
00186                 m_afMatrix[14] = kV[3];
00187                 break;
00188         case 3:
00189                 m_afMatrix[3] = kV[0];
00190                 m_afMatrix[7] = kV[1];
00191                 m_afMatrix[11] = kV[2];
00192                 m_afMatrix[15] = kV[3];
00193                 break;
00194         }
00195 }
00196 
00197 
00198 
00199 void K3dMatrix::GetColumn(K3dVector4 &rkColumn, const int iColumn)
00200 {
00201         if(!(0 <= iColumn) && (iColumn < 4) ) 
00202         {
00203                 cerr << "Error - K3dMatrix::SetColumn() -- error index " << iColumn << endl;
00204         }
00205 
00206         switch (iColumn)
00207         {
00208         case 0:
00209                 rkColumn[0] = m_afMatrix[0];
00210                 rkColumn[1] = m_afMatrix[4];
00211                 rkColumn[2] = m_afMatrix[8];
00212                 rkColumn[3] = m_afMatrix[12];
00213                 break;
00214         case 1:
00215                 rkColumn[0] = m_afMatrix[1];
00216                 rkColumn[1] = m_afMatrix[5];
00217                 rkColumn[2] = m_afMatrix[9];
00218                 rkColumn[3] = m_afMatrix[13];
00219                 break;
00220         case 2:
00221                 rkColumn[0] = m_afMatrix[2];
00222                 rkColumn[1] = m_afMatrix[6];
00223                 rkColumn[2] = m_afMatrix[10];
00224                 rkColumn[3] = m_afMatrix[14];
00225                 break;
00226         case 3:
00227                 rkColumn[0] = m_afMatrix[3];
00228                 rkColumn[1] = m_afMatrix[7];
00229                 rkColumn[2] = m_afMatrix[11];
00230                 rkColumn[3] = m_afMatrix[15];
00231                 break;
00232         }
00233 }
00234 
00235 
00236 
00237 // Assignment
00238 K3dMatrix& K3dMatrix::operator= (const K3dMatrix& rkM)
00239 {
00240         for(int i=0; i<16; i++)
00241         {
00242                 m_afMatrix[i] = rkM.m_afMatrix[i];
00243         }
00244         return *this;
00245 }
00246 
00247 
00248 
00249 // Compare arrays for comparison operators
00250 int K3dMatrix::CompareArrays(K3dMatrix& rkV)
00251 {
00252         unsigned int uiTest0;
00253         unsigned int uiTest1;
00254 
00255         for (int i = 0; i < 16; i++)
00256         {
00257                 uiTest0 = *(unsigned int*)&m_afMatrix[i];
00258                 uiTest1 = *(unsigned int*)&rkV.m_afMatrix[i];
00259                 if ( uiTest0 < uiTest1 )
00260                         return -1;
00261                 if ( uiTest0 > uiTest1 )
00262                         return +1;
00263         }
00264         return 0;
00265 
00266 }
00267 
00268 
00269 
00270 K3dMatrix K3dMatrix::operator+ (K3dMatrix& rkM)
00271 {
00272         K3dMatrix kSum;
00273         for (int i = 0; i < 16; i++)
00274                 kSum.m_afMatrix[i] = m_afMatrix[i] + rkM.m_afMatrix[i];
00275         return kSum;
00276 }
00277 
00278 
00279 
00280 K3dMatrix K3dMatrix::operator- (K3dMatrix& rkM)
00281 {
00282         K3dMatrix kSum;
00283         for (int i = 0; i < 16; i++)
00284                 kSum.m_afMatrix[i] = m_afMatrix[i] - rkM.m_afMatrix[i];
00285         return kSum;
00286 }
00287 
00288 
00289 
00290 K3dMatrix K3dMatrix::operator* (float fScalar)
00291 {
00292         K3dMatrix kM;
00293         for (int i = 0; i < 16; i++)
00294                 kM.m_afMatrix[i] = m_afMatrix[i] * fScalar;
00295 
00296         return kM;
00297 }
00298 
00299 
00300 
00301 K3dMatrix K3dMatrix::operator- ()
00302 {
00303         K3dMatrix kNeg;
00304         for (int i = 0; i < 16; i++)
00305                 kNeg.m_afMatrix[i] = -m_afMatrix[i];
00306         return kNeg;
00307 }
00308 
00309 
00310 
00311 K3dMatrix& K3dMatrix::operator+= ( K3dMatrix& rkM)
00312 {
00313         for (int i=0; i<16; i++)                                        
00314         {
00315                 this->m_afMatrix[i] += rkM.m_afMatrix[i]; 
00316         }
00317 
00318         return *this;
00319 }
00320 
00321 
00322 
00323 K3dMatrix& K3dMatrix::operator-= ( K3dMatrix& rkM)
00324 {
00325         for (int i=0; i<16; i++)                                        
00326         {
00327                 this->m_afMatrix[i] -= rkM.m_afMatrix[i]; 
00328         }
00329 
00330         return *this;
00331 }
00332 
00333 
00334 
00335 K3dMatrix& K3dMatrix::operator*= (float fScalar)
00336 {
00337         for (int i=0; i<16; i++)                                        
00338         {
00339                 this->m_afMatrix[i] *= fScalar; 
00340         }
00341 
00342         return *this;
00343 }
00344 
00345 
00346 
00347 K3dMatrix& K3dMatrix::operator/= (float fScalar)
00348 {
00349         float fInvScalar;
00350 
00351         if(fScalar)
00352         {
00353                 fInvScalar = 1.0f / fScalar;
00354                 for (int i=0; i<3; i++)                                 
00355                 {
00356                         m_afMatrix[i] *= fInvScalar;    
00357                 }
00358         }
00359         else
00360         {
00361                 this->LoadIdentity();
00362         }
00363 
00364 
00365         return *this;
00366 }
00367 
00368 
00369 
00370 // Multiply row by scalar
00371 void K3dMatrix::MultiplyRow(const float fScalar, const int iRow)
00372 {
00373         int i;
00374 
00375         if(!(0 <= iRow) && (iRow < 4) ) 
00376         {
00377                 cerr << "Error - K3dMatrix::SetColumn() -- error index -" << iRow << endl;
00378         }
00379 
00380         switch (iRow)
00381         {
00382         case 0:
00383                 for(i=0; i<=3; i++)
00384                 {
00385                         m_afMatrix[i] *= fScalar;
00386                 }
00387                 break;
00388         case 1:
00389                 for(i=4; i<=7; i++)
00390                 {
00391                         m_afMatrix[i] *= fScalar;
00392                 }
00393                 break;
00394         case 2:
00395                 for(i=8; i<=11; i++)
00396                 {
00397                         m_afMatrix[i] *= fScalar;
00398                 }
00399                 break;
00400         case 3:
00401                 for(i=12; i<=15; i++)
00402                 {
00403                         m_afMatrix[i] *= fScalar;
00404                 }
00405                 break;
00406         }
00407 
00408 }
00409 
00410 
00411 
00412 // Multiply column by scalar
00413 void K3dMatrix::MultiplyColumn(const float fScalar, const int iColumn)
00414 {
00415         if(!(0 <= iColumn) && (iColumn < 4) )
00416         {
00417                 cerr << "Error - K3dMatrix::MultiplyColumn() -- error index" << iColumn << endl;
00418         }
00419 
00420         switch (iColumn)
00421         {
00422         case 0:
00423                 m_afMatrix[0] *= fScalar;
00424                 m_afMatrix[4] *= fScalar;
00425                 m_afMatrix[8] *= fScalar;
00426                 m_afMatrix[12] *= fScalar;
00427                 break;
00428         case 1:
00429                 m_afMatrix[1] *= fScalar;
00430                 m_afMatrix[5] *= fScalar;
00431                 m_afMatrix[9] *= fScalar;
00432                 m_afMatrix[13] *= fScalar;
00433                 break;
00434         case 2:
00435                 m_afMatrix[2] *= fScalar;
00436                 m_afMatrix[6] *= fScalar;
00437                 m_afMatrix[10] *= fScalar;
00438                 m_afMatrix[14] *= fScalar;
00439                 break;
00440         case 3:
00441                 m_afMatrix[3] *= fScalar;
00442                 m_afMatrix[7] *= fScalar;
00443                 m_afMatrix[11] *= fScalar;
00444                 m_afMatrix[15] *= fScalar;
00445                 break;
00446         }       
00447 }

Generated on Thu Aug 16 23:53:27 2007 for K3dEngine by  doxygen 1.5.0