00001 #pragma once
00002
00003 #include "common.h"
00004 #include <math.h>
00005
00007 class Vector
00008 {
00009 public:
00013 Vector()
00014 {
00015 m_vfElements[0] = 0.0f;
00016 m_vfElements[1] = 0.0f;
00017 m_vfElements[2] = 0.0f;
00018 };
00019
00026 Vector(const float fX, const float fY, const float fZ)
00027 {
00028 m_vfElements[0] = fX;
00029 m_vfElements[1] = fY;
00030 m_vfElements[2] = fZ;
00031 };
00032
00033
00038 Vector::Vector(const float vfValues[])
00039 {
00040 m_vfElements[0] = vfValues[0];
00041 m_vfElements[1] = vfValues[1];
00042 m_vfElements[2] = vfValues[2];
00043 };
00044
00045
00049 ~Vector()
00050 {
00051 };
00052
00053
00060 void Set(const float fX, const float fY, const float fZ)
00061 {
00062 m_vfElements[0] = fX;
00063 m_vfElements[1] = fY;
00064 m_vfElements[2] = fZ;
00065 };
00066
00067
00072 void SetX(const float fX)
00073 {
00074 m_vfElements[0] = fX;
00075 };
00076
00081 void SetY(const float fY)
00082 {
00083 m_vfElements[1] = fY;
00084 };
00085
00090 void SetZ(const float fZ)
00091 {
00092 m_vfElements[2] = fZ;
00093 };
00094
00099 const float * Get() const
00100 {
00101 return m_vfElements;
00102 };
00103
00109 const float Get(const unsigned int uIndex) const
00110 {
00111 return m_vfElements[uIndex];
00112 };
00113
00118 const float GetX() const
00119 {
00120 return m_vfElements[0];
00121 };
00122
00127 const float GetY() const
00128 {
00129 return m_vfElements[1];
00130 };
00131
00136 const float GetZ() const
00137 {
00138 return m_vfElements[2];
00139 };
00140
00145 const float GetMagnitude() const
00146 {
00147 return sqrtf(GetX()*GetX() + GetY()*GetY() + GetZ()*GetZ());
00148 };
00149
00154 const float GetSquaredMagnitude() const
00155 {
00156 return (GetX()*GetX() + GetY()*GetY() + GetZ()*GetZ());
00157 };
00158
00163 const float GetDot(const Vector &vecOther) const
00164 {
00165 return (GetX() * vecOther.GetX() + GetY()*vecOther.GetY() + GetZ()*vecOther.GetZ());
00166 };
00167
00172 const Vector GetCross(const Vector &vecOther) const
00173 {
00174 return Vector(GetY()*vecOther.GetZ() - GetZ()*vecOther.GetY(), GetZ()*vecOther.GetX() - GetX() * vecOther.GetZ(), GetX()*vecOther.GetY() - GetY()*vecOther.GetX());
00175 };
00176
00181 const Vector GetNormalized() const
00182 {
00183 Vector vecNew = *this;
00184 float fMagnitude = vecNew.GetMagnitude();
00185
00186 if (fMagnitude > 0.0f)
00187 vecNew /= fMagnitude;
00188
00189 return vecNew;
00190 };
00191
00196 const Vector GetInverse() const
00197 {
00198 return Vector(1.0f/GetX(),1.0f/GetY(),1.0f/GetZ());
00199 };
00200
00204 void normalize()
00205 {
00206 (*this) = GetNormalized();
00207 };
00208
00212 void invert()
00213 {
00214 (*this) = GetInverse();
00215 };
00216
00217 const bool operator==(const Vector & vecOther) const
00218 {
00219 return (GetX() == vecOther.GetX()) && (GetY() == vecOther.GetY()) && (GetZ() == vecOther.GetZ());
00220 };
00221
00222 const bool operator!=(const Vector & vecOther) const
00223 {
00224 return !(*this == vecOther);
00225 };
00226
00227 float & operator[](const unsigned int uIndex)
00228 {
00229 return m_vfElements[uIndex];
00230 };
00231
00232 const float operator[](const unsigned int uIndex) const
00233 {
00234 return m_vfElements[uIndex];
00235 };
00236
00237 const Vector & operator+=(const Vector & vecOther)
00238 {
00239 SetX(GetX()+vecOther.GetX());
00240 SetY(GetY()+vecOther.GetY());
00241 SetZ(GetZ()+vecOther.GetZ());
00242 return *this;
00243 };
00244
00245 const Vector & operator-=(const Vector & vecOther)
00246 {
00247 SetX(GetX()-vecOther.GetX());
00248 SetY(GetY()-vecOther.GetY());
00249 SetZ(GetZ()-vecOther.GetZ());
00250 return *this;
00251 };
00252
00253 const Vector & operator*=(const Vector & vecOther)
00254 {
00255 SetX(GetX()*vecOther.GetX());
00256 SetY(GetY()*vecOther.GetY());
00257 SetZ(GetZ()*vecOther.GetZ());
00258 return *this;
00259 };
00260
00261 const Vector & operator*=(const float & fOther)
00262 {
00263 SetX(GetX()*fOther);
00264 SetY(GetY()*fOther);
00265 SetZ(GetZ()*fOther);
00266 return *this;
00267 };
00268
00269 const Vector & operator/=(const Vector & vecOther)
00270 {
00271 SetX(GetX()/vecOther.GetX());
00272 SetY(GetY()/vecOther.GetY());
00273 SetZ(GetZ()/vecOther.GetZ());
00274 return *this;
00275 };
00276
00277 const Vector & operator/=(const float & fOther)
00278 {
00279 SetX(GetX()/fOther);
00280 SetY(GetY()/fOther);
00281 SetZ(GetZ()/fOther);
00282 return *this;
00283 };
00284
00285 const Vector operator+(const Vector & vecOther) const
00286 {
00287 Vector vecNew = *this;
00288 vecNew += vecOther;
00289 return vecNew;
00290 };
00291
00292 const Vector operator-(const Vector & vecOther) const
00293 {
00294 Vector vecNew = *this;
00295 vecNew -= vecOther;
00296 return vecNew;
00297 };
00298
00299 const Vector operator*(const Vector & vecOther) const
00300 {
00301 Vector vecNew = *this;
00302 vecNew *= vecOther;
00303 return vecNew;
00304 };
00305
00306 const Vector operator*(const float & fOther) const
00307 {
00308 Vector vecNew = *this;
00309 vecNew *= fOther;
00310 return vecNew;
00311 };
00312
00313 const Vector operator/(const Vector & vecOther) const
00314 {
00315 Vector vecNew = *this;
00316 vecNew /= vecOther;
00317 return vecNew;
00318 };
00319
00320 const Vector operator/(const float & fOther) const
00321 {
00322 Vector vecNew = *this;
00323 vecNew /= fOther;
00324 return vecNew;
00325 };
00326
00327 const Vector operator-() const
00328 {
00329 return Vector(-GetX(),-GetY(),-GetZ());
00330 };
00331
00332 private:
00334 float m_vfElements[3];
00335 };
00336
00337 inline std::ostream & operator<< (std::ostream & os, const Vector & vecVector)
00338 {
00339 os << "(" << vecVector.GetX() << ";" << vecVector.GetY() << ";" << vecVector.GetZ() << ")";
00340 return os;
00341 }
00342
00343 inline std::istream & operator>> (std::istream & is, Vector & vecVector)
00344 {
00345
00346 float fX,fY,fZ;
00347 if (is >> eat("(") >> fX >> eat(";") >> fY >> eat(";") >> fZ >> eat(")"))
00348 vecVector.Set(fX,fY,fZ);
00349
00350 return is;
00351 }