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

#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/coalesce.hpp>
#include <mbgl/style/expression/curve.hpp>
#include <mbgl/style/function/convert.hpp>
#include <mbgl/style/function/composite_exponential_stops.hpp>
#include <mbgl/style/function/composite_interval_stops.hpp>
#include <mbgl/style/function/composite_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>::value,
        variant<
            ExponentialStops<T>,
            IntervalStops<T>,
            CategoricalStops<T>>,
        variant<
            IntervalStops<T>,
            CategoricalStops<T>>>;

    using Stops = std::conditional_t<
        util::Interpolatable<T>::value,
        variant<
            CompositeExponentialStops<T>,
            CompositeIntervalStops<T>,
            CompositeCategoricalStops<T>>,
        variant<
            CompositeIntervalStops<T>,
            CompositeCategoricalStops<T>>>;

    using Interpolator = expression::ExponentialInterpolator<T>;
    using Curve = expression::Curve<Interpolator>;

    CompositeFunction(std::string property_, Stops stops_, optional<T> defaultValue_ = {})
    :   property(std::move(property_)),
        stops(std::move(stops_)),
        defaultValue(std::move(defaultValue_)),
        expression(stops.match([&] (const auto& s) {
            return expression::Convert::toExpression(property, s, defaultValue);
        })),
        interpolator([&]() -> Interpolator {
            optional<Curve*> zoomCurve = findZoomCurve(expression.get());
            assert(zoomCurve);
            return (*zoomCurve)->getInterpolator();
        }())
    {}

    // Return the range obtained by evaluating the function at each of the zoom levels in zoomRange
    template <class Feature>
    Range<T> evaluate(const Range<float>& zoomRange, const Feature& feature, T finalDefaultValue) {
        return Range<T> {
            evaluate(zoomRange.min, feature, finalDefaultValue),
            evaluate(zoomRange.max, feature, finalDefaultValue)
        };
    }

    template <class Feature>
    T evaluate(float zoom, const Feature& feature, T finalDefaultValue) const {
        auto result = expression->evaluate<T>(expression::EvaluationParameters { {zoom}, &feature });
        if (!result) {
            return finalDefaultValue;
        }
        return *result;
    }
    
    Interpolator getInterpolator() const {
        return interpolator;
    }

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

    std::string property;
    Stops stops;
    optional<T> defaultValue;
    bool useIntegerZoom = false;

private:
    static optional<Curve*> findZoomCurve(expression::Expression* e) {
        if (auto curve = dynamic_cast<Curve*>(e)) {
            assert(curve->isZoomCurve());
            return {curve};
//        } else if (auto let = dynamic_cast<expression::Let*>(e)) {
//            return let->getUnsafeResultExpressionPointer();
        } else if (auto coalesce = dynamic_cast<expression::Coalesce*>(e)) {
            std::size_t length = coalesce->getLength();
            for (std::size_t i = 0; i < length; i++) {
                optional<Curve*> childCurve = findZoomCurve(coalesce->getChild(i));
                if (!childCurve) {
                    continue;
                } else {
                    return childCurve;
                }
            }
        }
        
        return optional<Curve*>();
    }

    std::shared_ptr<expression::Expression> expression;
    const Interpolator interpolator;
};

} // namespace style
} // namespace mbgl