Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Trim empty curves from paths when calling align_points. (#2409)

BorisTheBrave318fb522a6b9
diff --git a/manim/animation/indication.py b/manim/animation/indication.py
index 050f77e3d..fc2d4de2e 100644
--- a/manim/animation/indication.py
+++ b/manim/animation/indication.py
@@ -100,23 +100,19 @@ class FocusOn(Transform):
self.color = color
self.opacity = opacity
remover = True
- # Initialize with blank mobject, while create_target
- # and create_starting_mobject handle the meat
- super().__init__(VGroup(), run_time=run_time, remover=remover, **kwargs)
-
- def create_target(self) -> "Dot":
- little_dot = Dot(radius=0)
- little_dot.set_fill(self.color, opacity=self.opacity)
- little_dot.add_updater(lambda d: d.move_to(self.focus_point))
- return little_dot
-
- def create_starting_mobject(self) -> "Dot":
- return Dot(
+ starting_dot = Dot(
radius=config["frame_x_radius"] + config["frame_y_radius"],
stroke_width=0,
fill_color=self.color,
fill_opacity=0,
)
+ super().__init__(starting_dot, run_time=run_time, remover=remover, **kwargs)
+
+ def create_target(self) -> Dot:
+ little_dot = Dot(radius=0)
+ little_dot.set_fill(self.color, opacity=self.opacity)
+ little_dot.add_updater(lambda d: d.move_to(self.focus_point))
+ return little_dot
class Indicate(Transform):
diff --git a/manim/mobject/types/opengl_vectorized_mobject.py b/manim/mobject/types/opengl_vectorized_mobject.py
index c8c618727..704b50de4 100644
--- a/manim/mobject/types/opengl_vectorized_mobject.py
+++ b/manim/mobject/types/opengl_vectorized_mobject.py
@@ -708,7 +708,8 @@ class OpenGLVMobject(OpenGLMobject):
def get_subpaths(self):
"""Returns subpaths formed by the curves of the OpenGLVMobject.
- We define a subpath between two curve if one of their extreminities are coincidents.
+ Subpaths are ranges of curves with each pair of consecutive
+ curves having their end/start points coincident.
Returns
-------
@@ -1150,6 +1151,7 @@ class OpenGLVMobject(OpenGLMobject):
# Alignment
def align_points(self, vmobject):
+ # TODO: This shortcut can be a bit over eager. What if they have the same length, but different subpath lengths?
if self.get_num_points() == len(vmobject.points):
return
@@ -1177,7 +1179,16 @@ class OpenGLVMobject(OpenGLMobject):
if n >= len(path_list):
# Create a null path at the very end
return [path_list[-1][-1]] * nppc
- return path_list[n]
+ path = path_list[n]
+ # Check for useless points at the end of the path and remove them
+ # https://github.com/ManimCommunity/manim/issues/1959
+ while len(path) > nppc:
+ # If the last nppc points are all equal to the preceding point
+ if self.consider_points_equals(path[-nppc:], path[-nppc - 1]):
+ path = path[:-nppc]
+ else:
+ break
+ return path
for n in range(n_subpaths):
sp1 = get_nth_subpath(subpaths1, n)
diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.py
index 44426d536..c2f034d87 100644
--- a/manim/mobject/types/vectorized_mobject.py
+++ b/manim/mobject/types/vectorized_mobject.py
@@ -1018,7 +1018,7 @@ class VMobject(Mobject):
def get_subpaths(self) -> typing.Tuple:
"""Returns subpaths formed by the curves of the VMobject.
- We define a subpath between two curve if one of their extreminities are coincidents.
+ Subpaths are ranges of curves with each pair of consecutive curves having their end/start points coincident.
Returns
-------
@@ -1363,10 +1363,25 @@ class VMobject(Mobject):
)
# Alignment
- def align_points(self, vmobject):
- # This probably makes the current vmobject and the given one have the same number of points,
- # by adding extra points to the last sub-path. This method is never used in the whole library.
+ def align_points(self, vmobject: "VMobject"):
+ """Adds points to self and vmobject so that they both have the same number of subpaths, with
+ corresponding subpaths each containing the same number of points.
+
+ Points are added either by subdividing curves evenly along the subpath, or by creating new subpaths consisting
+ of a single point repeated.
+
+ Parameters
+ ----------
+ vmobject
+ The object to align points with.
+
+ Returns
+ -------
+ :class:`VMobject`
+ ``self``
+ """
self.align_rgbas(vmobject)
+ # TODO: This shortcut can be a bit over eager. What if they have the same length, but different subpath lengths?
if self.get_num_points() == vmobject.get_num_points():
return
@@ -1380,7 +1395,7 @@ class VMobject(Mobject):
if mob.has_new_path_started():
mob.add_line_to(mob.get_last_point())
- # Figure out what the subpaths are, and align
+ # Figure out what the subpaths are
subpaths1 = self.get_subpaths()
subpaths2 = vmobject.get_subpaths()
n_subpaths = max(len(subpaths1), len(subpaths2))
@@ -1394,9 +1409,19 @@ class VMobject(Mobject):
if n >= len(path_list):
# Create a null path at the very end
return [path_list[-1][-1]] * nppcc
- return path_list[n]
+ path = path_list[n]
+ # Check for useless points at the end of the path and remove them
+ # https://github.com/ManimCommunity/manim/issues/1959
+ while len(path) > nppcc:
+ # If the last nppc points are all equal to the preceding point
+ if self.consider_points_equals(path[-nppcc:], path[-nppcc - 1]):
+ path = path[:-nppcc]
+ else:
+ break
+ return path
for n in range(n_subpaths):
+ # For each pair of subpaths, add points until they are the same length
sp1 = get_nth_subpath(subpaths1, n)
sp2 = get_nth_subpath(subpaths2, n)
diff1 = max(0, (len(sp2) - len(sp1)) // nppcc)
diff --git a/tests/test_vectorized_mobject.py b/tests/test_vectorized_mobject.py
index 9a60ad7b9..e525260a1 100644
--- a/tests/test_vectorized_mobject.py
+++ b/tests/test_vectorized_mobject.py
@@ -1,7 +1,10 @@
+from math import cos, sin
+
import numpy as np
import pytest
from manim import Circle, Line, Mobject, Square, VDict, VGroup, VMobject
+from manim.constants import PI
def test_vmobject_point_from_propotion():
@@ -204,3 +207,61 @@ def test_vgroup_item_assignment_only_allows_vmobjects():
vgroup = VGroup(VMobject())
with pytest.raises(TypeError, match="All submobjects must be of type VMobject"):
vgroup[0] = "invalid object"
+
+
+def test_trim_dummy():
+ o = VMobject()
+ o.start_new_path(np.array([0, 0, 0]))
+ o.add_line_to(np.array([1, 0, 0]))
+ o.add_line_to(np.array([2, 0, 0]))
+ o.add_line_to(np.array([2, 0, 0])) # Dummy point, will be stripped from points
+ o.start_new_path(np.array([0, 1, 0]))
+ o.add_line_to(np.array([1, 2, 0]))
+
+ o2 = VMobject()
+ o2.start_new_path(np.array([0, 0, 0]))
+ o2.add_line_to(np.array([0, 1, 0]))
+ o2.start_new_path(np.array([1, 0, 0]))
+ o2.add_line_to(np.array([1, 1, 0]))
+ o2.add_line_to(np.array([1, 2, 0]))
+
+ def path_length(p):
+ return len(p) // o.n_points_per_cubic_curve
+
+ assert tuple(map(path_length, o.get_subpaths())) == (3, 1)
+ assert tuple(map(path_length, o2.get_subpaths())) == (1, 2)
+
+ o.align_points(o2)
+
+ assert tuple(map(path_length, o.get_subpaths())) == (2, 2)
+ assert tuple(map(path_length, o2.get_subpaths())) == (2, 2)
+
+
+def test_bounded_become():
+ """Tests that align_points generates a bounded number of points.
+ https://github.com/ManimCommunity/manim/issues/1959
+ """
+ o = VMobject()
+
+ def draw_circle(m: VMobject, n_points, x=0, y=0, r=1):
+ center = np.array([x, y, 0])
+ m.start_new_path(center + [r, 0, 0])
+ for i in range(1, n_points + 1):
+ theta = 2 * PI * i / n_points
+ m.add_line_to(center + [cos(theta) * r, sin(theta) * r, 0])
+
+ # o must contain some points, or else become behaves differently
+ draw_circle(o, 2)
+
+ for _ in range(20):
+ # Alternate between calls to become with different subpath sizes
+ a = VMobject()
+ draw_circle(a, 20)
+ o.become(a)
+ b = VMobject()
+ draw_circle(b, 15)
+ draw_circle(b, 15, x=3)
+ o.become(b)
+
+ # The number of points should be similar to the size of a and b
+ assert len(o.points) <= (20 + 15 + 15) * 4