K3dVector3.cpp

Go to the documentation of this file.
00001 
00014 /***************************************************************************
00015  *   Copyright (C) 2007 by Jan Koci   *
00016  *   honza.koci@email.cz   *
00017  *   http://kengine.sourceforge.net/tutorial/
00018  *                                                                         *
00019  *   This program is free software; you can redistribute it and/or modify  *
00020  *   it under the terms of the GNU General Public License as published by  *
00021  *   the Free Software Foundation; either version 2 of the License, or     *
00022  *   (at your option) any later version.                                   *
00023  *                                                                         *
00024  *   This program is distributed in the hope that it will be useful,       *
00025  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00026  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00027  *   GNU General Public License for more details.                          *
00028  *                                                                         *
00029  *   You should have received a copy of the GNU General Public License     *
00030  *   along with this program; if not, write to the                         *
00031  *   Free Software Foundation, Inc.,                                       *
00032  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00033  ***************************************************************************/
00034 
00035 
00036 #include "K3dVector3.h"
00037 
00038 K3dVector3::K3dVector3()
00039 {
00040         Reset();
00041 }
00042 
00043 K3dVector3::K3dVector3 ( const K3dVector3& _rkV )
00044 {
00045         for ( int i=0; i<3; i++ )
00046         {
00047                 m_afVector[i] = _rkV.m_afVector[i];
00048         }
00049 }
00050 
00051 K3dVector3::K3dVector3 ( const float afVector[3] )
00052 {
00053         m_afVector[0] = afVector[0];    // Set x
00054         m_afVector[1] = afVector[1];    // Set y
00055         m_afVector[2] = afVector[2];    // Set z
00056 }
00057 
00058 K3dVector3::K3dVector3 ( const unsigned char aucVector[3] )
00059 {
00060         m_afVector[0] = ( float ) aucVector[0]; // Set x
00061         m_afVector[1] = ( float ) aucVector[1]; // Set y
00062         m_afVector[2] = ( float ) aucVector[2]; // Set z
00063 }
00064 
00065 K3dVector3::K3dVector3 ( float x, float y, float z )
00066 {
00067         m_afVector[0] = x;      // Set x
00068         m_afVector[1] = y;      // Set y
00069         m_afVector[2] = z;      // Set z
00070 }
00071 
00072 K3dVector3::~K3dVector3()
00073 {
00074         Reset();
00075 }
00076 
00077 void K3dVector3::Reset()
00078 {
00079         m_afVector[0] = m_afVector[1] = m_afVector[2] = ( float ) 0;
00080 }
00081 
00082 K3dVector3& K3dVector3::operator= ( const K3dVector3& _rkV )
00083 {
00084         for ( int i=0; i<3; i++ )
00085         {
00086                 m_afVector[i] = _rkV.m_afVector[i];
00087         }
00088         return *this;
00089 }
00090 
00091 K3dVector3 K3dVector3::operator+ ( const K3dVector3& _rkV ) const
00092 {
00093         K3dVector3 kSum;
00094         for ( int i = 0; i < 3; i++ )
00095                 kSum.m_afVector[i] = m_afVector[i] + _rkV.m_afVector[i];
00096         return kSum;
00097 }
00098 
00099 K3dVector3 K3dVector3::operator+ ( const float fScalar ) const
00100 {
00101 
00102         float afValue[3];
00103         for ( int i=0; i<3; i++ )
00104         {
00105                 afValue[i] = m_afVector[i] + fScalar;
00106         }
00107         return K3dVector3 ( afValue );
00108 }
00109 
00110 K3dVector3& K3dVector3::operator+= ( const K3dVector3 &_rkVector )
00111 {
00112         for ( int i=0; i<3; i++ )
00113         {
00114                 this->m_afVector[i] += _rkVector.m_afVector[i];
00115         }
00116 
00117         return *this;
00118 }
00119 
00120 K3dVector3& K3dVector3::operator+= ( const float fScalar )
00121 {
00122         for ( int i=0; i<3; i++ )
00123         {
00124                 this->m_afVector[i] += fScalar;
00125         }
00126 
00127         return *this;
00128 }
00129 
00130 K3dVector3& K3dVector3::operator-= ( const float fScalar )
00131 {
00132         for ( int i=0; i<3; i++ )
00133         {
00134                 this->m_afVector[i] -= fScalar;
00135         }
00136 
00137         return *this;
00138 }
00139 
00140 K3dVector3& K3dVector3::operator-= ( const K3dVector3 &_rkVector )
00141 {
00142         m_afVector[0] -= _rkVector.m_afVector[0];
00143         m_afVector[1] -= _rkVector.m_afVector[1];
00144         m_afVector[2] -= _rkVector.m_afVector[2];
00145         return *this;
00146 }
00147 
00148 K3dVector3 K3dVector3::operator- ( const K3dVector3 &_rkVector ) const
00149 {
00150         float f_value[3];
00151         for ( int i=0; i<3; i++ )
00152         {
00153                 f_value[i] = m_afVector[i] - _rkVector[i];
00154         }
00155 
00156         return K3dVector3 ( f_value );
00157 }
00158 
00159 K3dVector3 K3dVector3::operator- ( const float f ) const
00160 {
00161         float afValue[3];
00162         for ( int i=0; i<3; i++ )
00163         {
00164                 afValue[i] = m_afVector[i] - f;
00165         }
00166 
00167         return K3dVector3 ( afValue );
00168 }
00169 
00170 K3dVector3 K3dVector3::operator- () const
00171 {
00172         K3dVector3 kNeg;
00173         for ( int i = 0; i < 3; i++ )
00174                 kNeg.m_afVector[i] = -m_afVector[i];
00175         return kNeg;
00176 }
00177 
00178 K3dVector3 K3dVector3::operator* ( const float num ) const
00179 {
00180         float f_value[3];
00181         for ( int i=0; i<3; i++ )
00182         {
00183                 f_value[i] = m_afVector[i] * num;
00184 
00185         }
00186         return K3dVector3 ( f_value );
00187 }
00188 
00189 K3dVector3 K3dVector3::operator*= ( const K3dVector3 &_rkVector )
00190 {
00191         for ( int i=0; i<3; i++ )
00192         {
00193                 m_afVector[i] *= _rkVector.m_afVector[i];
00194         }
00195         return *this;
00196 }
00197 
00198 K3dVector3 K3dVector3::operator*= ( const float fScalar )
00199 {
00200         for ( int i=0; i<3; i++ )
00201         {
00202                 m_afVector[i] *= fScalar;
00203         }
00204         return *this;
00205 }
00206 
00207 K3dVector3 K3dVector3::operator/= ( const K3dVector3 &_rkVector )
00208 {
00209         for ( int i=0; i<3; i++ )
00210         {
00211                 if ( _rkVector.m_afVector[i] )
00212                 {
00213                         m_afVector[i] /= _rkVector.m_afVector[i];
00214                 }
00215                 else
00216                 {
00217                         m_afVector[i] = 0;
00218                 }
00219         }
00220         return *this;
00221 }
00222 
00223 K3dVector3 K3dVector3::operator* ( const K3dVector3 &_rkVector ) const
00224 {
00225         float af[3];
00226         for ( int i=0; i<3; i++ )
00227         {
00228                 af[i] = m_afVector[i] * _rkVector.m_afVector[i];
00229         }
00230         return K3dVector3 ( af );
00231 }
00232 
00233 K3dVector3 K3dVector3::operator/ ( const K3dVector3 &_rkVector ) const
00234 {
00235         K3dVector3 kV ( 0,0,0 );
00236 
00237         for ( int i=0; i<3; i++ )
00238         {
00239                 if ( _rkVector.m_afVector[i] )
00240                 {
00241                         kV.m_afVector[i] = m_afVector[i] / _rkVector.m_afVector[i];
00242                 }
00243         }
00244         return kV;
00245 }
00246 
00247 K3dVector3 K3dVector3::operator/ ( const float fScalar ) const
00248 {
00249         K3dVector3 kV ( 0,0,0 );
00250         float fInvScalar;
00251         if ( fScalar )
00252         {
00253                 fInvScalar = 1.0f / fScalar;
00254                 for ( int i=0; i<3; i++ )
00255                 {
00256                         kV.m_afVector[i] = m_afVector[i] * fInvScalar;
00257                 }
00258         }
00259         return kV;
00260 }
00261 
00262 int K3dVector3::CompareArrays ( const K3dVector3& _rkV ) const
00263 {
00264         unsigned int uiTest0;
00265         unsigned int uiTest1;
00266 
00267         for ( int i = 0; i < 3; i++ )
00268         {
00269                 uiTest0 = * ( unsigned int* ) &m_afVector[i];
00270                 uiTest1 = * ( unsigned int* ) &_rkV.m_afVector[i];
00271                 if ( uiTest0 < uiTest1 )
00272                         return -1;
00273                 if ( uiTest0 > uiTest1 )
00274                         return +1;
00275         }
00276         return 0;
00277 }
00278 
00279 bool K3dVector3::operator== ( const K3dVector3& _rkV ) const
00280 {
00281         // Round vector scalars for prevent rounding errors
00282         if((K3dMath::EqualRange ( GetX(), _rkV.GetX(), K_EQUAL_RANGE )) == false)
00283         {
00284                 return false;
00285         }
00286         if((K3dMath::EqualRange ( GetY(), _rkV.GetY(), K_EQUAL_RANGE )) == false)
00287         {
00288                 return false;
00289         }
00290         if((K3dMath::EqualRange ( GetZ(), _rkV.GetZ(), K_EQUAL_RANGE )) == false)
00291         {
00292                 return false;
00293         }
00294         return true;
00295 }
00296 
00297 bool K3dVector3::operator== ( const K3dVector3& _rkV )
00298 {
00299         // Round vector scalars for prevent rounding errors
00300         if((K3dMath::EqualRange ( GetX(), _rkV.GetX(), K_EQUAL_RANGE )) == false)
00301         {
00302                 return false;
00303         }
00304         if((K3dMath::EqualRange ( GetY(), _rkV.GetY(), K_EQUAL_RANGE )) == false)
00305         {
00306                 return false;
00307         }
00308         if((K3dMath::EqualRange ( GetZ(), _rkV.GetZ(), K_EQUAL_RANGE )) == false)
00309         {
00310                 return false;
00311         }
00312         return true;
00313 }
00314 
00315 
00316 

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