-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec.h
184 lines (155 loc) · 4.07 KB
/
vec.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
#ifndef VEC_H
#define VEC_H
#include <algorithm>
#include <cassert>
#include <cmath>
#include <initializer_list>
#include <stdexcept>
#include <type_traits>
template<
int DIM,
typename T,
typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type
>
class vec;
template<int DIM, typename T>
vec<DIM, T> operator+(const vec<DIM, T>&, const vec<DIM, T>&);
template<int DIM, typename T>
vec<DIM, T> operator-(const vec<DIM, T>&, const vec<DIM, T>&);
template<int DIM, typename T>
T operator*(const vec<DIM, T>&, const vec<DIM, T>&);
template<int DIM, typename T>
vec<DIM, T> operator*(const vec<DIM, T>&, T);
template<int DIM, typename T>
vec<DIM, T> operator*(T, const vec<DIM, T>&);
template<int DIM, typename T>
vec<DIM, T> operator/(const vec<DIM, T>&, T);
template<int DIM, typename T>
bool same_direction(const vec<DIM, T>&, const vec<DIM, T>&);
template<int DIM, typename T, typename>
class vec {
public:
vec() : data() {}
vec(std::initializer_list<T> lst)
{
std::copy(lst.begin(), lst.end(), data);
for (size_t i = lst.size(); i < DIM; ++i) {
data[i] = T();
}
}
const T& operator[](int i) const
{
assert(i < DIM);
return data[i];
}
T& operator[](int i)
{
assert(i < DIM);
return data[i];
}
T magnitude_sq() const
{
T result = 0;
for (int i = 0; i < DIM; ++i) {
float e = static_cast<float>(data[i]);
result += e * e;
}
return result;
}
float magnitude() const
{
return sqrtf(static_cast<float>(magnitude_sq()));
}
vec normalized() const
{
return *this / magnitude();
}
static inline vec zero = vec();
vec operator-() const
{
vec<DIM, T> result;
for (int i = 0; i < DIM; ++i) {
result.data[i] = -this->data[i];
}
return result;
}
private:
T data[DIM];
friend vec operator+<>(const vec&, const vec&);
friend vec (::operator-<>)(const vec&, const vec&);
friend T operator*<>(const vec&, const vec&);
friend vec operator*<>(const vec&, T);
friend vec operator*<>(T, const vec&);
friend vec operator/<>(const vec&, T);
friend bool same_direction<>(const vec&, const vec&);
};
template<int DIM, typename T>
vec<DIM, T> operator+(const vec<DIM, T>& lhs, const vec<DIM, T>& rhs)
{
vec<DIM, T> result;
for (int i = 0; i < DIM; ++i) {
result.data[i] = lhs.data[i] + rhs.data[i];
}
return result;
}
template<int DIM, typename T>
vec<DIM, T> operator-(const vec<DIM, T>& lhs, const vec<DIM, T>& rhs)
{
vec<DIM, T> result;
for (int i = 0; i < DIM; ++i) {
result.data[i] = lhs.data[i] - rhs.data[i];
}
return result;
}
template<int DIM, typename T>
T operator*(const vec<DIM, T>& lhs, const vec<DIM, T>& rhs)
{
T result = 0;
for (int i = 0; i < DIM; ++i) {
result += lhs.data[i] * rhs.data[i];
}
return result;
}
template<int DIM, typename T>
vec<DIM, T> operator*(const vec<DIM, T>& vector, T scalar)
{
vec<DIM, T> result;
for (int i = 0; i < DIM; ++i) {
result.data[i] = vector.data[i] * scalar;
}
return result;
}
template<int DIM, typename T>
vec<DIM, T> operator*(T scalar, const vec<DIM, T>& vector)
{
vec<DIM, T> result;
for (int i = 0; i < DIM; ++i) {
result.data[i] = scalar * vector.data[i];
}
return result;
}
template<int DIM, typename T>
vec<DIM, T> operator/(const vec<DIM, T>& vector, T scalar)
{
vec<DIM, T> result;
for (int i = 0; i < DIM; ++i) {
result.data[i] = vector.data[i] / scalar;
}
return result;
}
template<int DIM, typename T>
bool same_direction(const vec<DIM, T>& u, const vec<DIM, T>& v)
{
T product{u * v};
if (product == 0) {
throw std::runtime_error("Direction comparison with a zero vector");
}
return product > 0;
}
typedef vec<2, float> vec2f;
typedef vec<3, float> vec3f;
typedef vec<4, float> vec4f;
typedef vec<2, int> vec2i;
typedef vec<3, int> vec3i;
typedef vec<4, int> vec4i;
#endif /* ifndef VEC_H */