From bd1f5214b8e4422e6a119d73f831d26780d26760 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Sat, 5 Jun 2021 16:35:15 +0200 Subject: [PATCH] Math: Replace M_PI defines with template constexpr constants --- include/Nazara/Math/Algorithm.hpp | 26 ++++++------------------- include/Nazara/Math/Algorithm.inl | 4 ++-- include/Nazara/Math/Angle.inl | 4 ++-- include/Nazara/Math/Matrix4.inl | 2 +- src/Nazara/Physics2D/Collider2D.cpp | 2 +- src/Nazara/Physics3D/Collider3D.cpp | 4 ++-- src/Nazara/Utility/AlgorithmUtility.cpp | 14 +++++-------- tests/Engine/Math/AlgorithmMathTest.cpp | 4 ++-- tests/Engine/Math/AngleTest.cpp | 12 ++++++------ 9 files changed, 27 insertions(+), 45 deletions(-) diff --git a/include/Nazara/Math/Algorithm.hpp b/include/Nazara/Math/Algorithm.hpp index 4c07bfb96..b628ed1cb 100644 --- a/include/Nazara/Math/Algorithm.hpp +++ b/include/Nazara/Math/Algorithm.hpp @@ -14,28 +14,14 @@ #include #include -#ifndef M_PI -#define M_PI 3.141592653589793238462643 -#endif - -#ifndef M_PI_2 -#define M_PI_2 1.5707963267948966192313217 -#endif - -#ifndef M_SQRT2 -#define M_SQRT2 1.4142135623730950488016887 -#endif - -#ifndef M_SQRT3 -#define M_SQRT3 1.7320508075688772935274463 -#endif - -#ifndef M_SQRT5 -#define M_SQRT5 2.23606797749979 -#endif - namespace Nz { + template constexpr T HalfPi = T(1.5707963267948966192313216916398); + template constexpr T Pi = T(3.1415926535897932384626433832795); + template constexpr T Sqrt2 = T(1.4142135623730950488016887242097); + template constexpr T Sqrt3 = T(1.7320508075688772935274463415059); + template constexpr T Sqrt5 = T(2.2360679774997896964091736687313); + template class Angle; template constexpr T Approach(T value, T objective, T increment); diff --git a/include/Nazara/Math/Algorithm.inl b/include/Nazara/Math/Algorithm.inl index 54b6c0d7c..c7bc4dc3c 100644 --- a/include/Nazara/Math/Algorithm.inl +++ b/include/Nazara/Math/Algorithm.inl @@ -203,7 +203,7 @@ namespace Nz template constexpr T DegreeToRadian(T degrees) { - return degrees * T(M_PI/180.0); + return degrees * T(Pi/180.0); } /*! @@ -578,7 +578,7 @@ namespace Nz template constexpr T RadianToDegree(T radians) { - return radians * T(180.0/M_PI); + return radians * T(180.0/Pi); } template diff --git a/include/Nazara/Math/Angle.inl b/include/Nazara/Math/Angle.inl index eee5d084d..65871a886 100644 --- a/include/Nazara/Math/Angle.inl +++ b/include/Nazara/Math/Angle.inl @@ -68,7 +68,7 @@ namespace Nz template static constexpr T GetLimit() { - return T(M_PI); + return Pi; } template static constexpr T FromDegrees(T degrees) @@ -236,7 +236,7 @@ namespace Nz * * If angle exceeds local limits positively or negatively, bring it back between them. * For degree angles, local limits are [-180, 180] - * For radian angles, local limits are [-M_PI, M_PI] + * For radian angles, local limits are [-Pi, Pi] */ template constexpr Angle& Angle::Normalize() diff --git a/include/Nazara/Math/Matrix4.inl b/include/Nazara/Math/Matrix4.inl index ce0370d8c..1a0f4fb7b 100644 --- a/include/Nazara/Math/Matrix4.inl +++ b/include/Nazara/Math/Matrix4.inl @@ -915,7 +915,7 @@ namespace Nz Matrix4& Matrix4::MakePerspective(RadianAngle angle, T ratio, T zNear, T zFar) { // https://docs.microsoft.com/fr-fr/windows/win32/direct3d10/d3d10-d3dxmatrixperspectivefovrh - angle = RadianAngle(M_PI_2) - angle / T(2.0); + angle = RadianAngle(HalfPi) - angle / T(2.0); T yScale = angle.GetTan(); diff --git a/src/Nazara/Physics2D/Collider2D.cpp b/src/Nazara/Physics2D/Collider2D.cpp index 4546df0e3..9e73a685c 100644 --- a/src/Nazara/Physics2D/Collider2D.cpp +++ b/src/Nazara/Physics2D/Collider2D.cpp @@ -73,7 +73,7 @@ namespace Nz Vector2f origin = FromChipmunk(pos); float r = static_cast(radius); - RadianAnglef angleBetweenVertices = 2.f * float(M_PI) / vertices.size(); + RadianAnglef angleBetweenVertices = 2.f * Pi / vertices.size(); for (std::size_t i = 0; i < vertices.size(); ++i) { RadianAnglef angle = float(i) * angleBetweenVertices; diff --git a/src/Nazara/Physics3D/Collider3D.cpp b/src/Nazara/Physics3D/Collider3D.cpp index b1c9681cd..f48fa75be 100644 --- a/src/Nazara/Physics3D/Collider3D.cpp +++ b/src/Nazara/Physics3D/Collider3D.cpp @@ -422,7 +422,7 @@ namespace Nz Boxf SphereCollider3D::ComputeAABB(const Matrix4f& offsetMatrix, const Vector3f& scale) const { - Vector3f size(m_radius * NazaraSuffixMacro(M_SQRT3, f) * scale); + Vector3f size(m_radius * Sqrt5 * scale); Vector3f position(offsetMatrix.GetTranslation()); return Boxf(position - size, position + size); @@ -430,7 +430,7 @@ namespace Nz float SphereCollider3D::ComputeVolume() const { - return float(M_PI) * m_radius * m_radius * m_radius / 3.f; + return Pi * m_radius * m_radius * m_radius / 3.f; } float SphereCollider3D::GetRadius() const diff --git a/src/Nazara/Utility/AlgorithmUtility.cpp b/src/Nazara/Utility/AlgorithmUtility.cpp index 8ad1970b4..430c966c7 100644 --- a/src/Nazara/Utility/AlgorithmUtility.cpp +++ b/src/Nazara/Utility/AlgorithmUtility.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020 Jérôme Leclercq +// Copyright (C) 2020 Jérôme Leclercq // This file is part of the "Nazara Engine - Utility module" // For conditions of distribution and use, see copyright notice in Config.hpp @@ -854,7 +854,7 @@ namespace Nz void GenerateCone(float length, float radius, unsigned int subdivision, const Matrix4f& matrix, const Rectf& textureCoords, VertexPointers vertexPointers, IndexIterator indices, Boxf* aabb, unsigned int indexOffset) { - constexpr float round = 2.f*static_cast(M_PI); + constexpr float round = 2.f * Pi; float delta = round/subdivision; *vertexPointers.positionPtr++ = matrix.GetTranslation(); // matrix.Transform(Vector3f(0.f)); @@ -997,23 +997,19 @@ namespace Nz float invSliceCount = 1.f / (sliceCount-1); float invStackCount = 1.f / (stackCount-1); - const float pi = static_cast(M_PI); // Pour éviter toute promotion en double - const float pi2 = pi * 2.f; - const float pi_2 = pi / 2.f; - for (unsigned int stack = 0; stack < stackCount; ++stack) { float stackVal = stack * invStackCount; - float stackValPi = stackVal * pi; + float stackValPi = stackVal * Pi; float sinStackValPi = std::sin(stackValPi); for (unsigned int slice = 0; slice < sliceCount; ++slice) { float sliceVal = slice * invSliceCount; - float sliceValPi2 = sliceVal * pi2; + float sliceValPi2 = sliceVal * 2.f * Pi; Vector3f normal; - normal.y = std::sin(-pi_2 + stackValPi); + normal.y = std::sin(-HalfPi + stackValPi); normal.x = std::cos(sliceValPi2) * sinStackValPi; normal.z = std::sin(sliceValPi2) * sinStackValPi; diff --git a/tests/Engine/Math/AlgorithmMathTest.cpp b/tests/Engine/Math/AlgorithmMathTest.cpp index 7881acf9a..36a9e94ea 100644 --- a/tests/Engine/Math/AlgorithmMathTest.cpp +++ b/tests/Engine/Math/AlgorithmMathTest.cpp @@ -61,7 +61,7 @@ TEST_CASE("DegreeToRadian", "[MATH][ALGORITHM]") { SECTION("Convert 45.f degree to radian") { - REQUIRE(Nz::DegreeToRadian(45.f) == Approx(M_PI / 4)); + REQUIRE(Nz::DegreeToRadian(45.f) == Approx(Nz::Pi / 4.f)); } } @@ -323,7 +323,7 @@ TEST_CASE("RadianToDegree", "[MATH][ALGORITHM]") { SECTION("PI / 4 to degree") { - REQUIRE(Nz::RadianToDegree(M_PI / 4) == Approx(45.f)); + REQUIRE(Nz::RadianToDegree(Nz::Pi / 4.f) == Approx(45.f)); } } diff --git a/tests/Engine/Math/AngleTest.cpp b/tests/Engine/Math/AngleTest.cpp index b0df420ef..efafcdab3 100644 --- a/tests/Engine/Math/AngleTest.cpp +++ b/tests/Engine/Math/AngleTest.cpp @@ -25,7 +25,7 @@ SCENARIO("Angle", "[MATH][ANGLE]") THEN("It should be equal to pi/2") { - Nz::RadianAnglef expectedResult(float(M_PI_2)); + Nz::RadianAnglef expectedResult(Nz::HalfPi); CHECK(radAngle == expectedResult); CHECK(angle.ToRadianAngle() == expectedResult); @@ -104,9 +104,9 @@ SCENARIO("Angle", "[MATH][ANGLE]") } } - GIVEN("A radian angle of -M_PI") + GIVEN("A radian angle of -Pi") { - Nz::RadianAnglef angle(float(-M_PI)); + Nz::RadianAnglef angle(-Nz::Pi); WHEN("We convert it to radians") { @@ -172,7 +172,7 @@ SCENARIO("Angle", "[MATH][ANGLE]") GIVEN("A radian angle of 7pi") { - Nz::RadianAnglef angle(float(7 * M_PI)); + Nz::RadianAnglef angle(7.f * Nz::Pi); WHEN("We normalize it") { @@ -180,7 +180,7 @@ SCENARIO("Angle", "[MATH][ANGLE]") THEN("It should be equal to a normalized version of itself") { - Nz::RadianAnglef expectedResult(float(M_PI)); + Nz::RadianAnglef expectedResult(Nz::Pi); CHECK(angle == expectedResult); } @@ -189,7 +189,7 @@ SCENARIO("Angle", "[MATH][ANGLE]") GIVEN("A radian angle of -4pi") { - Nz::RadianAnglef angle(float(-4 * M_PI)); + Nz::RadianAnglef angle(-4.f * Nz::Pi); WHEN("We normalize it") {