A community-maintained Python framework for creating mathematical animations.
Fix triangulation ring off-by-one error (#4819)
Theelxfbb3f8c5b028
diff --git a/manim/utils/space_ops.py b/manim/utils/space_ops.pyindex 4af2ad34d..d55c623c5 100644--- a/manim/utils/space_ops.py+++ b/manim/utils/space_ops.py@@ -773,7 +773,11 @@ def earclip_triangulation(verts: np.ndarray, ring_ends: list) -> list:# Move the ring which j belongs to from the# attached list to the detached listnew_ring = next(- (ring for ring in detached_rings if ring[0] <= j < ring[-1]), None+ # ring[-1] is the last valid index in the ring so the upper bound needs+ # to be inclusive. Otherwise, a connection point on a ring's final vertex+ # doesn't match any ring and triggers "Could not find a ring to attach"+ (ring for ring in detached_rings if ring[0] <= j <= ring[-1]),+ None,)if new_ring is not None:detached_rings.remove(new_ring)diff --git a/tests/module/utils/test_space_ops.py b/tests/module/utils/test_space_ops.pyindex 06bd59680..bcaf8168f 100644--- a/tests/module/utils/test_space_ops.py+++ b/tests/module/utils/test_space_ops.py@@ -122,3 +122,29 @@ def test_polar_coords():np.testing.assert_array_equal(np.round(spherical_to_cartesian(b), 4), np.array([0, 2, 0]))+++def test_triangulation_ring_connection():+ verts = np.array(+ [+ # outer ring+ [-2, -2, 0],+ [2, -2, 0],+ [2, 2, 0],+ [-2, 2, 0],+ # inner ring (hole)+ [-0.5, -1.5, 0],+ [-0.5, -0.5, 0],+ [-1.5, -0.5, 0],+ [-1.5, -1.5, 0],+ ],+ dtype=float,+ )+ ring_ends = [4, 8]++ triangulation = earclip_triangulation(verts, ring_ends)++ assert len(triangulation) > 0+ assert len(triangulation) % 3 == 0+ assert min(triangulation) >= 0+ assert max(triangulation) < len(verts)