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

#include <mbgl/util/feature.hpp>
#include <mbgl/util/interpolate.hpp>

#include <map>

namespace mbgl {
namespace style {

template <class T>
class ExponentialStops {
public:
    using Stops = std::map<float, T>;

    Stops stops;
    float base = 1.0f;

    ExponentialStops() = default;
    ExponentialStops(Stops stops_, float base_ = 1.0f)
        : stops(std::move(stops_)),
          base(base_) {
    }

    optional<T> evaluate(float z) const {
        if (stops.empty()) {
            return {};
        }

        auto it = stops.upper_bound(z);
        if (it == stops.end()) {
            return stops.rbegin()->second;
        } else if (it == stops.begin()) {
            return stops.begin()->second;
        } else {
            return util::interpolate(std::prev(it)->second, it->second,
                util::interpolationFactor(base, { std::prev(it)->first, it->first }, z));
        }
    }

    optional<T> evaluate(const Value& value) const {
        optional<float> z = numericValue<float>(value);
        if (!z) {
            return {};
        }
        return evaluate(*z);
    }

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

} // namespace style
} // namespace mbgl