Skip to content

Backport PR #15970 on branch v3.2.x (Process clip paths the same way as regular Paths.) #15984

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
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
Binary file modified lib/matplotlib/tests/baseline_images/test_axes/imshow_clip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 14 additions & 3 deletions src/_backend_agg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,15 @@ RendererAgg::restore_region(BufferRegion &region, int xx1, int yy1, int xx2, int
}

bool RendererAgg::render_clippath(py::PathIterator &clippath,
const agg::trans_affine &clippath_trans)
const agg::trans_affine &clippath_trans,
e_snap_mode snap_mode)
{
typedef agg::conv_transform<py::PathIterator> transformed_path_t;
typedef agg::conv_curve<transformed_path_t> curve_t;
typedef PathNanRemover<transformed_path_t> nan_removed_t;
typedef PathClipper<nan_removed_t> clipped_t;
typedef PathSnapper<clipped_t> snapped_t;
typedef PathSimplifier<snapped_t> simplify_t;
typedef agg::conv_curve<simplify_t> curve_t;

bool has_clippath = (clippath.total_vertices() != 0);

Expand All @@ -145,7 +150,13 @@ bool RendererAgg::render_clippath(py::PathIterator &clippath,

rendererBaseAlphaMask.clear(agg::gray8(0, 0));
transformed_path_t transformed_clippath(clippath, trans);
curve_t curved_clippath(transformed_clippath);
nan_removed_t nan_removed_clippath(transformed_clippath, true, clippath.has_curves());
clipped_t clipped_clippath(nan_removed_clippath, !clippath.has_curves(), width, height);
snapped_t snapped_clippath(clipped_clippath, snap_mode, clippath.total_vertices(), 0.0);
simplify_t simplified_clippath(snapped_clippath,
clippath.should_simplify() && !clippath.has_curves(),
clippath.simplify_threshold());
curve_t curved_clippath(simplified_clippath);
theRasterizer.add_path(curved_clippath);
rendererAlphaMask.color(agg::gray8(255, 255));
agg::render_scanlines(theRasterizer, scanlineAlphaMask, rendererAlphaMask);
Expand Down
16 changes: 8 additions & 8 deletions src/_backend_agg.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class RendererAgg
template <class R>
void set_clipbox(const agg::rect_d &cliprect, R &rasterizer);

bool render_clippath(py::PathIterator &clippath, const agg::trans_affine &clippath_trans);
bool render_clippath(py::PathIterator &clippath, const agg::trans_affine &clippath_trans, e_snap_mode snap_mode);

template <class PathIteratorType>
void _draw_path(PathIteratorType &path, bool has_clippath, const facepair_t &face, GCAgg &gc);
Expand Down Expand Up @@ -379,7 +379,7 @@ RendererAgg::_draw_path(path_t &path, bool has_clippath, const facepair_t &face,
// function
set_clipbox(gc.cliprect, theRasterizer);
if (has_clippath) {
render_clippath(gc.clippath.path, gc.clippath.trans);
render_clippath(gc.clippath.path, gc.clippath.trans, gc.snap_mode);
}

// Transfer the hatch to the main image buffer
Expand Down Expand Up @@ -468,7 +468,7 @@ RendererAgg::draw_path(GCAgg &gc, PathIterator &path, agg::trans_affine &trans,
theRasterizer.reset_clipping();
rendererBase.reset_clipping(true);
set_clipbox(gc.cliprect, theRasterizer);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans, gc.snap_mode);

trans *= agg::trans_affine_scaling(1.0, -1.0);
trans *= agg::trans_affine_translation(0.0, (double)height);
Expand Down Expand Up @@ -586,7 +586,7 @@ inline void RendererAgg::draw_markers(GCAgg &gc,
theRasterizer.reset_clipping();
rendererBase.reset_clipping(true);
set_clipbox(gc.cliprect, rendererBase);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans, gc.snap_mode);

double x, y;

Expand Down Expand Up @@ -832,7 +832,7 @@ inline void RendererAgg::draw_image(GCAgg &gc,
theRasterizer.reset_clipping();
rendererBase.reset_clipping(true);
set_clipbox(gc.cliprect, theRasterizer);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans, gc.snap_mode);

agg::rendering_buffer buffer;
buffer.attach(
Expand Down Expand Up @@ -941,7 +941,7 @@ inline void RendererAgg::_draw_path_collection_generic(GCAgg &gc,
theRasterizer.reset_clipping();
rendererBase.reset_clipping(true);
set_clipbox(cliprect, theRasterizer);
bool has_clippath = render_clippath(clippath, clippath_trans);
bool has_clippath = render_clippath(clippath, clippath_trans, gc.snap_mode);

// Set some defaults, assuming no face or edge
gc.linewidth = 0.0;
Expand Down Expand Up @@ -1249,7 +1249,7 @@ inline void RendererAgg::draw_gouraud_triangle(GCAgg &gc,
theRasterizer.reset_clipping();
rendererBase.reset_clipping(true);
set_clipbox(gc.cliprect, theRasterizer);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans, gc.snap_mode);

_draw_gouraud_triangle(points, colors, trans, has_clippath);
}
Expand All @@ -1263,7 +1263,7 @@ inline void RendererAgg::draw_gouraud_triangles(GCAgg &gc,
theRasterizer.reset_clipping();
rendererBase.reset_clipping(true);
set_clipbox(gc.cliprect, theRasterizer);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans);
bool has_clippath = render_clippath(gc.clippath.path, gc.clippath.trans, gc.snap_mode);

for (int i = 0; i < points.dim(0); ++i) {
typename PointArray::sub_t point = points.subarray(i);
Expand Down