[core] fix missing icon collision boxes

port https://github.com/mapbox/mapbox-gl-js/pull/1981
This commit is contained in:
Ansis Brammanis
2016-01-22 17:57:59 -08:00
parent 556690fae8
commit 5cd123422e
3 changed files with 19 additions and 11 deletions

View File

@@ -26,7 +26,7 @@
],
"devDependencies": {
"aws-sdk": "^2.2.21",
"mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#1b9035a02f8f23bf72a197c2a8d8f910d935346f",
"mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#7a17d43bd8482a01dc165de6fff6ae4c33c4fc5d",
"node-gyp": "^3.2.1",
"request": "^2.67.0",
"tape": "^4.2.2"

View File

@@ -5,7 +5,7 @@ namespace mbgl {
CollisionFeature::CollisionFeature(const std::vector<Coordinate> &line, const Anchor &anchor,
const float top, const float bottom, const float left, const float right,
const float boxScale, const float padding, const bool alongLine) {
const float boxScale, const float padding, const bool alongLine, const bool straight) {
if (top == 0 && bottom == 0 && left == 0 && right == 0) return;
@@ -22,14 +22,24 @@ CollisionFeature::CollisionFeature(const std::vector<Coordinate> &line, const An
height = std::max(10.0f * boxScale, height);
bboxifyLabel(line, anchor, length, height);
Coordinate anchorPoint(int16_t(anchor.x), int16_t(anchor.y));
if (straight) {
// used for icon labels that are aligned with the line, but don't curve along it
const vec2<double> vector = util::unit(vec2<double>(line[anchor.segment + 1] - line[anchor.segment])) * length;
const std::vector<Coordinate> newLine({ anchorPoint - vector, anchorPoint + vector });
bboxifyLabel(newLine, anchorPoint, 0, length, height);
} else {
// used for text labels that curve along a line
bboxifyLabel(line, anchorPoint, anchor.segment, length, height);
}
} else {
boxes.emplace_back(anchor, x1, y1, x2, y2, std::numeric_limits<float>::infinity());
}
}
void CollisionFeature::bboxifyLabel(const std::vector<Coordinate> &line,
const Anchor &anchor, const float labelLength, const float boxSize) {
Coordinate &anchorPoint, const int segment, const float labelLength, const float boxSize) {
const float step = boxSize / 2;
const unsigned int nBoxes = std::floor(labelLength / step);
@@ -38,10 +48,8 @@ void CollisionFeature::bboxifyLabel(const std::vector<Coordinate> &line,
// box is at the edge of the label.
const float firstBoxOffset = -boxSize / 2;
Coordinate anchorPoint = Coordinate{ (int16_t)anchor.x, (int16_t)anchor.y };
Coordinate &p = anchorPoint;
int index = anchor.segment + 1;
int index = segment + 1;
float anchorDistance = firstBoxOffset;
// move backwards along the line to the first segment the label appears on

View File

@@ -37,7 +37,7 @@ namespace mbgl {
const float boxScale, const float padding, const bool alongLine)
: CollisionFeature(line, anchor,
shapedText.top, shapedText.bottom, shapedText.left, shapedText.right,
boxScale, padding, alongLine) {}
boxScale, padding, alongLine, false) {}
// for icons
inline explicit CollisionFeature(const std::vector<Coordinate> &line, const Anchor &anchor,
@@ -45,17 +45,17 @@ namespace mbgl {
const float boxScale, const float padding, const bool alongLine)
: CollisionFeature(line, anchor,
shapedIcon.top, shapedIcon.bottom, shapedIcon.left, shapedIcon.right,
boxScale, padding, alongLine) {}
boxScale, padding, alongLine, true) {}
explicit CollisionFeature(const std::vector<Coordinate> &line, const Anchor &anchor,
const float top, const float bottom, const float left, const float right,
const float boxScale, const float padding, const bool alongLine);
const float boxScale, const float padding, const bool alongLine, const bool straight);
std::vector<CollisionBox> boxes;
private:
void bboxifyLabel(const std::vector<Coordinate> &line, const Anchor &anchor, const float length, const float height);
void bboxifyLabel(const std::vector<Coordinate> &line, Coordinate &anchorPoint, const int segment, const float length, const float height);
};
} // namespace mbgl