Browse Source

Math: added Range::fromCenter().

pull/273/merge
Vladimír Vondruš 8 years ago
parent
commit
bff7ae8d7e
  1. 2
      doc/changelog.dox
  2. 8
      doc/snippets/MagnumMath.cpp
  3. 19
      src/Magnum/Math/Range.h
  4. 8
      src/Magnum/Math/Test/RangeTest.cpp

2
doc/changelog.dox

@ -73,6 +73,8 @@ See also:
to @ref Math::join() for operating with @ref Math::Range instances to @ref Math::join() for operating with @ref Math::Range instances
- Added @ref Math::Range::contains() overloading taking a range, in addition - Added @ref Math::Range::contains() overloading taking a range, in addition
to vector to vector
- Added @ref Math::Range::fromCenter() and documented a hint how to create
integral centered ranges
- @ref Math::Range is now constructible from a @ref std::pair of values, - @ref Math::Range is now constructible from a @ref std::pair of values,
making it usable in combination with @ref Math::minmax(), for example making it usable in combination with @ref Math::minmax(), for example
- Added @ref Math::Constants::piQuarter() - Added @ref Math::Constants::piQuarter()

8
doc/snippets/MagnumMath.cpp

@ -867,4 +867,12 @@ Range3D bounds{Math::minmax({a, b, c})};
static_cast<void>(bounds); static_cast<void>(bounds);
} }
{
/* [Range-fromCenter-integer] */
Vector2i center, filterRadius;
auto filterArea = Range2Di::fromSize(center, Vector2i{1}).padded(filterRadius);
/* [Range-fromCenter-integer] */
static_cast<void>(filterArea);
}
} }

19
src/Magnum/Math/Range.h

@ -72,6 +72,20 @@ template<UnsignedInt dimensions, class T> class Range {
return {min, min+size}; return {min, min+size};
} }
/**
* @brief Create a range from center and half size
* @param center Range center
* @param halfSize Half size
*
* For creating integer center ranges you can use @ref fromSize()
* together with @ref padded(), for example:
*
* @snippet MagnumMath.cpp Range-fromCenter-integer
*/
static Range<dimensions, T> fromCenter(const VectorType& center, const VectorType& halfSize) {
return {center - halfSize, center + halfSize};
}
/** /**
* @brief Construct zero range * @brief Construct zero range
* *
@ -197,7 +211,7 @@ template<UnsignedInt dimensions, class T> class Range {
* *
* Translates the minimal and maximal coordinates by given amount. * Translates the minimal and maximal coordinates by given amount.
* Center remains the same. * Center remains the same.
* @see @ref translated() * @see @ref translated(), @ref fromCenter()
*/ */
Range<dimensions, T> padded(const VectorType& padding) const; Range<dimensions, T> padded(const VectorType& padding) const;
@ -254,6 +268,9 @@ template<UnsignedInt dimensions, class T> class Range {
#define MAGNUM_RANGE_SUBCLASS_IMPLEMENTATION(dimensions, Type, VectorType) \ #define MAGNUM_RANGE_SUBCLASS_IMPLEMENTATION(dimensions, Type, VectorType) \
static Type<T> fromSize(const VectorType<T>& min, const VectorType<T>& size) { \ static Type<T> fromSize(const VectorType<T>& min, const VectorType<T>& size) { \
return Range<dimensions, T>::fromSize(min, size); \ return Range<dimensions, T>::fromSize(min, size); \
} \
static Type<T> fromCenter(const VectorType<T>& center, const VectorType<T>& halfSize) { \
return Range<dimensions, T>::fromCenter(center, halfSize); \
} \ } \
\ \
Type<T> translated(const VectorType<T>& vector) const { \ Type<T> translated(const VectorType<T>& vector) const { \

8
src/Magnum/Math/Test/RangeTest.cpp

@ -101,6 +101,7 @@ struct RangeTest: Corrade::TestSuite::Tester {
void constructDefault(); void constructDefault();
void constructNoInit(); void constructNoInit();
void constructFromSize(); void constructFromSize();
void constructFromCenter();
void constructPair(); void constructPair();
void constructConversion(); void constructConversion();
void constructCopy(); void constructCopy();
@ -142,6 +143,7 @@ RangeTest::RangeTest() {
&RangeTest::constructDefault, &RangeTest::constructDefault,
&RangeTest::constructNoInit, &RangeTest::constructNoInit,
&RangeTest::constructFromSize, &RangeTest::constructFromSize,
&RangeTest::constructFromCenter,
&RangeTest::constructPair, &RangeTest::constructPair,
&RangeTest::constructConversion, &RangeTest::constructConversion,
&RangeTest::constructCopy, &RangeTest::constructCopy,
@ -240,6 +242,12 @@ void RangeTest::constructFromSize() {
CORRADE_COMPARE(Range3Di::fromSize({3, 5, -7}, {23, 78, 9}), Range3Di({3, 5, -7}, {26, 83, 2})); CORRADE_COMPARE(Range3Di::fromSize({3, 5, -7}, {23, 78, 9}), Range3Di({3, 5, -7}, {26, 83, 2}));
} }
void RangeTest::constructFromCenter() {
CORRADE_COMPARE(Range1Di::fromCenter(15, 3), (Range1Di{12, 18}));
CORRADE_COMPARE(Range2Di::fromCenter({15, 5}, {3, 10}), (Range2Di{{12, -5}, {18, 15}}));
CORRADE_COMPARE(Range3Di::fromCenter({15, 5, -7}, {3, 10, 9}), (Range3Di{{12, -5, -16}, {18, 15, 2}}));
}
void RangeTest::constructPair() { void RangeTest::constructPair() {
Vector2i a{10, 22}; Vector2i a{10, 22};
Vector2i b{30, 18}; Vector2i b{30, 18};

Loading…
Cancel
Save