port bboxifyLabel from -js

This commit is contained in:
Ansis Brammanis
2015-04-01 19:03:01 -07:00
parent c200053951
commit 1d50485e5b
5 changed files with 65 additions and 13 deletions

View File

@@ -1,4 +1,5 @@
#include <mbgl/text/collision_feature.hpp>
#include <mbgl/util/math.hpp>
namespace mbgl {
@@ -24,10 +25,65 @@ CollisionFeature::CollisionFeature(const std::vector<Coordinate> &line, const An
}
void CollisionFeature::bboxifyLabel(const std::vector<Coordinate> &line,
const Anchor &anchor, const float length, const float height) {
const Anchor &anchor, const float labelLength, const float boxSize) {
if (length && height && line.size() && anchor.x) {
fprintf(stderr, "");
const float step = boxSize / 2;
const unsigned int nBoxes = std::floor(labelLength / step);
// offset the center of the first box by half a box so that the edge of the
// 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;
float anchorDistance = firstBoxOffset;
// move backwards along the line to the first segment the label appears on
while (anchorDistance > -labelLength / 2) {
index--;
// there isn't enough room for the label after the beginning of the line
// checkMaxAngle should have already caught this
if (index < 0) return;
anchorDistance -= util::dist<float>(line[index], p);
p = line[index];
}
float segmentLength = util::dist<float>(line[index], line[index + 1]);
for (unsigned int i = 0; i < nBoxes; i++) {
// the distance the box will be from the anchor
const float boxDistanceToAnchor = -labelLength / 2 + i * step;
// the box is not on the current segment. Move to the next segment.
while (anchorDistance + segmentLength < boxDistanceToAnchor) {
anchorDistance += segmentLength;
index++;
// There isn't enough room before the end of the line.
if (index + 1 >= (int)line.size()) return;
segmentLength = util::dist<float>(line[index], line[index + 1]);
}
// the distance the box will be from the beginning of the segment
const float segmentBoxDistance = boxDistanceToAnchor - anchorDistance;
const auto& p0 = line[index];
const auto& p1 = line[index + 1];
vec2<float> boxAnchor = {
p0.x + segmentBoxDistance / segmentLength * (p1.x - p0.x),
p0.y + segmentBoxDistance / segmentLength * (p1.y - p0.y)
};
const float distanceToInnerEdge = std::max(std::fabs(boxDistanceToAnchor - firstBoxOffset) - step / 2, 0.0f);
const float maxScale = labelLength / 2 / distanceToInnerEdge;
boxes.emplace_back(boxAnchor, -boxSize / 2, -boxSize / 2, boxSize / 2, boxSize / 2, maxScale);
}
}