Repo created

This commit is contained in:
Fr4nz D13trich 2025-11-22 13:58:55 +01:00
parent 4af19165ec
commit 68073add76
12458 changed files with 12350765 additions and 2 deletions

View file

@ -0,0 +1,15 @@
project(poly_borders_tests)
set(SRC
mark_points_tests.cpp
remove_empty_spaces_tests.cpp
tools.cpp
tools.hpp
)
omim_add_test(${PROJECT_NAME} ${SRC})
target_link_libraries(${PROJECT_NAME}
poly_borders
platform_tests_support
)

View file

@ -0,0 +1,190 @@
#include "poly_borders/poly_borders_tests/tools.hpp"
#include "testing/testing.hpp"
#include "poly_borders/borders_data.hpp"
#include "platform/platform_tests_support/scoped_dir.hpp"
#include "platform/platform_tests_support/scoped_file.hpp"
#include "platform/platform_tests_support/writable_dir_changer.hpp"
#include "platform/platform.hpp"
#include "base/assert.hpp"
#include <memory>
#include <string>
#include <vector>
using namespace platform::tests_support;
using namespace platform;
using namespace poly_borders;
using namespace std;
namespace
{
static string const kTestDir = "borders_poly_dir";
void TestMarked(Polygon const & polygon, size_t i)
{
TEST(!polygon.m_points[i].m_links.empty(), (i, "th point point must be marked."));
}
void TestNotMarked(Polygon const & polygon, size_t i)
{
TEST(polygon.m_points[i].m_links.empty(), (i, "th point must not be marked."));
}
void CheckByMask(Polygon const & polygons, vector<bool> markedMask)
{
CHECK_EQUAL(polygons.m_points.size(), markedMask.size(), ());
for (size_t i = 0; i < polygons.m_points.size(); ++i)
if (markedMask[i])
TestMarked(polygons, i);
else
TestNotMarked(polygons, i);
}
UNIT_TEST(PolyBordersPostprocessor_MarkPoints_1)
{
ScopedDir const scopedDir(kTestDir);
string const & bordersDir = scopedDir.GetFullPath();
m2::PointD a(-1.0, -1.0);
m2::PointD b(-1.0, 1.0);
vector<vector<m2::PointD>> polygons1 = {{a, b, {1.0, 1.0}, {1.0, -1.0}}};
vector<vector<bool>> markedMask1 = {{true, true, false, false}};
vector<vector<m2::PointD>> polygons2 = {{a, b, {2.0, 1.0}, {5.0, -1.0}}};
vector<vector<bool>> markedMask2 = {{true, true, false, false}};
vector<shared_ptr<ScopedFile>> files;
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "First", polygons1));
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "Second", polygons2));
BordersData bordersData;
bordersData.Init(bordersDir);
auto const & bordersPolygon1 = bordersData.GetBordersPolygonByName("First" + BordersData::kBorderExtension + "1");
CheckByMask(bordersPolygon1, markedMask1[0]);
auto const & bordersPolygon2 = bordersData.GetBordersPolygonByName("Second" + BordersData::kBorderExtension + "1");
CheckByMask(bordersPolygon2, markedMask2[0]);
}
UNIT_TEST(PolyBordersPostprocessor_MarkPoints_2)
{
ScopedDir const scopedDir(kTestDir);
string const & bordersDir = scopedDir.GetFullPath();
vector<vector<m2::PointD>> polygons1 = {{{-1.0, -1.0}, {-1.0, 1.0}, {1.0, 1.0}, {1.0, -1.0}}};
vector<vector<bool>> markedMask1 = {{false, false, false, false}};
vector<vector<m2::PointD>> polygons2 = {{{-12.0, -1.0}, {-10.0, 1.0}, {2.0, 1.0}, {5.0, -1.0}}};
vector<vector<bool>> markedMask2 = {{false, false, false, false}};
vector<shared_ptr<ScopedFile>> files;
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "First", polygons1));
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "Second", polygons2));
BordersData bordersData;
bordersData.Init(bordersDir);
auto const & bordersPolygon1 = bordersData.GetBordersPolygonByName("First" + BordersData::kBorderExtension + "1");
CheckByMask(bordersPolygon1, markedMask1[0]);
auto const & bordersPolygon2 = bordersData.GetBordersPolygonByName("Second" + BordersData::kBorderExtension + "1");
CheckByMask(bordersPolygon2, markedMask2[0]);
}
UNIT_TEST(PolyBordersPostprocessor_MarkPoints_3)
{
ScopedDir const scopedDir(kTestDir);
string const & bordersDir = scopedDir.GetFullPath();
m2::PointD a(-2.0, 1.0);
m2::PointD b(0.0, 3.0);
m2::PointD c(3.0, -1.0);
m2::PointD d(-1.0, -3.0);
m2::PointD e(-4.0, 2.0);
m2::PointD f(-1.0, 4.0);
vector<vector<m2::PointD>> polygons1 = {{a, b, c, {1.0, -3.0}, d}};
vector<vector<bool>> markedMask1 = {{true, true, true, false, true}};
vector<vector<m2::PointD>> polygons2 = {{b, f, {2.0, 5.0}, {6.0, 3.0}, c}};
vector<vector<bool>> markedMask2 = {{true, true, false, false, true}};
vector<vector<m2::PointD>> polygons3 = {{a, b, f, {-3.0, 4.0}, e}};
vector<vector<bool>> markedMask3 = {{true, true, true, false, true}};
vector<vector<m2::PointD>> polygons4 = {{a, e, {-3.0, -1.0}, d}};
vector<vector<bool>> markedMask4 = {{true, true, false, true}};
vector<shared_ptr<ScopedFile>> files;
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "First", polygons1));
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "Second", polygons2));
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "Third", polygons3));
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "Fourth", polygons4));
BordersData bordersData;
bordersData.Init(bordersDir);
auto const & bordersPolygon1 = bordersData.GetBordersPolygonByName("First" + BordersData::kBorderExtension + "1");
CheckByMask(bordersPolygon1, markedMask1[0]);
auto const & bordersPolygon2 = bordersData.GetBordersPolygonByName("Second" + BordersData::kBorderExtension + "1");
CheckByMask(bordersPolygon2, markedMask2[0]);
auto const & bordersPolygon3 = bordersData.GetBordersPolygonByName("Third" + BordersData::kBorderExtension + "1");
CheckByMask(bordersPolygon3, markedMask3[0]);
auto const & bordersPolygon4 = bordersData.GetBordersPolygonByName("Fourth" + BordersData::kBorderExtension + "1");
CheckByMask(bordersPolygon4, markedMask4[0]);
}
UNIT_TEST(PolyBordersPostprocessor_MarkPoints_4)
{
ScopedDir const scopedDir(kTestDir);
string const & bordersDir = scopedDir.GetFullPath();
m2::PointD a(6.0, 2.0);
m2::PointD b(6.0, 4.0);
vector<vector<m2::PointD>> polygons1 = {{{-2.0, -2.0}, {-2.0, 2.0}, {2.0, 2.0}, {2.0, -2.0}},
{{4.0, 2.0}, {4.0, 4.0}, a, b}};
vector<vector<bool>> markedMask1 = {{false, false, false, false}, {false, false, true, true}};
vector<vector<m2::PointD>> polygons2 = {{a, b, {8.0, 6.0}, {8.0, 0.0}}};
vector<vector<bool>> markedMask2 = {{true, true, false, false}};
vector<shared_ptr<ScopedFile>> files;
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "First", polygons1));
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "Second", polygons2));
BordersData bordersData;
bordersData.Init(bordersDir);
auto const & firstBordersPolygon1 =
bordersData.GetBordersPolygonByName("First" + BordersData::kBorderExtension + "1");
CheckByMask(firstBordersPolygon1, markedMask1[0]);
auto const & secondBordersPolygon1 =
bordersData.GetBordersPolygonByName("First" + BordersData::kBorderExtension + "2");
CheckByMask(secondBordersPolygon1, markedMask1[1]);
auto const & bordersPolygon2 = bordersData.GetBordersPolygonByName("Second" + BordersData::kBorderExtension + "1");
CheckByMask(bordersPolygon2, markedMask2[0]);
}
} // namespace

View file

@ -0,0 +1,243 @@
#include "poly_borders/poly_borders_tests/tools.hpp"
#include "testing/testing.hpp"
#include "poly_borders/borders_data.hpp"
#include "platform/platform_tests_support/scoped_dir.hpp"
#include "platform/platform_tests_support/scoped_file.hpp"
#include "geometry/point2d.hpp"
#include <set>
#include <string>
#include <vector>
namespace remove_empty_spaces_tests
{
using namespace platform::tests_support;
using namespace platform;
using namespace poly_borders;
using namespace std;
string const kTestDir = "borders_poly_dir";
auto constexpr kSmallShift = 1e-9;
auto constexpr kSmallPointShift = m2::PointD(kSmallShift, kSmallShift);
void Process(BordersData & bordersData, string const & bordersDir)
{
bordersData.Init(bordersDir);
bordersData.RemoveEmptySpaceBetweenBorders();
}
bool ConsistsOf(Polygon const & polygon, vector<m2::PointD> const & points)
{
CHECK_EQUAL(polygon.m_points.size(), points.size(), ());
set<size_t> used;
for (auto const & point : points)
{
for (size_t i = 0; i < polygon.m_points.size(); ++i)
{
static double constexpr kEps = 1e-5;
if (AlmostEqualAbs(point, polygon.m_points[i].m_point, kEps) && used.count(i) == 0)
{
used.emplace(i);
break;
}
}
}
return used.size() == points.size();
}
UNIT_TEST(PolyBordersPostprocessor_RemoveEmptySpaces_1)
{
ScopedDir const scopedDir(kTestDir);
string const & bordersDir = scopedDir.GetFullPath();
m2::PointD a(0.0, 0.0);
m2::PointD b(1.0, 0.0);
m2::PointD c(2.0, 0.0);
m2::PointD d(3.0, 0.0);
m2::PointD e(4.0, 0.0);
vector<vector<m2::PointD>> polygons1 = {{a, b, c, d, e}};
vector<vector<m2::PointD>> polygons2 = {{a, b, c, d, e}};
vector<shared_ptr<ScopedFile>> files;
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "First", polygons1));
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "Second", polygons2));
BordersData bordersData;
Process(bordersData, bordersDir);
auto const & bordersPolygon1 = bordersData.GetBordersPolygonByName("First" + BordersData::kBorderExtension + "1");
TEST(ConsistsOf(bordersPolygon1, {a, b, c, d, e}), ());
auto const & bordersPolygon2 = bordersData.GetBordersPolygonByName("Second" + BordersData::kBorderExtension + "1");
TEST(ConsistsOf(bordersPolygon2, {a, b, c, d, e}), ());
}
UNIT_TEST(PolyBordersPostprocessor_RemoveEmptySpaces_2)
{
ScopedDir const scopedDir(kTestDir);
string const & bordersDir = scopedDir.GetFullPath();
m2::PointD a(0.0, 0.0);
m2::PointD b(1.0, 0.0);
// We should make c.y small because in other case changed area
// will be so great, that point |c| will not be removed.
m2::PointD c(2.0, kSmallShift);
m2::PointD d(3.0, 0.0);
m2::PointD e(4.0, 0.0);
// Point |c| is absent from polygons2, algorithm should remove |c| from polygon1.
vector<vector<m2::PointD>> polygons1 = {{a, b, c, d, e}};
vector<vector<m2::PointD>> polygons2 = {{a, b, d, e}};
vector<shared_ptr<ScopedFile>> files;
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "First", polygons1));
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "Second", polygons2));
BordersData bordersData;
Process(bordersData, bordersDir);
auto const & bordersPolygon1 = bordersData.GetBordersPolygonByName("First" + BordersData::kBorderExtension + "1");
TEST(ConsistsOf(bordersPolygon1, {a, b, d, e}), ());
auto const & bordersPolygon2 = bordersData.GetBordersPolygonByName("Second" + BordersData::kBorderExtension + "1");
TEST(ConsistsOf(bordersPolygon2, {a, b, d, e}), ());
}
// Like |PolyBordersPostprocessor_RemoveEmptySpaces_2| but two points will be
// added instead of one.
UNIT_TEST(PolyBordersPostprocessor_RemoveEmptySpaces_3)
{
ScopedDir const scopedDir(kTestDir);
string const & bordersDir = scopedDir.GetFullPath();
m2::PointD a(0.0, 0.0);
m2::PointD b(1.0, 0.0);
// We should make c.y (and d.y) small because in other case changed area
// will be so great, that point |c| (|d|) will not be removed.
m2::PointD c(2.0, kSmallShift);
m2::PointD d(2.5, kSmallShift);
m2::PointD e(4.0, 0.0);
m2::PointD f(5.0, 0.0);
// Point |c| and |d| is absent from polygons2, algorithm should remove |c| from polygon1.
vector<vector<m2::PointD>> polygons1 = {{a, b, c, d, e, f}};
vector<vector<m2::PointD>> polygons2 = {{a, b, e, f}};
vector<shared_ptr<ScopedFile>> files;
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "First", polygons1));
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "Second", polygons2));
BordersData bordersData;
Process(bordersData, bordersDir);
auto const & bordersPolygon1 = bordersData.GetBordersPolygonByName("First" + BordersData::kBorderExtension + "1");
TEST(ConsistsOf(bordersPolygon1, {a, b, e, f}), ());
auto const & bordersPolygon2 = bordersData.GetBordersPolygonByName("Second" + BordersData::kBorderExtension + "1");
TEST(ConsistsOf(bordersPolygon2, {a, b, e, f}), ());
}
// Do not remove point |c| because changed area is too big.
UNIT_TEST(PolyBordersPostprocessor_RemoveEmptySpaces_4)
{
ScopedDir const scopedDir(kTestDir);
string const & bordersDir = scopedDir.GetFullPath();
m2::PointD a(0.0, 0.0);
m2::PointD b(1.0, 0.0);
m2::PointD c(2.0, 1.0);
m2::PointD d(4.0, 0.0);
m2::PointD e(5.0, 0.0);
vector<vector<m2::PointD>> polygons1 = {{a, b, c, d, e}};
vector<vector<m2::PointD>> polygons2 = {{a, b, d, e}};
vector<shared_ptr<ScopedFile>> files;
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "First", polygons1));
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "Second", polygons2));
BordersData bordersData;
Process(bordersData, bordersDir);
auto const & bordersPolygon1 = bordersData.GetBordersPolygonByName("First" + BordersData::kBorderExtension + "1");
TEST(ConsistsOf(bordersPolygon1, {a, b, c, d, e}), ());
auto const & bordersPolygon2 = bordersData.GetBordersPolygonByName("Second" + BordersData::kBorderExtension + "1");
TEST(ConsistsOf(bordersPolygon2, {a, b, d, e}), ());
}
// Replace {c1, d1, e1} -> {c2, d2}.
UNIT_TEST(PolyBordersPostprocessor_RemoveEmptySpaces_5)
{
ScopedDir const scopedDir(kTestDir);
string const & bordersDir = scopedDir.GetFullPath();
m2::PointD a(0.0, 0.0);
m2::PointD b(9.0, 0.0);
m2::PointD c1(2.0, 3.0);
m2::PointD d1(4.0, 4.0);
m2::PointD e1(d1 + kSmallPointShift + kSmallPointShift);
m2::PointD c2(c1 + kSmallPointShift);
m2::PointD d2(d1 + kSmallPointShift);
vector<vector<m2::PointD>> polygons1 = {{a, c1, d1, e1, b}};
vector<vector<m2::PointD>> polygons2 = {{a, c2, d2, b}};
vector<shared_ptr<ScopedFile>> files;
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "First", polygons1));
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "Second", polygons2));
BordersData bordersData;
Process(bordersData, bordersDir);
auto const & bordersPolygon1 = bordersData.GetBordersPolygonByName("First" + BordersData::kBorderExtension + "1");
TEST(ConsistsOf(bordersPolygon1, {a, c2, d2, b}), ());
auto const & bordersPolygon2 = bordersData.GetBordersPolygonByName("Second" + BordersData::kBorderExtension + "1");
TEST(ConsistsOf(bordersPolygon2, {a, c2, d2, b}), ());
}
// Removes duplicates.
UNIT_TEST(PolyBordersPostprocessor_RemoveEmptySpaces_6)
{
ScopedDir const scopedDir(kTestDir);
string const & bordersDir = scopedDir.GetFullPath();
m2::PointD a(0.0, 0.0);
m2::PointD b(1.0, 0.0);
m2::PointD c(2.0, 1.0);
m2::PointD d(4.0, 0.0);
m2::PointD e(5.0, 0.0);
vector<vector<m2::PointD>> polygons1 = {{a, b, c, d, d, d, e, e, e}};
vector<vector<m2::PointD>> polygons2 = {{a, d, d, d, e}};
vector<shared_ptr<ScopedFile>> files;
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "First", polygons1));
files.emplace_back(CreatePolyBorderFileByPolygon(kTestDir, "Second", polygons2));
BordersData bordersData;
Process(bordersData, bordersDir);
auto const & bordersPolygon1 = bordersData.GetBordersPolygonByName("First" + BordersData::kBorderExtension + "1");
TEST(ConsistsOf(bordersPolygon1, {a, b, c, d, e}), ());
auto const & bordersPolygon2 = bordersData.GetBordersPolygonByName("Second" + BordersData::kBorderExtension + "1");
TEST(ConsistsOf(bordersPolygon2, {a, d, e}), ());
}
} // namespace remove_empty_spaces_tests

View file

@ -0,0 +1,45 @@
#include "poly_borders/poly_borders_tests/tools.hpp"
#include "poly_borders/borders_data.hpp"
#include "generator/borders.hpp"
#include "geometry/region2d.hpp"
#include "base/file_name_utils.hpp"
#include <string>
#include <vector>
using namespace platform::tests_support;
namespace
{
std::vector<m2::RegionD> ConvertFromPointsVector(std::vector<std::vector<m2::PointD>> const & polygons)
{
std::vector<m2::RegionD> res;
res.reserve(polygons.size());
for (auto const & polygon : polygons)
res.emplace_back(polygon);
return res;
}
} // namespace
namespace poly_borders
{
std::shared_ptr<ScopedFile> CreatePolyBorderFileByPolygon(std::string const & relativeDirPath, std::string const & name,
std::vector<std::vector<m2::PointD>> const & polygons)
{
std::string path = base::JoinPath(relativeDirPath, name + BordersData::kBorderExtension);
auto file = std::make_shared<ScopedFile>(path, ScopedFile::Mode::Create);
auto const targetDir = base::GetDirectory(file->GetFullPath());
auto const regions = ConvertFromPointsVector(polygons);
borders::DumpBorderToPolyFile(targetDir, name, regions);
return file;
}
} // namespace poly_borders

View file

@ -0,0 +1,16 @@
#pragma once
#include "platform/platform_tests_support/scoped_file.hpp"
#include "geometry/point2d.hpp"
#include <memory>
#include <string>
#include <vector>
namespace poly_borders
{
std::shared_ptr<platform::tests_support::ScopedFile> CreatePolyBorderFileByPolygon(
std::string const & relativeDirPath, std::string const & name,
std::vector<std::vector<m2::PointD>> const & polygons);
} // namespace poly_borders