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

Commit

Permalink
[core] Introduce CollisionIndex::getProjectedCollisionBoundaries()
Browse files Browse the repository at this point in the history
In order to remove the repeated code.
  • Loading branch information
pozdnyakov committed Jan 8, 2020
1 parent 99d097b commit 3efb7ce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
36 changes: 18 additions & 18 deletions src/mbgl/text/collision_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,7 @@ bool CollisionIndex::featureIntersectsTileBorders(const CollisionFeature& featur
assert(!feature.alongLine);
assert(!feature.boxes.empty());
const CollisionBox& box = feature.boxes.front();
const auto projectedPoint = projectAndGetPerspectiveRatio(posMatrix, box.anchor);
const float tileToViewport = textPixelRatio * projectedPoint.second;
CollisionBoundaries collisionBoundaries{{
(box.x1 + shift.x) * tileToViewport + projectedPoint.first.x,
(box.y1 + shift.y) * tileToViewport + projectedPoint.first.y,
(box.x2 + shift.x) * tileToViewport + projectedPoint.first.x,
(box.y2 + shift.y) * tileToViewport + projectedPoint.first.y,
}};

auto collisionBoundaries = getProjectedCollisionBoundaries(posMatrix, shift, textPixelRatio, box);
return overlapsTile(collisionBoundaries, tileEdges) && !isInsideTile(collisionBoundaries, tileEdges);
}

Expand All @@ -134,15 +126,9 @@ std::pair<bool, bool> CollisionIndex::placeFeature(
assert(projectedBoxes.empty());
if (!feature.alongLine) {
const CollisionBox& box = feature.boxes.front();
const auto projectedPoint = projectAndGetPerspectiveRatio(posMatrix, box.anchor);
const float tileToViewport = textPixelRatio * projectedPoint.second;
float px1 = (box.x1 + shift.x) * tileToViewport + projectedPoint.first.x;
float py1 = (box.y1 + shift.y) * tileToViewport + projectedPoint.first.y;
float px2 = (box.x2 + shift.x) * tileToViewport + projectedPoint.first.x;
float py2 = (box.y2 + shift.y) * tileToViewport + projectedPoint.first.y;
projectedBoxes.emplace_back(px1, py1, px2, py2);

CollisionBoundaries collisionBoundaries{{px1, py1, px2, py2}};
auto collisionBoundaries = getProjectedCollisionBoundaries(posMatrix, shift, textPixelRatio, box);
projectedBoxes.emplace_back(
collisionBoundaries[0], collisionBoundaries[1], collisionBoundaries[2], collisionBoundaries[3]);
if ((avoidEdges && !isInsideTile(collisionBoundaries, *avoidEdges)) || !isInsideGrid(collisionBoundaries) ||
(!allowOverlap && collisionGrid.hitTest(projectedBoxes.back().box(), collisionGroupPredicate))) {
return { false, false };
Expand Down Expand Up @@ -413,4 +399,18 @@ Point<float> CollisionIndex::projectPoint(const mat4& posMatrix, const Point<flo
static_cast<float>((((-p[1] / p[3] + 1) / 2) * size.height) + viewportPadding) };
}

CollisionBoundaries CollisionIndex::getProjectedCollisionBoundaries(const mat4& posMatrix,
Point<float> shift,
float textPixelRatio,
const CollisionBox& box) const {
const auto projectedPoint = projectAndGetPerspectiveRatio(posMatrix, box.anchor);
const float tileToViewport = textPixelRatio * projectedPoint.second;
return CollisionBoundaries{{
(box.x1 + shift.x) * tileToViewport + projectedPoint.first.x,
(box.y1 + shift.y) * tileToViewport + projectedPoint.first.y,
(box.x2 + shift.x) * tileToViewport + projectedPoint.first.x,
(box.y2 + shift.y) * tileToViewport + projectedPoint.first.y,
}};
}

} // namespace mbgl
4 changes: 4 additions & 0 deletions src/mbgl/text/collision_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class CollisionIndex {
std::pair<float,float> projectAnchor(const mat4& posMatrix, const Point<float>& point) const;
std::pair<Point<float>,float> projectAndGetPerspectiveRatio(const mat4& posMatrix, const Point<float>& point) const;
Point<float> projectPoint(const mat4& posMatrix, const Point<float>& point) const;
CollisionBoundaries getProjectedCollisionBoundaries(const mat4& posMatrix,
Point<float> shift,
float textPixelRatio,
const CollisionBox& box) const;

const TransformState transformState;

Expand Down
7 changes: 4 additions & 3 deletions src/mbgl/text/placement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,10 @@ void Placement::placeBucket(const SymbolBucket& bucket,
return result;
};

std::stable_sort(sorted.begin(), sorted.end(), [&](const SymbolInstance& a, const SymbolInstance& b) {
return intersectsTileBorder(a) && !intersectsTileBorder(b);
});
std::stable_sort(
sorted.begin(), sorted.end(), [&intersectsTileBorder](const SymbolInstance& a, const SymbolInstance& b) {
return intersectsTileBorder(a) && !intersectsTileBorder(b);
});

for (const SymbolInstance& symbol : sorted) {
placeSymbol(symbol);
Expand Down

0 comments on commit 3efb7ce

Please sign in to comment.