Browse Source

Component-wise comparison for Vector class.

pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
6dbdf58476
  1. 14
      src/Math/RectangularMatrix.h
  2. 15
      src/Math/Test/VectorTest.cpp
  3. 1
      src/Math/Test/VectorTest.h
  4. 48
      src/Math/Vector.h

14
src/Math/RectangularMatrix.h

@ -176,7 +176,12 @@ template<std::size_t cols, std::size_t rows, class T> 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<cols, rows, T>& other) const {
for(std::size_t i = 0; i != cols*rows; ++i)
if(!MathTypeTraits<T>::equals(_data[i], other._data[i])) return false;
@ -184,7 +189,12 @@ template<std::size_t cols, std::size_t rows, class T> 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<cols, rows, T>& other) const {
return !operator==(other);
}

15
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);
}

1
src/Math/Test/VectorTest.h

@ -24,6 +24,7 @@ class VectorTest: public Corrade::TestSuite::Tester<VectorTest> {
VectorTest();
void construct();
void compareComponentWise();
void dot();
void multiplyDivideComponentWise();
void dotSelf();

48
src/Math/Vector.h

@ -102,6 +102,54 @@ template<std::size_t size, class T> 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<size, T>& 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<size, T>& 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<size, T>& 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<size, T>& 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
*

Loading…
Cancel
Save