summaryrefslogtreecommitdiff
path: root/src/mbgl/text/placement.cpp
blob: b7de01623f739db33c728834a437403a25cd28de (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <mbgl/text/placement.hpp>
#include <mbgl/renderer/render_layer.hpp>
#include <mbgl/renderer/layers/render_symbol_layer.hpp>
#include <mbgl/renderer/render_tile.hpp>
#include <mbgl/tile/geometry_tile.cpp>
#include <mbgl/renderer/buckets/symbol_bucket.hpp>
#include <mbgl/renderer/bucket.hpp>

namespace mbgl {

OpacityState::OpacityState(bool placed_) : opacity(0), placed(placed_) {}

OpacityState::OpacityState(const OpacityState& prevState, float increment, bool placed_) :
    opacity(std::fmax(0, std::fmin(1, prevState.opacity + (prevState.placed ? increment : -increment)))),
    placed(placed_) {}

bool OpacityState::isHidden() const {
    return opacity == 0 && !placed;
}

JointOpacityState::JointOpacityState(bool placedIcon, bool placedText) :
    icon(OpacityState(placedIcon)),
    text(OpacityState(placedText)) {}

JointOpacityState::JointOpacityState(const JointOpacityState& prevOpacityState, float increment, bool placedIcon, bool placedText) :
    icon(OpacityState(prevOpacityState.icon, increment, placedIcon)),
    text(OpacityState(prevOpacityState.text, increment, placedText)) {}

bool JointOpacityState::isHidden() const {
    return icon.isHidden() && text.isHidden();
}

Placement::Placement(const TransformState& state_, MapMode mapMode_)
    : collisionIndex(state_)
    , recentUntil(TimePoint::min())
    , state(state_)
    , mapMode(mapMode_)
{}

void Placement::placeLayer(RenderSymbolLayer& symbolLayer, const mat4& projMatrix, bool showCollisionBoxes) {

    std::unordered_set<uint32_t> seenCrossTileIDs;

    for (RenderTile& renderTile : symbolLayer.renderTiles) {

        if (!renderTile.tile.isRenderable()) {
            continue;
        }

        auto bucket = renderTile.tile.getBucket(*symbolLayer.baseImpl);
        assert(dynamic_cast<SymbolBucket*>(bucket));
        SymbolBucket& symbolBucket = *reinterpret_cast<SymbolBucket*>(bucket);

        auto& layout = symbolBucket.layout;

        const float pixelsToTileUnits = renderTile.id.pixelsToTileUnits(1, state.getZoom());

        const float scale = std::pow(2, state.getZoom() - renderTile.tile.id.overscaledZ);
        const float textPixelRatio = util::EXTENT / (util::tileSize * renderTile.tile.id.overscaleFactor());

        mat4 posMatrix;
        state.matrixFor(posMatrix, renderTile.id);
        matrix::multiply(posMatrix, projMatrix, posMatrix);

        mat4 textLabelPlaneMatrix = getLabelPlaneMatrix(renderTile.matrix,
                layout.get<TextPitchAlignment>() == style::AlignmentType::Map,
                layout.get<TextRotationAlignment>() == style::AlignmentType::Map,
                state,
                pixelsToTileUnits);

        mat4 iconLabelPlaneMatrix = getLabelPlaneMatrix(renderTile.matrix,
                layout.get<IconPitchAlignment>() == style::AlignmentType::Map,
                layout.get<IconRotationAlignment>() == style::AlignmentType::Map,
                state,
                pixelsToTileUnits);

        placeLayerBucket(symbolBucket, posMatrix, textLabelPlaneMatrix, iconLabelPlaneMatrix, scale, textPixelRatio, showCollisionBoxes, seenCrossTileIDs);
    }
}

void Placement::placeLayerBucket(
        SymbolBucket& bucket,
        const mat4& posMatrix,
        const mat4& textLabelPlaneMatrix,
        const mat4& iconLabelPlaneMatrix,
        const float scale,
        const float textPixelRatio,
        const bool showCollisionBoxes,
        std::unordered_set<uint32_t>& seenCrossTileIDs) {

    // TODO collision debug array clearing

    auto partiallyEvaluatedTextSize = bucket.textSizeBinder->evaluateForZoom(state.getZoom());
    auto partiallyEvaluatedIconSize = bucket.iconSizeBinder->evaluateForZoom(state.getZoom());

    const bool iconWithoutText = !bucket.hasTextData() || bucket.layout.get<TextOptional>();
    const bool textWithoutIcon = !bucket.hasIconData() || bucket.layout.get<IconOptional>();

    for (auto& symbolInstance : bucket.symbolInstances) {

        // TODO symbollayout handles a couple special cases related to this. copy those over if needed.
        auto anchor = symbolInstance.anchor;
        const bool withinPlus0 = anchor.point.x >= 0 && anchor.point.x < util::EXTENT && anchor.point.y >= 0 && anchor.point.y < util::EXTENT;
        if (!withinPlus0) continue;

        if (seenCrossTileIDs.count(symbolInstance.crossTileID) == 0) {
            bool placeText = false;
            bool placeIcon = false;

            if (symbolInstance.placedTextIndices.size()) {
                assert(symbolInstance.placedTextIndices.size() != 0);

                PlacedSymbol& placedSymbol = bucket.text.placedSymbols.at(symbolInstance.placedTextIndices.at(0));
                const float fontSize = evaluateSizeForFeature(partiallyEvaluatedTextSize, placedSymbol);

                placeText = collisionIndex.placeFeature(symbolInstance.textCollisionFeature,
                        posMatrix, textLabelPlaneMatrix, textPixelRatio,
                        placedSymbol, scale, fontSize,
                        bucket.layout.get<TextAllowOverlap>(),
                        bucket.layout.get<TextPitchAlignment>() == style::AlignmentType::Map,
                        showCollisionBoxes);
            }

            if (symbolInstance.placedIconIndices.size()) {

                PlacedSymbol& placedSymbol = bucket.icon.placedSymbols.at(symbolInstance.placedIconIndices.at(0));
                const float fontSize = evaluateSizeForFeature(partiallyEvaluatedIconSize, placedSymbol);

                placeIcon = collisionIndex.placeFeature(symbolInstance.iconCollisionFeature,
                        posMatrix, iconLabelPlaneMatrix, textPixelRatio,
                        placedSymbol, scale, fontSize,
                        bucket.layout.get<IconAllowOverlap>(),
                        bucket.layout.get<IconPitchAlignment>() == style::AlignmentType::Map,
                        showCollisionBoxes);
            }

            // combine placements for icon and text
            if (!iconWithoutText && !textWithoutIcon) {
                placeText = placeIcon = placeText && placeIcon;
            } else if (!textWithoutIcon) {
                placeText = placeText && placeIcon;
            } else if (!iconWithoutText) {
                placeIcon = placeText && placeIcon;
            }

            if (placeText) {
                collisionIndex.insertFeature(symbolInstance.textCollisionFeature, bucket.layout.get<TextIgnorePlacement>());
            }

            if (placeIcon) {
                collisionIndex.insertFeature(symbolInstance.iconCollisionFeature, bucket.layout.get<IconIgnorePlacement>());
            }

            assert(symbolInstance.crossTileID != 0);

            placements.emplace(symbolInstance.crossTileID, PlacementPair(placeText, placeIcon));
            seenCrossTileIDs.insert(symbolInstance.crossTileID);
        }
    } 
}

bool Placement::commit(const Placement& prevPlacement, TimePoint now) {
    commitTime = now;

    bool placementChanged = false;

    float increment = mapMode == MapMode::Still ? 1.0 : std::chrono::duration<float>(commitTime - prevPlacement.commitTime) / Duration(std::chrono::milliseconds(300));

    if (increment) {}
    // add the opacities from the current placement, and copy their current values from the previous placement
    for (auto& placementPair : placements) {
        auto prevOpacity = prevPlacement.opacities.find(placementPair.first);
        if (prevOpacity != prevPlacement.opacities.end()) {
            opacities.emplace(placementPair.first, JointOpacityState(prevOpacity->second, increment, placementPair.second.icon, placementPair.second.text));
            placementChanged = placementChanged ||
                placementPair.second.icon != prevOpacity->second.icon.placed ||
                placementPair.second.text != prevOpacity->second.text.placed;
        } else {
            opacities.emplace(placementPair.first, JointOpacityState(placementPair.second.icon, placementPair.second.text));
            placementChanged = placementChanged || placementPair.second.icon || placementPair.second.text;
        }
    }

    // copy and update values from the previous placement that aren't in the current placement but haven't finished fading
    for (auto& prevOpacity : prevPlacement.opacities) {
        if (opacities.find(prevOpacity.first) == opacities.end()) {
            JointOpacityState jointOpacity(prevOpacity.second, increment, false, false);
            if (!jointOpacity.isHidden()) {
                opacities.emplace(prevOpacity.first, jointOpacity);
                placementChanged = placementChanged || prevOpacity.second.icon.placed || prevOpacity.second.text.placed;
            }
        }
    }

    return placementChanged;
}

void Placement::updateLayerOpacities(RenderSymbolLayer& symbolLayer) {
    for (RenderTile& renderTile : symbolLayer.renderTiles) {
        if (!renderTile.tile.isRenderable()) {
            continue;
        }

        auto bucket = renderTile.tile.getBucket(*symbolLayer.baseImpl);
        assert(dynamic_cast<SymbolBucket*>(bucket));
        SymbolBucket& symbolBucket = *reinterpret_cast<SymbolBucket*>(bucket);
        updateBucketOpacities(symbolBucket);
    }
}

void Placement::updateBucketOpacities(SymbolBucket& bucket) {
    // TODO check if this clear is necessary, whether the vector has been moved out
    if (bucket.hasTextData()) bucket.text.opacityVertices.clear();
    if (bucket.hasIconData()) bucket.icon.opacityVertices.clear();
    if (bucket.hasCollisionBoxData()) bucket.collisionBox.opacityVertices.clear();
    if (bucket.hasCollisionCircleData()) bucket.collisionCircle.opacityVertices.clear();

    for (SymbolInstance& symbolInstance : bucket.symbolInstances) {
        auto opacityState = getOpacity(symbolInstance.crossTileID);

        // TODO check if hasText is the right thing here, or if there are cases where hasText is true but it's not added to the buffers
        if (symbolInstance.hasText) {
            auto opacityVertex = SymbolOpacityAttributes::vertex(opacityState.text.placed, opacityState.text.opacity);
            for (size_t i = 0; i < symbolInstance.horizontalGlyphQuads.size() * 4; i++) {
                bucket.text.opacityVertices.emplace_back(opacityVertex);
            }
            for (size_t i = 0; i < symbolInstance.verticalGlyphQuads.size() * 4; i++) {
                bucket.text.opacityVertices.emplace_back(opacityVertex);
            }
            // TODO On JS we avoid setting this if it hasn't chnaged, but it may be cheap enough here we don't care
            for (auto index : symbolInstance.placedTextIndices) {
                bucket.text.placedSymbols[index].hidden = opacityState.isHidden();
            }
        }
        if (symbolInstance.hasIcon) {
            auto opacityVertex = SymbolOpacityAttributes::vertex(opacityState.icon.placed, opacityState.icon.opacity);
            if (symbolInstance.iconQuad) {
                bucket.icon.opacityVertices.emplace_back(opacityVertex);
                bucket.icon.opacityVertices.emplace_back(opacityVertex);
                bucket.icon.opacityVertices.emplace_back(opacityVertex);
                bucket.icon.opacityVertices.emplace_back(opacityVertex);
            }
            // TODO On JS we avoid setting this if it hasn't chnaged, but it may be cheap enough here we don't care
            for (auto index : symbolInstance.placedIconIndices) {
                bucket.icon.placedSymbols[index].hidden = opacityState.isHidden();
            }
        }
        
        if (symbolInstance.insideTileBoundaries) {
            auto updateCollisionBox = [&](const auto& feature, const bool placed) {
                for (const CollisionBox& box : feature.boxes) {
                    if (feature.alongLine) {
                       auto opacityVertex = CollisionBoxOpacityAttributes::vertex(placed, !box.used);
                        bucket.collisionCircle.opacityVertices.emplace_back(opacityVertex);
                        bucket.collisionCircle.opacityVertices.emplace_back(opacityVertex);
                        bucket.collisionCircle.opacityVertices.emplace_back(opacityVertex);
                        bucket.collisionCircle.opacityVertices.emplace_back(opacityVertex);
                    } else {
                        auto opacityVertex = CollisionBoxOpacityAttributes::vertex(placed, false);
                        bucket.collisionBox.opacityVertices.emplace_back(opacityVertex);
                        bucket.collisionBox.opacityVertices.emplace_back(opacityVertex);
                        bucket.collisionBox.opacityVertices.emplace_back(opacityVertex);
                        bucket.collisionBox.opacityVertices.emplace_back(opacityVertex);
                    }
                }
            };
            updateCollisionBox(symbolInstance.textCollisionFeature, opacityState.text.placed);
            updateCollisionBox(symbolInstance.iconCollisionFeature, opacityState.icon.placed);
        }
    }

    bucket.updateOpacity();
    bucket.sortFeatures(state.getAngle());
}

JointOpacityState Placement::getOpacity(uint32_t crossTileSymbolID) const {
    auto it = opacities.find(crossTileSymbolID);
    if (it != opacities.end()) {
        return it->second;
    } else {
        return JointOpacityState(false, false);
    }

}

float Placement::symbolFadeChange(TimePoint now) const {
    if (mapMode == MapMode::Still) {
        return 1.0;
    }
    return std::chrono::duration<float>(now - commitTime) / Duration(std::chrono::milliseconds(300));
}

bool Placement::hasTransitions(TimePoint now) const {
    return symbolFadeChange(now) < 1.0 || stale;
}

} // namespace mbgl