00001 #ifndef __VECTOR_HEADER__ 00002 #define __VECTOR_HEADER__ 00003 00004 #include <algorithm> 00005 #include <iostream> 00006 #include <float.h> 00007 #include <art/epsilon.h> 00008 00009 //==========[ Forward References ]========================= 00010 00011 template <class T> class Vec; 00012 template <class T> class Vec3; 00013 template <class T> class Vec4; 00014 template <class T> class Mat3; 00015 template <class T> class Mat4; 00016 00017 //==========[ Exception Classes ]========================== 00018 00019 class VectorSizeMismatch {}; 00020 00021 //==========[ class Vec2 ]================================= 00022 00023 template <class T> 00024 class Vec2 { 00025 00026 //---[ Private Variable Declarations ]------- 00027 00028 // x, y 00029 T n[2]; 00030 00031 public: 00032 00033 //---[ Constructors ]------------------------ 00034 00035 Vec2() { n[0] = FLT_MAX; n[1] = FLT_MAX; } 00036 Vec2( const T x, const T y ) 00037 { n[0] = x; n[1] = y; } 00038 Vec2( const Vec2<T>& v ) 00039 { n[0] = v.n[0]; n[1] = v.n[1]; } 00040 00041 //---[ Equal Operators ]--------------------- 00042 00043 Vec2<T>& operator=( const Vec2<T>& v ) 00044 { n[0] = v.n[0]; n[1] = v.n[1]; return *this; } 00045 Vec2<T>& operator +=( const Vec2<T>& v ) 00046 { n[0] += v.n[0]; n[1] += v.n[1]; return *this; } 00047 Vec2<T>& operator -= ( const Vec2<T>& v ) 00048 { n[0] -= v.n[0]; n[1] -= v.n[1]; return *this; } 00049 float operator * ( const Vec2<T>& v ) 00050 { return (n[0] * v.n[0] + n[1] * v.n[1]);} 00051 Vec2<T>& operator *= ( const T d ) 00052 { n[0] *= d; n[1] *= d; return *this; } 00053 Vec2<T>& operator /= ( const T d ) 00054 { n[0] /= d; n[1] /= d; return *this; } 00055 00056 //---[ Access Operators ]-------------------- 00057 00058 T& operator []( int i ) 00059 { return n[i]; } 00060 T operator []( int i ) const 00061 { return n[i]; } 00062 00063 //---[ Arithmetic Operators ]---------------- 00064 00065 Vec2<T> operator-( const Vec2<T>& a ) { 00066 return Vec2<T>(n[0]-a.n[0],n[1]-a.n[1]); } 00067 Vec2<T> operator+( const Vec2<T>& a ) { 00068 return Vec2<T>(a.n[0]+n[0],a.n[1]+n[1]); } 00069 Vec2<T> operator*( const T d) { 00070 return Vec2<T>(d*n[0],d*n[1] );} 00071 //---[ Conversion Operators ]---------------- 00072 00073 const T* getPointer() const { return n; } 00074 00075 //---[ Length Methods ]---------------------- 00076 00077 float length2() const 00078 { return n[0]*n[0] + n[1]*n[1]; } 00079 float length() const 00080 { return sqrtf( length2() ); } 00081 00082 //---[ Normalization ]----------------------- 00083 00084 void normalize() { 00085 float len = length(); 00086 if (!Epsilon::equal(len,0)) { 00087 n[0] /= len; 00088 n[1] /= len; 00089 } 00090 } 00091 //---[Dot Product]-------------------------- 00092 00093 float dot(const Vec2<T>& a){ 00094 return n[0]*a.n[0]+n[1]*a.n[1]; 00095 } 00096 00097 //---[ Zero Test ]--------------------------- 00098 00099 bool iszero() { return ( (n[0]==0 && n[1]==0) ? true : false); }; 00100 void zeroElements() { memset(n,0,sizeof(T)*2); } 00101 00102 //---[ Friend Methods ]---------------------- 00103 /* not implemented 00104 template <class U> friend T operator *( const Vec3<T>& a, const Vec4<T>& b ); 00105 template <class U> friend T operator *( const Vec4<T>& b, const Vec3<T>& a ); 00106 template <class U> friend Vec3<T> operator -( const Vec3<T>& v ); 00107 template <class U> friend Vec3<T> operator *( const Vec3<T>& a, const double d ); 00108 template <class U> friend Vec3<T> operator *( const double d, const Vec3<T>& a ); 00109 template <class U> friend Vec3<T> operator *( const Vec3<T>& v, Mat4<T>& a ); 00110 template <class U> friend T operator *( const Vec3<T>& a, const Vec3<T>& b ); 00111 template <class U> friend Vec3<T> operator *( const Mat3<T>& a, const Vec3<T>& v ); 00112 template <class U> friend Vec3<T> operator *( const Vec3<T>& v, const Mat3<T>& a ); 00113 template <class U> friend Vec3<T> operator *( const Mat4<T>& a, const Vec3<T>& v ); 00114 template <class U> friend Vec3<T> operator /( const Vec3<T>& a, const double d ); 00115 template <class U> friend Vec3<T> operator ^( const Vec3<T>& a, const Vec3<T>& b ); 00116 template <class U> friend bool operator ==( const Vec3<T>& a, const Vec3<T>& b ); 00117 template <class U> friend bool operator !=( const Vec3<T>& a, const Vec3<T>& b ); 00118 template <class U> friend ostream& operator <<( ostream& os, const Vec3<T>& v ); 00119 template <class U> friend istream& operator >>( istream& is, Vec3<T>& v ); 00120 template <class U> friend Vec3<T> minimum( const Vec3<T>& a, const Vec3<T>& b ); 00121 template <class U> friend Vec3<T> maximum( const Vec3<T>& a, const Vec3<T>& b ); 00122 template <class U> friend Vec3<T> prod( const Vec3<T>& a, const Vec3<T>& b ); 00123 */ 00124 }; 00125 00126 typedef Vec2<int> Vec2i; 00127 typedef Vec2<float> Vec2f; 00128 00129 #endif