-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector3.h
More file actions
305 lines (234 loc) · 9.31 KB
/
vector3.h
File metadata and controls
305 lines (234 loc) · 9.31 KB
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
#pragma once
#include <iostream>
#include <cstddef>
#include <cmath>
#include <cassert>
#include <limits>
/************************************************* Declaration **************************************************/
template <typename T>
class Vector3T {
public:
static const std::size_t dim = 3;
// Initialize with default value
explicit Vector3T();
// Initialize with a certain value
explicit Vector3T(T val);
// Initialize with 3 values
explicit Vector3T(T val_x, T val_y, T val_z);
// Initialize with an array of the same type
explicit Vector3T(T val_array[3]);
~Vector3T() = default;
// Copy constructor with Vector3T of type K
template <typename K>
Vector3T(const Vector3T<K> &v);
// Default copy constructor and assignment operator
Vector3T(const Vector3T<T> &v) = default;
Vector3T &operator=(const Vector3T &v) = default;
T& operator[](const size_t i);
T operator[](const size_t i) const;
typedef T value_type;
// Vector components
T x, y, z;
};
// Basic vector arithmetics
template <typename T> Vector3T<T> operator+ (const Vector3T<T>& lhs, const Vector3T<T>& rhs);
template <typename T> Vector3T<T> operator+=( Vector3T<T>& lhs, const Vector3T<T>& rhs);
template <typename T> Vector3T<T> operator- (const Vector3T<T>& lhs);
template <typename T> Vector3T<T> operator- (const Vector3T<T>& lhs, const Vector3T<T>& rhs);
template <typename T> Vector3T<T> operator-=( Vector3T<T>& lhs, const Vector3T<T>& rhs);
template <typename T> Vector3T<T> operator* (const Vector3T<T>& lhs, const T rhs);
template <typename T> Vector3T<T> operator* (const T lhs, const Vector3T<T>& rhs);
template <typename T> Vector3T<T> operator* (const Vector3T<T>& lhs, const Vector3T<T>& rhs);
template <typename T> Vector3T<T> operator*=( Vector3T<T>& lhs, const T rhs);
template <typename T> Vector3T<T> operator*=( Vector3T<T>& lhs, const Vector3T<T>& rhs);
template <typename T> Vector3T<T> operator/ (const Vector3T<T>& lhs, const T rhs);
template <typename T> Vector3T<T> operator/ (const Vector3T<T>& lhs, const Vector3T<T>& rhs);
template <typename T> Vector3T<T> operator/=( Vector3T<T>& lhs, const T rhs);
template <typename T> Vector3T<T> operator/=( Vector3T<T>& lhs, const Vector3T<T>& rhs);
// Equality / Inequality tests
template <typename T> bool operator!=(const Vector3T<T>& lhs, const Vector3T<T>& rhs);
template <typename T> bool operator==(const Vector3T<T>& lhs, const Vector3T<T>& rhs);
// Dot product, Cross product, Basis change
template <typename T> T dot (const Vector3T<T>& lhs, const Vector3T<T>& rhs);
template <typename T> Vector3T<T> cross (const Vector3T<T>& lhs, const Vector3T<T>& rhs);
template <typename T> Vector3T<T> basis (const Vector3T<T>& lhs, const Vector3T<T>& rhs);
// Norms
template <typename T> T norm2 (const Vector3T<T>& lhs);
template <typename T> T norm (const Vector3T<T>& lhs);
template <typename T> Vector3T<T> normalize ( Vector3T<T> lhs);
/******************************************** Typedefs for common vectors *******************************************/
typedef Vector3T<unsigned int> Vector3I;
typedef Vector3T<float> Vector3F;
typedef Vector3T<double> Vector3D;
typedef Vector3I Vec3i;
typedef Vector3F Vec3f;
typedef Vector3D Vec3;
/*************************************************** Implementation *************************************************/
template <typename T>
inline Vector3T<T>::Vector3T() : x(std::numeric_limits<T>::min()),
y(std::numeric_limits<T>::min()),
z(std::numeric_limits<T>::min()) { }
template <typename T>
inline Vector3T<T>::Vector3T(T val) : x(val),
y(val),
z(val) { }
template <typename T>
inline Vector3T<T>::Vector3T(T val_x, T val_y, T val_z) : x(val_x),
y(val_y),
z(val_z) { }
template <typename T>
inline Vector3T<T>::Vector3T(T val_array[3]) : x(val_array[0]),
y(val_array[1]),
z(val_array[2]) { }
template <typename T>
template <typename K>
inline Vector3T<T>::Vector3T(const Vector3T<K> &v) : x(static_cast<T>(v.x)),
y(static_cast<T>(v.y)),
z(static_cast<T>(v.z)) { }
template <typename T>
inline T& Vector3T<T>::operator[](const size_t i){
assert(i < dim);
return (&x)[i];
}
template <typename T>
inline T Vector3T<T>::operator[](const size_t i) const{
assert(i < dim);
return (&x)[i];
}
// Basic vector arithmetics
template <typename T>
inline Vector3T<T> operator+ (const Vector3T<T>& lhs, const Vector3T<T>& rhs){
Vector3T<T> res_vec(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
return res_vec;
}
template <typename T>
inline Vector3T<T> operator+=( Vector3T<T>& lhs, const Vector3T<T>& rhs){
lhs.x += rhs.x;
lhs.y += rhs.y;
lhs.z += rhs.z;
return lhs;
}
template <typename T>
inline Vector3T<T> operator- (const Vector3T<T>& lhs){
Vector3T<T> res_vec(-lhs.x, -lhs.y, -lhs.z);
return res_vec;
}
template <typename T>
inline Vector3T<T> operator- (const Vector3T<T>& lhs, const Vector3T<T>& rhs){
Vector3T<T> res_vec(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
return res_vec;
}
template <typename T>
inline Vector3T<T> operator-=( Vector3T<T>& lhs, const Vector3T<T>& rhs){
lhs.x -= rhs.x;
lhs.y -= rhs.y;
lhs.z -= rhs.z;
return lhs;
}
template <typename T>
inline Vector3T<T> operator* (const Vector3T<T>& lhs, const T rhs){
Vector3T<T> res_vec(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs);
return res_vec;
}
template <typename T>
inline Vector3T<T> operator* (const T lhs, const Vector3T<T>& rhs){
Vector3T<T> res_vec(rhs.x * lhs, rhs.y * lhs, rhs.z * lhs);
return res_vec;
}
template <typename T>
inline Vector3T<T> operator* (const Vector3T<T>& lhs, const Vector3T<T>& rhs){
Vector3T<T> res_vec(lhs.x * rhs.x, lhs.y * rhs.y, lhs.z * rhs.z);
return res_vec;
}
template <typename T>
inline Vector3T<T> operator*=( Vector3T<T>& lhs, const T rhs){
lhs.x *= rhs;
lhs.y *= rhs;
lhs.z *= rhs;
return lhs;
}
template <typename T>
inline Vector3T<T> operator*=( Vector3T<T>& lhs, const Vector3T<T>& rhs){
lhs.x *= rhs.x;
lhs.y *= rhs.y;
lhs.z *= rhs.z;
return lhs;
}
template <typename T>
inline Vector3T<T> operator/ (const Vector3T<T>& lhs, const T rhs){
Vector3T<T> res_vec(lhs.x / rhs, lhs.y / rhs, lhs.z / rhs);
return res_vec;
}
template <typename T>
inline Vector3T<T> operator/ (const Vector3T<T>& lhs, const Vector3T<T>& rhs){
Vector3T<T> res_vec(lhs.x / rhs.x, lhs.y / rhs.y, lhs.z / rhs.z);
return res_vec;
}
template <typename T>
inline Vector3T<T> operator/=( Vector3T<T>& lhs, const T rhs){
lhs.x /= rhs;
lhs.y /= rhs;
lhs.z /= rhs;
return lhs;
}
template <typename T>
inline Vector3T<T> operator/=( Vector3T<T>& lhs, const Vector3T<T>& rhs){
lhs.x /= rhs.x;
lhs.y /= rhs.y;
lhs.z /= rhs.z;
return lhs;
}
// Equality / Inequality tests
template <typename T>
inline bool operator!=(const Vector3T<T>& lhs, const Vector3T<T>& rhs){
return (lhs.x != rhs.x || lhs.y != rhs.y || lhs.z != rhs.z);
}
template <typename T>
inline bool operator==(const Vector3T<T>& lhs, const Vector3T<T>& rhs){
return (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z);
}
// Dot product, Cross product.
template <typename T>
inline T dot(const Vector3T<T>& lhs, const Vector3T<T>& rhs){
T res_val(lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z);
return res_val;
}
template <typename T>
inline Vector3T<T> cross(const Vector3T<T>& lhs, const Vector3T<T>& rhs){
Vector3T<T> res_vec(lhs.y * rhs.z - rhs.y * lhs.z,
lhs.z * rhs.x - rhs.z * lhs.x,
lhs.x * rhs.y - rhs.x * lhs.y);
return res_vec;
}
template <typename T>
inline Vector3T<T> basis(const Vector3T<T> &lhs, const Vector3T<T> &rhs){
Vec3 u, w, v = rhs;
if (rhs.z < -0.9999999) {
u = Vec3(0.0, -1.0, 0.0);
w = Vec3(-1.0, 0.0, 0.0);
}
else {
const double a = 1.0 / (1.0 + rhs.z);
const double b = -rhs.x * rhs.y * a;
u = Vec3(1.0 - rhs.x * rhs.x * a, b, -rhs.x);
w = Vec3(b, 1.0 - rhs.y * rhs.y * a, -rhs.y);
}
return Vec3(dot(lhs, Vec3(u.x, v.x, w.x)), dot(lhs, Vec3(u.y, v.y, w.y)), dot(lhs, Vec3(u.z, v.z, w.z)));
}
// Norms
template <typename T>
inline T norm2(const Vector3T<T>& lhs){
return dot(lhs, lhs);
}
template <typename T>
inline T norm(const Vector3T<T>& lhs){
return std::sqrt(norm2(lhs));
}
template <typename T>
inline Vector3T<T> normalize(Vector3T<T> lhs){
T div = 1.0 / norm(lhs);
lhs.x *= div;
lhs.y *= div;
lhs.z *= div;
return lhs;
}