diff options
Diffstat (limited to 'vector3d.h')
-rw-r--r-- | vector3d.h | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/vector3d.h b/vector3d.h new file mode 100644 index 0000000..7daadd6 --- /dev/null +++ b/vector3d.h | |||
@@ -0,0 +1,130 @@ | |||
1 | #ifndef VECTOR3D_H_26B75ED0 | ||
2 | #define VECTOR3D_H_26B75ED0 | ||
3 | |||
4 | #include <ostream> | ||
5 | #include <cmath> | ||
6 | |||
7 | class Vector3D { | ||
8 | public: | ||
9 | double x, y, z; | ||
10 | |||
11 | Vector3D() : x(0.0), y(0.0), z(0.0) {} | ||
12 | Vector3D(double x, double y, double z) : x(x), y(y), z(z) {} | ||
13 | Vector3D(double c) : x(c), y(c), z(c) {} | ||
14 | Vector3D(const Vector3D& v) : x(v.x), y(v.y), z(v.z) {} | ||
15 | |||
16 | inline double& operator[] (const int& index) | ||
17 | { | ||
18 | // Yuck | ||
19 | return ( &x )[ index ]; | ||
20 | } | ||
21 | |||
22 | inline const double& operator[] (const int& index) const | ||
23 | { | ||
24 | return ( &x )[ index ]; | ||
25 | } | ||
26 | |||
27 | inline bool operator==(const Vector3D& v) const | ||
28 | { | ||
29 | return v.x == x && v.y == y && v.z == z; | ||
30 | } | ||
31 | |||
32 | inline Vector3D operator-() const | ||
33 | { | ||
34 | return Vector3D( -x, -y, -z ); | ||
35 | } | ||
36 | |||
37 | inline Vector3D operator+(const Vector3D& v) const | ||
38 | { | ||
39 | return Vector3D( x + v.x, y + v.y, z + v.z ); | ||
40 | } | ||
41 | |||
42 | inline Vector3D operator-(const Vector3D& v) const | ||
43 | { | ||
44 | return Vector3D( x - v.x, y - v.y, z - v.z ); | ||
45 | } | ||
46 | |||
47 | inline Vector3D operator*(const double& c) const | ||
48 | { | ||
49 | return Vector3D( x * c, y * c, z * c ); | ||
50 | } | ||
51 | |||
52 | inline Vector3D operator/(const double& c) const | ||
53 | { | ||
54 | const double rc = 1.0/c; | ||
55 | return Vector3D( rc * x, rc * y, rc * z ); | ||
56 | } | ||
57 | |||
58 | inline void operator+=(const Vector3D& v) | ||
59 | { | ||
60 | x += v.x; | ||
61 | y += v.y; | ||
62 | z += v.z; | ||
63 | } | ||
64 | |||
65 | inline void operator-=(const Vector3D& v) | ||
66 | { | ||
67 | x -= v.x; | ||
68 | y -= v.y; | ||
69 | z -= v.z; | ||
70 | } | ||
71 | |||
72 | inline void operator*=(const double& c) | ||
73 | { | ||
74 | x *= c; | ||
75 | y *= c; | ||
76 | z *= c; | ||
77 | } | ||
78 | |||
79 | inline void operator/=(const double& c) | ||
80 | { | ||
81 | (*this) *= ( 1./c ); | ||
82 | } | ||
83 | |||
84 | inline double norm() const | ||
85 | { | ||
86 | return sqrt( x*x + y*y + z*z ); | ||
87 | } | ||
88 | |||
89 | inline double norm2() const | ||
90 | { | ||
91 | return x*x + y*y + z*z; | ||
92 | } | ||
93 | |||
94 | inline Vector3D unit() const | ||
95 | { | ||
96 | double rNorm = 1. / sqrt( x*x + y*y + z*z ); | ||
97 | return Vector3D( rNorm*x, rNorm*y, rNorm*z ); | ||
98 | } | ||
99 | |||
100 | inline void normalize() | ||
101 | { | ||
102 | (*this) /= norm(); | ||
103 | } | ||
104 | |||
105 | }; | ||
106 | |||
107 | // left scalar multiplication | ||
108 | inline Vector3D operator* (const double& c, const Vector3D& v) | ||
109 | { | ||
110 | return Vector3D( c * v.x, c * v.y, c * v.z ); | ||
111 | } | ||
112 | |||
113 | // dot product (a.k.a. inner or scalar product) | ||
114 | inline double dot(const Vector3D& u, const Vector3D& v) | ||
115 | { | ||
116 | return u.x*v.x + u.y*v.y + u.z*v.z ; | ||
117 | } | ||
118 | |||
119 | // cross product | ||
120 | inline Vector3D cross(const Vector3D& u, const Vector3D& v) | ||
121 | { | ||
122 | return Vector3D( u.y*v.z - u.z*v.y, | ||
123 | u.z*v.x - u.x*v.z, | ||
124 | u.x*v.y - u.y*v.x ); | ||
125 | } | ||
126 | |||
127 | // prints components | ||
128 | std::ostream& operator<<(std::ostream& os, const Vector3D& v); | ||
129 | |||
130 | #endif /* end of include guard: VECTOR3D_H_26B75ED0 */ | ||