Skip to content

Commit 7879fe4

Browse files
authored
Fix VMobject.add_points_as_corners() to return self and safely handle empty points parameter (#4091)
* Fix VMobject.add_points_as_corners() to return self and safely handle case where no points are passed * Add test_vmobject_add_points_as_corners() * assert np.allclose() -> np.testing.assert_allclose()
1 parent 17d4a7f commit 7879fe4

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

manim/mobject/types/vectorized_mobject.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ def close_path(self) -> None:
10391039
if not self.is_closed():
10401040
self.add_line_to(self.get_subpaths()[-1][0])
10411041

1042-
def add_points_as_corners(self, points: Point3DLike_Array) -> Point3D_Array:
1042+
def add_points_as_corners(self, points: Point3DLike_Array) -> Self:
10431043
"""Append multiple straight lines at the end of
10441044
:attr:`VMobject.points`, which connect the given ``points`` in order
10451045
starting from the end of the current path. These ``points`` would be
@@ -1058,10 +1058,14 @@ def add_points_as_corners(self, points: Point3DLike_Array) -> Point3D_Array:
10581058
path.
10591059
"""
10601060
points = np.asarray(points).reshape(-1, self.dim)
1061+
num_points = points.shape[0]
1062+
if num_points == 0:
1063+
return self
1064+
10611065
if self.has_new_path_started():
10621066
# Pop the last point from self.points and
10631067
# add it to start_corners
1064-
start_corners = np.empty((len(points), self.dim))
1068+
start_corners = np.empty((num_points, self.dim))
10651069
start_corners[0] = self.points[-1]
10661070
start_corners[1:] = points[:-1]
10671071
end_corners = points
@@ -1078,8 +1082,7 @@ def add_points_as_corners(self, points: Point3DLike_Array) -> Point3D_Array:
10781082
new_points[i::nppcc] = interpolate(start_corners, end_corners, t)
10791083

10801084
self.append_points(new_points)
1081-
# TODO: shouldn't this method return self instead of points?
1082-
return points
1085+
return self
10831086

10841087
def set_points_as_corners(self, points: Point3DLike_Array) -> Self:
10851088
"""Given an array of points, set them as corners of the

tests/module/mobject/types/vectorized_mobject/test_vectorized_mobject.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,37 @@ def test_vmobject_add():
6565
assert len(obj.submobjects) == 1
6666

6767

68+
def test_vmobject_add_points_as_corners():
69+
points = np.array(
70+
[
71+
[2, 0, 0],
72+
[1, 1, 0],
73+
[-1, 1, 0],
74+
[-2, 0, 0],
75+
[-1, -1, 0],
76+
[1, -1, 0],
77+
[2, 0, 0],
78+
]
79+
)
80+
81+
# Test that add_points_as_corners(points) is equivalent to calling
82+
# add_line_to(point) for every point in points.
83+
obj1 = VMobject().start_new_path(points[0]).add_points_as_corners(points[1:])
84+
obj2 = VMobject().start_new_path(points[0])
85+
for point in points[1:]:
86+
obj2.add_line_to(point)
87+
np.testing.assert_allclose(obj1.points, obj2.points)
88+
89+
# Test that passing an array with no points does nothing.
90+
obj3 = VMobject().start_new_path(points[0])
91+
points3_old = obj3.points.copy()
92+
obj3.add_points_as_corners([])
93+
np.testing.assert_allclose(points3_old, obj3.points)
94+
95+
obj3.add_points_as_corners(points[1:]).add_points_as_corners([])
96+
np.testing.assert_allclose(obj1.points, obj3.points)
97+
98+
6899
def test_vmobject_point_from_proportion():
69100
obj = VMobject()
70101

0 commit comments

Comments
 (0)