From dd8eb6bc02a858b9e021b5d1e9482a5b208389a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 12 Dec 2012 20:08:29 +0100 Subject: [PATCH] Removed one swizzle() overload. It is less efficient because of all that runtime computation and can't be made to use SIMD operations so why bother with it at all. --- doc/matrix-vector.dox | 6 ++-- src/Swizzle.h | 64 ++-------------------------------------- src/Test/SwizzleTest.cpp | 30 +++---------------- 3 files changed, 9 insertions(+), 91 deletions(-) diff --git a/doc/matrix-vector.dox b/doc/matrix-vector.dox index 6b05b00c3..9faab0993 100644 --- a/doc/matrix-vector.dox +++ b/doc/matrix-vector.dox @@ -134,13 +134,11 @@ xyz.xy() *= 5; @endcode Color3 and Color4 name their components `rgba` instead of `xyzw`. -For more involved operations with components there are two swizzle() functions, -they have the same features, but one is guaranteed to do most of the work at -compile-time, while the second has more convenient syntax: +For more involved operations with components there is the swizzle() function: @code Vector4 original(-1, 2, 3, 4); Vector4 bgra = swizzle<'b', 'g', 'r', 'a'>(original); // { 3, 2, -1, 4 } -Vector<6, int> a10rgb = swizzle(original, "a10rgb"); // { 4, 1, 0, -1, 2, 3 } +Vector<6, int> w10xyz = swizzle<'w', '1', '0', 'x', 'y', 'z'>(original); // { 4, 1, 0, -1, 2, 3 } @endcode @section matrix-vector-column-major Matrices are column-major and vectors are columns diff --git a/src/Swizzle.h b/src/Swizzle.h index 0805f6a9c..af2ff2186 100644 --- a/src/Swizzle.h +++ b/src/Swizzle.h @@ -16,7 +16,7 @@ */ /** @file - * @brief Functions Magnum::swizzle(const T&), Magnum::swizzle(const T&, const char(&)[]) + * @brief Function Magnum::swizzle() */ #include "Color.h" @@ -25,9 +25,6 @@ namespace Magnum { #ifndef DOXYGEN_GENERATING_OUTPUT namespace Implementation { - using Math::Implementation::Sequence; - using Math::Implementation::GenerateSequence; - template struct ComponentAtPosition { static_assert(size > position, "Swizzle parameter out of range of base vector"); @@ -60,28 +57,6 @@ namespace Implementation { template struct TypeForSize<3, Color4> { typedef Color3 Type; }; template struct TypeForSize<4, Color3> { typedef Color4 Type; }; template struct TypeForSize<4, Color4> { typedef Color4 Type; }; - - template inline constexpr T componentAtPosition(const Math::Vector& vector, std::size_t position) { - return size > position ? vector[position] : throw; - } - - template inline constexpr T component(const Math::Vector& vector, char component) { - return component == 'x' ? componentAtPosition(vector, 0) : - component == 'y' ? componentAtPosition(vector, 1) : - component == 'z' ? componentAtPosition(vector, 2) : - component == 'w' ? componentAtPosition(vector, 3) : - component == 'r' ? componentAtPosition(vector, 0) : - component == 'g' ? componentAtPosition(vector, 1) : - component == 'b' ? componentAtPosition(vector, 2) : - component == 'a' ? componentAtPosition(vector, 3) : - component == '0' ? T(0) : - component == '1' ? T(1) : - throw; - } - - template inline constexpr Math::Vector swizzleFrom(Sequence, const Math::Vector& vector, const char(&components)[sizeof...(sequence)+1]) { - return {component(vector, components[sequence])...}; - } } #endif @@ -98,13 +73,8 @@ auto vec = swizzle<'a', '1', '0', 'r', 'g', 'b'>(original); 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 Vector2, Vector3, Vector4, Color3 or Color4 -specialization is returned. - -@attention This function is less convenient to write than -swizzle(const T&, const char(&)[newSize]), but the evaluation of -the swizzling operation is guaranteed to be always done at compile time -instead of at runtime. +four-component, corresponding Math::Vector2, Math::Vector3, Math::Vector4, +Color3 or Color4 specialization is returned. @see @ref matrix-vector-component-access, Vector4::xyz(), Color4::rgb(), Vector4::xy(), Vector3::xy() @@ -113,34 +83,6 @@ template inline constexpr typename Implementation:: return {Implementation::Component::value(vector)...}; } -/** -@brief Swizzle Vector components - -Creates new vector from given components. Example: -@code -Vector4i original(-1, 2, 3, 4); - -auto vec = swizzle(original, "a10rgb"); -// 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 Vector2, Vector3 or Vector4 specialization is -returned. - -@attention This function is more convenient to write than -swizzle(const T&), but unless the result is marked with -`constexpr`, the evaluation of the swizzling operation probably won't be -evaluated at compile time, but at runtime. - -@see @ref matrix-vector-component-access, Vector4::xyz(), Color4::rgb(), - Vector4::xy(), Vector3::xy() -*/ -template inline constexpr typename Implementation::TypeForSize::Type swizzle(const T& vector, const char(&components)[newSize]) { - return Implementation::swizzleFrom(typename Implementation::GenerateSequence::Type(), vector, components); -} - } #endif diff --git a/src/Test/SwizzleTest.cpp b/src/Test/SwizzleTest.cpp index 5da5a9bf3..627d302c2 100644 --- a/src/Test/SwizzleTest.cpp +++ b/src/Test/SwizzleTest.cpp @@ -31,61 +31,39 @@ SwizzleTest::SwizzleTest() { } void SwizzleTest::xyzw() { - Vector4i orig(2, 4, 5, 7); - Vector4i swizzled(5, 2, 7, 4); - CORRADE_COMPARE(swizzle(orig, "zxwy"), swizzled); - CORRADE_COMPARE((swizzle<'z', 'x', 'w', 'y'>(orig)), swizzled); + CORRADE_COMPARE((swizzle<'z', 'x', 'w', 'y'>(Vector4i(2, 4, 5, 7))), Vector4i(5, 2, 7, 4)); } void SwizzleTest::rgba() { - Vector4i orig(2, 4, 5, 7); - Vector4i swizzled(5, 2, 7, 4); - CORRADE_COMPARE(swizzle(orig, "brag"), swizzled); - CORRADE_COMPARE((swizzle<'b', 'r', 'a', 'g'>(orig)), swizzled); + CORRADE_COMPARE((swizzle<'b', 'r', 'a', 'g'>(Vector4i(2, 4, 5, 7))), Vector4i(5, 2, 7, 4)); } void SwizzleTest::constants() { - Vector4i orig(2, 4, 5, 7); - Vector4i swizzled(1, 7, 0, 4); - CORRADE_COMPARE(swizzle(orig, "1w0g"), swizzled); - CORRADE_COMPARE((swizzle<'1', 'w', '0', 'g'>(orig)), swizzled); + CORRADE_COMPARE((swizzle<'1', 'w', '0', 'g'>(Vector4i(2, 4, 5, 7))), Vector4i(1, 7, 0, 4)); } void SwizzleTest::fromSmall() { - /* Force compile-time evaluation for both */ - constexpr Vector2i orig(1, 2); - constexpr Vector3i swizzled(swizzle(orig, "gxr")); - CORRADE_VERIFY((std::integral_constant::value)); - CORRADE_COMPARE((swizzle<'g', 'x', 'r'>(orig)), Vector3i(2, 1, 1)); + CORRADE_COMPARE((swizzle<'g', 'x', 'r'>(Vector2i(1, 2))), Vector3i(2, 1, 1)); } void SwizzleTest::type() { Vector4i orig; CORRADE_VERIFY((std::is_same(orig)), Vector2i>::value)); - CORRADE_VERIFY((std::is_same::value)); CORRADE_VERIFY((std::is_same(orig)), Vector3i>::value)); - CORRADE_VERIFY((std::is_same::value)); CORRADE_VERIFY((std::is_same(orig)), Vector4i>::value)); - CORRADE_VERIFY((std::is_same::value)); Color3 origColor3; Color4 origColor4; CORRADE_VERIFY((std::is_same(origColor3)), Color3<>>::value)); CORRADE_VERIFY((std::is_same(origColor4)), Color3>::value)); - CORRADE_VERIFY((std::is_same>::value)); - CORRADE_VERIFY((std::is_same>::value)); CORRADE_VERIFY((std::is_same(origColor3)), Color4<>>::value)); CORRADE_VERIFY((std::is_same(origColor4)), Color4>::value)); - CORRADE_VERIFY((std::is_same>::value)); - CORRADE_VERIFY((std::is_same>::value)); } void SwizzleTest::defaultType() { Vector4i orig(1, 2, 3, 4); CORRADE_COMPARE(swizzle<'b'>(orig), (Math::Vector<1, std::int32_t>(3))); - CORRADE_COMPARE(swizzle(orig, "b"), (Math::Vector<1, std::int32_t>(3))); CORRADE_COMPARE((swizzle<'b', 'r', 'a', 'g', 'z', 'y', 'x'>(orig)), (Math::Vector<7, std::int32_t>(3, 1, 4, 2, 3, 2, 1))); - CORRADE_COMPARE(swizzle(orig, "bragzyx"), (Math::Vector<7, std::int32_t>(3, 1, 4, 2, 3, 2, 1))); } }}