Browse Source

Returning references from vector component accessors.

Before it has to be done with overly verbose and cumbersome Java-style:

    Vector4 vec;
    vec.setX(vec.x() + 5);
    vec = Vector4(vec.xyz()*2, vec.w());

Now it can be done this way:

    vec.x() += 5;
    vec.xyz() *= 2;
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
4d46fd12cf
  1. 33
      src/Color.h
  2. 9
      src/Math/Vector2.h
  3. 16
      src/Math/Vector3.h
  4. 23
      src/Math/Vector4.h
  5. 3
      src/Test/SwizzleTest.cpp

33
src/Color.h

@ -224,13 +224,12 @@ template<class T> class Color3: public Math::Vector3<T> {
*/
inline constexpr Color3(T r, T g, T b): Math::Vector3<T>(r, g, b) {}
inline constexpr T r() const { return Math::Vector3<T>::x(); } /**< @brief R component */
inline constexpr T g() const { return Math::Vector3<T>::y(); } /**< @brief G component */
inline constexpr T b() const { return Math::Vector3<T>::z(); } /**< @brief B component */
inline void setR(T value) { Math::Vector3<T>::setX(value); } /**< @brief Set R component */
inline void setG(T value) { Math::Vector3<T>::setY(value); } /**< @brief Set G component */
inline void setB(T value) { Math::Vector3<T>::setZ(value); } /**< @brief Set B component */
inline T& r() { return Math::Vector3<T>::x(); } /**< @brief R component */
inline constexpr T r() const { return Math::Vector3<T>::x(); } /**< @overload */
inline T& g() { return Math::Vector3<T>::y(); } /**< @brief G component */
inline constexpr T g() const { return Math::Vector3<T>::y(); } /**< @overload */
inline T& b() { return Math::Vector3<T>::z(); } /**< @brief B component */
inline constexpr T b() const { return Math::Vector3<T>::z(); } /**< @overload */
/**
* @brief Convert to HSV
@ -362,15 +361,14 @@ template<class T> class Color4: public Math::Vector4<T> {
is fairly common, nearly always with A set to 1 */
inline constexpr Color4(const Math::Vector<3, T>& rgb, T a = Implementation::defaultAlpha<T>()): Math::Vector4<T>(rgb[0], rgb[1], rgb[2], a) {}
inline constexpr T r() const { return Math::Vector4<T>::x(); } /**< @brief R component */
inline constexpr T g() const { return Math::Vector4<T>::y(); } /**< @brief G component */
inline constexpr T b() const { return Math::Vector4<T>::z(); } /**< @brief B component */
inline constexpr T a() const { return Math::Vector4<T>::w(); } /**< @brief A component */
inline void setR(T value) { Math::Vector4<T>::setX(value); } /**< @brief Set R component */
inline void setG(T value) { Math::Vector4<T>::setY(value); } /**< @brief Set G component */
inline void setB(T value) { Math::Vector4<T>::setZ(value); } /**< @brief Set B component */
inline void setA(T value) { Math::Vector4<T>::setW(value); } /**< @brief Set A component */
inline T& r() { return Math::Vector4<T>::x(); } /**< @brief R component */
inline constexpr T r() const { return Math::Vector4<T>::x(); } /**< @overload */
inline T& g() { return Math::Vector4<T>::y(); } /**< @brief G component */
inline constexpr T g() const { return Math::Vector4<T>::y(); } /**< @overload */
inline T& b() { return Math::Vector4<T>::z(); } /**< @brief B component */
inline constexpr T b() const { return Math::Vector4<T>::z(); } /**< @overload */
inline T& a() { return Math::Vector4<T>::w(); } /**< @brief A component */
inline constexpr T a() const { return Math::Vector4<T>::w(); } /**< @overload */
/**
* @brief RGB part of the vector
@ -378,7 +376,8 @@ template<class T> class Color4: public Math::Vector4<T> {
*
* @see swizzle()
*/
inline constexpr Color3<T> rgb() const { return Math::Vector4<T>::xyz(); }
inline Color3<T>& rgb() { return Math::Vector4<T>::xyz(); }
inline constexpr Color3<T> rgb() const { return Math::Vector4<T>::xyz(); } /**< @overload */
/** @copydoc Color3::toHSV() */
inline constexpr HSV toHSV() const {

9
src/Math/Vector2.h

@ -81,11 +81,10 @@ template<class T> class Vector2: public Vector<2, T> {
*/
inline constexpr Vector2(T x, T y): Vector<2, T>(x, y) {}
inline constexpr T x() const { return (*this)[0]; } /**< @brief X component */
inline constexpr T y() const { return (*this)[1]; } /**< @brief Y component */
inline void setX(T value) { (*this)[0] = value; } /**< @brief Set X component */
inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */
inline T& x() { return (*this)[0]; } /**< @brief X component */
inline constexpr T x() const { return (*this)[0]; } /**< @overload */
inline T& y() { return (*this)[1]; } /**< @brief Y component */
inline constexpr T y() const { return (*this)[1]; } /**< @overload */
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector2, 2)
MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(1, 2, Vector2<T>)

16
src/Math/Vector3.h

@ -123,13 +123,12 @@ template<class T> class Vector3: public Vector<3, T> {
*/
inline constexpr Vector3(const Vector<2, T>& xy, T z): Vector<3, T>(xy[0], xy[1], z) {}
inline constexpr T x() const { return (*this)[0]; } /**< @brief X component */
inline constexpr T y() const { return (*this)[1]; } /**< @brief Y component */
inline constexpr T z() const { return (*this)[2]; } /**< @brief Z component */
inline void setX(T value) { (*this)[0] = value; } /**< @brief Set X component */
inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */
inline void setZ(T value) { (*this)[2] = value; } /**< @brief Set Z component */
inline T& x() { return (*this)[0]; } /**< @brief X component */
inline constexpr T x() const { return (*this)[0]; } /**< @overload */
inline T& y() { return (*this)[1]; } /**< @brief Y component */
inline constexpr T y() const { return (*this)[1]; } /**< @overload */
inline T& z() { return (*this)[2]; } /**< @brief Z component */
inline constexpr T z() const { return (*this)[2]; } /**< @overload */
/**
* @brief XY part of the vector
@ -137,7 +136,8 @@ template<class T> class Vector3: public Vector<3, T> {
*
* @see swizzle()
*/
inline constexpr Vector2<T> xy() const { return Vector2<T>::from(Vector<3, T>::data()); }
inline Vector2<T>& xy() { return Vector2<T>::from(Vector<3, T>::data()); }
inline constexpr Vector2<T> xy() const { return Vector2<T>::from(Vector<3, T>::data()); } /**< @overload */
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector3, 3)
MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(1, 3, Vector3<T>)

23
src/Math/Vector4.h

@ -61,15 +61,14 @@ template<class T> class Vector4: public Vector<4, T> {
is fairly common, nearly always with W set to 1 */
inline constexpr Vector4(const Vector<3, T>& xyz, T w = T(1)): Vector<4, T>(xyz[0], xyz[1], xyz[2], w) {}
inline constexpr T x() const { return (*this)[0]; } /**< @brief X component */
inline constexpr T y() const { return (*this)[1]; } /**< @brief Y component */
inline constexpr T z() const { return (*this)[2]; } /**< @brief Z component */
inline constexpr T w() const { return (*this)[3]; } /**< @brief W component */
inline void setX(T value) { (*this)[0] = value; } /**< @brief Set X component */
inline void setY(T value) { (*this)[1] = value; } /**< @brief Set Y component */
inline void setZ(T value) { (*this)[2] = value; } /**< @brief Set Z component */
inline void setW(T value) { (*this)[3] = value; } /**< @brief Set W component */
inline T& x() { return (*this)[0]; } /**< @brief X component */
inline constexpr T x() const { return (*this)[0]; } /**< @overload */
inline T& y() { return (*this)[1]; } /**< @brief Y component */
inline constexpr T y() const { return (*this)[1]; } /**< @overload */
inline T& z() { return (*this)[2]; } /**< @brief Z component */
inline constexpr T z() const { return (*this)[2]; } /**< @overload */
inline T& w() { return (*this)[3]; } /**< @brief W component */
inline constexpr T w() const { return (*this)[3]; } /**< @overload */
/**
* @brief XYZ part of the vector
@ -77,7 +76,8 @@ template<class T> class Vector4: public Vector<4, T> {
*
* @see swizzle()
*/
inline constexpr Vector3<T> xyz() const { return Vector3<T>::from(Vector<4, T>::data()); }
inline Vector3<T>& xyz() { return Vector3<T>::from(Vector<4, T>::data()); }
inline constexpr Vector3<T> xyz() const { return Vector3<T>::from(Vector<4, T>::data()); } /**< @overload */
/**
* @brief XY part of the vector
@ -85,7 +85,8 @@ template<class T> class Vector4: public Vector<4, T> {
*
* @see swizzle()
*/
inline constexpr Vector2<T> xy() const { return Vector2<T>::from(Vector<4, T>::data()); }
inline Vector2<T>& xy() { return Vector2<T>::from(Vector<4, T>::data()); }
inline constexpr Vector2<T> xy() const { return Vector2<T>::from(Vector<4, T>::data()); } /**< @overload */
MAGNUM_VECTOR_SUBCLASS_IMPLEMENTATION(Vector4, 4)
MAGNUM_RECTANGULARMATRIX_SUBCLASS_OPERATOR_IMPLEMENTATION(1, 3, Vector4<T>)

3
src/Test/SwizzleTest.cpp

@ -52,7 +52,8 @@ void SwizzleTest::rgba() {
void SwizzleTest::fromSmall() {
/* Force compile-time evaluation for both */
constexpr Vector2 orig(1, 2);
CORRADE_VERIFY((integral_constant<bool, swizzle(orig, "gxr").x() == 2>::value));
constexpr Vector3 swizzled(swizzle(orig, "gxr"));
CORRADE_VERIFY((integral_constant<bool, swizzled.x() == 2>::value));
CORRADE_COMPARE((swizzle<'g', 'x', 'r'>(orig)), Vector3(2, 1, 1));
}

Loading…
Cancel
Save