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