From a741fc14852e58f8a4c57a0c0df8e3bb64e65a9a Mon Sep 17 00:00:00 2001 From: Orange Date: Thu, 11 Jun 2026 23:41:07 +0300 Subject: [PATCH] now add constexpr gcem tests for triangle and mat classes --- include/omath/linear_algebra/mat.hpp | 21 +++++++++++++++++++-- include/omath/linear_algebra/triangle.hpp | 6 +++--- tests/general/unit_test_angle.cpp | 18 +++++++++++++++++- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/include/omath/linear_algebra/mat.hpp b/include/omath/linear_algebra/mat.hpp index 2cbf078..a3ee0ee 100644 --- a/include/omath/linear_algebra/mat.hpp +++ b/include/omath/linear_algebra/mat.hpp @@ -622,10 +622,14 @@ namespace omath template [[nodiscard("You must use extracted scale")]] - Vector3 mat_extract_scale(const Mat<4, 4, Type, St>& mat) noexcept + OMATH_CONSTEXPR Vector3 mat_extract_scale(const Mat<4, 4, Type, St>& mat) noexcept { auto column_length = [](const Type x, const Type y, const Type z) { +#ifdef OMATH_CONSTEXPR + return static_cast(gcem::sqrt(x * x + y * y + z * z)); +#else return static_cast(std::sqrt(x * x + y * y + z * z)); +#endif }; const auto scale_x = column_length(mat.at(0, 0), mat.at(1, 0), mat.at(2, 0)); @@ -635,9 +639,16 @@ namespace omath constexpr auto epsilon = std::numeric_limits::epsilon(); return { +#ifdef OMATH_CONSTEXPR + gcem::abs(scale_x) < epsilon ? Type{1} : scale_x, + gcem::abs(scale_y) < epsilon ? Type{1} : scale_y, + gcem::abs(scale_z) < epsilon ? Type{1} : scale_z, +#else std::abs(scale_x) < epsilon ? Type{1} : scale_x, std::abs(scale_y) < epsilon ? Type{1} : scale_y, std::abs(scale_z) < epsilon ? Type{1} : scale_z, +#endif + }; } @@ -654,9 +665,15 @@ namespace omath const auto m22 = mat.at(2, 2) / scale.z; return { - angles::radians_to_degrees(std::atan2(m21, m22)), +#ifdef OMATH_CONSTEXPR + angles::radians_to_degrees(gcem::atan2(m21, m22)), + angles::radians_to_degrees(gcem::asin(std::clamp(-m20, Type{-1}, Type{1}))), + angles::radians_to_degrees(gcem::atan2(m10, m00)), +#else + angles::radians_to_degrees(gc::atan2(m21, m22)), angles::radians_to_degrees(std::asin(std::clamp(-m20, Type{-1}, Type{1}))), angles::radians_to_degrees(std::atan2(m10, m00)), +#endif }; } diff --git a/include/omath/linear_algebra/triangle.hpp b/include/omath/linear_algebra/triangle.hpp index 1d8e68c..a62f888 100644 --- a/include/omath/linear_algebra/triangle.hpp +++ b/include/omath/linear_algebra/triangle.hpp @@ -3,7 +3,7 @@ // #pragma once #include "vector3.hpp" - +#include "omath/internal/optional_constexpr_math.hpp" namespace omath { /* @@ -40,13 +40,13 @@ namespace omath } [[nodiscard]] - Vector::ContainedType side_a_length() const + OMATH_CONSTEXPR Vector::ContainedType side_a_length() const { return m_vertex1.distance_to(m_vertex2); } [[nodiscard]] - Vector::ContainedType side_b_length() const + OMATH_CONSTEXPR Vector::ContainedType side_b_length() const { return m_vertex3.distance_to(m_vertex2); } diff --git a/tests/general/unit_test_angle.cpp b/tests/general/unit_test_angle.cpp index d1fc410..ad17e65 100644 --- a/tests/general/unit_test_angle.cpp +++ b/tests/general/unit_test_angle.cpp @@ -2,10 +2,10 @@ // Created by Orange on 11/30/2024. // -#include #include #include #include +#include using namespace omath; @@ -19,6 +19,14 @@ namespace constexpr float k_eps = 1e-5f; +#ifdef OMATH_USE_GCEM + constexpr bool close_to(const float actual, const float expected, const float epsilon) + { + const float diff = actual - expected; + return (diff < 0.0f ? -diff : diff) <= epsilon; + } +#endif + } // namespace // ---------- Construction / factories ---------- @@ -190,3 +198,11 @@ TEST(UnitTestAngle, BinaryMinus_ReturnsWrappedDiff) const Deg c = a - b; // expect 340° EXPECT_FLOAT_EQ(c.as_degrees(), 340.0f); } + +#ifdef OMATH_USE_GCEM +static_assert(close_to(Pitch::from_degrees(0.0f).sin(), 0.0f, k_eps), "Sin should be constexpr with gcem"); +static_assert(close_to(Pitch::from_degrees(0.0f).cos(), 1.0f, k_eps), "Cos should be constexpr with gcem"); +static_assert(close_to(Pitch::from_degrees(45.0f).tan(), 1.0f, 1e-4f), "Tan should be constexpr with gcem"); +static_assert(close_to(Pitch::from_degrees(45.0f).cot(), 1.0f, 1e-4f), "Cot should be constexpr with gcem"); +static_assert(close_to(Pitch::from_degrees(45.0f).atan(), 0.66577375f, 1e-6f), "Atan should be constexpr with gcem"); +#endif