Math: Replace M_PI defines with template constexpr constants
This commit is contained in:
parent
df8b4b59e3
commit
bd1f5214b8
|
|
@ -14,28 +14,14 @@
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#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
|
namespace Nz
|
||||||
{
|
{
|
||||||
|
template<typename T> constexpr T HalfPi = T(1.5707963267948966192313216916398);
|
||||||
|
template<typename T> constexpr T Pi = T(3.1415926535897932384626433832795);
|
||||||
|
template<typename T> constexpr T Sqrt2 = T(1.4142135623730950488016887242097);
|
||||||
|
template<typename T> constexpr T Sqrt3 = T(1.7320508075688772935274463415059);
|
||||||
|
template<typename T> constexpr T Sqrt5 = T(2.2360679774997896964091736687313);
|
||||||
|
|
||||||
template<AngleUnit Unit, typename T> class Angle;
|
template<AngleUnit Unit, typename T> class Angle;
|
||||||
|
|
||||||
template<typename T> constexpr T Approach(T value, T objective, T increment);
|
template<typename T> constexpr T Approach(T value, T objective, T increment);
|
||||||
|
|
|
||||||
|
|
@ -203,7 +203,7 @@ namespace Nz
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr T DegreeToRadian(T degrees)
|
constexpr T DegreeToRadian(T degrees)
|
||||||
{
|
{
|
||||||
return degrees * T(M_PI/180.0);
|
return degrees * T(Pi<T>/180.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
@ -578,7 +578,7 @@ namespace Nz
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr T RadianToDegree(T radians)
|
constexpr T RadianToDegree(T radians)
|
||||||
{
|
{
|
||||||
return radians * T(180.0/M_PI);
|
return radians * T(180.0/Pi<T>);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ namespace Nz
|
||||||
|
|
||||||
template<typename T> static constexpr T GetLimit()
|
template<typename T> static constexpr T GetLimit()
|
||||||
{
|
{
|
||||||
return T(M_PI);
|
return Pi<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T> static constexpr T FromDegrees(T degrees)
|
template<typename T> 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.
|
* If angle exceeds local limits positively or negatively, bring it back between them.
|
||||||
* For degree angles, local limits are [-180, 180]
|
* 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<AngleUnit Unit, typename T>
|
template<AngleUnit Unit, typename T>
|
||||||
constexpr Angle<Unit, T>& Angle<Unit, T>::Normalize()
|
constexpr Angle<Unit, T>& Angle<Unit, T>::Normalize()
|
||||||
|
|
|
||||||
|
|
@ -915,7 +915,7 @@ namespace Nz
|
||||||
Matrix4<T>& Matrix4<T>::MakePerspective(RadianAngle<T> angle, T ratio, T zNear, T zFar)
|
Matrix4<T>& Matrix4<T>::MakePerspective(RadianAngle<T> angle, T ratio, T zNear, T zFar)
|
||||||
{
|
{
|
||||||
// https://docs.microsoft.com/fr-fr/windows/win32/direct3d10/d3d10-d3dxmatrixperspectivefovrh
|
// https://docs.microsoft.com/fr-fr/windows/win32/direct3d10/d3d10-d3dxmatrixperspectivefovrh
|
||||||
angle = RadianAngle<T>(M_PI_2) - angle / T(2.0);
|
angle = RadianAngle<T>(HalfPi<T>) - angle / T(2.0);
|
||||||
|
|
||||||
T yScale = angle.GetTan();
|
T yScale = angle.GetTan();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ namespace Nz
|
||||||
Vector2f origin = FromChipmunk(pos);
|
Vector2f origin = FromChipmunk(pos);
|
||||||
float r = static_cast<float>(radius);
|
float r = static_cast<float>(radius);
|
||||||
|
|
||||||
RadianAnglef angleBetweenVertices = 2.f * float(M_PI) / vertices.size();
|
RadianAnglef angleBetweenVertices = 2.f * Pi<float> / vertices.size();
|
||||||
for (std::size_t i = 0; i < vertices.size(); ++i)
|
for (std::size_t i = 0; i < vertices.size(); ++i)
|
||||||
{
|
{
|
||||||
RadianAnglef angle = float(i) * angleBetweenVertices;
|
RadianAnglef angle = float(i) * angleBetweenVertices;
|
||||||
|
|
|
||||||
|
|
@ -422,7 +422,7 @@ namespace Nz
|
||||||
|
|
||||||
Boxf SphereCollider3D::ComputeAABB(const Matrix4f& offsetMatrix, const Vector3f& scale) const
|
Boxf SphereCollider3D::ComputeAABB(const Matrix4f& offsetMatrix, const Vector3f& scale) const
|
||||||
{
|
{
|
||||||
Vector3f size(m_radius * NazaraSuffixMacro(M_SQRT3, f) * scale);
|
Vector3f size(m_radius * Sqrt5<float> * scale);
|
||||||
Vector3f position(offsetMatrix.GetTranslation());
|
Vector3f position(offsetMatrix.GetTranslation());
|
||||||
|
|
||||||
return Boxf(position - size, position + size);
|
return Boxf(position - size, position + size);
|
||||||
|
|
@ -430,7 +430,7 @@ namespace Nz
|
||||||
|
|
||||||
float SphereCollider3D::ComputeVolume() const
|
float SphereCollider3D::ComputeVolume() const
|
||||||
{
|
{
|
||||||
return float(M_PI) * m_radius * m_radius * m_radius / 3.f;
|
return Pi<float> * m_radius * m_radius * m_radius / 3.f;
|
||||||
}
|
}
|
||||||
|
|
||||||
float SphereCollider3D::GetRadius() const
|
float SphereCollider3D::GetRadius() const
|
||||||
|
|
|
||||||
|
|
@ -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"
|
// This file is part of the "Nazara Engine - Utility module"
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// 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)
|
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<float>(M_PI);
|
constexpr float round = 2.f * Pi<float>;
|
||||||
float delta = round/subdivision;
|
float delta = round/subdivision;
|
||||||
|
|
||||||
*vertexPointers.positionPtr++ = matrix.GetTranslation(); // matrix.Transform(Vector3f(0.f));
|
*vertexPointers.positionPtr++ = matrix.GetTranslation(); // matrix.Transform(Vector3f(0.f));
|
||||||
|
|
@ -997,23 +997,19 @@ namespace Nz
|
||||||
float invSliceCount = 1.f / (sliceCount-1);
|
float invSliceCount = 1.f / (sliceCount-1);
|
||||||
float invStackCount = 1.f / (stackCount-1);
|
float invStackCount = 1.f / (stackCount-1);
|
||||||
|
|
||||||
const float pi = static_cast<float>(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)
|
for (unsigned int stack = 0; stack < stackCount; ++stack)
|
||||||
{
|
{
|
||||||
float stackVal = stack * invStackCount;
|
float stackVal = stack * invStackCount;
|
||||||
float stackValPi = stackVal * pi;
|
float stackValPi = stackVal * Pi<float>;
|
||||||
float sinStackValPi = std::sin(stackValPi);
|
float sinStackValPi = std::sin(stackValPi);
|
||||||
|
|
||||||
for (unsigned int slice = 0; slice < sliceCount; ++slice)
|
for (unsigned int slice = 0; slice < sliceCount; ++slice)
|
||||||
{
|
{
|
||||||
float sliceVal = slice * invSliceCount;
|
float sliceVal = slice * invSliceCount;
|
||||||
float sliceValPi2 = sliceVal * pi2;
|
float sliceValPi2 = sliceVal * 2.f * Pi<float>;
|
||||||
|
|
||||||
Vector3f normal;
|
Vector3f normal;
|
||||||
normal.y = std::sin(-pi_2 + stackValPi);
|
normal.y = std::sin(-HalfPi<float> + stackValPi);
|
||||||
normal.x = std::cos(sliceValPi2) * sinStackValPi;
|
normal.x = std::cos(sliceValPi2) * sinStackValPi;
|
||||||
normal.z = std::sin(sliceValPi2) * sinStackValPi;
|
normal.z = std::sin(sliceValPi2) * sinStackValPi;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ TEST_CASE("DegreeToRadian", "[MATH][ALGORITHM]")
|
||||||
{
|
{
|
||||||
SECTION("Convert 45.f degree to radian")
|
SECTION("Convert 45.f degree to radian")
|
||||||
{
|
{
|
||||||
REQUIRE(Nz::DegreeToRadian(45.f) == Approx(M_PI / 4));
|
REQUIRE(Nz::DegreeToRadian(45.f) == Approx(Nz::Pi<float> / 4.f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -323,7 +323,7 @@ TEST_CASE("RadianToDegree", "[MATH][ALGORITHM]")
|
||||||
{
|
{
|
||||||
SECTION("PI / 4 to degree")
|
SECTION("PI / 4 to degree")
|
||||||
{
|
{
|
||||||
REQUIRE(Nz::RadianToDegree(M_PI / 4) == Approx(45.f));
|
REQUIRE(Nz::RadianToDegree(Nz::Pi<float> / 4.f) == Approx(45.f));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ SCENARIO("Angle", "[MATH][ANGLE]")
|
||||||
|
|
||||||
THEN("It should be equal to pi/2")
|
THEN("It should be equal to pi/2")
|
||||||
{
|
{
|
||||||
Nz::RadianAnglef expectedResult(float(M_PI_2));
|
Nz::RadianAnglef expectedResult(Nz::HalfPi<float>);
|
||||||
|
|
||||||
CHECK(radAngle == expectedResult);
|
CHECK(radAngle == expectedResult);
|
||||||
CHECK(angle.ToRadianAngle() == 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<float>);
|
||||||
|
|
||||||
WHEN("We convert it to radians")
|
WHEN("We convert it to radians")
|
||||||
{
|
{
|
||||||
|
|
@ -172,7 +172,7 @@ SCENARIO("Angle", "[MATH][ANGLE]")
|
||||||
|
|
||||||
GIVEN("A radian angle of 7pi")
|
GIVEN("A radian angle of 7pi")
|
||||||
{
|
{
|
||||||
Nz::RadianAnglef angle(float(7 * M_PI));
|
Nz::RadianAnglef angle(7.f * Nz::Pi<float>);
|
||||||
|
|
||||||
WHEN("We normalize it")
|
WHEN("We normalize it")
|
||||||
{
|
{
|
||||||
|
|
@ -180,7 +180,7 @@ SCENARIO("Angle", "[MATH][ANGLE]")
|
||||||
|
|
||||||
THEN("It should be equal to a normalized version of itself")
|
THEN("It should be equal to a normalized version of itself")
|
||||||
{
|
{
|
||||||
Nz::RadianAnglef expectedResult(float(M_PI));
|
Nz::RadianAnglef expectedResult(Nz::Pi<float>);
|
||||||
|
|
||||||
CHECK(angle == expectedResult);
|
CHECK(angle == expectedResult);
|
||||||
}
|
}
|
||||||
|
|
@ -189,7 +189,7 @@ SCENARIO("Angle", "[MATH][ANGLE]")
|
||||||
|
|
||||||
GIVEN("A radian angle of -4pi")
|
GIVEN("A radian angle of -4pi")
|
||||||
{
|
{
|
||||||
Nz::RadianAnglef angle(float(-4 * M_PI));
|
Nz::RadianAnglef angle(-4.f * Nz::Pi<float>);
|
||||||
|
|
||||||
WHEN("We normalize it")
|
WHEN("We normalize it")
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue