| 1 |
tim |
1427 |
#ifndef _VECTOR3D_H_ |
| 2 |
|
|
#define _VECTOR3D_H_ |
| 3 |
|
|
|
| 4 |
|
|
#include <cmath> |
| 5 |
|
|
#include <iostream> |
| 6 |
|
|
|
| 7 |
|
|
using namespace std; |
| 8 |
|
|
|
| 9 |
|
|
class Vector3d{ |
| 10 |
|
|
|
| 11 |
|
|
public: |
| 12 |
|
|
|
| 13 |
|
|
Vector3d(){ |
| 14 |
|
|
this->x = double(); |
| 15 |
|
|
this->y = double(); |
| 16 |
|
|
this->z = double(); |
| 17 |
|
|
|
| 18 |
|
|
} |
| 19 |
|
|
|
| 20 |
|
|
Vector3d( double x, double y, double z){ |
| 21 |
|
|
this->x = x; |
| 22 |
|
|
this->y = y; |
| 23 |
|
|
this->z = z; |
| 24 |
|
|
} |
| 25 |
|
|
|
| 26 |
|
|
Vector3d(double* r){ |
| 27 |
|
|
this->x = r[0]; |
| 28 |
|
|
this->y = r[1]; |
| 29 |
|
|
this->z = r[2]; |
| 30 |
|
|
} |
| 31 |
|
|
|
| 32 |
|
|
Vector3d(const Vector3d& v1){ |
| 33 |
|
|
this->x = v1.x; |
| 34 |
|
|
this->y = v1.y; |
| 35 |
|
|
this->z = v1.z; |
| 36 |
|
|
} |
| 37 |
|
|
|
| 38 |
|
|
double& operator[](unsigned int index){ |
| 39 |
|
|
switch (index){ |
| 40 |
|
|
case 0 : |
| 41 |
|
|
return x; |
| 42 |
|
|
|
| 43 |
|
|
case 1 : |
| 44 |
|
|
return y; |
| 45 |
|
|
|
| 46 |
|
|
case 2 : |
| 47 |
|
|
return z; |
| 48 |
|
|
|
| 49 |
|
|
default: |
| 50 |
|
|
cerr << index <<" is invalid index" << endl; |
| 51 |
|
|
exit(1); |
| 52 |
|
|
} |
| 53 |
|
|
} |
| 54 |
|
|
|
| 55 |
|
|
const double& operator[](unsigned int index) const{ |
| 56 |
|
|
switch (index){ |
| 57 |
|
|
case 0 : |
| 58 |
|
|
return x; |
| 59 |
|
|
|
| 60 |
|
|
case 1 : |
| 61 |
|
|
return y; |
| 62 |
|
|
|
| 63 |
|
|
case 2 : |
| 64 |
|
|
return z; |
| 65 |
|
|
|
| 66 |
|
|
default: |
| 67 |
|
|
cerr << index <<" is invalid index" << endl; |
| 68 |
|
|
exit(1); |
| 69 |
|
|
} |
| 70 |
|
|
} |
| 71 |
|
|
|
| 72 |
|
|
Vector3d& operator=( const Vector3d& v1 ){ |
| 73 |
|
|
if(this == & v1) |
| 74 |
|
|
return *this; |
| 75 |
|
|
|
| 76 |
|
|
this->x = v1.x; |
| 77 |
|
|
this->y = v1.y; |
| 78 |
|
|
this->z = v1.z; |
| 79 |
|
|
|
| 80 |
|
|
return *this; |
| 81 |
|
|
} |
| 82 |
|
|
|
| 83 |
|
|
bool operator ==( const Vector3d& v1 ){ |
| 84 |
|
|
return this->x == v1.x && this->y == v1.y && this->z == v1.z; |
| 85 |
|
|
} |
| 86 |
|
|
|
| 87 |
|
|
bool operator !=( const Vector3d& v1 ){ |
| 88 |
|
|
return this->x != v1.x || this->y != v1.y || this->z != v1.z; |
| 89 |
|
|
} |
| 90 |
|
|
|
| 91 |
|
|
void neg(){ |
| 92 |
|
|
this->x = -this->x; |
| 93 |
|
|
this->y = -this->y; |
| 94 |
|
|
this->z = -this->z; |
| 95 |
|
|
} |
| 96 |
|
|
|
| 97 |
|
|
void add( const Vector3d& v1 ){ |
| 98 |
|
|
this->x += v1.x; |
| 99 |
|
|
this->y += v1.y; |
| 100 |
|
|
this->z += v1.z; |
| 101 |
|
|
} |
| 102 |
|
|
|
| 103 |
|
|
void add( const Vector3d& v1, const Vector3d &v2 ){ |
| 104 |
|
|
this->x = v1.x + v1.x; |
| 105 |
|
|
this->y = v1.y + v2.y; |
| 106 |
|
|
this->z = v1.z + v2.z; |
| 107 |
|
|
|
| 108 |
|
|
} |
| 109 |
|
|
|
| 110 |
|
|
void sub( const Vector3d& v1 ){ |
| 111 |
|
|
this->x -= v1.x; |
| 112 |
|
|
this->y -= v1.y; |
| 113 |
|
|
this->z -= v1.z; |
| 114 |
|
|
} |
| 115 |
|
|
|
| 116 |
|
|
void sub( const Vector3d& v1, const Vector3d &v2 ){ |
| 117 |
|
|
this->x = v1.x - v1.x; |
| 118 |
|
|
this->y = v1.y - v2.y; |
| 119 |
|
|
this->z = v1.z - v2.z; |
| 120 |
|
|
} |
| 121 |
|
|
|
| 122 |
|
|
void mul( double r ){ |
| 123 |
|
|
this->x *= r; |
| 124 |
|
|
this->y *= r; |
| 125 |
|
|
this->z *= r; |
| 126 |
|
|
} |
| 127 |
|
|
|
| 128 |
|
|
void mul( double r, const Vector3d& v1 ){ |
| 129 |
|
|
this->x = r * v1.x; |
| 130 |
|
|
this->y = r * v1.y; |
| 131 |
|
|
this->z = r * v1.z; |
| 132 |
|
|
|
| 133 |
|
|
} |
| 134 |
|
|
|
| 135 |
|
|
void mul( const Vector3d& v1, double r ){ |
| 136 |
|
|
this->x = v1.x * r; |
| 137 |
|
|
this->y = v1.y * r; |
| 138 |
|
|
this->z = v1.z * r; |
| 139 |
|
|
} |
| 140 |
|
|
|
| 141 |
|
|
void div( double r){ |
| 142 |
|
|
this->x /= r; |
| 143 |
|
|
this->y /= r; |
| 144 |
|
|
this->z /= r; |
| 145 |
|
|
} |
| 146 |
|
|
|
| 147 |
|
|
void div( const Vector3d& v1, double r ){ |
| 148 |
|
|
this->x = v1.x/r; |
| 149 |
|
|
this->y = v1.y/r; |
| 150 |
|
|
this->z = v1.z/r; |
| 151 |
|
|
} |
| 152 |
|
|
|
| 153 |
|
|
void operator +=( const Vector3d& v1 ){ |
| 154 |
|
|
this->x += v1.x; |
| 155 |
|
|
this->y += v1.y; |
| 156 |
|
|
this->z += v1.z; |
| 157 |
|
|
} |
| 158 |
|
|
|
| 159 |
|
|
void operator -=( const Vector3d& v1 ){ |
| 160 |
|
|
this->x -= v1.x; |
| 161 |
|
|
this->y -= v1.y; |
| 162 |
|
|
this->z -= v1.z; |
| 163 |
|
|
} |
| 164 |
|
|
|
| 165 |
|
|
void operator *=( double r ){ |
| 166 |
|
|
this->x *= r; |
| 167 |
|
|
this->y *= r; |
| 168 |
|
|
this->z *= r; |
| 169 |
|
|
} |
| 170 |
|
|
|
| 171 |
|
|
void operator /=( double r ){ |
| 172 |
|
|
this->x /= r; |
| 173 |
|
|
this->y /= r; |
| 174 |
|
|
this->z /= r; |
| 175 |
|
|
} |
| 176 |
|
|
|
| 177 |
|
|
//vector addition |
| 178 |
|
|
friend Vector3d operator+ ( const Vector3d& v1, const Vector3d& v2){ |
| 179 |
|
|
return Vector3d(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); |
| 180 |
|
|
} |
| 181 |
|
|
|
| 182 |
|
|
//vector subtraction |
| 183 |
|
|
friend Vector3d operator- ( const Vector3d& v1, const Vector3d& v2) { |
| 184 |
|
|
return Vector3d(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); |
| 185 |
|
|
} |
| 186 |
|
|
//unary minus |
| 187 |
|
|
friend Vector3d operator- ( const Vector3d& v) { |
| 188 |
|
|
return Vector3d(-v.x, -v.y, -v.z); |
| 189 |
|
|
} |
| 190 |
|
|
//multiply by a scalar |
| 191 |
|
|
friend Vector3d operator* ( const double& r, const Vector3d& v) { |
| 192 |
|
|
return Vector3d( r*v.x, r*v.y, r*v.z); |
| 193 |
|
|
} |
| 194 |
|
|
|
| 195 |
|
|
//multiply by a scalar |
| 196 |
|
|
friend Vector3d operator* ( const Vector3d& v, const double& r) { |
| 197 |
|
|
return Vector3d( r*v.x, r*v.y, r*v.z); |
| 198 |
|
|
} |
| 199 |
|
|
//divide by a scalar |
| 200 |
|
|
friend Vector3d operator/ ( const Vector3d& v, const double& r) { |
| 201 |
|
|
return Vector3d( v.x/r, v.y/r, v.z/r); |
| 202 |
|
|
} |
| 203 |
|
|
|
| 204 |
|
|
|
| 205 |
|
|
//dot product |
| 206 |
|
|
friend double dotProduct (const Vector3d& v1, const Vector3d& v2){ |
| 207 |
|
|
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; |
| 208 |
|
|
} |
| 209 |
|
|
|
| 210 |
|
|
//cross product |
| 211 |
|
|
friend Vector3d crossProduct (const Vector3d& v1, const Vector3d& v2){ |
| 212 |
|
|
Vector3d result; |
| 213 |
|
|
|
| 214 |
|
|
result.x = v1.y * v2.z - v1.z * v2.y ; |
| 215 |
|
|
result.y = -v1.x * v2.z + v1.z * v2.x ; |
| 216 |
|
|
result.z = v1.x * v2.y - v1.y * v2.x; |
| 217 |
|
|
|
| 218 |
|
|
return result; |
| 219 |
|
|
} |
| 220 |
|
|
|
| 221 |
|
|
void normalize(){ |
| 222 |
|
|
double len; |
| 223 |
|
|
|
| 224 |
|
|
len = length(); |
| 225 |
|
|
x /= len ; |
| 226 |
|
|
y /= len; |
| 227 |
|
|
z /= len; |
| 228 |
|
|
} |
| 229 |
|
|
|
| 230 |
|
|
double length(){ |
| 231 |
|
|
return sqrt(x*x + y*y + z*z); |
| 232 |
|
|
} |
| 233 |
|
|
|
| 234 |
|
|
double length2(){ |
| 235 |
|
|
return x*x + y*y + z*z; |
| 236 |
|
|
} |
| 237 |
|
|
|
| 238 |
|
|
public: |
| 239 |
|
|
|
| 240 |
tim |
1443 |
double x; |
| 241 |
|
|
double y; |
| 242 |
|
|
double z; |
| 243 |
tim |
1427 |
}; |
| 244 |
|
|
|
| 245 |
|
|
|
| 246 |
|
|
#endif //end ifndef _VECTOR3D_H_ |