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