summaryrefslogtreecommitdiff
path: root/include/mbgl/style/expression/curve.hpp
blob: 9b7a6b64f520b3cb6d3a3ee6e611516a3e53c4c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#pragma once

#include <map>
#include <mbgl/util/interpolate.hpp>
#include <mbgl/util/range.hpp>
#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/compound_expression.hpp>
#include <mbgl/style/expression/parsing_context.hpp>
#include <mbgl/style/conversion.hpp>

namespace mbgl {
namespace style {
namespace expression {

template <class T>
class ExponentialInterpolator {
public:
    ExponentialInterpolator(float base_) : base(base_) {}

    float base;
    
    float interpolationFactor(const Range<float>& inputLevels, const float& input) const {
        return util::interpolationFactor(base, inputLevels, input);
    }
    
    EvaluationResult interpolate(const Range<Value>& outputs, const float& t) const {
        optional<T> lower = fromExpressionValue<T>(outputs.min);
        if (!lower) {
            // TODO - refactor fromExpressionValue to return EvaluationResult<T> so as to
            // consolidate DRY up producing this error message.
            return EvaluationError {
                "Expected value to be of type " + toString(valueTypeToExpressionType<T>()) +
                ", but found " + toString(typeOf(outputs.min)) + " instead."
            };
        }
        const auto& upper = fromExpressionValue<T>(outputs.max);
        if (!upper) {
            return EvaluationError {
                "Expected value to be of type " + toString(valueTypeToExpressionType<T>()) +
                ", but found " + toString(typeOf(outputs.min)) + " instead."
            };
        }
        T result = util::interpolate(*lower, *upper, t);
        return toExpressionValue(result);
    }
};

class StepInterpolator {
public:
    float interpolationFactor(const Range<float>&, const float&) const {
        return 0;
    }
    EvaluationResult interpolate(const Range<Value>&, const float&) const {
        // Assume that Curve::evaluate() will always short circuit due to
        // interpolationFactor always returning 0.
        assert(false);
        return Null;
    }
};

namespace detail {

// used for storing intermediate state during parsing
struct ExponentialInterpolation { float base; std::string name = "exponential"; };
struct StepInterpolation {};

} // namespace detail


template <typename InterpolatorT>
class Curve : public Expression {
public:
    using Interpolator = InterpolatorT;
    
    Curve(const type::Type& type,
          Interpolator interpolator_,
          std::unique_ptr<Expression> input_,
          std::map<float, std::unique_ptr<Expression>> stops_
    ) : Expression(type),
        interpolator(std::move(interpolator_)),
        input(std::move(input_)),
        stops(std::move(stops_))
    {}
    
    EvaluationResult evaluate(const EvaluationParameters& params) const override {
        const auto& x = input->evaluate<float>(params);
        if (!x) { return x.error(); }
        
        if (stops.empty()) {
            return EvaluationError { "No stops in exponential curve." };
        }

        auto it = stops.upper_bound(*x);
        if (it == stops.end()) {
            return stops.rbegin()->second->evaluate(params);
        } else if (it == stops.begin()) {
            return stops.begin()->second->evaluate(params);
        } else {
            float t = interpolator.interpolationFactor({ std::prev(it)->first, it->first }, *x);
            if (t == 0.0f) {
                return std::prev(it)->second->evaluate(params);
            }
            if (t == 1.0f) {
                return it->second->evaluate(params);
            }
            
            EvaluationResult lower = std::prev(it)->second->evaluate(params);
            if (!lower) {
                return lower.error();
            }
            EvaluationResult upper = it->second->evaluate(params);
            if (!upper) {
                return upper.error();
            }

            return interpolator.interpolate({*lower, *upper}, t);
        }
        
    }
    
    bool isFeatureConstant() const override {
        if (!input->isFeatureConstant()) { return false; }
        for (const auto& stop : stops) {
            if (!stop.second->isFeatureConstant()) { return false; }
        }
        return true;
    }

    bool isZoomConstant() const override {
        if (!input->isZoomConstant()) { return false; }
        for (const auto& stop : stops) {
            if (!stop.second->isZoomConstant()) { return false; }
        }
        return true;
    }
    
    bool isZoomCurve() const {
        if (CompoundExpressionBase* z = dynamic_cast<CompoundExpressionBase*>(input.get())) {
            return z->getName() == "zoom";
        }
        return false;
    }
    
    Interpolator getInterpolator() const {
        return interpolator;
    }
    
private:
    Interpolator interpolator;
    std::unique_ptr<Expression> input;
    std::map<float, std::unique_ptr<Expression>> stops;
};

struct ParseCurve {
    template <typename V>
    static ParseResult parse(const V& value, ParsingContext ctx) {
        using namespace mbgl::style::conversion;
        assert(isArray(value));
        auto length = arrayLength(value);
        if (length < 5) {
            ctx.error("Expected at least 4 arguments, but found only " + std::to_string(length - 1) + ".");
            return ParseResult();
        }
        
        // [curve, interp, input, 2 * (n pairs)...]
        if (length % 2 != 1) {
            ctx.error("Expected an even number of arguments.");
            return ParseResult();
        }
        
        const auto& interp = arrayMember(value, 1);
        if (!isArray(interp) || arrayLength(interp) == 0) {
            ctx.error("Expected an interpolation type expression.");
            return ParseResult();
        }

        variant<detail::StepInterpolation,
                detail::ExponentialInterpolation> interpolation;
        
        const auto& interpName = toString(arrayMember(interp, 0));
        if (interpName && *interpName == "step") {
            interpolation = detail::StepInterpolation{};
        } else if (interpName && *interpName == "linear") {
            interpolation = detail::ExponentialInterpolation { 1.0f, "linear" };
        } else if (interpName && *interpName == "exponential") {
            optional<double> base;
            if (arrayLength(interp) == 2) {
                base = toDouble(arrayMember(interp, 1));
            }
            if (!base) {
                ctx.error("Exponential interpolation requires a numeric base.");
                return ParseResult();
            }
            interpolation = detail::ExponentialInterpolation { static_cast<float>(*base) };
        } else {
            ctx.error("Unknown interpolation type " + (interpName ? *interpName : ""));
            return ParseResult();
        }
        
        ParseResult input = parseExpression(arrayMember(value, 2), ParsingContext(ctx, 2, {type::Number}));
        if (!input) {
            return input;
        }
        
        std::map<float, std::unique_ptr<Expression>> stops;
        optional<type::Type> outputType = ctx.expected;
        
        double previous = - std::numeric_limits<double>::infinity();
        for (std::size_t i = 3; i + 1 < length; i += 2) {
            const optional<mbgl::Value>& labelValue = toValue(arrayMember(value, i));
            optional<double> label;
            optional<std::string> labelError;
            if (labelValue) {
                labelValue->match(
                    [&](uint64_t n) {
                        if (!Value::isSafeNumericValue(n)) {
                            labelError = {"Numeric values must be no larger than " + std::to_string(Value::max()) + "."};
                        } else {
                            label = {static_cast<double>(n)};
                        }
                    },
                    [&](int64_t n) {
                        if (!Value::isSafeNumericValue(n)) {
                            labelError = {"Numeric values must be no larger than " + std::to_string(Value::max()) + "."};
                        } else {
                            label = {static_cast<double>(n)};
                        }
                    },
                    [&](double n) {
                        if (!Value::isSafeNumericValue(n)) {
                            labelError = {"Numeric values must be no larger than " + std::to_string(Value::max()) + "."};
                        } else {
                            label = {static_cast<double>(n)};
                        }
                    },
                    [&](const auto&) {}
                );
            }
            if (!label) {
                ctx.error(labelError ? *labelError :
                    R"(Input/output pairs for "curve" expressions must be defined using literal numeric values (not computed expressions) for the input values.)",
                    i);
                return ParseResult();
            }
            
            if (*label < previous) {
                ctx.error(
                    R"(Input/output pairs for "curve" expressions must be arranged with input values in strictly ascending order.)",
                    i
                );
                return ParseResult();
            }
            previous = *label;
            
            auto output = parseExpression(arrayMember(value, i + 1), ParsingContext(ctx, i + 1, outputType));
            if (!output) {
                return ParseResult();
            }
            if (!outputType) {
                outputType = (*output)->getType();
            }

            stops.emplace(*label, std::move(*output));
        }
        
        assert(outputType);
        
        if (
            !interpolation.template is<detail::StepInterpolation>() &&
            *outputType != type::Number &&
            *outputType != type::Color &&
            !(
                outputType->is<type::Array>() &&
                outputType->get<type::Array>().itemType == type::Number
            )
        )
        {
            ctx.error("Type " + toString(*outputType) +
                " is not interpolatable, and thus cannot be used as a " +
                *interpName + " curve's output type.");
            return ParseResult();
        }
        
        return interpolation.match(
            [&](const detail::StepInterpolation&) -> ParseResult {
                return ParseResult(std::make_unique<Curve<StepInterpolator>>(
                    *outputType,
                    StepInterpolator(),
                    std::move(*input),
                    std::move(stops)
                ));
            },
            [&](const detail::ExponentialInterpolation& exponentialInterpolation) -> ParseResult {
                const float base = exponentialInterpolation.base;
                return outputType->match(
                    [&](const type::NumberType&) -> ParseResult {
                        return ParseResult(std::make_unique<Curve<ExponentialInterpolator<float>>>(
                            *outputType,
                            ExponentialInterpolator<float>(base),
                            std::move(*input),
                            std::move(stops)
                        ));
                    },
                    [&](const type::ColorType&) -> ParseResult {
                        return ParseResult(std::make_unique<Curve<ExponentialInterpolator<mbgl::Color>>>(
                            *outputType,
                            ExponentialInterpolator<mbgl::Color>(base),
                            std::move(*input),
                            std::move(stops)
                        ));
                    },
                    [&](const type::Array& arrayType) -> ParseResult {
                        if (arrayType.itemType == type::Number && arrayType.N) {
                            return ParseResult(std::make_unique<Curve<ExponentialInterpolator<std::vector<Value>>>>(
                                *outputType,
                                ExponentialInterpolator<std::vector<Value>>(base),
                                std::move(*input),
                                std::move(stops)
                            ));
                        } else {
                            assert(false); // interpolability already checked above.
                            return ParseResult();
                        }
                    },
                    [&](const auto&) {
                        assert(false); // interpolability already checked above.
                        return ParseResult();
                    }
                );
            }
        );
    }
};

} // namespace expression
} // namespace style
} // namespace mbgl