00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __TVECTOR3_H__
00013 #define __TVECTOR3_H__
00014
00015
00016
00017
00018 #include <Definitions.h>
00019 #include <math.h>
00020
00021
00022
00023
00024
00025
00026
00027
00028 template<typename T>
00029 struct TVector3
00030 {
00031
00032
00033
00034 public:
00035
00036
00037 TVector3() {}
00038 TVector3(const T &v) : m_X(v), m_Y(v), m_Z(v) {}
00039 TVector3(const T &x, const T &y, const T &z) : m_X(x), m_Y(y), m_Z(z) {}
00040 TVector3(const TVector3<T> &other) : m_X(other.m_X), m_Y(other.m_Y), m_Z(other.m_Z) {}
00041
00042 const TVector3<T> &operator=(const TVector3<T> &other) {m_X = other.m_X; m_Y = other.m_Y; m_Z = other.m_Z; return *this;}
00043
00044 const TVector3<T> operator+(const TVector3<T> &other) const {return TVector3<T>(m_X + other.m_X, m_Y + other.m_Y, m_Z + other.m_Z);}
00045 const TVector3<T> &operator+=(const TVector3<T> &other) {m_X += other.m_X; m_Y += other.m_Y; m_Z += other.m_Z; return *this;}
00046
00047 const TVector3<T> operator-(const TVector3<T> &other) const {return TVector3<T>(m_X - other.m_X, m_Y - other.m_Y, m_Z - other.m_Z);}
00048 const TVector3<T> &operator-=(const TVector3<T> &other) {m_X -= other.m_X; m_Y -= other.m_Y; m_Z -= other.m_Z; return *this;}
00049
00050 const TVector3<T> operator*(const T &factor) const {return TVector3<T>(m_X * factor, m_Y * factor, m_Z * factor);}
00051 const TVector3<T> &operator*=(const T &factor) {m_X *= factor; m_Y *= factor; m_Z *= factor; return *this;}
00052
00053 const TVector3<T> operator/(const T ") const {return TVector3<T>(m_X / quot, m_Y / quot, m_Z / quot);}
00054 const TVector3<T> &operator/=(const T ") {m_X /= quot; m_Y /= quot; m_Z /= quot; return *this;}
00055
00056
00057 const TVector3<T> operator*(const TVector3<T> &other) const {return TVector3<T>(m_Y * other.m_Z - m_Z * other.m_Y, m_Z * other.m_X - m_X * other.m_Z, m_X * other.m_Y - m_Y * other.m_X);}
00058
00059 const TVector3<T> operator-() const {return TVector3<T>(-m_X, -m_Y, -m_Z);}
00060
00061 bool operator==(const TVector3<T> &other) {return m_X == other.m_X && m_Y == other.m_Y && m_Z == other.m_Z;}
00062 bool operator!=(const TVector3<T> &other) {return m_X != other.m_X || m_Y != other.m_Y || m_Z != other.m_Z;}
00063
00064 T &operator[](const byte &i) {assert(i < 3 && "Error: Invalid index for TVector3::operator[]!");
00065 return *(&m_X + i);}
00066
00067 const T &operator[](const byte &i) const {assert(i < 3 && "Error: Invalid index for TVector3::operator[]!");
00068 return *(&m_X + i);}
00069
00070 void Set(const T &x, const T &y, const T &z) {m_X = x; m_Y = y; m_Z = z;}
00071
00072 const float Length() const {return sqrt(m_X * m_X + m_Y * m_Y + m_Z * m_Z);}
00073 const float LengthSqr() const {return m_X * m_X + m_Y * m_Y + m_Z * m_Z;}
00074
00075 void Normalize() {assert(LengthSqr() && "Error: TVector3::Normalize called for a zero vector!");
00076 float fL = 1 / Length(); m_X *= fL; m_Y *= fL; m_Z *= fL;}
00077
00078 const T Dot(const TVector3<T> &other) const {return m_X * other.m_X + m_Y * other.m_Y + m_Z * other.m_Z;}
00079
00080 void LookAlongRot(TVector3<T> vDir);
00081 void ShortestRot(const TVector3<T> &vFrom, const TVector3<T> &vTo);
00082
00083 void Log(word wTargetLog) const {LOG(wTargetLog, "(%.2f, %.2f, %.2f)", m_X, m_Y, m_Z);}
00084
00085
00086
00087
00088 public:
00089 T m_X, m_Y, m_Z;
00090 };
00091
00092 typedef TVector3<float> CVector3f;
00093 typedef TVector3<sword> CVector3sw;
00094 typedef TVector3<sdword> CVector3sdw;
00095 typedef TVector3<double> CVector3d;
00096
00097 #include "CMatrix33.h"
00098
00099 template<typename T>
00100 void TVector3<T>::LookAlongRot(TVector3<T> vDir)
00101 {
00102 float fLen = vDir.Length();
00103 if(fLen > 0.0001)
00104 {
00105 vDir /= fLen;
00106 TVector3<T> vUp, vRight;
00107 if(vDir.m_Y > 0.9999f)
00108 {
00109 vUp.Set(0.0f, 0.0f, 1.0f);
00110 }
00111 else
00112 {
00113 vUp.Set(0.0f, 1.0f, 0.0f);
00114 }
00115 vRight = vDir * vUp;
00116 vRight.Normalize();
00117 vUp = vRight * vDir;
00118
00119 CMatrix33 mat;
00120 mat._11 = vRight.m_X;
00121 mat._21 = vRight.m_Y;
00122 mat._31 = vRight.m_Z;
00123 mat._12 = vUp.m_X;
00124 mat._22 = vUp.m_Y;
00125 mat._32 = vUp.m_Z;
00126
00127 mat._13 = -vDir.m_X;
00128 mat._23 = -vDir.m_Y;
00129 mat._33 = -vDir.m_Z;
00130
00131
00132 *this = mat.GetZXYRotationVector();
00133 }
00134 }
00135
00136 template<typename T>
00137 void TVector3<T>::ShortestRot(const TVector3<T> &vFrom, const TVector3<T> &vTo)
00138 {
00139 *this = vTo - vFrom;
00140
00141 while(m_X > MY_PI)
00142 {
00143 m_X -= 2.0f * MY_PI;
00144 }
00145 while(m_X < -MY_PI)
00146 {
00147 m_X += 2.0f * MY_PI;
00148 }
00149 while(m_Y > MY_PI)
00150 {
00151 m_Y -= 2.0f * MY_PI;
00152 }
00153 while(m_Y < -MY_PI)
00154 {
00155 m_Y += 2.0f * MY_PI;
00156 }
00157 while(m_Z > MY_PI)
00158 {
00159 m_Z -= 2.0f * MY_PI;
00160 }
00161 while(m_Z < -MY_PI)
00162 {
00163 m_Z += 2.0f * MY_PI;
00164 }
00165 }
00166
00167 #endif