Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Polygon Intersect #23

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion includes/polygon_clipping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> intersect_index = GetIntersectionPointIndex(clipping_list[query_index]);

if (intersect_index.has_value()) {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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<int> subject_intersect_index = GetIntersectionPointIndex(subject_list[query_node_index]);

if (subject_intersect_index.has_value()) {
Expand Down Expand Up @@ -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<Point> 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);
Expand All @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions polygon3.chal
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1, 5
1, 1
4, 1
4, 5
8 changes: 8 additions & 0 deletions polygon4.chal
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
5, 4
3, 4
3, 6
2, 6
2, 0
3, 0
3, 2
5, 2
17 changes: 17 additions & 0 deletions tests/cpp/ut_polygon_clipping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,21 @@ TEST(POLYGON_CLIPPING_TEST, test_create_vertex_list_should_return_correct_vertex

ASSERT_EQ(points, std::vector<Point>({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> polygon_ptr1 = std::make_shared<Polygon>(polygon1);
std::shared_ptr<Polygon> polygon_ptr2 = std::make_shared<Polygon>(polygon2);
PolygonClipping polygon_clipping(polygon_ptr1, polygon_ptr2);
std::vector<Point> points;

polygon_clipping.CreateVertexList(points, polygon_ptr2, polygon_ptr1);

ASSERT_EQ(points, std::vector<Point>({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)}));
}
Loading