Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Add unit test for calculateTileDistances
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshalamov authored and tobrun committed Dec 18, 2019
1 parent bee9034 commit 58e1c9b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions next/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/layout/symbol_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/layout/symbol_layout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class SymbolLayout final : public Layout {
* @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,
Expand Down
1 change: 1 addition & 0 deletions test/test-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 27 additions & 0 deletions test/text/calculate_tile_distances.test.cpp
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 58e1c9b

Please sign in to comment.