port clipLines from -js

This commit is contained in:
Ansis Brammanis
2015-04-02 11:54:28 -07:00
parent 2767a9b04e
commit 07b85043c8
3 changed files with 90 additions and 2 deletions

View File

@@ -18,6 +18,7 @@
#include <mbgl/util/token.hpp>
#include <mbgl/util/math.hpp>
#include <mbgl/util/merge_lines.hpp>
#include <mbgl/util/clip_lines.hpp>
#include <mbgl/util/std.hpp>
namespace mbgl {
@@ -269,9 +270,11 @@ void SymbolBucket::addFeature(const std::vector<std::vector<Coordinate>> &lines,
layout.icon.rotation_alignment == RotationAlignmentType::Map &&
layout.placement == PlacementType::Line;
// TODO clip lines here
auto& clippedLines = layout.placement == PlacementType::Line ?
util::clipLines(lines, 0, 0, 4096, 4096) :
lines;
for (const std::vector<Coordinate> &line : lines) {
for (const std::vector<Coordinate> &line : clippedLines) {
if (!line.size()) continue;
// Calculate the anchor points around which you want to place labels

View File

@@ -0,0 +1,66 @@
#include "clip_lines.hpp"
namespace mbgl {
namespace util {
std::vector<std::vector<Coordinate>> clipLines(const std::vector<std::vector<Coordinate>> &lines,
const int16_t x1, const int16_t y1, const int16_t x2, const int16_t y2) {
std::vector<std::vector<Coordinate>> clippedLines;
for (auto& line : lines) {
if (!line.size())
continue;
auto end = line.end() - 1;
for (auto it = line.begin(); it != end; it++) {
Coordinate p0 = *(it);
Coordinate p1 = *(it + 1);
if (p0.x < x1 && p1.x < x1) {
continue;
} else if (p0.x < x1) {
p0 = { x1, static_cast<int16_t>(p0.y + static_cast<float>(p1.y - p0.y) * ((x1 - p0.x) / (p1.x - p0.x))) };
} else if (p1.x < x1) {
p1 = { x1, static_cast<int16_t>(p0.y + static_cast<float>(p1.y - p0.y) * ((x1 - p0.x) / (p1.x - p0.x))) };
}
if (p0.y < y1 && p1.y < y1) {
continue;
} else if (p0.y < y1) {
p0 = { static_cast<int16_t>(p0.x + static_cast<float>(p1.x - p0.x) * ((y1 - p0.y) / (p1.y - p0.y))), y1 };
} else if (p1.y < y1) {
p1 = { static_cast<int16_t>(p0.x + static_cast<float>(p1.x - p0.x) * ((y1 - p0.y) / (p1.y - p0.y))), y1 };
}
if (p0.x >= x2 && p1.x >= x2) {
continue;
} else if (p0.x >= x2) {
p0 = { x2, static_cast<int16_t>(p0.y + static_cast<float>(p1.y - p0.y) * ((x2 - p0.x) / (p1.x - p0.x))) };
} else if (p1.x >= x2) {
p1 = { x2, static_cast<int16_t>(p0.y + static_cast<float>(p1.y - p0.y) * ((x2 - p0.x) / (p1.x - p0.x))) };
}
if (p0.y >= y2 && p1.y >= y2) {
continue;
} else if (p0.y >= y2) {
p0 = { static_cast<int16_t>(p0.x + static_cast<float>(p1.x - p0.x) * ((y2 - p0.y) / (p1.y - p0.y))), y2 };
} else if (p1.y >= y2) {
p1 = { static_cast<int16_t>(p0.x + static_cast<float>(p1.x - p0.x) * ((y2 - p0.y) / (p1.y - p0.y))), y2 };
}
if (!clippedLines.size() || (clippedLines.back().size() && !(p0 == clippedLines.back().back()))) {
clippedLines.emplace_back();
clippedLines.back().push_back(p0);
}
clippedLines.back().push_back(p1);
}
}
return clippedLines;
}
} // end namespace util
} // end namespace mbgl

View File

@@ -0,0 +1,19 @@
#ifndef MBGL_UTIL_CLIP_LINES
#define MBGL_UTIL_CLIP_LINES
#include <map>
#include <string>
#include <vector>
#include <mbgl/renderer/symbol_bucket.hpp>
namespace mbgl {
namespace util {
std::vector<std::vector<Coordinate>> clipLines(const std::vector<std::vector<Coordinate>> &lines,
const int16_t x1, const int16_t y1, const int16_t x2, const int16_t y2);
} // end namespace util
} // end namespace mbgl
#endif