From 1f375581c3a31adea9281a7494a73e5c8f1c9a17 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 13 Sep 2016 13:44:20 -0700 Subject: [core] Move {clip,merge}_lines to layout directory --- cmake/core-files.cmake | 8 +-- src/mbgl/layout/clip_lines.cpp | 68 ++++++++++++++++++++++++ src/mbgl/layout/clip_lines.hpp | 16 ++++++ src/mbgl/layout/merge_lines.cpp | 106 ++++++++++++++++++++++++++++++++++++++ src/mbgl/layout/merge_lines.hpp | 26 ++++++++++ src/mbgl/layout/symbol_layout.cpp | 4 +- src/mbgl/util/clip_lines.cpp | 68 ------------------------ src/mbgl/util/clip_lines.hpp | 16 ------ src/mbgl/util/merge_lines.cpp | 106 -------------------------------------- src/mbgl/util/merge_lines.hpp | 26 ---------- test/util/merge_lines.cpp | 2 +- 11 files changed, 223 insertions(+), 223 deletions(-) create mode 100644 src/mbgl/layout/clip_lines.cpp create mode 100644 src/mbgl/layout/clip_lines.hpp create mode 100644 src/mbgl/layout/merge_lines.cpp create mode 100644 src/mbgl/layout/merge_lines.hpp delete mode 100644 src/mbgl/util/clip_lines.cpp delete mode 100644 src/mbgl/util/clip_lines.hpp delete mode 100644 src/mbgl/util/merge_lines.cpp delete mode 100644 src/mbgl/util/merge_lines.hpp diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index 20aa4dfd0c..851fe4bd56 100644 --- a/cmake/core-files.cmake +++ b/cmake/core-files.cmake @@ -80,6 +80,10 @@ set(MBGL_CORE_FILES src/mbgl/gl/object_store.hpp # layout + src/mbgl/layout/clip_lines.cpp + src/mbgl/layout/clip_lines.hpp + src/mbgl/layout/merge_lines.cpp + src/mbgl/layout/merge_lines.hpp src/mbgl/layout/symbol_layout.cpp src/mbgl/layout/symbol_layout.hpp @@ -410,8 +414,6 @@ set(MBGL_CORE_FILES src/mbgl/util/chrono.cpp src/mbgl/util/clip_id.cpp src/mbgl/util/clip_id.hpp - src/mbgl/util/clip_lines.cpp - src/mbgl/util/clip_lines.hpp src/mbgl/util/color.cpp src/mbgl/util/compression.cpp src/mbgl/util/constants.cpp @@ -443,8 +445,6 @@ set(MBGL_CORE_FILES src/mbgl/util/mat4.hpp src/mbgl/util/math.cpp src/mbgl/util/math.hpp - src/mbgl/util/merge_lines.cpp - src/mbgl/util/merge_lines.hpp src/mbgl/util/premultiply.cpp src/mbgl/util/premultiply.hpp src/mbgl/util/rapidjson.hpp diff --git a/src/mbgl/layout/clip_lines.cpp b/src/mbgl/layout/clip_lines.cpp new file mode 100644 index 0000000000..05b9103f69 --- /dev/null +++ b/src/mbgl/layout/clip_lines.cpp @@ -0,0 +1,68 @@ +#include + +#include + +namespace mbgl { +namespace util { + +GeometryCollection clipLines(const GeometryCollection &lines, + const int16_t x1, const int16_t y1, const int16_t x2, const int16_t y2) { + + GeometryCollection clippedLines; + + for (auto& line : lines) { + + if (line.empty()) + continue; + + auto end = line.end() - 1; + for (auto it = line.begin(); it != end; it++) { + GeometryCoordinate p0 = *(it); + GeometryCoordinate p1 = *(it + 1); + + if (p0.x < x1 && p1.x < x1) { + continue; + } else if (p0.x < x1) { + p0 = { x1, static_cast(::round(p0.y + (p1.y - p0.y) * ((float)(x1 - p0.x) / (p1.x - p0.x)))) }; + } else if (p1.x < x1) { + p1 = { x1, static_cast(::round(p0.y + (p1.y - p0.y) * ((float)(x1 - p0.x) / (p1.x - p0.x)))) }; + } + + if (p0.y < y1 && p1.y < y1) { + continue; + } else if (p0.y < y1) { + p0 = { static_cast(::round(p0.x + (p1.x - p0.x) * ((float)(y1 - p0.y) / (p1.y - p0.y)))), y1 }; + } else if (p1.y < y1) { + p1 = { static_cast(::round(p0.x + (p1.x - p0.x) * ((float)(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(::round(p0.y + (p1.y - p0.y) * ((float)(x2 - p0.x) / (p1.x - p0.x)))) }; + } else if (p1.x >= x2) { + p1 = { x2, static_cast(::round(p0.y + (p1.y - p0.y) * ((float)(x2 - p0.x) / (p1.x - p0.x)))) }; + } + + if (p0.y >= y2 && p1.y >= y2) { + continue; + } else if (p0.y >= y2) { + p0 = { static_cast(::round(p0.x + (p1.x - p0.x) * ((float)(y2 - p0.y) / (p1.y - p0.y)))), y2 }; + } else if (p1.y >= y2) { + p1 = { static_cast(::round(p0.x + (p1.x - p0.x) * ((float)(y2 - p0.y) / (p1.y - p0.y)))), y2 }; + } + + if (clippedLines.empty() || (!clippedLines.back().empty() && !(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 diff --git a/src/mbgl/layout/clip_lines.hpp b/src/mbgl/layout/clip_lines.hpp new file mode 100644 index 0000000000..c1cfd4bbb9 --- /dev/null +++ b/src/mbgl/layout/clip_lines.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include +#include +#include +#include + +namespace mbgl { +namespace util { + + +GeometryCollection clipLines(const GeometryCollection &lines, + const int16_t x1, const int16_t y1, const int16_t x2, const int16_t y2); + +} // end namespace util +} // end namespace mbgl diff --git a/src/mbgl/layout/merge_lines.cpp b/src/mbgl/layout/merge_lines.cpp new file mode 100644 index 0000000000..1e33a655e7 --- /dev/null +++ b/src/mbgl/layout/merge_lines.cpp @@ -0,0 +1,106 @@ +#include + +#include + +namespace mbgl { +namespace util { + +using Index = std::unordered_map; + +unsigned int mergeFromRight(std::vector &features, + Index &rightIndex, + Index::iterator left, + size_t rightKey, + GeometryCollection &geom) { + + unsigned int index = left->second; + rightIndex.erase(left); + rightIndex[rightKey] = index; + features[index].geometry[0].pop_back(); + features[index].geometry[0].insert( + features[index].geometry[0].end(), geom[0].begin(), geom[0].end()); + geom[0].clear(); + return index; +} + +unsigned int mergeFromLeft(std::vector &features, + Index &leftIndex, + size_t leftKey, + Index::iterator right, + GeometryCollection &geom) { + + unsigned int index = right->second; + leftIndex.erase(right); + leftIndex[leftKey] = index; + geom[0].pop_back(); + geom[0].insert( + geom[0].end(), features[index].geometry[0].begin(), features[index].geometry[0].end()); + features[index].geometry[0].clear(); + std::swap(features[index].geometry[0], geom[0]); + return index; +} + +enum class Side { + Left = false, + Right = true, +}; + +size_t +getKey(const std::u32string& text, const GeometryCollection& geom, Side side) { + const GeometryCoordinate& coord = side == Side::Right ? geom[0].back() : geom[0].front(); + + auto hash = std::hash()(text); + boost::hash_combine(hash, coord.x); + boost::hash_combine(hash, coord.y); + return hash; +} + +void mergeLines(std::vector &features) { + + Index leftIndex; + Index rightIndex; + + for (unsigned int k = 0; k < features.size(); k++) { + SymbolFeature &feature = features[k]; + GeometryCollection &geometry = feature.geometry; + + if (!feature.label.length()) { + continue; + } + + const auto leftKey = getKey(feature.label, geometry, Side::Left); + const auto rightKey = getKey(feature.label, geometry, Side::Right); + + const auto left = rightIndex.find(leftKey); + const auto right = leftIndex.find(rightKey); + + if ((left != rightIndex.end()) && (right != leftIndex.end()) && + (left->second != right->second)) { + // found lines with the same text adjacent to both ends of the current line, merge all + // three + unsigned int j = mergeFromLeft(features, leftIndex, leftKey, right, geometry); + unsigned int i = + mergeFromRight(features, rightIndex, left, rightKey, features[j].geometry); + + leftIndex.erase(leftKey); + rightIndex.erase(rightKey); + rightIndex[getKey(feature.label, features[i].geometry, Side::Right)] = i; + + } else if (left != rightIndex.end()) { + // found mergeable line adjacent to the start of the current line, merge + mergeFromRight(features, rightIndex, left, rightKey, geometry); + + } else if (right != leftIndex.end()) { + // found mergeable line adjacent to the end of the current line, merge + mergeFromLeft(features, leftIndex, leftKey, right, geometry); + + } else { + // no adjacent lines, add as a new item + leftIndex[leftKey] = k; + rightIndex[rightKey] = k; + } + } +} + +} // end namespace util +} // end namespace mbgl diff --git a/src/mbgl/layout/merge_lines.hpp b/src/mbgl/layout/merge_lines.hpp new file mode 100644 index 0000000000..bb4c01f6a2 --- /dev/null +++ b/src/mbgl/layout/merge_lines.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include +#include +#include +#include + +namespace mbgl { +namespace util { + +unsigned int mergeFromRight(std::vector &features, + std::unordered_map &rightIndex, + std::unordered_map::iterator left, + std::string &rightKey, + GeometryCollection &geom); + +unsigned int mergeFromLeft(std::vector &features, + std::unordered_map &leftIndex, + std::string &leftKey, + std::unordered_map::iterator right, + GeometryCollection &geom); + +void mergeLines(std::vector &features); + +} // end namespace util +} // end namespace mbgl diff --git a/src/mbgl/layout/symbol_layout.cpp b/src/mbgl/layout/symbol_layout.cpp index aa22eb428d..2a85ead32b 100644 --- a/src/mbgl/layout/symbol_layout.cpp +++ b/src/mbgl/layout/symbol_layout.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include @@ -10,8 +12,6 @@ #include #include #include -#include -#include #include #include #include diff --git a/src/mbgl/util/clip_lines.cpp b/src/mbgl/util/clip_lines.cpp deleted file mode 100644 index 81021f4b5b..0000000000 --- a/src/mbgl/util/clip_lines.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#include - -#include - -namespace mbgl { -namespace util { - -GeometryCollection clipLines(const GeometryCollection &lines, - const int16_t x1, const int16_t y1, const int16_t x2, const int16_t y2) { - - GeometryCollection clippedLines; - - for (auto& line : lines) { - - if (line.empty()) - continue; - - auto end = line.end() - 1; - for (auto it = line.begin(); it != end; it++) { - GeometryCoordinate p0 = *(it); - GeometryCoordinate p1 = *(it + 1); - - if (p0.x < x1 && p1.x < x1) { - continue; - } else if (p0.x < x1) { - p0 = { x1, static_cast(::round(p0.y + (p1.y - p0.y) * ((float)(x1 - p0.x) / (p1.x - p0.x)))) }; - } else if (p1.x < x1) { - p1 = { x1, static_cast(::round(p0.y + (p1.y - p0.y) * ((float)(x1 - p0.x) / (p1.x - p0.x)))) }; - } - - if (p0.y < y1 && p1.y < y1) { - continue; - } else if (p0.y < y1) { - p0 = { static_cast(::round(p0.x + (p1.x - p0.x) * ((float)(y1 - p0.y) / (p1.y - p0.y)))), y1 }; - } else if (p1.y < y1) { - p1 = { static_cast(::round(p0.x + (p1.x - p0.x) * ((float)(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(::round(p0.y + (p1.y - p0.y) * ((float)(x2 - p0.x) / (p1.x - p0.x)))) }; - } else if (p1.x >= x2) { - p1 = { x2, static_cast(::round(p0.y + (p1.y - p0.y) * ((float)(x2 - p0.x) / (p1.x - p0.x)))) }; - } - - if (p0.y >= y2 && p1.y >= y2) { - continue; - } else if (p0.y >= y2) { - p0 = { static_cast(::round(p0.x + (p1.x - p0.x) * ((float)(y2 - p0.y) / (p1.y - p0.y)))), y2 }; - } else if (p1.y >= y2) { - p1 = { static_cast(::round(p0.x + (p1.x - p0.x) * ((float)(y2 - p0.y) / (p1.y - p0.y)))), y2 }; - } - - if (clippedLines.empty() || (!clippedLines.back().empty() && !(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 diff --git a/src/mbgl/util/clip_lines.hpp b/src/mbgl/util/clip_lines.hpp deleted file mode 100644 index c1cfd4bbb9..0000000000 --- a/src/mbgl/util/clip_lines.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace mbgl { -namespace util { - - -GeometryCollection clipLines(const GeometryCollection &lines, - const int16_t x1, const int16_t y1, const int16_t x2, const int16_t y2); - -} // end namespace util -} // end namespace mbgl diff --git a/src/mbgl/util/merge_lines.cpp b/src/mbgl/util/merge_lines.cpp deleted file mode 100644 index 663f757dca..0000000000 --- a/src/mbgl/util/merge_lines.cpp +++ /dev/null @@ -1,106 +0,0 @@ -#include "merge_lines.hpp" - -#include - -namespace mbgl { -namespace util { - -using Index = std::unordered_map; - -unsigned int mergeFromRight(std::vector &features, - Index &rightIndex, - Index::iterator left, - size_t rightKey, - GeometryCollection &geom) { - - unsigned int index = left->second; - rightIndex.erase(left); - rightIndex[rightKey] = index; - features[index].geometry[0].pop_back(); - features[index].geometry[0].insert( - features[index].geometry[0].end(), geom[0].begin(), geom[0].end()); - geom[0].clear(); - return index; -} - -unsigned int mergeFromLeft(std::vector &features, - Index &leftIndex, - size_t leftKey, - Index::iterator right, - GeometryCollection &geom) { - - unsigned int index = right->second; - leftIndex.erase(right); - leftIndex[leftKey] = index; - geom[0].pop_back(); - geom[0].insert( - geom[0].end(), features[index].geometry[0].begin(), features[index].geometry[0].end()); - features[index].geometry[0].clear(); - std::swap(features[index].geometry[0], geom[0]); - return index; -} - -enum class Side { - Left = false, - Right = true, -}; - -size_t -getKey(const std::u32string& text, const GeometryCollection& geom, Side side) { - const GeometryCoordinate& coord = side == Side::Right ? geom[0].back() : geom[0].front(); - - auto hash = std::hash()(text); - boost::hash_combine(hash, coord.x); - boost::hash_combine(hash, coord.y); - return hash; -} - -void mergeLines(std::vector &features) { - - Index leftIndex; - Index rightIndex; - - for (unsigned int k = 0; k < features.size(); k++) { - SymbolFeature &feature = features[k]; - GeometryCollection &geometry = feature.geometry; - - if (!feature.label.length()) { - continue; - } - - const auto leftKey = getKey(feature.label, geometry, Side::Left); - const auto rightKey = getKey(feature.label, geometry, Side::Right); - - const auto left = rightIndex.find(leftKey); - const auto right = leftIndex.find(rightKey); - - if ((left != rightIndex.end()) && (right != leftIndex.end()) && - (left->second != right->second)) { - // found lines with the same text adjacent to both ends of the current line, merge all - // three - unsigned int j = mergeFromLeft(features, leftIndex, leftKey, right, geometry); - unsigned int i = - mergeFromRight(features, rightIndex, left, rightKey, features[j].geometry); - - leftIndex.erase(leftKey); - rightIndex.erase(rightKey); - rightIndex[getKey(feature.label, features[i].geometry, Side::Right)] = i; - - } else if (left != rightIndex.end()) { - // found mergeable line adjacent to the start of the current line, merge - mergeFromRight(features, rightIndex, left, rightKey, geometry); - - } else if (right != leftIndex.end()) { - // found mergeable line adjacent to the end of the current line, merge - mergeFromLeft(features, leftIndex, leftKey, right, geometry); - - } else { - // no adjacent lines, add as a new item - leftIndex[leftKey] = k; - rightIndex[rightKey] = k; - } - } -} - -} // end namespace util -} // end namespace mbgl diff --git a/src/mbgl/util/merge_lines.hpp b/src/mbgl/util/merge_lines.hpp deleted file mode 100644 index bb4c01f6a2..0000000000 --- a/src/mbgl/util/merge_lines.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace mbgl { -namespace util { - -unsigned int mergeFromRight(std::vector &features, - std::unordered_map &rightIndex, - std::unordered_map::iterator left, - std::string &rightKey, - GeometryCollection &geom); - -unsigned int mergeFromLeft(std::vector &features, - std::unordered_map &leftIndex, - std::string &leftKey, - std::unordered_map::iterator right, - GeometryCollection &geom); - -void mergeLines(std::vector &features); - -} // end namespace util -} // end namespace mbgl diff --git a/test/util/merge_lines.cpp b/test/util/merge_lines.cpp index 4b6bad15f6..4b8b0c4e79 100644 --- a/test/util/merge_lines.cpp +++ b/test/util/merge_lines.cpp @@ -1,6 +1,6 @@ #include -#include +#include const std::u32string aaa = U"a"; const std::u32string bbb = U"b"; -- cgit v1.2.1