00001
00002
00003
00004
00005
00006
00012 #ifndef VECTOR_H
00013 #define VECTOR_H
00014
00015 #include <string>
00016 #include <iostream>
00017 #include <math.h>
00018 #include <sys/time.h>
00019
00020 using namespace std;
00021
00026 class Vector {
00027 public:
00031 double x;
00032
00036 double y;
00037
00041 double z;
00042
00049 inline Vector(double x_,double y_,double z_) {
00050 x=x_;
00051 y=y_;
00052 z=z_;
00053 }
00054
00058 inline Vector() {}
00059
00063 inline void normalise() {
00064 double length_;
00065
00066 length_ = length();
00067 if (length_ != 0) {
00068 x /= length_;
00069 y /= length_;
00070 z /= length_;
00071 } else {
00072 x = 0;
00073 y = 0;
00074 z = 0;
00075 }
00076 }
00077
00081 inline Vector getNormalised() {
00082 double length_;
00083
00084 length_ = length();
00085 if (length_ != 0)
00086 return Vector(x / length_,y / length_,z / length_);
00087 else
00088 return Vector (0,0,0);
00089 }
00090
00096 inline Vector crossProduct(const Vector & b) {
00097 Vector tmp_;
00098 tmp_.x = (y * b.z) - (z * b.y);
00099 tmp_.y = (z * b.x) - (x * b.z);
00100 tmp_.z = (x * b.y) - (y * b.x);
00101 return tmp_;
00102 }
00103
00107 inline double length() {
00108 return sqrt((x * x) + (y * y) + (z * z));
00109 }
00110
00116 inline double scalarProduct(const Vector & b) {
00117 return (x * b.x) + (y * b.y) + (z * b.z);
00118 }
00119
00120
00121
00126 inline double operator*(const Vector & b) {
00127 return (x * b.x) + (y * b.y) + (z * b.z);
00128 }
00129
00134 inline Vector operator-(const Vector & a) {
00135 return Vector(x-a.x,y-a.y,z-a.z);
00136 }
00137
00142 inline Vector operator+(const Vector & a) {
00143 return Vector(x+a.x,y+a.y,z+a.z);
00144 }
00145
00150 inline Vector & operator+=(const Vector & a) {
00151 x+=a.x;
00152 y+=a.y;
00153 z+=a.z;
00154 return *this;
00155 }
00156
00161 inline Vector & operator-=(const Vector & a) {
00162 x-=a.x;
00163 y-=a.y;
00164 z-=a.z;
00165 return *this;
00166 }
00167
00172 inline bool operator==(const Vector & a) {
00173 if(x==a.x && y==a.y && z==a.z)
00174 return true;
00175 else
00176 return false;
00177 }
00178
00183 inline Vector operator*(const double a) {
00184 return Vector(x*a,y*a,z*a);
00185 }
00186
00191 inline Vector operator/(const double a) {
00192 return Vector(x/a,y/a,z/a);
00193 }
00194
00199 inline Vector operator+(const double a) {
00200 return Vector(x+a,y+a,z+a);
00201 }
00202
00207 inline Vector& operator*=(const double a) {
00208 x*=a;
00209 y*=a;
00210 z*=a;
00211 return *this;
00212 }
00213
00218 inline Vector& operator/=(const double a) {
00219 x/=a;
00220 y/=a;
00221 z/=a;
00222 return *this;
00223 }
00224 };
00225
00232 inline ostream & operator<<(ostream &out_, const Vector & v_) {
00233 out_ << " X: " << v_.x << " Y: " << v_.y << " Z: " << v_.z;
00234 return out_;
00235 }
00236
00243 inline istream & operator>>(istream &in_, Vector & v_) {
00244 string s_;
00245 in_ >> s_ ;
00246 in_ >> v_.x ;
00247 in_ >> s_ ;
00248 in_ >> v_.y ;
00249 in_ >> s_ ;
00250 in_ >> v_.z ;
00251 return in_;
00252 }
00253
00254 #endif