32template<
int N,
typename T>
class vecN;
35template<
typename T>
class vecN<2, T>
41 constexpr vecN() : x(T(0)), y(T(0)) {}
44 constexpr explicit vecN(T
t) : x(
t), y(
t) {}
51 template<
int N2>
constexpr explicit vecN(
const vecN<N2, T>& v) : x(v.x), y(v.y) {}
54 constexpr vecN(T a, T b) : x(a), y(b) {}
68 [[nodiscard]]
constexpr const T*
data()
const {
return &x; }
69 [[nodiscard]]
constexpr T*
data() {
return &x; }
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]; }
88template<
typename T>
class vecN<3, T>
91 constexpr vecN() : x(T(0)), y(T(0)), z(T(0)) {}
92 constexpr explicit vecN(T
t) : x(
t), y(
t), z(
t) {}
94 constexpr explicit vecN(
const vecN<3, T2>& v) : x(T(v.x)), y(T(v.y)), z(T(v.z)) {}
96 constexpr vecN(T a, T b, T c) : x(a), y(b), z(c) {}
101 if (i == 0)
return x;
102 if (i == 1)
return y;
103 if (i == 2)
return z;
107 if (i == 0)
return x;
108 if (i == 1)
return y;
109 if (i == 2)
return z;
113 [[nodiscard]]
constexpr const T*
data()
const {
return &x; }
114 [[nodiscard]]
constexpr T*
data() {
return &x; }
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]; }
131template<
typename T>
class vecN<4, T>
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) {}
144 if (i == 0)
return x;
145 if (i == 1)
return y;
146 if (i == 2)
return z;
147 if (i == 3)
return w;
151 if (i == 0)
return x;
152 if (i == 1)
return y;
153 if (i == 2)
return z;
154 if (i == 3)
return w;
158 [[nodiscard]]
constexpr const T*
data()
const {
return &x; }
159 [[nodiscard]]
constexpr T*
data() {
return &x; }
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]; }
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));
196[[nodiscard]]
inline float rsqrt(
float x)
198 return 1.0f / sqrtf(x);
200[[nodiscard]]
inline double rsqrt(
double x)
202 return 1.0 / sqrt(x);
206template<
typename T> [[nodiscard]]
constexpr T
radians(T d)
210template<
typename T> [[nodiscard]]
constexpr T
degrees(T r)
219template<
int N,
typename T>
226template<
int N,
typename T>
230 for (
auto i :
xrange(N)) r[i] = x[i] + y[i];
235template<
int N,
typename T>
239 for (
auto i :
xrange(N)) r[i] = x[i] - y[i];
244template<
int N,
typename T>
248 for (
auto i :
xrange(N)) r[i] = x * y[i];
253template<
int N,
typename T>
257 for (
auto i :
xrange(N)) r[i] = x[i] * y;
262template<
int N,
typename T>
266 for (
auto i :
xrange(N)) r[i] = x[i] * y[i];
271template<
int N,
typename T>
275 for (
auto i :
xrange(N)) r[i] = T(1) / x[i];
280template<
int N,
typename T>
287template<
int N,
typename T>
290 return x * (T(1) / y);
294template<
int N,
typename T>
301template<
int N,
typename T>
305 for (
auto i :
xrange(N)) r[i] = std::min(x[i], y[i]);
310template<
int N,
typename T>
314 for (
auto i :
xrange(1, N)) r = std::min(r, x[i]);
319template<
int N,
typename T>
323 for (
auto i :
xrange(N)) r[i] = std::max(x[i], y[i]);
328template<
int N,
typename T>
331 return min(maxVal,
max(minVal, x));
335template<
int N,
typename T>
342template<
int N,
typename T>
346 for (
auto i :
xrange(N)) result += x[i];
349template<
int N,
typename T>
356template<
int N,
typename T>
361template<
int N,
typename T>
368template<
int N,
typename T>
375template<
int N,
typename T>
382template<
int N,
typename T>
393 a.z * b.x - a.x * b.z,
394 a.x * b.y - a.y * b.x);
398template<
int N,
typename T>
404 for (
auto i :
xrange(N)) r[i] = narrow_cast<int>(lrintf(narrow_cast<float>(x[i])));
410template<
int N,
typename T>
414 for (
auto i :
xrange(N)) r[i] =
int(x[i]);
419template<
int N,
typename T>
423 for (
auto i :
xrange(N)) {
437#if defined(__clang__)
438#pragma clang diagnostic push
439#pragma clang diagnostic ignored "-Wmismatched-tags"
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
446 template<
size_t I,
int N,
typename T>
class tuple_element<I,
gl::vecN<N, T>> {
constexpr T & get() noexcept
constexpr vecN & operator+=(const vecN &v)
constexpr bool operator==(const vecN &) const =default
constexpr T operator[](unsigned i) const
constexpr vecN(const vecN< 2, T2 > &v)
constexpr T & operator[](unsigned i)
constexpr vecN & operator*=(T t)
constexpr const T * data() const
constexpr T get() const noexcept
constexpr vecN & operator-=(const vecN &v)
constexpr vecN & operator*=(const vecN &v)
constexpr vecN(const vecN< N2, T > &v)
constexpr vecN & operator-=(const vecN &v)
constexpr vecN(const vecN< 2, T > &a, T b)
constexpr vecN & operator*=(T t)
constexpr T & operator[](unsigned i)
constexpr vecN(const vecN< 3, T2 > &v)
constexpr bool operator==(const vecN &) const =default
constexpr vecN & operator+=(const vecN &v)
constexpr vecN(T a, T b, T c)
constexpr T operator[](unsigned i) const
constexpr T & get() noexcept
constexpr vecN(const vecN< 4, T > &v)
constexpr vecN(T a, const vecN< 2, T > &b)
constexpr vecN & operator*=(const vecN &v)
constexpr T get() const noexcept
constexpr const T * data() const
constexpr T & get() noexcept
constexpr vecN & operator*=(T t)
constexpr vecN & operator*=(const vecN &v)
constexpr vecN(const vecN< 4, T2 > &v)
constexpr vecN & operator+=(const vecN &v)
constexpr vecN & operator-=(const vecN &v)
constexpr bool operator==(const vecN &) const =default
constexpr vecN(T a, const vecN< 3, T > &b)
constexpr T & operator[](unsigned i)
constexpr vecN(const vecN< 2, T > &a, const vecN< 2, T > &b)
constexpr T operator[](unsigned i) const
constexpr T get() const noexcept
constexpr vecN(T a, T b, T c, T d)
constexpr vecN(const vecN< 3, T > &a, T b)
constexpr const T * data() const
constexpr vecN< N, T > recip(const vecN< N, T > &x)
constexpr T min_component(const vecN< N, T > &x)
constexpr T dot(const vecN< N, T > &x, const vecN< N, T > &y)
constexpr vecN< 3, T > cross(const vecN< 3, T > &a, const vecN< 3, T > &b)
constexpr vecN< N, int > trunc(const vecN< N, T > &x)
constexpr matMxN< M, N, T > operator*(T x, const matMxN< M, N, T > &A)
T length(const vecN< N, T > &x)
constexpr vecN< N, T > operator/(T x, const vecN< N, T > &y)
vecN< N, int > round(const vecN< N, T > &x)
constexpr T length2(const vecN< N, T > &x)
constexpr vecN< N, T > min(const vecN< N, T > &x, const vecN< N, T > &y)
constexpr vecN< N, T > dot_broadcast(const vecN< N, T > &x, const vecN< N, T > &y)
constexpr vecN< N, T > sum_broadcast(const vecN< N, T > &x)
constexpr T sum(const vecN< N, T > &x)
std::ostream & operator<<(std::ostream &os, const matMxN< M, N, T > &A)
constexpr matMxN< M, N, T > operator-(const matMxN< M, N, T > &A, const matMxN< M, N, T > &B)
vecN< N, T > normalize(const vecN< N, T > &x)
constexpr vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
constexpr vecN< N, T > clamp(const vecN< N, T > &x, const vecN< N, T > &minVal, const vecN< N, T > &maxVal)
constexpr matMxN< M, N, T > operator+(const matMxN< M, N, T > &A, const matMxN< M, N, T > &B)
constexpr auto xrange(T e)