8000 more robust testing, updated logic for lastMax vars · matplotlib/matplotlib@2e5d178 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e5d178

Browse files
committed
more robust testing, updated logic for lastMax vars
1 parent 5dbd024 commit 2e5d178

File tree

2 files changed

+117
-4
lines changed

2 files changed

+117
-4
lines changed

lib/matplotlib/tests/test_simplification.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import io
55

66
import numpy as np
7+
from numpy.testing import assert_array_almost_equal
8+
79
import pytest
810

911
from matplotlib.testing.decorators import image_comparison
@@ -62,6 +64,102 @@ def test_noise():
6264
assert simplified.vertices.size == 25340
6365

6466

67+
def test_antiparallel_simplification():
68+
# test ending on a maximum
69+
x = [ 0, 0, 0, 0, 0, 1]
70+
y = [.5, 1, -1, 1, 2, .5]
71+
72+
fig, ax = plt.subplots()
73+
p1 = ax.plot(x, y)
74+
75+
path = p1[0].get_path()
76+
transform = p1[0].get_transform()
77+
path = transform.transform_path(path)
78+
simplified = path.cleaned(simplify=True)
79+
simplified = transform.inverted().transform_path(simplified)
80+
81+
assert_array_almost_equal([[ 0. , 0.5],
82+
[ 0. , -1. ],
83+
[ 0. , 2. ],
84+
[ 1. , 0.5]],
85+
simplified.vertices[:-2, :])
86+
87+
# test ending on a minimum
88+
x = [ 0, 0, 0, 0, 0, 1]
89+
y = [.5, 1, -1, 1, -2, .5]
90+
91+
fig, ax = plt.subplots()
92+
p1 = ax.plot(x, y)
93+
94+
path = p1[0].get_path()
95+
transform = p1[0].get_transform()
96+
path = transform.transform_path(path)
97+
simplified = path.cleaned(simplify=True)
98+
simplified = transform.inverted().transform_path(simplified)
99+
100+
assert_array_almost_equal([[ 0. , 0.5],
101+
[ 0. , 1. ],
102+
[ 0. , -2. ],
103+
[ 1. , 0.5]],
104+
simplified.vertices[:-2, :])
105+
106+
# test ending in between
107+
x = [ 0, 0, 0, 0, 0, 1]
108+
y = [.5, 1, -1, 1, 0, .5]
109+
110+
fig, ax = plt.subplots()
111+
p1 = ax.plot(x, y)
112+
113+
path = p1[0].get_path()
114+
transform = p1[0].get_transform()
115+
path = transform.transform_path(path)
116+
simplified = path.cleaned(simplify=True)
117+
simplified = transform.inverted().transform_path(simplified)
118+
119+
assert_array_almost_equal([[ 0. , 0.5],
120+
[ 0. , 1. ],
121+
[ 0. , -1. ],
122+
[ 0. , 0. ],
123+
[ 1. , 0.5]],
124+
simplified.vertices[:-2, :])
125+
126+
# test no anti-parallel ending at max
127+
x = [ 0, 0, 0, 0, 0, 1]
128+
y = [.5, 1, 2, 1, 3, .5]
129+
130+
fig, ax = plt.subplots()
131+
p1 = ax.plot(x, y)
132+
133+
path = p1[0].get_path()
134+
transform = p1[0].get_transform()
135+
path = transform.transform_path(path)
136+
simplified = path.cleaned(simplify=True)
137+
simplified = transform.inverted().transform_path(simplified)
138+
139+
assert_array_almost_equal([[ 0. , 0.5],
140+
[ 0. , 3. ],
141+
[ 1. , 0.5]],
142+
simplified.vertices[:-2, :])
143+
144+
# test no anti-parallel ending in middle
145+
x = [ 0, 0, 0, 0, 0, 1]
146+
y = [.5, 1, 2, 1, 1, .5]
147+
148+
fig, ax = plt.subplots()
149+
p1 = ax.plot(x, y)
150+
151+
path = p1[0].get_path()
152+
transform = p1[0].get_transform()
153+
path = transform.transform_path(path)
57AE 154+
simplified = path.cleaned(simplify=True)
155+
simplified = transform.inverted().transform_path(simplified)
156+
157+
assert_array_almost_equal([[ 0. , 0.5],
158+
[ 0. , 2. ],
159+
[ 0. , 1. ],
160+
[ 1. , 0.5]],
161+
simplified.vertices[:-2, :])
162+
65163
def test_sine_plus_noise():
66164
np.random.seed(0)
67165
x = (np.sin(np.linspace(0, np.pi * 2.0, 50000)) +

src/path_converters.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ class PathSimplifier : protected EmbeddedQueue<9>
822822
double m_dnorm2BackwardMax;
823823
bool m_lastForwardMax;
824824
bool m_lastBackwardMax;
825+
bool m_needToPushBack;
825826
double m_nextX;
826827
double m_nextY;
827828
double m_nextBackwardX;
@@ -831,12 +832,26 @@ class PathSimplifier : protected EmbeddedQueue<9>
831832

832833
inline void _push(double *x, double *y)
833834
{
834-
queue_push(agg::path_cmd_line_to, m_nextX, m_nextY);
835+
m_needToPushBack = (m_dnorm2BackwardMax > 0.0);
835836

836837
/* If we observed any backward (anti-parallel) vectors, then
837-
we need to also move to the furthest backward point. */
838-
if (m_dnorm2BackwardMax > 0.0) {
839-
queue_push(agg::path_cmd_line_to, m_nextBackwardX, m_nextBackwardY);
838+
we need to push both forward and backward vectors. */
839+
if (m_needToPushBack) {
840+
/* If the last vector seen was the maximum in the forward direction,
841+
then we need to push the forward after the backward. Otherwise,
842+
the last vector seen was the maximum in the backward direction,
843+
or somewhere in between, either way we are safe pushing forward
844+
before backward. */
845+
if (m_lastForwardMax) {
846+
queue_push(agg::path_cmd_line_to, m_nextBackwardX, m_nextBackwardY);
847+
queue_push(agg::path_cmd_line_to, m_nextX, m_nextY);
848+
} else {
849+
queue_push(agg::path_cmd_l 634A ine_to, m_nextX, m_nextY);
850+
queue_push(agg::path_cmd_line_to, m_nextBackwardX, m_nextBackwardY);
851+
}
852+
} else {
853+
/* If we did not observe any backwards vectors, just push forward. */
854+
queue_push(agg::path_cmd_line_to, m_nextX, m_nextY);
840855
}
841856

842857
/* If we clipped some segments between this line and the next line

0 commit comments

Comments
 (0)
0