summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2019-12-09 15:23:12 +0200
committerTobrun <tobrun.van.nuland@gmail.com>2019-12-18 13:36:44 +0100
commit50cc1e99645e5d53f0036d2611e0d15e2b607707 (patch)
tree7e94706f2d2eb693e3bd7f044a6afa869ad8a9d0
parentcd337ffa27caff425045c5b492cc14ed9cf0e36d (diff)
downloadqtlocation-mapboxgl-50cc1e99645e5d53f0036d2611e0d15e2b607707.tar.gz
[core] Add unit test for calculateTileDistances
-rw-r--r--next/test/CMakeLists.txt1
-rw-r--r--src/mbgl/layout/symbol_layout.cpp6
-rw-r--r--src/mbgl/layout/symbol_layout.hpp3
-rw-r--r--test/test-files.json1
-rw-r--r--test/text/calculate_tile_distances.test.cpp27
5 files changed, 34 insertions, 4 deletions
diff --git a/next/test/CMakeLists.txt b/next/test/CMakeLists.txt
index 278595ab51..8a2e34fe9a 100644
--- a/next/test/CMakeLists.txt
+++ b/next/test/CMakeLists.txt
@@ -64,6 +64,7 @@ add_library(
${MBGL_ROOT}/test/style/style_layer.test.cpp
${MBGL_ROOT}/test/style/style_parser.test.cpp
${MBGL_ROOT}/test/text/bidi.test.cpp
+ ${MBGL_ROOT}/test/text/calculate_tile_distances.test.cpp
${MBGL_ROOT}/test/text/cross_tile_symbol_index.test.cpp
${MBGL_ROOT}/test/text/formatted.test.cpp
${MBGL_ROOT}/test/text/get_anchors.test.cpp
diff --git a/src/mbgl/layout/symbol_layout.cpp b/src/mbgl/layout/symbol_layout.cpp
index 2e339af482..fc8af2c076 100644
--- a/src/mbgl/layout/symbol_layout.cpp
+++ b/src/mbgl/layout/symbol_layout.cpp
@@ -693,12 +693,12 @@ bool SymbolLayout::anchorIsTooClose(const std::u16string& text, const float repe
// Analog of `addToLineVertexArray` in JS. This version doesn't need to build up a line array like the
// JS version does, but it uses the same logic to calculate tile distances.
-std::vector<float> CalculateTileDistances(const GeometryCoordinates& line, const Anchor& anchor) {
+std::vector<float> SymbolLayout::calculateTileDistances(const GeometryCoordinates& line, const Anchor& anchor) {
std::vector<float> tileDistances(line.size());
if (anchor.segment) {
std::size_t segment = *anchor.segment;
assert(segment < line.size());
- auto sumForwardLength = util::dist<float>(anchor.point, line[segment + 1]);
+ auto sumForwardLength = (segment + 1 < line.size()) ? util::dist<float>(anchor.point, line[segment + 1]) : .0f;
auto sumBackwardLength = util::dist<float>(anchor.point, line[segment]);
for (std::size_t i = segment + 1; i < line.size(); ++i) {
tileDistances[i] = sumForwardLength;
@@ -842,7 +842,7 @@ std::size_t SymbolLayout::addSymbolGlyphQuads(SymbolBucket& bucket,
symbolInstance.textOffset,
writingMode,
symbolInstance.line(),
- CalculateTileDistances(symbolInstance.line(), symbolInstance.anchor),
+ calculateTileDistances(symbolInstance.line(), symbolInstance.anchor),
placedIconIndex);
placedIndex = bucket.text.placedSymbols.size() - 1;
PlacedSymbol& placedSymbol = bucket.text.placedSymbols.back();
diff --git a/src/mbgl/layout/symbol_layout.hpp b/src/mbgl/layout/symbol_layout.hpp
index 8f6e4595e3..57a104bf45 100644
--- a/src/mbgl/layout/symbol_layout.hpp
+++ b/src/mbgl/layout/symbol_layout.hpp
@@ -55,7 +55,8 @@ public:
* @return std::array<float, 2> offset along x- and y- axis correspondingly.
*/
static std::array<float, 2> evaluateVariableOffset(style::SymbolAnchorType anchor, std::array<float, 2> textOffset);
-
+
+ static std::vector<float> calculateTileDistances(const GeometryCoordinates& line, const Anchor& anchor);
private:
void addFeature(const size_t,
diff --git a/test/test-files.json b/test/test-files.json
index 012b381f87..b997503877 100644
--- a/test/test-files.json
+++ b/test/test-files.json
@@ -66,6 +66,7 @@
"test/style/style_layer.test.cpp",
"test/style/style_parser.test.cpp",
"test/text/bidi.test.cpp",
+ "test/text/calculate_tile_distances.test.cpp",
"test/text/cross_tile_symbol_index.test.cpp",
"test/text/formatted.test.cpp",
"test/text/get_anchors.test.cpp",
diff --git a/test/text/calculate_tile_distances.test.cpp b/test/text/calculate_tile_distances.test.cpp
new file mode 100644
index 0000000000..990de3d4ec
--- /dev/null
+++ b/test/text/calculate_tile_distances.test.cpp
@@ -0,0 +1,27 @@
+#include <mbgl/test/util.hpp>
+
+#include <mbgl/geometry/anchor.hpp>
+#include <mbgl/layout/symbol_layout.hpp>
+#include <mbgl/tile/geometry_tile_data.hpp>
+
+using namespace mbgl;
+
+TEST(calculateTileDistances, Point) {
+ const GeometryCoordinates line = {Point<int16_t>{1, 1}};
+ const auto distances = SymbolLayout::calculateTileDistances(line, Anchor(1.0f, 1.0f, .0f, 0u));
+ EXPECT_EQ(distances.size(), 1u);
+ EXPECT_EQ(distances[0], .0f);
+}
+
+TEST(calculateTileDistances, Line) {
+ const GeometryCoordinates line = {
+ Point<int16_t>{1, 1}, Point<int16_t>{1, 2}, Point<int16_t>{1, 3}, Point<int16_t>{1, 4}};
+ const auto distances = SymbolLayout::calculateTileDistances(line, Anchor(1.0f, 3.0f, .0f, 2u));
+ EXPECT_EQ(distances, std::vector<float>({2.0f, 1.0f, 0.0f, 1.0f}));
+}
+
+TEST(calculateTileDistances, EmptySegment) {
+ const GeometryCoordinates line = {};
+ const auto distances = SymbolLayout::calculateTileDistances(line, Anchor(1.0f, 1.0f, .0f));
+ EXPECT_EQ(distances.size(), 0u);
+}