From 26a0a8d046cd551892c341e76ef7ac823913984a Mon Sep 17 00:00:00 2001 From: Slendi Date: Fri, 14 Nov 2025 16:17:57 +0200 Subject: [PATCH] Remove some methods if Vec doesn't use floats Signed-off-by: Slendi --- include/smath.hpp | 63 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/include/smath.hpp b/include/smath.hpp index db30463..551e065 100644 --- a/include/smath.hpp +++ b/include/smath.hpp @@ -162,6 +162,14 @@ public: VEC_ACC(v, 2, 1) #undef VEC_ACC + // Unary + constexpr auto operator-() noexcept -> Vec { + Vec r{}; + for (std::size_t i = 0; i < N; ++i) + r[i] = -(*this)[i]; + return r; + } + // RHS operations friend constexpr auto operator+(T s, Vec const &v) noexcept -> Vec { return v + s; @@ -217,29 +225,56 @@ public: VEC_OP_ASSIGN(/) #undef VEC_OP_ASSIGN - constexpr auto magnitude() const noexcept -> T { + constexpr bool operator==(Vec const &v) const noexcept { + for (std::size_t i = 0; i < N; ++i) + if ((*this)[i] != v[i]) + return false; + return true; + } + + constexpr bool operator!=(Vec const &v) const noexcept { + return !(*this == v); + } + + constexpr auto magnitude() const noexcept -> T + requires std::is_floating_point_v + { T total = 0; for (auto const &v : *this) total += v * v; return std::sqrt(total); } - constexpr auto length() const noexcept -> T { return this->magnitude(); } + constexpr auto length() const noexcept -> T + requires std::is_floating_point_v + { + return this->magnitude(); + } - constexpr Vec normalized_safe(T eps = eps_default) const noexcept { + template + requires std::is_floating_point_v + constexpr Vec normalized_safe(U eps = eps_default) const noexcept { auto m = magnitude(); return (m > eps) ? (*this) / m : Vec{}; } - constexpr Vec normalize_safe(T eps = eps_default) const noexcept { + template + requires std::is_floating_point_v + constexpr Vec normalize_safe(U eps = eps_default) const noexcept { return normalized_safe(eps); } - [[nodiscard]] constexpr auto normalized() noexcept -> Vec const { + [[nodiscard]] constexpr auto normalized() noexcept -> Vec const + requires std::is_floating_point_v + { return (*this) / this->magnitude(); } - [[nodiscard]] constexpr auto normalize() noexcept -> Vec const { + [[nodiscard]] constexpr auto normalize() noexcept -> Vec const + requires std::is_floating_point_v + { return this->normalized(); } - [[nodiscard]] constexpr auto unit() noexcept -> Vec const { + [[nodiscard]] constexpr auto unit() noexcept -> Vec const + requires std::is_floating_point_v + { return this->normalized(); } @@ -254,6 +289,7 @@ public: static constexpr T eps_default = T(1e-6); template + requires std::is_floating_point_v [[nodiscard]] constexpr auto approx_equal(Vec const &rhs, U eps = eps_default) const noexcept { using F = std::conditional_t, U, double>; @@ -263,7 +299,10 @@ public: return true; } - template constexpr auto magnitude_promoted() const noexcept { + template + constexpr auto magnitude_promoted() const noexcept + requires std::is_floating_point_v + { using F = std::conditional_t, U, double>; F s = 0; for (auto v : *this) @@ -279,11 +318,15 @@ public: (*this)[0] * r[1] - (*this)[1] * r[0]}; } - constexpr T distance(Vec const &r) const noexcept { + constexpr T distance(Vec const &r) const noexcept + requires std::is_floating_point_v + { return (*this - r).magnitude(); } - constexpr Vec project_onto(Vec const &n) const noexcept { + constexpr Vec project_onto(Vec const &n) const noexcept + requires std::is_floating_point_v + { auto d = this->dot(n); auto nn = n.dot(n); return (nn ? (d / nn) * n : Vec());