Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Pre-defined variable anchor at tile borders
Browse files Browse the repository at this point in the history
In tile mode, stick to the first variable anchor if the label intersects tile borders.
  • Loading branch information
pozdnyakov committed Jan 7, 2020
1 parent f08bf06 commit 1e5cbd7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/mbgl/text/collision_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ bool CollisionIndex::featureIntersectsTileBorders(const CollisionFeature& featur
float px2 = (box.x2 + shift.x) * tileToViewport + projectedPoint.first.x;
float py2 = (box.y2 + shift.y) * tileToViewport + projectedPoint.first.y;

return !intersectsTileBorders(px1, py1, px2, py2, tileEdges);
return intersectsTileBorders(px1, py1, px2, py2, tileEdges);
}

std::pair<bool,bool> CollisionIndex::placeFeature(const CollisionFeature& feature,
Expand Down
67 changes: 48 additions & 19 deletions src/mbgl/text/placement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,13 @@ void Placement::placeBucket(
auto partiallyEvaluatedTextSize = bucket.textSizeBinder->evaluateForZoom(state.getZoom());
auto partiallyEvaluatedIconSize = bucket.iconSizeBinder->evaluateForZoom(state.getZoom());

optional<CollisionTileBoundaries> tileBorders;
optional<CollisionTileBoundaries> avoidEdges;
if (mapMode == MapMode::Tile && (layout.get<style::SymbolAvoidEdges>() ||
layout.get<style::SymbolPlacement>() == style::SymbolPlacementType::Line ||
!layout.get<style::TextVariableAnchor>().empty())) {
avoidEdges = collisionIndex.projectTileBoundaries(posMatrix);
if (mapMode == MapMode::Tile) tileBorders = collisionIndex.projectTileBoundaries(posMatrix);

if (tileBorders && (layout.get<style::SymbolAvoidEdges>() ||
layout.get<style::SymbolPlacement>() == style::SymbolPlacementType::Line)) {
avoidEdges = tileBorders;
}

const bool textAllowOverlap = layout.get<style::TextAllowOverlap>();
Expand Down Expand Up @@ -262,14 +264,23 @@ void Placement::placeBucket(

// Line or point label placement
if (variableTextAnchors.empty()) {
const auto placeFeature = [&] (const CollisionFeature& collisionFeature, style::TextWritingModeType orientation) {
const auto placeFeature = [&](const CollisionFeature& collisionFeature,
style::TextWritingModeType orientation) {
textBoxes.clear();
auto placedFeature = collisionIndex.placeFeature(collisionFeature, {},
posMatrix, textLabelPlaneMatrix, pixelRatio,
placedSymbol, scale, fontSize,
auto placedFeature = collisionIndex.placeFeature(collisionFeature,
{},
posMatrix,
textLabelPlaneMatrix,
pixelRatio,
placedSymbol,
scale,
fontSize,
layout.get<style::TextAllowOverlap>(),
pitchWithMap,
params.showCollisionBoxes, avoidEdges, collisionGroup.second, textBoxes);
params.showCollisionBoxes,
avoidEdges,
collisionGroup.second,
textBoxes);
if (placedFeature.first) {
placedOrientations.emplace(symbolInstance.crossTileID, orientation);
}
Expand Down Expand Up @@ -329,8 +340,13 @@ void Placement::placeBucket(
std::pair<bool, bool> placedFeature = {false, false};
const size_t anchorsSize = variableTextAnchors.size();
const size_t placementAttempts = textAllowOverlap ? anchorsSize * 2 : anchorsSize;
const bool stickToFirstAnchor = tileBorders && !avoidEdges;

for (size_t i = 0u; i < placementAttempts; ++i) {
auto anchor = variableTextAnchors[i % anchorsSize];
const auto& avoidEdgesForVariableAnchor =
(stickToFirstAnchor && anchor != variableTextAnchors.front()) ? tileBorders : avoidEdges;

const bool allowOverlap = (i >= anchorsSize);
shift = calculateVariableLayoutOffset(anchor,
width,
Expand All @@ -352,7 +368,7 @@ void Placement::placeBucket(
allowOverlap,
pitchWithMap,
params.showCollisionBoxes,
avoidEdges,
avoidEdgesForVariableAnchor,
collisionGroup.second,
textBoxes);

Expand All @@ -368,7 +384,7 @@ void Placement::placeBucket(
iconAllowOverlap,
pitchWithMap,
params.showCollisionBoxes,
avoidEdges,
avoidEdgesForVariableAnchor,
collisionGroup.second,
iconBoxes);
iconBoxes.clear();
Expand Down Expand Up @@ -454,14 +470,21 @@ void Placement::placeBucket(
const auto& iconBuffer = symbolInstance.hasSdfIcon() ? bucket.sdfIcon : bucket.icon;
const PlacedSymbol& placedSymbol = iconBuffer.placedSymbols.at(*symbolInstance.placedIconIndex);
const float fontSize = evaluateSizeForFeature(partiallyEvaluatedIconSize, placedSymbol);
const auto& placeIconFeature = [&] (const CollisionFeature& collisionFeature) {
return collisionIndex.placeFeature(collisionFeature, shift,
posMatrix, iconLabelPlaneMatrix, pixelRatio,
placedSymbol, scale, fontSize,
const auto& placeIconFeature = [&](const CollisionFeature& collisionFeature) {
return collisionIndex.placeFeature(collisionFeature,
shift,
posMatrix,
iconLabelPlaneMatrix,
pixelRatio,
placedSymbol,
scale,
fontSize,
layout.get<style::IconAllowOverlap>(),
pitchWithMap,
params.showCollisionBoxes, avoidEdges,
collisionGroup.second, iconBoxes);
params.showCollisionBoxes,
avoidEdges,
collisionGroup.second,
iconBoxes);
};

std::pair<bool, bool> placedIcon = {false, false};
Expand Down Expand Up @@ -890,7 +913,13 @@ void Placement::updateBucketOpacities(SymbolBucket& bucket,
bucket.iconCollisionBox->dynamicVertices.extend(feature.boxes.size() * 4, dynamicVertex);
};

auto updateTextCollisionBox = [this, &bucket, &symbolInstance, &state, variablePlacement, rotateWithMap, pitchWithMap](const auto& feature, const bool placed) {
auto updateTextCollisionBox = [this,
&bucket,
&symbolInstance,
&state,
variablePlacement,
rotateWithMap,
pitchWithMap](const auto& feature, const bool placed) {
Point<float> shift{0.0f, 0.0f};
if (feature.alongLine) {
return shift;
Expand Down Expand Up @@ -923,7 +952,7 @@ void Placement::updateBucketOpacities(SymbolBucket& bucket,
bucket.textCollisionBox->dynamicVertices.extend(feature.boxes.size() * 4, dynamicVertex);
return shift;
};

auto updateCollisionCircles = [&](const auto& feature, const bool placed, bool isText) {
if (!feature.alongLine) {
return;
Expand Down

0 comments on commit 1e5cbd7

Please sign in to comment.