Browse Source

Math: add Vector2::{r,g}() and Vector3::rg().

For consistency with X/Y accessors. No reason to not have them.
pull/617/head
Vladimír Vondruš 3 years ago
parent
commit
862c48517f
  1. 6
      src/Magnum/Math/Test/Vector2Test.cpp
  2. 13
      src/Magnum/Math/Test/Vector3Test.cpp
  3. 41
      src/Magnum/Math/Vector2.h
  4. 18
      src/Magnum/Math/Vector3.h
  5. 18
      src/Magnum/Math/Vector4.h

6
src/Magnum/Math/Test/Vector2Test.cpp

@ -204,13 +204,19 @@ void Vector2Test::convert() {
void Vector2Test::access() {
Vector2 vec(1.0f, -2.0f);
CORRADE_COMPARE(vec.x(), 1.0f);
CORRADE_COMPARE(vec.r(), 1.0f);
CORRADE_COMPARE(vec.y(), -2.0f);
CORRADE_COMPARE(vec.g(), -2.0f);
constexpr Vector2 cvec(1.0f, -2.0f);
constexpr Float x = cvec.x();
constexpr Float r = cvec.r();
constexpr Float y = cvec.y();
constexpr Float g = cvec.g();
CORRADE_COMPARE(x, 1.0f);
CORRADE_COMPARE(r, 1.0f);
CORRADE_COMPARE(y, -2.0f);
CORRADE_COMPARE(g, -2.0f);
}
void Vector2Test::cross() {

13
src/Magnum/Math/Test/Vector3Test.cpp

@ -262,12 +262,17 @@ void Vector3Test::scales() {
void Vector3Test::twoComponent() {
Vector3 a(1.0f, 2.0f, 3.0f);
CORRADE_COMPARE(a.xy(), Vector2(1.0f, 2.0f));
CORRADE_COMPARE(a.rg(), Vector2(1.0f, 2.0f));
constexpr Vector3 b(1.0f, 2.0f, 3.0f);
constexpr Vector2 c = b.xy();
constexpr Float d = b.xy().y();
CORRADE_COMPARE(c, Vector2(1.0f, 2.0f));
CORRADE_COMPARE(d, 2.0f);
constexpr Vector2 c1 = b.xy();
constexpr Vector2 c2 = b.rg();
constexpr Float d1 = b.xy().y();
constexpr Float d2 = b.rg().g();
CORRADE_COMPARE(c1, Vector2(1.0f, 2.0f));
CORRADE_COMPARE(c2, Vector2(1.0f, 2.0f));
CORRADE_COMPARE(d1, 2.0f);
CORRADE_COMPARE(d2, 2.0f);
}
void Vector3Test::strictWeakOrdering() {

41
src/Magnum/Math/Vector2.h

@ -149,11 +149,48 @@ template<class T> class Vector2: public Vector<2, T> {
/** @brief Copy constructor */
constexpr /*implicit*/ Vector2(const Vector<2, T>& other) noexcept: Vector<2, T>(other) {}
T& x() { return Vector<2, T>::_data[0]; } /**< @brief X component */
/**
* @brief X component
*
* @see @ref r()
*/
T& x() { return Vector<2, T>::_data[0]; }
constexpr T x() const { return Vector<2, T>::_data[0]; } /**< @overload */
T& y() { return Vector<2, T>::_data[1]; } /**< @brief Y component */
/**
* @brief Y component
*
* @see @ref g()
*/
T& y() { return Vector<2, T>::_data[1]; }
constexpr T y() const { return Vector<2, T>::_data[1]; } /**< @overload */
/**
* @brief R component
* @m_since_latest
*
* Equivalent to @ref x().
*/
T& r() { return Vector<2, T>::_data[0]; }
/**
* @overload
* @m_since_latest
*/
constexpr T r() const { return Vector<2, T>::_data[0]; }
/**
* @brief Y component
* @m_since_latest
*
* Equivalent to @ref y().
*/
T& g() { return Vector<2, T>::_data[1]; }
/**
* @overload
* @m_since_latest
*/
constexpr T g() const { return Vector<2, T>::_data[1]; }
/**
* @brief Perpendicular vector
*

18
src/Magnum/Math/Vector3.h

@ -229,13 +229,29 @@ template<class T> class Vector3: public Vector<3, T> {
* @brief XY part of the vector
* @return First two components of the vector
*
* @see @ref gather(), @ref scatter()
* @see @ref rg(), @ref gather(), @ref scatter()
*/
Vector2<T>& xy() { return Vector2<T>::from(Vector<3, T>::data()); }
constexpr const Vector2<T> xy() const {
return {Vector<3, T>::_data[0], Vector<3, T>::_data[1]};
} /**< @overload */
/**
* @brief RG part of the vector
* @return First two components of the vector
* @m_since_latest
*
* Equivalent to @ref xy().
*/
Vector2<T>& rg() { return Vector2<T>::from(Vector<3, T>::data()); }
/**
* @overload
* @m_since_latest
*/
constexpr const Vector2<T> rg() const {
return {Vector<3, T>::_data[0], Vector<3, T>::_data[1]};
}
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(3, Vector3)
private:

18
src/Magnum/Math/Vector4.h

@ -206,13 +206,29 @@ template<class T> class Vector4: public Vector<4, T> {
* @brief XY part of the vector
* @return First two components of the vector
*
* @see @ref gather(), @ref scatter()
* @see @ref rg(), @ref gather(), @ref scatter()
*/
Vector2<T>& xy() { return Vector2<T>::from(Vector<4, T>::data()); }
constexpr const Vector2<T> xy() const {
return {Vector<4, T>::_data[0], Vector<4, T>::_data[1]};
} /**< @overload */
/**
* @brief RG part of the vector
* @return First two components of the vector
* @m_since_latest
*
* Equivalent to @ref xy().
*/
Vector2<T>& rg() { return Vector2<T>::from(Vector<4, T>::data()); }
/**
* @overload
* @m_since_latest
*/
constexpr const Vector2<T> rg() const {
return {Vector<4, T>::_data[0], Vector<4, T>::_data[1]};
}
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(4, Vector4)
};

Loading…
Cancel
Save