diff --git a/src/Math/Functions.h b/src/Math/Functions.h index 1cdb809fb..25f6776be 100644 --- a/src/Math/Functions.h +++ b/src/Math/Functions.h @@ -211,6 +211,51 @@ template Vector abs(const Vector& a } #endif +/** @brief Nearest not larger integer */ +#ifdef DOXYGEN_GENERATING_OUTPUT +template inline T floor(const T& a); +#else +template inline typename std::enable_if::value, T>::type floor(T a) { + return std::floor(a); +} +template Vector floor(const Vector& a) { + Vector out; + for(std::size_t i = 0; i != size; ++i) + out[i] = std::floor(a[i]); + return out; +} +#endif + +/** @brief Round value to nearest integer */ +#ifdef DOXYGEN_GENERATING_OUTPUT +template inline T round(const T& a); +#else +template inline typename std::enable_if::value, T>::type round(T a) { + return std::round(a); +} +template Vector round(const Vector& a) { + Vector out; + for(std::size_t i = 0; i != size; ++i) + out[i] = std::round(a[i]); + return out; +} +#endif + +/** @brief Nearest not smaller integer */ +#ifdef DOXYGEN_GENERATING_OUTPUT +template inline T ceil(const T& a); +#else +template inline typename std::enable_if::value, T>::type ceil(T a) { + return std::ceil(a); +} +template Vector ceil(const Vector& a) { + Vector out; + for(std::size_t i = 0; i != size; ++i) + out[i] = std::ceil(a[i]); + return out; +} +#endif + /** @brief Square root diff --git a/src/Math/Test/FunctionsTest.cpp b/src/Math/Test/FunctionsTest.cpp index 2d83cf349..1b86f1985 100644 --- a/src/Math/Test/FunctionsTest.cpp +++ b/src/Math/Test/FunctionsTest.cpp @@ -39,6 +39,11 @@ class FunctionsTest: public Corrade::TestSuite::Tester { void maxList(); void sign(); void abs(); + + void floor(); + void round(); + void ceil(); + void sqrt(); void sqrtInverted(); void clamp(); @@ -76,6 +81,11 @@ FunctionsTest::FunctionsTest() { &FunctionsTest::maxList, &FunctionsTest::sign, &FunctionsTest::abs, + + &FunctionsTest::floor, + &FunctionsTest::round, + &FunctionsTest::ceil, + &FunctionsTest::sqrt, &FunctionsTest::sqrtInverted, &FunctionsTest::clamp, @@ -135,6 +145,21 @@ void FunctionsTest::abs() { CORRADE_COMPARE(Math::abs(Vector3i(5, -3, 2)), Vector3i(5, 3, 2)); } +void FunctionsTest::floor() { + CORRADE_COMPARE(Math::floor(0.7f), 0.0f); + CORRADE_COMPARE(Math::floor(Vector3(2.3f, 0.7f, 1.5f)), Vector3(2.0f, 0.0f, 1.0f)); +} + +void FunctionsTest::round() { + CORRADE_COMPARE(Math::round(2.3f), 2.0f); + CORRADE_COMPARE(Math::round(Vector3(2.3f, 0.7f, 1.5f)), Vector3(2.0f, 1.0f, 2.0f)); +} + +void FunctionsTest::ceil() { + CORRADE_COMPARE(Math::ceil(2.3f), 3.0f); + CORRADE_COMPARE(Math::ceil(Vector3(2.3f, 0.7f, 1.5f)), Vector3(3.0f, 1.0f, 2.0f)); +} + void FunctionsTest::sqrt() { CORRADE_COMPARE(Math::sqrt(16), 4); CORRADE_COMPARE(Math::sqrt(Vector3i(256, 1, 0)), Vector3i(16, 1, 0));