summaryrefslogtreecommitdiff
path: root/src/style/properties.cpp
blob: a0f2a659df49ec5a3195b2d0966e2397a8ff800a (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
#include <llmr/style/properties.hpp>


#include <cassert>
#include <cmath>

using namespace llmr;


float functions::null(float z, const std::vector<float>&) {
    return 0;
}

bool functions::null(float z, const std::vector<bool>&) {
    return false;
}

float functions::constant(float z, const std::vector<float>& values) {
    assert(values.size() == 1);
    return values.front();
}

bool functions::constant(float z, const std::vector<bool>& values) {
    assert(values.size() == 1);
    return values.front();
}


float functions::stops(float z, const std::vector<float>& stops) {
    // We need an even number of stop values.
    if (stops.size() % 2 != 0) return 0;

    // Accounts for us rendering tiles at 512x512, so our zoom levels are shifted by 1.
    z += 1;

    bool smaller = false;
    float smaller_z, smaller_val;
    bool larger = false;
    float larger_z, larger_val;

    for (uint32_t i = 0; i < stops.size(); i += 2) {
        float stop_z = stops[i];
        float stop_val = stops[i + 1];
        if (stop_z <= z && (!smaller || smaller_z < stop_z)) { smaller = true; smaller_z = stop_z; smaller_val = stop_val; }
        if (stop_z >= z && (!larger || larger_z > stop_z)) { larger = true; larger_z = stop_z; larger_val = stop_val; }
    }

    if (smaller && larger) {
        if (larger_z == smaller_z || larger_val == smaller_val) return smaller_val;
        float factor = (z - smaller_z) / (larger_z - smaller_z);
        // Linear interpolation if base is 0
        if (smaller_val == 0) return factor * larger_val;
        // Exponential interpolation between the values
        return smaller_val * pow(larger_val / smaller_val, factor);
    } else if (larger || smaller) {
        // Do not draw a line.
        return -std::numeric_limits<float>::infinity();

        // Exponential extrapolation of the smaller or larger value
        //var edge = larger || smaller;
        //return Math.pow(2, z) * (edge.val / Math.pow(2, edge.z));
    } else {
        // No stop defined.
        return 1;
    }
}

bool functions::stops(float z, const std::vector<bool>& values) {
    // TODO
    return false;
}

float functions::linear(float z, const std::vector<float>& values) {
    if (values.size() != 5) {
        return 0;
    }

    const float z_base = values[0];
    const float val = values[1];
    const float slope = values[2];
    const float min = values[3];
    const float max = values[4];

    return fmin(fmax(min, val + (z - z_base) * slope), max);
}

bool functions::linear(float z, const std::vector<bool>&) {
    // TODO
    return false;
}