From 0177ec8b5dfc9f0cc54f2b5e1887ea28108a767f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 13 Aug 2012 15:17:13 +0200 Subject: [PATCH] Moved swizzle() functions to root namespace. They aren't much useful in Math namespace and here it allows to handle also Color3/Color4 types. Moreover all RGBA component names were removed from Math namespace, so it would be inconsistent to have them there only in swizzle() functions. --- src/CMakeLists.txt | 1 + src/Math/CMakeLists.txt | 1 - src/Math/Test/CMakeLists.txt | 2 -- src/{Math => }/Swizzle.h | 34 +++++++++++++++++------------ src/Test/CMakeLists.txt | 1 + src/{Math => }/Test/SwizzleTest.cpp | 17 ++++++++++++--- src/{Math => }/Test/SwizzleTest.h | 8 +++---- 7 files changed, 40 insertions(+), 24 deletions(-) rename src/{Math => }/Swizzle.h (74%) rename src/{Math => }/Test/SwizzleTest.cpp (74%) rename src/{Math => }/Test/SwizzleTest.h (86%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cd2fe4927..a3639825c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -56,6 +56,7 @@ set(Magnum_HEADERS Scene.h Shader.h SizeTraits.h + Swizzle.h Texture.h TypeTraits.h diff --git a/src/Math/CMakeLists.txt b/src/Math/CMakeLists.txt index d12a6a16c..a0cc17b69 100644 --- a/src/Math/CMakeLists.txt +++ b/src/Math/CMakeLists.txt @@ -6,7 +6,6 @@ set(MagnumMath_HEADERS Matrix.h Matrix3.h Matrix4.h - Swizzle.h Vector.h Vector2.h Vector3.h diff --git a/src/Math/Test/CMakeLists.txt b/src/Math/Test/CMakeLists.txt index f71db4d9a..b77ac8e4c 100644 --- a/src/Math/Test/CMakeLists.txt +++ b/src/Math/Test/CMakeLists.txt @@ -6,8 +6,6 @@ corrade_add_test2(MathVector2Test Vector2Test.cpp) corrade_add_test2(MathVector3Test Vector3Test.cpp) corrade_add_test2(MathVector4Test Vector4Test.cpp) -corrade_add_test2(MathSwizzleTest SwizzleTest.cpp) - corrade_add_test2(MathMatrixTest MatrixTest.cpp) corrade_add_test2(MathMatrix3Test Matrix3Test.cpp) corrade_add_test2(MathMatrix4Test Matrix4Test.cpp) diff --git a/src/Math/Swizzle.h b/src/Swizzle.h similarity index 74% rename from src/Math/Swizzle.h rename to src/Swizzle.h index e964948a6..84af4858b 100644 --- a/src/Math/Swizzle.h +++ b/src/Swizzle.h @@ -1,5 +1,5 @@ -#ifndef Magnum_Math_Swizzle_h -#define Magnum_Math_Swizzle_h +#ifndef Magnum_Swizzle_h +#define Magnum_Swizzle_h /* Copyright © 2010, 2011, 2012 Vladimír Vondruš @@ -19,12 +19,15 @@ * @brief Function Magnum::Math::swizzle() */ -#include "Vector4.h" +#include "Color.h" -namespace Magnum { namespace Math { +namespace Magnum { #ifndef DOXYGEN_GENERATING_OUTPUT namespace Implementation { + using Math::Implementation::Sequence; + using Math::Implementation::GenerateSequence; + template struct GetPosition { static_assert(size > position, "Swizzle parameter out of range of base vector"); @@ -42,12 +45,15 @@ namespace Implementation { template struct GetComponent: public GetPosition {}; template struct TypeForSize { - typedef Vector Type; + typedef Math::Vector Type; }; - - template struct TypeForSize<2, T> { typedef Vector2 Type; }; - template struct TypeForSize<3, T> { typedef Vector3 Type; }; - template struct TypeForSize<4, T> { typedef Vector4 Type; }; + template struct TypeForSize<2, T> { typedef Math::Vector2 Type; }; + template struct TypeForSize<3, T> { typedef Math::Vector3 Type; }; + template struct TypeForSize<4, T> { typedef Math::Vector4 Type; }; + template struct TypeForSize<3, Color3> { typedef Color3 Type; }; + template struct TypeForSize<3, Color4> { typedef Color3 Type; }; + template struct TypeForSize<4, Color3> { typedef Color4 Type; }; + template struct TypeForSize<4, Color4> { typedef Color4 Type; }; inline constexpr size_t getPosition(size_t size, size_t position) { return size > position ? position : throw; @@ -65,7 +71,7 @@ namespace Implementation { throw; } - template inline constexpr Vector swizzleFrom(Sequence, const Vector& vector, const char(&components)[sizeof...(sequence)+1]) { + template inline constexpr Math::Vector swizzleFrom(Sequence, const Math::Vector& vector, const char(&components)[sizeof...(sequence)+1]) { return {vector[getComponent(components[sequence])]...}; } } @@ -93,8 +99,8 @@ instead of at runtime. @see Vector4::xyz(), Vector4::rgb(), Vector4::xy(), Vector3::xy() */ -template inline constexpr typename Implementation::TypeForSize::Type swizzle(const Vector& vector) { - return {vector[Implementation::GetComponent::value()]...}; +template inline constexpr typename Implementation::TypeForSize::Type swizzle(const T& vector) { + return {vector[Implementation::GetComponent::value()]...}; } /** @@ -119,10 +125,10 @@ evaluated at compile time, but at runtime. @see Vector4::xyz(), Vector4::rgb(), Vector4::xy(), Vector3::xy() */ -template inline constexpr typename Implementation::TypeForSize::Type swizzle(const Vector& vector, const char(&components)[newSize]) { +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/CMakeLists.txt b/src/Test/CMakeLists.txt index 6c20f9a69..67650069a 100644 --- a/src/Test/CMakeLists.txt +++ b/src/Test/CMakeLists.txt @@ -3,3 +3,4 @@ corrade_add_test2(CameraTest CameraTest.cpp LIBRARIES Magnum) corrade_add_test2(SceneTest SceneTest.cpp LIBRARIES Magnum) corrade_add_test2(ColorTest ColorTest.cpp) +corrade_add_test2(SwizzleTest SwizzleTest.cpp) diff --git a/src/Math/Test/SwizzleTest.cpp b/src/Test/SwizzleTest.cpp similarity index 74% rename from src/Math/Test/SwizzleTest.cpp rename to src/Test/SwizzleTest.cpp index 52f590ae8..299603280 100644 --- a/src/Math/Test/SwizzleTest.cpp +++ b/src/Test/SwizzleTest.cpp @@ -19,9 +19,9 @@ using namespace std; -CORRADE_TEST_MAIN(Magnum::Math::Test::SwizzleTest) +CORRADE_TEST_MAIN(Magnum::Test::SwizzleTest) -namespace Magnum { namespace Math { namespace Test { +namespace Magnum { namespace Test { typedef Math::Vector2 Vector2; typedef Math::Vector3 Vector3; @@ -66,6 +66,17 @@ void SwizzleTest::type() { CORRADE_VERIFY((is_same::value)); CORRADE_VERIFY((is_same(orig)), Vector4>::value)); CORRADE_VERIFY((is_same::value)); + + Color3 origColor3; + Color4 origColor4; + CORRADE_VERIFY((is_same(origColor3)), Color3>::value)); + CORRADE_VERIFY((is_same(origColor4)), Color3>::value)); + CORRADE_VERIFY((is_same>::value)); + CORRADE_VERIFY((is_same>::value)); + CORRADE_VERIFY((is_same(origColor3)), Color4>::value)); + CORRADE_VERIFY((is_same(origColor4)), Color4>::value)); + CORRADE_VERIFY((is_same>::value)); + CORRADE_VERIFY((is_same>::value)); } void SwizzleTest::defaultType() { @@ -76,4 +87,4 @@ void SwizzleTest::defaultType() { CORRADE_COMPARE(swizzle(orig, "bragzyx"), Vector<7>(3, 1, 4, 2, 3, 2, 1)); } -}}} +}} diff --git a/src/Math/Test/SwizzleTest.h b/src/Test/SwizzleTest.h similarity index 86% rename from src/Math/Test/SwizzleTest.h rename to src/Test/SwizzleTest.h index a08dfdd17..b43180be0 100644 --- a/src/Math/Test/SwizzleTest.h +++ b/src/Test/SwizzleTest.h @@ -1,5 +1,5 @@ -#ifndef Magnum_Math_Test_SwizzleTest_h -#define Magnum_Math_Test_SwizzleTest_h +#ifndef Magnum_Test_SwizzleTest_h +#define Magnum_Test_SwizzleTest_h /* Copyright © 2010, 2011, 2012 Vladimír Vondruš @@ -17,7 +17,7 @@ #include -namespace Magnum { namespace Math { namespace Test { +namespace Magnum { namespace Test { class SwizzleTest: public Corrade::TestSuite::Tester { public: @@ -30,6 +30,6 @@ class SwizzleTest: public Corrade::TestSuite::Tester { void defaultType(); }; -}}} +}} #endif