-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec3.h
More file actions
313 lines (269 loc) · 7.81 KB
/
vec3.h
File metadata and controls
313 lines (269 loc) · 7.81 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
306
307
308
309
310
311
312
313
#ifndef __VEC3_H__
#define __VEC3_H__
#include<iostream>
#include<algorithm>
#include<inttypes.h>
#include<cmath>
#include<cassert>
template<class T>
class vec3 {
public:
static constexpr const size_t dim = 3;
vec3(void); ///< default constructor
vec3(const T& a, const T& b, const T& c); ///< initialized constructor
vec3(const vec3& other); ///< copy constructor
~vec3(void); ///< destructor
const T& operator[](size_t n) const; ///< RO component access
T& operator[](size_t n); ///< RW component access
const T& operator()(size_t n) const; ///< RO component access
T& operator()(size_t n); ///< RW component access
operator const T* (void) const; ///< cast to const pointer
operator T* (void); ///< cast to pointer
//T& x, & y, & z; ///< named attributes .x .y .z
//T& r, & g, & b; ///< named attributes .r .g .b
vec3& operator= (const vec3& other); ///< assignment operator
template<class D> vec3<D> as(void) const; ///< cast into vec3<D>
inline T dot(const vec3<T> & other) const; ///< dot product
T operator*(const vec3<T>& other) const; ///< vector dot product
T sqr_length(void) const; ///< squared length
T length(void) const; ///< length
void normalize(void); ///< normalize this vector
vec3<T> normalized(void) const; ///< return normalized vector
vec3<T> operator+(const vec3<T>& other) const; ///< vector addition
vec3<T> operator-(const vec3<T>& other) const; ///< vector subtraction
vec3<T> operator^(const vec3<T>& other) const; ///< vector cross product
vec3<T> operator-(void) const; ///< negation
vec3<T>& operator+=(const vec3<T>& other); ///< cumulative addition
vec3<T>& operator-=(const vec3<T>& other); ///< cumulative subtraction
vec3<T>& operator^=(const vec3<T>& other); ///< cumulative cross product
vec3<T> operator*(const T& val) const; ///< multiplication with rhs scalar
vec3<T> operator/(const T& val) const; ///< division by rhs scalar
vec3<T>& operator*=(const T& val); ///< cumulative multiplication with rhs scalar
vec3<T>& operator/=(const T& val); ///< cumulative division by rhs scalar
void swap(vec3& other); ///< swaps this vector with other
//protected:
union {
struct {
T x, y, z;
};
struct {
T r, g, b;
};
T m_data[dim];
};
};
using vec3f = vec3<float>;
using vec3d = vec3<double>;
using vec3i = vec3<int32_t>;
template<class T>
vec3<T> operator*(const T& val, vec3<T>& v) { ///< multiplication with lhs scalar
return vec3<T>(val * v.x, val * v.y, val * v.z);
}
template<class T>
vec3<T> lerp(const vec3<T>& A, const vec3<T>& B, const T& alpha) { ///< linear interpolation of vectors
return vec3<T>(
lerp(A.x, B.x, alpha),
lerp(A.y, B.y, alpha),
lerp(A.z, B.z, alpha));
}
template<class T>
std::ostream& operator<<(std::ostream& stream, const vec3<T>& v) { ///< debug output of vectors
stream << "[ " << v.x << " " << v.y << " " << v.z << " ]";
return stream;
}
template<class T>
vec3<T> clamp(const vec3<T>& val, const vec3<T>& vmin = vec3<T>(T(0), T(0), T(0)), const vec3<T>& vmax = vec3<T>(T(0), T(0), T(0))) { ///< component-wise clamp for vectors
return vec3<T>(
std::min<T>(std::max<T>(val.x, vmin.x), vmax.x),
std::min<T>(std::max<T>(val.y, vmin.y), vmax.y),
std::min<T>(std::max<T>(val.z, vmin.z), vmax.z)
);
}
/// extend std::max and std::min to vectors (component-wise)
/// also, define std::swap
namespace std {
template<class T>
inline vec3<T> max(const vec3<T>& A, const vec3<T>& B) {
return vec3<T>(
std::max(A.x, B.x),
std::max(A.y, B.y),
std::max(A.z, B.z)
);
}
template<class T>
inline vec3<T> min(const vec3<T>& A, const vec3<T>& B) {
return vec3<T>(
std::min(A.x, B.x),
std::min(A.y, B.y),
std::min(A.z, B.z)
);
}
template<class T>
inline vec3<T> abs(const vec3<T>& A) {
return vec3<T>(
std::abs(A.x),
std::abs(A.y),
std::abs(A.z)
);
}
};
template<class T>
vec3<T>::vec3(void) {//: x(m_data[0]), y(m_data[1]), z(m_data[2]), r(m_data[0]), g(m_data[1]), b(m_data[2]) {
m_data[0] = m_data[1] = m_data[2] = T(0);
}
template<class T>
vec3<T>::vec3(const T& a, const T& b, const T& c) {//: x(m_data[0]), y(m_data[1]), z(m_data[2]), r(m_data[0]), g(m_data[1]), b(m_data[2]) {
m_data[0] = a;
m_data[1] = b;
m_data[2] = c;
}
template<class T>
vec3<T>::vec3(const vec3& other) {//: x(m_data[0]), y(m_data[1]), z(m_data[2]), r(m_data[0]), g(m_data[1]), b(m_data[2]) {
*this = other;
}
template<class T>
vec3<T>::~vec3(void) {
}
template<class T>
const T& vec3<T>::operator[](size_t n) const {
assert("vec3[] -- invalid argument" && n < dim);
return m_data[n];
}
template<class T>
T& vec3<T>::operator[](size_t n) {
assert("vec3[] -- invalid argument" && n < dim);
return m_data[n];
}
template<class T>
const T& vec3<T>::operator()(size_t n) const {
assert("vec3() -- invalid argument" && n < dim);
return m_data[n];
}
template<class T>
T& vec3<T>::operator()(size_t n) {
assert("vec3() -- invalid argument" && n < dim);
return m_data[n];
}
template<class T>
vec3<T>::operator const T* (void) const {
return m_data;
}
template<class T>
vec3<T>::operator T* (void) {
return m_data;
}
template<class T>
vec3<T>& vec3<T>::operator= (const vec3& other) {
m_data[0] = other.m_data[0];
m_data[1] = other.m_data[1];
m_data[2] = other.m_data[2];
return *this;
}
template<class T>
template<class D>
vec3<D> vec3<T>::as(void) const {
return vec3<D>(D(m_data[0]), D(m_data[1]), D(m_data[2]));
}
template<class T>
T vec3<T>::operator*(const vec3<T>& other) const {
return x * other.x + y * other.y + z * other.z;
}
template<class T>
T vec3<T>::sqr_length(void) const {
return x * x + y * y + z * z;
}
template<class T>
T vec3<T>::length(void) const {
return T(sqrt(sqr_length()));
}
template<class T>
void vec3<T>::normalize(void) {
T ooLen = sqr_length();
if (ooLen == T(0)) return;
ooLen = T(1) / sqrt(ooLen);
x *= ooLen;
y *= ooLen;
z *= ooLen;
}
template<class T>
vec3<T> vec3<T>::normalized(void) const {
T ooLen = sqr_length();
if (ooLen == T(0)) return *this;
ooLen = T(1) / sqrt(ooLen);
return vec3<T>(x * ooLen, y * ooLen, z * ooLen);
}
template<class T>
inline T vec3<T>::dot(const vec3<T>& other) const {
return x * other.x + y * other.y + z * other.z;
}
template<class T>
vec3<T> vec3<T>::operator+(const vec3<T>& other) const {
return vec3(x + other.x, y + other.y, z + other.z);
}
template<class T>
vec3<T> vec3<T>::operator-(const vec3<T>& other) const {
return vec3(x - other.x, y - other.y, z - other.z);
}
template<class T>
vec3<T> vec3<T>::operator^(const vec3<T>& other) const {
return vec3(
y * other.z - other.y * z,
z * other.x - other.z * x,
x * other.y - other.x * y
);
}
template<class T>
vec3<T> vec3<T>::operator-(void) const {
return vec3(-x, -y, -z);
}
template<class T>
vec3<T>& vec3<T>::operator+=(const vec3<T>& other) {
x += other.x;
y += other.y;
z += other.z;
return *this;
}
template<class T>
vec3<T>& vec3<T>::operator-=(const vec3<T>& other) {
x -= other.x;
y -= other.x;
z -= other.z;
return *this;
}
template<class T>
vec3<T>& vec3<T>::operator^=(const vec3<T>& other) {
T tmp1 = y * other.z - other.y * z;
T tmp2 = z * other.x - other.z * x;
z = x * other.y - other.x * y;
x = tmp1;
y = tmp2;
}
template<class T>
vec3<T> vec3<T>::operator*(const T& val) const {
return vec3<T>(x * val, y * val, z * val);
}
template<class T>
vec3<T> vec3<T>::operator/(const T& val) const {
return vec3<T>(x / val, y / val, z / val);
}
template<class T>
vec3<T>& vec3<T>::operator*=(const T& val) {
x *= val;
y *= val;
z *= val;
return *this;
}
template<class T>
vec3<T>& vec3<T>::operator/=(const T& val) {
x /= val;
y /= val;
z /= val;
return *this;
}
template<class T>
void vec3<T>::swap(vec3& other) {
std::swap(x, other.x);
std::swap(y, other.y);
std::swap(z, other.z);
}
#endif