Anchors resample(const GeometryCoordinates &line, const float offset, const float spacing, const float angleWindowSize, const float maxAngle, const float labelLength, const bool continuedLine, const bool placeAtMiddle) { const float halfLabelLength = labelLength / 2.0f; float lineLength = 0; for (auto it = line.begin(), end = line.end() - 1; it != end; it++) { lineLength += util::dist<float>(*(it), *(it + 1)); } float distance = 0; float markedDistance = offset - spacing; Anchors anchors; assert(spacing > 0.0); int i = 0; for (auto it = line.begin(), end = line.end() - 1; it != end; it++, i++) { const GeometryCoordinate &a = *(it); const GeometryCoordinate &b = *(it + 1); const float segmentDist = util::dist<float>(a, b); const float angle = util::angle_to(b, a); while (markedDistance + spacing < distance + segmentDist) { markedDistance += spacing; float t = (markedDistance - distance) / segmentDist, x = util::interpolate(float(a.x), float(b.x), t), y = util::interpolate(float(a.y), float(b.y), t); // Check that the point is within the tile boundaries and that // the label would fit before the beginning and end of the line // if placed at this point. if (x >= 0 && x < util::EXTENT && y >= 0 && y < util::EXTENT && markedDistance - halfLabelLength >= 0.0f && markedDistance + halfLabelLength <= lineLength) { Anchor anchor(::round(x), ::round(y), angle, 0.5f, i); if (!angleWindowSize || checkMaxAngle(line, anchor, labelLength, angleWindowSize, maxAngle)) { anchors.push_back(anchor); } } } distance += segmentDist; } if (!placeAtMiddle && anchors.empty() && !continuedLine) { // The first attempt at finding anchors at which labels can be placed failed. // Try again, but this time just try placing one anchor at the middle of the line. // This has the most effect for short lines in overscaled tiles, since the // initial offset used in overscaled tiles is calculated to align labels with positions in // parent tiles instead of placing the label as close to the beginning as possible. anchors = resample(line, distance / 2, spacing, angleWindowSize, maxAngle, labelLength, continuedLine, true); } return anchors; }
bool lineIntersectsLine(const GeometryCoordinates& lineA, const GeometryCoordinates& lineB) { if (lineA.size() == 0 || lineB.size() == 0) return false; for (auto i = lineA.begin(); i != lineA.end() - 1; i++) { auto& a0 = *i; auto& a1 = *(i + 1); for (auto j = lineB.begin(); j != lineB.end() - 1; j++) { auto& b0 = *j; auto& b1 = *(j + 1); if (lineSegmentIntersectsLineSegment(a0, a1, b0, b1)) return true; } } return false; }
bool polygonContainsPoint(const GeometryCoordinates& ring, const GeometryCoordinate& p) { bool c = false; for (auto i = ring.begin(), j = ring.end() - 1; i != ring.end(); j = i++) { auto& p1 = *i; auto& p2 = *j; if (((p1.y > p.y) != (p2.y > p.y)) && (p.x < float(p2.x - p1.x) * float(p.y - p1.y) / float(p2.y - p1.y) + p1.x)) { c = !c; } } return c; }
bool pointIntersectsBufferedLine(const GeometryCoordinate& p, const GeometryCoordinates& line, const float radius) { const float radiusSquared = radius * radius; if (line.size() == 1) return util::distSqr<float>(p, line.at(0)) < radiusSquared; if (line.size() == 0) return false; for (auto i = line.begin() + 1; i != line.end(); i++) { // Find line segments that have a distance <= radius^2 to p // In that case, we treat the line as "containing point p". auto& v = *(i - 1); auto& w = *i; if (distToSegmentSquared(p, v, w) < radiusSquared) return true; } return false; }