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

Skip to content

Commit 1d7f094

Browse files
committed
Trim Gouraud triangles that contain NaN
Agg enters an infinite loop if you give it points that are NaN, due to converting the values to fixed-point integers, and then oscillating between the large values that result from that conversion. NaN values may be introduced after transforming the input, so we need to trim those after transformation. This matches what is done in normal paths.
1 parent e65a4e3 commit 1d7f094

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lib/matplotlib/tests/test_transforms.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,25 @@ def test_pcolormesh_pre_transform_limits():
142142
assert_almost_equal(expected, ax.dataLim.get_points())
143143

144144

145+
def test_pcolormesh_gouraud_nans():
146+
np.random.seed(19680801)
147+
148+
values = np.linspace(0, 180, 3)
149+
radii = np.linspace(100, 1000, 10)
150+
z, y = np.meshgrid(values, radii)
151+
x = np.radians(np.random.rand(*z.shape) * 100)
152+
153+
fig = plt.figure()
154+
ax = fig.add_subplot(111, projection="polar")
155+
# Setting the limit to cause clipping of the r values causes NaN to be
156+
# introduced; these should not crash but be ignored as in other path
157+
# operations.
158+
ax.set_rlim(101, 1000)
159+
ax.pcolormesh(x, y, z, shading="gouraud")
160+
161+
fig.canvas.draw()
162+
163+
145164
def test_Affine2D_from_values():
146165
points = np.array([[0, 0],
147166
[10, 20],

src/_backend_agg.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,9 @@ inline void RendererAgg::_draw_gouraud_triangle(PointArray &points,
11931193
tpoints[i][j] = points(i, j);
11941194
}
11951195
trans.transform(&tpoints[i][0], &tpoints[i][1]);
1196+
if(std::isnan(tpoints[i][0]) || std::isnan(tpoints[i][1])) {
1197+
return;
1198+
}
11961199
}
11971200

11981201
span_alloc_t span_alloc;

0 commit comments

Comments
 (0)