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 
8 
9 #include <memory>
10 #include <map>
11 #include <cmath>
12 #include <optional>
13 
14 namespace mbgl {
15 namespace style {
16 namespace expression {
17 
19 
20 class Interpolate : public Expression {
21 public:
22  Interpolate(const type::Type& type_,
23  Interpolator interpolator_,
24  std::unique_ptr<Expression> input_,
25  std::map<double, std::unique_ptr<Expression>> stops_);
26 
27  const std::unique_ptr<Expression>& getInput() const { return input; }
28  const Interpolator& getInterpolator() const { return interpolator; }
29 
30  void eachChild(const std::function<void(const Expression&)>& visit) const override {
31  visit(*input);
32  for (const auto& stop : stops) {
33  visit(*stop.second);
34  }
35  }
36 
37  void eachStop(const std::function<void(double, const Expression&)>& visit) const {
38  for (const auto& stop : stops) {
39  visit(stop.first, *stop.second);
40  }
41  }
42 
43  // Return the smallest range of stops that covers the interval [lower, upper]
44  Range<float> getCoveringStops(const double lower, const double upper) const {
46  }
47 
48  double interpolationFactor(const Range<double>& inputLevels, const double inputValue) const {
49  return interpolator.match(
50  [&](const auto& interp) { return interp.interpolationFactor(inputLevels, inputValue); }
51  );
52  }
53 
54  bool operator==(const Expression& e) const override {
55  if (e.getKind() == Kind::Interpolate) {
56  auto rhs = static_cast<const Interpolate*>(&e);
57  if (interpolator != rhs->interpolator ||
58  *input != *(rhs->input) ||
59  stops.size() != rhs->stops.size())
60  {
61  return false;
62  }
63 
64  return Expression::childrenEqual(stops, rhs->stops);
65  }
66  return false;
67  }
68 
69  std::vector<std::optional<Value>> possibleOutputs() const override;
70  mbgl::Value serialize() const override;
71  std::string getOperator() const override { return "interpolate"; }
72 
73 protected:
75  const std::unique_ptr<Expression> input;
76  const std::map<double, std::unique_ptr<Expression>> stops;
77 };
78 
80  Interpolator interpolator,
81  std::unique_ptr<Expression> input,
82  std::map<double, std::unique_ptr<Expression>> stops,
83  ParsingContext& ctx);
84 
85 } // namespace expression
86 } // namespace style
87 } // namespace mbgl
static bool childrenEqual(const T &lhs, const T &rhs)
Definition: expression.hpp:226
const std::map< double, std::unique_ptr< Expression > > stops
Definition: interpolate.hpp:76
std::string getOperator() const override
Definition: interpolate.hpp:71
Interpolate(const type::Type &type_, Interpolator interpolator_, std::unique_ptr< Expression > input_, std::map< double, std::unique_ptr< Expression >> stops_)
const Interpolator & getInterpolator() const
Definition: interpolate.hpp:28
void eachChild(const std::function< void(const Expression &)> &visit) const override
Definition: interpolate.hpp:30
const std::unique_ptr< Expression > input
Definition: interpolate.hpp:75
mbgl::Value serialize() const override
Range< float > getCoveringStops(const double lower, const double upper) const
Definition: interpolate.hpp:44
const std::unique_ptr< Expression > & getInput() const
Definition: interpolate.hpp:27
double interpolationFactor(const Range< double > &inputLevels, const double inputValue) const
Definition: interpolate.hpp:48
void eachStop(const std::function< void(double, const Expression &)> &visit) const
Definition: interpolate.hpp:37
bool operator==(const Expression &e) const override
Definition: interpolate.hpp:54
std::vector< std::optional< Value > > possibleOutputs() const override
std::unique_ptr< Expression > string(std::unique_ptr< Expression >, std::unique_ptr< Expression > def=nullptr)
variant< NullType, NumberType, BooleanType, StringType, ColorType, ObjectType, ValueType, mapbox::util::recursive_wrapper< Array >, CollatorType, FormattedType, ErrorType, ImageType > Type
Definition: type.hpp:108
variant< ExponentialInterpolator, CubicBezierInterpolator > Interpolator
Range< float > getCoveringStops(const std::map< double, std::unique_ptr< Expression >> &stops, double lower, double upper)
Return the smallest range of stops that covers the interval [lower, upper].
std::optional< std::unique_ptr< Expression > > ParseResult
ParseResult createInterpolate(type::Type type, Interpolator interpolator, std::unique_ptr< Expression > input, std::map< double, std::unique_ptr< Expression >> stops, ParsingContext &ctx)
ParseResult parseInterpolate(const mbgl::style::conversion::Convertible &value, ParsingContext &ctx)
Definition: actor.hpp:15
mapbox::base::Value Value
Definition: feature.hpp:11