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 termsK3dVector3 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 00033 #include "K3dVector3Obj.h" 00034 00035 K3dVector3Obj::K3dVector3Obj() 00036 { 00037 m_iId = -1; 00038 } 00039 00040 K3dVector3Obj::K3dVector3Obj ( const K3dVector3Obj &_rkV ) 00041 { 00042 for ( int i=0; i<3; i++ ) 00043 { 00044 GetVector() [i] = _rkV.GetVector() [i]; 00045 } 00046 m_iId = -1; 00047 } 00048 00049 K3dVector3Obj::K3dVector3Obj ( const float _afVector[3] ) 00050 { 00051 for ( int i=0; i<3; i++ ) 00052 { 00053 GetVector() [i] = _afVector[i]; 00054 } 00055 m_iId = -1; 00056 } 00057 00058 K3dVector3Obj::K3dVector3Obj ( const unsigned char _aucVector[3] ) 00059 { 00060 for ( int i=0; i<3; i++ ) 00061 { 00062 GetVector() [i] = ( float ) _aucVector[i]; 00063 } 00064 m_iId = -1; 00065 } 00066 00067 K3dVector3Obj::K3dVector3Obj ( float _fX, float _fY, float _fZ ) 00068 { 00069 GetX() = _fX; // Set x 00070 GetY() = _fY; // Set y 00071 GetZ() = _fZ; // Set z 00072 m_iId = -1; 00073 } 00074 00075 K3dVector3Obj::~K3dVector3Obj() 00076 {} 00077 00078 K3dVector3Obj& K3dVector3Obj::operator= ( const K3dVector3Obj& _rkV ) 00079 { 00080 for ( int i=0; i<3; i++ ) 00081 { 00082 GetVector() [i] = _rkV.GetVector() [i]; 00083 } 00084 return *this; 00085 } 00086 00088 // Comparison operators 00090 bool K3dVector3Obj::operator== ( const K3dVector3Obj &_rkV ) const 00091 { 00092 // Round vector scalars for prevent rounding errors 00093 if((K3dMath::EqualRange ( GetX(), _rkV.GetX(), K_EQUAL_RANGE )) == false) 00094 { 00095 return false; 00096 } 00097 if((K3dMath::EqualRange ( GetY(), _rkV.GetY(), K_EQUAL_RANGE )) == false) 00098 { 00099 return false; 00100 } 00101 if((K3dMath::EqualRange ( GetZ(), _rkV.GetZ(), K_EQUAL_RANGE )) == false) 00102 { 00103 return false; 00104 } 00105 return true; 00106 } 00107 00108 bool K3dVector3Obj::operator!= ( const K3dVector3Obj &_rkV ) const 00109 { 00110 if ( ( GetX() != _rkV.GetX() ) && ( GetY() != _rkV.GetY() ) && ( GetZ() != _rkV.GetZ() ) ) 00111 { 00112 return true; 00113 } 00114 else 00115 { 00116 return false; 00117 } 00118 } 00119 00120 int K3dVector3Obj::CompareArrays ( const K3dVector3Obj &_rkV ) const 00121 { 00122 unsigned int uiTest0; 00123 unsigned int uiTest1; 00124 00125 for ( int i = 0; i < 3; i++ ) 00126 { 00127 uiTest0 = * ( unsigned int* ) &GetVector() [i]; 00128 uiTest1 = * ( unsigned int* ) &_rkV.GetVector() [i]; 00129 if ( uiTest0 < uiTest1 ) 00130 return -1; 00131 if ( uiTest0 > uiTest1 ) 00132 return +1; 00133 } 00134 return 0; 00135 } 00136 00137 bool K3dVector3Obj::operator< ( const K3dVector3Obj &_rkV ) const 00138 { 00139 return CompareArrays ( _rkV ) < 0; 00140 } 00141 00142 bool K3dVector3Obj::operator<= ( const K3dVector3Obj &_rkV ) const 00143 { 00144 return CompareArrays ( _rkV ) <= 0; 00145 } 00146 00147 bool K3dVector3Obj::operator> ( const K3dVector3Obj &_rkV ) const 00148 { 00149 return CompareArrays ( _rkV ) > 0; 00150 } 00151 00152 bool K3dVector3Obj::operator>= ( const K3dVector3Obj &_rkV ) const 00153 { 00154 return CompareArrays ( _rkV ) >= 0; 00155 } 00156 00157 00158 00159 00160 00162 // Arithmetic operations 00164 00165 K3dVector3Obj K3dVector3Obj::operator+ ( const K3dVector3Obj &_rkV ) const 00166 { 00167 K3dVector3Obj kV; 00168 for ( int i = 0; i < 3; i++ ) 00169 { 00170 kV.GetVector() [i] = GetVector() [i] + _rkV.GetVector() [i]; 00171 } 00172 return kV; 00173 } 00174 00175 K3dVector3Obj K3dVector3Obj::operator+ ( const float _fScalar ) const 00176 { 00177 K3dVector3Obj kV; 00178 for ( int i=0; i<3; i++ ) 00179 { 00180 kV.GetVector() [i] = GetVector() [i] + _fScalar; 00181 } 00182 return kV; 00183 } 00184 00185 K3dVector3Obj K3dVector3Obj::operator- ( const K3dVector3Obj &_rkV ) const 00186 { 00187 K3dVector3Obj kV; 00188 for ( int i = 0; i < 3; i++ ) 00189 { 00190 kV.GetVector() [i] = GetVector() [i] - _rkV.GetVector() [i]; 00191 } 00192 return kV; 00193 } 00194 00195 K3dVector3Obj K3dVector3Obj::operator- ( const float _fScalar ) const 00196 { 00197 K3dVector3Obj kV; 00198 for ( int i=0; i<3; i++ ) 00199 { 00200 kV.GetVector() [i] = GetVector() [i] - _fScalar; 00201 } 00202 return kV; 00203 } 00204 00205 K3dVector3Obj K3dVector3Obj::operator- () const 00206 { 00207 K3dVector3Obj kV; 00208 for ( int i=0; i<3; i++ ) 00209 { 00210 kV.GetVector() [i] = -GetVector() [i]; 00211 } 00212 return kV; 00213 } 00214 00215 K3dVector3Obj K3dVector3Obj::operator* ( const K3dVector3Obj &_rkV ) const 00216 { 00217 K3dVector3Obj kV; 00218 for ( int i = 0; i < 3; i++ ) 00219 { 00220 kV.GetVector() [i] = GetVector() [i] * _rkV.GetVector() [i]; 00221 } 00222 return kV; 00223 } 00224 00225 K3dVector3Obj K3dVector3Obj::operator* ( const float _fScalar ) const 00226 { 00227 K3dVector3Obj kV; 00228 for ( int i=0; i<3; i++ ) 00229 { 00230 kV.GetVector() [i] = GetVector() [i] * _fScalar; 00231 } 00232 return kV; 00233 } 00234 00235 K3dVector3Obj K3dVector3Obj::operator/ ( const K3dVector3Obj &_rkV ) const 00236 { 00237 K3dVector3Obj kV; 00238 for ( int i = 0; i < 3; i++ ) 00239 { 00240 if ( _rkV.GetVector() [i] != ( float ) 0 ) 00241 { 00242 kV.GetVector() [i] = GetVector() [i] / _rkV.GetVector() [i]; 00243 } 00244 } 00245 return kV; 00246 } 00247 00248 K3dVector3Obj K3dVector3Obj::operator/ ( const float _fScalar ) const 00249 { 00250 K3dVector3Obj kV; 00251 for ( int i=0; i<3; i++ ) 00252 { 00253 if ( _fScalar != ( float ) 0 ) 00254 { 00255 kV.GetVector() [i] = GetVector() [i] / _fScalar; 00256 } 00257 } 00258 return kV; 00259 } 00260 00261 00262 00264 // Arithmetic updates 00266 00267 K3dVector3Obj& K3dVector3Obj::operator+= ( const K3dVector3Obj &_rkV ) 00268 { 00269 for ( int i=0; i<3; i++ ) 00270 { 00271 GetVector() [i] += _rkV.GetVector() [i]; 00272 } 00273 return *this; 00274 } 00275 00276 K3dVector3Obj& K3dVector3Obj::operator+= ( const float _fScalar ) 00277 { 00278 for ( int i=0; i<3; i++ ) 00279 { 00280 GetVector() [i] += _fScalar; 00281 } 00282 return *this; 00283 } 00284 00285 K3dVector3Obj& K3dVector3Obj::operator-= ( const K3dVector3Obj &_rkV ) 00286 { 00287 for ( int i=0; i<3; i++ ) 00288 { 00289 GetVector() [i] -= _rkV.GetVector() [i]; 00290 } 00291 return *this; 00292 } 00293 00294 K3dVector3Obj& K3dVector3Obj::operator-= ( const float _fScalar ) 00295 { 00296 for ( int i=0; i<3; i++ ) 00297 { 00298 GetVector() [i] -= _fScalar; 00299 } 00300 return *this; 00301 } 00302 00303 K3dVector3Obj& K3dVector3Obj::operator*= ( const K3dVector3Obj &_rkV ) 00304 { 00305 for ( int i=0; i<3; i++ ) 00306 { 00307 GetVector() [i] *= _rkV.GetVector() [i]; 00308 } 00309 return *this; 00310 } 00311 00312 K3dVector3Obj& K3dVector3Obj::operator*= ( const float _fScalar ) 00313 { 00314 for ( int i=0; i<3; i++ ) 00315 { 00316 GetVector() [i] *= _fScalar; 00317 } 00318 return *this; 00319 } 00320 00321 K3dVector3Obj& K3dVector3Obj::operator/= ( const K3dVector3Obj &_rkV ) 00322 { 00323 for ( int i=0; i<3; i++ ) 00324 { 00325 if ( _rkV.GetVector() [i] != ( float ) 0 ) 00326 { 00327 GetVector() [i] /= _rkV.GetVector() [i]; 00328 } 00329 } 00330 return *this; 00331 } 00332 00333 K3dVector3Obj& K3dVector3Obj::operator/= ( const float _fScalar ) 00334 { 00335 for ( int i=0; i<3; i++ ) 00336 { 00337 if ( _fScalar != ( float ) 0 ) 00338 { 00339 GetVector() [i] /= _fScalar; 00340 } 00341 } 00342 return *this; 00343 } 00344 00345