summaryrefslogtreecommitdiff
path: root/deps/geojsonvt/6.3.0/include/mapbox/geojsonvt/wrap.hpp
blob: 495ccc58000bb7e058e86ed78af1865ecdfaaed2 (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
#pragma once

#include <mapbox/geojsonvt/clip.hpp>
#include <mapbox/geojsonvt/types.hpp>

namespace mapbox {
namespace geojsonvt {
namespace detail {

inline void shiftCoords(vt_features& features, double offset) {
    for (auto& feature : features) {
        mapbox::geometry::for_each_point(feature.geometry,
                                         [offset](vt_point& point) { point.x += offset; });
        feature.bbox.min.x += offset;
        feature.bbox.max.x += offset;
    }
}

inline vt_features wrap(const vt_features& features, double buffer) {
    // left world copy
    auto left = clip<0>(features, -1 - buffer, buffer, -1, 2);
    // right world copy
    auto right = clip<0>(features, 1 - buffer, 2 + buffer, -1, 2);

    if (left.empty() && right.empty())
        return features;

    // center world copy
    auto merged = clip<0>(features, -buffer, 1 + buffer, -1, 2);

    if (!left.empty()) {
        // merge left into center
        shiftCoords(left, 1.0);
        merged.insert(merged.begin(), left.begin(), left.end());
    }
    if (!right.empty()) {
        // merge right into center
        shiftCoords(right, -1.0);
        merged.insert(merged.end(), right.begin(), right.end());
    }
    return merged;
}

} // namespace detail
} // namespace geojsonvt
} // namespace mapbox