Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return type of Polygram.get_vertex_groups() and rename variables in .round_corners() #4063

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 34 additions & 33 deletions manim/mobject/geometry/polygram.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from typing_extensions import Self

from manim.typing import (
ManimFloat,
Point3D,
Point3D_Array,
Point3DLike,
Expand Down Expand Up @@ -122,27 +121,31 @@ def get_vertices(self) -> Point3D_Array:
"""
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 = []

Expand All @@ -151,10 +154,10 @@ def get_vertex_groups(self) -> npt.NDArray[ManimFloat]:
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_groups

def round_corners(
self,
Expand Down Expand Up @@ -223,18 +226,18 @@ def construct(self):

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 - v1
vect2 = v3 - v2
Expand All @@ -243,10 +246,10 @@ def construct(self):

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. clockwise
sign = np.sign(np.cross(vect1, vect2)[2])
Expand All @@ -261,17 +264,17 @@ def construct(self):

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_curves
else:
averageLengthPerCurve = 1
average_arc_length = 1.0

# To ensure that we loop through starting with last
arcs = [arcs[-1], *arcs[:-1]]
Expand All @@ -284,9 +287,7 @@ def construct(self):

# Make sure anchors are evenly distributed, if necessary
if 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)

Expand Down
Loading