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