fix(geometry): clamp round_corners cut_off_length to prevent OOM on collinear vertices#4835
Open
sridhar-3009 wants to merge 2 commits into
Open
Conversation
…ollinear vertices
When three or more consecutive vertices of a Polygon are collinear, one
of the adjacent edge pairs becomes anti-parallel (angle ≈ π). The formula
cut_off_length = radius * tan(angle / 2)
evaluates tan(π/2) ≈ 1.6e16, making cut_off_length astronomically large.
ArcBetweenPoints then tried to span endpoints that are ~2e15 units apart,
causing the reported 8.70 PiB memory allocation attempt.
Fix: clamp cut_off_length to min(|edge1|, |edge2|) / 2 before passing it
to ArcBetweenPoints. This ensures the arc start and end points never
exceed the midpoint of either adjacent segment, regardless of the angle.
The collinear degenerate case now silently produces a valid (finite)
mobject instead of attempting a fatal allocation.
Fixes ManimCommunity#3052.
for more information, see https://pre-commit.ci
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3052.
Root cause
Polygram.round_cornerscomputes the distance between the corner vertex and the start of the rounded arc:When three consecutive vertices are collinear, one adjacent edge pair is anti-parallel (angle ≈ π). Since
tan(π/2)approaches infinity in floating point (~1.6 × 10¹⁶),cut_off_lengthbecomes astronomically large.ArcBetweenPointsthen tries to span endpoints that are ~2 × 10¹⁵ units apart, triggering the reported 8.70 PiB allocation attempt.Fix
Clamp
cut_off_lengthtomin(|edge1|, |edge2|) / 2usingnp.clipbefore passing it toArcBetweenPoints. This ensures the rounded arc start/end points never exceed the midpoint of either adjacent segment, regardless of the angle between them.The degenerate collinear case now silently produces a valid, finite-coordinate mobject instead of attempting a fatal allocation. This matches the expected behavior described in the issue: "draw a straight line (but not try to allocate 8.70 PiB of memory)."
Test
test_round_corners_collinear_points_does_not_oom:Polygonwith three collinear pointsround_corners(0.15)Test plan
pytest tests/module/mobject/geometry/test_unit_geometry.py --no-cov— 13 passedtest_round_corners_collinear_points_does_not_oompasses