MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
interpolate.hpp
Go to the documentation of this file.
1 #pragma once
2 
6 #include <mbgl/util/color.hpp>
7 #include <mbgl/util/range.hpp>
8 
9 #include <array>
10 #include <vector>
11 #include <string>
12 #include <type_traits>
13 #include <utility>
14 
15 namespace mbgl {
16 namespace util {
17 
18 float interpolationFactor(float base, Range<float> range, float z);
19 
20 template <class T, class Enabled = void>
21 struct Interpolator;
22 
23 template <typename T>
24 T interpolate(const T& a, const T& b, const double t) {
25  return Interpolator<T>()(a, b, t);
26 }
27 
28 template <typename T>
29 T interpolate(const T& a, const T& b, const float t) {
30  return Interpolator<T>()(a, b, t);
31 }
32 
33 template <class T, class Enabled>
34 struct Interpolator {
35  T operator()(const T& a, const T& b, const double t) const {
36  return a * (1.0 - t) + b * t;
37  }
38 };
39 
40 template <>
41 struct Interpolator<float> {
42  float operator()(const float& a, const float& b, const float t) const {
43  return a * (1.0f - t) + b * t;
44  }
45 
46  float operator()(const float& a, const float& b, const double t) const {
47  return static_cast<float>(a * (1.0 - t) + b * t);
48  }
49 };
50 
51 template <class T, std::size_t N>
52 struct Interpolator<std::array<T, N>> {
53 private:
54  using Array = std::array<T, N>;
55 
56  template <std::size_t... I>
57  Array operator()(const Array& a, const Array& b, const double t, std::index_sequence<I...>) {
58  return {{ interpolate(a[I], b[I], t)... }};
59  }
60 
61 public:
62  Array operator()(const Array& a, const Array& b, const double t) {
63  return operator()(a, b, t, std::make_index_sequence<N>());
64  }
65 };
66 
67 template <std::size_t N>
68 struct Interpolator<std::array<float, N>> {
69 private:
70  using Array = std::array<float, N>;
71 
72  template <std::size_t... I>
73  Array operator()(const Array& a, const Array& b, const float t, std::index_sequence<I...>) {
74  return {{ interpolate(a[I], b[I], t)... }};
75  }
76 
77 public:
78  Array operator()(const Array& a, const Array& b, const float t) {
79  return operator()(a, b, t, std::make_index_sequence<N>());
80  }
81 };
82 
83 
92 template<>
93 struct Interpolator<std::vector<style::expression::Value>> {
94  std::vector<style::expression::Value> operator()(const std::vector<style::expression::Value>& a,
95  const std::vector<style::expression::Value>& b,
96  const double t) const {
97  assert(a.size() == b.size());
98  if (a.empty()) return {};
99  std::vector<style::expression::Value> result;
100  for (std::size_t i = 0; i < a.size(); i++) {
101  assert(a[i].template is<double>());
102  assert(b[i].template is<double>());
104  a[i].template get<double>(),
105  b[i].template get<double>(),
106  t);
107  result.push_back(item);
108  }
109  return result;
110  }
111 };
112 
113 template <>
114 struct Interpolator<style::Position> {
115 public:
116  style::Position operator()(const style::Position& a, const style::Position& b, const float t) {
117  auto pos = style::Position();
118  auto interpolated = interpolate(a.getCartesian(), b.getCartesian(), t);
119  pos.setCartesian(interpolated);
120  return { pos };
121  }
122 };
123 
124 template <>
126 public:
127  Color operator()(const Color& a, const Color& b, const float t) {
128  return {
129  interpolate(a.r, b.r, t),
130  interpolate(a.g, b.g, t),
131  interpolate(a.b, b.b, t),
132  interpolate(a.a, b.a, t)
133  };
134  }
135 
136  Color operator()(const Color& a, const Color& b, const double t) {
137  return {
138  interpolate(a.r, b.r, t),
139  interpolate(a.g, b.g, t),
140  interpolate(a.b, b.b, t),
141  interpolate(a.a, b.a, t)
142  };
143  }
144 };
145 
146 template <>
147 struct Interpolator<style::Rotation> {
148 public:
149  style::Rotation operator()(const style::Rotation& a, const style::Rotation& b, const double t) {
150  assert(a.period() == b.period());
151  auto period = a.period();
152  auto aAngle = std::fmod(a.getAngle(), period);
153  auto bAngle = std::fmod(b.getAngle(), period);
154 
155  if (aAngle - bAngle > period * 0.5) {
156  return {std::fmod(aAngle * (1.0 - t) + (bAngle + period) * t, period)};
157  }
158 
159  if (aAngle - bAngle < period * -0.5) {
160  return {std::fmod((aAngle + period) * (1.0 - t) + bAngle * t, period)};
161  }
162 
163  return {aAngle * (1.0 - t) + bAngle * t};
164  }
165 };
166 
168  template <class T>
169  T operator()(const T& a, const T&, const double) const {
170  return a;
171  }
172 };
173 
174 template <>
175 struct Interpolator<bool>
176  : Uninterpolated {};
177 
178 template <class T>
179 struct Interpolator<T, typename std::enable_if_t<std::is_enum_v<T>>>
180  : Uninterpolated {};
181 
182 template <>
183 struct Interpolator<std::string>
184  : Uninterpolated {};
185 
186 template <class T>
187 struct Interpolator<std::vector<T>>
188  : Uninterpolated {};
189 
190 template <class T>
192  : std::conditional_t<
193  !std::is_base_of_v<Uninterpolated, Interpolator<T>>,
194  std::true_type,
195  std::false_type> {};
196 
197 
198 
199 } // namespace util
200 } // namespace mbgl
float r
Definition: color.hpp:28
float a
Definition: color.hpp:31
float b
Definition: color.hpp:30
float g
Definition: color.hpp:29
std::array< float, 3 > getCartesian() const
Definition: position.hpp:27
double getAngle() const noexcept
Definition: rotation.hpp:14
constexpr double period() const noexcept
Definition: rotation.hpp:13
std::unique_ptr< Expression > string(std::unique_ptr< Expression >, std::unique_ptr< Expression > def=nullptr)
variant< ExponentialInterpolator, CubicBezierInterpolator > Interpolator
float interpolationFactor(float base, Range< float > range, float z)
T interpolate(const T &a, const T &b, const double t)
Definition: interpolate.hpp:24
Definition: actor.hpp:15
mapbox::base::Value Value
Definition: feature.hpp:11
Definition: tile_id.hpp:256
Color operator()(const Color &a, const Color &b, const float t)
Color operator()(const Color &a, const Color &b, const double t)
float operator()(const float &a, const float &b, const float t) const
Definition: interpolate.hpp:42
float operator()(const float &a, const float &b, const double t) const
Definition: interpolate.hpp:46
style::Position operator()(const style::Position &a, const style::Position &b, const float t)
style::Rotation operator()(const style::Rotation &a, const style::Rotation &b, const double t)
T operator()(const T &a, const T &b, const double t) const
Definition: interpolate.hpp:35
T operator()(const T &a, const T &, const double) const