From 862c48517fbd0d276c35b7c3071f7e2a53ca9382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 12 Mar 2023 11:42:06 +0100 Subject: [PATCH] Math: add Vector2::{r,g}() and Vector3::rg(). For consistency with X/Y accessors. No reason to not have them. --- src/Magnum/Math/Test/Vector2Test.cpp | 6 ++++ src/Magnum/Math/Test/Vector3Test.cpp | 13 ++++++--- src/Magnum/Math/Vector2.h | 41 ++++++++++++++++++++++++++-- src/Magnum/Math/Vector3.h | 18 +++++++++++- src/Magnum/Math/Vector4.h | 18 +++++++++++- 5 files changed, 88 insertions(+), 8 deletions(-) diff --git a/src/Magnum/Math/Test/Vector2Test.cpp b/src/Magnum/Math/Test/Vector2Test.cpp index 13b7fd19d..4f111fee5 100644 --- a/src/Magnum/Math/Test/Vector2Test.cpp +++ b/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() { diff --git a/src/Magnum/Math/Test/Vector3Test.cpp b/src/Magnum/Math/Test/Vector3Test.cpp index 152df7301..094727cff 100644 --- a/src/Magnum/Math/Test/Vector3Test.cpp +++ b/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() { diff --git a/src/Magnum/Math/Vector2.h b/src/Magnum/Math/Vector2.h index bd34167d5..17f84c7cc 100644 --- a/src/Magnum/Math/Vector2.h +++ b/src/Magnum/Math/Vector2.h @@ -149,11 +149,48 @@ template 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 * diff --git a/src/Magnum/Math/Vector3.h b/src/Magnum/Math/Vector3.h index 4fd40db82..6df78cea9 100644 --- a/src/Magnum/Math/Vector3.h +++ b/src/Magnum/Math/Vector3.h @@ -229,13 +229,29 @@ template 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& xy() { return Vector2::from(Vector<3, T>::data()); } constexpr const Vector2 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& rg() { return Vector2::from(Vector<3, T>::data()); } + /** + * @overload + * @m_since_latest + */ + constexpr const Vector2 rg() const { + return {Vector<3, T>::_data[0], Vector<3, T>::_data[1]}; + } + MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(3, Vector3) private: diff --git a/src/Magnum/Math/Vector4.h b/src/Magnum/Math/Vector4.h index c2254ec50..7c2eea34f 100644 --- a/src/Magnum/Math/Vector4.h +++ b/src/Magnum/Math/Vector4.h @@ -206,13 +206,29 @@ template 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& xy() { return Vector2::from(Vector<4, T>::data()); } constexpr const Vector2 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& rg() { return Vector2::from(Vector<4, T>::data()); } + /** + * @overload + * @m_since_latest + */ + constexpr const Vector2 rg() const { + return {Vector<4, T>::_data[0], Vector<4, T>::_data[1]}; + } + MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(4, Vector4) };