8000 Fix path simplification of closed loops by QuLogic · Pull Request #21387 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix path simplification of closed loops #21387

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

Merged
merged 5 commits into from
Oct 28, 2021
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
154 changes: 152 additions & 2 deletions lib/matplotlib/tests/test_simplification.py
8000 8000 10000
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import pytest

from matplotlib.testing.decorators import image_comparison
from matplotlib.testing.decorators import (
check_figures_equal, image_comparison, remove_ticks_and_titles)
import matplotlib.pyplot as plt

from matplotlib import patches, transforms
Expand Down Expand Up @@ -230,7 +231,7 @@ def test_sine_plus_noise():
assert simplified.vertices.size == 25240


@image_comparison(['simplify_curve'], remove_text=True)
@image_comparison(['simplify_curve'], remove_text=True, tol=0.017)
def test_simplify_curve():
pp1 = patches.PathPatch(
Path([(0, 0), (1, 0), (1, 1), (np.nan, 1), (0, 0), (2, 0), (2, 2),
Expand All @@ -245,6 +246,155 @@ def test_simplify_curve():
ax.set_ylim((0, 2))


@check_figures_equal()
def test_closed_path_nan_removal(fig_test, fig_ref):
ax_test = fig_test.subplots(2, 2).flatten()
ax_ref = fig_ref.subplots(2, 2).flatten()

# NaN on the first point also removes the last point, because it's closed.
path = Path(
[[-3, np.nan], [3, -3], [3, 3], [-3, 3], [-3, -3]],
[Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY])
ax_test[0].add_patch(patches.PathPatch(path, facecolor='none'))
path = Path(
[[-3, np.nan], [3, -3], [3, 3], [-3, 3], [-3, np.nan]],
[Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO])
ax_ref[0].add_patch(patches.PathPatch(path, facecolor='none'))

# NaN on second-last point should not re-close.
path = Path(
[[-2, -2], [2, -2], [2, 2], [-2, np.nan], [-2, -2]],
[Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY])
ax_test[0].add_patch(patches.PathPatch(path, facecolor='none'))
path = Path(
[[-2, -2], [2, -2], [2, 2], [-2, np.nan], [-2, -2]],
[Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO])
ax_ref[0].add_patch(patches.PathPatch(path, facecolor='none'))

# Test multiple loops in a single path (with same paths as above).
path = Path(
[[-3, np.nan], [3, -3], [3, 3], [-3, 3], [-3, -3],
[-2, -2], [2, -2], [2, 2], [-2, np.nan], [-2, -2]],
[Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY,
Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY])
ax_test[1].add_patch(patches.PathPatch(path, facecolor='none'))
path = Path(
[[-3, np.nan], [3, -3], [3, 3], [-3, 3], [-3, np.nan],
[-2, -2], [2, -2], [2, 2], [-2, np.nan], [-2, -2]],
[Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO,
Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO])
ax_ref[1].add_patch(patches.PathPatch(path, facecolor='none'))

# NaN in first point of CURVE3 should not re-close, and hide entire curve.
path = Path(
[[-1, -1], [1, -1], [1, np.nan], [0, 1], [-1, 1], [-1, -1]],
[Path.MOVETO, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.LINETO,
Path.CLOSEPOLY])
ax_test[2].add_patch(patches.PathPatch(path, facecolor='none'))
path = Path(
[[-1, -1], [1, -1], [1, np.nan], [0, 1], [-1, 1], [-1, -1]],
[Path.MOVETO, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.LINETO,
Path.CLOSEPOLY])
ax_ref[2].add_patch(patches.PathPatch(path, facecolor='none'))

# NaN in second point of CURVE3 should not re-close, and hide entire curve
# plus next line segment.
path = Path(
[[-3, -3], [3, -3], [3, 0], [0, np.nan], [-3, 3], [-3, -3]],
[Path.MOVETO, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.LINETO,
Path.LINETO])
ax_test[2].add_patch(patches.PathPatch(path, facecolor='none'))
path = Path(
[[-3, -3], [3, -3], [3, 0], [0, np.nan], [-3, 3], [-3, -3]],
[Path.MOVETO, Path.LINETO, Path.CURVE3, Path.CURVE3, Path.LINETO,
Path.LINETO])
ax_ref[2].add_patch(patches.PathPatch(path, facecolor='none'))

# NaN in first point of CURVE4 should not re-close, and hide entire curve.
path = Path(
[[-1, -1], [1, -1], [1, np.nan], [0, 0], [0, 1], [-1, 1], [-1, -1]],
[Path.MOVETO, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4,
Path.LINETO, Path.CLOSEPOLY])
ax_test[3].add_patch(patches.PathPatch(path, facecolor='none'))
path = Path(
[[-1, -1], [1, -1], [1, np.nan], [0, 0], [0, 1], [-1, 1], [-1, -1]],
[Path.MOVETO, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4,
Path.LINETO, Path.CLOSEPOLY])
ax_ref[3].add_patch(patches.PathPatch(path, facecolor='none'))

# NaN in second point of CURVE4 should not re-close, and hide entire curve.
path = Path(
[[-2, -2], [2, -2], [2, 0], [0, np.nan], [0, 2], [-2, 2], [-2, -2]],
[Path.MOVETO, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4,
Path.LINETO, Path.LINETO])
ax_test[3].add_patch(patches.PathPatch(path, facecolor='none'))
path = Path(
[[-2, -2], [2, -2], [2, 0], [0, np.nan], [0, 2], [-2, 2], [-2, -2]],
[Path.MOVETO, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4,
Path.LINETO, Path.LINETO])
ax_ref[3].add_patch(patches.PathPatch(path, facecolor='none'))

# NaN in third point of CURVE4 should not re-close, and hide entire curve
# plus next line segment.
path = Path(
[[-3, -3], [3, -3], [3, 0], [0, 0], [0, np.nan], [-3, 3], [-3, -3]],
[Path.MOVETO, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4,
Path.LINETO, Path.LINETO])
ax_test[3].add_patch(patches.PathPatch(path, facecolor='none'))
path = Path(
[[-3, -3], [3, -3], [3, 0], [0, 0], [0, np.nan], [-3, 3], [-3, -3]],
[Path.MOVETO, Path.LINETO, Path.CURVE4, Path.CURVE4, Path.CURVE4,
Path.LINETO, Path.LINETO])
ax_ref[3].add_patch(patches.PathPatch(path, facecolor='none'))

# Keep everything clean.
for ax in [*ax_test.flat, *ax_ref.flat]:
ax.set(xlim=(-3.5, 3.5), ylim=(-3.5, 3.5))
remove_ticks_and_titles(fig_test)
remove_ticks_and_titles(fig_ref)


@check_figures_equal()
def test_closed_path_clipping(fig_test, fig_ref):
vertices = []
for roll in range(8):
offset = 0.1 * roll + 0.1

# A U-like pattern.
pattern = [
[-0.5, 1.5], [-0.5, -0.5], [1.5, -0.5], [1.5, 1.5], # Outer square
# With a notch in the top.
[1 - offset / 2, 1.5], [1 - offset / 2, offset],
[offset / 2, offset], [offset / 2, 1.5],
]

# Place the initial/final point anywhere in/out of the clipping area.
pattern = np.roll(pattern, roll, axis=0)
pattern = np.concatenate((pattern, pattern[:1, :]))

vertices.append(pattern)

# Multiple subpaths are used here to ensure they aren't broken by closed
# loop clipping.
codes = np.full(len(vertices[0]), Path.LINETO)
codes[0] = Path.MOVETO
codes[-1] = Path.CLOSEPOLY
codes = np.tile(codes, len(vertices))
vertices = np.concatenate(vertices)

fig_test.set_size_inches((5, 5))
path = Path(vertices, codes)
fig_test.add_artist(patches.PathPatch(path, facecolor='none'))

# For reference, we draw the same thing, but unclosed by using a line to
# the last point only.
fig_ref.set_size_inches((5, 5))
codes = codes.copy()
codes[codes == Path.CLOSEPOLY] = Path.LINETO
path = Path(vertices, codes)
fig_ref.add_artist(patches.PathPatch(path, facecolor='none'))


@image_comparison(['hatch_simplify'], remove_text=True)
def test_hatch():
fig, ax = plt.subplots()
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def test_clipping_of_log():
clip=(0, 0, 100, 100),
simplify=False)
tpoints, tcodes = zip(*result)
assert_allclose(tcodes, path.codes)
assert_allclose(tcodes, path.codes[:-1]) # No longer closed.


class NonAffineForTest(mtransforms.Transform):
Expand Down
4 changes: 2 additions & 2 deletions src/_backend_agg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ bool RendererAgg::render_clippath(py::PathIterator &clippath,

rendererBaseAlphaMask.clear(agg::gray8(0, 0));
transformed_path_t transformed_clippath(clippath, trans);
nan_removed_t nan_removed_clippath(transformed_clippath, true, clippath.has_curves());
nan_removed_t nan_removed_clippath(transformed_clippath, true, clippath.has_codes());
snapped_t snapped_clippath(nan_removed_clippath, snap_mode, clippath.total_vertices(), 0.0);
simplify_t simplified_clippath(snapped_clippath,
clippath.should_simplify() && !clippath.has_curves(),
clippath.should_simplify() && !clippath.has_codes(),
clippath.simplify_threshold());
curve_t curved_clippath(simplified_clippath);
theRasterizer.add_path(curved_clippath);
Expand Down
16 changes: 8 additions & 8 deletions src/_backend_agg.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class RendererAgg
DashesVector &linestyles,
AntialiasedArray &antialiaseds,
bool check_snap,
bool has_curves);
bool has_codes);

template <class PointArray, class ColorArray>
void _draw_gouraud_triangle(PointArray &points,
Expand Down Expand Up @@ -478,7 +478,7 @@ RendererAgg::draw_path(GCAgg &gc, PathIterator &path, agg::trans_affine &trans,
}

transformed_path_t tpath(path, trans);
nan_removed_t nan_removed(tpath, true, path.has_curves());
nan_removed_t nan_removed(tpath, true, path.has_codes());
clipped_t clipped(nan_removed, clip, width, height);
snapped_t snapped(clipped, gc.snap_mode, path.total_vertices(), snapping_linewidth);
simplify_t simplified(snapped, simplify, path.simplify_threshold());
Expand Down Expand Up @@ -512,7 +512,7 @@ inline void RendererAgg::draw_markers(GCAgg &gc,
trans *= agg::trans_affine_translation(0.5, (double)height + 0.5);

transformed_path_t marker_path_transformed(marker_path, marker_trans);
nan_removed_t marker_path_nan_removed(marker_path_transformed, true, marker_path.has_curves());
nan_removed_t marker_path_nan_removed(marker_path_transformed, true, marker_path.has_codes());
snap_t marker_path_snapped(marker_path_nan_removed,
gc.snap_mode,
marker_path.total_vertices(),
Expand Down Expand Up @@ -910,7 +910,7 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
DashesVector &linestyles,
AntialiasedArray &antialiaseds,
bool check_snap,
bool has_curves)
bool has_codes)
{
typedef agg::conv_transform<typename PathGenerator::path_iterator> transformed_path_t;
typedef PathNanRemover<transformed_path_t> nan_removed_t;
Expand Down Expand Up @@ -998,11 +998,11 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
gc.isaa = antialiaseds(i % Naa);

transformed_path_t tpath(path, trans);
nan_removed_t nan_removed(tpath, true, has_curves);
nan_removed_t nan_removed(tpath, true, has_codes);
clipped_t clipped(nan_removed, do_clip, width, height);
snapped_t snapped(
clipped, gc.snap_mode, path.total_vertices(), points_to_pixels(gc.linewidth));
if (has_curves) {
if (has_codes) {
snapped_curve_t curve(snapped);
_draw_path(curve, has_clippath, face, gc);
} else {
Expand All @@ -1012,9 +1012,9 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
gc.isaa = antialiaseds(i % Naa);

transformed_path_t tpath(path, trans);
nan_removed_t nan_removed(tpath, true, has_curves);
nan_removed_t nan_removed(tpath, true, has_codes);
clipped_t clipped(nan_removed, do_clip, width, height);
if (has_curves) {
if (has_codes) {
curve_t curve(clipped);
_draw_path(curve, has_clippath, face, gc);
} else {
Expand Down
20 changes: 10 additions & 10 deletions src/_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ inline void points_in_path(PointArray &points,
}

transformed_path_t trans_path(path, trans);
no_nans_t no_nans_path(trans_path, true, path.has_curves());
no_nans_t no_nans_path(trans_path, true, path.has_codes());
curve_t curved_path(no_nans_path);
if (r != 0.0) {
contour_t contoured_path(curved_path);
Expand Down Expand Up @@ -305,7 +305,7 @@ void points_on_path(PointArray &points,
}

transformed_path_t trans_path(path, trans);
no_nans_t nan_removed_path(trans_path, true, path.has_curves());
no_nans_t nan_removed_path(trans_path, true, path.has_codes());
curve_t curved_path(nan_removed_path);
stroke_t stroked_path(curved_path);
stroked_path.width(r * 2.0);
Expand Down Expand Up @@ -378,7 +378,7 @@ void update_path_extents(PathIterator &path, agg::trans_affine &trans, extent_li
unsigned code;

transformed_path_t tpath(path, trans);
nan_removed_t nan_removed(tpath, true, path.has_curves());
nan_removed_t nan_removed(tpath, true, path.has_codes());

nan_removed.rewind(0);

Expand Down Expand Up @@ -512,7 +512,7 @@ bool path_in_path(PathIterator1 &a,
}

transformed_path_t b_path_trans(b, btrans);
no_nans_t b_no_nans(b_path_trans, true, b.has_curves());
no_nans_t b_no_nans(b_path_trans, true, b.has_codes());
curve_t b_curved(b_no_nans);

double x, y;
Expand Down Expand Up @@ -884,8 +884,8 @@ bool path_intersects_path(PathIterator1 &p1, PathIterator2 &p2)
return false;
}

no_nans_t n1(p1, true, p1.has_curves());
no_nans_t n2(p2, true, p2.has_curves());
no_nans_t n1(p1, true, p1.has_codes());
no_nans_t n2(p2, true, p2.has_codes());

curve_t c1(n1);
curve_t c2(n2);
Expand Down Expand Up @@ -949,7 +949,7 @@ bool path_intersects_rectangle(PathIterator &path,
return false;
}

no_nans_t no_nans(path, true, path.has_curves());
no_nans_t no_nans(path, true, path.has_codes());
curve_t curve(no_nans);

double cx = (rect_x1 + rect_x2) * 0.5, cy = (rect_y1 + rect_y2) * 0.5;
Expand Down Expand Up @@ -998,7 +998,7 @@ void convert_path_to_polygons(PathIterator &path,
bool simplify = path.should_simplify();

transformed_path_t tpath(path, trans);
nan_removal_t nan_removed(tpath, true, path.has_curves());
nan_removal_t nan_removed(tpath, true, path.has_codes());
clipped_t clipped(nan_removed, do_clip, width, height);
simplify_t simplified(clipped, simplify, path.simplify_threshold());
curve_t curve(simplified);
Expand Down Expand Up @@ -1063,7 +1063,7 @@ void cleanup_path(PathIterator &path,
typedef Sketch<curve_t> sketch_t;

transformed_path_t tpath(path, trans);
nan_removal_t nan_removed(tpath, remove_nans, path.has_curves());
nan_removal_t nan_removed(tpath, remove_nans, path.has_codes());
clipped_t clipped(nan_removed, do_clip, rect);
snapped_t snapped(clipped, snap_mode, path.total_vertices(), stroke_width);
simplify_t simplified(snapped, do_simplify, path.simplify_threshold());
Expand Down Expand Up @@ -1222,7 +1222,7 @@ bool convert_to_string(PathIterator &path,
bool do_clip = (clip_rect.x1 < clip_rect.x2 && clip_rect.y1 < clip_rect.y2);

transformed_path_t tpath(path, trans);
nan_removal_t nan_removed(tpath, true, path.has_curves());
nan_removal_t nan_removed(tpath, true, path.has_codes());
clipped_t clipped(nan_removed, do_clip, clip_rect);
simplify_t simplified(clipped, simplify, path.simplify_threshold());

Expand Down
Loading
0