00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef TEDDY__MATHS__VECTOR_4_H
00032 #define TEDDY__MATHS__VECTOR_4_H
00033
00034
00035 #include "Teddy/Maths/Vector.h"
00036 #include "Teddy/SysSupport/StdMaths.h"
00037
00038
00039 namespace Teddy {
00040 namespace Maths {
00041
00042
00051 #ifndef SWIG
00052 #define Vector4 TVector4<float>
00053 #define IntVector4 TVector4<int>
00054 #define FloatVector4 TVector4<float>
00055 #define DoubleVector4 TVector4<double>
00056 #define ByteVector4 TVector4<unsigned char>
00057 #define VECTOR4_EPSILON 0.00001
00058 #endif
00059
00060
00068 template <typename T> class TVector4 {
00069 public:
00070 T v[4];
00071
00072 public:
00073
00074 TVector4(){}
00075
00076 TVector4( const T a, const T b, const T c, const T d ){
00077 v[0] = a;
00078 v[1] = b;
00079 v[2] = c;
00080 v[3] = d;
00081 }
00082
00083 TVector4( const T t ){ *this = t; }
00084 TVector4( const T *pt ){ *this = pt; }
00085 TVector4( const TVector<T> &vec ){ *this = vec; }
00086 TVector4( const TVector<T> &vec, const T t ){ v[0] = vec.v[0]; v[1] = vec.v[1]; v[2] = vec.v[2]; v[3] = t; }
00087 TVector4( const TVector4<T> &vec ){ *this = vec; }
00088
00089
00090 operator T* () { return v; }
00091 T &operator [] ( const int n ) { return v[n]; }
00092 operator const T* () const { return v; }
00093 T operator [] ( const int n ) const { return v[n]; }
00094 TVector4<T> operator - () const { return TVector4<T>(-v[0], -v[1], -v[2], -v[3]); }
00095
00096
00097 void operator =( const T t ) { v[0] = v[1] = v[2] = v[3] = t; }
00098 void operator =( const T *pt ) { v[0] = pt[0]; v[1] = pt[1]; v[2] = pt[2]; v[3] = pt[3]; }
00099 void operator =( const TVector <T> &vec ) { v[0] = vec.v[0]; v[1] = vec.v[1]; v[2] = vec.v[2]; v[3] = 0; }
00100 void operator =( const TVector4<T> &vec ) { v[0] = vec.v[0]; v[1] = vec.v[1]; v[2] = vec.v[2]; v[3] = vec.v[3]; }
00101
00102 bool operator==( const TVector4<T> &vec ) const {
00103 return (
00104 Abs(v[0] - vec.v[0]) <= (T)VECTOR4_EPSILON &&
00105 Abs(v[1] - vec.v[1]) <= (T)VECTOR4_EPSILON &&
00106 Abs(v[2] - vec.v[2]) <= (T)VECTOR4_EPSILON &&
00107 Abs(v[3] - vec.v[3]) <= (T)VECTOR4_EPSILON
00108 );
00109 }
00110
00111 bool operator!=( const TVector4<T> &vec ) const {
00112 return !(*this == v);
00113 }
00114
00115
00116 TVector4<T> operator+ ( const T t ) const { return TVector4<T>(v[0]+t, v[1]+t, v[2]+t, v[3]+t); }
00117 TVector4<T> operator- ( const T t ) const { return TVector4<T>(v[0]-t, v[1]-t, v[2]-t, v[3]-t); }
00118 TVector4<T> operator* ( const T t ) const { return TVector4<T>(v[0]*t, v[1]*t, v[2]*t, v[3]*t); }
00119 TVector4<T> operator/ ( const T t ) const { return TVector4<T>(v[0]/t, v[1]/t, v[2]/t, v[3]/t); }
00120 const TVector4<T> &operator+=( const T t ) { v[0] += t; v[1] += t; v[2] += t; v[3] += t; return *this; }
00121 const TVector4<T> &operator-=( const T t ) { v[0] -= t; v[1] -= t; v[2] -= t; v[3] -= t; return *this; }
00122 const TVector4<T> &operator*=( const T t ) { v[0] *= t; v[1] *= t; v[2] *= t; v[3] *= t; return *this; }
00123 const TVector4<T> &operator/=( const T t ) { v[0] /= t; v[1] /= t; v[2] /= t; v[3] /= t; return *this; }
00124
00125
00126 TVector4<T> operator+ ( const TVector4<T> &vec ) const { return TVector4<T>(v[0]+vec.v[0], v[1]+vec.v[1], v[2]+vec.v[2], v[3]+vec.v[3]); }
00127 TVector4<T> operator- ( const TVector4<T> &vec ) const { return TVector4<T>(v[0]-vec.v[0], v[1]-vec.v[1], v[2]-vec.v[2], v[3]-vec.v[3]); }
00128 TVector4<T> operator* ( const TVector4<T> &vec ) const { return TVector4<T>(v[0]*vec.v[0], v[1]*vec.v[1], v[2]*vec.v[2], v[3]*vec.v[3]); }
00129 TVector4<T> operator/ ( const TVector4<T> &vec ) const { return TVector4<T>(v[0]/vec.v[0], v[1]/vec.v[1], v[2]/vec.v[2], v[3]/vec.v[3]); }
00130 const TVector4<T> &operator+=( const TVector4<T> &vec ) { v[0] += vec.v[0]; v[1] += vec.v[1]; v[2] += vec.v[2]; v[3] += vec.v[3]; return *this; }
00131 const TVector4<T> &operator-=( const TVector4<T> &vec ) { v[0] -= vec.v[0]; v[1] -= vec.v[1]; v[2] -= vec.v[2]; v[3] -= vec.v[3]; return *this; }
00132 const TVector4<T> &operator*=( const TVector4<T> &vec ) { v[0] *= vec.v[0]; v[1] *= vec.v[1]; v[2] *= vec.v[2]; v[3] *= vec.v[3]; return *this; }
00133 const TVector4<T> &operator/=( const TVector4<T> &vec ) { v[0] /= vec.v[0]; v[1] /= vec.v[1]; v[2] /= vec.v[2]; v[3] /= vec.v[3]; return *this; }
00134
00135
00136 T operator| ( const TVector4<T> &vec ) const { return v[0]*vec[0] + v[1]*vec[1] + v[2]*vec[2] + v[3]*vec[3]; }
00137
00138
00139 T magnitudeSquared() const { return v[0]*v[0] + v[1]*v[1] + v[2]*v[2] + v[3]*v[3]; }
00140 T magnitude () const { return (T)sqrt( magnitudeSquared() ); }
00141 void normalize () { *this /= Magnitude(); }
00142 };
00143
00144
00145 #ifdef SWIG
00146 %template(Vector4) TVector4<float>;
00147 %template(IntVector4) TVector4<int>;
00148 %template(FloatVector4) TVector4<float>;
00149 %template(DoubleVector4) TVector4<double>;
00150 #endif
00151
00152
00153 };
00154 };
00155
00156
00157 #endif // TEDDY__MATHS__VECTOR_4_H
00158