Browse Source

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?).
pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
799bc629eb
  1. 16
      src/Math/Test/VectorTest.cpp
  2. 57
      src/Math/Vector.h

16
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() {

57
src/Math/Vector.h

@ -25,6 +25,7 @@
#include <Utility/Debug.h>
#include <Utility/ConfigurationValue.h>
#include "Math/BoolVector.h"
#include "Math/MathTypeTraits.h"
#include "magnumVisibility.h"
@ -201,52 +202,44 @@ template<std::size_t size, class T> 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<size, T>& other) const {
/** @brief Component-wise less than */
inline BoolVector<size> operator<(const Vector<size, T>& other) const {
BoolVector<size> 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<size, T>& other) const {
/** @brief Component-wise less than or equal */
inline BoolVector<size> operator<=(const Vector<size, T>& other) const {
BoolVector<size> 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<size, T>& other) const {
/** @brief Component-wise greater than or equal */
inline BoolVector<size> operator>=(const Vector<size, T>& other) const {
BoolVector<size> 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<size, T>& other) const {
/** @brief Component-wise greater than */
inline BoolVector<size> operator>(const Vector<size, T>& other) const {
BoolVector<size> 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;
}
/**

Loading…
Cancel
Save