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]; }
86 for (
auto i :
xrange(2)) r[i] = v1[i] + v2[i];
93 for (
auto i :
xrange(2)) r[i] = v1[i] - v2[i];
100 for (
auto i :
xrange(2)) r[i] = a * v[i];
107 for (
auto i :
xrange(2)) r[i] = v[i] * a;
115 for (
auto i :
xrange(2)) r[i] = v1[i] * v2[i];
122 for (
auto i :
xrange(2)) r[i] = T(1) / v[i];
133 return v * (T(1) / a);
138 return v1 * recip(v2);
144 for (
auto i :
xrange(2)) {
156template<
typename T>
class vecN<3, T>
159 constexpr vecN() : x(T(0)), y(T(0)), z(T(0)) {}
161 template<
typename T2>
164 constexpr vecN(T a, T b, T c) : x(a), y(b), z(c) {}
169 if (i == 0)
return x;
170 if (i == 1)
return y;
171 if (i == 2)
return z;
175 if (i == 0)
return x;
176 if (i == 1)
return y;
177 if (i == 2)
return z;
181 [[nodiscard]]
constexpr const T*
data()
const {
return &x; }
182 [[nodiscard]]
constexpr T*
data() {
return &x; }
184 template<
size_t I> [[nodiscard]]
constexpr T
get() const noexcept {
return (*
this)[I]; }
185 template<
size_t I> [[nodiscard]]
constexpr T&
get() noexcept {
return (*
this)[I]; }
197 for (
auto i :
xrange(3)) r[i] = v1[i] + v2[i];
204 for (
auto i :
xrange(3)) r[i] = v1[i] - v2[i];
211 for (
auto i :
xrange(3)) r[i] = a * v[i];
218 for (
auto i :
xrange(3)) r[i] = v[i] * a;
226 for (
auto i :
xrange(3)) r[i] = v1[i] * v2[i];
233 for (
auto i :
xrange(3)) r[i] = T(1) / v[i];
244 return v * (T(1) / a);
249 return v1 * recip(v2);
255 for (
auto i :
xrange(3)) {
267template<
typename T>
class vecN<4, T>
270 constexpr vecN() : x(T(0)), y(T(0)), z(T(0)), w(T(0)) {}
271 constexpr explicit vecN(T
t) : x(
t), y(
t), z(
t), w(
t) {}
272 template<
typename T2>
273 constexpr explicit vecN(
const vecN<4, T2>& v) : x(T(v.x)), y(T(v.y)), z(T(v.z)), w(T(v.w)) {}
274 constexpr vecN(T a, T b, T c, T d) : x(a), y(b), z(c), w(d) {}
280 if (i == 0)
return x;
281 if (i == 1)
return y;
282 if (i == 2)
return z;
283 if (i == 3)
return w;
287 if (i == 0)
return x;
288 if (i == 1)
return y;
289 if (i == 2)
return z;
290 if (i == 3)
return w;
294 [[nodiscard]]
constexpr const T*
data()
const {
return &x; }
295 [[nodiscard]]
constexpr T*
data() {
return &x; }
297 template<
size_t I> [[nodiscard]]
constexpr T
get() const noexcept {
return (*
this)[I]; }
298 template<
size_t I> [[nodiscard]]
constexpr T&
get() noexcept {
return (*
this)[I]; }
311 for (
auto i :
xrange(4)) r[i] = v1[i] + v2[i];
318 for (
auto i :
xrange(4)) r[i] = v1[i] - v2[i];
325 for (
auto i :
xrange(4)) r[i] = a * v[i];
332 for (
auto i :
xrange(4)) r[i] = v[i] * a;
340 for (
auto i :
xrange(4)) r[i] = v1[i] * v2[i];
347 for (
auto i :
xrange(4)) r[i] = T(1) / v[i];
358 return v * (T(1) / a);
363 return v1 * recip(v2);
369 for (
auto i :
xrange(4)) {
389static_assert(
sizeof(
vec2) == 2 *
sizeof(
float));
390static_assert(
sizeof(
vec3) == 3 *
sizeof(
float));
391static_assert(
sizeof(
vec4) == 4 *
sizeof(
float));
392static_assert(
sizeof(
ivec2) == 2 *
sizeof(
int));
393static_assert(
sizeof(
ivec3) == 3 *
sizeof(
int));
394static_assert(
sizeof(
ivec4) == 4 *
sizeof(
int));
400[[nodiscard]]
inline float rsqrt(
float x)
402 return 1.0f / sqrtf(x);
404[[nodiscard]]
inline double rsqrt(
double x)
406 return 1.0 / sqrt(x);
410template<
typename T> [[nodiscard]]
constexpr T
radians(T d)
414template<
typename T> [[nodiscard]]
constexpr T
degrees(T r)
423template<
int N,
typename T>
430template<
int N,
typename T>
434 for (
auto i :
xrange(N)) r[i] = std::min(x[i], y[i]);
439template<
int N,
typename T>
443 for (
auto i :
xrange(1, N)) r = std::min(r, x[i]);
448template<
int N,
typename T>
452 for (
auto i :
xrange(N)) r[i] = std::max(x[i], y[i]);
457template<
int N,
typename T>
460 return min(maxVal,
max(minVal, x));
464template<
int N,
typename T>
471template<
int N,
typename T>
475 for (
auto i :
xrange(N)) result += x[i];
478template<
int N,
typename T>
485template<
int N,
typename T>
490template<
int N,
typename T>
497template<
int N,
typename T>
504template<
int N,
typename T>
511template<
int N,
typename T>
522 a.z * b.x - a.x * b.z,
523 a.x * b.y - a.y * b.x);
527template<
int N,
typename T>
533 for (
auto i :
xrange(N)) r[i] = narrow_cast<int>(lrintf(narrow_cast<float>(x[i])));
539template<
int N,
typename T>
543 for (
auto i :
xrange(N)) r[i] =
int(x[i]);
554#if defined(__clang__)
555#pragma clang diagnostic push
556#pragma clang diagnostic ignored "-Wmismatched-tags"
558 template<
int N,
typename T>
class tuple_size<
gl::vecN<N, T>>
559 :
public std::integral_constant<size_t, N> {};
560#if defined(__clang__)
561#pragma clang diagnostic pop
563 template<
size_t I,
int N,
typename T>
class tuple_element<I,
gl::vecN<N, T>> {
constexpr friend vecN operator*(const vecN &v, T a)
constexpr T & get() noexcept
constexpr vecN & operator+=(const vecN &v)
constexpr bool operator==(const vecN &) const =default
constexpr T operator[](unsigned i) const
constexpr friend vecN operator/(const vecN &v1, const vecN &v2)
constexpr vecN(const vecN< 2, T2 > &v)
constexpr friend vecN operator*(const vecN &v1, const vecN &v2)
constexpr T & operator[](unsigned i)
constexpr friend vecN operator+(const vecN &v1, const vecN &v2)
constexpr vecN & operator*=(T t)
constexpr const T * data() const
constexpr T get() const noexcept
constexpr vecN & operator-=(const vecN &v)
constexpr friend vecN operator/(T a, const vecN &v)
friend std::ostream & operator<<(std::ostream &os, const vecN &v)
constexpr friend vecN operator/(const vecN &v, T a)
constexpr vecN & operator*=(const vecN &v)
constexpr friend vecN recip(const vecN &v)
constexpr vecN(const vecN< N2, T > &v)
constexpr friend vecN operator*(T a, const vecN &v)
constexpr friend vecN operator-(const vecN &v1, const vecN &v2)
constexpr vecN & operator-=(const vecN &v)
constexpr friend vecN operator*(const vecN &v, T a)
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 friend vecN operator/(const vecN &v1, const vecN &v2)
constexpr friend vecN operator*(const vecN &v1, const vecN &v2)
constexpr bool operator==(const vecN &) const =default
constexpr friend vecN operator+(const vecN &v1, const vecN &v2)
constexpr vecN & operator+=(const vecN &v)
constexpr vecN(T a, T b, T c)
constexpr friend vecN operator/(T a, const vecN &v)
constexpr T operator[](unsigned i) const
friend std::ostream & operator<<(std::ostream &os, const vecN &v)
constexpr T & get() noexcept
constexpr friend vecN operator/(const vecN &v, T a)
constexpr vecN(const vecN< 4, T > &v)
constexpr friend vecN recip(const vecN &v)
constexpr friend vecN operator*(T a, const vecN &v)
constexpr vecN(T a, const vecN< 2, T > &b)
constexpr friend vecN operator-(const vecN &v1, const vecN &v2)
constexpr vecN & operator*=(const vecN &v)
constexpr T get() const noexcept
constexpr const T * data() const
constexpr T & get() noexcept
constexpr friend vecN operator*(const vecN &v, T a)
constexpr vecN & operator*=(T t)
constexpr vecN & operator*=(const vecN &v)
constexpr vecN(const vecN< 4, T2 > &v)
constexpr friend vecN operator/(const vecN &v1, const vecN &v2)
constexpr vecN & operator+=(const vecN &v)
constexpr friend vecN operator*(const vecN &v1, const vecN &v2)
constexpr vecN & operator-=(const vecN &v)
constexpr bool operator==(const vecN &) const =default
constexpr friend vecN operator+(const vecN &v1, const vecN &v2)
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 friend vecN operator/(T a, const vecN &v)
friend std::ostream & operator<<(std::ostream &os, const vecN &v)
constexpr friend vecN operator/(const vecN &v, T a)
constexpr T operator[](unsigned i) const
constexpr friend vecN recip(const vecN &v)
constexpr T get() const noexcept
constexpr vecN(T a, T b, T c, T d)
constexpr friend vecN operator*(T a, const vecN &v)
constexpr vecN(const vecN< 3, T > &a, T b)
constexpr friend vecN operator-(const vecN &v1, const vecN &v2)
constexpr const T * data() const
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-(const matMxN< M, N, T > &A)
T length(const vecN< N, T > &x)
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)
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 auto xrange(T e)