From 799bc629eb717cb80cb9e3497e5183f4175b38ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 27 Jan 2013 01:36:48 +0100 Subject: [PATCH] Math: matrix/vector rework, part 4: using BoolVector for comparisons. The comparisons have also SIMD instructions returning bool vector, this future-proofs them. Also it isn't confusing anymore (a < b is true when all are less or just one?). --- src/Math/Test/VectorTest.cpp | 16 ++++------ src/Math/Vector.h | 57 ++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 43 deletions(-) diff --git a/src/Math/Test/VectorTest.cpp b/src/Math/Test/VectorTest.cpp index d5130414f..4f66d1342 100644 --- a/src/Math/Test/VectorTest.cpp +++ b/src/Math/Test/VectorTest.cpp @@ -140,17 +140,11 @@ void VectorTest::compare() { } 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))); + typedef BoolVector<3> BoolVector3; + CORRADE_COMPARE(Vector3(1.0f, -1.0f, 5.0f) < Vector3(1.1f, -1.0f, 3.0f), BoolVector3(0x1)); + CORRADE_COMPARE(Vector3(1.0f, -1.0f, 5.0f) <= Vector3(1.1f, -1.0f, 3.0f), BoolVector3(0x3)); + CORRADE_COMPARE(Vector3(1.0f, -1.0f, 5.0f) >= Vector3(1.1f, -1.0f, 3.0f), BoolVector3(0x6)); + CORRADE_COMPARE(Vector3(1.0f, -1.0f, 5.0f) > Vector3(1.1f, -1.0f, 3.0f), BoolVector3(0x4)); } void VectorTest::negative() { diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 6758d19de..45c3f2af0 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -25,6 +25,7 @@ #include #include +#include "Math/BoolVector.h" #include "Math/MathTypeTraits.h" #include "magnumVisibility.h" @@ -201,52 +202,44 @@ template class Vector { return !operator==(other); } - /** - * @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 { + /** @brief Component-wise less than */ + inline BoolVector operator<(const Vector& other) const { + BoolVector out; + for(std::size_t i = 0; i != size; ++i) - if(_data[i] >= other._data[i]) return false; + out.set(i, _data[i] < other._data[i]); - return true; + return out; } - /** - * @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 { + /** @brief Component-wise less than or equal */ + inline BoolVector operator<=(const Vector& other) const { + BoolVector out; + for(std::size_t i = 0; i != size; ++i) - if(_data[i] > other._data[i]) return false; + out.set(i, _data[i] <= other._data[i]); - return true; + return out; } - /** - * @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 { + /** @brief Component-wise greater than or equal */ + inline BoolVector operator>=(const Vector& other) const { + BoolVector out; + for(std::size_t i = 0; i != size; ++i) - if(_data[i] < other._data[i]) return false; + out.set(i, _data[i] >= other._data[i]); - return true; + return out; } - /** - * @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 { + /** @brief Component-wise greater than */ + inline BoolVector operator>(const Vector& other) const { + BoolVector out; + for(std::size_t i = 0; i != size; ++i) - if(_data[i] <= other._data[i]) return false; + out.set(i, _data[i] > other._data[i]); - return true; + return out; } /**