From 8a639391348cd7f995433a191fd9646195f59e69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 3 Feb 2013 17:00:31 +0100 Subject: [PATCH] Math: added vector/scalar abs() function. --- src/Math/Functions.h | 15 +++++++++++++++ src/Math/Test/FunctionsTest.cpp | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/src/Math/Functions.h b/src/Math/Functions.h index fe767f915..76bff57b4 100644 --- a/src/Math/Functions.h +++ b/src/Math/Functions.h @@ -117,6 +117,21 @@ template Vector max(const Vector& a } #endif +/** @brief Absolute value */ +#ifdef DOXYGEN_GENERATING_OUTPUT +template inline T abs(const T& a); +#else +template inline typename std::enable_if::value, T>::type abs(T a) { + return std::abs(a); +} +template Vector abs(const Vector& a) { + Vector out; + for(std::size_t i = 0; i != size; ++i) + out[i] = std::abs(a[i]); + return out; +} +#endif + /** @brief Clamp value diff --git a/src/Math/Test/FunctionsTest.cpp b/src/Math/Test/FunctionsTest.cpp index f71362c65..dd88b08cc 100644 --- a/src/Math/Test/FunctionsTest.cpp +++ b/src/Math/Test/FunctionsTest.cpp @@ -26,6 +26,7 @@ class FunctionsTest: public Corrade::TestSuite::Tester { void min(); void max(); + void abs(); void clamp(); void normalizeUnsigned(); void normalizeSigned(); @@ -47,6 +48,7 @@ typedef Math::Vector3 Vector3i; FunctionsTest::FunctionsTest() { addTests(&FunctionsTest::min, &FunctionsTest::max, + &FunctionsTest::abs, &FunctionsTest::clamp, &FunctionsTest::normalizeUnsigned, &FunctionsTest::normalizeSigned, @@ -69,6 +71,12 @@ void FunctionsTest::max() { CORRADE_COMPARE(Math::max(Vector3i(5, -3, 2), Vector3i(9, -5, 18)), Vector3i(9, -3, 18)); } +void FunctionsTest::abs() { + CORRADE_COMPARE(Math::abs(-5), 5); + CORRADE_COMPARE(Math::abs(5), 5); + CORRADE_COMPARE(Math::abs(Vector3i(5, -3, 2)), Vector3i(5, 3, 2)); +} + void FunctionsTest::clamp() { CORRADE_COMPARE(Math::clamp(0.5f, -1.0f, 5.0f), 0.5f); CORRADE_COMPARE(Math::clamp(-1.6f, -1.0f, 5.0f), -1.0f);