From aa8e3b16377579e589cf9a60b015a1d7f583b39a Mon Sep 17 00:00:00 2001 From: Slendi Date: Sat, 10 Jan 2026 15:33:04 +0200 Subject: [PATCH] Fixes Signed-off-by: Slendi --- include/smath.hpp | 55 +++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/include/smath.hpp b/include/smath.hpp index 63e12d9..bf4a00d 100644 --- a/include/smath.hpp +++ b/include/smath.hpp @@ -577,46 +577,49 @@ using Vec2d = Vec<2, double>; using Vec3d = Vec<3, double>; using Vec4d = Vec<4, double>; -template constexpr auto deg(T const value) +template +using angle_ret_t = std::conditional_t, float, double>; + +template constexpr auto deg(T value) { - using R = std::common_type_t; + using R = angle_ret_t; + if constexpr (detail::SMATH_ANGLE_UNIT_ID == detail::AngularUnit::Degrees) { return static_cast(value); } else if constexpr (detail::SMATH_ANGLE_UNIT_ID == detail::AngularUnit::Radians) { return static_cast(value) * static_cast(std::numbers::pi / 180.0); - } else if constexpr (detail::SMATH_ANGLE_UNIT_ID - == detail::AngularUnit::Turns) { + } else { return static_cast(value) / static_cast(360.0); } } -template constexpr auto rad(T const value) +template constexpr auto rad(T value) { - using R = std::common_type_t; + using R = angle_ret_t; + if constexpr (detail::SMATH_ANGLE_UNIT_ID == detail::AngularUnit::Degrees) { return static_cast(value) * static_cast(180.0 / std::numbers::pi); } else if constexpr (detail::SMATH_ANGLE_UNIT_ID == detail::AngularUnit::Radians) { return static_cast(value); - } else if constexpr (detail::SMATH_ANGLE_UNIT_ID - == detail::AngularUnit::Turns) { + } else { return static_cast(value) / (static_cast(2.0) * static_cast(std::numbers::pi)); } } -template constexpr auto turns(T const value) +template constexpr auto turns(T value) { - using R = std::common_type_t; + using R = angle_ret_t; + if constexpr (detail::SMATH_ANGLE_UNIT_ID == detail::AngularUnit::Degrees) { return static_cast(value) * static_cast(360.0); } else if constexpr (detail::SMATH_ANGLE_UNIT_ID == detail::AngularUnit::Radians) { return static_cast(value) * (static_cast(2.0) * static_cast(std::numbers::pi)); - } else if constexpr (detail::SMATH_ANGLE_UNIT_ID - == detail::AngularUnit::Turns) { + } else { return static_cast(value); } } @@ -1105,28 +1108,18 @@ inline auto matrix_perspective( template [[nodiscard]] inline auto matrix_look_at(Vec<3, T> const eye, - Vec<3, T> const center, Vec<3, T> const up, bool flip_z_axis = false) - -> Mat<4, 4, T> + Vec<3, T> const center, Vec<3, T> const up) -> Mat<4, 4, T> { - auto f = (center - eye).normalized(); - auto s = f.cross(up).normalized(); + auto f = (center - eye).normalized_safe(); + auto s = f.cross(up).normalized_safe(); auto u = s.cross(f); - if (!flip_z_axis) { - return Mat<4, 4, T> { - Vec<4, T> { s.x(), s.y(), s.z(), 0 }, - Vec<4, T> { u.x(), u.y(), u.z(), 0 }, - Vec<4, T> { -f.x(), -f.y(), -f.z(), 0 }, - Vec<4, T> { -s.dot(eye), -u.dot(eye), f.dot(eye), 1 }, - }; - } else { - return Mat<4, 4, T> { - Vec<4, T> { s.x(), s.y(), s.z(), 0 }, - Vec<4, T> { u.x(), u.y(), u.z(), 0 }, - Vec<4, T> { f.x(), f.y(), f.z(), 0 }, - Vec<4, T> { -s.dot(eye), -u.dot(eye), -f.dot(eye), 1 }, - }; - } + return Mat<4, 4, T> { + Vec<4, T> { s.x(), s.y(), s.z(), 0 }, + Vec<4, T> { u.x(), u.y(), u.z(), 0 }, + Vec<4, T> { -f.x(), -f.y(), -f.z(), 0 }, + Vec<4, T> { -s.dot(eye), -u.dot(eye), f.dot(eye), 1 }, + }; } template