-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector3.h
executable file
·264 lines (205 loc) · 5.88 KB
/
Vector3.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#ifndef MIRO_VECTOR3_H_INCLUDED
#define MIRO_VECTOR3_H_INCLUDED
#include <math.h>
#include <float.h>
#include <iostream>
#ifdef _WIN32
#pragma warning(disable:4305) // disable useless warnings
#pragma warning(disable:4244)
#endif
class Vector3
{
public:
float x, y, z; // The x & y & z coordinates.
Vector3() : x(0), y(0), z(0) {}
Vector3(float s) : x(s), y(s), z(s) {}
Vector3(float xVal, float yVal, float zVal) : x(xVal), y(yVal), z(zVal) {}
// Assignment operator.
const Vector3 & operator=(const Vector3& a) {x = a.x; y = a.y; z = a.z; return *this;}
// Assignment operator.
const Vector3 & operator=(float a) {x = y = z = a; return *this;}
void set(float a) {x = y = z = a;}
void set(float a, float b, float c) {x = a; y = b; z = c;}
void set(const Vector3 & v) {x = v.x; y = v.y; z = v.z;}
// Access operator.
/*
Returns the ith component of the vector.
\param i The component to return.
\warning i must be either 0, 1, or 2 in order to get expected results.
*/
float & operator[](int i) {return (&x)[i];}
// Constant access operator.
/*
Returns the ith component of a constant vector.
\param i The component to return.
\warning i must be either 0, 1, or 2 in order to get expected results.
*/
const float & operator[](int i) const {return (&x)[i];}
// Component-wise vector addition operator.
Vector3 operator+(const Vector3& v) const
{
return Vector3(x + v.x, y + v.y, z + v.z);
}
// Component-wise vector addition-assignment operator.
const Vector3 & operator+=(const Vector3& v)
{
x += v.x; y += v.y; z += v.z; return *this;
}
// Scalar addition-assignment operator.
const Vector3 & operator+=(float a) {x += a; y += a; z += a; return *this;}
// Component-wise vector subtraction operator.
Vector3 operator-(const Vector3& v) const
{
return Vector3(x - v.x, y - v.y, z - v.z);
}
// Component-wise vector subtraction-assignment operator.
const Vector3 & operator-=(const Vector3& v)
{
x -= v.x; y -= v.y; z -= v.z; return *this;
}
// Component-wise scalar subtraction assignment operator.
const Vector3 & operator-=(float a) {x -= a; y -= a; z -= a; return *this;}
// Scalar multiplication operator.
Vector3 operator*(float a) const {return Vector3(x * a, y * a, z * a);}
// Component-wise vector multiplication operator.
Vector3 operator*(const Vector3& v) const
{
return Vector3(x * v.x, y * v.y, z * v.z);
}
// Scalar multiplication-assignment operator.
const Vector3 & operator*=(float a) {x *= a; y *= a; z *= a; return *this;}
// Component-wise vector multiplication-assignment operator.
const Vector3 & operator*=(const Vector3& v)
{
x *= v.x; y *= v.y; z *= v.z; return *this;
}
// Negation operator.
Vector3 operator-() const {return Vector3(-x, -y, -z);}
const Vector3 & negate() {x = -x; y = -y; z = -z; return *this;}
// Scalar division operator.
Vector3 operator/(float a) const
{
float inv = float(1) / a;
return Vector3(x * inv, y * inv, z * inv);
}
// Component-wise vector division operator.
Vector3 operator/(const Vector3 & v) const
{
return Vector3(x / v.x, y / v.y, z / v.z);
}
// Scalar division-assignment operator.
const Vector3 & operator/=(float a)
{
float inv = float(1) / a;
x *= inv; y *= inv; z *= inv;
return *this;
}
// Component-wise vector division-assignment operator.
const Vector3 & operator/=(const Vector3 & v)
{
x /= v.x; y /= v.y; z /= v.z; return *this;
}
// Vector equivalence operator.
/*
Tests to see if each component of \a v is equal to each component of
this Vector3.
*/
bool operator==(const Vector3 & v) const
{
return(v.x == x && v.y == y && v.z == z);
}
// Vector difference operator.
/*
Tests to see if any component is different between the two Vec3s.
*/
bool operator!=(const Vector3 & v) const
{
return(v.x != x || v.y != y || v.z != z);
}
// Length<sup>2</sup>.
/*
Returns the geometric length<sup>2</sup> of the vector.
*/
float length2() const;
// Length.
/*
Returns the geometric length of the vector.
*/
float length() const {return sqrtf(length2());}
// Normalizes the vector and return its length.
/*
Scales each component of the vector in order to get unit
length without changing direction.
\return The length of the vector prior to normalization.
*/
float unitize()
{
float l = length();
*this /= l;
return l;
}
// Normalize a vector and return a reference to it.
/*
Scales each component of the vector in order to get unit
length without changing direction.
\return A reference to the vector.
*/
const Vector3 & normalize()
{
return (*this /= length());
}
// Return a normalized copy of the vector.
Vector3 normalized() const
{
return( *this / length());
}
// Return a rotated copy of the vector
Vector3 rotated(float theta, const Vector3 & w) const;
// Rotate this vector about another vector, w, by theta radians.
const Vector3 & rotate(float theta, const Vector3 & w)
{
return *this = rotated(theta, w);
}
};
// Multiply a scalar by a Vec3.
inline Vector3
operator*(float s, const Vector3& v)
{
return Vector3(v.x * s, v.y * s, v.z * s);
}
// The dot product of two Vec3s.
inline float
dot(const Vector3 & a, const Vector3 & b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
// The cross product of two Vec3s.
inline Vector3
cross(const Vector3 & a, const Vector3 & b)
{
return Vector3(a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
inline float
Vector3::length2() const
{
return dot(*this, *this);
}
// Return a rotated copy of the vector
inline Vector3
Vector3::rotated(float theta, const Vector3 & w) const
{
float c = cosf(theta);
float s = sinf(theta);
Vector3 v0 = dot(*this, w) * w;
Vector3 v1 = *this - v0;
Vector3 v2 = cross(w, v1);
return v0 + c * v1 + s * v2;
}
inline std::ostream &
operator<<(std::ostream& out, const Vector3& v)
{
return out << v.x << " " << v.y << " " << v.z;
}
#endif // MIRO_VECTOR3_H_INCLUDED