From c0a5e5dadd339fae9ca35666b2c0ba5762d0ddfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 21 Feb 2013 00:37:55 +0100 Subject: [PATCH] Math: added sin(), cos() and tan() accepting either Rad or Deg. --- src/Math/Functions.h | 16 ++++++++++++++++ src/Math/Test/FunctionsTest.cpp | 17 ++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Math/Functions.h b/src/Math/Functions.h index 7db0b70dc..a887121a8 100644 --- a/src/Math/Functions.h +++ b/src/Math/Functions.h @@ -71,6 +71,22 @@ std::uint32_t MAGNUM_EXPORT log2(std::uint32_t number); */ std::uint32_t MAGNUM_EXPORT log(std::uint32_t base, std::uint32_t number); +/** @brief Sine */ +template inline T sin(Rad angle) { return std::sin(T(angle)); } + +/** @brief Cosine */ +template inline T cos(Rad angle) { return std::cos(T(angle)); } + +/** @brief Tangent */ +template inline T tan(Rad angle) { return std::tan(T(angle)); } + +/** @todo Can't trigonometric functions be done with only one overload? */ +#ifndef DOXYGEN_GENERATING_OUTPUT +template inline T sin(Deg angle) { return sin(Rad(angle)); } +template inline T cos(Deg angle) { return cos(Rad(angle)); } +template inline T tan(Deg angle) { return tan(Rad(angle)); } +#endif + /** @{ @name Scalar/vector functions diff --git a/src/Math/Test/FunctionsTest.cpp b/src/Math/Test/FunctionsTest.cpp index 6fc7ea819..70d261c66 100644 --- a/src/Math/Test/FunctionsTest.cpp +++ b/src/Math/Test/FunctionsTest.cpp @@ -40,8 +40,12 @@ class FunctionsTest: public Corrade::TestSuite::Tester { void pow(); void log(); void log2(); + void trigonometric(); }; +typedef Math::Constants Constants; +typedef Math::Deg Deg; +typedef Math::Rad Rad; typedef Math::Vector3 Vector3; typedef Math::Vector3 Vector3ub; typedef Math::Vector3 Vector3b; @@ -60,9 +64,11 @@ FunctionsTest::FunctionsTest() { &FunctionsTest::denormalizeSigned, &FunctionsTest::renormalizeUnsinged, &FunctionsTest::renormalizeSinged, + &FunctionsTest::pow, &FunctionsTest::log, - &FunctionsTest::log2); + &FunctionsTest::log2, + &FunctionsTest::trigonometric); } void FunctionsTest::min() { @@ -242,6 +248,15 @@ void FunctionsTest::log2() { CORRADE_COMPARE(Math::log2(2153), 11); } +void FunctionsTest::trigonometric() { + CORRADE_COMPARE(Math::sin(Deg(30.0f)), 0.5f); + CORRADE_COMPARE(Math::sin(Rad(Constants::pi()/6)), 0.5f); + CORRADE_COMPARE(Math::cos(Deg(60.0f)), 0.5f); + CORRADE_COMPARE(Math::cos(Rad(Constants::pi()/3)), 0.5f); + CORRADE_COMPARE(Math::tan(Deg(45.0f)), 1.0f); + CORRADE_COMPARE(Math::tan(Rad(Constants::pi()/4)), 1.0f); +} + }}} CORRADE_TEST_MAIN(Magnum::Math::Test::FunctionsTest)