diff --git a/src/Magnum/Math/Range.h b/src/Magnum/Math/Range.h index 7b79214fb..2a5702321 100644 --- a/src/Magnum/Math/Range.h +++ b/src/Magnum/Math/Range.h @@ -29,6 +29,7 @@ * @brief Class @ref Magnum::Math::Range, @ref Magnum::Math::Range2D, @ref Magnum::Math::Range3D, alias @ref Magnum::Math::Range1D */ +#include "Magnum/Math/Functions.h" #include "Magnum/Math/Vector3.h" namespace Magnum { namespace Math { @@ -523,6 +524,19 @@ template class Range3D: public Range<3, T> { MAGNUM_RANGE_SUBCLASS_IMPLEMENTATION(3, Range3D, Vector3) }; +/** @relates Range +@brief Join two ranges + +Returns a range that contains both input ranges. If one of the ranges is empty, +only the other is returned. Results are undefined if any range has negative +size. +*/ +template inline Range join(const Range& a, const Range& b) { + if(a.min() == a.max()) return b; + if(b.min() == b.max()) return a; + return {min(a.min(), b.min()), max(a.max(), b.max())}; +} + /** @debugoperator{Magnum::Math::Range} */ template Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Range& value) { debug << "Range({" << Corrade::Utility::Debug::nospace << value.min()[0]; diff --git a/src/Magnum/Math/Test/RangeTest.cpp b/src/Magnum/Math/Test/RangeTest.cpp index 0973ab36f..f6938ef51 100644 --- a/src/Magnum/Math/Test/RangeTest.cpp +++ b/src/Magnum/Math/Test/RangeTest.cpp @@ -123,6 +123,8 @@ struct RangeTest: Corrade::TestSuite::Tester { void padded(); void scaled(); + void join(); + void subclassTypes(); void subclass(); @@ -157,6 +159,8 @@ RangeTest::RangeTest() { &RangeTest::padded, &RangeTest::scaled, + &RangeTest::join, + &RangeTest::subclassTypes, &RangeTest::subclass, @@ -446,6 +450,18 @@ void RangeTest::scaled() { CORRADE_COMPARE(a.scaled({2, -3}), b); } +void RangeTest::join() { + Range2Di a{{12, 20}, {15, 35}}; + Range2Di b{{10, 25}, {17, 105}}; + Range2Di c{{130, -15}, {130, -15}}; + Range2Di d{{10, 20}, {17, 105}}; + + CORRADE_COMPARE(Math::join(a, b), d); + CORRADE_COMPARE(Math::join(b, a), d); + CORRADE_COMPARE(Math::join(a, c), a); + CORRADE_COMPARE(Math::join(c, a), a); +} + template class BasicRect: public Math::Range<2, T> { public: /* MSVC 2015 can't handle {} here */