summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2019-12-09 10:41:27 +0200
committerTobrun <tobrun.van.nuland@gmail.com>2019-12-18 13:36:44 +0100
commitcd337ffa27caff425045c5b492cc14ed9cf0e36d (patch)
tree429c8679f7619be52ec793babadf8a9c5727ecab
parent8e9538110a4fc7a69762f96f0d6ab08ed4c77432 (diff)
downloadqtlocation-mapboxgl-cd337ffa27caff425045c5b492cc14ed9cf0e36d.tar.gz
[core] Add unit test for getAnchors and remove unused field
-rw-r--r--next/test/CMakeLists.txt1
-rw-r--r--src/mbgl/geometry/anchor.hpp5
-rw-r--r--src/mbgl/text/get_anchors.cpp8
-rw-r--r--test/test-files.json1
-rw-r--r--test/text/get_anchors.test.cpp130
-rw-r--r--test/text/quads.test.cpp4
6 files changed, 140 insertions, 9 deletions
diff --git a/next/test/CMakeLists.txt b/next/test/CMakeLists.txt
index 363e52c95d..278595ab51 100644
--- a/next/test/CMakeLists.txt
+++ b/next/test/CMakeLists.txt
@@ -66,6 +66,7 @@ add_library(
${MBGL_ROOT}/test/text/bidi.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
${MBGL_ROOT}/test/text/glyph_manager.test.cpp
${MBGL_ROOT}/test/text/glyph_pbf.test.cpp
${MBGL_ROOT}/test/text/language_tag.test.cpp
diff --git a/src/mbgl/geometry/anchor.hpp b/src/mbgl/geometry/anchor.hpp
index 1a2cbd3dac..f29796bf48 100644
--- a/src/mbgl/geometry/anchor.hpp
+++ b/src/mbgl/geometry/anchor.hpp
@@ -11,11 +11,10 @@ class Anchor {
public:
Point<float> point;
float angle = 0.0f;
- float scale = 0.0f;
optional<std::size_t> segment;
- Anchor(float x_, float y_, float angle_, float scale_, optional<std::size_t> segment_ = nullopt)
- : point(x_, y_), angle(angle_), scale(scale_), segment(segment_) {}
+ Anchor(float x_, float y_, float angle_, optional<std::size_t> segment_ = nullopt)
+ : point(x_, y_), angle(angle_), segment(segment_) {}
};
using Anchors = std::vector<Anchor>;
diff --git a/src/mbgl/text/get_anchors.cpp b/src/mbgl/text/get_anchors.cpp
index a2401a020a..714bc79fea 100644
--- a/src/mbgl/text/get_anchors.cpp
+++ b/src/mbgl/text/get_anchors.cpp
@@ -61,7 +61,7 @@ static Anchors resample(const GeometryCoordinates& line,
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);
+ Anchor anchor(::round(x), ::round(y), angle, i);
if (!angleWindowSize || checkMaxAngle(line, anchor, labelLength, angleWindowSize, maxAngle)) {
anchors.push_back(anchor);
@@ -160,9 +160,9 @@ optional<Anchor> getCenterAnchor(const GeometryCoordinates& line,
float t = (centerDistance - prevDistance) / segmentDistance,
x = util::interpolate(float(a.x), float(b.x), t),
y = util::interpolate(float(a.y), float(b.y), t);
-
- Anchor anchor(::round(x), ::round(y), util::angle_to(b, a), 0.5f, i);
-
+
+ Anchor anchor(::round(x), ::round(y), util::angle_to(b, a), i);
+
if (!angleWindowSize || checkMaxAngle(line, anchor, labelLength, angleWindowSize, maxAngle)) {
return anchor;
}
diff --git a/test/test-files.json b/test/test-files.json
index d388788212..012b381f87 100644
--- a/test/test-files.json
+++ b/test/test-files.json
@@ -68,6 +68,7 @@
"test/text/bidi.test.cpp",
"test/text/cross_tile_symbol_index.test.cpp",
"test/text/formatted.test.cpp",
+ "test/text/get_anchors.test.cpp",
"test/text/glyph_manager.test.cpp",
"test/text/glyph_pbf.test.cpp",
"test/text/language_tag.test.cpp",
diff --git a/test/text/get_anchors.test.cpp b/test/text/get_anchors.test.cpp
new file mode 100644
index 0000000000..84dea4394f
--- /dev/null
+++ b/test/text/get_anchors.test.cpp
@@ -0,0 +1,130 @@
+#include <mbgl/test/util.hpp>
+
+#include <mbgl/text/get_anchors.hpp>
+
+using namespace mbgl;
+
+namespace mbgl {
+constexpr inline bool operator==(const Anchor& rhs, const Anchor& lhs) {
+ return rhs.point == lhs.point && rhs.angle == lhs.angle && rhs.segment == lhs.segment;
+}
+} // namespace mbgl
+
+namespace {
+const auto makeLine = [](std::size_t shift = 0u) {
+ GeometryCoordinates line;
+ for (std::size_t i = shift; i < 10u + shift; ++i) {
+ line.push_back({1, static_cast<int16_t>(i)});
+ }
+ return line;
+};
+
+const GeometryCoordinates continuedLine = makeLine();
+const GeometryCoordinates nonContinuedLine = makeLine(1u);
+
+const float smallSpacing = 2.0f;
+const float bigSpacing = 3.0f;
+const float textLeft = -1.0f;
+const float textRight = 1.0f;
+const float iconLeft = -0.5f;
+const float iconRight = 0.5f;
+const float glyphSize = 0.1f;
+const float boxScale = 1.0f;
+const float overscaling = 1.0f;
+} // namespace
+
+TEST(getAnchors, NonContinuedLineShortLabels) {
+ const Anchors anchors = getAnchors(
+ nonContinuedLine, bigSpacing, M_PI, textLeft, textRight, iconLeft, iconRight, glyphSize, boxScale, overscaling);
+ const Anchors expected_anchors = {Anchor(1.0f, 2.0f, 1.570796371f, 1u),
+ Anchor(1.0f, 5.0f, 1.570796371f, 4u),
+ Anchor(1.0f, 8.0f, 1.570796371f, 7u)};
+
+ EXPECT_EQ(anchors, expected_anchors);
+}
+
+TEST(getAnchors, NonContinuedLineLongLabels) {
+ const Anchors anchors = getAnchors(nonContinuedLine,
+ smallSpacing,
+ M_PI,
+ textLeft,
+ textRight,
+ iconLeft,
+ iconRight,
+ glyphSize,
+ boxScale,
+ overscaling);
+ const Anchors expected_anchors = {Anchor(1.0f, 2.0f, 1.570796371f, 1u),
+ Anchor(1.0f, 5.0f, 1.570796371f, 3u),
+ Anchor(1.0f, 7.0f, 1.570796371f, 6u)};
+
+ EXPECT_EQ(anchors, expected_anchors);
+}
+
+TEST(getAnchors, ContinuedLineShortLabels) {
+ const Anchors anchors = getAnchors(
+ continuedLine, bigSpacing, M_PI, textLeft, textRight, iconLeft, iconRight, glyphSize, boxScale, overscaling);
+ const Anchors expected_anchors = {Anchor(1.0f, 2.0f, 1.570796371f, 1u),
+ Anchor(1.0f, 5.0f, 1.570796371f, 4u),
+ Anchor(1.0f, 8.0f, 1.570796371f, 7u)};
+
+ EXPECT_EQ(anchors, expected_anchors);
+}
+
+TEST(getAnchors, ContinuedLineLongLabels) {
+ const Anchors anchors = getAnchors(
+ continuedLine, smallSpacing, M_PI, textLeft, textRight, iconLeft, iconRight, glyphSize, boxScale, overscaling);
+ const Anchors expected_anchors = {Anchor(1.0f, 1.0f, 1.570796371f, 1u),
+ Anchor(1.0f, 4.0f, 1.570796371f, 3u),
+ Anchor(1.0f, 6.0f, 1.570796371f, 6u)};
+
+ EXPECT_EQ(anchors, expected_anchors);
+}
+
+TEST(getAnchors, OverscaledAnchorsInParent) {
+ const Anchors anchors = getAnchors(
+ nonContinuedLine, bigSpacing, M_PI, textLeft, textRight, iconLeft, iconRight, glyphSize, boxScale, overscaling);
+ const Anchors childAnchors = getAnchors(nonContinuedLine,
+ bigSpacing / 2,
+ M_PI,
+ textLeft,
+ textRight,
+ iconLeft,
+ iconRight,
+ glyphSize,
+ 0.5f /*boxScale*/,
+ 2.0f /*overscaling*/);
+ for (const auto& anchor : anchors) {
+ EXPECT_TRUE(std::find(childAnchors.begin(), childAnchors.end(), anchor) != childAnchors.end());
+ }
+}
+
+TEST(getAnchors, UseMidpointForShortLine) {
+ const GeometryCoordinates shortLine = {Point<int16_t>{1, 1}, Point<int16_t>{1, 3}};
+ const Anchors anchors = getAnchors(
+ shortLine, smallSpacing, M_PI, textLeft, textRight, iconLeft, iconRight, glyphSize, boxScale, overscaling);
+ const Anchors expected_anchors = {Anchor(1.0f, 2.0f, 1.570796371f, 0u)};
+
+ EXPECT_EQ(anchors, expected_anchors);
+}
+
+TEST(getAnchors, GetCenterAnchor) {
+ const GeometryCoordinates line = {
+ Point<int16_t>{1, 1}, Point<int16_t>{1, 3}, Point<int16_t>{3, 6}, Point<int16_t>{4, 7}};
+ const auto anchor = getCenterAnchor(line, M_PI, textLeft, textRight, iconLeft, iconRight, glyphSize, boxScale);
+ EXPECT_TRUE(anchor);
+ EXPECT_EQ(*anchor, Anchor(2.0f, 4.0f, .982793748f, 1u));
+}
+
+TEST(getAnchors, GetCenterAnchorOutsideTileBounds) {
+ const GeometryCoordinates line = {Point<int16_t>{-10, -10}, Point<int16_t>{5, 5}};
+ const auto anchor = getCenterAnchor(line, M_PI, textLeft, textRight, iconLeft, iconRight, glyphSize, boxScale);
+ EXPECT_TRUE(anchor);
+ EXPECT_EQ(*anchor, Anchor(-3.0f, -3.0f, .785398185f, 0u));
+}
+
+TEST(getAnchors, GetCenterAnchorFailMaxAngle) {
+ const GeometryCoordinates line = {Point<int16_t>{1, 1}, Point<int16_t>{1, 3}, Point<int16_t>{3, 3}};
+ const auto anchor = getCenterAnchor(line, M_PI / 4, textLeft, textRight, iconLeft, iconRight, glyphSize, boxScale);
+ EXPECT_FALSE(anchor);
+}
diff --git a/test/text/quads.test.cpp b/test/text/quads.test.cpp
index 64255c72db..5a78195faa 100644
--- a/test/text/quads.test.cpp
+++ b/test/text/quads.test.cpp
@@ -12,7 +12,7 @@ using namespace mbgl::style;
TEST(getIconQuads, normal) {
SymbolLayoutProperties::Evaluated layout;
- Anchor anchor(2.0, 3.0, 0.0, 0.5f, 0);
+ Anchor anchor(2.0, 3.0, 0.0, 0);
ImagePosition image = {
mapbox::Bin(-1, 15, 11, 0, 0, 0, 0),
style::Image::Impl("test", PremultipliedImage({1,1}), 1.0)
@@ -35,7 +35,7 @@ TEST(getIconQuads, normal) {
}
TEST(getIconQuads, style) {
- Anchor anchor(0.0, 0.0, 0.0, 0.5f, 0);
+ Anchor anchor(0.0, 0.0, 0.0, 0);
const ImagePosition image = {mapbox::Bin(-1, 20, 20, 0, 0, 0, 0),
style::Image::Impl("test", PremultipliedImage({1, 1}), 1.0)};