openMSX
gl_vec.hh
Go to the documentation of this file.
1#ifndef GL_VEC_HH
2#define GL_VEC_HH
3
4// This code implements a mathematical vector, comparable in functionality
5// and syntax to the vector types in GLSL.
6//
7// Only vector sizes 2, 3 and 4 are supported. Though when it doesn't
8// complicate stuff the code was written to support any size.
9//
10// Most basic functionality is already there, but this is not meant to be a
11// full set of GLSL functions. We can always extend the functionality if/when
12// required.
13//
14// In the past we had (some) manual SSE optimizations in this code. Though for
15// the functions that matter (matrix-vector and matrix-matrix multiplication are
16// built on top of the functions in this file), the compiler's
17// auto-vectorization has become as good as the manually vectorized code.
18
19#include "Math.hh"
20#include "narrow.hh"
21#include "unreachable.hh"
22#include "xrange.hh"
23#include <algorithm>
24#include <array>
25#include <cmath>
26#include <iostream>
27#include <utility>
28
29namespace gl {
30
31// Vector with N components of type T.
32template<int N, typename T> class vecN;
33
34// Specialization for N=2.
35template<typename T> class vecN<2, T>
36{
37public:
38 // Default copy-constructor and assignment operator.
39
40 // Construct vector containing all zeros.
41 constexpr vecN() : x(T(0)), y(T(0)) {}
42
43 // Construct vector containing the same value repeated N times.
44 constexpr explicit vecN(T t) : x(t), y(t) {}
45
46 // Conversion constructor from vector of same size but different type.
47 template<typename T2>
48 constexpr explicit vecN(const vecN<2, T2>& v) : x(T(v.x)), y(T(v.y)) {}
49
50 // Construct from larger vector (higher order elements are dropped).
51 template<int N2> constexpr explicit vecN(const vecN<N2, T>& v) : x(v.x), y(v.y) {}
52
53 // Construct vector from 2 given values.
54 constexpr vecN(T a, T b) : x(a), y(b) {}
55
56 // Access the i-th element of this vector.
57 [[nodiscard]] constexpr T operator[](unsigned i) const {
58 if (i == 0) return x;
59 if (i == 1) return y;
61 }
62 [[nodiscard]] constexpr T& operator[](unsigned i) {
63 if (i == 0) return x;
64 if (i == 1) return y;
66 }
67
68 [[nodiscard]] constexpr const T* data() const { return &x; }
69 [[nodiscard]] constexpr T* data() { return &x; }
70
71 // For structured bindings
72 template<size_t I> [[nodiscard]] constexpr T get() const noexcept { return (*this)[I]; }
73 template<size_t I> [[nodiscard]] constexpr T& get() noexcept { return (*this)[I]; }
74
75 // Assignment version of the +,-,* operations defined below.
76 constexpr vecN& operator+=(const vecN& v) { *this = *this + v; return *this; }
77 constexpr vecN& operator-=(const vecN& v) { *this = *this - v; return *this; }
78 constexpr vecN& operator*=(const vecN& v) { *this = *this * v; return *this; }
79 constexpr vecN& operator*=(T t) { *this = *this * t; return *this; }
80
81 [[nodiscard]] constexpr bool operator==(const vecN&) const = default;
82
83public:
84 T x, y;
85};
86
87// Specialization for N=3.
88template<typename T> class vecN<3, T>
89{
90public:
91 constexpr vecN() : x(T(0)), y(T(0)), z(T(0)) {}
92 constexpr explicit vecN(T t) : x(t), y(t), z(t) {}
93 template<typename T2>
94 constexpr explicit vecN(const vecN<3, T2>& v) : x(T(v.x)), y(T(v.y)), z(T(v.z)) {}
95 constexpr explicit vecN(const vecN<4, T>& v) : x(v.x), y(v.y), z(v.z) {}
96 constexpr vecN(T a, T b, T c) : x(a), y(b), z(c) {}
97 constexpr vecN(T a, const vecN<2, T>& b) : x(a), y(b.x), z(b.y) {}
98 constexpr vecN(const vecN<2, T>& a, T b) : x(a.x), y(a.y), z(b) {}
99
100 [[nodiscard]] constexpr T operator[](unsigned i) const {
101 if (i == 0) return x;
102 if (i == 1) return y;
103 if (i == 2) return z;
105 }
106 [[nodiscard]] constexpr T& operator[](unsigned i) {
107 if (i == 0) return x;
108 if (i == 1) return y;
109 if (i == 2) return z;
111 }
112
113 [[nodiscard]] constexpr const T* data() const { return &x; }
114 [[nodiscard]] constexpr T* data() { return &x; }
115
116 template<size_t I> [[nodiscard]] constexpr T get() const noexcept { return (*this)[I]; }
117 template<size_t I> [[nodiscard]] constexpr T& get() noexcept { return (*this)[I]; }
118
119 constexpr vecN& operator+=(const vecN& v) { *this = *this + v; return *this; }
120 constexpr vecN& operator-=(const vecN& v) { *this = *this - v; return *this; }
121 constexpr vecN& operator*=(const vecN& v) { *this = *this * v; return *this; }
122 constexpr vecN& operator*=(T t) { *this = *this * t; return *this; }
123
124 [[nodiscard]] constexpr bool operator==(const vecN&) const = default;
125
126public:
127 T x, y, z;
128};
129
130// Specialization for N=4.
131template<typename T> class vecN<4, T>
132{
133public:
134 constexpr vecN() : x(T(0)), y(T(0)), z(T(0)), w(T(0)) {}
135 constexpr explicit vecN(T t) : x(t), y(t), z(t), w(t) {}
136 template<typename T2>
137 constexpr explicit vecN(const vecN<4, T2>& v) : x(T(v.x)), y(T(v.y)), z(T(v.z)), w(T(v.w)) {}
138 constexpr vecN(T a, T b, T c, T d) : x(a), y(b), z(c), w(d) {}
139 constexpr vecN(T a, const vecN<3, T>& b) : x(a), y(b.x), z(b.y), w(b.z) {}
140 constexpr vecN(const vecN<3, T>& a, T b) : x(a.x), y(a.y), z(a.z), w(b) {}
141 constexpr vecN(const vecN<2, T>& a, const vecN<2, T>& b) : x(a.x), y(a.y), z(b.x), w(b.y) {}
142
143 [[nodiscard]] constexpr T operator[](unsigned i) const {
144 if (i == 0) return x;
145 if (i == 1) return y;
146 if (i == 2) return z;
147 if (i == 3) return w;
149 }
150 [[nodiscard]] constexpr T& operator[](unsigned i) {
151 if (i == 0) return x;
152 if (i == 1) return y;
153 if (i == 2) return z;
154 if (i == 3) return w;
156 }
157
158 [[nodiscard]] constexpr const T* data() const { return &x; }
159 [[nodiscard]] constexpr T* data() { return &x; }
160
161 template<size_t I> [[nodiscard]] constexpr T get() const noexcept { return (*this)[I]; }
162 template<size_t I> [[nodiscard]] constexpr T& get() noexcept { return (*this)[I]; }
163
164 // Assignment version of the +,-,* operations defined below.
165 constexpr vecN& operator+=(const vecN& v) { *this = *this + v; return *this; }
166 constexpr vecN& operator-=(const vecN& v) { *this = *this - v; return *this; }
167 constexpr vecN& operator*=(const vecN& v) { *this = *this * v; return *this; }
168 constexpr vecN& operator*=(T t) { *this = *this * t; return *this; }
169
170 [[nodiscard]] constexpr bool operator==(const vecN&) const = default;
171
172public:
173 T x, y, z, w;
174};
175
176
177// Convenience typedefs (same names as used by GLSL).
184
185static_assert(sizeof( vec2) == 2 * sizeof(float));
186static_assert(sizeof( vec3) == 3 * sizeof(float));
187static_assert(sizeof( vec4) == 4 * sizeof(float));
188static_assert(sizeof(ivec2) == 2 * sizeof(int));
189static_assert(sizeof(ivec3) == 3 * sizeof(int));
190static_assert(sizeof(ivec4) == 4 * sizeof(int));
191
192
193// -- Scalar functions --
194
195// reciprocal square root
196[[nodiscard]] inline float rsqrt(float x)
197{
198 return 1.0f / sqrtf(x);
199}
200[[nodiscard]] inline double rsqrt(double x)
201{
202 return 1.0 / sqrt(x);
203}
204
205// convert radians <-> degrees
206template<typename T> [[nodiscard]] constexpr T radians(T d)
207{
208 return d * T(Math::pi / 180.0);
209}
210template<typename T> [[nodiscard]] constexpr T degrees(T r)
211{
212 return r * T(180.0 / Math::pi);
213}
214
215
216// -- Vector functions --
217
218// vector negation
219template<int N, typename T>
220[[nodiscard]] constexpr vecN<N, T> operator-(const vecN<N, T>& x)
221{
222 return vecN<N, T>() - x;
223}
224
225// vector + vector
226template<int N, typename T>
227[[nodiscard]] constexpr vecN<N, T> operator+(const vecN<N, T>& x, const vecN<N, T>& y)
228{
229 vecN<N, T> r;
230 for (auto i : xrange(N)) r[i] = x[i] + y[i];
231 return r;
232}
233
234// vector - vector
235template<int N, typename T>
236[[nodiscard]] constexpr vecN<N, T> operator-(const vecN<N, T>& x, const vecN<N, T>& y)
237{
238 vecN<N, T> r;
239 for (auto i : xrange(N)) r[i] = x[i] - y[i];
240 return r;
241}
242
243// scalar * vector
244template<int N, typename T>
245[[nodiscard]] constexpr vecN<N, T> operator*(T x, const vecN<N, T>& y)
246{
247 vecN<N, T> r;
248 for (auto i : xrange(N)) r[i] = x * y[i];
249 return r;
250}
251
252// vector * scalar
253template<int N, typename T>
254[[nodiscard]] constexpr vecN<N, T> operator*(const vecN<N, T>& x, T y)
255{
256 vecN<N, T> r;
257 for (auto i : xrange(N)) r[i] = x[i] * y;
258 return r;
259}
260
261// vector * vector
262template<int N, typename T>
263[[nodiscard]] constexpr vecN<N, T> operator*(const vecN<N, T>& x, const vecN<N, T>& y)
264{
265 vecN<N, T> r;
266 for (auto i : xrange(N)) r[i] = x[i] * y[i];
267 return r;
268}
269
270// element-wise reciprocal
271template<int N, typename T>
272[[nodiscard]] constexpr vecN<N, T> recip(const vecN<N, T>& x)
273{
274 vecN<N, T> r;
275 for (auto i : xrange(N)) r[i] = T(1) / x[i];
276 return r;
277}
278
279// scalar / vector
280template<int N, typename T>
281[[nodiscard]] constexpr vecN<N, T> operator/(T x, const vecN<N, T>& y)
282{
283 return x * recip(y);
284}
285
286// vector / scalar
287template<int N, typename T>
288[[nodiscard]] constexpr vecN<N, T> operator/(const vecN<N, T>& x, T y)
289{
290 return x * (T(1) / y);
291}
292
293// vector / vector
294template<int N, typename T>
295[[nodiscard]] constexpr vecN<N, T> operator/(const vecN<N, T>& x, const vecN<N, T>& y)
296{
297 return x * recip(y);
298}
299
300// min(vector, vector)
301template<int N, typename T>
302[[nodiscard]] constexpr vecN<N, T> min(const vecN<N, T>& x, const vecN<N, T>& y)
303{
304 vecN<N, T> r;
305 for (auto i : xrange(N)) r[i] = std::min(x[i], y[i]);
306 return r;
307}
308
309// min(vector, vector)
310template<int N, typename T>
311[[nodiscard]] constexpr T min_component(const vecN<N, T>& x)
312{
313 T r = x[0];
314 for (auto i : xrange(1, N)) r = std::min(r, x[i]);
315 return r;
316}
317
318// max(vector, vector)
319template<int N, typename T>
320[[nodiscard]] constexpr vecN<N, T> max(const vecN<N, T>& x, const vecN<N, T>& y)
321{
322 vecN<N, T> r;
323 for (auto i : xrange(N)) r[i] = std::max(x[i], y[i]);
324 return r;
325}
326
327// clamp(vector, vector, vector)
328template<int N, typename T>
329[[nodiscard]] constexpr vecN<N, T> clamp(const vecN<N, T>& x, const vecN<N, T>& minVal, const vecN<N, T>& maxVal)
330{
331 return min(maxVal, max(minVal, x));
332}
333
334// clamp(vector, scalar, scalar)
335template<int N, typename T>
336[[nodiscard]] constexpr vecN<N, T> clamp(const vecN<N, T>& x, T minVal, T maxVal)
337{
338 return clamp(x, vecN<N, T>(minVal), vecN<N, T>(maxVal));
339}
340
341// sum of components
342template<int N, typename T>
343[[nodiscard]] constexpr T sum(const vecN<N, T>& x)
344{
345 T result(0);
346 for (auto i : xrange(N)) result += x[i];
347 return result;
348}
349template<int N, typename T>
350[[nodiscard]] constexpr vecN<N, T> sum_broadcast(const vecN<N, T>& x)
351{
352 return vecN<N, T>(sum(x));
353}
354
355// dot product
356template<int N, typename T>
357[[nodiscard]] constexpr T dot(const vecN<N, T>& x, const vecN<N, T>& y)
358{
359 return sum(x * y);
360}
361template<int N, typename T>
362[[nodiscard]] constexpr vecN<N, T> dot_broadcast(const vecN<N, T>& x, const vecN<N, T>& y)
363{
364 return sum_broadcast(x * y);
365}
366
367// squared length (norm-2)
368template<int N, typename T>
369[[nodiscard]] constexpr T length2(const vecN<N, T>& x)
370{
371 return dot(x, x);
372}
373
374// length (norm-2)
375template<int N, typename T>
376[[nodiscard]] inline T length(const vecN<N, T>& x)
377{
378 return std::sqrt(length2(x));
379}
380
381// normalize vector
382template<int N, typename T>
383[[nodiscard]] inline vecN<N, T> normalize(const vecN<N, T>& x)
384{
385 return x * rsqrt(length2(x));
386}
387
388// cross product (only defined for vectors of length 3)
389template<typename T>
390[[nodiscard]] constexpr vecN<3, T> cross(const vecN<3, T>& a, const vecN<3, T>& b)
391{
392 return vecN<3, T>(a.y * b.z - a.z * b.y,
393 a.z * b.x - a.x * b.z,
394 a.x * b.y - a.y * b.x);
395}
396
397// round each component to the nearest integer (returns a vector of integers)
398template<int N, typename T>
399[[nodiscard]] inline vecN<N, int> round(const vecN<N, T>& x)
400{
401 vecN<N, int> r;
402 // note: std::lrint() is more generic (e.g. also works with double),
403 // but Dingux doesn't seem to have std::lrint().
404 for (auto i : xrange(N)) r[i] = narrow_cast<int>(lrintf(narrow_cast<float>(x[i])));
405 return r;
406}
407
408// truncate each component to the nearest integer that is not bigger in
409// absolute value (returns a vector of integers)
410template<int N, typename T>
411[[nodiscard]] constexpr vecN<N, int> trunc(const vecN<N, T>& x)
412{
413 vecN<N, int> r;
414 for (auto i : xrange(N)) r[i] = int(x[i]);
415 return r;
416}
417
418// Textual representation. (Only) used to debug unittest.
419template<int N, typename T>
420std::ostream& operator<<(std::ostream& os, const vecN<N, T>& x)
421{
422 os << "[ ";
423 for (auto i : xrange(N)) {
424 os << x[i] << ' ';
425 }
426 os << ']';
427 return os;
428}
429
430} // namespace gl
431
432// Support for structured bindings
433namespace std {
434// On some platforms tuple_size is a class and on others it is a struct.
435// Such a mismatch is only a problem when targeting the Microsoft C++ ABI,
436// which we don't do when compiling with Clang.
437#if defined(__clang__)
438#pragma clang diagnostic push
439#pragma clang diagnostic ignored "-Wmismatched-tags"
440#endif
441 template<int N, typename T> class tuple_size<gl::vecN<N, T>>
442 : public std::integral_constant<size_t, N> {};
443#if defined(__clang__)
444#pragma clang diagnostic pop
445#endif
446 template<size_t I, int N, typename T> class tuple_element<I, gl::vecN<N, T>> {
447 public:
448 using type = T;
449 };
450}
451
452#endif // GL_VEC_HH
TclObject t
constexpr T & get() noexcept
Definition gl_vec.hh:73
constexpr vecN & operator+=(const vecN &v)
Definition gl_vec.hh:76
constexpr bool operator==(const vecN &) const =default
constexpr T operator[](unsigned i) const
Definition gl_vec.hh:57
constexpr vecN(const vecN< 2, T2 > &v)
Definition gl_vec.hh:48
constexpr T & operator[](unsigned i)
Definition gl_vec.hh:62
constexpr vecN(T a, T b)
Definition gl_vec.hh:54
constexpr T * data()
Definition gl_vec.hh:69
constexpr vecN & operator*=(T t)
Definition gl_vec.hh:79
constexpr const T * data() const
Definition gl_vec.hh:68
constexpr T get() const noexcept
Definition gl_vec.hh:72
constexpr vecN & operator-=(const vecN &v)
Definition gl_vec.hh:77
constexpr vecN()
Definition gl_vec.hh:41
constexpr vecN & operator*=(const vecN &v)
Definition gl_vec.hh:78
constexpr vecN(T t)
Definition gl_vec.hh:44
constexpr vecN(const vecN< N2, T > &v)
Definition gl_vec.hh:51
constexpr vecN & operator-=(const vecN &v)
Definition gl_vec.hh:120
constexpr vecN(const vecN< 2, T > &a, T b)
Definition gl_vec.hh:98
constexpr vecN & operator*=(T t)
Definition gl_vec.hh:122
constexpr T & operator[](unsigned i)
Definition gl_vec.hh:106
constexpr vecN(const vecN< 3, T2 > &v)
Definition gl_vec.hh:94
constexpr bool operator==(const vecN &) const =default
constexpr vecN & operator+=(const vecN &v)
Definition gl_vec.hh:119
constexpr vecN(T a, T b, T c)
Definition gl_vec.hh:96
constexpr T operator[](unsigned i) const
Definition gl_vec.hh:100
constexpr vecN()
Definition gl_vec.hh:91
constexpr T & get() noexcept
Definition gl_vec.hh:117
constexpr vecN(T t)
Definition gl_vec.hh:92
constexpr vecN(const vecN< 4, T > &v)
Definition gl_vec.hh:95
constexpr vecN(T a, const vecN< 2, T > &b)
Definition gl_vec.hh:97
constexpr T * data()
Definition gl_vec.hh:114
constexpr vecN & operator*=(const vecN &v)
Definition gl_vec.hh:121
constexpr T get() const noexcept
Definition gl_vec.hh:116
constexpr const T * data() const
Definition gl_vec.hh:113
constexpr T & get() noexcept
Definition gl_vec.hh:162
constexpr vecN()
Definition gl_vec.hh:134
constexpr vecN & operator*=(T t)
Definition gl_vec.hh:168
constexpr vecN & operator*=(const vecN &v)
Definition gl_vec.hh:167
constexpr vecN(const vecN< 4, T2 > &v)
Definition gl_vec.hh:137
constexpr vecN & operator+=(const vecN &v)
Definition gl_vec.hh:165
constexpr vecN & operator-=(const vecN &v)
Definition gl_vec.hh:166
constexpr bool operator==(const vecN &) const =default
constexpr vecN(T a, const vecN< 3, T > &b)
Definition gl_vec.hh:139
constexpr T & operator[](unsigned i)
Definition gl_vec.hh:150
constexpr T * data()
Definition gl_vec.hh:159
constexpr vecN(const vecN< 2, T > &a, const vecN< 2, T > &b)
Definition gl_vec.hh:141
constexpr vecN(T t)
Definition gl_vec.hh:135
constexpr T operator[](unsigned i) const
Definition gl_vec.hh:143
constexpr T get() const noexcept
Definition gl_vec.hh:161
constexpr vecN(T a, T b, T c, T d)
Definition gl_vec.hh:138
constexpr vecN(const vecN< 3, T > &a, T b)
Definition gl_vec.hh:140
constexpr const T * data() const
Definition gl_vec.hh:158
constexpr double pi
Definition Math.hh:24
Definition gl_mat.hh:23
constexpr vecN< N, T > recip(const vecN< N, T > &x)
Definition gl_vec.hh:272
vecN< 3, float > vec3
Definition gl_vec.hh:179
vecN< 3, int > ivec3
Definition gl_vec.hh:182
constexpr T min_component(const vecN< N, T > &x)
Definition gl_vec.hh:311
constexpr T dot(const vecN< N, T > &x, const vecN< N, T > &y)
Definition gl_vec.hh:357
vecN< 2, int > ivec2
Definition gl_vec.hh:181
constexpr vecN< 3, T > cross(const vecN< 3, T > &a, const vecN< 3, T > &b)
Definition gl_vec.hh:390
constexpr vecN< N, int > trunc(const vecN< N, T > &x)
Definition gl_vec.hh:411
vecN< 2, float > vec2
Definition gl_vec.hh:178
constexpr matMxN< M, N, T > operator*(T x, const matMxN< M, N, T > &A)
Definition gl_mat.hh:159
vecN< 4, int > ivec4
Definition gl_vec.hh:183
vecN< 4, float > vec4
Definition gl_vec.hh:180
T length(const vecN< N, T > &x)
Definition gl_vec.hh:376
constexpr vecN< N, T > operator/(T x, const vecN< N, T > &y)
Definition gl_vec.hh:281
vecN< N, int > round(const vecN< N, T > &x)
Definition gl_vec.hh:399
constexpr T length2(const vecN< N, T > &x)
Definition gl_vec.hh:369
constexpr vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
Definition gl_vec.hh:302
constexpr vecN< N, T > dot_broadcast(const vecN< N, T > &x, const vecN< N, T > &y)
Definition gl_vec.hh:362
constexpr T degrees(T r)
Definition gl_vec.hh:210
constexpr vecN< N, T > sum_broadcast(const vecN< N, T > &x)
Definition gl_vec.hh:350
constexpr T sum(const vecN< N, T > &x)
Definition gl_vec.hh:343
std::ostream & operator<<(std::ostream &os, const matMxN< M, N, T > &A)
Definition gl_mat.hh:334
constexpr T radians(T d)
Definition gl_vec.hh:206
constexpr matMxN< M, N, T > operator-(const matMxN< M, N, T > &A, const matMxN< M, N, T > &B)
Definition gl_mat.hh:143
vecN< N, T > normalize(const vecN< N, T > &x)
Definition gl_vec.hh:383
constexpr vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition gl_vec.hh:320
float rsqrt(float x)
Definition gl_vec.hh:196
constexpr vecN< N, T > clamp(const vecN< N, T > &x, const vecN< N, T > &minVal, const vecN< N, T > &maxVal)
Definition gl_vec.hh:329
constexpr matMxN< M, N, T > operator+(const matMxN< M, N, T > &A, const matMxN< M, N, T > &B)
Definition gl_mat.hh:134
STL namespace.
#define UNREACHABLE
constexpr auto xrange(T e)
Definition xrange.hh:132