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