-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
153 lines (123 loc) · 3.73 KB
/
vector.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
/**
* vector.h
* Purpose: general purpose 3D vector operations
*
* @author Manuel Infosec
* @version 1.0
*/
#ifndef VECTOR_H
#define VECTOR_H
#include "matrix.h"
/**
* vector class, for representation and manipulation of vectors
*
* @param T the data type being stored in the vector.
*/
template<typename T>
class vector {
public:
/**
* The x, y, and z coordinates
*/
T x, y, z;
/**
* Copy constructor
*
* @param v the vector to copy
*/
vector<T>(const vector<T> &v): x(v.x), y(v.y), z(v.z) {}
/**
* Constructor for vector. Passing two dimensions results in Z being set to T(0).
*
* @param _x the x value.
* @param _y the y value.
* @param _z the z value (defaults to 0).
*/
vector(T _x, T _y, T _z = T(0)): x(_x), y(_y), z(_z){}
/**
* Adds two vectors and returns their sum.
*
* @param v the vector to add.
* @return the vector sum of the two vectors.
*/
inline vector<T> operator + (const vector<T> &v) const {
return vector<T>(x + v.x, y + v.y, z + v.z);
}
/**
* Returns the negative of the vector
*
* @return the negative of the vector.
*/
inline vector<T> operator - () const {
return vector<T>(-x, -y, -z);
}
/**
* Subtracts two vectors and returns their difference.
*
* @param v the vector to subtract.
* @return the vector difference of the two vectors.
*/
inline vector<T> operator - (const vector<T> &v) {
return (*this) + (-v);
}
/**
* Scales a vector by a constant.
*
* @param t the constant to scale by.
* @return the scaled vector.
*/
inline vector<T> operator * (const T &t) const {
return vector<T>(t * x, t * y, t * z);
}
/**
* Takes two vectors and returns their dot product.
*
* @param v the vector to take the dot product with.
* @return the dot product of the two vectors.
*/
inline T operator * (const vector<T> &v) const {
return x * v.x + y * v.y + z * v.z;
}
/**
* Takes two vectors and returns their dot product.
*
* @param v the vector to take the dot product with.
* @return the dot product of the two vectors.
*/
inline vector<T> operator ^ (const vector<T> &v) const {
return vector<T>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
/**
* Returns the magnitude squared of the vector.
*
* The magnitude is squared to avoid having to convert data types (int -> double).
*
* @return the magnitude of the vector, squared.
*/
inline T magnitude() const {
return (*this) * (*this);
}
/**
* Returns the unit vector of the vector.
* Please don't use this unless sqrt accepts the data type T...
*
* @return the vector as a unit vector
*/
inline vector<T> normalize() const {
return (*this) * sqrt(1/magnitude());
}
/**
* Returns the vector as a matrix.
* This is for use with Euler Angles
*
* @return the vector as a column matrix.
*/
inline matrix<T> to_matrix() const {
matrix<T> ret = matrix<T>(3,1);
ret(0, 0) = x;
ret(0, 1) = y;
ret(0, 2) = z;
return ret;
}
};
#endif