8000 Use CLOSEPOLY kind code to close tricontourf polygons · matplotlib/matplotlib@837235e · GitHub
[go: up one dir, main page]

Skip to content

Commit 837235e

Browse files
committed
Use CLOSEPOLY kind code to close tricontourf polygons
1 parent e240080 commit 837235e

File tree

2 files changed

+76
-12
lines changed

2 files changed

+76
-12
lines changed

lib/matplotlib/tests/test_triangulation.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,3 +1341,69 @@ def test_triplot_label():
13411341
assert labels == ['label']
13421342
assert len(handles) == 1
13431343
assert handles[0] is lines
1344+
1345+
1346+
def test_tricontour_path():
1347+
x = [0, 4, 4, 0, 2]
1348+
y = [0, 0, 4, 4, 2]
1349+
triang = mtri.Triangulation(x, y)
1350+
_, ax = plt.subplots()
1351+
1352+
# Line strip from boundary to boundary
1353+
cs = ax.tricontour(triang, [1, 0, 0, 0, 0], levels=[0.5])
1354+
assert len(cs.collections) == 1
1355+
paths = cs.collections[0].get_paths()
1356+
assert len(paths) == 1
1357+
expected_vertices = [[2, 0], [1, 1], [0, 2]]
1358+
assert_array_almost_equal(paths[0].vertices, expected_vertices)
1359+
assert_array_equal(paths[0].codes, [1, 2, 2])
1360+
assert_array_almost_equal(
1361+
paths[0].to_polygons(closed_only=False), [expected_vertices])
1362+
1363+
# Closed line loop inside domain
1364+
cs = ax.tricontour(triang, [0, 0, 0, 0, 1], levels=[0.5])
1365+
assert len(cs.collections) == 1
1366+
paths = cs.collections[0].get_paths()
1367+
assert len(paths) == 1
1368+
expected_vertices = [[3, 1], [3, 3], [1, 3], [1, 1], [3, 1]]
1369+
assert_array_almost_equal(paths[0].vertices, expected_vertices)
1370+
assert_array_equal(paths[0].codes, [1, 2, 2, 2, 79])
1371+
assert_array_almost_equal(paths[0].to_polygons(), [expected_vertices])
1372+
1373+
1374+
def test_tricontourf_path():
1375+
x = [0, 4, 4, 0, 2]
1376+
y = [0, 0, 4, 4, 2]
1377+
triang = mtri.Triangulation(x, y)
1378+
_, ax = plt.subplots()
1379+
1380+
# Polygon inside domain
1381+
cs = ax.tricontourf(triang, [0, 0, 0, 0, 1], levels=[0.5, 1.5])
1382+
assert len(cs.collections) == 1
1383+
paths = cs.collections[0].get_paths()
1384+
assert len(paths) == 1
1385+
expected_vertices = [[3, 1], [3, 3], [1, 3], [1, 1], [3, 1]]
1386+
assert_array_almost_equal(paths[0].vertices, expected_vertices)
1387+
assert_array_equal(paths[0].codes, [1, 2, 2, 2, 79])
1388+
assert_array_almost_equal(paths[0].to_polygons(), [expected_vertices])
1389+
1390+
# Polygon following boundary and inside domain
1391+
cs = ax.tricontourf(triang, [1, 0, 0, 0, 0], levels=[0.5, 1.5])
1392+
assert len(cs.collections) == 1
1393+
paths = cs.collections[0].get_paths()
1394+
assert len(paths) == 1
1395+
expected_vertices = [[2, 0], [1, 1], [0, 2], [0, 0], [2, 0]]
1396+
assert_array_almost_equal(paths[0].vertices, expected_vertices)
1397+
assert_array_equal(paths[0].codes, [1, 2, 2, 2, 79])
1398+
assert_array_almost_equal(paths[0].to_polygons(), [expected_vertices])
1399+
1400+
# Polygon is outer boundary with hole
1401+
cs = ax.tricontourf(triang, [0, 0, 0, 0, 1], levels=[-0.5, 0.5])
1402+
assert len(cs.collections) == 1
1403+
paths = cs.collections[0].get_paths()
1404+
assert len(paths) == 1
1405+
expected_vertices = [[0, 0], [4, 0], [4, 4], [0, 4], [0, 0],
1406+
[1, 1], [1, 3], [3, 3], [3, 1], [1, 1]]
1407+
assert_array_almost_equal(paths[0].vertices, expected_vertices)
1408+
assert_array_equal(paths[0].codes, [1, 2, 2, 2, 79, 1, 2, 2, 2, 79])
1409+
assert_array_almost_equal(paths[0].to_polygons(), np.split(expected_vertices, [5]))

src/tri/_tri.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,9 @@ py::tuple TriContourGenerator::contour_to_segs_and_kinds(const Contour& contour)
744744
*segs_ptr++ = point->y;
745745
*codes_ptr++ = (point == line->begin() ? MOVETO : LINETO);
746746
}
747+
748+
if (line->size() > 1)
749+
*(codes_ptr-1) = CLOSEPOLY;
747750
}
748751

749752
py::list vertices_list(1);
@@ -860,11 +863,8 @@ void TriContourGenerator::find_boundary_lines_filled(Contour& contour,
860863
lower_level, upper_level, on_upper);
861864
} while (tri_edge != start_tri_edge);
862865

863-
// Filled contour lines must not have same first and last
864-
// points.
865-
if (contour_line.size() > 1 &&
866-
contour_line.front() == contour_line.back())
867-
contour_line.pop_back();
866+
// Close polygon.
867+
contour_line.push_back(contour_line.front());
868868
}
869869
}
870870
}
@@ -883,6 +883,9 @@ void TriContourGenerator::find_boundary_lines_filled(Contour& contour,
883883
for (Boundary::size_type j = 0; j < boundary.size(); ++j)
884884
contour_line.push_back(triang.get_point_coords(
885885
triang.get_triangle_point(boundary[j])));
886+
887+
// Close polygon.
888+
contour_line.push_back(contour_line.front());
886889
}
887890
}
888891
}
@@ -915,13 +918,8 @@ void TriContourGenerator::find_interior_lines(Contour& contour,
915918
TriEdge tri_edge = triang.get_neighbor_edge(tri, edge);
916919
follow_interior(contour_line, tri_edge, false, level, on_upper);
917920

918-
if (!filled)
919-
// Non-filled contour lines must be closed.
920-
contour_line.push_back(contour_line.front());
921-
else if (contour_line.size() > 1 &&
922-
contour_line.front() == contour_line.back())
923-
// Filled contour lines must not have same first and last points.
924-
contour_line.pop_back();
921+
// Close line loop
922+
contour_line.push_back(contour_line.front());
925923
}
926924
}
927925

0 commit comments

Comments
 (0)
0