-
Notifications
You must be signed in to change notification settings - Fork 4
/
VectorBase.h
312 lines (274 loc) · 17.8 KB
/
VectorBase.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
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
/**
* GLSL++
*
* Dan Israel Malta
**/
#pragma once
#include "common.h"
#include <array>
#include <algorithm>
#include <execution>
#include <assert.h>
#include <iostream>
#include <string>
namespace GLSLCPP {
/**
* \brief fixed size numerical vector
*
* @param {T, in} underlying type
* @param {N, in} size
**/
template<typename T, std::size_t N> class VectorBase {
static_assert(std::is_arithmetic<T>::value, "VectorBase<T,N> - T must be of numerical type.");
// properties
public:
alignas(std::aligned_storage<sizeof(T), alignof(T)>::type) std::array<T, N> m_data{};
// constructors
public:
// default constructor
VectorBase() : m_data() {}
// construct using a single value
template<typename U> explicit constexpr
VectorBase(const U xi_value = U{}, REQUIRE(is_ArithmeticConvertible_v<U, T>)) {
m_data.fill(static_cast<T>(xi_value));
}
// construct using N individual values (of same type)
template<typename ...Us, REQUIRE((sizeof...(Us) == N) && Are_ArithmeticConvertible<T, Us...>::value)>
explicit constexpr VectorBase(Us... xi_values) : m_data({ static_cast<T>(xi_values)... }) {}
// construct from a swizzle
template<typename U> explicit constexpr VectorBase(U&& s, REQUIRE(is_SwizzleOfLength_v<U, N>)) noexcept {
s.Pack(std::forward<U>(m_data));
}
// construct from any Vector object (copied object size must be equal or larger to constructed one size)
template<typename U> explicit constexpr VectorBase(const U& v, REQUIRE(Is_VectorOfLength_v<U, N>)) {
for_each(m_data, [&, i = 0](auto & elm) mutable {
elm = static_cast<T>(v[i]);
++i;
});
}
template<typename U> explicit constexpr VectorBase(U&& v, REQUIRE(Is_VectorOfLength_v<U, N>)) noexcept {
for_each(m_data, [&, i = 0](auto & elm) mutable {
elm = static_cast<T>(std::move(v[i]));
++i;
});
}
// construct based upon a function whos input argument is element index
constexpr explicit VectorBase(const std::function<T(std::size_t)>& xi_func) {
for_each(m_data, [&, i = 0](auto & elm) mutable {
elm = xi_func(i);
++i;
});
}
constexpr explicit VectorBase(std::function<T(std::size_t)>&& xi_func) noexcept {
for_each(m_data, [&, i = 0](auto & elm) mutable {
elm = xi_func(i);
++i;
});
}
// assign a function whos input argument is element index
VectorBase& operator = (const std::function<T(std::size_t)>& xi_func) {
for_each(m_data, [&, i = 0](auto & elm) mutable {
elm = xi_func(i);
++i;
});
return *this;
}
VectorBase& operator = (std::function<T(std::size_t)>&& xi_func) noexcept {
for_each(m_data, [&, i = 0](auto & elm) mutable {
elm = xi_func(i);
++i;
});
return *this;
}
// copy semantics
VectorBase(const VectorBase&) = default;
VectorBase& operator=(const VectorBase&) = default;
// move semantics
VectorBase(VectorBase&&) noexcept = default;
VectorBase& operator=(VectorBase&&) noexcept = default;
// operator overloading
public:
// '[]' element access
constexpr T operator[](const std::size_t i) const { assert(i < N && " attempting tp access an out-of-region index"); return m_data[i]; }
constexpr T& operator[](const std::size_t i) { assert(i < N && " attempting tp access an out-of-region index"); return m_data[i]; }
// castings
public:
// cast to Vector2
template<typename U, std::size_t M = N, REQUIRE(M >= 2)> operator Vector2<U>() const {
return Vector2<U>(static_cast<U>(m_data[0]), static_cast<U>(m_data[1]));
}
// cast to Vector3
template<typename U, std::size_t M = N, REQUIRE(M >= 3)> operator Vector3<U>() const {
return Vector3<U>(static_cast<U>(m_data[0]), static_cast<U>(m_data[1]), static_cast<U>(m_data[2]));
}
// cast to Vector4
template<typename U, std::size_t M = N, REQUIRE(M >= 4)> operator Vector4<U>() const {
return Vector4<U>(static_cast<U>(m_data[0]), static_cast<U>(m_data[1]),
static_cast<U>(m_data[2]), static_cast<U>(m_data[3]));
}
// iterators
public:
auto begin() noexcept -> decltype(m_data.begin()) { return m_data.begin(); }
auto rbegin() noexcept -> decltype(m_data.rbegin()) { return m_data.rbegin(); }
auto cbegin() noexcept -> decltype(m_data.cbegin()) { return m_data.cbegin(); }
auto crbegin() noexcept -> decltype(m_data.crbegin()) { return m_data.crbegin(); }
auto end() noexcept -> decltype(m_data.end()) { return m_data.end(); }
auto rend() noexcept -> decltype(m_data.rend()) { return m_data.rend(); }
auto cend() noexcept -> decltype(m_data.cend()) { return m_data.cend(); }
auto crend() noexcept -> decltype(m_data.crend()) { return m_data.crend(); }
// assignment operations
public:
// assign an element
template<typename U, REQUIRE(is_ArithmeticConvertible_v<U, T>)> constexpr VectorBase& operator=(const U xi_value) {
for_each(m_data, [this, v = static_cast<T>(xi_value)](auto & elm) {
elm = v;
});
return *this;
}
// assign a swizzle
template<typename U, REQUIRE(is_SwizzleOfLength_v<U, N>)> constexpr VectorBase& operator=(U&& s) {
VectorBase<T, N> temp(s);
m_data = std::move(temp.m_data);
return *this;
}
// query operations
public:
// extract vector size
constexpr std::size_t length() const noexcept { return N; }
// compound operators
public:
#define M_OPERATOR(OP) \
template<typename U, REQUIRE(is_ArithmeticConvertible_v<U, T>)> \
constexpr inline VectorBase& operator OP (const U xi_value) { \
for_each(m_data, [this, xi_value](auto& elm) { \
elm OP static_cast<T>(xi_value); \
}); \
return *this; \
} \
template<typename U> constexpr inline VectorBase& operator OP (const VectorBase<U, N>& xi_vector) { \
for_each(m_data, [this, xi_vector, i = 0](auto& elm) mutable { \
elm OP static_cast<T>(xi_vector[i]); \
++i; \
}); \
return *this; \
} \
template<typename U> constexpr inline VectorBase& operator OP (VectorBase<U, N>&& xi_vector) { \
for_each(m_data, [this, xi_vector, i = 0](auto& elm) mutable { \
elm OP std::move(static_cast<T>(xi_vector[i])); \
++i; \
}); \
return *this; \
} \
template<typename U, REQUIRE(is_SwizzleOfLength_v<U, N>)> constexpr inline VectorBase& operator OP (U s) { \
VectorBase<T, N>&& temp(s); \
for_each(m_data, [this, t = temp, i = 0](auto& elm) mutable { \
elm OP t[i]; \
++i; \
}); \
return *this; \
}
M_OPERATOR(-= );
M_OPERATOR(+= );
M_OPERATOR(*= );
M_OPERATOR(/= );
M_OPERATOR(&= );
M_OPERATOR(|= );
M_OPERATOR(^= );
M_OPERATOR(>>= );
M_OPERATOR(<<= );
#undef M_OPERATOR
// stream operator overloading
public:
// output vector to stream
friend std::ostream& operator<<(std::ostream& xio_stream, VectorBase& xi_vector) {
xio_stream << "{";
if (isVectorLalue(xi_vector)) {
for (std::size_t i{}; i < N - 1; ++i) {
xio_stream << std::to_string(xi_vector[i]) << ", ";
}
xio_stream << xi_vector[N - 1] << "}";
}
else {
for (std::size_t i{}; i < N - 1; ++i) {
xio_stream << std::to_string(std::move(xi_vector[i])) << ", ";
}
xio_stream << std::move(xi_vector[N - 1]) << "}";
}
return xio_stream;
}
// read the space-separated components of a vector from a stream
friend std::istream& operator>>(std::istream & is, VectorBase & xi_vector) {
if (isVectorLalue(xi_vector)) {
for (std::size_t i{}; i < N; ++i) {
is >> xi_vector[i];
}
}
else {
for (std::size_t i{}; i < N; ++i) {
is >> std::move(xi_vector[i]);
}
}
return is;
}
};
#define M_OPERATOR(OP, AOP) \
template<typename T, typename U, REQUIRE(std::is_arithmetic_v<T> && is_Vector_v<U>)> \
constexpr inline U operator OP (U xi_vector, const T xi_scalar) { \
if constexpr (is_VectorBase_v<U>) { \
return (xi_vector AOP xi_scalar); \
} else { \
return (xi_vector.m_data AOP xi_scalar); \
} \
} \
template<typename T, typename U, REQUIRE(std::is_arithmetic_v<T> && is_Vector_v<U>)> \
constexpr inline U operator OP (const T xi_scalar, U xi_vector) { \
if constexpr (is_VectorBase_v<U>) { \
return (xi_vector AOP xi_scalar); \
} else { \
return (xi_vector.m_data AOP xi_scalar); \
} \
} \
template<typename T, typename U, REQUIRE(is_Vector_v<U> && is_Vector_v<T> && (Length_v<U> == Length_v<T>))> \
constexpr inline U operator OP (U xi_lhs, const T xi_rhs) { \
constexpr bool is_lhs_vectorbase{ is_VectorBase_v<U> }, \
is_rhs_vectorbase{ is_VectorBase_v<T> }; \
\
if constexpr (is_lhs_vectorbase && is_rhs_vectorbase) { \
return (xi_lhs AOP xi_rhs); \
} else if constexpr (is_lhs_vectorbase && !is_rhs_vectorbase) { \
return (xi_lhs AOP xi_rhs.m_data); \
} else if constexpr (!is_lhs_vectorbase && is_rhs_vectorbase) { \
return (xi_lhs.m_data AOP xi_rhs); \
} else { \
return (xi_lhs.m_data AOP xi_rhs.m_data); \
} \
} \
template<typename T, typename U, REQUIRE(is_Vector_v<T> && is_Swizzle_v<U> && (Length_v<T> == Length_v<U>))> \
constexpr inline U operator OP (T xi_vector, const U& xi_swiz) { \
if constexpr (is_VectorBase_v<T>) { \
return (xi_vector AOP xi_swiz); \
} \
else { \
return (xi_vector.m_data AOP xi_swiz); \
} \
} \
template<typename T, typename U, REQUIRE(is_Vector_v<T> && is_Swizzle_v<U> && (Length_v<T> == Length_v<U>))> \
constexpr inline U operator OP (U& xi_swiz, T xi_vector) { \
if constexpr (is_VectorBase_v<T>) { \
return (xi_vector AOP xi_swiz); \
} \
else { \
return (xi_vector.m_data AOP xi_swiz); \
} \
}
M_OPERATOR(+, +=);
M_OPERATOR(-, -=);
M_OPERATOR(*, *=);
M_OPERATOR(/ , /=);
M_OPERATOR(&, &=);
M_OPERATOR(| , |=);
M_OPERATOR(^, ^=);
M_OPERATOR(>> , >>=);
M_OPERATOR(<< , <<=);
#undef M_OPERATOR
}; // namespace GLSLCPP