From 60f86b8ea0c2bfe1c7015bd68ae84cd29388798c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 30 Jan 2013 14:36:45 +0100 Subject: [PATCH] Math: added Vector::{min,max}Abs(). --- src/Math/Test/VectorTest.cpp | 16 ++++++++++++++++ src/Math/Vector.h | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index adaa4f166..ba53288cc 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -48,7 +48,9 @@ class VectorTest: public Corrade::TestSuite::Tester { void sum(); void product(); void min(); + void minAbs(); void max(); + void maxAbs(); void projected(); void angle(); @@ -85,7 +87,9 @@ VectorTest::VectorTest() { &VectorTest::sum, &VectorTest::product, &VectorTest::min, + &VectorTest::minAbs, &VectorTest::max, + &VectorTest::maxAbs, &VectorTest::projected, &VectorTest::angle, @@ -217,11 +221,23 @@ void VectorTest::min() { CORRADE_COMPARE(Vector3(1.0f, -2.0f, 3.0f).min(), -2.0f); } +void VectorTest::minAbs() { + /* Check that initial value is absolute and also all others */ + CORRADE_COMPARE(Vector3(-2.0f, 1.0f, 3.0f).minAbs(), 1.0f); + CORRADE_COMPARE(Vector3(1.0f, -2.0f, 3.0f).minAbs(), 1.0f); +} + void VectorTest::max() { /* Check also that initial value isn't initialized to 0 */ CORRADE_COMPARE(Vector3(-1.0f, -2.0f, -3.0f).max(), -1.0f); } +void VectorTest::maxAbs() { + /* Check that initial value is absolute and also all others */ + CORRADE_COMPARE(Vector3(-5.0f, 1.0f, 3.0f).maxAbs(), 5.0f); + CORRADE_COMPARE(Vector3(1.0f, -5.0f, 3.0f).maxAbs(), 5.0f); +} + void VectorTest::projected() { Vector3 line(1.0f, -1.0f, 0.5f); Vector3 projected = Vector3(1.0f, 2.0f, 3.0f).projected(line); diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 58d9e6d1d..f05b2ff6f 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -483,6 +483,16 @@ template class Vector { return out; } + /** @brief Minimal absolute value in the vector */ + T minAbs() const { + T out(std::abs(_data[0])); + + for(std::size_t i = 1; i != size; ++i) + out = std::min(out, std::abs(_data[i])); + + return out; + } + /** @brief Maximal value in the vector */ T max() const { T out(_data[0]); @@ -493,6 +503,16 @@ template class Vector { return out; } + /** @brief Maximal absolute value in the vector */ + T maxAbs() const { + T out(std::abs(_data[0])); + + for(std::size_t i = 1; i != size; ++i) + out = std::max(out, std::abs(_data[i])); + + return out; + } + private: /* Implementation for Vector::Vector(const Vector&) */ template inline constexpr explicit Vector(Implementation::Sequence, const Vector& vector): _data{T(vector.data()[sequence])...} {}