00001 #ifndef Vector_h
00002 #define Vector_h
00003
00004 template <class T> class Vector3 {
00005 public:
00006 T x, y, z;
00007
00008
00009 Vector3(){}
00010
00011 Vector3( T i, T j, T k): x(i), y(j), z(k){
00012 }
00013
00014 Vector3 cross(const Vector3 &other){
00015 Vector3 res;
00016
00017 res.x=(y*other.z)-(z*other.y);
00018 res.y=(z*other.x)-(x*other.z);
00019 res.z=(x*other.y)-(y*other.x);
00020
00021 return res;
00022
00023 }
00024 float lengthSquared() const {
00025 return (x*x + y*y + z*z);
00026 }
00027
00028
00029 float length() const {
00030 return sqrtf(lengthSquared());
00031 }
00032
00033
00034 void normalize() {
00035 float len = length();
00036 float len_inv = 1.0f/len;
00037 x *= len_inv;
00038 y *= len_inv;
00039 z *= len_inv;
00040 }
00041
00042 Vector3 &operator () (T i, T j, T k){
00043 x=i; y=j; z=k;
00044 return *this;
00045 }
00046
00047 Vector3 operator + ( const Vector3 &other ){
00048 Vector3 res;
00049
00050 res.x = x + other.x;
00051 res.y = y + other.y;
00052 res.z = z + other.z;
00053
00054 return res;
00055 }
00056
00057 Vector3 operator - ( const Vector3 &other ){
00058 Vector3 res;
00059
00060 res.x = x - other.x;
00061 res.y = y - other.y;
00062 res.z = z - other.z;
00063
00064 return res;
00065 }
00066
00067 Vector3 operator * ( const Vector3 &other ) {
00068 Vector3 res;
00069
00070 res.x = x * other.x;
00071 res.y = y * other.y;
00072 res.z = z * other.z;
00073
00074 return res;
00075 }
00076
00077 Vector3 operator / ( const Vector3 &other ) {
00078 Vector3 res;
00079
00080 res.x = x / other.x;
00081 res.y = y / other.y;
00082 res.z = z / other.z;
00083
00084 return res;
00085 }
00086
00087 Vector3 operator * ( const T scalar ) {
00088 Vector3 res;
00089
00090 res.x = x * scalar;
00091 res.y = y * scalar;
00092 res.z = z * scalar;
00093
00094 return res;
00095 }
00096
00097 Vector3 operator / ( const T scalar ) {
00098 Vector3 res;
00099
00100 res.x = x / scalar;
00101 res.y = y / scalar;
00102 res.z = z / scalar;
00103
00104 return res;
00105 }
00106
00107 bool operator == ( const Vector3 &other ) {
00108 if(other.x!=x)
00109 return 0;
00110
00111 if(other.y!=y)
00112 return 0;
00113
00114 if(other.z!=z)
00115 return 0;
00116
00117 return 1;
00118 }
00119
00120 bool operator != ( const Vector3 &other ) {
00121 if(other.x!=x)
00122 return 1;
00123
00124 if(other.y!=y)
00125 return 1;
00126
00127 if(other.z!=z)
00128 return 1;
00129
00130 return 0;
00131 }
00132
00133 Vector3 operator - ( ) {
00134 Vector3 res;
00135
00136 res.x = -x;
00137 res.y = -y;
00138 res.z = -z;
00139
00140 return res;
00141 }
00142 };
00143
00144 typedef Vector3<float> FVector;
00145 typedef Vector3<int> IVector;
00146
00147 static FVector operator /(const FVector fv, const IVector iv){
00148 FVector res;
00149
00150 res.x = fv.x / iv.x;
00151 res.y = fv.y / iv.y;
00152 res.z = fv.z / iv.z;
00153
00154 return res;
00155 }
00156
00157
00158 #endif