8000 Don't disable path clipping on paths with codes. · matplotlib/matplotlib@1918143 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1918143

Browse files
committed
Don't disable path clipping on paths with codes.
When `PathClipper` is applied, it is disabled if `has_curves()` is true. However, that method simply checks if the Path has _codes_, not curves. This means Paths created with codes, but using only MOVETO/LINETO, are not clipped when they could be. Additionally, the description for `PathClipper` says that "Curve segments are not clipped, but are always included in their entirety.", meaning there is no need to turn it off when the Path contains curves, as they will at least be handled, if not fully clipped.
1 parent 11258c2 commit 1918143

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed
Loading

lib/matplotlib/tests/test_simplification.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ def test_diamond():
4747
ax.set_ylim(-0.6, 0.6)
4848

4949

50+
def test_clipping_out_of_bounds():
51+
# Should work on a Path *without* codes.
52+
path = Path([(0, 0), (1, 2), (2, 1)])
53+
simplified = path.cleaned(clip=(10, 10, 20, 20))
54+
assert_array_equal(simplified.vertices, [(0, 0)])
55+
assert simplified.codes == [Path.STOP]
56+
57+
# Should work on a Path *with* codes, and no curves.
58+
path = Path([(0, 0), (1, 2), (2, 1)],
59+
[Path.MOVETO, Path.LINETO, Path.LINETO])
60+
simplified = path.cleaned(clip=(10, 10, 20, 20))
61+
assert_array_equal(simplified.vertices, [(0, 0)])
62+
assert simplified.codes == [Path.STOP]
63+
64+
# A Path with curves does not do any clipping yet.
65+
path = Path([(0, 0), (1, 2), (2, 3)],
66+
[Path.MOVETO, Path.CURVE3, Path.CURVE3])
67+
simplified = path.cleaned()
68+
simplified_clipped = path.cleaned(clip=(10, 10, 20, 20))
69+
assert_array_equal(simplified.vertices, simplified_clipped.vertices)
70+
assert_array_equal(simplified.codes, simplified_clipped.codes)
71+
72+
5073
def test_noise():
5174
np.random.seed(0)
5275
x = np.random.uniform(size=50000) * 50

src/_backend_agg.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ RendererAgg::draw_path(GCAgg &gc, PathIterator &path, agg::trans_affine &trans,
470470

471471
trans *= agg::trans_affine_scaling(1.0, -1.0);
472472
trans *= agg::trans_affine_translation(0.0, (double)height);
473-
bool clip = !face.first && !gc.has_hatchpath() && !path.has_curves();
473+
bool clip = !face.first && !gc.has_hatchpath();
474474
bool simplify = path.should_simplify() && clip;
475475
double snapping_linewidth = points_to_pixels(gc.linewidth);
476476
if (gc.color.a == 0.0) {
@@ -992,7 +992,7 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
992992
}
993993
}
994994

995-
bool do_clip = !face.first && !gc.has_hatchpath() && !has_curves;
995+
bool do_clip = !face.first && !gc.has_hatchpath();
996996

997997
if (check_snap) {
998998
gc.isaa = antialiaseds(i % Naa);

src/_path.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ void convert_path_to_polygons(PathIterator &path,
985985

986986
transformed_path_t tpath(path, trans);
987987
nan_removal_t nan_removed(tpath, true, path.has_curves());
988-
clipped_t clipped(nan_removed, do_clip && !path.has_curves(), width, height);
988+
clipped_t clipped(nan_removed, do_clip, width, height);
989989
simplify_t simplified(clipped, simplify, path.simplify_threshold());
990990
curve_t curve(simplified);
991991

@@ -1050,7 +1050,7 @@ void cleanup_path(PathIterator &path,
10501050

10511051
transformed_path_t tpath(path, trans);
10521052
nan_removal_t nan_removed(tpath, remove_nans, path.has_curves());
1053-
clipped_t clipped(nan_removed, do_clip && !path.has_curves(), rect);
1053+
clipped_t clipped(nan_removed, do_clip, rect);
10541054
snapped_t snapped(clipped, snap_mode, path.total_vertices(), stroke_width);
10551055
simplify_t simplified(snapped, do_simplify, path.simplify_threshold());
10561056

@@ -1209,7 +1209,7 @@ bool convert_to_string(PathIterator &path,
12091209

12101210
transformed_path_t tpath(path, trans);
12111211
nan_removal_t nan_removed(tpath, true, path.has_curves());
1212-
clipped_t clipped(nan_removed, do_clip && !path.has_curves(), clip_rect);
1212+
clipped_t clipped(nan_removed, do_clip, clip_rect);
12131213
simplify_t simplified(clipped, simplify, path.simplify_threshold());
12141214

12151215
buffersize = path.total_vertices() * (precision + 5) * 4;

0 commit comments

Comments
 (0)
0