diff --git a/include/Pomdog/Math/MathHelper.hpp b/include/Pomdog/Math/MathHelper.hpp index 2a6edfeb3..10925f003 100644 --- a/include/Pomdog/Math/MathHelper.hpp +++ b/include/Pomdog/Math/MathHelper.hpp @@ -4,8 +4,6 @@ #include "Pomdog/Math/Degree.hpp" #include "Pomdog/Math/Radian.hpp" -#include -#include #include namespace Pomdog { @@ -95,7 +93,7 @@ T SmoothStep(T const& min, T const& max, T const& amount) } //------------------------------------------------------------------- template -Radian ToRadians(Degree const& degrees) noexcept +Radian ToRadians(const Degree& degrees) noexcept { static_assert(std::is_floating_point::value, ""); constexpr auto scaleFactor = MathConstants::Pi() * (T{1} / T{180}); @@ -103,12 +101,28 @@ Radian ToRadians(Degree const& degrees) noexcept } //------------------------------------------------------------------- template -Degree ToDegrees(Radian const& radians) noexcept +Radian ToRadians(const T& degrees) noexcept +{ + static_assert(std::is_floating_point::value, ""); + constexpr auto scaleFactor = MathConstants::Pi() * (T{1} / T{180}); + return Radian(degrees * scaleFactor); +} +//------------------------------------------------------------------- +template +Degree ToDegrees(const Radian& radians) noexcept { static_assert(std::is_floating_point::value, ""); constexpr auto scaleFactor = T{180} * (T{1} / MathConstants::Pi()); return Degree(radians.value * scaleFactor); } +//------------------------------------------------------------------- +template +Degree ToDegrees(const T& radians) noexcept +{ + static_assert(std::is_floating_point::value, ""); + constexpr auto scaleFactor = T{180} * (T{1} / MathConstants::Pi()); + return Degree(radians * scaleFactor); +} } // namespace MathHelper } // namespace Pomdog diff --git a/test/FrameworkTest/Math/MathHelperTest.cpp b/test/FrameworkTest/Math/MathHelperTest.cpp index da4bbecf2..b3f139985 100644 --- a/test/FrameworkTest/Math/MathHelperTest.cpp +++ b/test/FrameworkTest/Math/MathHelperTest.cpp @@ -4,6 +4,8 @@ #include namespace MathHelper = Pomdog::MathHelper; +using Pomdog::Degree; +using Pomdog::Radian; TEST(MathHelper, Clamp) { @@ -110,3 +112,35 @@ TEST(MathHelper, SmoothStep) EXPECT_LT(MathHelper::SmoothStep(0.0f, 1.0f, 0.8f), MathHelper::SmoothStep(0.0f, 1.0f, 0.9f)); EXPECT_LT(MathHelper::SmoothStep(0.0f, 1.0f, 0.9f), MathHelper::SmoothStep(0.0f, 1.0f, 1.0f)); } + +TEST(MathHelper, ToDegrees) +{ + constexpr auto epsilon = 0.0000001f; + EXPECT_NEAR(0.0f, MathHelper::ToDegrees(0.0f).value, epsilon); + EXPECT_NEAR(45.0f, MathHelper::ToDegrees(0.7853981633f).value, epsilon); + EXPECT_NEAR(90.0f, MathHelper::ToDegrees(1.5707963267f).value, epsilon); + EXPECT_NEAR(180.0f, MathHelper::ToDegrees(3.1415926535f).value, epsilon); + EXPECT_NEAR(360.0f, MathHelper::ToDegrees(6.2831853071f).value, epsilon); + + EXPECT_NEAR(0.0f, MathHelper::ToDegrees(Radian(0.0f)).value, epsilon); + EXPECT_NEAR(45.0f, MathHelper::ToDegrees(Radian(0.7853981633f)).value, epsilon); + EXPECT_NEAR(90.0f, MathHelper::ToDegrees(Radian(1.5707963267f)).value, epsilon); + EXPECT_NEAR(180.0f, MathHelper::ToDegrees(Radian(3.1415926535f)).value, epsilon); + EXPECT_NEAR(360.0f, MathHelper::ToDegrees(Radian(6.2831853071f)).value, epsilon); +} + +TEST(MathHelper, ToRadians) +{ + constexpr auto epsilon = 0.0000005f; + EXPECT_NEAR(0.0f, MathHelper::ToRadians(0.0f).value, epsilon); + EXPECT_NEAR(0.7853981633f, MathHelper::ToRadians(45.0f).value, epsilon); + EXPECT_NEAR(1.5707963267f, MathHelper::ToRadians(90.0f).value, epsilon); + EXPECT_NEAR(3.1415926535f, MathHelper::ToRadians(180.0f).value, epsilon); + EXPECT_NEAR(6.2831853071f, MathHelper::ToRadians(360.0f).value, epsilon); + + EXPECT_NEAR(0.0f, MathHelper::ToRadians(Degree(0.0f)).value, epsilon); + EXPECT_NEAR(0.7853981633f, MathHelper::ToRadians(Degree(45.0f)).value, epsilon); + EXPECT_NEAR(1.5707963267f, MathHelper::ToRadians(Degree(90.0f)).value, epsilon); + EXPECT_NEAR(3.1415926535f, MathHelper::ToRadians(Degree(180.0f)).value, epsilon); + EXPECT_NEAR(6.2831853071f, MathHelper::ToRadians(Degree(360.0f)).value, epsilon); +}