#ifndef Magnum_Math_Swizzle_h #define Magnum_Math_Swizzle_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Vladimír Vondruš 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. */ /** @file * @brief Function @ref Magnum::Math::gather(), @ref Magnum::Math::scatter() */ #include "Magnum/Math/Vector.h" namespace Magnum { namespace Math { namespace Implementation { template struct ComponentAtPosition { static_assert(size > position, "swizzle parameter out of range of gather vector"); template constexpr static T value(const Math::Vector& vector) { return vector[position]; } }; template struct Component {}; template struct Component: public ComponentAtPosition {}; template struct Component: public ComponentAtPosition {}; template struct Component: public ComponentAtPosition {}; template struct Component: public ComponentAtPosition {}; template struct Component: public ComponentAtPosition {}; template struct Component: public ComponentAtPosition {}; template struct Component: public ComponentAtPosition {}; template struct Component: public ComponentAtPosition {}; template struct Component { template constexpr static T value(const Math::Vector&) { return T(0); } }; template struct Component { template constexpr static T value(const Math::Vector&) { return T(1); } }; template struct TypeForSize { typedef Math::Vector Type; }; template struct ComponentOr { static_assert(component == 'x' || component == 'r' || ((component == 'y' || component == 'g') && size > 1) || ((component == 'z' || component == 'b') && size > 2) || ((component == 'w' || component == 'a') && size > 3), "swizzle parameter out of range of scatter vector"); template constexpr static T value(const Math::Vector& vector, const T&) { return vector[i]; } }; template struct Value { template constexpr static T value(const Math::Vector&, const T& value) { return value; } }; template struct ComponentOr: Value {}; template struct ComponentOr: Value {}; template struct ComponentOr: Value {}; template struct ComponentOr: Value {}; template struct ComponentOr: Value {}; template struct ComponentOr: Value {}; template struct ComponentOr: Value {}; template struct ComponentOr: Value {}; template constexpr T componentOr(const T& vector, const typename T::Type& value, Sequence) { return {ComponentOr::value(vector, value)...}; } template constexpr T scatterRecursive(const T& vector, const Math::Vector&, std::size_t) { return vector; } template constexpr T scatterRecursive(const T& vector, const Math::Vector& values, std::size_t valueIndex) { return scatterRecursive( componentOr(vector, values[valueIndex], typename GenerateSequence::Type{}), values, valueIndex + 1); } } /** @brief Gather @ref Vector components Creates a new vector from given components. Example: @snippet MagnumMath.cpp gather You can use letters @cpp 'x' @ce, @cpp 'y' @ce, @cpp 'z' @ce, @cpp 'w' @ce and @cpp 'r' @ce, @cpp 'g' @ce, @cpp 'b' @ce, @cpp 'a' @ce for addressing components or letters @cpp '0' @ce and @cpp '1' @ce 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 Vector2, @ref Vector3, @ref Vector4, @ref Color3 or @ref Color4 specialization is returned. @see @ref scatter(), @ref matrix-vector-component-access, @ref Vector4::xyz(), @ref Vector4::rgb(), @ref Vector4::xy(), @ref Vector3::xy() */ template constexpr typename Implementation::TypeForSize::Type gather(const T& vector) { return {Implementation::Component::value(vector)...}; } /** @brief Scatter @ref Vector components @param vector Vector to update @param values Values to update it with @return Updated vector Returns a copy of @p vector with particular components updated from @p values. Inverse to @ref gather(), supporting the same component addressing except for @cpp '0' @ce and @cpp '1' @ce. Example: @snippet MagnumMath.cpp scatter @see @ref matrix-vector-component-access, @ref Vector4::xyz(), @ref Vector4::rgb(), @ref Vector4::xy(), @ref Vector3::xy() */ template constexpr T scatter(const T& vector, const Vector& values) { return Implementation::scatterRecursive(vector, values, 0); } #ifdef MAGNUM_BUILD_DEPRECATED /** @brief @copybrief gather() * @deprecated Use @ref gather instead. */ template CORRADE_DEPRECATED("use gather() instead") constexpr typename Implementation::TypeForSize::Type swizzle(const T& vector) { return gather(vector); } #endif }} #endif