-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.hh
executable file
·346 lines (273 loc) · 7.6 KB
/
vector.hh
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* \file vector.hh Contains the implementation of the Vector3D class
* and several operator overloads.
*/
#ifndef VECTOR_HH
#define VECTOR_HH
#include <iostream>
#include <cassert>
#include <cmath>
using namespace std;
/** A shorter definition for an unsigned integer.
*/
typedef unsigned int uint;
/**
* An implementation of a 3-dimensional vector of floats.
* Can be used to represent both direction and position vectors.
* The basis for this vector space is formed by the unit vectors:
* (1, 0, 0), (0, 1, 0) and (0, 0, 1).
* The class lacks a desctructor because it makes no use of dynamically
* allocated objects.
*/
class Vector3D
{
private:
/**
* An array of floats holding the coordinates of the vector.
* The coordinates are as follows:
* vec[0] == x
* vec[1] == y
* vec[2] == z
*/
float vec[3];
public:
//Constructors
/** The default/copy constructor.
* Initializes all components to 0 (zero).
* @see Vector3D(float x, float y, float z)
* @return Has no return value.
*/
Vector3D();
/** Another constructor initializing the vector by componets.
* @param x the x component
* @param y the y component
* @param z the z component
* @see Vector3D()
* @return Has no return value.
*/
Vector3D(float x, float y, float z);
Vector3D(float * vec_);
//Compound operators
/** An overload of the += operator.
* @param Other The right hand side argument.
*/
Vector3D & operator+=(const Vector3D & Other);
/** An overload of the -= operator.
* @param Other The right hand side argument.
*/
Vector3D & operator-=(const Vector3D & Other);
/** An overload of the *= operator (scaling of a vector).
*/
Vector3D & operator*=(float scalar);
/** An overload of the /= operator (scaling of a vector).
*/
Vector3D & operator/=(float scalar);
//Accessor & Mutator overloads of []
/** An overload of the [] operator.
* @param index The index of the component to access.
* @see vec
* @see uint
* @return Returns the corresponding component of the vector
* as a constant float.
* Used on the right hand side of any binary operation.
*/
const float operator[](uint index) const;
/** An overload of the [] operator.
* @param index The index of the component to access.
* @see vec
* @see uint
* @return Returns a reference to the corresponding component of the vector \
* Used on the left hand side of any binary operation.
*/
float & operator[](uint index);
//Additional methods
/** Returns the squared magnitude of the vector.
*/
const float magn2() const;
/** Returns the magnintude of the vector.
*/
const float magn() const;
/** Normalizes the vector.
*/
void normalize();
};
//Default constructor
inline Vector3D::Vector3D()
{
vec[0] = 0;
vec[1] = 0;
vec[2] = 0;
}
// Constructor with initialization componentwise
inline Vector3D::Vector3D(float x, float y, float z)
{
vec[0] = x;
vec[1] = y;
vec[2] = z;
}
inline Vector3D::Vector3D(float * vec_)
{
vec[0] = vec_[0];
vec[1] = vec_[1];
vec[2] = vec_[2];
}
// [] operator overload, used on the RIGHT HAND SIDE,
// returns a "read-only" object
inline const float Vector3D::operator[](uint index) const
{
assert(index < 3);
return vec[index];
}
// [] operator overload, used on the LEFT HAND SIDE ,
// return a "writable" object. Supports chaining
inline float & Vector3D::operator[](uint index)
{
assert(index < 3);
return vec[index];
}
inline Vector3D & Vector3D::operator+=(const Vector3D & Other)
{
vec[0] += Other[0];
vec[1] += Other[1];
vec[2] += Other[2];
return *this;
}
inline Vector3D & Vector3D::operator-=(const Vector3D & Other)
{
vec[0] -= Other[0];
vec[1] -= Other[1];
vec[2] -= Other[2];
return *this;
}
inline Vector3D & Vector3D::operator*=(const float scalar)
{
vec[0] *= scalar;
vec[1] *= scalar;
vec[2] *= scalar;
return *this;
}
// Compound division by a non-zero scalar
inline Vector3D & Vector3D::operator/=(const float scalar)
{
assert(scalar != 0);
vec[0] /= scalar;
vec[1] /= scalar;
vec[2] /= scalar;
return *this;
}
// The unary - operator.
/** An implementation of the subtraction operation for vectors.
*/
inline const Vector3D operator-(const Vector3D & Vec)
{
Vector3D NewVector = Vec;
return NewVector *= -1;
}
// Vector * Scalar multiplication
/** Scaling a vector by multiplication.
* Usage: vector * scalar
*/
inline const Vector3D operator*(const Vector3D & RHS, const float scalar)
{
Vector3D NewVector = RHS;
return NewVector *= scalar;
}
// Scalar * Vector multiplication
/** Scaling a vector by multiplication.
* Usage: scalar * vector
*/
inline const Vector3D operator*(const float scalar, const Vector3D & Other)
{
return Other * scalar;
}
//The binary - operator between 2 vectors
//Could be implemented as A + (-B)
/** Substraction of two vectors.
*/
inline const Vector3D operator-(const Vector3D & RHS, const Vector3D & LHS)
{
Vector3D NewVector = RHS;
return NewVector -= LHS;
}
//Addition of 2 vectors
/** Addition of two vectors.
*/
inline const Vector3D operator+(const Vector3D & RHS, const Vector3D & LHS)
{
Vector3D NewVector = RHS;
return NewVector += LHS;
}
// Vector * 1 / Scalar
// Could be implemented as Vector * (1 / Scalar)
/** Scaling a vector by division.
*/
inline const Vector3D operator/(const Vector3D & RHS, const float scalar)
{
// The non-nullity of the scalar is checked in /=
Vector3D NewVector = RHS;
return NewVector /= scalar;
}
//Vector dot product
/** The dot product of two vectors.
* @param V1 The first vector.
* @param V2 The second vector.
* @return The result is invariant of the order of V1 and V2.
*/
inline const float dot(const Vector3D &V1, const Vector3D &V2)
{
return V1[0] * V2[0] + V1[1] * V2[1] + V1[2] * V2[2];
}
//Vector cross product
// Convention X * Y = Z
/**
* The cross product of two vectors.
* @param Left The left hand side argument of the operation.
* @param Right The right hand side argument of the operation.
* @return A constant 3-dimensional vector.
* Convetion: cross(x, y) = z (Right hand sided coordinate system)
*/
inline const Vector3D cross(const Vector3D &Left, const Vector3D &Right)
{
Vector3D NewVector;
NewVector[0] = Left[1] * Right[2] - Left[2] * Right[1];
NewVector[1] = Left[2] * Right[0] - Left[0] * Right[2];
NewVector[2] = Left[0] * Right[1] - Left[1] * Right[0];
return NewVector;
}
inline const Vector3D project(const Vector3D & Left, const Vector3D & Right)
{
return Right * dot(Left, Right) / dot(Right, Right);
}
// The squared magnintude of the vector
inline const float Vector3D::magn2() const
{
return dot(*this,*this);
}
//The mangintude of the vector
inline const float Vector3D::magn() const
{
return sqrt(magn2());
}
// Normalizes the vector
inline void Vector3D::normalize()
{
float magnitude = magn();
vec[0] /= magnitude;
vec[1] /= magnitude;
vec[2] /= magnitude;
}
// Left shift operator for output to streams
/** An overload of the shift-left operator
* Outputs the components of the vector to the stream in the form (x, y, z).
* No newline at the end.
*/
inline ostream & operator<<(ostream & os, const Vector3D & Vec)
{
os << "("
<< Vec[0] << ", "
<< Vec[1] << ", "
<< Vec[2]
<< ")";
return os;
}
#endif // VECTOR_HH