00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef KEOS_VECTOR4_H
00023 #define KEOS_VECTOR4_H
00024
00025 #include "KeosPrerequisites.h"
00026
00027 namespace Keos
00028 {
00029
00033 template <class T>
00034 class CVector4
00035 {
00036 public :
00037
00040 CVector4(T X = 0, T Y = 0, T Z = 0, T W = 1);
00041
00044 void Set(T X, T Y, T Z, T W);
00045
00048 T Length() const;
00049
00052 T LengthSq() const;
00053
00056 void Normalize();
00057
00058
00059 CVector4<T> operator +() const;
00060 CVector4<T> operator -() const;
00061
00062 CVector4<T> operator +(const CVector4<T>& v) const;
00063 CVector4<T> operator -(const CVector4<T>& v) const;
00064
00065 const CVector4<T>& operator +=(const CVector4<T>& v);
00066 const CVector4<T>& operator -=(const CVector4<T>& v);
00067
00068 CVector4<T> operator *(T t) const;
00069 CVector4<T> operator /(T t) const;
00070
00071 const CVector4<T>& operator *=(T t);
00072 const CVector4<T>& operator /=(T t);
00073
00074 bool operator ==(const CVector4<T>& v) const;
00075 bool operator !=(const CVector4<T>& v) const;
00076
00080 operator T*();
00081
00082
00083 T x, y, z, w;
00084 };
00085
00086
00087 template <class T> CVector4<T> operator * (const CVector4<T>& v, T t);
00088 template <class T> CVector4<T> operator / (const CVector4<T>& v, T t);
00089 template <class T> CVector4<T> operator * (T t, const CVector4<T>& v);
00090 template <class T> T VectorDot (const CVector4<T>& v1, const CVector4<T>& v2);
00091 template <class T> CVector4<T> VectorCross(const CVector4<T>& v1, const CVector4<T>& v2);
00092 template <class T> std::istream& operator >>(std::istream& Stream, CVector4<T>& Vector);
00093 template <class T> std::ostream& operator <<(std::ostream& Stream, const CVector4<T>& Vector);
00094
00095
00096 typedef CVector4<float> TVector4F;
00097
00098
00099
00100
00101
00102
00103
00104 template <class T>
00105 inline CVector4<T>::CVector4(T X, T Y, T Z, T W) :
00106 x(X),
00107 y(Y),
00108 z(Z),
00109 w(W)
00110 {}
00111
00112
00113 template <class T>
00114 inline void CVector4<T>::Set(T X, T Y, T Z, T W)
00115 {
00116 x = X;
00117 y = Y;
00118 z = Z;
00119 w = W;
00120 }
00121
00122
00123 template <class T>
00124 inline T CVector4<T>::Length() const
00125 {
00126 return std::sqrt(LengthSq());
00127 }
00128
00129
00130 template <class T>
00131 inline T CVector4<T>::LengthSq() const
00132 {
00133 return x * x + y * y + z * z + w * w;
00134 }
00135
00136
00137 template <class T>
00138 inline void CVector4<T>::Normalize()
00139 {
00140 T Norm = Length();
00141
00142 if (std::abs(Norm) > std::numeric_limits<T>::epsilon())
00143 {
00144 x /= Norm;
00145 y /= Norm;
00146 z /= Norm;
00147 w /= Norm;
00148 }
00149 }
00150
00151
00152 template <class T>
00153 inline CVector4<T> CVector4<T>::operator +() const
00154 {
00155 return this;
00156 }
00157
00158
00159 template <class T>
00160 inline CVector4<T> CVector4<T>::operator -() const
00161 {
00162 return CVector4<T>(-x, -y, -z, -w);
00163 }
00164
00165
00166 template <class T>
00167 inline CVector4<T> CVector4<T>::operator +(const CVector4<T>& v) const
00168 {
00169 return CVector4<T>(x + v.x, y + v.y, z + v.z, w + v.w);
00170 }
00171
00172
00173 template <class T>
00174 inline CVector4<T> CVector4<T>::operator -(const CVector4<T>& v) const
00175 {
00176 return CVector4<T>(x - v.x, y - v.y, z - v.z, w - v.w);
00177 }
00178
00179
00180 template <class T>
00181 inline const CVector4<T>& CVector4<T>::operator +=(const CVector4<T>& v)
00182 {
00183 x += v.x;
00184 y += v.y;
00185 z += v.z;
00186 w += v.w;
00187
00188 return *this;
00189 }
00190
00191
00192 template <class T>
00193 inline const CVector4<T>& CVector4<T>::operator -=(const CVector4<T>& v)
00194 {
00195 x -= v.x;
00196 y -= v.y;
00197 z -= v.z;
00198 w -= v.w;
00199
00200 return *this;
00201 }
00202
00203
00204 template <class T>
00205 inline CVector4<T> CVector4<T>::operator *(T t) const
00206 {
00207 return CVector4<T>(x * t, y * t, z * t, w * t);
00208 }
00209
00210
00211 template <class T>
00212 inline CVector4<T> CVector4<T>::operator /(T t) const
00213 {
00214 return CVector4<T>(x / t, y / t, z / t, w / t);
00215 }
00216
00217
00218 template <class T>
00219 inline const CVector4<T>& CVector4<T>::operator *=(T t)
00220 {
00221 x *= t;
00222 y *= t;
00223 z *= t;
00224 w *= t;
00225
00226 return *this;
00227 }
00228
00229
00230 template <class T>
00231 inline const CVector4<T>& CVector4<T>::operator /=(T t)
00232 {
00233 x /= t;
00234 y /= t;
00235 z /= t;
00236 w /= t;
00237
00238 return *this;
00239 }
00240
00241
00242 template <class T>
00243 inline bool CVector4<T>::operator ==(const CVector4<T>& v) const
00244 {
00245 return ((std::abs(x - v.x) <= std::numeric_limits<T>::epsilon()) &&
00246 (std::abs(y - v.y) <= std::numeric_limits<T>::epsilon()) &&
00247 (std::abs(z - v.z) <= std::numeric_limits<T>::epsilon()) &&
00248 (std::abs(w - v.w) <= std::numeric_limits<T>::epsilon()));
00249 }
00250
00251
00252 template <class T>
00253 inline bool CVector4<T>::operator !=(const CVector4<T>& v) const
00254 {
00255 return !(*this == v);
00256 }
00257
00258
00259 template <class T>
00260 inline CVector4<T>::operator T*()
00261 {
00262 return &x;
00263 }
00264
00265
00266 template <class T>
00267 inline CVector4<T> operator *(const CVector4<T>& v, T t)
00268 {
00269 return CVector4<T>(v.x * t, v.y * t, v.z * t, v.w * t);
00270 }
00271
00272
00273 template <class T>
00274 inline CVector4<T> operator /(const CVector4<T>& v, T t)
00275 {
00276 return CVector4<T>(v.x / t, v.y / t, v.z / t, v.w / t);
00277 }
00278
00279
00280 template <class T>
00281 inline CVector4<T> operator *(T t, const CVector4<T>& v)
00282 {
00283 return v * t;
00284 }
00285
00286
00287 template <class T>
00288 inline T VectorDot(const CVector4<T>& v1, const CVector4<T>& v2)
00289 {
00290 return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
00291 }
00292
00293
00294 template <class T>
00295 inline CVector4<T> VectorCross(const CVector4<T>& v1, const CVector4<T>& v2)
00296 {
00297
00298 return CVector4<T>(v1.y * v2.z - v1.z * v2.y, v1.z * v2.w - v1.w * v2.z, v1.w * v2.x - v1.x * v2.w, v1.x * v2.y - v1.y * v2.x);
00299 }
00300
00301
00302 template <class T>
00303 inline std::istream& operator >>(std::istream& Stream, CVector4<T>& Vector)
00304 {
00305 return Stream >> Vector.x >> Vector.y >> Vector.z >> Vector.w;
00306 }
00307
00308
00309 template <class T>
00310 inline std::ostream& operator <<(std::ostream& Stream, const CVector4<T>& Vector)
00311 {
00312 return Stream << Vector.x << " " << Vector.y << " " << Vector.z << " " << Vector.w;
00313 }
00314
00315
00316 }
00317
00318
00319 #endif // KEOS_VECTOR4_H