Browse Source

Math: implemented minmax() for arbitrary vectors.

pull/193/head
Vladimír Vondruš 9 years ago
parent
commit
353d7ff59d
  1. 2
      src/Magnum/Math/Functions.h
  2. 8
      src/Magnum/Math/Test/Vector2Test.cpp
  3. 11
      src/Magnum/Math/Test/VectorTest.cpp
  4. 24
      src/Magnum/Math/Vector.h
  5. 9
      src/Magnum/Math/Vector2.h

2
src/Magnum/Math/Functions.h

@ -315,7 +315,7 @@ template<class T> inline T max(std::initializer_list<T> 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<class T> inline std::pair<T, T> minmax(const T& a, const T& b);

8
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);

11
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);

24
src/Magnum/Math/Vector.h

@ -563,17 +563,24 @@ template<std::size_t size, class T> 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<T, T> minmax() const;
private:
/* Implementation for Vector<size, T>::Vector(const Vector<size, U>&) */
template<class U, std::size_t ...sequence> constexpr explicit Vector(Implementation::Sequence<sequence...>, const Vector<size, U>& vector) noexcept: _data{T(vector._data[sequence])...} {}
@ -1362,6 +1369,19 @@ template<std::size_t size, class T> inline T Vector<size, T>::max() const {
return out;
}
template<std::size_t size, class T> inline std::pair<T, T> Vector<size, T>::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 {

9
src/Magnum/Math/Vector2.h

@ -183,15 +183,6 @@ template<class T> 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<T, T> minmax() const {
return x() < y() ? std::make_pair(x(), y()) : std::make_pair(y(), x());
}
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(2, Vector2)
};

Loading…
Cancel
Save