summaryrefslogtreecommitdiff
path: root/src/mbgl/layer/fill_layer.cpp
blob: 850bac4ab4690182626b4c5400bd7e8b3145067e (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 <mbgl/layer/fill_layer.hpp>
#include <mbgl/style/style_bucket_parameters.hpp>
#include <mbgl/renderer/fill_bucket.hpp>
#include <mbgl/geometry/feature_index.hpp>
#include <mbgl/util/math.hpp>
#include <mbgl/util/intersection_tests.hpp>

namespace mbgl {

std::unique_ptr<StyleLayer> FillLayer::clone() const {
    return std::make_unique<FillLayer>(*this);
}

void FillLayer::parsePaints(const JSValue& layer) {
    paint.fillAntialias.parse("fill-antialias", layer);
    paint.fillOpacity.parse("fill-opacity", layer);
    paint.fillColor.parse("fill-color", layer);
    paint.fillOutlineColor.parse("fill-outline-color", layer);
    paint.fillTranslate.parse("fill-translate", layer);
    paint.fillTranslateAnchor.parse("fill-translate-anchor", layer);
    paint.fillPattern.parse("fill-pattern", layer);
}

void FillLayer::cascade(const StyleCascadeParameters& parameters) {
    paint.fillAntialias.cascade(parameters);
    paint.fillOpacity.cascade(parameters);
    paint.fillColor.cascade(parameters);
    paint.fillOutlineColor.cascade(parameters);
    paint.fillTranslate.cascade(parameters);
    paint.fillTranslateAnchor.cascade(parameters);
    paint.fillPattern.cascade(parameters);
}

bool FillLayer::recalculate(const StyleCalculationParameters& parameters) {
    bool hasTransitions = false;

    hasTransitions |= paint.fillAntialias.calculate(parameters);
    hasTransitions |= paint.fillOpacity.calculate(parameters);
    hasTransitions |= paint.fillColor.calculate(parameters);
    hasTransitions |= paint.fillOutlineColor.calculate(parameters);
    hasTransitions |= paint.fillTranslate.calculate(parameters);
    hasTransitions |= paint.fillTranslateAnchor.calculate(parameters);
    hasTransitions |= paint.fillPattern.calculate(parameters);

    passes = RenderPass::None;

    if (paint.fillAntialias) {
        passes |= RenderPass::Translucent;
    }

    if (!paint.fillPattern.value.from.empty() || (paint.fillColor.value[3] * paint.fillOpacity) < 1.0f) {
        passes |= RenderPass::Translucent;
    } else {
        passes |= RenderPass::Opaque;
    }

    return hasTransitions;
}

std::unique_ptr<Bucket> FillLayer::createBucket(StyleBucketParameters& parameters) const {
    auto bucket = std::make_unique<FillBucket>();

    auto& name = bucketName();
    parameters.eachFilteredFeature(filter, [&] (const auto& feature, std::size_t index, const std::string& layerName) {
        auto geometries = feature.getGeometries();
        bucket->addGeometry(geometries);
        parameters.featureIndex.insert(geometries, index, layerName, name);
    });

    return std::move(bucket);
}

float FillLayer::getQueryRadius() const {
    const std::array<float, 2>& translate = paint.fillTranslate;
    return util::length(translate[0], translate[1]);
}

bool FillLayer::queryIntersectsGeometry(
        const GeometryCollection& queryGeometry,
        const GeometryCollection& geometry,
        const float bearing,
        const float pixelsToTileUnits) const {

    auto translatedQueryGeometry = FeatureIndex::translateQueryGeometry(
            queryGeometry, paint.fillTranslate, paint.fillTranslateAnchor, bearing, pixelsToTileUnits);

    return util::multiPolygonIntersectsMultiPolygon(translatedQueryGeometry.value_or(queryGeometry), geometry);
}

} // namespace mbgl