summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/buckets/heatmap_bucket.cpp
blob: 287c9ed26a386ea714137bcaa9170749da9bf64d (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
#include <mbgl/renderer/buckets/heatmap_bucket.hpp>
#include <mbgl/renderer/bucket_parameters.hpp>
#include <mbgl/programs/heatmap_program.hpp>
#include <mbgl/style/layers/heatmap_layer_impl.hpp>
#include <mbgl/renderer/layers/render_heatmap_layer.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/math.hpp>

namespace mbgl {

using namespace style;

HeatmapBucket::HeatmapBucket(const BucketParameters& parameters, const std::vector<Immutable<style::LayerProperties>>& layers)
    : mode(parameters.mode) {
    for (const auto& layer : layers) {
        paintPropertyBinders.emplace(
            std::piecewise_construct,
            std::forward_as_tuple(layer->baseImpl->id),
            std::forward_as_tuple(
                getEvaluated<HeatmapLayerProperties>(layer),
                parameters.tileID.overscaledZ));
    }
}

HeatmapBucket::~HeatmapBucket() = default;

void HeatmapBucket::upload(gfx::UploadPass& uploadPass) {
    vertexBuffer = uploadPass.createVertexBuffer(std::move(vertices));
    indexBuffer = uploadPass.createIndexBuffer(std::move(triangles));

    for (auto& pair : paintPropertyBinders) {
        pair.second.upload(uploadPass);
    }

    uploaded = true;
}

bool HeatmapBucket::hasData() const {
    return !segments.empty();
}

bool HeatmapBucket::supportsLayer(const style::Layer::Impl& impl) const {
    return style::HeatmapLayer::Impl::staticTypeInfo() == impl.getTypeInfo();
}

void HeatmapBucket::addFeature(const GeometryTileFeature& feature,
                               const GeometryCollection& geometry,
                               const ImagePositions&,
                               const PatternLayerMap&) {
    constexpr const uint16_t vertexLength = 4;

    for (auto& points : geometry) {
        for(auto& point : points) {
            auto x = point.x;
            auto y = point.y;

            // Do not include points that are outside the tile boundaries.
            if (x < 0 || x >= util::EXTENT || y < 0 || y >= util::EXTENT) {
                continue;
            }

            if (segments.empty() || segments.back().vertexLength + vertexLength > std::numeric_limits<uint16_t>::max()) {
                // Move to a new segments because the old one can't hold the geometry.
                segments.emplace_back(vertices.elements(), triangles.elements());
            }

            // this geometry will be of the Point type, and we'll derive
            // two triangles from it.
            //
            // ┌─────────┐
            // │ 4     3 │
            // │         │
            // │ 1     2 │
            // └─────────┘
            //
            vertices.emplace_back(HeatmapProgram::vertex(point, -1, -1)); // 1
            vertices.emplace_back(HeatmapProgram::vertex(point,  1, -1)); // 2
            vertices.emplace_back(HeatmapProgram::vertex(point,  1,  1)); // 3
            vertices.emplace_back(HeatmapProgram::vertex(point, -1,  1)); // 4

            auto& segment = segments.back();
            assert(segment.vertexLength <= std::numeric_limits<uint16_t>::max());
            uint16_t index = segment.vertexLength;

            // 1, 2, 3
            // 1, 4, 3
            triangles.emplace_back(index, index + 1, index + 2);
            triangles.emplace_back(index, index + 3, index + 2);

            segment.vertexLength += vertexLength;
            segment.indexLength += 6;
        }
    }

    for (auto& pair : paintPropertyBinders) {
        pair.second.populateVertexVectors(feature, vertices.elements(), {}, {});
    }
}

float HeatmapBucket::getQueryRadius(const RenderLayer& layer) const {
    (void)layer;
    return 0;
}

} // namespace mbgl