diff --git a/src/Magnum/Math/Functions.h b/src/Magnum/Math/Functions.h index 346225a05..034af3e60 100644 --- a/src/Magnum/Math/Functions.h +++ b/src/Magnum/Math/Functions.h @@ -315,7 +315,7 @@ template inline T max(std::initializer_list list) { /** @brief Minimum and maximum of two values -@see @ref min(), @ref max(), @ref clamp(), @ref Vector2::minmax() +@see @ref min(), @ref max(), @ref clamp(), @ref Vector::minmax() */ #ifdef DOXYGEN_GENERATING_OUTPUT template inline std::pair minmax(const T& a, const T& b); diff --git a/src/Magnum/Math/Test/Vector2Test.cpp b/src/Magnum/Math/Test/Vector2Test.cpp index 964dc4091..31d855441 100644 --- a/src/Magnum/Math/Test/Vector2Test.cpp +++ b/src/Magnum/Math/Test/Vector2Test.cpp @@ -68,7 +68,6 @@ struct Vector2Test: Corrade::TestSuite::Tester { void scales(); void perpendicular(); void aspectRatio(); - void minmax(); void swizzleType(); void debug(); @@ -94,7 +93,6 @@ Vector2Test::Vector2Test() { &Vector2Test::scales, &Vector2Test::perpendicular, &Vector2Test::aspectRatio, - &Vector2Test::minmax, &Vector2Test::swizzleType, &Vector2Test::debug, @@ -228,12 +226,6 @@ void Vector2Test::aspectRatio() { CORRADE_COMPARE(Vector2(3.0f, 4.0f).aspectRatio(), 0.75f); } -void Vector2Test::minmax() { - const auto expected = std::make_pair(-5.0f, 4.0f); - CORRADE_COMPARE(Vector2(-5.0f, 4.0f).minmax(), expected); - CORRADE_COMPARE(Vector2(4.0f, -5.0f).minmax(), expected); -} - void Vector2Test::swizzleType() { constexpr Vector<4, Int> orig; constexpr auto a = swizzle<'y', 'a'>(orig); diff --git a/src/Magnum/Math/Test/VectorTest.cpp b/src/Magnum/Math/Test/VectorTest.cpp index 85eac12ba..5adbd4383 100644 --- a/src/Magnum/Math/Test/VectorTest.cpp +++ b/src/Magnum/Math/Test/VectorTest.cpp @@ -94,6 +94,7 @@ struct VectorTest: Corrade::TestSuite::Tester { void product(); void min(); void max(); + void minmax(); void projected(); void projectedOntoNormalized(); @@ -460,6 +461,16 @@ void VectorTest::max() { CORRADE_COMPARE(Vector3(-1.0f, -2.0f, -3.0f).max(), -1.0f); } +void VectorTest::minmax() { + const auto expected = std::make_pair(-3.0f, 2.0f); + CORRADE_COMPARE((Vector3{-1.0f, 2.0f, -3.0f}.minmax()), expected); + CORRADE_COMPARE((Vector3{-1.0f, -3.0f, 2.0f}.minmax()), expected); + CORRADE_COMPARE((Vector3{2.0f, -1.0f, -3.0f}.minmax()), expected); + CORRADE_COMPARE((Vector3{2.0f, -3.0f, -1.0f}.minmax()), expected); + CORRADE_COMPARE((Vector3{-3.0f, 2.0f, -1.0f}.minmax()), expected); + CORRADE_COMPARE((Vector3{-3.0f, -1.0f, 2.0f}.minmax()), expected); +} + 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/Magnum/Math/Vector.h b/src/Magnum/Math/Vector.h index ec8533c53..173b5d9e4 100644 --- a/src/Magnum/Math/Vector.h +++ b/src/Magnum/Math/Vector.h @@ -563,17 +563,24 @@ template class Vector { /** * @brief Minimal value in the vector * - * @see @ref Math::min(), @ref Vector2::minmax() + * @see @ref Math::min(), @ref minmax() */ T min() const; /** * @brief Maximal value in the vector * - * @see @ref Math::max(), @ref Vector2::minmax() + * @see @ref Math::max(), @ref minmax() */ T max() const; + /** + * @brief Minimal and maximal value in the vector + * + * @see @ref min(), @ref max(), @ref Math::minmax() + */ + std::pair minmax() const; + private: /* Implementation for Vector::Vector(const Vector&) */ template constexpr explicit Vector(Implementation::Sequence, const Vector& vector) noexcept: _data{T(vector._data[sequence])...} {} @@ -1362,6 +1369,19 @@ template inline T Vector::max() const { return out; } +template inline std::pair Vector::minmax() const { + T min{_data[0]}, max{_data[0]}; + + for(std::size_t i = 1; i != size; ++i) { + if(_data[i] < min) + min = _data[i]; + else if(_data[i] > max) + max = _data[i]; + } + + return {min, max}; +} + }} namespace Corrade { namespace Utility { diff --git a/src/Magnum/Math/Vector2.h b/src/Magnum/Math/Vector2.h index d1870107d..4987747cb 100644 --- a/src/Magnum/Math/Vector2.h +++ b/src/Magnum/Math/Vector2.h @@ -183,15 +183,6 @@ template class Vector2: public Vector<2, T> { */ T aspectRatio() const { return x()/y(); } - /** - * @brief Minimum and maximum value - * - * @see @ref min(), @ref max(), @ref Math::minmax() - */ - std::pair minmax() const { - return x() < y() ? std::make_pair(x(), y()) : std::make_pair(y(), x()); - } - MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(2, Vector2) };