From 46d6aebe35032ad3225ef75166505c760504af68 Mon Sep 17 00:00:00 2001 From: ntut-xuan Date: Mon, 23 Dec 2024 00:45:19 +0800 Subject: [PATCH 1/2] Fix: Polygon Intersect --- includes/polygon_clipping.hpp | 32 +++++++++++++++++++++++++++++++- polygon3.chal | 4 ++++ polygon4.chal | 8 ++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 polygon3.chal create mode 100644 polygon4.chal diff --git a/includes/polygon_clipping.hpp b/includes/polygon_clipping.hpp index e339fba..6cfe6fa 100644 --- a/includes/polygon_clipping.hpp +++ b/includes/polygon_clipping.hpp @@ -54,6 +54,8 @@ class PolygonClipping { int query_index = (clip_list_intersect_start_index + j + 1) % clipping_list_size; clip_vertexes.push_back(clipping_list[query_index]); + std::cout << "Rounding clipping_list node " << clipping_list[query_index].ToString() << std::endl; + std::optional intersect_index = GetIntersectionPointIndex(clipping_list[query_index]); if (intersect_index.has_value()) { @@ -87,6 +89,22 @@ class PolygonClipping { CreateVertexList(subject_list, subjectPolygon, clippingPolygon); CreateVertexList(clipping_list, clippingPolygon, subjectPolygon); + for (Point point : subject_list) { + if (GetIntersectionPointIndex(point).has_value()) { + std::cout << "Subject Polygon have intersect point " << point.ToString() << std::endl; + } else { + std::cout << "Subject Polygon have point " << point.ToString() << std::endl; + } + } + + for (Point point : clipping_list) { + if (GetIntersectionPointIndex(point).has_value()) { + std::cout << "Clipping Polygon have intersect point " << point.ToString() << std::endl; + } else { + std::cout << "Clipping Polygon have point " << point.ToString() << std::endl; + } + } + // subjectPolygon clockwise should different on clippingPolygon. if ((subjectPolygon->IsClockwise() ^ clippingPolygon->IsClockwise()) == 0) { std::reverse(clipping_list.begin(), clipping_list.end()); @@ -115,6 +133,8 @@ class PolygonClipping { size_t query_node_index = (i) % (subject_list.size()); clip_vertexes.push_back(subject_list[query_node_index]); + std::cout << "Rounding subject node " << subject_list[query_node_index].ToString() << std::endl; + std::optional subject_intersect_index = GetIntersectionPointIndex(subject_list[query_node_index]); if (subject_intersect_index.has_value()) { @@ -145,6 +165,9 @@ class PolygonClipping { Point v11 = major_polygon->GetVertexs()->at((i) % major_polygon_vertex_size); Point v12 = major_polygon->GetVertexs()->at((i + 1) % major_polygon_vertex_size); Line line1(v11, v12); + + std::vector intersect_points; + for (size_t j = 0; j < minor_polygon_vertex_size; j++) { Point v21 = minor_polygon->GetVertexs()->at((j) % minor_polygon_vertex_size); Point v22 = minor_polygon->GetVertexs()->at((j + 1) % minor_polygon_vertex_size); @@ -155,13 +178,20 @@ class PolygonClipping { if (intersect_point_optional.has_value()) { std::cout << "Create Intersect Point " << intersect_point_optional.value().ToString() << std::endl; Point intersect_point = intersect_point_optional.value(); - list.push_back(intersect_point); + intersect_points.push_back(intersect_point); if (point_to_intersection_index.find(intersect_point) == point_to_intersection_index.end()) { point_to_intersection_index[intersect_point] = point_to_intersection_index.size(); } } } + std::sort(intersect_points.begin(), intersect_points.end(), + [v11](const Point &a, const Point &b) { return GetDistance(v11, a) < GetDistance(v11, b); }); + + for (Point p : intersect_points) { + list.push_back(p); + } + if (i != major_polygon_vertex_size - 1) { list.push_back(v12); } diff --git a/polygon3.chal b/polygon3.chal new file mode 100644 index 0000000..9eaaa53 --- /dev/null +++ b/polygon3.chal @@ -0,0 +1,4 @@ +1, 5 +1, 1 +4, 1 +4, 5 \ No newline at end of file diff --git a/polygon4.chal b/polygon4.chal new file mode 100644 index 0000000..47df4d0 --- /dev/null +++ b/polygon4.chal @@ -0,0 +1,8 @@ +5, 4 +3, 4 +3, 6 +2, 6 +2, 0 +3, 0 +3, 2 +5, 2 \ No newline at end of file From 3044a2b536353a013568bf2b1346425098c0af29 Mon Sep 17 00:00:00 2001 From: ntut-xuan Date: Mon, 23 Dec 2024 01:12:36 +0800 Subject: [PATCH 2/2] Add: Tests --- tests/cpp/ut_polygon_clipping.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/cpp/ut_polygon_clipping.cpp b/tests/cpp/ut_polygon_clipping.cpp index 8cca8e0..216fe83 100644 --- a/tests/cpp/ut_polygon_clipping.cpp +++ b/tests/cpp/ut_polygon_clipping.cpp @@ -15,4 +15,21 @@ TEST(POLYGON_CLIPPING_TEST, test_create_vertex_list_should_return_correct_vertex ASSERT_EQ(points, std::vector({Point(1, 5), Point(3, 2), Point(6, 6), Point(8, 5), Point(10, 4), Point(11.5, 10), Point(12, 12), Point(6, 12)})); +} + +TEST(POLYGON_CLIPPING_TEST, + test_create_vertex_list_with_multiple_intersect_in_the_same_line_should_return_correct_vertex_list) { + Polygon polygon1({Point(1, 5), Point(1, 1), Point(4, 1), Point(4, 5)}); + Polygon polygon2( + {Point(5, 4), Point(3, 4), Point(3, 6), Point(2, 6), Point(2, 0), Point(3, 0), Point(3, 2), Point(5, 2)}); + std::shared_ptr polygon_ptr1 = std::make_shared(polygon1); + std::shared_ptr polygon_ptr2 = std::make_shared(polygon2); + PolygonClipping polygon_clipping(polygon_ptr1, polygon_ptr2); + std::vector points; + + polygon_clipping.CreateVertexList(points, polygon_ptr2, polygon_ptr1); + + ASSERT_EQ(points, std::vector({Point(5, 4), Point(4, 4), Point(3, 4), Point(3, 5), Point(3, 6), Point(2, 6), + Point(2, 5), Point(2, 1), Point(2, 0), Point(3, 0), Point(3, 1), Point(3, 2), + Point(4, 2), Point(5, 2)})); } \ No newline at end of file