Skip to content

Prevent round_corners() failures for collinear vertices - #4906

Merged
behackl merged 1 commit into
ManimCommunity:mainfrom
MeiSiristhebest:fix/round-corners-collinear-oom-3052
Aug 1, 2026
Merged

Prevent round_corners() failures for collinear vertices#4906
behackl merged 1 commit into
ManimCommunity:mainfrom
MeiSiristhebest:fix/round-corners-collinear-oom-3052

Conversation

@MeiSiristhebest

Copy link
Copy Markdown
Contributor

Summary

Fixes #3052.

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

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

When three consecutive vertices are collinear, one corner forms a (near) 180 degree turn, so tan(angle / 2) diverges to infinity. This previously produced either astronomically large coordinates (default round_corners, e.g. ~2.4e15) or an 8.70 PiB MemoryError when anchors were evenly distributed.

This PR clamps cut_off_length to at most half the shorter adjacent edge via np.clip, so the rounded arc endpoints can never be pushed to infinity. The degenerate collinear case now yields a finite, valid mobject.

Changes

  • manim/mobject/geometry/polygram.py: clamp cut_off_length to [-max_cut_off, max_cut_off] where max_cut_off = min(|vect1|, |vect2|) / 2.
  • tests/module/mobject/geometry/test_unit_geometry.py: add test_round_corners_collinear_points_stays_finite covering both round_corners modes.

Test plan

  • pytest tests/module/mobject/geometry/test_unit_geometry.py -k round_corners passes; full file 13 passed locally.
  • Verified: a collinear polygon no longer produces non-finite coordinates or a MemoryError, and a normal polygon still rounds as before.

Notes

Earlier attempts #4835 and #4873 were closed; this PR applies the same validated clamp direction with a regression test and a clean commit.

Fixes ManimCommunity#3052

When three consecutive vertices of a Polygram are collinear, the turn at a
corner is (near) 180 degrees, which makes tan(angle / 2) diverge to infinity.
This previously produced either astronomically large coordinates (default
round_corners) or an 8.70 PiB MemoryError when anchors were evenly distributed.

Clamp cut_off_length to at most half the shorter adjacent edge so the rounded
arc endpoints can never be pushed to infinity.

@behackl behackl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks for contributing and including the detailed comments!

@behackl
behackl merged commit aefe1b3 into ManimCommunity:main Aug 1, 2026
17 checks passed
@behackl behackl added the pr:bugfix Bug fix for use in PRs solving a specific issue:bug label Aug 1, 2026
@behackl behackl changed the title fix(geometry): clamp round_corners cut-off length on collinear vertices Prevent round_corners() failures for collinear vertices Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:bugfix Bug fix for use in PRs solving a specific issue:bug

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

2 participants