00001
00006 #ifndef VECTOR3_CM
00007 #define VECTOR3_CM
00008
00009 #define NOMINMAX
00010 #include <iostream>
00011 #define _USE_MATH_DEFINES
00012 #include <math.h>
00013
00020 const int vDim = 3;
00021
00025 class Vector3
00026 {
00027
00028 private:
00029 double vec[vDim];
00030
00031 void copy (const Vector3 & o){
00032 for (int i = 0; i < vDim; i++)
00033 vec[i] = o.vec[i];
00034 }
00035
00036 const Vector3& operator*=(const Vector3& rhs){
00037 for (int i = 0; i < vDim; i++)
00038 vec[i] = vec[i]*rhs.vec[i];
00039 return *this;
00040 }
00041
00042 public:
00043
00044
00046 static Vector3 e1() {return Vector3(1, 0, 0);}
00048 static Vector3 e2() {return Vector3(0, 1, 0);}
00050 static Vector3 e3() {return Vector3(0, 0, 1);}
00052 static Vector3 ones() {return Vector3(1, 1, 1);}
00053
00055 Vector3(void){
00056 for (int i = 0; i < vDim; i++)
00057 vec[i] = 0;
00058 }
00059
00064 Vector3(double x, double y){
00065 vec[0] = x; vec[1] = y; vec[2] = 0;
00066 }
00067
00068 Vector3(double x, double y, double z){
00069 vec[0] = x; vec[1] = y; vec[2] = z;
00070 }
00071
00072 Vector3(const Vector3 & o){
00073 copy(o);
00074 }
00075
00079 bool operator==(const Vector3& rhs) const{
00080 return (fabs(vec[0] - rhs.vec[0]) < 1.e-14 &&
00081 fabs(vec[1] - rhs.vec[1]) < 1.e-14 &&
00082 fabs(vec[2] - rhs.vec[2]) < 1.e-14);
00083 }
00084
00085 bool operator!=(const Vector3& rhs) const{
00086 return (!(*this == rhs));
00087 }
00088
00089 const Vector3& operator=(const Vector3& rhs){
00090 if (this != &rhs)
00091 copy(rhs);
00092 return *this;
00093 }
00094
00095 const Vector3& operator+=(const Vector3& rhs){
00096 for (int i = 0; i < vDim; i++)
00097 vec[i] = vec[i]+rhs.vec[i];
00098 return *this;
00099 }
00100
00101 const Vector3& operator-=(const Vector3& rhs){
00102 for (int i = 0; i < vDim; i++)
00103 vec[i] = vec[i]-rhs.vec[i];
00104 return *this;
00105 }
00106
00107
00108 const Vector3& operator*=(const double& rhs){
00109 for (int i = 0; i < vDim; i++)
00110 vec[i] = vec[i]*rhs;
00111 return *this;
00112 }
00113
00114 const Vector3& operator/=(const double& rhs){
00115 for (int i = 0; i < vDim; i++)
00116 vec[i] = vec[i]/rhs;
00117 return *this;
00118 }
00119
00120
00121 Vector3 operator+(const Vector3& rhs) const {
00122 Vector3 tmp(*this);
00123 tmp += rhs;
00124 return tmp;
00125 }
00126
00127
00128 Vector3 operator-(const Vector3& rhs) const {
00129 Vector3 tmp(*this);
00130 tmp -= rhs;
00131 return tmp;
00132 }
00133
00137 double dot(const Vector3& rhs) const {
00138 Vector3 tmp(*this);
00139 tmp *= rhs;
00140 double sum = 0;
00141 for (int i = 0; i < vDim; i++)
00142 sum += tmp.vec[i];
00143 return sum;
00144 }
00145
00146 Vector3 operator*(const double& rhs) const {
00147 Vector3 tmp(*this);
00148 tmp *= rhs;
00149 return tmp;
00150 }
00151
00152 Vector3 operator/(const double& rhs) const {
00153 Vector3 tmp(*this);
00154 tmp /= rhs;
00155 return tmp;
00156 }
00157
00158
00162 double & operator[] (int i){
00163 return vec[i];
00164 }
00165
00169 Vector3 crossProduct(const Vector3& rhs) const{
00170 Vector3 tmp;
00171 tmp[0] = vec[1]*rhs[2] - vec[2]*rhs[1];
00172 tmp[1] = vec[2]*rhs[0] - vec[0]*rhs[2];
00173 tmp[2] = vec[0]*rhs[1] - vec[1]*rhs[0];
00174 return tmp;
00175 }
00176
00180 double operator[] (int i) const{
00181 if (i >= vDim || i < 0){
00182 std::cerr << "Out of bounds vector accessing: [" << i << "]" << std::endl;
00183 return 0;
00184 }
00185 else
00186 return vec[i];
00187 }
00188
00189 double x() const {return vec[0];}
00190 double & x() {return vec[0];}
00191 double y() const {return vec[1];}
00192 double & y() {return vec[1];}
00193 double z() const {return vec[2];}
00194 double & z() {return vec[2];}
00195
00199 double abs () const {
00200 double sum = 0;
00201 for (int i = 0; i < vDim; i++)
00202 sum += vec[i]*vec[i];
00203 return sqrt(sum);
00204 }
00205
00210 Vector3 norm() const {
00211 Vector3 norm;
00212 double length = abs();
00213 for (int i = 0; i < vDim; i++)
00214 norm[i] = vec[i]/length;
00215 return norm;
00216 }
00217
00221 void normalize(){
00222 double length = abs();
00223 for (int i = 0; i < vDim; i++)
00224 vec[i] = vec[i]/length;
00225 }
00226
00230 void negate () {
00231 vec[0] = -vec[0];
00232 vec[1] = -vec[1];
00233 vec[2] = -vec[2];
00234 }
00235
00236
00237 void print(std::ostream& out, char * name) const{
00238 for (int i = 0; i < vDim; i++)
00239 out << name << "(" << (i+1) << ")= " << vec[i] << "; ";
00240 out << std::endl;
00241 }
00242
00243 friend std::ostream& operator<<(std::ostream& out,const Vector3 & m) {
00244 out << m.vec[0];
00245 for (int i = 1; i < vDim; i++)
00246 out << " " << m.vec[i];
00247 return out;
00248 }
00249
00250
00251 friend std::istream& operator>>(std::istream& in, Vector3 & m) {
00252 for (int i = 0; i < vDim; i++)
00253 in >> m.vec[i];
00254 return in;
00255 }
00256 };
00257
00258 #endif
00259