summaryrefslogtreecommitdiff
path: root/src/mbgl/style/expression/step.cpp
blob: ee6055091e90f45d190e73afe350df7ddff08da4 (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
#include <mbgl/style/expression/step.hpp>
#include <mbgl/style/expression/get_covering_stops.hpp>
#include <mbgl/util/string.hpp>

#include <cmath>

namespace mbgl {
namespace style {
namespace expression {

Step::Step(const type::Type& type_,
           std::unique_ptr<Expression> input_,
           std::map<double, std::unique_ptr<Expression>> stops_)
  : Expression(Kind::Step, type_),
    input(std::move(input_)),
    stops(std::move(stops_))
{
    assert(input->getType() == type::Number);
}

EvaluationResult Step::evaluate(const EvaluationContext& params) const {
    const EvaluationResult evaluatedInput = input->evaluate(params);
    if (!evaluatedInput) {
        return evaluatedInput.error();
    }

    float x = *fromExpressionValue<float>(*evaluatedInput);
    if (std::isnan(x)) {
        return EvaluationError { "Input is not a number." };
    }

    if (stops.empty()) {
        return EvaluationError { "No stops in step 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 {
        return std::prev(it)->second->evaluate(params);
    }
}

void Step::eachChild(const std::function<void(const Expression&)>& visit) const {
    visit(*input);
    for (auto it = stops.begin(); it != stops.end(); it++) {
        visit(*it->second);
    }
}

void Step::eachStop(const std::function<void(double, const Expression&)>& visit) const {
    for (const auto &stop : stops) {
        visit(stop.first, *stop.second);
    }
}

bool Step::operator==(const Expression& e) const {
    if (e.getKind() == Kind::Step) {
        auto rhs = static_cast<const Step*>(&e);
        return *input == *(rhs->input) && Expression::childrenEqual(stops, rhs->stops);
    }
    return false;
}

std::vector<optional<Value>> Step::possibleOutputs() const {
    std::vector<optional<Value>> result;
    for (const auto& stop : stops) {
        for (auto& output : stop.second->possibleOutputs()) {
            result.push_back(std::move(output));
        }
    }
    return result;
}

Range<float> Step::getCoveringStops(const double lower, const double upper) const {
    return ::mbgl::style::expression::getCoveringStops(stops, lower, upper);
}


ParseResult Step::parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx) {
    assert(isArray(value));

    auto length = arrayLength(value);

    if (length - 1 < 4) {
        ctx.error("Expected at least 4 arguments, but found only " + util::toString(length - 1) + ".");
        return ParseResult();
    }
    
    // [step, input, firstOutput_value, 2 * (n pairs)...]
    if ((length - 1) % 2 != 0) {
        ctx.error("Expected an even number of arguments.");
        return ParseResult();
    }
    
    ParseResult input = ctx.parse(arrayMember(value, 1), 1, {type::Number});
    if (!input) {
        return input;
    }
    
    std::map<double, std::unique_ptr<Expression>> stops;
    optional<type::Type> outputType;
    if (ctx.getExpected() && *ctx.getExpected() != type::Value) {
        outputType = ctx.getExpected();
    }
    
    double previous = - std::numeric_limits<double>::infinity();
    
    // consume the first output value, which doesn't have a corresponding input value,
    // before proceeding into the "stops" loop below.
    auto firstOutput = ctx.parse(arrayMember(value, 2), 2, outputType);
    if (!firstOutput) {
        return ParseResult();
    }
    if (!outputType) {
        outputType = (*firstOutput)->getType();
    }
    stops.emplace(-std::numeric_limits<double>::infinity(), std::move(*firstOutput));
    
    
    for (std::size_t i = 3; i + 1 < length; i += 2) {
        const optional<mbgl::Value> labelValue = toValue(arrayMember(value, i));
        optional<double> label;
        if (labelValue) {
            labelValue->match(
                [&](uint64_t n) {
                    if (n > std::numeric_limits<double>::max()) {
                        label = {std::numeric_limits<double>::infinity()};
                    } else {
                        label = {static_cast<double>(n)};
                    }
                },
                [&](int64_t n) {
                    if (n > std::numeric_limits<double>::max()) {
                        label = {std::numeric_limits<double>::infinity()};
                    } else {
                        label = {static_cast<double>(n)};
                    }
                },
                [&](double n) {
                    if (n > std::numeric_limits<double>::max()) {
                        label = {std::numeric_limits<double>::infinity()};
                    } else {
                        label = {static_cast<double>(n)};
                    }
                },
                [&](const auto&) {}
            );
        }
        if (!label) {
            ctx.error(R"(Input/output pairs for "step" 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 "step" expressions must be arranged with input values in strictly ascending order.)",
                i
            );
            return ParseResult();
        }
        previous = *label;
        
        auto output = ctx.parse(arrayMember(value, i + 1), i + 1, outputType);
        if (!output) {
            return ParseResult();
        }
        if (!outputType) {
            outputType = (*output)->getType();
        }

        stops.emplace(*label, std::move(*output));
    }
    
    assert(outputType);
    
    return ParseResult(std::make_unique<Step>(*outputType, std::move(*input), std::move(stops)));
}

mbgl::Value Step::serialize() const {
    std::vector<mbgl::Value> serialized;
    serialized.emplace_back(getOperator());
    serialized.emplace_back(input->serialize());
    for (auto& entry : stops) {
        if (entry.first > -std::numeric_limits<double>::infinity()) {
            serialized.emplace_back(entry.first);
        }
        serialized.emplace_back(entry.second->serialize());
    }
    return serialized;
}

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