diff options
| author | lizzie <lizzie@eden-emu.dev> | 2026-09-08 20:39:39 +0200 |
|---|---|---|
| committer | crueter <crueter@eden-emu.dev> | 2026-09-08 20:39:39 +0200 |
| commit | fdd8d4252c60b055cac2d792b414ba15046d484d (patch) | |
| tree | c78cbafdb916f4936873d733c9c37abbc9a03962 /src/common/vector_math.h | |
| parent | 1036982d2f70af9d72e9827ffab73d4349508bbb (diff) | |
[common] remove unused vector_math.h fluff (#4333)
Signed-off-by: lizzie <lizzie@eden-emu.dev>
- [x] I have read and followed the [Contribution Guidelines](https://git.eden-emu.dev/eden-emu/eden/src/branch/master/CONTRIBUTING.md#code-contributions).
- [x] I have read and followed the [AI Policy](https://git.eden-emu.dev/eden-emu/eden/src/branch/master/docs/policies/AI.md)
- [x] I have read and followed the [Coding Guidelines](https://git.eden-emu.dev/eden-emu/eden/src/branch/master/docs/policies/Coding.md) to the best of my ability.
-------------------
We don't use half of this file for anything -- it's dead code
also the NEON intrinsic replacements suck -- we are not beating GLM (plus we use registers for decomposition instead of using vec regs like everyone else does, so the "optimizations" dont matter)
Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/4333
Reviewed-by: CamilleLaVey <camillelavey99@gmail.com>
Reviewed-by: Maufeat <sahyno1996@gmail.com>
Diffstat (limited to 'src/common/vector_math.h')
| -rw-r--r-- | src/common/vector_math.h | 800 |
1 files changed, 88 insertions, 712 deletions
diff --git a/src/common/vector_math.h b/src/common/vector_math.h index 829a79ce5a..f99fdcb01e 100644 --- a/src/common/vector_math.h +++ b/src/common/vector_math.h @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project +// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project // SPDX-License-Identifier: GPL-3.0-or-later // SPDX-FileCopyrightText: 2014 Tony Wasserka @@ -7,752 +7,128 @@ #pragma once -#ifdef __ARM_NEON -#include <arm_neon.h> -#endif - #include <cmath> #include <type_traits> namespace Common { -template <typename T> -class Vec2; -template <typename T> -class Vec3; -template <typename T> -class Vec4; - -template <typename T> -class Vec2 { -public: - T x{}; - T y{}; - - constexpr Vec2() = default; - constexpr Vec2(const T& x_, const T& y_) : x(x_), y(y_) {} - - template <typename T2> - [[nodiscard]] constexpr Vec2<T2> Cast() const { - return Vec2<T2>(static_cast<T2>(x), static_cast<T2>(y)); - } - - [[nodiscard]] static constexpr Vec2 AssignToAll(const T& f) { - return Vec2{f, f}; - } - - [[nodiscard]] constexpr Vec2<decltype(T{} + T{})> operator+(const Vec2& other) const { - return {x + other.x, y + other.y}; - } - constexpr Vec2& operator+=(const Vec2& other) { - x += other.x; - y += other.y; - return *this; - } - [[nodiscard]] constexpr Vec2<decltype(T{} - T{})> operator-(const Vec2& other) const { - return {x - other.x, y - other.y}; - } - constexpr Vec2& operator-=(const Vec2& other) { - x -= other.x; - y -= other.y; - return *this; - } - - template <typename U = T> - [[nodiscard]] constexpr Vec2<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const { - return {-x, -y}; - } - [[nodiscard]] constexpr Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const { - return {x * other.x, y * other.y}; - } - - template <typename V> - [[nodiscard]] constexpr Vec2<decltype(T{} * V{})> operator*(const V& f) const { - using TV = decltype(T{} * V{}); - using C = std::common_type_t<T, V>; - - return { - static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)), - static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)), - }; - } - - template <typename V> - constexpr Vec2& operator*=(const V& f) { - *this = *this * f; - return *this; - } - - template <typename V> - [[nodiscard]] constexpr Vec2<decltype(T{} / V{})> operator/(const V& f) const { - using TV = decltype(T{} / V{}); - using C = std::common_type_t<T, V>; - - return { - static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)), - static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)), - }; - } - - template <typename V> - constexpr Vec2& operator/=(const V& f) { - *this = *this / f; - return *this; - } - - [[nodiscard]] constexpr T Length2() const { - return x * x + y * y; - } - - // Only implemented for T=float - [[nodiscard]] float Length() const; - [[nodiscard]] float Normalize(); // returns the previous length, which is often useful - - [[nodiscard]] constexpr T& operator[](std::size_t i) { - return *((&x) + i); - } - [[nodiscard]] constexpr const T& operator[](std::size_t i) const { - return *((&x) + i); - } - - constexpr void SetZero() { - x = 0; - y = 0; - } - - // Common aliases: UV (texel coordinates), ST (texture coordinates) - [[nodiscard]] constexpr T& u() { - return x; - } - [[nodiscard]] constexpr T& v() { - return y; - } - [[nodiscard]] constexpr T& s() { - return x; - } - [[nodiscard]] constexpr T& t() { - return y; - } - - [[nodiscard]] constexpr const T& u() const { - return x; - } - [[nodiscard]] constexpr const T& v() const { - return y; - } - [[nodiscard]] constexpr const T& s() const { - return x; - } - [[nodiscard]] constexpr const T& t() const { - return y; - } - - // swizzlers - create a subvector of specific components - [[nodiscard]] constexpr Vec2 yx() const { - return Vec2(y, x); - } - [[nodiscard]] constexpr Vec2 vu() const { - return Vec2(y, x); - } - [[nodiscard]] constexpr Vec2 ts() const { - return Vec2(y, x); - } -}; - -template <typename T, typename V> -[[nodiscard]] constexpr Vec2<T> operator*(const V& f, const Vec2<T>& vec) { - using C = std::common_type_t<T, V>; - - return Vec2<T>(static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.x)), - static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.y))); -} - -using Vec2f = Vec2<float>; - -template <> -inline float Vec2<float>::Length() const { - return std::sqrt(x * x + y * y); -} - -template <> -inline float Vec2<float>::Normalize() { - float length = Length(); - *this /= length; - return length; -} - -template <typename T> -class Vec3 { +template <typename T, size_t N> +class Vec { public: - T x{}; - T y{}; - T z{}; + std::array<T, N> elems{}; - constexpr Vec3() = default; - constexpr Vec3(const T& x_, const T& y_, const T& z_) : x(x_), y(y_), z(z_) {} - - template <typename T2> - [[nodiscard]] constexpr Vec3<T2> Cast() const { - return Vec3<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z)); - } - - [[nodiscard]] static constexpr Vec3 AssignToAll(const T& f) { - return Vec3(f, f, f); - } + constexpr Vec() = default; + constexpr Vec(T e0) noexcept : elems{e0} {} + constexpr Vec(T e0, T e1) noexcept : elems{e0, e1} {} + constexpr Vec(T e0, T e1, T e2) noexcept : elems{e0, e1, e2} {} + constexpr Vec(T e0, T e1, T e2, T e4) noexcept : elems{e0, e1, e2, e4} {} + //explicit constexpr Vec(const std::initializer_list<T> elems_) noexcept : elems{elems_} {} - [[nodiscard]] constexpr Vec3<decltype(T{} + T{})> operator+(const Vec3& other) const { - return {x + other.x, y + other.y, z + other.z}; + [[nodiscard]] constexpr Vec<decltype(T{} + T{}), N> operator+(const Vec o) const noexcept { + Vec<decltype(T{} + T{}), N> r{}; + for (size_t i = 0; i < N; ++i) + r.elems[i] = elems[i] + o.elems[i]; + return r; } + constexpr Vec<T, N> operator+=(const Vec<T, N> o) noexcept { return *this = *this + o; } - constexpr Vec3& operator+=(const Vec3& other) { - x += other.x; - y += other.y; - z += other.z; - return *this; - } - - [[nodiscard]] constexpr Vec3<decltype(T{} - T{})> operator-(const Vec3& other) const { - return {x - other.x, y - other.y, z - other.z}; - } - - constexpr Vec3& operator-=(const Vec3& other) { - x -= other.x; - y -= other.y; - z -= other.z; - return *this; + [[nodiscard]] constexpr Vec<decltype(T{} - T{}), N> operator-(const Vec o) const noexcept { + Vec<decltype(T{} - T{}), N> r{}; + for (size_t i = 0; i < N; ++i) + r.elems[i] = elems[i] - o.elems[i]; + return r; } + constexpr Vec<T, N> operator-=(const Vec<T, N> o) noexcept { return *this = *this - o; } template <typename U = T> - [[nodiscard]] constexpr Vec3<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const { - return {-x, -y, -z}; + [[nodiscard]] constexpr Vec<std::enable_if_t<std::is_signed_v<U>, U>, N> operator-() const noexcept { + Vec<U, N> r{}; + for (size_t i = 0; i < N; ++i) + r.elems[i] = -elems[i]; + return r; } - [[nodiscard]] constexpr Vec3<decltype(T{} * T{})> operator*(const Vec3& other) const { - return {x * other.x, y * other.y, z * other.z}; + [[nodiscard]] constexpr Vec<decltype(T{} * T{}), N> operator*(const Vec o) const noexcept { + Vec<decltype(T{} * T{}), N> r{}; + for (size_t i = 0; i < N; ++i) + r.elems[i] = elems[i] * o.elems[i]; + return r; } - template <typename V> - [[nodiscard]] constexpr Vec3<decltype(T{} * V{})> operator*(const V& f) const { + [[nodiscard]] constexpr Vec<decltype(T{} * V{}), N> operator*(const V f) const noexcept { using TV = decltype(T{} * V{}); using C = std::common_type_t<T, V>; - - return { - static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)), - static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)), - static_cast<TV>(static_cast<C>(z) * static_cast<C>(f)), - }; + Vec<TV, N> r{}; + for (size_t i = 0; i < N; ++i) + r.elems[i] = TV(C(elems[i]) * C(f)); + return r; } - template <typename V> - constexpr Vec3& operator*=(const V& f) { - *this = *this * f; - return *this; - } + constexpr Vec<T, N> operator*=(const V f) noexcept { return *this = *this * f; } + template <typename V> - [[nodiscard]] constexpr Vec3<decltype(T{} / V{})> operator/(const V& f) const { + [[nodiscard]] constexpr Vec<decltype(T{} / V{}), N> operator/(const V f) const noexcept { using TV = decltype(T{} / V{}); using C = std::common_type_t<T, V>; - - return { - static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)), - static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)), - static_cast<TV>(static_cast<C>(z) / static_cast<C>(f)), - }; + Vec<TV, N> r{}; + for (size_t i = 0; i < N; ++i) + r.elems[i] = TV(C(elems[i]) / C(f)); + return r; } - template <typename V> - constexpr Vec3& operator/=(const V& f) { - *this = *this / f; - return *this; - } - - void RotateFromOrigin(float roll, float pitch, float yaw) { - float temp = y; - y = std::cos(roll) * y - std::sin(roll) * z; - z = std::sin(roll) * temp + std::cos(roll) * z; - - temp = x; - x = std::cos(pitch) * x + std::sin(pitch) * z; - z = -std::sin(pitch) * temp + std::cos(pitch) * z; - - temp = x; - x = std::cos(yaw) * x - std::sin(yaw) * y; - y = std::sin(yaw) * temp + std::cos(yaw) * y; - } + constexpr Vec<T, N> operator/=(const V f) noexcept { return *this = *this / f; } - [[nodiscard]] constexpr T Length2() const { - return x * x + y * y + z * z; + [[nodiscard]] constexpr T Length2() const noexcept { + T r{}; + for (size_t i = 0; i < N; ++i) + r += elems[i] * elems[i]; + return r; } - // Only implemented for T=float - [[nodiscard]] float Length() const; - [[nodiscard]] Vec3 Normalized() const; - [[nodiscard]] float Normalize(); // returns the previous length, which is often useful - - [[nodiscard]] constexpr T& operator[](std::size_t i) { - return *((&x) + i); - } - - [[nodiscard]] constexpr const T& operator[](std::size_t i) const { - return *((&x) + i); - } - - constexpr void SetZero() { - x = 0; - y = 0; - z = 0; - } - - // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates) - [[nodiscard]] constexpr T& u() { - return x; - } - [[nodiscard]] constexpr T& v() { - return y; - } - [[nodiscard]] constexpr T& w() { - return z; - } - - [[nodiscard]] constexpr T& r() { - return x; - } - [[nodiscard]] constexpr T& g() { - return y; - } - [[nodiscard]] constexpr T& b() { - return z; - } - - [[nodiscard]] constexpr T& s() { - return x; - } - [[nodiscard]] constexpr T& t() { - return y; - } - [[nodiscard]] constexpr T& q() { - return z; - } - - [[nodiscard]] constexpr const T& u() const { - return x; - } - [[nodiscard]] constexpr const T& v() const { - return y; - } - [[nodiscard]] constexpr const T& w() const { - return z; - } - - [[nodiscard]] constexpr const T& r() const { - return x; - } - [[nodiscard]] constexpr const T& g() const { - return y; - } - [[nodiscard]] constexpr const T& b() const { - return z; - } - - [[nodiscard]] constexpr const T& s() const { - return x; - } - [[nodiscard]] constexpr const T& t() const { - return y; - } - [[nodiscard]] constexpr const T& q() const { - return z; - } - -// swizzlers - create a subvector of specific components -// e.g. Vec2 uv() { return Vec2(x,y); } -// _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all -// component names (x<->r) and permutations (xy<->yx) -#define _DEFINE_SWIZZLER2(a, b, name) \ - [[nodiscard]] constexpr Vec2<T> name() const { return Vec2<T>(a, b); } -#define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \ - _DEFINE_SWIZZLER2(a, b, a##b); \ - _DEFINE_SWIZZLER2(a, b, a2##b2); \ - _DEFINE_SWIZZLER2(a, b, a3##b3); \ - _DEFINE_SWIZZLER2(a, b, a4##b4); \ - _DEFINE_SWIZZLER2(b, a, b##a); \ - _DEFINE_SWIZZLER2(b, a, b2##a2); \ - _DEFINE_SWIZZLER2(b, a, b3##a3); \ - _DEFINE_SWIZZLER2(b, a, b4##a4) - - DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t); - DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q); - DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q); -#undef DEFINE_SWIZZLER2 -#undef _DEFINE_SWIZZLER2 -}; - -template <typename T, typename V> -[[nodiscard]] constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) { - using C = std::common_type_t<T, V>; - - return Vec3<T>(static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.x)), - static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.y)), - static_cast<T>(static_cast<C>(f) * static_cast<C>(vec.z))); -} - -template <> -inline float Vec3<float>::Length() const { - return std::sqrt(x * x + y * y + z * z); -} - -template <> -inline Vec3<float> Vec3<float>::Normalized() const { - return *this / Length(); -} - -template <> -inline float Vec3<float>::Normalize() { - float length = Length(); - *this /= length; - return length; -} - -using Vec3f = Vec3<float>; - -template <typename T> -class Vec4 { -public: - T x{}; - T y{}; - T z{}; - T w{}; - - constexpr Vec4() = default; - constexpr Vec4(const T& x_, const T& y_, const T& z_, const T& w_) - : x(x_), y(y_), z(z_), w(w_) {} - - template <typename T2> - [[nodiscard]] constexpr Vec4<T2> Cast() const { - return Vec4<T2>(static_cast<T2>(x), static_cast<T2>(y), static_cast<T2>(z), - static_cast<T2>(w)); - } - - [[nodiscard]] static constexpr Vec4 AssignToAll(const T& f) { - return Vec4(f, f, f, f); - } - - [[nodiscard]] constexpr Vec4<decltype(T{} + T{})> operator+(const Vec4& other) const { - return {x + other.x, y + other.y, z + other.z, w + other.w}; - } - - constexpr Vec4& operator+=(const Vec4& other) { - x += other.x; - y += other.y; - z += other.z; - w += other.w; - return *this; - } - - [[nodiscard]] constexpr Vec4<decltype(T{} - T{})> operator-(const Vec4& other) const { - return {x - other.x, y - other.y, z - other.z, w - other.w}; - } - - constexpr Vec4& operator-=(const Vec4& other) { - x -= other.x; - y -= other.y; - z -= other.z; - w -= other.w; - return *this; - } - - template <typename U = T> - [[nodiscard]] constexpr Vec4<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const { - return {-x, -y, -z, -w}; - } - - [[nodiscard]] constexpr Vec4<decltype(T{} * T{})> operator*(const Vec4& other) const { - return {x * other.x, y * other.y, z * other.z, w * other.w}; - } - - template <typename V> - [[nodiscard]] constexpr Vec4<decltype(T{} * V{})> operator*(const V& f) const { - using TV = decltype(T{} * V{}); - using C = std::common_type_t<T, V>; - - return { - static_cast<TV>(static_cast<C>(x) * static_cast<C>(f)), - static_cast<TV>(static_cast<C>(y) * static_cast<C>(f)), - static_cast<TV>(static_cast<C>(z) * static_cast<C>(f)), - static_cast<TV>(static_cast<C>(w) * static_cast<C>(f)), - }; - } - - template <typename V> - constexpr Vec4& operator*=(const V& f) { - *this = *this * f; - return *this; - } - - template <typename V> - [[nodiscard]] constexpr Vec4<decltype(T{} / V{})> operator/(const V& f) const { - using TV = decltype(T{} / V{}); - using C = std::common_type_t<T, V>; - + [[nodiscard]] T Length() const { return T(std::sqrt(float(Length2()))); } + [[nodiscard]] Vec<T, N> Normalized() const { return *this / Length(); } + [[nodiscard]] constexpr T& operator[](std::size_t i) noexcept { return elems[i]; } + [[nodiscard]] constexpr const T& operator[](std::size_t i) const noexcept { return elems[i]; } + + [[nodiscard]] std::array<decltype(-T{}), 16> ToMatrix() const { + const T x2 = elems[0] * elems[0]; + const T y2 = elems[1] * elems[1]; + const T z2 = elems[2] * elems[2]; + + const T xy = elems[0] * elems[1]; + const T wz = elems[3] * elems[2]; + const T xz = elems[0] * elems[2]; + const T wy = elems[3] * elems[1]; + const T yz = elems[1] * elems[2]; + const T wx = elems[3] * elems[0]; return { - static_cast<TV>(static_cast<C>(x) / static_cast<C>(f)), - static_cast<TV>(static_cast<C>(y) / static_cast<C>(f)), - static_cast<TV>(static_cast<C>(z) / static_cast<C>(f)), - static_cast<TV>(static_cast<C>(w) / static_cast<C>(f)), + 1.0f - 2.0f * (y2 + z2), + 2.0f * (xy + wz), + 2.0f * (xz - wy), + 0.0f, + 2.0f * (xy - wz), + 1.0f - 2.0f * (x2 + z2), + 2.0f * (yz + wx), + 0.0f, + 2.0f * (xz + wy), + 2.0f * (yz - wx), + 1.0f - 2.0f * (x2 + y2), + 0.0f, + 0.0f, + 0.0f, + 0.0f, + 1.0f }; } - - template <typename V> - constexpr Vec4& operator/=(const V& f) { - *this = *this / f; - return *this; - } - - [[nodiscard]] constexpr T Length2() const { - return x * x + y * y + z * z + w * w; - } - - [[nodiscard]] constexpr T& operator[](std::size_t i) { - return *((&x) + i); - } - - [[nodiscard]] constexpr const T& operator[](std::size_t i) const { - return *((&x) + i); - } - - constexpr void SetZero() { - x = 0; - y = 0; - z = 0; - w = 0; - } - - // Common alias: RGBA (colors) - [[nodiscard]] constexpr T& r() { - return x; - } - [[nodiscard]] constexpr T& g() { - return y; - } - [[nodiscard]] constexpr T& b() { - return z; - } - [[nodiscard]] constexpr T& a() { - return w; - } - - [[nodiscard]] constexpr const T& r() const { - return x; - } - [[nodiscard]] constexpr const T& g() const { - return y; - } - [[nodiscard]] constexpr const T& b() const { - return z; - } - [[nodiscard]] constexpr const T& a() const { - return w; - } - -// Swizzlers - Create a subvector of specific components -// e.g. Vec2 uv() { return Vec2(x,y); } - -// _DEFINE_SWIZZLER2 defines a single such function -// DEFINE_SWIZZLER2_COMP1 defines one-component functions for all component names (x<->r) -// DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and -// permutations (xy<->yx) -#define _DEFINE_SWIZZLER2(a, b, name) \ - [[nodiscard]] constexpr Vec2<T> name() const { return Vec2<T>(a, b); } -#define DEFINE_SWIZZLER2_COMP1(a, a2) \ - _DEFINE_SWIZZLER2(a, a, a##a); \ - _DEFINE_SWIZZLER2(a, a, a2##a2) -#define DEFINE_SWIZZLER2_COMP2(a, b, a2, b2) \ - _DEFINE_SWIZZLER2(a, b, a##b); \ - _DEFINE_SWIZZLER2(a, b, a2##b2); \ - _DEFINE_SWIZZLER2(b, a, b##a); \ - _DEFINE_SWIZZLER2(b, a, b2##a2) - - DEFINE_SWIZZLER2_COMP2(x, y, r, g); - DEFINE_SWIZZLER2_COMP2(x, z, r, b); - DEFINE_SWIZZLER2_COMP2(x, w, r, a); - DEFINE_SWIZZLER2_COMP2(y, z, g, b); - DEFINE_SWIZZLER2_COMP2(y, w, g, a); - DEFINE_SWIZZLER2_COMP2(z, w, b, a); - DEFINE_SWIZZLER2_COMP1(x, r); - DEFINE_SWIZZLER2_COMP1(y, g); - DEFINE_SWIZZLER2_COMP1(z, b); - DEFINE_SWIZZLER2_COMP1(w, a); -#undef DEFINE_SWIZZLER2_COMP1 -#undef DEFINE_SWIZZLER2_COMP2 -#undef _DEFINE_SWIZZLER2 - -#define _DEFINE_SWIZZLER3(a, b, c, name) \ - [[nodiscard]] constexpr Vec3<T> name() const { return Vec3<T>(a, b, c); } -#define DEFINE_SWIZZLER3_COMP1(a, a2) \ - _DEFINE_SWIZZLER3(a, a, a, a##a##a); \ - _DEFINE_SWIZZLER3(a, a, a, a2##a2##a2) -#define DEFINE_SWIZZLER3_COMP3(a, b, c, a2, b2, c2) \ - _DEFINE_SWIZZLER3(a, b, c, a##b##c); \ - _DEFINE_SWIZZLER3(a, c, b, a##c##b); \ - _DEFINE_SWIZZLER3(b, a, c, b##a##c); \ - _DEFINE_SWIZZLER3(b, c, a, b##c##a); \ - _DEFINE_SWIZZLER3(c, a, b, c##a##b); \ - _DEFINE_SWIZZLER3(c, b, a, c##b##a); \ - _DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \ - _DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \ - _DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \ - _DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \ - _DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \ - _DEFINE_SWIZZLER3(c, b, a, c2##b2##a2) - - DEFINE_SWIZZLER3_COMP3(x, y, z, r, g, b); - DEFINE_SWIZZLER3_COMP3(x, y, w, r, g, a); - DEFINE_SWIZZLER3_COMP3(x, z, w, r, b, a); - DEFINE_SWIZZLER3_COMP3(y, z, w, g, b, a); - DEFINE_SWIZZLER3_COMP1(x, r); - DEFINE_SWIZZLER3_COMP1(y, g); - DEFINE_SWIZZLER3_COMP1(z, b); - DEFINE_SWIZZLER3_COMP1(w, a); -#undef DEFINE_SWIZZLER3_COMP1 -#undef DEFINE_SWIZZLER3_COMP3 -#undef _DEFINE_SWIZZLER3 }; -template <typename T, typename V> -[[nodiscard]] constexpr Vec4<decltype(V{} * T{})> operator*(const V& f, const Vec4<T>& vec) { - using TV = decltype(V{} * T{}); +template <typename T, size_t N, typename V> +[[nodiscard]] constexpr Vec<T, N> operator*(const V f, const Vec<T, N> v) noexcept { using C = std::common_type_t<T, V>; - - return { - static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.x)), - static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.y)), - static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.z)), - static_cast<TV>(static_cast<C>(f) * static_cast<C>(vec.w)), - }; -} - -using Vec4f = Vec4<float>; - -template <typename T> -constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec2<T>& a, const Vec2<T>& b) { - return a.x * b.x + a.y * b.y; -} - -template <typename T> -[[nodiscard]] constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec3<T>& a, const Vec3<T>& b) { - return a.x * b.x + a.y * b.y + a.z * b.z; -} - -template <typename T> -[[nodiscard]] constexpr decltype(T{} * T{} + T{} * T{}) Dot(const Vec4<T>& a, const Vec4<T>& b) { - return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; -} - -template <> -[[nodiscard]] inline float Dot(const Vec4<float>& a, const Vec4<float>& b) { -#ifdef __ARM_NEON - float32x4_t va = vld1q_f32(&a.x); - float32x4_t vb = vld1q_f32(&b.x); - float32x4_t result = vmulq_f32(va, vb); -#if defined(__aarch64__) // Use vaddvq_f32 in ARMv8 architectures - return vaddvq_f32(result); -#else // Use manual addition for older architectures - float32x2_t sum2 = vadd_f32(vget_high_f32(result), vget_low_f32(result)); - return vget_lane_f32(vpadd_f32(sum2, sum2), 0); -#endif -#else - return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; -#endif -} - -template <typename T> -[[nodiscard]] constexpr Vec3<decltype(T{} * T{} - T{} * T{})> Cross(const Vec3<T>& a, - const Vec3<T>& b) { - return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; -} - -// linear interpolation via float: 0.0=begin, 1.0=end -template <typename X> -[[nodiscard]] constexpr decltype(X{} * float{} + X{} * float{}) Lerp(const X& begin, const X& end, - const float t) { - return begin * (1.f - t) + end * t; -} - -// linear interpolation via int: 0=begin, base=end -template <typename X, int base> -[[nodiscard]] constexpr decltype((X{} * int{} + X{} * int{}) / base) LerpInt(const X& begin, - const X& end, - const int t) { - return (begin * (base - t) + end * t) / base; -} - -// bilinear interpolation. s is for interpolating x00-x01 and x10-x11, and t is for the second -// interpolation. -template <typename X> -[[nodiscard]] constexpr auto BilinearInterp(const X& x00, const X& x01, const X& x10, const X& x11, - const float s, const float t) { - auto y0 = Lerp(x00, x01, s); - auto y1 = Lerp(x10, x11, s); - return Lerp(y0, y1, t); -} - -// Utility vector factories -template <typename T> -[[nodiscard]] constexpr Vec2<T> MakeVec(const T& x, const T& y) { - return Vec2<T>{x, y}; -} - -template <typename T> -[[nodiscard]] constexpr Vec3<T> MakeVec(const T& x, const T& y, const T& z) { - return Vec3<T>{x, y, z}; -} - -template <typename T> -[[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw) { - return MakeVec(x, y, zw[0], zw[1]); -} - -template <typename T> -[[nodiscard]] constexpr Vec3<T> MakeVec(const Vec2<T>& xy, const T& z) { - return MakeVec(xy[0], xy[1], z); -} - -template <typename T> -[[nodiscard]] constexpr Vec3<T> MakeVec(const T& x, const Vec2<T>& yz) { - return MakeVec(x, yz[0], yz[1]); -} - -template <typename T> -[[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w) { - return Vec4<T>{x, y, z, w}; -} - -template <typename T> -[[nodiscard]] constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w) { - return MakeVec(xy[0], xy[1], z, w); -} - -template <typename T> -[[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w) { - return MakeVec(x, yz[0], yz[1], w); -} - -// NOTE: This has priority over "Vec2<Vec2<T>> MakeVec(const Vec2<T>& x, const Vec2<T>& y)". -// Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error -// out soon enough due to misuse of the returned structure. -template <typename T> -[[nodiscard]] constexpr Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw) { - return MakeVec(xy[0], xy[1], zw[0], zw[1]); -} - -template <typename T> -[[nodiscard]] constexpr Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w) { - return MakeVec(xyz[0], xyz[1], xyz[2], w); -} - -template <typename T> -[[nodiscard]] constexpr Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw) { - return MakeVec(x, yzw[0], yzw[1], yzw[2]); + Vec<T, N> r{}; + for (size_t i = 0; i < N; ++i) + r.elems[i] = T(C(f) * C(v.elems[i])); + return r; } } // namespace Common |
