2026-08-31 19:26:09 +00:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
|
2025-08-02 01:48:04 +02:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
2022-04-28 18:24:11 +02:00
|
|
|
// SPDX-FileCopyrightText: 2014 Tony Wasserka
|
|
|
|
|
// SPDX-FileCopyrightText: 2014 Dolphin Emulator Project
|
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause AND GPL-2.0-or-later
|
2014-07-27 20:07:45 +02:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2025-08-02 01:48:04 +02:00
|
|
|
#ifdef __ARM_NEON
|
|
|
|
|
#include <arm_neon.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-07-27 20:07:45 +02:00
|
|
|
#include <cmath>
|
2017-08-29 12:59:54 -05:00
|
|
|
#include <type_traits>
|
2014-07-27 20:07:45 +02:00
|
|
|
|
2019-02-26 22:38:34 -05:00
|
|
|
namespace Common {
|
2014-07-27 20:07:45 +02:00
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename T>
|
|
|
|
|
class Vec2;
|
|
|
|
|
template <typename T>
|
|
|
|
|
class Vec3;
|
|
|
|
|
template <typename T>
|
2026-08-31 19:26:09 +00:00
|
|
|
class Quaternion;
|
2014-07-27 20:07:45 +02:00
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename T>
|
2014-07-27 20:07:45 +02:00
|
|
|
class Vec2 {
|
|
|
|
|
public:
|
2018-05-01 21:25:20 -04:00
|
|
|
T x{};
|
|
|
|
|
T y{};
|
2014-07-27 20:07:45 +02:00
|
|
|
|
2018-08-07 21:32:05 -04:00
|
|
|
constexpr Vec2() = default;
|
2026-08-31 19:26:09 +00:00
|
|
|
constexpr Vec2(const T& x_, const T& y_) noexcept : x(x_), y(y_) {}
|
2014-07-27 20:07:45 +02:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<decltype(T{} + T{})> operator+(const Vec2& other) const noexcept {
|
2018-08-07 21:32:05 -04:00
|
|
|
return {x + other.x, y + other.y};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2026-08-31 19:26:09 +00:00
|
|
|
constexpr Vec2& operator+=(const Vec2& other) noexcept {
|
2016-09-18 09:38:01 +09:00
|
|
|
x += other.x;
|
|
|
|
|
y += other.y;
|
2018-08-07 21:32:05 -04:00
|
|
|
return *this;
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<decltype(T{} - T{})> operator-(const Vec2& other) const noexcept {
|
2018-08-07 21:32:05 -04:00
|
|
|
return {x - other.x, y - other.y};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2026-08-31 19:26:09 +00:00
|
|
|
constexpr Vec2& operator-=(const Vec2& other) noexcept {
|
2016-09-18 09:38:01 +09:00
|
|
|
x -= other.x;
|
|
|
|
|
y -= other.y;
|
2018-08-07 21:32:05 -04:00
|
|
|
return *this;
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2017-07-11 19:51:29 +03:00
|
|
|
|
2017-08-29 12:59:54 -05:00
|
|
|
template <typename U = T>
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const noexcept {
|
2018-08-07 21:32:05 -04:00
|
|
|
return {-x, -y};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<decltype(T{} * T{})> operator*(const Vec2& other) const noexcept {
|
2018-08-07 21:32:05 -04:00
|
|
|
return {x * other.x, y * other.y};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename V>
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<decltype(T{} * V{})> operator*(const V& f) const noexcept {
|
2020-10-14 02:51:14 -04:00
|
|
|
using TV = decltype(T{} * V{});
|
|
|
|
|
using C = std::common_type_t<T, V>;
|
|
|
|
|
return {
|
2026-08-31 19:26:09 +00:00
|
|
|
TV(C(x) * C(f)),
|
|
|
|
|
TV(C(y) * C(f)),
|
2020-10-14 02:51:14 -04:00
|
|
|
};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename V>
|
2026-08-31 19:26:09 +00:00
|
|
|
constexpr Vec2& operator*=(const V& f) noexcept {
|
|
|
|
|
return *this = *this * f;
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename V>
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<decltype(T{} / V{})> operator/(const V& f) const noexcept {
|
2020-10-14 02:51:14 -04:00
|
|
|
using TV = decltype(T{} / V{});
|
|
|
|
|
using C = std::common_type_t<T, V>;
|
|
|
|
|
return {
|
2026-08-31 19:26:09 +00:00
|
|
|
TV(C(x) / C(f)),
|
|
|
|
|
TV(C(y) / C(f)),
|
2020-10-14 02:51:14 -04:00
|
|
|
};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename V>
|
2026-08-31 19:26:09 +00:00
|
|
|
constexpr Vec2& operator/=(const V& f) noexcept {
|
|
|
|
|
return *this = *this / f;
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
|
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr T Length2() const noexcept {
|
2016-09-18 09:38:01 +09:00
|
|
|
return x * x + y * y;
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only implemented for T=float
|
2020-08-14 09:38:45 -04:00
|
|
|
[[nodiscard]] float Length() const;
|
2014-07-27 20:07:45 +02:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr T& operator[](std::size_t i) noexcept {
|
2014-07-27 20:07:45 +02:00
|
|
|
return *((&x) + i);
|
|
|
|
|
}
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr const T& operator[](std::size_t i) const noexcept {
|
2014-07-27 20:07:45 +02:00
|
|
|
return *((&x) + i);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename T, typename V>
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec2<T> operator*(const V& f, const Vec2<T>& vec) noexcept {
|
2020-10-14 02:51:14 -04:00
|
|
|
using C = std::common_type_t<T, V>;
|
2026-08-31 19:26:09 +00:00
|
|
|
return Vec2<T>(T(C(f) * C(vec.x)), T(C(f) * C(vec.y)));
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-07 14:15:56 -04:00
|
|
|
using Vec2f = Vec2<float>;
|
2014-07-27 20:07:45 +02:00
|
|
|
|
2016-12-11 23:28:55 +02:00
|
|
|
template <>
|
|
|
|
|
inline float Vec2<float>::Length() const {
|
|
|
|
|
return std::sqrt(x * x + y * y);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename T>
|
|
|
|
|
class Vec3 {
|
2014-07-27 20:07:45 +02:00
|
|
|
public:
|
2018-05-01 21:25:20 -04:00
|
|
|
T x{};
|
|
|
|
|
T y{};
|
|
|
|
|
T z{};
|
2014-07-27 20:07:45 +02:00
|
|
|
|
2018-08-07 21:32:05 -04:00
|
|
|
constexpr Vec3() = default;
|
2026-08-31 19:26:09 +00:00
|
|
|
constexpr Vec3(const T& x_, const T& y_, const T& z_) noexcept : x(x_), y(y_), z(z_) {}
|
2014-07-27 20:07:45 +02:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<decltype(T{} + T{})> operator+(const Vec3& other) const noexcept {
|
2018-08-07 21:32:05 -04:00
|
|
|
return {x + other.x, y + other.y, z + other.z};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
constexpr Vec3 operator+=(const Vec3& other) noexcept {
|
2016-09-18 09:38:01 +09:00
|
|
|
x += other.x;
|
|
|
|
|
y += other.y;
|
|
|
|
|
z += other.z;
|
2018-08-07 21:32:05 -04:00
|
|
|
return *this;
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<decltype(T{} - T{})> operator-(const Vec3& other) const noexcept {
|
2018-08-07 21:32:05 -04:00
|
|
|
return {x - other.x, y - other.y, z - other.z};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
constexpr Vec3 operator-=(const Vec3& other) noexcept {
|
2016-09-18 09:38:01 +09:00
|
|
|
x -= other.x;
|
|
|
|
|
y -= other.y;
|
|
|
|
|
z -= other.z;
|
2018-08-07 21:32:05 -04:00
|
|
|
return *this;
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2017-07-11 19:51:29 +03:00
|
|
|
|
2017-08-29 12:59:54 -05:00
|
|
|
template <typename U = T>
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<std::enable_if_t<std::is_signed_v<U>, U>> operator-() const noexcept {
|
2018-08-07 21:32:05 -04:00
|
|
|
return {-x, -y, -z};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<decltype(T{} * T{})> operator*(const Vec3& other) const noexcept {
|
2018-08-07 21:32:05 -04:00
|
|
|
return {x * other.x, y * other.y, z * other.z};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename V>
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<decltype(T{} * V{})> operator*(const V& f) const noexcept {
|
2020-10-14 02:51:14 -04:00
|
|
|
using TV = decltype(T{} * V{});
|
|
|
|
|
using C = std::common_type_t<T, V>;
|
|
|
|
|
|
|
|
|
|
return {
|
2026-08-31 19:26:09 +00:00
|
|
|
TV(C(x) * C(f)),
|
|
|
|
|
TV(C(y) * C(f)),
|
|
|
|
|
TV(C(z) * C(f)),
|
2020-10-14 02:51:14 -04:00
|
|
|
};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename V>
|
2026-08-31 19:26:09 +00:00
|
|
|
constexpr Vec3 operator*=(const V& f) noexcept {
|
|
|
|
|
return *this = *this * f;
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename V>
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<decltype(T{} / V{})> operator/(const V& f) const noexcept {
|
2020-10-14 02:51:14 -04:00
|
|
|
using TV = decltype(T{} / V{});
|
|
|
|
|
using C = std::common_type_t<T, V>;
|
|
|
|
|
|
|
|
|
|
return {
|
2026-08-31 19:26:09 +00:00
|
|
|
TV(C(x) / C(f)),
|
|
|
|
|
TV(C(y) / C(f)),
|
|
|
|
|
TV(C(z) / C(f)),
|
2020-10-14 02:51:14 -04:00
|
|
|
};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename V>
|
2026-08-31 19:26:09 +00:00
|
|
|
constexpr Vec3& operator/=(const V& f) noexcept {
|
|
|
|
|
return *this = *this / f;
|
2023-05-05 12:29:26 -06:00
|
|
|
}
|
|
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr T Length2() const noexcept {
|
2016-09-18 09:38:01 +09:00
|
|
|
return x * x + y * y + z * z;
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only implemented for T=float
|
2020-08-14 09:38:45 -04:00
|
|
|
[[nodiscard]] float Length() const;
|
|
|
|
|
[[nodiscard]] Vec3 Normalized() const;
|
2014-07-27 20:07:45 +02:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr T& operator[](std::size_t i) noexcept {
|
2014-07-27 20:07:45 +02:00
|
|
|
return *((&x) + i);
|
|
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr const T& operator[](std::size_t i) const noexcept {
|
2014-07-27 20:07:45 +02:00
|
|
|
return *((&x) + i);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename T, typename V>
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] constexpr Vec3<T> operator*(const V& f, const Vec3<T>& vec) noexcept {
|
2020-10-14 02:51:14 -04:00
|
|
|
using C = std::common_type_t<T, V>;
|
2026-08-31 19:26:09 +00:00
|
|
|
return Vec3<T>(T(C(f) * C(vec.x)), T(C(f) * C(vec.y)), T(C(f) * C(vec.z)));
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
|
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <>
|
2014-08-23 13:48:00 +02:00
|
|
|
inline float Vec3<float>::Length() const {
|
|
|
|
|
return std::sqrt(x * x + y * y + z * z);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <>
|
2014-08-23 13:48:00 +02:00
|
|
|
inline Vec3<float> Vec3<float>::Normalized() const {
|
|
|
|
|
return *this / Length();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-07 14:15:56 -04:00
|
|
|
using Vec3f = Vec3<float>;
|
2014-07-27 20:07:45 +02:00
|
|
|
|
2016-09-18 09:38:01 +09:00
|
|
|
template <typename T>
|
2026-08-31 19:26:09 +00:00
|
|
|
class Quaternion {
|
2014-07-27 20:07:45 +02:00
|
|
|
public:
|
2026-08-31 19:26:09 +00:00
|
|
|
Vec3<T> xyz;
|
2018-05-01 21:25:20 -04:00
|
|
|
T w{};
|
2014-07-27 20:07:45 +02:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] Quaternion<decltype(T{} + T{})> operator+(const Quaternion& other) const noexcept {
|
|
|
|
|
return {xyz + other.xyz, w + other.w};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2017-07-11 20:08:56 +03:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] Quaternion<decltype(T{} - T{})> operator-(const Quaternion& other) const noexcept {
|
|
|
|
|
return {xyz - other.xyz, w - other.w};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] Quaternion<decltype(T{} * T{} - T{} * T{})> operator*(const Quaternion& other) const noexcept {
|
|
|
|
|
return {xyz * other.w + other.xyz * w + Cross(xyz, other.xyz), w * other.w - Dot(xyz, other.xyz)};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] Quaternion<T> Normalized() const noexcept {
|
|
|
|
|
T length = std::sqrt(xyz.Length2() + w * w);
|
|
|
|
|
return {xyz / length, w / length};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
[[nodiscard]] std::array<decltype(-T{}), 16> ToMatrix() const {
|
|
|
|
|
const T x2 = xyz[0] * xyz[0];
|
|
|
|
|
const T y2 = xyz[1] * xyz[1];
|
|
|
|
|
const T z2 = xyz[2] * xyz[2];
|
2018-08-07 21:32:05 -04:00
|
|
|
|
2026-08-31 19:26:09 +00:00
|
|
|
const T xy = xyz[0] * xyz[1];
|
|
|
|
|
const T wz = w * xyz[2];
|
|
|
|
|
const T xz = xyz[0] * xyz[2];
|
|
|
|
|
const T wy = w * xyz[1];
|
|
|
|
|
const T yz = xyz[1] * xyz[2];
|
|
|
|
|
const T wx = w * xyz[0];
|
2020-10-14 02:51:14 -04:00
|
|
|
|
|
|
|
|
return {
|
2026-08-31 19:26:09 +00:00
|
|
|
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
|
2020-10-14 02:51:14 -04:00
|
|
|
};
|
2014-07-27 20:07:45 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-26 22:38:34 -05:00
|
|
|
} // namespace Common
|