Browse Source

Math: ability to join two ranges.

I had to include Functions.h, there was no other way to do this. I have
to schedule include cleanup soon.
pull/136/head
Vladimír Vondruš 10 years ago
parent
commit
da9e86fc77
  1. 14
      src/Magnum/Math/Range.h
  2. 16
      src/Magnum/Math/Test/RangeTest.cpp

14
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 T> 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<UnsignedInt dimensions, class T> inline Range<dimensions, T> join(const Range<dimensions, T>& a, const Range<dimensions, T>& 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<UnsignedInt dimensions, class T> Corrade::Utility::Debug& operator<<(Corrade::Utility::Debug& debug, const Range<dimensions, T>& value) {
debug << "Range({" << Corrade::Utility::Debug::nospace << value.min()[0];

16
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 T> class BasicRect: public Math::Range<2, T> {
public:
/* MSVC 2015 can't handle {} here */

Loading…
Cancel
Save