Browse Source

Math: make Range2D and Range3D properly convertible from std::pair.

Not sure what was I doing in 64f8a90095.
pull/449/head
Vladimír Vondruš 6 years ago
parent
commit
5be7fbe1e8
  1. 3
      doc/changelog.dox
  2. 14
      doc/snippets/MagnumMath.cpp
  3. 43
      src/Magnum/Math/Range.h
  4. 17
      src/Magnum/Math/Test/RangeTest.cpp

3
doc/changelog.dox

@ -548,6 +548,9 @@ See also:
wireframe rendering artifacts
- @ref Math::angle() got fixed to not produce NaN results for vectors,
complex numbers or quaternions very close to each other
- @ref Math::Range was made to be implicitly convertible from @ref std::pair
in 2018.10, but the @ref Math::Range2D and @ref Math::Range3D were only
explicitly, and even that only by an accident
- Fixed various cases where @ref GL::Context would call GL 3+ APIs on GL 2.1
contexts, causing GL errors. This makes context creation completely
error-free on Mesa's Zink GL-over-Vulkan driver.

14
doc/snippets/MagnumMath.cpp

@ -1064,10 +1064,18 @@ static_cast<void>(c);
}
{
/* [Range-construct-minmax] */
/* [Range-construct-minmax2D] */
Vector2 texcoords[50];
Range2D bounds = Math::minmax(texcoords);
/* [Range-construct-minmax2D] */
static_cast<void>(bounds);
}
{
/* [Range-construct-minmax3D] */
Vector3 a, b, c;
Range3D bounds{Math::minmax({a, b, c})};
/* [Range-construct-minmax] */
Range3D bounds = Math::minmax({a, b, c});
/* [Range-construct-minmax3D] */
static_cast<void>(bounds);
}

43
src/Magnum/Math/Range.h

@ -122,9 +122,10 @@ template<UnsignedInt dimensions, class T> class Range {
* Useful in combination with e.g. @ref minmax(), here for example to
* calculate bounds of a triangle:
*
* @snippet MagnumMath.cpp Range-construct-minmax
* @snippet MagnumMath.cpp Range-construct-minmax3D
*
* @todo std::pair constructors are not constexpr in C++11, make it so in C++14 */
* @todo std::pair constructors are not constexpr in C++11, make it so in C++14
*/
/*implicit*/ Range(const std::pair<VectorType, VectorType>& minmax) noexcept:
_min{minmax.first}, _max{minmax.second} {}
@ -377,6 +378,25 @@ template<class T> class Range2D: public Range<2, T> {
/** @copydoc Range(const VectorType&, const VectorType&) */
constexpr /*implicit*/ Range2D(const Vector2<T>& min, const Vector2<T>& max) noexcept: Range<2, T>(min, max) {}
/**
* @brief Construct a range from a pair of minimal and maximal coordinates
* @m_since_latest
*
* Useful in combination with e.g. @ref minmax(), here for example to
* calculate texture bounds:
*
* @snippet MagnumMath.cpp Range-construct-minmax2D
*
* @todo std::pair constructors are not constexpr in C++11, make it so in C++14
*/
/*implicit*/ Range2D(const std::pair<Vector2<T>, Vector2<T>>& minmax) noexcept: Range<2, T>{minmax.first, minmax.second} {}
/**
* @overload
* @m_since_latest
*/
/*implicit*/ Range2D(const std::pair<Vector<2, T>, Vector<2, T>>& minmax) noexcept: Range<2, T>{minmax.first, minmax.second} {}
/** @copydoc Range(const Range<dimensions, U>&) */
template<class U> constexpr explicit Range2D(const Range2D<U>& other) noexcept: Range<2, T>(other) {}
@ -512,6 +532,25 @@ template<class T> class Range3D: public Range<3, T> {
/** @copydoc Range(const VectorType&, const VectorType&) */
constexpr /*implicit*/ Range3D(const Vector3<T>& min, const Vector3<T>& max) noexcept: Range<3, T>(min, max) {}
/**
* @brief Construct a range from a pair of minimal and maximal coordinates
* @m_since_latest
*
* Useful in combination with e.g. @ref minmax(), here for example to
* calculate bounds of a triangle:
*
* @snippet MagnumMath.cpp Range-construct-minmax3D
*
* @todo std::pair constructors are not constexpr in C++11, make it so in C++14
*/
/*implicit*/ Range3D(const std::pair<Vector3<T>, Vector3<T>>& minmax) noexcept: Range<3, T>{minmax.first, minmax.second} {}
/**
* @overload
* @m_since_latest
*/
/*implicit*/ Range3D(const std::pair<Vector<3, T>, Vector<3, T>>& minmax) noexcept: Range<3, T>{minmax.first, minmax.second} {}
/** @copydoc Range(const Range<dimensions, U>&) */
template<class U> constexpr explicit Range3D(const Range3D<U>& other) noexcept: Range<3, T>(other) {}

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

@ -283,15 +283,26 @@ void RangeTest::constructPair() {
Vector2i b{30, 18};
Vector2i c{20, 25};
Range1Di bounds1a{Math::minmax({a.x(), b.x(), c.x()})};
/* Conversion should be implicit, so not using {} */
Range1Di bounds1a = Math::minmax({a.x(), b.x(), c.x()});
Range1Di bounds1c{10, 30};
CORRADE_COMPARE(bounds1a, bounds1c);
Range2Di bounds2a{Math::minmax({a, b, c})};
Range2Di bounds2b{std::pair<Math::Vector<2, Int>, Math::Vector<2, Int>>{{10, 18}, {30, 25}}};
Range2Di bounds2a = Math::minmax({a, b, c});
Range2Di bounds2b = std::pair<Math::Vector<2, Int>, Math::Vector<2, Int>>{{10, 18}, {30, 25}};
Range2Di bounds2c{{10, 18}, {30, 25}};
CORRADE_COMPARE(bounds2a, bounds2c);
CORRADE_COMPARE(bounds2b, bounds2c);
Vector3i a3{a, 122};
Vector3i b3{b, 122};
Vector3i c3{c, 123};
Range3Di bounds3a = Math::minmax({a3, b3, c3});
Range3Di bounds3b = std::pair<Math::Vector<3, Int>, Math::Vector<3, Int>>{{10, 18, 122}, {30, 25, 123}};
Range3Di bounds3c{{10, 18, 122}, {30, 25, 123}};
CORRADE_COMPARE(bounds3a, bounds3c);
CORRADE_COMPARE(bounds3b, bounds3c);
}
void RangeTest::constructConversion() {

Loading…
Cancel
Save