K3dVector3.h

Go to the documentation of this file.
00001 
00015 /***************************************************************************
00016  *   Copyright (C) 2007 by Jan Koci   *
00017  *   honza.koci@email.cz   *
00018  *   http://kengine.sourceforge.net/tutorial/
00019  *                                                                         *
00020  *   This program is free software; you can redistribute it and/or modify  *
00021  *   it under the terms of the GNU General Public License as published by  *
00022  *   the Free Software Foundation; either version 2 of the License, or     *
00023  *   (at your option) any later version.                                   *
00024  *                                                                         *
00025  *   This program 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         *
00028  *   GNU General Public License for more details.                          *
00029  *                                                                         *
00030  *   You should have received a copy of the GNU General Public License     *
00031  *   along with this program; if not, write to the                         *
00032  *   Free Software Foundation, Inc.,                                       *
00033  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00034  ***************************************************************************/
00035 
00036 
00037 #pragma once
00038 
00039 
00040 #include <iostream>
00041 #include <fstream>
00042 
00043 #include "../System/K3dString.h"
00044 #include "../System/K3dDefines.h"
00045 #include "K3dMath.h"
00046 
00047 
00048 using namespace std;
00049 
00051 // 3D vector class
00053 class K3dVector3
00054 {
00055                 float m_afVector[3];    
00056         public:
00057                 K3dVector3();
00058                 K3dVector3 ( const K3dVector3& _rkV );
00059                 K3dVector3 ( const float afVector[3] );
00060                 K3dVector3 ( float x, float y, float z );
00061                 K3dVector3 ( const unsigned char aucVector[3] );
00062                 ~K3dVector3();
00063 
00065                 // Coordinate access
00067 
00068                 // Get vector array
00069                 float *GetVectorArray()
00070                 {
00071                         return m_afVector;
00072                 }
00073                 const float *GetVectorArray() const
00074                 {
00075                         return m_afVector;
00076                 }
00077                 K3dVector3 &GetVector()
00078                 {
00079                         return *this;
00080                 }
00081 
00082                 const K3dVector3 &GetVector() const
00083                 {
00084                         return *this;
00085                 }
00086 
00087                 void Set ( K3dVector3 &_rV );
00088                 void Set ( float vector[3] );
00089                 void Set ( float x, float y, float z );
00090                 void SetX ( const float fX );
00091                 void SetY ( const float fY );
00092                 void SetZ ( const float fZ );
00094                 float &GetX()
00095                 {
00096                         return m_afVector[0];
00097                 }
00098                 const float &GetX() const
00099                 {
00100                         return m_afVector[0];
00101                 }
00103                 float &GetY()
00104                 {
00105                         return m_afVector[1];
00106                 }
00107                 const float &GetY() const
00108                 {
00109                         return m_afVector[1];
00110                 }
00112                 float &GetZ()
00113                 {
00114                         return m_afVector[2];
00115                 }
00116                 const float &GetZ() const
00117                 {
00118                         return m_afVector[2];
00119                 }
00120                 float GetScalar ( const int index );
00121                 void SetScalar ( const float fScalar, const int index );
00122                 void Reset();
00123 
00125                 // Operators
00127 
00128                 float &operator[] ( int index ) ;
00129                 const float &operator[] ( int index ) const;
00130 
00132                 // Assignment operator
00134                 K3dVector3& operator= ( const K3dVector3& _rkV );
00135 
00137                 // Comparison operators
00139                 bool operator== ( const K3dVector3& _rkV ) const;
00140                 bool operator== ( const K3dVector3& _rkV );
00141                 bool operator!= ( const K3dVector3& _rkV ) const;
00142                 bool operator< ( const K3dVector3& _rkV ) const;
00143                 bool operator<= ( const K3dVector3& _rkV ) const;
00144                 bool operator> ( const K3dVector3& _rkV ) const;
00145                 bool operator>= ( const K3dVector3& _rkV ) const;
00146                 // Compare arrays for comparison operators
00147                 int CompareArrays ( const K3dVector3& _rkV ) const;
00148 
00149 
00151                 // Arithmetic operations
00153 
00154                 // Overloaded operator+
00155                 K3dVector3 operator+ ( const K3dVector3& _rkV ) const;
00156                 K3dVector3 operator+ ( const float fScalar ) const;
00157                 K3dVector3 operator- ( const K3dVector3 &_rkVector ) const;
00158                 K3dVector3 operator- ( const float fScalar ) const;
00159                 K3dVector3 operator- () const;
00160                 K3dVector3 operator* ( const K3dVector3 &_rkVector ) const;
00161                 K3dVector3 operator* ( const float fScalar ) const;
00162                 K3dVector3 operator/ ( const K3dVector3 &_rkVector ) const;
00163                 K3dVector3 operator/ ( const float fScalar ) const;
00164 
00166                 // Arithmetic updates
00168 
00169                 K3dVector3& operator+= ( const K3dVector3 &_rkVector );
00170                 K3dVector3& operator+= ( const float fScalar );
00171                 K3dVector3& operator-= ( const K3dVector3 &_rkVector );
00172                 K3dVector3& operator-= ( const float fScalar );
00173                 K3dVector3 operator*= ( const K3dVector3 &_rkVector );
00174                 K3dVector3 operator*= ( const float fScalar );
00175                 K3dVector3 operator/= ( const K3dVector3 &_rkVector );
00176                 K3dVector3 operator/= ( const float fScalar );
00177 };
00178 
00179 inline bool K3dVector3::operator!= ( const K3dVector3& _rkV ) const
00180 {
00181         return memcmp ( m_afVector, _rkV.m_afVector, 3*sizeof ( float ) ) != 0;
00182 }
00183 
00184 inline bool K3dVector3::operator< ( const K3dVector3& _rkV ) const
00185 {
00186         return CompareArrays ( _rkV ) < 0;
00187 }
00188 
00189 inline bool K3dVector3::operator<= ( const K3dVector3& _rkV ) const
00190 {
00191         return CompareArrays ( _rkV ) <= 0;
00192 }
00193 
00194 inline bool K3dVector3::operator> ( const K3dVector3& _rkV ) const
00195 {
00196         return CompareArrays ( _rkV ) > 0;
00197 }
00198 
00199 inline bool K3dVector3::operator>= ( const K3dVector3& _rkV ) const
00200 {
00201         return CompareArrays ( _rkV ) >= 0;
00202 }
00203 
00204 inline K3dVector3 K3dVector3::operator/= ( float fScalar )
00205 {
00206         float fInvScalar;
00207 
00208         if ( fScalar )
00209         {
00210                 fInvScalar = 1.0f / fScalar;
00211                 for ( int i=0; i<3; i++ )
00212                 {
00213                         m_afVector[i] *= fInvScalar;
00214                 }
00215         }
00216         else
00217         {
00218                 this->Set ( 0,0,0 );
00219         }
00220         return *this;
00221 }
00222 
00223 inline float &K3dVector3::operator[] ( int i )
00224 {
00225         if ( ! ( 0 <= i ) && ( i < 3 ) )
00226         {
00227                 cerr << "Error index -- Index must be from 0 to 2" << i <<endl;
00228                 return m_afVector[0];
00229         }
00230         return m_afVector[i];
00231 }
00232 
00233 inline const float &K3dVector3::operator[] ( int i ) const
00234 {
00235         if ( ! ( 0 <= i ) && ( i < 3 ) )
00236         {
00237                 cerr << "Error index -- Index must be from 0 to 2" << i <<endl;
00238                 return m_afVector[0];
00239         }
00240         return m_afVector[i];
00241 }
00242 
00244 inline float K3dVector3::GetScalar ( const int i )
00245 {
00246         if ( ! ( 0 <= i ) && ( i < 3 ) )
00247         {
00248                 cerr << "Error index -- Index must be from 0 to 2" << i <<endl;
00249                 return ( float ) 0;
00250         }
00251         return m_afVector[i];
00252 }
00253 
00255 inline void K3dVector3::SetScalar ( const float fScalar, const int i )
00256 {
00257         if ( ! ( 0 <= i ) && ( i < 3 ) )
00258         {
00259                 cerr << "Error index -- Index must be from 0 to 2" << i <<endl;
00260                 return;
00261         }
00262         m_afVector[i] = fScalar;
00263 }
00264 
00266 inline void K3dVector3::Set ( float vector[3] )
00267 {
00268         m_afVector[0] = vector[0];
00269         m_afVector[1] = vector[1];
00270         m_afVector[2] = vector[2];
00271 }
00272 
00274 inline void K3dVector3::Set ( float x, float y, float z )
00275 {
00276         m_afVector[0] = x;
00277         m_afVector[1] = y;
00278         m_afVector[2] = z;
00279 }
00280 
00282 inline void K3dVector3::SetX ( const float fX )
00283 {
00284         m_afVector[0] = fX;
00285 }
00286 
00288 inline void K3dVector3::SetY ( const float fY )
00289 {
00290         m_afVector[1] = fY;
00291 }
00292 
00294 inline void K3dVector3::SetZ ( const float fZ )
00295 {
00296         m_afVector[2] = fZ;
00297 }
00298 
00300 inline void K3dVector3::Set ( K3dVector3 &_rV )
00301 {
00302         m_afVector[0] = _rV.m_afVector[0];
00303         m_afVector[1] = _rV.m_afVector[1];
00304         m_afVector[2] = _rV.m_afVector[2];
00305 }
00306 
00307 

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