-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec4.h
279 lines (239 loc) · 6.64 KB
/
vec4.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
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
#ifndef __VEC4_H__
#define __VEC4_H__
#include<iostream>
#include<algorithm>
#include<inttypes.h>
#include<cmath>
#include<cassert>
template<class T>
class vec4 {
public:
static constexpr const size_t dim = 4;
vec4(void); ///< default constructor
vec4(const T& a, const T& b, const T& c, const T& d); ///< initialized constructor
vec4(const vec4& other); ///< copy constructor
~vec4(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
vec4& operator= (const vec4& other); ///< assignment operator
template<class D> vec4<D> as(void) const; ///< cast into vec4<D>
T operator*(const vec4<T>& other) const; ///< vector dot product
T sqr_length(void) const; ///< squared length
T length(void) const; ///< length
void normalize(void); ///< normalize this vector
vec4<T> normalized(void) const; ///< return normalized vector
vec4<T> operator+(const vec4<T>& other) const; ///< vector addition
vec4<T> operator-(const vec4<T>& other) const; ///< vector subtraction
vec4<T> operator-(void) const; ///< negation
vec4<T>& operator+=(const vec4<T>& other); ///< cumulative addition
vec4<T>& operator-=(const vec4<T>& other); ///< cumulative subtraction
vec4<T> operator*(const T& val) const; ///< multiplication with rhs scalar
vec4<T> operator/(const T& val) const; ///< division by rhs scalar
vec4<T>& operator*=(const T& val); ///< cumulative multiplication with rhs scalar
vec4<T>& operator/=(const T& val); ///< cumulative division by rhs scalar
void swap(vec4& other); ///< swaps this vector with other
//protected:
union {
struct {
T x, y, z, w;
};
struct {
T r, g, b, a;
};
T m_data[dim];
};
};
using vec4f = vec4<float>;
using vec4d = vec4<double>;
using vec4i = vec4<int32_t>;
template<class T>
vec4<T> operator*(const T& val, vec4<T>& v) { ///< multiplication with lhs scalar
return vec4<T>(val * v.x, val * v.y, val * v.z, val*v.w);
}
template<class T>
vec4<T> lerp(const vec4<T>& A, const vec4<T>& B, const T& alpha) { ///< linear interpolation of vectors
return vec4<T>(
lerp(A.x, B.x, alpha),
lerp(A.y, B.y, alpha),
lerp(A.z, B.z, alpha),
lerp(A.w, B.w, alpha));
}
template<class T>
std::ostream& operator<<(std::ostream& stream, const vec4<T>& v) { ///< debug output of vectors
stream << "[ " << v.x << " " << v.y << " " << v.z << " " << v.w << " ]";
return stream;
}
/// extend std::max and std::min to vectors (component-wise)
/// also, define std::swap
namespace std {
template<class T>
inline vec4<T> max(const vec4<T>& A, const vec4<T>& B) {
return vec4<T>(
std::max(A.x, B.x),
std::max(A.y, B.y),
std::max(A.z, B.z),
std::max(A.w, B.w)
);
}
template<class T>
inline vec4<T> min(const vec4<T>& A, const vec4<T>& B) {
return vec4<T>(
std::min(A.x, B.x),
std::min(A.y, B.y),
std::min(A.z, B.z),
std::min(A.w, B.w)
);
}
};
template<class T>
vec4<T>::vec4(void) : x(m_data[0]), y(m_data[1]), z(m_data[2]), w(m_data[3]) {
m_data[0] = m_data[1] = m_data[2] = m_data[3] = 0.0f;
}
template<class T>
vec4<T>::vec4(const T& a, const T& b, const T& c, const T& d) {
m_data[0] = a;
m_data[1] = b;
m_data[2] = c;
m_data[3] = d;
}
template<class T>
vec4<T>::vec4(const vec4& other) {
*this = other;
}
template<class T>
vec4<T>::~vec4(void) {
}
template<class T>
const T& vec4<T>::operator[](size_t n) const {
assert("vec4[] -- invalid argument" && n < dim);
return m_data[n];
}
template<class T>
T& vec4<T>::operator[](size_t n) {
assert("vec4[] -- invalid argument" && n < dim);
return m_data[n];
}
template<class T>
const T& vec4<T>::operator()(size_t n) const {
assert("vec4() -- invalid argument" && n < dim);
return m_data[n];
}
template<class T>
T& vec4<T>::operator()(size_t n) {
assert("vec4() -- invalid argument" && n < dim);
return m_data[n];
}
template<class T>
vec4<T>::operator const T* (void) const {
return m_data;
}
template<class T>
vec4<T>::operator T* (void) {
return m_data;
}
template<class T>
vec4<T>& vec4<T>::operator= (const vec4& other) {
m_data[0] = other.m_data[0];
m_data[1] = other.m_data[1];
m_data[2] = other.m_data[2];
m_data[3] = other.m_data[3];
return *this;
}
template<class T>
template<class D>
vec4<D> vec4<T>::as(void) const {
return vec4<D>(D(m_data[0]), D(m_data[1]), D(m_data[2]), D(m_data[3]));
}
template<class T>
T vec4<T>::operator*(const vec4<T>& other) const {
return x * other.x + y * other.y + z * other.z * w * other.w;
}
template<class T>
T vec4<T>::sqr_length(void) const {
return x * x + y * y + z * z + w*w;
}
template<class T>
T vec4<T>::length(void) const {
return sqrt(sqr_length());
}
template<class T>
void vec4<T>::normalize(void) {
T ooLen = sqr_length();
if (ooLen == T(0)) return;
ooLen = T(1) / sqrt(ooLen);
x *= ooLen;
y *= ooLen;
z *= ooLen;
w *= ooLen;
}
template<class T>
vec4<T> vec4<T>::normalized(void) const {
T ooLen = sqr_length();
if (ooLen == T(0)) return *this;
ooLen = T(1) / sqrt(ooLen);
return vec4<T>(x * ooLen, y * ooLen, z * ooLen, w * ooLen);
}
template<class T>
vec4<T> vec4<T>::operator+(const vec4<T>& other) const {
return vec4(x + other.x, y + other.y, z + other.z, w + other.w);
}
template<class T>
vec4<T> vec4<T>::operator-(const vec4<T>& other) const {
return vec(x - other.x, y - other.y, z - other.z, w - other.w);
}
template<class T>
vec4<T> vec4<T>::operator-(void) const {
return vec4(-x, -y, -z, -w);
}
template<class T>
vec4<T>& vec4<T>::operator+=(const vec4<T>& other) {
x += other.x;
y += other.y;
z += other.z;
w += other.w;
return *this;
}
template<class T>
vec4<T>& vec4<T>::operator-=(const vec4<T>& other) {
x -= other.x;
y -= other.x;
z -= other.z;
w -= other.w;
return *this;
}
template<class T>
vec4<T> vec4<T>::operator*(const T& val) const {
return vec4<T>(x * val, y * val, z * val, w * val);
}
template<class T>
vec4<T> vec4<T>::operator/(const T& val) const {
return vec4<T>(x / val, y / val, z / val, w / val);
}
template<class T>
vec4<T>& vec4<T>::operator*=(const T& val) {
x *= val;
y *= val;
z *= val;
w *= val;
return *this;
}
template<class T>
vec4<T>& vec4<T>::operator/=(const T& val) {
x /= val;
y /= val;
z /= val;
w /= val;
return *this;
}
template<class T>
void vec4<T>::swap(vec4& other) {
std::swap(x, other.x);
std::swap(y, other.y);
std::swap(z, other.z);
std::swap(w, other.w);
}
#endif