Browse Source

Deprecate swizzle() in favor of Math::swizzle().

All the functionality is moved to Math::swizzle() and the result is
casted to given type only if its header is included. Thus it is possible
to remove include dependency on Color. The original swizzle() is now
just an alias marked as deprecated and will be removed in future
release.
pull/23/head
Vladimír Vondruš 13 years ago
parent
commit
68b36c8c86
  1. 4
      src/CMakeLists.txt
  2. 7
      src/Color.h
  3. 27
      src/Math/Swizzle.h
  4. 8
      src/Math/Test/Vector2Test.cpp
  5. 7
      src/Math/Test/Vector3Test.cpp
  6. 8
      src/Math/Test/Vector4Test.cpp
  7. 5
      src/Math/Vector2.h
  8. 4
      src/Math/Vector3.h
  9. 4
      src/Math/Vector4.h
  10. 48
      src/Swizzle.h
  11. 1
      src/Test/CMakeLists.txt
  12. 19
      src/Test/ColorTest.cpp
  13. 83
      src/Test/SwizzleTest.cpp

4
src/CMakeLists.txt

@ -129,7 +129,6 @@ set(Magnum_HEADERS
ResourceManager.h
Sampler.h
Shader.h
Swizzle.h
Texture.h
TextureFormat.h
Timeline.h
@ -140,7 +139,8 @@ set(Magnum_HEADERS
# Deprecated headers
if(BUILD_DEPRECATED)
set(Magnum_HEADERS ${Magnum_HEADERS}
ImageFormat.h)
ImageFormat.h
Swizzle.h)
endif()
# Desktop-only headers and libraries

7
src/Color.h

@ -367,6 +367,13 @@ template<class T> inline Debug operator<<(Debug debug, const BasicColor4<T>& val
return debug << static_cast<const Math::Vector4<T>&>(value);
}
namespace Math { namespace Implementation {
template<class T> struct TypeForSize<3, BasicColor3<T>> { typedef BasicColor3<T> Type; };
template<class T> struct TypeForSize<3, BasicColor4<T>> { typedef BasicColor3<T> Type; };
template<class T> struct TypeForSize<4, BasicColor3<T>> { typedef BasicColor4<T> Type; };
template<class T> struct TypeForSize<4, BasicColor4<T>> { typedef BasicColor4<T> Type; };
}}
}
namespace Corrade { namespace Utility {

27
src/Math/Swizzle.h

@ -54,6 +54,10 @@ namespace Implementation {
template<std::size_t size> struct Component<size, '1'> {
template<class T> constexpr static T value(const Math::Vector<size, T>&) { return T(1); }
};
template<std::size_t size, class T> struct TypeForSize {
typedef Math::Vector<size, typename T::Type> Type;
};
}
/**
@ -61,23 +65,22 @@ namespace Implementation {
Creates new vector from given components. Example:
@code
Vector4<Int> original(-1, 2, 3, 4);
Vector4i original(-1, 2, 3, 4);
auto vec = swizzle<'w', '1', '0', 'x', 'y', 'z'>(original);
// vec == { 4, 1, 0, -1, 2, 3 }
@endcode
You can use letters `x`, `y`, `z`, `w` for addressing components or letters
`0` and `1` for zero and one. Count of elements is unlimited, but must be at
least one.
See also Magnum::swizzle() which has some added convenience features not
present in this lightweight implementation for Math namespace.
@see @ref matrix-vector-component-access, Vector4::xyz(),
Vector4::xy(), Vector3::xy()
You can use letters `x`, `y`, `z`, `w` and `r`, `g`, `b`, `a` for addressing
components or letters `0` and `1` for zero and one. Count of elements is
unlimited, but must be at least one. If the resulting vector is two, three or
four-component, corresponding @ref Math::Vector2, @ref Math::Vector3,
@ref Math::Vector4, @ref Color3 or @ref Color4 specialization is returned.
@see @ref matrix-vector-component-access, @ref Vector4::xyz(),
@ref Vector4::xy(), @ref Vector3::xy(), @ref Color4::rgb()
*/
template<char ...components, std::size_t size, class T> constexpr Vector<sizeof...(components), T> swizzle(const Vector<size, T>& vector) {
return {Implementation::Component<size, components>::value(vector)...};
template<char ...components, class T> constexpr typename Implementation::TypeForSize<sizeof...(components), T>::Type swizzle(const T& vector) {
return {Implementation::Component<T::Size, components>::value(vector)...};
}
}}

8
src/Math/Test/Vector2Test.cpp

@ -69,6 +69,7 @@ class Vector2Test: public Corrade::TestSuite::Tester {
void perpendicular();
void aspectRatio();
void swizzleType();
void debug();
void configuration();
};
@ -93,6 +94,7 @@ Vector2Test::Vector2Test() {
&Vector2Test::perpendicular,
&Vector2Test::aspectRatio,
&Vector2Test::swizzleType,
&Vector2Test::debug,
&Vector2Test::configuration});
}
@ -203,6 +205,12 @@ void Vector2Test::aspectRatio() {
CORRADE_COMPARE(a.aspectRatio(), 0.75f);
}
void Vector2Test::swizzleType() {
constexpr Vector<4, Int> orig;
constexpr auto a = swizzle<'y', 'a'>(orig);
CORRADE_VERIFY((std::is_same<decltype(a), const Vector2i>::value));
}
void Vector2Test::debug() {
std::ostringstream o;
Debug(&o) << Vector2(0.5f, 15.0f);

7
src/Math/Test/Vector3Test.cpp

@ -69,6 +69,7 @@ class Vector3Test: public Corrade::TestSuite::Tester {
void scales();
void twoComponent();
void swizzleType();
void debug();
void configuration();
};
@ -224,6 +225,12 @@ void Vector3Test::twoComponent() {
CORRADE_COMPARE(d, 2.0f);
}
void Vector3Test::swizzleType() {
constexpr Vector<4, Int> orig;
constexpr auto b = swizzle<'y', 'z', 'a'>(orig);
CORRADE_VERIFY((std::is_same<decltype(b), const Vector3i>::value));
}
void Vector3Test::debug() {
std::ostringstream o;
Debug(&o) << Vector3(0.5f, 15.0f, 1.0f);

8
src/Math/Test/Vector4Test.cpp

@ -67,6 +67,7 @@ class Vector4Test: public Corrade::TestSuite::Tester {
void threeComponent();
void twoComponent();
void swizzleType();
void debug();
void configuration();
};
@ -90,6 +91,7 @@ Vector4Test::Vector4Test() {
&Vector4Test::threeComponent,
&Vector4Test::twoComponent,
&Vector4Test::swizzleType,
&Vector4Test::debug,
&Vector4Test::configuration});
}
@ -215,6 +217,12 @@ void Vector4Test::twoComponent() {
CORRADE_COMPARE(d, 1.0f);
}
void Vector4Test::swizzleType() {
constexpr Vector4i orig;
constexpr auto c = swizzle<'y', 'a', 'y', 'x'>(orig);
CORRADE_VERIFY((std::is_same<decltype(c), const Vector4i>::value));
}
void Vector4Test::debug() {
std::ostringstream o;
Debug(&o) << Vector4(0.5f, 15.0f, 1.0f, 1.0f);

5
src/Math/Vector2.h

@ -153,6 +153,11 @@ template<class T> inline Corrade::Utility::Debug operator<<(Corrade::Utility::De
return debug << static_cast<const Vector<2, T>&>(value);
}
namespace Implementation {
template<std::size_t, class> struct TypeForSize;
template<class T> struct TypeForSize<2, T> { typedef Math::Vector2<typename T::Type> Type; };
}
}}
namespace Corrade { namespace Utility {

4
src/Math/Vector3.h

@ -213,6 +213,10 @@ template<class T> inline Corrade::Utility::Debug operator<<(Corrade::Utility::De
return debug << static_cast<const Vector<3, T>&>(value);
}
namespace Implementation {
template<class T> struct TypeForSize<3, T> { typedef Math::Vector3<typename T::Type> Type; };
}
}}
namespace Corrade { namespace Utility {

4
src/Math/Vector4.h

@ -177,6 +177,10 @@ template<class T> inline Corrade::Utility::Debug operator<<(Corrade::Utility::De
return debug << static_cast<const Vector<4, T>&>(value);
}
namespace Implementation {
template<class T> struct TypeForSize<4, T> { typedef Math::Vector4<typename T::Type> Type; };
}
}}
namespace Corrade { namespace Utility {

48
src/Swizzle.h

@ -25,50 +25,30 @@
*/
/** @file /Swizzle.h
* @brief Function Magnum::swizzle()
* @brief Function @ref Magnum::swizzle()
* @deprecated Use @ref Math/Swizzle.h instead.
*/
#include "Math/Swizzle.h"
#ifdef MAGNUM_BUILD_DEPRECATED
#include "Color.h"
namespace Magnum {
namespace Implementation {
template<std::size_t size, class T> struct TypeForSize {
typedef Math::Vector<size, typename T::Type> Type;
};
template<class T> struct TypeForSize<2, T> { typedef Math::Vector2<typename T::Type> Type; };
template<class T> struct TypeForSize<3, T> { typedef Math::Vector3<typename T::Type> Type; };
template<class T> struct TypeForSize<4, T> { typedef Math::Vector4<typename T::Type> Type; };
template<class T> struct TypeForSize<3, BasicColor3<T>> { typedef BasicColor3<T> Type; };
template<class T> struct TypeForSize<3, BasicColor4<T>> { typedef BasicColor3<T> Type; };
template<class T> struct TypeForSize<4, BasicColor3<T>> { typedef BasicColor4<T> Type; };
template<class T> struct TypeForSize<4, BasicColor4<T>> { typedef BasicColor4<T> Type; };
}
/**
@brief Swizzle Vector components
Creates new vector from given components. Example:
@code
Vector4i original(-1, 2, 3, 4);
auto vec = swizzle<'a', '1', '0', 'r', 'g', 'b'>(original);
// vec == { 4, 1, 0, -1, 2, 3 }
@endcode
You can use letters `x`, `y`, `z`, `w` and `r`, `g`, `b`, `a` for addressing
components or letters `0` and `1` for zero and one. Count of elements is
unlimited, but must be at least one. If the resulting vector is two, three or
four-component, corresponding Math::Vector2, Math::Vector3, Math::Vector4,
Color3 or Color4 specialization is returned.
@see @ref matrix-vector-component-access, Math::swizzle(), Vector4::xyz(),
Color4::rgb(), Vector4::xy(), Vector3::xy()
@copybrief Math::swizzle()
@deprecated Use @ref Magnum::Math::swizzle() "Math::swizzle()" instead.
*/
template<char ...components, class T> constexpr typename Implementation::TypeForSize<sizeof...(components), T>::Type swizzle(const T& vector) {
return {Math::Implementation::Component<T::Size, components>::value(vector)...};
}
#ifdef DOXYGEN_GENERATING_OUTPUT
template<char ...components, class T> constexpr typename Math::Implementation::TypeForSize<sizeof...(components), T>::Type swizzle(const T& vector);
#else
using Math::swizzle;
#endif
}
#else
#error
#endif
#endif

1
src/Test/CMakeLists.txt

@ -33,7 +33,6 @@ corrade_add_test(MeshTest MeshTest.cpp LIBRARIES Magnum)
corrade_add_test(RendererTest RendererTest.cpp LIBRARIES Magnum)
corrade_add_test(ResourceManagerTest ResourceManagerTest.cpp LIBRARIES MagnumTestLib)
corrade_add_test(SamplerTest SamplerTest.cpp LIBRARIES Magnum)
corrade_add_test(SwizzleTest SwizzleTest.cpp LIBRARIES MagnumMathTestLib)
if(BUILD_GL_TESTS)
corrade_add_test(BufferGLTest BufferGLTest.cpp LIBRARIES ${GL_TEST_LIBRARIES})

19
src/Test/ColorTest.cpp

@ -46,6 +46,7 @@ class ColorTest: public TestSuite::Tester {
void hsvOverflow();
void hsvAlpha();
void swizzleType();
void debug();
void configuration();
};
@ -66,6 +67,7 @@ ColorTest::ColorTest() {
&ColorTest::hsvOverflow,
&ColorTest::hsvAlpha,
&ColorTest::swizzleType,
&ColorTest::debug,
&ColorTest::configuration});
}
@ -137,6 +139,23 @@ void ColorTest::hsvAlpha() {
CORRADE_COMPARE(Color4ub::fromHSV(Deg(230.0f), 0.749f, 0.427f, 23), Color4ub(27, 40, 108, 23));
}
void ColorTest::swizzleType() {
constexpr Color3 origColor3;
constexpr BasicColor4<UnsignedByte> origColor4;
constexpr auto a = Math::swizzle<'y', 'z', 'r'>(origColor3);
CORRADE_VERIFY((std::is_same<decltype(a), const Color3>::value));
constexpr auto b = Math::swizzle<'y', 'z', 'a'>(origColor4);
CORRADE_VERIFY((std::is_same<decltype(b), const BasicColor3<UnsignedByte>>::value));
constexpr auto c = Math::swizzle<'y', 'z', 'y', 'x'>(origColor3);
CORRADE_VERIFY((std::is_same<decltype(c), const Color4>::value));
constexpr auto d = Math::swizzle<'y', 'a', 'y', 'x'>(origColor4);
CORRADE_VERIFY((std::is_same<decltype(d), const BasicColor4<UnsignedByte>>::value));
}
void ColorTest::debug() {
std::ostringstream o;
Debug(&o) << Color3(0.5f, 0.75f, 1.0f);

83
src/Test/SwizzleTest.cpp

@ -1,83 +0,0 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <TestSuite/Tester.h>
#include "Swizzle.h"
namespace Magnum { namespace Test {
class SwizzleTest: public TestSuite::Tester {
public:
SwizzleTest();
void type();
void defaultType();
};
SwizzleTest::SwizzleTest() {
addTests({&SwizzleTest::type,
&SwizzleTest::defaultType});
}
void SwizzleTest::type() {
constexpr Vector4i orig;
constexpr Color3 origColor3;
constexpr BasicColor4<UnsignedByte> origColor4;
constexpr auto a = swizzle<'y', 'a'>(orig);
CORRADE_VERIFY((std::is_same<decltype(a), const Vector2i>::value));
constexpr auto b = swizzle<'y', 'z', 'a'>(orig);
CORRADE_VERIFY((std::is_same<decltype(b), const Vector3i>::value));
constexpr auto c = swizzle<'y', 'a', 'y', 'x'>(orig);
CORRADE_VERIFY((std::is_same<decltype(c), const Vector4i>::value));
constexpr auto d = swizzle<'y', 'z', 'r'>(origColor3);
CORRADE_VERIFY((std::is_same<decltype(d), const Color3>::value));
constexpr auto e = swizzle<'y', 'z', 'a'>(origColor4);
CORRADE_VERIFY((std::is_same<decltype(e), const BasicColor3<UnsignedByte>>::value));
constexpr auto f = swizzle<'y', 'z', 'y', 'x'>(origColor3);
CORRADE_VERIFY((std::is_same<decltype(f), const Color4>::value));
constexpr auto g = swizzle<'y', 'a', 'y', 'x'>(origColor4);
CORRADE_VERIFY((std::is_same<decltype(g), const BasicColor4<UnsignedByte>>::value));
}
void SwizzleTest::defaultType() {
constexpr Vector4i orig(1, 2, 3, 4);
constexpr auto a = swizzle<'b'>(orig);
CORRADE_COMPARE(a, (Math::Vector<1, Int>(3)));
constexpr auto b = swizzle<'b', 'r', 'a', 'g', 'z', 'y', 'x'>(orig);
CORRADE_COMPARE(b, (Math::Vector<7, Int>(3, 1, 4, 2, 3, 2, 1)));
}
}}
CORRADE_TEST_MAIN(Magnum::Test::SwizzleTest)
Loading…
Cancel
Save