summaryrefslogtreecommitdiff
path: root/src/geometry/resample.cpp
blob: c40fc9bb0a710b498fb031e651029d4b7a80478e (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
#include <mbgl/geometry/resample.hpp>

#include <mbgl/util/math.hpp>

#include <cmath>

namespace mbgl {

const float minScale = 0.5f;
const std::array<std::vector<float>, 4> minScaleArrays = {{
    /*1:*/ { minScale },
    /*2:*/ { minScale, 2 },
    /*4:*/ { minScale, 4, 2, 4 },
    /*8:*/ { minScale, 8, 4, 8, 2, 8, 4, 8 }
}};


Anchors resample(const std::vector<Coordinate> &vertices, float spacing,
                 const float /*minScale*/, float maxScale, const float tilePixelRatio,
                 const int start) {

    maxScale = std::round(std::fmax(std::fmin(8.0f, maxScale / 2.0f), 1.0f));
    spacing *= tilePixelRatio / maxScale;
    const size_t index = util::clamp<size_t>(std::floor(std::log(maxScale) / std::log(2)), 0, minScaleArrays.size() - 1);
    const std::vector<float> &minScales = minScaleArrays[index];
    const size_t len = minScales.size();

    float distance = 0.0f;
    float markedDistance = 0.0f;
    int added = start;

    Anchors points;

    auto end = vertices.end() - 1;
    int i = 0;
    for (auto it = vertices.begin(); it != end; it++, i++) {
        const Coordinate &a = *(it), b = *(it + 1);

        float segmentDist = util::dist<float>(a, b);
        float angle = util::angle_to(b, a);

        while (markedDistance + spacing < distance + segmentDist) {
            markedDistance += spacing;

            float t = (markedDistance - distance) / segmentDist,
                  x = util::interp(a.x, b.x, t), y = util::interp(a.y, b.y, t),
                  s = minScales[added % len];

            if (x >= 0 && x < 4096 && y >= 0 && y < 4096) {
                points.emplace_back(x, y, angle, s, i);
            }

            added++;
        }

        distance += segmentDist;
    }

    return points;
}
}