diff --git a/src/Math/RectangularMatrix.h b/src/Math/RectangularMatrix.h index bc4822323..e01321277 100644 --- a/src/Math/RectangularMatrix.h +++ b/src/Math/RectangularMatrix.h @@ -176,7 +176,12 @@ template class RectangularMatrix { return _data[col*rows+row]; } - /** @brief Equality operator */ + /** + * @brief Equality operator + * + * @see Vector::operator<(), Vector::operator<=(), Vector::operator>=(), + * Vector::operator>() + */ inline bool operator==(const RectangularMatrix& other) const { for(std::size_t i = 0; i != cols*rows; ++i) if(!MathTypeTraits::equals(_data[i], other._data[i])) return false; @@ -184,7 +189,12 @@ template class RectangularMatrix { return true; } - /** @brief Non-equality operator */ + /** + * @brief Non-equality operator + * + * @see Vector::operator<(), Vector::operator<=(), Vector::operator>=(), + * Vector::operator>() + */ inline constexpr bool operator!=(const RectangularMatrix& other) const { return !operator==(other); } diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index e89907bf4..f9a86cc62 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -32,6 +32,7 @@ typedef Vector<3, float> Vector3; VectorTest::VectorTest() { addTests(&VectorTest::construct, + &VectorTest::compareComponentWise, &VectorTest::dot, &VectorTest::multiplyDivideComponentWise, &VectorTest::dotSelf, @@ -53,6 +54,20 @@ void VectorTest::construct() { CORRADE_COMPARE(Vector4::from(data), Vector4(1.0f, 2.0f, 3.0f, 4.0f)); } +void VectorTest::compareComponentWise() { + CORRADE_VERIFY(Vector4(1.0f, -2.0f, -5.0f, 7.0f) < Vector4(1.1f, -1.0f, 3.0f, 8.0f)); + CORRADE_VERIFY(!(Vector4(1.0f, -2.0f, -5.0f, 7.0f) < Vector4(1.1f, -1.0f, 3.0f, 7.0f))); + + CORRADE_VERIFY(Vector4(1.0f, -2.0f, -5.0f, 7.0f) <= Vector4(1.1f, -1.0f, 3.0f, 7.0f)); + CORRADE_VERIFY(!(Vector4(1.0f, -2.0f, -5.0f, 7.0f) <= Vector4(1.0f, -2.0f, -5.0f, 6.0f))); + + CORRADE_VERIFY(Vector4(1.1f, -1.0f, 3.0f, 7.0f) >= Vector4(1.0f, -2.0f, -5.0f, 7.0f)); + CORRADE_VERIFY(!(Vector4(1.0f, -2.0f, -5.0f, 6.0f) >= Vector4(1.0f, -2.0f, -5.0f, 7.0f))); + + CORRADE_VERIFY(Vector4(1.1f, -1.0f, 3.0f, 8.0f) > Vector4(1.0f, -2.0f, -5.0f, 7.0f)); + CORRADE_VERIFY(!(Vector4(1.1f, -1.0f, 3.0f, 7.0f) > Vector4(1.0f, -2.0f, -5.0f, 7.0f))); +} + void VectorTest::dot() { CORRADE_COMPARE(Vector4::dot({1.0f, 0.5f, 0.75f, 1.5f}, {2.0f, 4.0f, 1.0f, 7.0f}), 15.25f); } diff --git a/src/Math/Test/VectorTest.h b/src/Math/Test/VectorTest.h index 1af4b7122..ca8f48e48 100644 --- a/src/Math/Test/VectorTest.h +++ b/src/Math/Test/VectorTest.h @@ -24,6 +24,7 @@ class VectorTest: public Corrade::TestSuite::Tester { VectorTest(); void construct(); + void compareComponentWise(); void dot(); void multiplyDivideComponentWise(); void dotSelf(); diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 9fd4acba5..3ed1f71b3 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -102,6 +102,54 @@ template class Vector: public RectangularMatrix<1, si inline T& operator[](std::size_t pos) { return RectangularMatrix<1, size, T>::_data[pos]; } inline constexpr T operator[](std::size_t pos) const { return RectangularMatrix<1, size, T>::_data[pos]; } /**< @overload */ + /** + * @brief Component-wise less than + * @return `True` if all components are smaller than their + * counterparts in @p other, `false` otherwise + */ + inline bool operator<(const Vector& other) const { + for(std::size_t i = 0; i != size; ++i) + if((*this)[i] >= other[i]) return false; + + return true; + } + + /** + * @brief Component-wise less than or equal + * @return `True` if all components are smaller than or equal to their + * counterparts in @p other, `false` otherwise + */ + inline bool operator<=(const Vector& other) const { + for(std::size_t i = 0; i != size; ++i) + if((*this)[i] > other[i]) return false; + + return true; + } + + /** + * @brief Component-wise greater than or equal + * @return `True` if all components are larger than or equal to their + * counterparts in @p other, `false` otherwise + */ + inline bool operator>=(const Vector& other) const { + for(std::size_t i = 0; i != size; ++i) + if((*this)[i] < other[i]) return false; + + return true; + } + + /** + * @brief Component-wise greater than + * @return `True` if all components are larger than their + * counterparts in @p other, `false` otherwise + */ + inline bool operator>(const Vector& other) const { + for(std::size_t i = 0; i != size; ++i) + if((*this)[i] <= other[i]) return false; + + return true; + } + /** * @brief Multiply vector component-wise *