From 03b13de73dcf5729a0703486aace9ac0da5a2d7f Mon Sep 17 00:00:00 2001 From: BillSenior Date: Tue, 18 Jun 2024 16:54:07 +0200 Subject: [PATCH] GRIDEDIT-759 Added unit test for linear refine of polygons --- libs/MeshKernel/tests/src/PolygonsTests.cpp | 69 +++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/libs/MeshKernel/tests/src/PolygonsTests.cpp b/libs/MeshKernel/tests/src/PolygonsTests.cpp index e67b6e6f6..04e3a9104 100644 --- a/libs/MeshKernel/tests/src/PolygonsTests.cpp +++ b/libs/MeshKernel/tests/src/PolygonsTests.cpp @@ -954,3 +954,72 @@ TEST_P(IsPointInPolygonsTests, parameters) ASSERT_EQ(result, expectedResult); } INSTANTIATE_TEST_SUITE_P(IsPointInPolygonsParametrizedTests, IsPointInPolygonsTests, ::testing::ValuesIn(IsPointInPolygonsTests::GetData())); + + +TEST(Polygons, BasicLinearRefinePolygon) +{ + // Prepare + std::vector nodes; + + nodes.push_back({0.0, 10.0}); + nodes.push_back({1.0, 10.0}); + nodes.push_back({3.0, 10.0}); + nodes.push_back({11.0, 10.0}); + nodes.push_back({11.0, 0.0}); + nodes.push_back({0.0, 0.0}); + nodes.push_back({0.0, 10.0}); + + std::vector expected; + + expected.push_back({0.0, 10.0}); + expected.push_back({1.0, 10.0}); + expected.push_back({3.476743906, 10.0}); + expected.push_back({7.180339887, 10.0}); + expected.push_back({11, 8.281492376}); + expected.push_back({11.0, 0.0}); + expected.push_back({0.0, 0.0}); + expected.push_back({0.0, 10.0}); + + meshkernel::Polygons polygons(nodes, meshkernel::Projection::cartesian); + + // Execute + const auto refinedPolygon = polygons.LinearRefinePolygon(0, 1, 4); + + ASSERT_EQ (expected.size (), refinedPolygon.size ()); + constexpr double tolerance = 1.0e-8; + + for (size_t i = 0; i < refinedPolygon.size (); ++i) + { + EXPECT_NEAR (expected[i].x, refinedPolygon[i].x, tolerance); + EXPECT_NEAR (expected[i].y, refinedPolygon[i].y, tolerance); + } +} + +TEST(Polygons, LinearRefinePolygonSameNodes) +{ + // Prepare + std::vector nodes; + + nodes.push_back({0.0, 10.0}); + nodes.push_back({1.0, 10.0}); + nodes.push_back({3.0, 10.0}); + nodes.push_back({11.0, 10.0}); + nodes.push_back({11.0, 0.0}); + nodes.push_back({0.0, 0.0}); + nodes.push_back({0.0, 10.0}); + + std::vector expected(nodes); + + meshkernel::Polygons polygons(nodes, meshkernel::Projection::cartesian); + + // Execute + const auto refinedPolygon = polygons.LinearRefinePolygon(0, 1, 1); + + ASSERT_EQ (expected.size (), refinedPolygon.size ()); + + for (size_t i = 0; i < refinedPolygon.size (); ++i) + { + EXPECT_EQ (expected[i].x, refinedPolygon[i].x); + EXPECT_EQ (expected[i].y, refinedPolygon[i].y); + } +}