From bff7ae8d7eab2be96e046a9923c89aec1b484e13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 26 Aug 2018 02:00:43 +0200 Subject: [PATCH] Math: added Range::fromCenter(). --- doc/changelog.dox | 2 ++ doc/snippets/MagnumMath.cpp | 8 ++++++++ src/Magnum/Math/Range.h | 19 ++++++++++++++++++- src/Magnum/Math/Test/RangeTest.cpp | 8 ++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index 957206ec2..2b5d0c3ea 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -73,6 +73,8 @@ See also: to @ref Math::join() for operating with @ref Math::Range instances - Added @ref Math::Range::contains() overloading taking a range, in addition 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, making it usable in combination with @ref Math::minmax(), for example - Added @ref Math::Constants::piQuarter() diff --git a/doc/snippets/MagnumMath.cpp b/doc/snippets/MagnumMath.cpp index a63a79301..2b9f3ca11 100644 --- a/doc/snippets/MagnumMath.cpp +++ b/doc/snippets/MagnumMath.cpp @@ -867,4 +867,12 @@ Range3D bounds{Math::minmax({a, b, c})}; static_cast(bounds); } +{ +/* [Range-fromCenter-integer] */ +Vector2i center, filterRadius; +auto filterArea = Range2Di::fromSize(center, Vector2i{1}).padded(filterRadius); +/* [Range-fromCenter-integer] */ +static_cast(filterArea); +} + } diff --git a/src/Magnum/Math/Range.h b/src/Magnum/Math/Range.h index 3e5ff4847..a3181815f 100644 --- a/src/Magnum/Math/Range.h +++ b/src/Magnum/Math/Range.h @@ -72,6 +72,20 @@ template class Range { 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 fromCenter(const VectorType& center, const VectorType& halfSize) { + return {center - halfSize, center + halfSize}; + } + /** * @brief Construct zero range * @@ -197,7 +211,7 @@ template class Range { * * Translates the minimal and maximal coordinates by given amount. * Center remains the same. - * @see @ref translated() + * @see @ref translated(), @ref fromCenter() */ Range padded(const VectorType& padding) const; @@ -254,6 +268,9 @@ template class Range { #define MAGNUM_RANGE_SUBCLASS_IMPLEMENTATION(dimensions, Type, VectorType) \ static Type fromSize(const VectorType& min, const VectorType& size) { \ return Range::fromSize(min, size); \ + } \ + static Type fromCenter(const VectorType& center, const VectorType& halfSize) { \ + return Range::fromCenter(center, halfSize); \ } \ \ Type translated(const VectorType& vector) const { \ diff --git a/src/Magnum/Math/Test/RangeTest.cpp b/src/Magnum/Math/Test/RangeTest.cpp index 9528aff6d..c7114a0b9 100644 --- a/src/Magnum/Math/Test/RangeTest.cpp +++ b/src/Magnum/Math/Test/RangeTest.cpp @@ -101,6 +101,7 @@ struct RangeTest: Corrade::TestSuite::Tester { void constructDefault(); void constructNoInit(); void constructFromSize(); + void constructFromCenter(); void constructPair(); void constructConversion(); void constructCopy(); @@ -142,6 +143,7 @@ RangeTest::RangeTest() { &RangeTest::constructDefault, &RangeTest::constructNoInit, &RangeTest::constructFromSize, + &RangeTest::constructFromCenter, &RangeTest::constructPair, &RangeTest::constructConversion, &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})); } +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() { Vector2i a{10, 22}; Vector2i b{30, 18};