From e7cae67e9f4be256ae2e55d5b9136d2c6f20c8de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 24 Oct 2015 20:45:33 +0200 Subject: [PATCH] Math: added sincos(). Mainly a convenience function in case you want to compute sin and cos of the same, potentially longer expression, and you don't want to have repeated code or temporary variables. On some architectures might use faster instruction that computes both values in one shot. --- src/Magnum/Math/Functions.h | 27 ++++++++++++++++++++++++-- src/Magnum/Math/Test/FunctionsTest.cpp | 6 ++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Magnum/Math/Functions.h b/src/Magnum/Math/Functions.h index 2062f9af3..e570f3bfa 100644 --- a/src/Magnum/Math/Functions.h +++ b/src/Magnum/Math/Functions.h @@ -104,7 +104,11 @@ template std::pair div(Integral x, Integral /* The functions accept Unit instead of Rad to make them working with operator products (e.g. 2*35.0_degf, which is of type Unit) */ -/** @brief Sine */ +/** +@brief Sine + +@see @ref sincos() +*/ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T sin(Rad angle); #else @@ -112,7 +116,11 @@ template inline T sin(Unit angle) { return std::sin(T(angle)); template inline T sin(Unit angle) { return sin(Rad(angle)); } #endif -/** @brief Cosine */ +/** +@brief Cosine + +@see @ref sincos() +*/ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T cos(Rad angle); #else @@ -120,6 +128,21 @@ template inline T cos(Unit angle) { return std::cos(T(angle)); template inline T cos(Unit angle) { return cos(Rad(angle)); } #endif +/** +@brief Sine and cosine + +On some architectures might be faster than doing both computations separately. +@see @ref sin(), @ref cos() +*/ +#ifdef DOXYGEN_GENERATING_OUTPUT +template inline std::pair sincos(Rad angle); +#else +template inline std::pair sincos(Unit angle) { + return {std::sin(T(angle)) ,std::cos(T(angle))}; +} +template inline std::pair sincos(Unit angle) { return sincos(Rad(angle)); } +#endif + /** @brief Tangent */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline T tan(Rad angle); diff --git a/src/Magnum/Math/Test/FunctionsTest.cpp b/src/Magnum/Math/Test/FunctionsTest.cpp index fe62e6391..3fcf635c8 100644 --- a/src/Magnum/Math/Test/FunctionsTest.cpp +++ b/src/Magnum/Math/Test/FunctionsTest.cpp @@ -453,6 +453,9 @@ void FunctionsTest::trigonometric() { CORRADE_COMPARE(Math::cos(Rad(Constants::pi()/3)), 0.5f); CORRADE_COMPARE_AS(Math::acos(0.5f), Deg(60.0f), Deg); + CORRADE_COMPARE(Math::sincos(Deg(30.0f)), std::make_pair(0.5f, 0.8660254037844386f)); + CORRADE_COMPARE(Math::sincos(Rad(Constants::pi()/6)), std::make_pair(0.5f, 0.8660254037844386f)); + CORRADE_COMPARE(Math::tan(Deg(45.0f)), 1.0f); CORRADE_COMPARE(Math::tan(Rad(Constants::pi()/4)), 1.0f); CORRADE_COMPARE_AS(Math::atan(1.0f), Deg(45.0f), Deg); @@ -469,6 +472,9 @@ void FunctionsTest::trigonometricWithBase() { CORRADE_COMPARE(Math::cos(2*Deg(30.0f)), 0.5f); CORRADE_COMPARE(Math::cos(2*Rad(Constants::pi()/6)), 0.5f); + CORRADE_COMPARE(Math::sincos(2*Deg(15.0f)), std::make_pair(0.5f, 0.8660254037844386f)); + CORRADE_COMPARE(Math::sincos(2*Rad(Constants::pi()/12)), std::make_pair(0.5f, 0.8660254037844386f)); + CORRADE_COMPARE(Math::tan(2*Deg(22.5f)), 1.0f); CORRADE_COMPARE(Math::tan(2*Rad(Constants::pi()/8)), 1.0f); }