From f82803917acc180865709a8d7a89a30ab131ca7a Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 28 Apr 2016 12:14:27 -0700 Subject: [PATCH] [core] Eliminate interpolate special cases (#4882) --- src/mbgl/style/function_evaluator.cpp | 1 + src/mbgl/style/function_evaluator.hpp | 7 +++ src/mbgl/util/interpolate.hpp | 89 +++++++++++++++------------ 3 files changed, 56 insertions(+), 41 deletions(-) diff --git a/src/mbgl/style/function_evaluator.cpp b/src/mbgl/style/function_evaluator.cpp index 4c60207da..c58e1d39d 100644 --- a/src/mbgl/style/function_evaluator.cpp +++ b/src/mbgl/style/function_evaluator.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include diff --git a/src/mbgl/style/function_evaluator.hpp b/src/mbgl/style/function_evaluator.hpp index 3a73ec909..7da1c299e 100644 --- a/src/mbgl/style/function_evaluator.hpp +++ b/src/mbgl/style/function_evaluator.hpp @@ -2,6 +2,7 @@ #define MBGL_STYLE_FUNCTION_EVALUATOR #include +#include namespace mbgl { @@ -33,6 +34,12 @@ public: Faded operator()(const Function&, const StyleCalculationParameters&) const; }; +namespace util { +template +struct Interpolator> + : Uninterpolated {}; +} + } // namespace mbgl #endif diff --git a/src/mbgl/util/interpolate.hpp b/src/mbgl/util/interpolate.hpp index c2b7afcb8..18a9e6bbc 100644 --- a/src/mbgl/util/interpolate.hpp +++ b/src/mbgl/util/interpolate.hpp @@ -1,57 +1,64 @@ -#ifndef MBGL_UTIL_INTERPOLATE -#define MBGL_UTIL_INTERPOLATE +#pragma once #include #include - -#include -#include +#include +#include +#include namespace mbgl { namespace util { -template -T interpolate(const T a, const T b, const double t) { - return a * (1.0 - t) + b * t; -} +template +struct Interpolator; template -inline std::array interpolate(const std::array& a, const std::array& b, const double t) { - return {{ - interpolate(a[0], b[0], t), - interpolate(a[1], b[1], t), - interpolate(a[2], b[2], t), - interpolate(a[3], b[3], t) - }}; +T interpolate(const T& a, const T& b, const double t) { + return Interpolator()(a, b, t); } -template -inline std::array interpolate(const std::array& a, const std::array& b, const double t) { - return {{ - interpolate(a[0], b[0], t), - interpolate(a[1], b[1], t) - }}; -} -// fake interpolations that just return the first value -template<> inline bool interpolate(const bool a, const bool, const double) { return a; } -template<> inline std::vector interpolate(const std::vector a, const std::vector, const double) { return a; } -template<> inline std::vector interpolate(const std::vector a, const std::vector, const double) { return a; } -template<> inline std::string interpolate(const std::string a, const std::string, const double) { return a; } -template<> inline TranslateAnchorType interpolate(const TranslateAnchorType a, const TranslateAnchorType, const double) { return a; } -template<> inline RotateAnchorType interpolate(const RotateAnchorType a, const RotateAnchorType, const double) { return a; } -template<> inline LineCapType interpolate(const LineCapType a, const LineCapType, const double) { return a; } -template<> inline LineJoinType interpolate(const LineJoinType a, const LineJoinType, const double) { return a; } -template<> inline SymbolPlacementType interpolate(const SymbolPlacementType a, const SymbolPlacementType, const double) { return a; } -template<> inline TextAnchorType interpolate(const TextAnchorType a, const TextAnchorType, const double) { return a; } -template<> inline TextJustifyType interpolate(const TextJustifyType a, const TextJustifyType, const double) { return a; } -template<> inline TextTransformType interpolate(const TextTransformType a, const TextTransformType, const double) { return a; } -template<> inline RotationAlignmentType interpolate(const RotationAlignmentType a, const RotationAlignmentType, const double) { return a; } +template +struct Interpolator { + T operator()(const T& a, const T& b, const double t) const { + return a * (1.0 - t) + b * t; + } +}; -template<> inline Faded interpolate(const Faded, const Faded b, const double) { return b; } -template<> inline Faded> interpolate(const Faded>, const Faded> b, const double) { return b; } +template +struct Interpolator> { +private: + using Array = std::array; + + template + Array operator()(const Array& a, const Array& b, const double t, std::index_sequence) { + return {{ interpolate(a[I], b[I], t)... }}; + } + +public: + Array operator()(const Array& a, const Array& b, const double t) { + return operator()(a, b, t, std::make_index_sequence()); + } +}; + +struct Uninterpolated { + template + T operator()(const T&, const T& b, const double) const { + return b; + } +}; + +template +struct Interpolator::value>> + : Uninterpolated {}; + +template <> +struct Interpolator + : Uninterpolated {}; + +template +struct Interpolator> + : Uninterpolated {}; } // namespace util } // namespace mbgl - -#endif