Skip to content

Ignore non-draw codes when calculating path extent #19216

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 1 commit into from
Jan 6, 2021

Conversation

dstansby
Copy link
Member

@dstansby dstansby commented Jan 2, 2021

When calculating the extents of a path, any MOVETO, CLOESPOLY, or STOP codes shouldn't be taken into account. This is a follow up to #16832.

@dstansby
Copy link
Member Author

dstansby commented Jan 2, 2021

🤔 looking at the failing tests it looks like any MOVETO codes are deliberately included in the path extents. This should probably just ignore CLOSEPOLY and STOP.

Comment on lines 628 to 634
draw_idxs = np.in1d(self.codes, Path.LINETO)
# Include the start and end point of each line
draw_idxs = draw_idxs | np.roll(draw_idxs, -1)
# Add in commands that aren't ignored
# (in the straight line case here this is only MOVETO)
draw_idxs = draw_idxs | ~np.in1d(self.codes, Path.IGNORED_CODES)
xys = self.vertices[draw_idxs]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this way of writing is more concise:

Suggested change
draw_idxs = np.in1d(self.codes, Path.LINETO)
# Include the start and end point of each line
draw_idxs = draw_idxs | np.roll(draw_idxs, -1)
# Add in commands that aren't ignored
# (in the straight line case here this is only MOVETO)
draw_idxs = draw_idxs | ~np.in1d(self.codes, Path.IGNORED_CODES)
xys = self.vertices[draw_idxs]
# we have only straight lines:
# consider line starts, line ends and points moved to
line_end_mask = np.isin(self.codes, Path.LINETO)
line_start_mask = np.roll(line_end_mask, -1)
moveto_mask = np.isin(self.codes, Path.MOVETO)
xys = self.vertices[line_start_mask | line_end_mask | moveto_mask]

Note also that I've used np.isin() instead of np.in1d() because self.codes are 1D.

Edit: I'm wondering if

xys = self.vertices[np.isin(self.codes, [Path.LINETO, Path.MOVETO])]

would be sufficient. Here, the codes of points at line starts cannot be STOP, CURVE3, CURVE4. The only grey area would be CLOSEPOLY followed by LINETO, in which case the line start would be a CLOSEPOLY node. IMHO that's not quite well defined CLOSEPOLY should always be followed by MOVETO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they can be CURVE{3,4}, as there will always be a final control point for those curves (the curve endpoint) which one can then draw a straight line from.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the elif conditions explicitly excludes CURVE{3,4} here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the lag. A couple of things:

The only grey area would be CLOSEPOLY followed by LINETO, in which case the line start would be a CLOSEPOLY node. IMHO that's not quite well defined CLOSEPOLY should always be followed by MOVETO.

While "undefined" is a reasonable guess, AFAICT, all backends currently handle this behavior consistently. That is, CLOSEPOLY's vertex is ignored and it is treated as "(start_point, LINETO)" plus adding the extra cap.

Which means that CLOSEPOLY followed by LINETO is well-defined, and does the natural thing:

import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch

p = Path(
    [[0, 0], [0, 1], [1, 1], [5, 5], [1, 0], [0, -1], [-5, 5]], 
    [Path.MOVETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY, 
     Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
)
fig, ax = plt.subplots()
ax.add_patch(PathPatch(p))
ax.set_xlim([-0.5, 1.5])
ax.set_ylim([-1.5, 1.5])

image

And, unless I'm missing something, then

I'm wondering if

xys = self.vertices[np.isin(self.codes, [Path.LINETO, Path.MOVETO])]

would be sufficient.

tl;dr: yes. We should just be doing this.

@dstansby
Copy link
Member Author

dstansby commented Jan 3, 2021

👍 thanks for the suggestions, agreed that they make the code read much better

Copy link
Member

@QuLogic QuLogic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the ignored code list be integrated into #19088?

Copy link
Contributor

@brunobeltran brunobeltran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this catch @dstansby!

Not sure why this wasn't just xys = self.vertices[np.isin(self.codes, [Path.MOVETO, Path.LINETO])] right from the start.

@dstansby
Copy link
Member Author

dstansby commented Jan 5, 2021

Thanks for the reviews. There were enough changes that I've just amended the commit and force pushed a new version.

@jklymak jklymak requested a review from brunobeltran January 6, 2021 03:45
Copy link
Contributor

@brunobeltran brunobeltran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@brunobeltran brunobeltran merged commit e904fa3 into matplotlib:master Jan 6, 2021
@QuLogic QuLogic added this to the v3.4.0 milestone Jan 6, 2021
@dstansby dstansby deleted the path-extent branch January 6, 2021 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants