now add constexpr gcem tests for triangle and mat classes

This commit is contained in:
2026-06-11 23:41:07 +03:00
parent a2be99be50
commit a741fc1485
3 changed files with 39 additions and 6 deletions
+19 -2
View File
@@ -622,10 +622,14 @@ namespace omath
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard("You must use extracted scale")]]
Vector3<Type> mat_extract_scale(const Mat<4, 4, Type, St>& mat) noexcept
OMATH_CONSTEXPR Vector3<Type> 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<Type>(gcem::sqrt(x * x + y * y + z * z));
#else
return static_cast<Type>(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<Type>::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
};
}
+3 -3
View File
@@ -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);
}
+17 -1
View File
@@ -2,10 +2,10 @@
// Created by Orange on 11/30/2024.
//
#include <omath/trigonometry/angle.hpp>
#include <cmath>
#include <gtest/gtest.h>
#include <numbers>
#include <omath/trigonometry/angle.hpp>
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