K3dVector4.h

Go to the documentation of this file.
00001 
00011 /***************************************************************************
00012  *   Copyright (C) 2007 by Jan Koci   *
00013  *   honza.koci@email.cz   *
00014  *   http://kengine.sourceforge.net/tutorial/
00015  *                                                                         *
00016  *   This program is free software; you can redistribute it and/or modify  *
00017  *   it under the terms of the GNU General Public License as published by  *
00018  *   the Free Software Foundation; either version 2 of the License, or     *
00019  *   (at your option) any later version.                                   *
00020  *                                                                         *
00021  *   This program is distributed in the hope that it will be useful,       *
00022  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00023  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00024  *   GNU General Public License for more details.                          *
00025  *                                                                         *
00026  *   You should have received a copy of the GNU General Public License     *
00027  *   along with this program; if not, write to the                         *
00028  *   Free Software Foundation, Inc.,                                       *
00029  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00030  ***************************************************************************/
00031 
00032 #pragma once
00033 
00034 #include <iostream>
00035 #include <cmath>
00036 
00037 #include "../System/K3dString.h"
00038 
00040 // 4D vector class
00042 class K3dVector4:
00043                         public K3dString
00044 {
00045                 //      4d vector x,y,z,w
00046                 float m_afVector[4];
00047         public:
00048 
00049 
00050                 //      Reset constructor (0, 0, 0, 0)
00051                 K3dVector4();
00052                 // Constructor set private vector
00053                 K3dVector4 ( const K3dVector4& rkV );
00054                 //      Constructor set vector from array
00055                 K3dVector4 ( const float afVector[4] );
00056                 //      Constructor set vector from x, y, z,w values
00057                 K3dVector4 ( const float fX, const float fY, const float fZ, const float  fW );
00058                 // Constructor to set vector from unsigned char array
00059                 K3dVector4 ( const unsigned char aucVector[4] );
00061                 ~K3dVector4();
00062 
00064                 // Coordinate access
00066 
00067                 // Get vector array
00068                 float *GetVector();
00069                 // Overloaded operator[] allows set vector arrays
00070                 float &operator[] ( int index ) ;
00071                 // Set vector from another vector
00072                 void Set ( K3dVector4 &_rkVec );
00073                 // Set vector from      array values
00074                 void Set ( float _rkVector[4] );
00075                 // Set vector from      (x, y, z) values
00076                 void Set ( const float fX, const float fY, const float fZ, const float fW );
00077                 // Get scalar by index
00078                 float GetScalar ( const int index );
00079                 // Set value by index
00080                 void SetScalar ( const float fScalar, const int index );
00081 
00083                 // Assignment operator
00085                 K3dVector4& operator= ( K3dVector4& rkV );
00086 
00088                 // Comparison operators
00090                 bool operator== ( K3dVector4& rkV );
00091                 bool operator!= ( K3dVector4& rkV );
00092                 bool operator< ( K3dVector4& rkV );
00093                 bool operator<= ( K3dVector4& rkV );
00094                 bool operator> ( K3dVector4& rkV );
00095                 bool operator>= ( K3dVector4& rkV );
00096                 // Compare arrays for comparison operators
00097                 int CompareArrays ( K3dVector4& rkV );
00098 
00099 
00101                 // Arithmetic operations
00103 
00104                 // Overloaded operator+
00105                 K3dVector4 operator+ ( K3dVector4& rkV );
00106                 // Overloaded operator+ allows add vector and float value
00107                 K3dVector4 operator+ ( float fScalar );
00108                 // Overloaded operator- allows subtract two vectors
00109                 K3dVector4 operator- ( K3dVector4 &rkVector );
00110                 // Overloaded operator- allows add vector and float value
00111                 K3dVector4 operator- ( float fScalar );
00112                 K3dVector4 operator- ();
00113                 // Overloaded operator* allows multiply two vectors
00114                 K3dVector4 operator* ( K3dVector4 &rkVector );
00115                 // Overloaded operator* allows multiply vector and float value
00116                 K3dVector4 operator* ( float fScalar );
00117                 // Overloaded operator* allows divide two vectors
00118                 K3dVector4 operator/ ( K3dVector4 &rkVector );
00119                 // Overloaded operator* allows divide two vectors
00120                 K3dVector4 operator/ ( float fScalar );
00121 
00122 
00124                 // Arithmetic updates
00126 
00127                 // Overloaded operator+=
00128                 K3dVector4& operator+= ( const K3dVector4 &rkVector );
00129                 // Overloaded scalar operator+=
00130                 K3dVector4& operator+= ( float fScalar );
00131                 // Overloaded operator-=
00132                 K3dVector4& operator-= ( const K3dVector4 &rkVector );
00133                 // Overloaded scalar operator-=
00134                 K3dVector4& operator-= ( float fScalar );
00135                 // Overloaded operator*= allows multiply two vectors
00136                 K3dVector4 operator*= ( const K3dVector4 &rkVector );
00137                 // Overloaded operator*= allows multiply vector and float value
00138                 K3dVector4 operator*= ( float fScalar );
00139                 // Overloaded operator/= allows divide two vectors
00140                 K3dVector4 operator/= ( const K3dVector4 &rkVector );
00141                 // Overloaded operator/= allows divide vector and float value
00142                 K3dVector4 operator/= ( float fScalar );
00143 
00144 
00146                 // Functions
00148                 // Calc square of vector
00149                 K3dVector4 Sqr();
00150                 //      Reset vector to (0, 0, 0, 0 )
00151                 void Reset();
00152                 // Calculate vector length.
00153                 float Length();
00154                 // Calc square vector length
00155                 float SquaredLength ();
00156                 //      Normalize vector
00157                 void Normalize();
00158                 // Invert vector
00159                 void Invert();
00160                 // Calculate dot product between two vectors
00161                 float Dot ( const K3dVector4 &_rkV1, const K3dVector4 &_rkkV2 );
00162                 // Calculate dot product between two vectors
00163                 float Dot ( const K3dVector4 &_rkV );
00164 
00165 
00166 };
00167 
00168 
00169 
00170 
00171 
00172 // Comparison ==
00173 inline bool K3dVector4::operator== ( K3dVector4& rkV )
00174 {
00175         return memcmp ( m_afVector, rkV.m_afVector, 4*sizeof ( float ) ) == 0;
00176 }
00177 
00178 
00179 
00180 inline bool K3dVector4::operator!= ( K3dVector4& rkV )
00181 {
00182         return memcmp ( m_afVector, rkV.m_afVector, 4*sizeof ( float ) ) != 0;
00183 }
00184 
00185 
00186 
00187 inline bool K3dVector4::operator< ( K3dVector4& rkV )
00188 {
00189         return CompareArrays ( rkV ) < 0;
00190 }
00191 
00192 
00193 
00194 inline bool K3dVector4::operator<= ( K3dVector4& rkV )
00195 {
00196         return CompareArrays ( rkV ) <= 0;
00197 }
00198 
00199 
00200 
00201 inline bool K3dVector4::operator> ( K3dVector4& rkV )
00202 {
00203         return CompareArrays ( rkV ) > 0;
00204 }
00205 
00206 
00207 
00208 inline bool K3dVector4::operator>= ( K3dVector4& rkV )
00209 {
00210         return CompareArrays ( rkV ) >= 0;
00211 }
00212 
00213 
00214 
00215 
00216 // Overloaded operator/= allows divide vector and float value
00217 inline K3dVector4 K3dVector4::operator/= ( float fScalar )
00218 {
00219         float fInvScalar;
00220 
00221         if ( fScalar )
00222         {
00223                 fInvScalar = 1.0f / fScalar;
00224                 for ( int i=0; i<4; i++ )
00225                 {
00226                         m_afVector[i] *= fInvScalar;
00227                 }
00228         }
00229         else
00230         {
00231                 this->Set ( 0,0,0,0 );
00232         }
00233 
00234 
00235         return *this;
00236 }
00237 
00238 
00239 // Overloaded operator* allows divide vector by scalar
00240 inline K3dVector4 K3dVector4::operator/ ( float fScalar )
00241 {
00242         K3dVector4 kV ( 0,0,0,0 );
00243 
00244         if ( fScalar )
00245         {
00246                 for ( int i=0; i<4; i++ )                                               // Go through x, y, z values
00247                 {
00248                         kV.m_afVector[i] = m_afVector[i] / fScalar;
00249                 }
00250         }
00251         return kV;
00252 }
00253 
00254 
00255 
00256 // Overloaded operator[] allows set vector arrays
00257 inline float &K3dVector4::operator[] ( int i )
00258 {
00259         if ( ! ( 0 <= i ) && ( i < 4 ) )
00260         {
00261                 cerr  << "Error :: K3dVector4::operator[] -- wrong index " << i << endl;
00262                 return m_afVector[0];
00263                 //CDebug::InfoMsg("K3dVector4::operator[]() -- error index", i);
00264         }
00265         return m_afVector[i];
00266 }
00267 
00268 
00269 
00270 
00271 
00272 // Get vector pointer
00273 inline float *K3dVector4::GetVector()
00274 {
00275         return m_afVector;
00276 }
00277 
00278 
00279 
00280 // Get scalar by index
00281 inline float K3dVector4::GetScalar ( const int i )
00282 {
00283         if ( ! ( 0 <= i ) && ( i < 4 ) )
00284         {
00285                 cerr  << "Error :: K3dVector4::GetScalar() -- wrong index " << i << endl;
00286                 return ( float ) 0;
00287                 //CDebug::InfoMsg("K3dVector4::operator[]() -- error index", i);
00288         }
00289         return m_afVector[i];
00290 }
00291 
00292 
00293 
00294 // Set scalar by index
00295 inline void K3dVector4::SetScalar ( const float fScalar, const int i )
00296 {
00297         if ( ! ( 0 <= i ) && ( i < 4 ) )
00298         {
00299                 cerr  << "Error :: K3dVector4::SetScalar() -- wrong index " << i << endl;
00300                 return;
00301                 //CDebug::InfoMsg("K3dVector4::operator[]() -- error index", i);
00302         }
00303         m_afVector[i] = fScalar;
00304 }
00305 
00306 
00307 
00308 // Invert vector
00309 inline void K3dVector4::Invert()
00310 {
00311         m_afVector[0] = -m_afVector[0];
00312         m_afVector[1] = -m_afVector[1];
00313         m_afVector[2] = -m_afVector[2];
00314         m_afVector[3] = -m_afVector[3];
00315 
00316 }
00317 
00318 
00319 
00320 // Calculate dot product between two vectors
00321 inline float K3dVector4::Dot ( const K3dVector4 &_rkV1, const K3dVector4 &_rkV2 )
00322 {
00323         float fX,fY, fZ, fW, fResult;
00324 
00325         fX = _rkV1.m_afVector[0] * _rkV2.m_afVector[0];
00326         fY = _rkV1.m_afVector[1] * _rkV2.m_afVector[1];
00327         fZ = _rkV1.m_afVector[2] * _rkV2.m_afVector[2];
00328         fW = _rkV1.m_afVector[3] * _rkV2.m_afVector[3];
00329 
00330         fResult = fX + fY + fZ + fW;
00331         return fResult;
00332 }
00333 
00334 
00335 
00336 
00337 // Calculate dot product between two vectors
00338 inline float K3dVector4::Dot ( const K3dVector4 &_rkVector )
00339 {
00340 
00341         float fX,fY, fZ, fW, fResult;
00342 
00343         fX = m_afVector[0] * _rkVector.m_afVector[0];
00344         fY = m_afVector[1] * _rkVector.m_afVector[1];
00345         fZ = m_afVector[2] * _rkVector.m_afVector[2];
00346         fW = m_afVector[3] * _rkVector.m_afVector[3];
00347 
00348         fResult = fX + fY + fZ + fW;
00349         return fResult;
00350 
00351 }
00352 
00353 
00354 
00355 //      Reset vector to (0, 0, 0, 0)
00356 inline void K3dVector4::Reset()
00357 {
00358         m_afVector[0] = m_afVector[1] = m_afVector[2] = m_afVector[3] = 0;
00359 }
00360 
00361 
00362 
00363 // Set vector from another vector
00364 inline void K3dVector4::Set ( K3dVector4 &_rkVec )
00365 {
00366         m_afVector[0] = _rkVec.m_afVector[0];
00367         m_afVector[1] = _rkVec.m_afVector[1];
00368         m_afVector[2] = _rkVec.m_afVector[2];
00369         m_afVector[3] = _rkVec.m_afVector[3];
00370 }
00371 
00372 
00373 
00374 // Set x,y,z coordinates from array
00375 inline void K3dVector4::Set ( float vector[4] )
00376 {
00377         m_afVector[0] = vector[0];
00378         m_afVector[1] = vector[1];
00379         m_afVector[2] = vector[2];
00380         m_afVector[3] = vector[3];
00381 }
00382 
00383 
00384 
00385 // Set vector from      (x, y, z, w) values
00386 inline void K3dVector4::Set ( const float fX, const float fY, const float fZ, const float fW )
00387 {
00388         m_afVector[0] = fX;
00389         m_afVector[1] = fY;
00390         m_afVector[2] = fZ;
00391         m_afVector[3] = fW;
00392 }
00393 
00394 
00395 
00396 
00397 //      Normalize vector
00398 inline void K3dVector4::Normalize()
00399 {
00400 
00401         float fLength0, fLength1;
00402 
00403         // Calculate vector lenght
00404         fLength0 = Length();
00405 
00406         if ( fLength0 )
00407         {
00408                 fLength1 = 1.0f/fLength0;
00409                 m_afVector[0] *= fLength1;
00410                 m_afVector[1] *= fLength1;
00411                 m_afVector[2] *= fLength1;
00412                 m_afVector[3] *= fLength1;
00413         }
00414 }
00415 
00416 
00417 
00418 // Calculate vector length
00419 inline float K3dVector4::Length()
00420 {
00421         return ( float ) sqrt ( m_afVector[0] * m_afVector[0] + m_afVector[1] * m_afVector[1] + m_afVector[2] * m_afVector[2] + m_afVector[3] * m_afVector[3] );
00422 }
00423 
00424 
00425 
00426 // Calc square vector length
00427 inline float K3dVector4::SquaredLength ()
00428 {
00429         float fSqrLen = 0.0f;
00430         for ( int i = 0; i < 4; i++ )
00431                 fSqrLen += m_afVector[i]*m_afVector[i];
00432         return fSqrLen;
00433 }
00434 
00435 
00436 
00437 // Calc square of vector
00438 inline K3dVector4 K3dVector4::Sqr()
00439 {
00440         K3dVector4 kV;
00441 
00442         kV = *this;
00443 
00444         kV *= kV;
00445 
00446         return kV;
00447 }
00448 

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