|
1 /* |
|
2 * C++ RayTracer |
|
3 * file: vector.h |
|
4 * |
|
5 * Radek Brich, 2006 |
|
6 */ |
|
7 |
|
8 #ifndef VECTOR_H |
|
9 #define VECTOR_H |
|
10 |
|
11 #include <math.h> |
|
12 #include <iostream> |
|
13 |
|
14 using namespace std; |
|
15 |
|
16 class Vector3 |
|
17 { |
|
18 public: |
|
19 // data |
|
20 union { |
|
21 struct { |
|
22 Float x, y, z; |
|
23 }; |
|
24 struct { |
|
25 Float r, g, b; |
|
26 }; |
|
27 Float cell[3]; |
|
28 }; |
|
29 |
|
30 // constructors |
|
31 Vector3(): x(0.0f), y(0.0f), z(0.0f) {}; |
|
32 Vector3(Float ax, Float ay, Float az): x(ax), y(ay), z(az) {}; |
|
33 |
|
34 // index operator |
|
35 Float &operator[](int index) { return cell[index]; }; |
|
36 |
|
37 bool operator==(Vector3 &v) { return x==v.x && y==v.y && z==v.z; }; |
|
38 |
|
39 // normalize |
|
40 Vector3 normalize() |
|
41 { |
|
42 Float f = 1.0f / mag(); |
|
43 x *= f; |
|
44 y *= f; |
|
45 z *= f; |
|
46 return *this; |
|
47 } |
|
48 |
|
49 // get normalized copy |
|
50 Vector3 unit() |
|
51 { |
|
52 Vector3 u(*this); |
|
53 return u.normalize();; |
|
54 } |
|
55 |
|
56 // square magnitude, magnitude |
|
57 Float mag2() { return x * x + y * y + z * z; } |
|
58 Float mag() { return sqrtf(mag2()); } |
|
59 |
|
60 // negative |
|
61 Vector3 operator-() { return Vector3(-x, -y, -z); } |
|
62 |
|
63 // accumulate |
|
64 Vector3 operator+=(const Vector3 &v) |
|
65 { |
|
66 x += v.x; |
|
67 y += v.y; |
|
68 z += v.z; |
|
69 return *this; |
|
70 }; |
|
71 |
|
72 // sum |
|
73 friend Vector3 operator+(const Vector3 &a, const Vector3 &b) |
|
74 { |
|
75 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); |
|
76 }; |
|
77 |
|
78 // difference |
|
79 friend Vector3 operator-(const Vector3 &a, const Vector3 &b) |
|
80 { |
|
81 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); |
|
82 }; |
|
83 |
|
84 // dot product |
|
85 friend Float dot(const Vector3 &a, const Vector3 &b) |
|
86 { |
|
87 return a.x * b.x + a.y * b.y + a.z * b.z; |
|
88 }; |
|
89 |
|
90 // cross product |
|
91 friend Vector3 cross(const Vector3 &a, const Vector3 &b) |
|
92 { |
|
93 return Vector3(a.y * b.z - a.z * b.y, |
|
94 a.z * b.x - a.x * b.z, |
|
95 a.x * b.y - a.y * b.x); |
|
96 }; |
|
97 |
|
98 // product of vector and scalar |
|
99 friend Vector3 operator*(const Vector3 &v, const Float &f) |
|
100 { |
|
101 return Vector3(f * v.x, f * v.y, f * v.z); |
|
102 } |
|
103 |
|
104 friend Vector3 operator*(const Float &f, const Vector3 &v) |
|
105 { |
|
106 return v * f; |
|
107 }; |
|
108 |
|
109 // vector plus scalar |
|
110 friend Vector3 operator+(const Vector3 &v, const Float &f) |
|
111 { |
|
112 return Vector3(v.x + f, v.y + f, v.z + f); |
|
113 } |
|
114 |
|
115 // vector minus scalar |
|
116 friend Vector3 operator-(const Vector3 &v, const Float &f) |
|
117 { |
|
118 return Vector3(v.x - f, v.y - f, v.z - f); |
|
119 } |
|
120 |
|
121 // cell by cell product (only usable for colours) |
|
122 friend Vector3 operator*(const Vector3 &a, const Vector3 &b) |
|
123 { |
|
124 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z); |
|
125 }; |
|
126 |
|
127 // print |
|
128 friend ostream & operator<<(ostream &st, const Vector3 &v) |
|
129 { |
|
130 return st << "(" << v.x << ", " << v.y << ", " << v.z << ")"; |
|
131 } |
|
132 }; |
|
133 |
|
134 typedef Vector3 Colour; |
|
135 |
|
136 #endif |