summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/possibly_evaluated_property_value.hpp
blob: 1198e43aae014aaf4f8687734f53a679f18aeaed (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
#pragma once

#include <mbgl/style/property_expression.hpp>
#include <mbgl/renderer/cross_faded_property_evaluator.hpp>
#include <mbgl/util/interpolate.hpp>
#include <mbgl/util/variant.hpp>

namespace mbgl {

template <class T>
class PossiblyEvaluatedPropertyValue {
private:
    using Value = variant<
        T,
        style::PropertyExpression<T>>;

    Value value;

public:
    PossiblyEvaluatedPropertyValue() = default;
    PossiblyEvaluatedPropertyValue(Value v)
        : value(std::move(v)) {}

    bool isConstant() const {
        return value.template is<T>();
    }

    optional<T> constant() const {
        return value.match(
            [&] (const T& t) { return optional<T>(t); },
            [&] (const auto&) { return optional<T>(); });
    }

    T constantOr(const T& t) const {
        return constant().value_or(t);
    }

    template <class... Ts>
    auto match(Ts&&... ts) const {
        return value.match(std::forward<Ts>(ts)...);
    }

    template <class Feature>
    T evaluate(const Feature& feature, float zoom, T defaultValue) const {
        return this->match(
            [&] (const T& constant_) { return constant_; },
            [&] (const style::PropertyExpression<T>& expression) {
                return expression.evaluate(zoom, feature, defaultValue);
            }
        );
    }

    template <class Feature>
    T evaluate(const Feature& feature, float zoom, const FeatureState& featureState, T defaultValue) const {
        return this->match([&](const T& constant_) { return constant_; },
                           [&](const style::PropertyExpression<T>& expression) {
                               return expression.evaluate(zoom, feature, featureState, defaultValue);
                           });
    }
};

template <class T>
class PossiblyEvaluatedPropertyValue<Faded<T>> {
private:
    using Value = variant<
        Faded<T>,
        style::PropertyExpression<T>>;

    Value value;

public:
    PossiblyEvaluatedPropertyValue() = default;
    PossiblyEvaluatedPropertyValue(Value v)
        : value(std::move(v)) {}

    bool isConstant() const {
        return value.template is<Faded<T>>();
    }

    optional<Faded<T>> constant() const {
        return value.match(
            [&] (const Faded<T>& t) { return optional<Faded<T>>(t); },
            [&] (const auto&) { return optional<Faded<T>>(); });
    }

    Faded<T> constantOr(const Faded<T>& t) const {
        return constant().value_or(t);
    }

    template <class... Ts>
    auto match(Ts&&... ts) const {
        return value.match(std::forward<Ts>(ts)...);
    }

    template <class Feature>
    Faded<T> evaluate(const Feature& feature, float zoom, T defaultValue) const {
        return this->match(
            [&] (const Faded<T>& constant_) { return constant_; },
            [&] (const style::PropertyExpression<T>& expression) {
                if (!expression.isZoomConstant()) {
                    const T min = expression.evaluate(floor(zoom), feature, defaultValue);
                    const T max = expression.evaluate(floor(zoom) + 1, feature, defaultValue);
                    return Faded<T> {min, max};
                } else {
                    const T evaluated = expression.evaluate(feature, defaultValue);
                    return Faded<T> {evaluated, evaluated};
                }
            }
        );
    }
};


namespace util {

template <typename T>
struct Interpolator<PossiblyEvaluatedPropertyValue<T>> {
    PossiblyEvaluatedPropertyValue<T> operator()(const PossiblyEvaluatedPropertyValue<T>& a,
                                                 const PossiblyEvaluatedPropertyValue<T>& b,
                                                 const double t) const {
        if (a.isConstant() && b.isConstant()) {
            return { interpolate(*a.constant(), *b.constant(), t) };
        } else {
            return { a };
        }
    }
};

} // namespace util

} // namespace mbgl