A community-maintained Python framework for creating mathematical animations.
fix(geometry): clamp round_corners cut-off length on collinear vertices (#4906)
Nefelibataaefe1b3893ec
diff --git a/manim/mobject/geometry/polygram.py b/manim/mobject/geometry/polygram.pyindex 2a2ca133f..252630097 100644--- a/manim/mobject/geometry/polygram.py+++ b/manim/mobject/geometry/polygram.py@@ -254,6 +254,15 @@ class Polygram(VMobject, metaclass=ConvertToOpenGL):# Distance between vertex and start of the arccut_off_length = current_radius * np.tan(angle / 2)+ # Clamp the cut-off length so that a (near) 180 degree turn+ # between consecutive edges - which makes ``tan(angle / 2)``+ # diverge to infinity - cannot push the arc endpoints to+ # infinity. This also bounds the rounded corner to at most half+ # of the shorter adjacent edge, matching the expected behavior+ # for degenerate collinear polygons (see issue #3052).+ max_cut_off = min(np.linalg.norm(vect1), np.linalg.norm(vect2)) / 2+ cut_off_length = np.clip(cut_off_length, -max_cut_off, max_cut_off)+# Determines counterclockwise vs. clockwisesign = np.sign(np.cross(vect1, vect2)[2])diff --git a/tests/module/mobject/geometry/test_unit_geometry.py b/tests/module/mobject/geometry/test_unit_geometry.pyindex e6da24835..1f0f5f527 100644--- a/tests/module/mobject/geometry/test_unit_geometry.py+++ b/tests/module/mobject/geometry/test_unit_geometry.py@@ -15,6 +15,7 @@ from manim import (BackgroundRectangle,Circle,Line,+ Polygon,Polygram,Sector,Square,@@ -263,3 +264,18 @@ def test_Circle_point_at_angle():# Angle 0 should return start point even after reflectionp_reflected_0 = reflected_circle.point_at_angle(0)np.testing.assert_array_almost_equal(p_reflected_0, reflected_start, decimal=5)+++def test_round_corners_collinear_points_stays_finite():+ # Regression test for https://github.com/ManimCommunity/manim/issues/3052+ # Collinear vertices create a (near) 180 degree turn at a corner, which+ # makes ``tan(angle / 2)`` diverge to infinity. This previously produced+ # either astronomically large coordinates (default mode) or an 8.70 PiB+ # ``MemoryError`` when anchors were evenly distributed.+ for kwargs in ({}, {"evenly_distribute_anchors": True}):+ poly = Polygon([-1, 0, 0], [0, 0, 0], [1, 0, 0])+ poly.round_corners(0.15, **kwargs)+ assert np.all(np.isfinite(poly.points))+ # The rounded corner is bounded by half the shorter adjacent edge, so+ # no coordinate should blow up beyond the original extent.+ assert np.max(np.abs(poly.points)) <= 1.0