#ifndef Magnum_Swizzle_h #define Magnum_Swizzle_h /* Copyright © 2010, 2011, 2012 Vladimír Vondruš This file is part of Magnum. Magnum is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3 only, as published by the Free Software Foundation. Magnum is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License version 3 for more details. */ /** @file /Swizzle.h * @brief Function Magnum::swizzle() */ #include "Math/Swizzle.h" #include "Color.h" namespace Magnum { #ifndef DOXYGEN_GENERATING_OUTPUT namespace Math { namespace Implementation { template struct Component: public ComponentAtPosition {}; template struct Component: public ComponentAtPosition {}; template struct Component: public ComponentAtPosition {}; template struct Component: public ComponentAtPosition {}; }} namespace Implementation { template struct TypeForSize { typedef Math::Vector 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; }; } #endif /** @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() */ template inline constexpr typename Implementation::TypeForSize::Type swizzle(const T& vector) { return {Math::Implementation::Component::value(vector)...}; } } #endif