Browse Source

Math: added modulo operations for Vector.

pull/34/head
Vladimír Vondruš 13 years ago
parent
commit
f10f2d8726
  1. 24
      src/Math/Test/VectorTest.cpp
  2. 79
      src/Math/Vector.h

24
src/Math/Test/VectorTest.cpp

@ -74,6 +74,7 @@ class VectorTest: public Corrade::TestSuite::Tester {
void multiplyDivideIntegral();
void multiplyDivideComponentWise();
void multiplyDivideComponentWiseIntegral();
void modulo();
void bitwise();
void compare();
@ -128,6 +129,7 @@ VectorTest::VectorTest() {
&VectorTest::multiplyDivideIntegral,
&VectorTest::multiplyDivideComponentWise,
&VectorTest::multiplyDivideComponentWiseIntegral,
&VectorTest::modulo,
&VectorTest::bitwise,
&VectorTest::compare,
@ -337,6 +339,15 @@ void VectorTest::multiplyDivideComponentWiseIntegral() {
/* Using integer vector as divisor is not supported */
}
void VectorTest::modulo() {
typedef Math::Vector<2, Int> Vector2i;
const Vector2i a(4, 13);
const Vector2i b(2, 5);
CORRADE_COMPARE(a % 2, Vector2i(0, 1));
CORRADE_COMPARE(a % b, Vector2i(0, 3));
}
void VectorTest::bitwise() {
typedef Math::Vector<2, Int> Vector2i;
@ -479,9 +490,16 @@ void VectorTest::subclassTypes() {
CORRADE_VERIFY((std::is_same<decltype(a *= c), Vec2&>::value));
CORRADE_VERIFY((std::is_same<decltype(a /= c), Vec2&>::value));
/* Bitwise operations */
/* Modulo operations */
const Vec2i ci;
Vec2i i;
const Int j = {};
CORRADE_VERIFY((std::is_same<decltype(ci % j), Vec2i>::value));
CORRADE_VERIFY((std::is_same<decltype(i %= j), Vec2i&>::value));
CORRADE_VERIFY((std::is_same<decltype(ci % ci), Vec2i>::value));
CORRADE_VERIFY((std::is_same<decltype(i %= ci), Vec2i&>::value));
/* Bitwise operations */
CORRADE_VERIFY((std::is_same<decltype(~ci), Vec2i>::value));
CORRADE_VERIFY((std::is_same<decltype(ci & ci), Vec2i>::value));
CORRADE_VERIFY((std::is_same<decltype(ci | ci), Vec2i>::value));
@ -528,6 +546,10 @@ void VectorTest::subclass() {
CORRADE_COMPARE(Vec2(-2.0f, 5.0f)*Vec2(1.5f, -2.0f), Vec2(-3.0f, -10.0f));
CORRADE_COMPARE(Vec2(-2.0f, 5.0f)/Vec2(2.0f/3.0f, -0.5f), Vec2(-3.0f, -10.0f));
/* Modulo operations */
CORRADE_COMPARE(Vec2i(4, 13) % 2, Vec2i(0, 1));
CORRADE_COMPARE(Vec2i(4, 13) % Vec2i(2, 5), Vec2i(0, 3));
/* Bitwise operations */
CORRADE_COMPARE(~Vec2i(85, 240), Vec2i(-86, -241));
CORRADE_COMPARE(Vec2i(85, 240) & Vec2i(170, 85), Vec2i(0, 80));

79
src/Math/Vector.h

@ -563,6 +563,70 @@ template<std::size_t size, class T> inline Vector<size, T> operator/(
return out;
}
/** @relates Vector
@brief Do modulo of integral vector and assign
The computation is done in-place.
*/
template<std::size_t size, class Integral> inline
#ifdef DOXYGEN_GENERATING_OUTPUT
Vector<size, Integral>&
#else
typename std::enable_if<std::is_integral<Integral>::value, Vector<size, Integral>&>::type
#endif
operator%=(Vector<size, Integral>& a, Integral b) {
for(std::size_t i = 0; i != size; ++i)
a[i] %= b;
return a;
}
/** @relates Vector
@brief Modulo of integral vector
*/
template<std::size_t size, class Integral> inline
#ifdef DOXYGEN_GENERATING_OUTPUT
Vector<size, Integral>
#else
typename std::enable_if<std::is_integral<Integral>::value, Vector<size, Integral>>::type
#endif
operator%(const Vector<size, Integral>& a, Integral b) {
Vector<size, Integral> copy(a);
return copy %= b;
}
/** @relates Vector
@brief Do modulo of two integral vectors and assign
The computation is done in-place.
*/
template<std::size_t size, class Integral> inline
#ifdef DOXYGEN_GENERATING_OUTPUT
Vector<size, Integral>&
#else
typename std::enable_if<std::is_integral<Integral>::value, Vector<size, Integral>&>::type
#endif
operator%=(Vector<size, Integral>& a, const Vector<size, Integral>& b) {
for(std::size_t i = 0; i != size; ++i)
a[i] %= b[i];
return a;
}
/** @relates Vector
@brief Modulo of two integral vectors
*/
template<std::size_t size, class Integral> inline
#ifdef DOXYGEN_GENERATING_OUTPUT
Vector<size, Integral>
#else
typename std::enable_if<std::is_integral<Integral>::value, Vector<size, Integral>>::type
#endif
operator%(const Vector<size, Integral>& a, const Vector<size, Integral>& b) {
Vector<size, Integral> copy(a);
return copy %= b;
}
/** @relates Vector
@brief Bitwise NOT of integral vector
*/
@ -1057,6 +1121,21 @@ extern template Corrade::Utility::Debug MAGNUM_EXPORT operator<<(Corrade::Utilit
return number/static_cast<const Math::Vector<size, T>&>(vector); \
} \
\
template<class Integral> inline typename std::enable_if<std::is_integral<Integral>::value, Type<Integral>&>::type operator%=(Type<Integral>& a, Integral b) { \
static_cast<Math::Vector<size, Integral>&>(a) %= b; \
return a; \
} \
template<class Integral> inline typename std::enable_if<std::is_integral<Integral>::value, Type<Integral>>::type operator%(const Type<Integral>& a, Integral b) { \
return static_cast<const Math::Vector<size, Integral>&>(a) % b; \
} \
template<class Integral> inline typename std::enable_if<std::is_integral<Integral>::value, Type<Integral>&>::type operator%=(Type<Integral>& a, const Math::Vector<size, Integral>& b) { \
static_cast<Math::Vector<size, Integral>&>(a) %= b; \
return a; \
} \
template<class Integral> inline typename std::enable_if<std::is_integral<Integral>::value, Type<Integral>>::type operator%(const Type<Integral>& a, const Math::Vector<size, Integral>& b) { \
return static_cast<const Math::Vector<size, Integral>&>(a) % b; \
} \
\
template<class Integral> inline typename std::enable_if<std::is_integral<Integral>::value, Type<Integral>>::type operator~(const Type<Integral>& vector) { \
return ~static_cast<const Math::Vector<size, Integral>&>(vector); \
} \

Loading…
Cancel
Save