00001
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #pragma once
00033
00034 #include <iostream>
00035 #include <cmath>
00036
00037 #include "../System/K3dString.h"
00038
00040
00042 class K3dVector4:
00043 public K3dString
00044 {
00045
00046 float m_afVector[4];
00047 public:
00048
00049
00050
00051 K3dVector4();
00052
00053 K3dVector4 ( const K3dVector4& rkV );
00054
00055 K3dVector4 ( const float afVector[4] );
00056
00057 K3dVector4 ( const float fX, const float fY, const float fZ, const float fW );
00058
00059 K3dVector4 ( const unsigned char aucVector[4] );
00061 ~K3dVector4();
00062
00064
00066
00067
00068 float *GetVector();
00069
00070 float &operator[] ( int index ) ;
00071
00072 void Set ( K3dVector4 &_rkVec );
00073
00074 void Set ( float _rkVector[4] );
00075
00076 void Set ( const float fX, const float fY, const float fZ, const float fW );
00077
00078 float GetScalar ( const int index );
00079
00080 void SetScalar ( const float fScalar, const int index );
00081
00083
00085 K3dVector4& operator= ( K3dVector4& rkV );
00086
00088
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
00097 int CompareArrays ( K3dVector4& rkV );
00098
00099
00101
00103
00104
00105 K3dVector4 operator+ ( K3dVector4& rkV );
00106
00107 K3dVector4 operator+ ( float fScalar );
00108
00109 K3dVector4 operator- ( K3dVector4 &rkVector );
00110
00111 K3dVector4 operator- ( float fScalar );
00112 K3dVector4 operator- ();
00113
00114 K3dVector4 operator* ( K3dVector4 &rkVector );
00115
00116 K3dVector4 operator* ( float fScalar );
00117
00118 K3dVector4 operator/ ( K3dVector4 &rkVector );
00119
00120 K3dVector4 operator/ ( float fScalar );
00121
00122
00124
00126
00127
00128 K3dVector4& operator+= ( const K3dVector4 &rkVector );
00129
00130 K3dVector4& operator+= ( float fScalar );
00131
00132 K3dVector4& operator-= ( const K3dVector4 &rkVector );
00133
00134 K3dVector4& operator-= ( float fScalar );
00135
00136 K3dVector4 operator*= ( const K3dVector4 &rkVector );
00137
00138 K3dVector4 operator*= ( float fScalar );
00139
00140 K3dVector4 operator/= ( const K3dVector4 &rkVector );
00141
00142 K3dVector4 operator/= ( float fScalar );
00143
00144
00146
00148
00149 K3dVector4 Sqr();
00150
00151 void Reset();
00152
00153 float Length();
00154
00155 float SquaredLength ();
00156
00157 void Normalize();
00158
00159 void Invert();
00160
00161 float Dot ( const K3dVector4 &_rkV1, const K3dVector4 &_rkkV2 );
00162
00163 float Dot ( const K3dVector4 &_rkV );
00164
00165
00166 };
00167
00168
00169
00170
00171
00172
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
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
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++ )
00247 {
00248 kV.m_afVector[i] = m_afVector[i] / fScalar;
00249 }
00250 }
00251 return kV;
00252 }
00253
00254
00255
00256
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
00264 }
00265 return m_afVector[i];
00266 }
00267
00268
00269
00270
00271
00272
00273 inline float *K3dVector4::GetVector()
00274 {
00275 return m_afVector;
00276 }
00277
00278
00279
00280
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
00288 }
00289 return m_afVector[i];
00290 }
00291
00292
00293
00294
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
00302 }
00303 m_afVector[i] = fScalar;
00304 }
00305
00306
00307
00308
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
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
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
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
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
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
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
00398 inline void K3dVector4::Normalize()
00399 {
00400
00401 float fLength0, fLength1;
00402
00403
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
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
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
00438 inline K3dVector4 K3dVector4::Sqr()
00439 {
00440 K3dVector4 kV;
00441
00442 kV = *this;
00443
00444 kV *= kV;
00445
00446 return kV;
00447 }
00448