Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fix is_closed misclassifying closed curves in negative coordinate space (#5010)

Syed Osama Ali Shahb949b73e5bd4
diff --git a/manim/utils/bezier.py b/manim/utils/bezier.py
index c5577866f..b2e8d08de 100644
--- a/manim/utils/bezier.py
+++ b/manim/utils/bezier.py
@@ -1956,7 +1956,7 @@ def is_closed(points: Point3D_Array) -> bool:
start, end = points[0], points[-1]
rtol = 1e-5
atol = 1e-8
- tolerance = atol + rtol * start
+ tolerance = atol + rtol * np.abs(start)
if abs(end[0] - start[0]) > tolerance[0]:
return False
if abs(end[1] - start[1]) > tolerance[1]:
diff --git a/tests/module/utils/test_bezier.py b/tests/module/utils/test_bezier.py
index 19d3076ff..6d5a05c61 100644
--- a/tests/module/utils/test_bezier.py
+++ b/tests/module/utils/test_bezier.py
@@ -11,6 +11,7 @@ from manim.utils.bezier import (
get_quadratic_approximation_of_cubic,
get_smooth_cubic_bezier_handle_points,
interpolate,
+ is_closed,
partial_bezier_points,
split_bezier,
subdivide_bezier,
@@ -231,3 +232,20 @@ def test_interpolate() -> None:
alpha = 0.09739
val = interpolate(start, end, alpha)
assert np.allclose(val, np.array([117.06622]))
+
+
+def test_is_closed_in_negative_space() -> None:
+ """:func:`is_closed` must use ``abs`` in its relative tolerance.
+
+ It hand-rolls ``np.allclose``; without ``abs`` on the reference term the
+ tolerance ``atol + rtol * start`` goes negative once a start coordinate is
+ below ``-1e-3``, so an exactly closed curve shifted into negative space was
+ misclassified as open.
+ """
+ closed = np.array([[-5.0, 0.0, 0.0], [1.0, 2.0, 3.0], [-5.0, 0.0, 0.0]])
+ assert is_closed(closed)
+ # A positive-space control and an open curve stay correct.
+ assert is_closed(np.array([[5.0, 0.0, 0.0], [1.0, 2.0, 3.0], [5.0, 0.0, 0.0]]))
+ assert not is_closed(
+ np.array([[-5.0, 0.0, 0.0], [1.0, 2.0, 3.0], [-5.0, 0.0, 1.0]])
+ )