Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11d00b5

Browse files
committed
BF: ignore CLOSEPOLY after NaN in PathNanRemover
1 parent b560513 commit 11d00b5

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/matplotlib/tests/test_path.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,15 @@ def test_intersect_zero_length_segment():
452452

453453
assert outline_path.intersects_path(this_path)
454454
assert this_path.intersects_path(outline_path)
455+
456+
457+
def test_cleanup_closepoly():
458+
# if the first connected component of a Path ends in a CLOSEPOLY, but that
459+
# component contains a NaN, then Path.cleaned should ignore not just the
460+
# control points but also the CLOSEPOLY, since it has nowhere valid to
461+
# point.
462+
p = Path([[np.nan, np.nan], [np.nan, np.nan]],
463+
[Path.MOVETO, Path.CLOSEPOLY])
464+
cleaned = p.cleaned(remove_nans=True)
465+
assert len(cleaned) == 1
466+
assert cleaned.codes[0] == Path.STOP

src/path_converters.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ class PathNanRemover : protected EmbeddedQueue<4>
163163
VertexSource *m_source;
164164
bool m_remove_nans;
165165
bool m_has_curves;
166+
bool valid_segment_exists;
166167

167168
public:
168169
/* has_curves should be true if the path contains bezier curve
@@ -172,7 +173,9 @@ class PathNanRemover : protected EmbeddedQueue<4>
172173
PathNanRemover(VertexSource &source, bool remove_nans, bool has_curves)
173174
: m_source(&source), m_remove_nans(remove_nans), m_has_curves(has_curves)
174175
{
175-
// empty
176+
// ignore all close/end_poly commands until after the first valid
177+
// (nan-free) command is encountered
178+
valid_segment_exists = false;
176179
}
177180

178181
inline void rewind(unsigned path_id)
@@ -202,8 +205,11 @@ class PathNanRemover : protected EmbeddedQueue<4>
202205
are found along the way, the queue is emptied, and
203206
the next curve segment is handled. */
204207
code = m_source->vertex(x, y);
205-
if (code == agg::path_cmd_stop ||
206-
code == (agg::path_cmd_end_poly | agg::path_flags_close)) {
208+
if (code == agg::path_cmd_stop) {
209+
return code;
210+
}
211+
if (code == (agg::path_cmd_end_poly | agg::path_flags_close)
212+
&& valid_segment_exists) {
207213
return code;
208214
}
209215

@@ -224,6 +230,7 @@ class PathNanRemover : protected EmbeddedQueue<4>
224230
}
225231

226232
if (!has_nan) {
233+
valid_segment_exists = true;
227234
break;
228235
}
229236

0 commit comments

Comments
 (0)