diff --git a/src/Magnum/Math/BoolVector.h b/src/Magnum/Math/BoolVector.h index 40cf5e3f4..87df34d18 100644 --- a/src/Magnum/Math/BoolVector.h +++ b/src/Magnum/Math/BoolVector.h @@ -49,6 +49,13 @@ namespace Implementation { template struct GenerateSequence<0, sequence...> { typedef Sequence Type; }; + + template struct GenerateReverseSequence: + GenerateReverseSequence {}; + + template struct GenerateReverseSequence<0, sequence...> { + typedef Sequence Type; + }; #endif template constexpr T repeat(T value, std::size_t) { return value; } diff --git a/src/Magnum/Math/Test/VectorTest.cpp b/src/Magnum/Math/Test/VectorTest.cpp index 2c835d5f4..c4f25cc90 100644 --- a/src/Magnum/Math/Test/VectorTest.cpp +++ b/src/Magnum/Math/Test/VectorTest.cpp @@ -100,6 +100,8 @@ struct VectorTest: Corrade::TestSuite::Tester { void projected(); void projectedOntoNormalized(); + void flipped(); + void angle(); void subclassTypes(); @@ -158,6 +160,8 @@ VectorTest::VectorTest() { &VectorTest::projected, &VectorTest::projectedOntoNormalized, + &VectorTest::flipped, + &VectorTest::angle, &VectorTest::subclassTypes, @@ -462,6 +466,12 @@ void VectorTest::projectedOntoNormalized() { CORRADE_COMPARE(projected, vector.projected(line)); } +void VectorTest::flipped() { + constexpr Vector4 vector{1.0f, -3.5f, 2.1f, 0.5f}; + constexpr Vector4 flipped = vector.flipped(); + CORRADE_COMPARE(flipped, (Vector4{0.5f, 2.1f, -3.5f, 1.0f})); +} + void VectorTest::angle() { std::ostringstream o; Error redirectError{&o}; @@ -557,6 +567,7 @@ void VectorTest::subclassTypes() { CORRADE_VERIFY((std::is_same::value)); CORRADE_VERIFY((std::is_same::value)); CORRADE_VERIFY((std::is_same::value)); + CORRADE_VERIFY((std::is_same::value)); } void VectorTest::subclass() { @@ -638,6 +649,8 @@ void VectorTest::subclass() { CORRADE_COMPARE(Vec2(3.0f, 0.0f).resized(6.0f), Vec2(6.0f, 0.0f)); CORRADE_COMPARE(Vec2(1.0f, 1.0f).projected({0.0f, 2.0f}), Vec2(0.0f, 1.0f)); CORRADE_COMPARE(Vec2(1.0f, 1.0f).projectedOntoNormalized({0.0f, 1.0f}), Vec2(0.0f, 1.0f)); + constexpr Vec2 flipped = Vec2{1.0f, 0.4f}.flipped(); + CORRADE_COMPARE(flipped, (Vec2{0.4f, 1.0f})); } void VectorTest::debug() { diff --git a/src/Magnum/Math/Vector.h b/src/Magnum/Math/Vector.h index c78619a1c..5c435b826 100644 --- a/src/Magnum/Math/Vector.h +++ b/src/Magnum/Math/Vector.h @@ -536,6 +536,15 @@ template class Vector { */ Vector projectedOntoNormalized(const Vector& line) const; + /** + * @brief Flipped vector + * + * Returns the vector with components in reverse order. + */ + constexpr Vector flipped() const { + return flippedInternal(typename Implementation::GenerateReverseSequence::Type{}); + } + /** * @brief Sum of values in the vector * @@ -575,6 +584,10 @@ template class Vector { return {sequence < otherSize ? a[sequence] : value...}; } + template constexpr Vector flippedInternal(Implementation::Sequence) const { + return {(*this)[sequence]...}; + } + T _data[size]; }; @@ -1164,6 +1177,9 @@ extern template MAGNUM_EXPORT Corrade::Utility::Debug& operator<<(Corrade::Utili } \ Type projectedOntoNormalized(const Math::Vector& other) const { \ return Math::Vector::projectedOntoNormalized(other); \ + } \ + constexpr Type flipped() const { \ + return Math::Vector::flipped(); \ } #define MAGNUM_VECTORn_OPERATOR_IMPLEMENTATION(size, Type) \