src/Vector.h

Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include "common.h"
00004 
00005 // 3-Vector class
00006 class Vector
00007 {
00008 public:
00009         Vector()
00010         {
00011                 m_vfElements[0] = 0.0f;
00012                 m_vfElements[1] = 0.0f;
00013                 m_vfElements[2] = 0.0f;
00014         };
00015 
00016         Vector(const float fX, const float fY, const float fZ)
00017         {
00018                 m_vfElements[0] = fX;
00019                 m_vfElements[1] = fY;
00020                 m_vfElements[2] = fZ;
00021         };
00022 
00023         Vector::Vector(const float vfValues[])
00024         {
00025                 m_vfElements[0] = vfValues[0];
00026                 m_vfElements[1] = vfValues[1];
00027                 m_vfElements[2] = vfValues[2];
00028         };
00029 
00030         ~Vector()
00031         {
00032         };
00033 
00034         void Set(const float fX, const float fY, const float fZ)
00035         {
00036                 m_vfElements[0] = fX;
00037                 m_vfElements[1] = fY;
00038                 m_vfElements[2] = fZ;
00039         };
00040 
00041         void SetX(const float fX)
00042         {
00043                 m_vfElements[0] = fX;
00044         };
00045 
00046         void SetY(const float fY)
00047         {
00048                 m_vfElements[1] = fY;
00049         };
00050 
00051         void SetZ(const float fZ)
00052         {
00053                 m_vfElements[2] = fZ;
00054         };
00055 
00056         const float * Get() const
00057         {
00058                 return m_vfElements;
00059         };
00060 
00061         const float Get(const unsigned int uIndex) const
00062         {
00063                 return m_vfElements[uIndex];
00064         };
00065 
00066         const float GetX() const
00067         {
00068                 return m_vfElements[0];
00069         };
00070 
00071         const float GetY() const
00072         {
00073                 return m_vfElements[1];
00074         };
00075 
00076         const float GetZ() const
00077         {
00078                 return m_vfElements[2];
00079         };
00080 
00081         const float GetMagnitude() const
00082         {
00083                 return sqrtf(GetX()*GetX() + GetY()*GetY() + GetZ()*GetZ());
00084         };
00085 
00086         const float GetSquaredMagnitude() const
00087         {
00088                 return (GetX()*GetX() + GetY()*GetY() + GetZ()*GetZ());
00089         };
00090 
00091         const float GetDot(const Vector &vecOther) const
00092         {
00093                 return (GetX() * vecOther.GetX() + GetY()*vecOther.GetY() + GetZ()*vecOther.GetZ());
00094         };
00095 
00096         const Vector GetCross(const Vector &vecOther) const
00097         {
00098                 return Vector(GetY()*vecOther.GetZ() - GetZ()*vecOther.GetY(), GetZ()*vecOther.GetX() - GetX() * vecOther.GetZ(), GetX()*vecOther.GetY() - GetY()*vecOther.GetX()); 
00099         };
00100 
00101         const Vector GetNormalized() const
00102         {
00103                 Vector vecNew = *this;
00104                 float fMagnitude = vecNew.GetMagnitude();
00105 
00106                 if (fMagnitude > 0.0f)
00107                         vecNew /= fMagnitude;
00108 
00109                 return vecNew;
00110         };
00111 
00112         const Vector GetInverse() const
00113         {
00114                 return Vector(1.0f/GetX(),1.0f/GetY(),1.0f/GetZ());
00115         };
00116 
00117         void normalize()
00118         {
00119                 (*this) = GetNormalized();
00120         };
00121 
00122         void invert()
00123         {
00124                 (*this) = GetInverse();
00125         };
00126 
00127         const bool operator==(const Vector & vecOther) const
00128         {
00129                 return (GetX() == vecOther.GetX()) && (GetY() == vecOther.GetY()) && (GetZ() == vecOther.GetZ());
00130         };
00131 
00132         const bool operator!=(const Vector & vecOther) const
00133         {
00134                 return !(*this == vecOther);
00135         };
00136 
00137         float & operator[](const unsigned int uIndex)
00138         {
00139                 return m_vfElements[uIndex];
00140         };
00141 
00142         const float operator[](const unsigned int uIndex) const
00143         {
00144                 return m_vfElements[uIndex];
00145         };
00146 
00147         const Vector & operator+=(const Vector & vecOther)
00148         {
00149                 SetX(GetX()+vecOther.GetX());
00150                 SetY(GetY()+vecOther.GetY());
00151                 SetZ(GetZ()+vecOther.GetZ());
00152                 return *this;
00153         };
00154 
00155         const Vector & operator-=(const Vector & vecOther)
00156         {
00157                 SetX(GetX()-vecOther.GetX());
00158                 SetY(GetY()-vecOther.GetY());
00159                 SetZ(GetZ()-vecOther.GetZ());
00160                 return *this;
00161         };
00162 
00163         const Vector & operator*=(const Vector & vecOther)
00164         {
00165                 SetX(GetX()*vecOther.GetX());
00166                 SetY(GetY()*vecOther.GetY());
00167                 SetZ(GetZ()*vecOther.GetZ());
00168                 return *this;
00169         };
00170 
00171         const Vector & operator*=(const float & fOther)
00172         {
00173                 SetX(GetX()*fOther);
00174                 SetY(GetY()*fOther);
00175                 SetZ(GetZ()*fOther);
00176                 return *this;
00177         };
00178 
00179         const Vector & operator/=(const Vector & vecOther)
00180         {
00181                 SetX(GetX()/vecOther.GetX());
00182                 SetY(GetY()/vecOther.GetY());
00183                 SetZ(GetZ()/vecOther.GetZ());
00184                 return *this;
00185         };
00186 
00187         const Vector & operator/=(const float & fOther)
00188         {
00189                 SetX(GetX()/fOther);
00190                 SetY(GetY()/fOther);
00191                 SetZ(GetZ()/fOther);
00192                 return *this;
00193         };
00194 
00195         const Vector operator+(const Vector & vecOther) const
00196         {
00197                 Vector vecNew = *this;
00198                 vecNew += vecOther;
00199                 return vecNew;
00200         };
00201 
00202         const Vector operator-(const Vector & vecOther) const
00203         {
00204                 Vector vecNew = *this;
00205                 vecNew -= vecOther;
00206                 return vecNew;
00207         };
00208 
00209         const Vector operator*(const Vector & vecOther) const
00210         {
00211                 Vector vecNew = *this;
00212                 vecNew *= vecOther;
00213                 return vecNew;
00214         };
00215 
00216         const Vector operator*(const float & fOther) const
00217         {
00218                 Vector vecNew = *this;
00219                 vecNew *= fOther;
00220                 return vecNew;
00221         };
00222 
00223         const Vector operator/(const Vector & vecOther) const
00224         {
00225                 Vector vecNew = *this;
00226                 vecNew /= vecOther;
00227                 return vecNew;
00228         };
00229 
00230         const Vector operator/(const float & fOther) const
00231         {
00232                 Vector vecNew = *this;
00233                 vecNew /= fOther;
00234                 return vecNew;
00235         };
00236 
00237         const Vector operator-() const
00238         {
00239                 return Vector(-GetX(),-GetY(),-GetZ());
00240         };
00241 
00242 private:
00243         float m_vfElements[3];
00244 };
00245 
00246 inline std::ostream & operator<< (std::ostream & os, const Vector & vecVector)
00247 {
00248         os << "(" << vecVector.GetX() << ";" << vecVector.GetY() << ";" << vecVector.GetZ() << ")";
00249         return os;
00250 }
00251 
00252 inline std::istream & operator>> (std::istream & is, Vector & vecVector)
00253 {
00254 
00255         float fX,fY,fZ;
00256         if (is >> eat("(") >> fX >> eat(";") >> fY >> eat(";") >> fZ >> eat(")"))
00257                 vecVector.Set(fX,fY,fZ);
00258 
00259         return is;
00260 }

Generated on Mon Dec 10 18:18:11 2007 for VisLU by  doxygen 1.5.4