summaryrefslogtreecommitdiff
path: root/include/mbgl/style/function/composite_function.hpp
blob: 169a45543578420fe809e07243efa0bce375f53c (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
#pragma once

#include <mbgl/style/function/exponential_stops.hpp>
#include <mbgl/style/function/interval_stops.hpp>
#include <mbgl/style/function/categorical_stops.hpp>
#include <mbgl/util/interpolate.hpp>
#include <mbgl/util/range.hpp>
#include <mbgl/util/variant.hpp>

#include <string>
#include <tuple>

namespace mbgl {

class GeometryTileFeature;

namespace style {

// A CompositeFunction consists of an outer zoom function whose stop range values are
// "inner" source functions. It provides the GL Native implementation of
// "zoom-and-property" functions from the style spec.

template <class T>
class CompositeFunction {
public:
    using InnerStops = std::conditional_t<
        util::Interpolatable<T>,
        variant<
            ExponentialStops<T>,
            IntervalStops<T>,
            CategoricalStops<T>>,
        variant<
            IntervalStops<T>,
            CategoricalStops<T>>>;

    using Stops = std::conditional_t<
        util::Interpolatable<T>,
        variant<
            std::map<float, ExponentialStops<T>>,
            std::map<float, IntervalStops<T>>,
            std::map<float, CategoricalStops<T>>>,
        variant<
            std::map<float, IntervalStops<T>>,
            std::map<float, CategoricalStops<T>>>>;

    CompositeFunction(std::string property_, Stops stops_)
        : property(std::move(property_)),
          stops(std::move(stops_)) {
    }

    std::tuple<Range<float>, Range<InnerStops>>
    coveringRanges(float zoom) const {
        return stops.match(
            [&] (const auto& s) {
                assert(!s.empty());
                auto minIt = s.lower_bound(zoom);
                auto maxIt = s.upper_bound(zoom);
                if (minIt != s.begin()) {
                    minIt--;
                }
                return std::make_tuple(
                    Range<float> {
                        minIt == s.end() ? s.rbegin()->first : minIt->first,
                        maxIt == s.end() ? s.rbegin()->first : maxIt->first
                    },
                    Range<InnerStops> {
                        minIt == s.end() ? s.rbegin()->second : minIt->second,
                        maxIt == s.end() ? s.rbegin()->second : maxIt->second
                    }
                );
            }
        );
    }

    Range<T> evaluate(Range<InnerStops> coveringStops,
                      const GeometryTileFeature& feature) const {
        optional<Value> v = feature.getValue(property);
        if (!v) {
            return { T(), T() };
        }
        auto eval = [&] (const auto& s) {
            return s.evaluate(*v);
        };
        return Range<T> {
            coveringStops.min.match(eval),
            coveringStops.max.match(eval)
        };
    }

    T evaluate(float zoom, const GeometryTileFeature& feature) const {
        std::tuple<Range<float>, Range<InnerStops>> ranges = coveringRanges(zoom);
        Range<T> resultRange = evaluate(std::get<1>(ranges), feature);
        return util::interpolate(
            resultRange.min,
            resultRange.max,
            util::interpolationFactor(1.0f, std::get<0>(ranges), zoom));
    }

    friend bool operator==(const CompositeFunction& lhs,
                           const CompositeFunction& rhs) {
        return lhs.property == rhs.property && lhs.stops == rhs.stops;
    }

    std::string property;
    Stops stops;
};

} // namespace style
} // namespace mbgl