Skip to content

Commit

Permalink
Add ToDegrees() and ToRadians() overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
mogemimi committed Feb 7, 2016
1 parent 6e1b35f commit d8c5e0b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
22 changes: 18 additions & 4 deletions include/Pomdog/Math/MathHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "Pomdog/Math/Degree.hpp"
#include "Pomdog/Math/Radian.hpp"
#include <cstdint>
#include <cfloat>
#include <type_traits>

namespace Pomdog {
Expand Down Expand Up @@ -95,20 +93,36 @@ T SmoothStep(T const& min, T const& max, T const& amount)
}
//-------------------------------------------------------------------
template <typename T>
Radian<T> ToRadians(Degree<T> const& degrees) noexcept
Radian<T> ToRadians(const Degree<T>& degrees) noexcept
{
static_assert(std::is_floating_point<T>::value, "");
constexpr auto scaleFactor = MathConstants<T>::Pi() * (T{1} / T{180});
return Radian<T>(degrees.value * scaleFactor);
}
//-------------------------------------------------------------------
template <typename T>
Degree<T> ToDegrees(Radian<T> const& radians) noexcept
Radian<T> ToRadians(const T& degrees) noexcept
{
static_assert(std::is_floating_point<T>::value, "");
constexpr auto scaleFactor = MathConstants<T>::Pi() * (T{1} / T{180});
return Radian<T>(degrees * scaleFactor);
}
//-------------------------------------------------------------------
template <typename T>
Degree<T> ToDegrees(const Radian<T>& radians) noexcept
{
static_assert(std::is_floating_point<T>::value, "");
constexpr auto scaleFactor = T{180} * (T{1} / MathConstants<T>::Pi());
return Degree<T>(radians.value * scaleFactor);
}
//-------------------------------------------------------------------
template <typename T>
Degree<T> ToDegrees(const T& radians) noexcept
{
static_assert(std::is_floating_point<T>::value, "");
constexpr auto scaleFactor = T{180} * (T{1} / MathConstants<T>::Pi());
return Degree<T>(radians * scaleFactor);
}

} // namespace MathHelper
} // namespace Pomdog
34 changes: 34 additions & 0 deletions test/FrameworkTest/Math/MathHelperTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <gtest/iutest_switch.hpp>

namespace MathHelper = Pomdog::MathHelper;
using Pomdog::Degree;
using Pomdog::Radian;

TEST(MathHelper, Clamp)
{
Expand Down Expand Up @@ -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<float>(0.0f)).value, epsilon);
EXPECT_NEAR(45.0f, MathHelper::ToDegrees(Radian<float>(0.7853981633f)).value, epsilon);
EXPECT_NEAR(90.0f, MathHelper::ToDegrees(Radian<float>(1.5707963267f)).value, epsilon);
EXPECT_NEAR(180.0f, MathHelper::ToDegrees(Radian<float>(3.1415926535f)).value, epsilon);
EXPECT_NEAR(360.0f, MathHelper::ToDegrees(Radian<float>(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<float>(0.0f)).value, epsilon);
EXPECT_NEAR(0.7853981633f, MathHelper::ToRadians(Degree<float>(45.0f)).value, epsilon);
EXPECT_NEAR(1.5707963267f, MathHelper::ToRadians(Degree<float>(90.0f)).value, epsilon);
EXPECT_NEAR(3.1415926535f, MathHelper::ToRadians(Degree<float>(180.0f)).value, epsilon);
EXPECT_NEAR(6.2831853071f, MathHelper::ToRadians(Degree<float>(360.0f)).value, epsilon);
}

0 comments on commit d8c5e0b

Please sign in to comment.