A community-maintained Python framework for creating mathematical animations.
Fix return type of `Polygram.get_vertex_groups()` and rename variables in `.round_corners()` (#4063)
Francisco Manríquez Novoaf304bd93eab1
diff --git a/manim/mobject/geometry/polygram.py b/manim/mobject/geometry/polygram.pyindex 7baf6f183..5a8dabdac 100644--- a/manim/mobject/geometry/polygram.py+++ b/manim/mobject/geometry/polygram.py@@ -38,7 +38,6 @@ if TYPE_CHECKING:from typing_extensions import Selffrom manim.typing import (- ManimFloat,Point3D,Point3D_Array,Point3DLike,@@ -122,39 +121,45 @@ class Polygram(VMobject, metaclass=ConvertToOpenGL):"""return self.get_start_anchors()- def get_vertex_groups(self) -> npt.NDArray[ManimFloat]:+ def get_vertex_groups(self) -> list[Point3D_Array]:"""Gets the vertex groups of the :class:`Polygram`.Returns-------- :class:`numpy.ndarray`- The vertex groups of the :class:`Polygram`.+ list[Point3D_Array]+ The list of vertex groups of the :class:`Polygram`.Examples--------::- >>> poly = Polygram([ORIGIN, RIGHT, UP], [LEFT, LEFT + UP, 2 * LEFT])- >>> poly.get_vertex_groups()- array([[[ 0., 0., 0.],- [ 1., 0., 0.],- [ 0., 1., 0.]],- <BLANKLINE>- [[-1., 0., 0.],- [-1., 1., 0.],- [-2., 0., 0.]]])+ >>> poly = Polygram([ORIGIN, RIGHT, UP, LEFT + UP], [LEFT, LEFT + UP, 2 * LEFT])+ >>> groups = poly.get_vertex_groups()+ >>> len(groups)+ 2+ >>> groups[0]+ array([[ 0., 0., 0.],+ [ 1., 0., 0.],+ [ 0., 1., 0.],+ [-1., 1., 0.]])+ >>> groups[1]+ array([[-1., 0., 0.],+ [-1., 1., 0.],+ [-2., 0., 0.]])"""vertex_groups = []+ # TODO: If any of the original vertex groups contained the starting vertex N+ # times, then .get_vertex_groups() splits it into N vertex groups.group = []for start, end in zip(self.get_start_anchors(), self.get_end_anchors()):group.append(start)if self.consider_points_equals(end, group[0]):- vertex_groups.append(group)+ vertex_groups.append(np.array(group))group = []- return np.array(vertex_groups)+ return vertex_groupsdef round_corners(self,@@ -223,18 +228,18 @@ class Polygram(VMobject, metaclass=ConvertToOpenGL):new_points: list[Point3D] = []- for vertices in self.get_vertex_groups():+ for vertex_group in self.get_vertex_groups():arcs = []# Repeat the radius list as necessary in order to provide a radius# for each vertex.if isinstance(radius, (int, float)):- radius_list = [radius] * len(vertices)+ radius_list = [radius] * len(vertex_group)else:- radius_list = radius * ceil(len(vertices) / len(radius))+ radius_list = radius * ceil(len(vertex_group) / len(radius))- for currentRadius, (v1, v2, v3) in zip(- radius_list, adjacent_n_tuples(vertices, 3)+ for current_radius, (v1, v2, v3) in zip(+ radius_list, adjacent_n_tuples(vertex_group, 3)):vect1 = v2 - v1vect2 = v3 - v2@@ -243,10 +248,10 @@ class Polygram(VMobject, metaclass=ConvertToOpenGL):angle = angle_between_vectors(vect1, vect2)# Negative radius gives concave curves- angle *= np.sign(currentRadius)+ angle *= np.sign(current_radius)# Distance between vertex and start of the arc- cut_off_length = currentRadius * np.tan(angle / 2)+ cut_off_length = current_radius * np.tan(angle / 2)# Determines counterclockwise vs. clockwisesign = np.sign(np.cross(vect1, vect2)[2])@@ -261,17 +266,17 @@ class Polygram(VMobject, metaclass=ConvertToOpenGL):if evenly_distribute_anchors:# Determine the average length of each curve- nonZeroLengthArcs = [arc for arc in arcs if len(arc.points) > 4]- if len(nonZeroLengthArcs):- totalArcLength = sum(- [arc.get_arc_length() for arc in nonZeroLengthArcs]+ nonzero_length_arcs = [arc for arc in arcs if len(arc.points) > 4]+ if len(nonzero_length_arcs) > 0:+ total_arc_length = sum(+ [arc.get_arc_length() for arc in nonzero_length_arcs])- totalCurveCount = (- sum([len(arc.points) for arc in nonZeroLengthArcs]) / 4+ num_curves = (+ sum([len(arc.points) for arc in nonzero_length_arcs]) / 4)- averageLengthPerCurve = totalArcLength / totalCurveCount+ average_arc_length = total_arc_length / num_curveselse:- averageLengthPerCurve = 1+ average_arc_length = 1.0# To ensure that we loop through starting with lastarcs = [arcs[-1], *arcs[:-1]]@@ -284,9 +289,7 @@ class Polygram(VMobject, metaclass=ConvertToOpenGL):# Make sure anchors are evenly distributed, if necessaryif evenly_distribute_anchors:- line.insert_n_curves(- ceil(line.get_length() / averageLengthPerCurve)- )+ line.insert_n_curves(ceil(line.get_length() / average_arc_length))new_points.extend(line.points)diff --git a/tests/module/mobject/geometry/test_unit_geometry.py b/tests/module/mobject/geometry/test_unit_geometry.pyindex 4f5403a14..b8594260b 100644--- a/tests/module/mobject/geometry/test_unit_geometry.py+++ b/tests/module/mobject/geometry/test_unit_geometry.py@@ -4,7 +4,14 @@ import loggingimport numpy as np-from manim import BackgroundRectangle, Circle, Sector, Square, SurroundingRectangle+from manim import (+ BackgroundRectangle,+ Circle,+ Polygram,+ Sector,+ Square,+ SurroundingRectangle,+)logger = logging.getLogger(__name__)@@ -15,6 +22,88 @@ def test_get_arc_center():)+def test_Polygram_get_vertex_groups():+ # Test that, once a Polygram polygram is created with some vertex groups,+ # polygram.get_vertex_groups() (usually) returns the same vertex groups.+ vertex_groups_arr = [+ # 2 vertex groups for polygram 1+ [+ # Group 1: Triangle+ np.array(+ [+ [2, 1, 0],+ [0, 2, 0],+ [-2, 1, 0],+ ]+ ),+ # Group 2: Square+ np.array(+ [+ [1, 0, 0],+ [0, 1, 0],+ [-1, 0, 0],+ [0, -1, 0],+ ]+ ),+ ],+ # 3 vertex groups for polygram 1+ [+ # Group 1: Quadrilateral+ np.array(+ [+ [2, 0, 0],+ [0, -1, 0],+ [0, 0, -2],+ [0, 1, 0],+ ]+ ),+ # Group 2: Triangle+ np.array(+ [+ [3, 1, 0],+ [0, 0, 2],+ [2, 0, 0],+ ]+ ),+ # Group 3: Pentagon+ np.array(+ [+ [1, -1, 0],+ [1, 1, 0],+ [0, 2, 0],+ [-1, 1, 0],+ [-1, -1, 0],+ ]+ ),+ ],+ ]++ for vertex_groups in vertex_groups_arr:+ polygram = Polygram(*vertex_groups)+ poly_vertex_groups = polygram.get_vertex_groups()+ for poly_group, group in zip(poly_vertex_groups, vertex_groups):+ np.testing.assert_array_equal(poly_group, group)++ # If polygram is a Polygram of a vertex group containing the start vertex N times,+ # then polygram.get_vertex_groups() splits it into N vertex groups.+ splittable_vertex_group = np.array(+ [+ [0, 1, 0],+ [1, -2, 0],+ [1, 2, 0],+ [0, 1, 0], # same vertex as start+ [-1, 2, 0],+ [-1, -2, 0],+ [0, 1, 0], # same vertex as start+ [0.5, 2, 0],+ [-0.5, 2, 0],+ ]+ )++ polygram = Polygram(splittable_vertex_group)+ assert len(polygram.get_vertex_groups()) == 3++def test_SurroundingRectangle():circle = Circle()square = Square()