Skip to content

fix(geometry): clamp round_corners cut_off_length to prevent OOM on collinear vertices#4835

Open
sridhar-3009 wants to merge 2 commits into
ManimCommunity:mainfrom
sridhar-3009:fix/round-corners-collinear-oom-3052
Open

fix(geometry): clamp round_corners cut_off_length to prevent OOM on collinear vertices#4835
sridhar-3009 wants to merge 2 commits into
ManimCommunity:mainfrom
sridhar-3009:fix/round-corners-collinear-oom-3052

Conversation

@sridhar-3009

Copy link
Copy Markdown
Contributor

Summary

Fixes #3052.

Root cause

Polygram.round_corners computes the distance between the corner vertex and the start of the rounded arc:

cut_off_length = current_radius * np.tan(angle / 2)

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_length becomes astronomically large. ArcBetweenPoints then tries to span endpoints that are ~2 × 10¹⁵ units apart, triggering the reported 8.70 PiB allocation attempt.

Fix

Clamp cut_off_length to min(|edge1|, |edge2|) / 2 using np.clip before passing it to ArcBetweenPoints. 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:

  • Creates a Polygon with three collinear points
  • Calls round_corners(0.15)
  • Asserts the result contains only finite coordinates (no Inf or NaN)

Test plan

  • pytest tests/module/mobject/geometry/test_unit_geometry.py --no-cov — 13 passed
  • New test test_round_corners_collinear_points_does_not_oom passes

sridhar-3009 and others added 2 commits June 26, 2026 21:56
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Polygon tries to allocate 8.7 PiB of memory when rounding corners

1 participant