Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Improve performance for :meth:`~become` (#2508)

Darylgolden8d94325058d0
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py
index 226400d39..c450b6da5 100644
--- a/manim/mobject/mobject.py
+++ b/manim/mobject/mobject.py
@@ -2471,10 +2471,26 @@ class Mobject:
return self.shuffle(*args, **kwargs)
# Alignment
- def align_data(self, mobject: "Mobject"):
+ def align_data(self, mobject: "Mobject", skip_point_alignment: bool = False):
+ """Aligns the data of this mobject with another mobject.
+
+ Afterwards, the two mobjects will have the same number of submobjects
+ (see :meth:`.align_submobjects`), the same parent structure (see
+ :meth:`.null_point_align`). If ``skip_point_alignment`` is false,
+ they will also have the same number of points (see :meth:`.align_points`).
+
+ Parameters
+ ----------
+ mobject
+ The other mobject this mobject should be aligned to.
+ skip_point_alignment
+ Controls whether or not the computationally expensive
+ point alignment is skipped (default: False).
+ """
self.null_point_align(mobject)
self.align_submobjects(mobject)
- self.align_points(mobject)
+ if not skip_point_alignment:
+ self.align_points(mobject)
# Recurse
for m1, m2 in zip(self.submobjects, mobject.submobjects):
m1.align_data(m2)
@@ -2644,7 +2660,7 @@ class Mobject:
if match_center:
mobject.move_to(self.get_center())
- self.align_data(mobject)
+ self.align_data(mobject, skip_point_alignment=True)
for sm1, sm2 in zip(self.get_family(), mobject.get_family()):
sm1.points = np.array(sm2.points)
sm1.interpolate_color(sm1, sm2, 1)
@@ -2667,7 +2683,6 @@ class Mobject:
self.play(circ.animate.match_points(square))
self.wait(0.5)
"""
- self.align_data(mobject)
for sm1, sm2 in zip(self.get_family(), mobject.get_family()):
sm1.points = np.array(sm2.points)
return self
diff --git a/tests/test_vectorized_mobject.py b/tests/test_vectorized_mobject.py
index e525260a1..5da5c3ac1 100644
--- a/tests/test_vectorized_mobject.py
+++ b/tests/test_vectorized_mobject.py
@@ -3,7 +3,7 @@ from math import cos, sin
import numpy as np
import pytest
-from manim import Circle, Line, Mobject, Square, VDict, VGroup, VMobject
+from manim import Circle, Line, Mobject, RegularPolygon, Square, VDict, VGroup, VMobject
from manim.constants import PI
@@ -265,3 +265,27 @@ def test_bounded_become():
# The number of points should be similar to the size of a and b
assert len(o.points) <= (20 + 15 + 15) * 4
+
+
+def test_vmobject_same_points_become():
+ a = Square()
+ b = Circle()
+ a.become(b)
+ assert np.array_equal(a.points, b.points)
+ assert len(a.submobjects) == len(b.submobjects)
+
+
+def test_vmobject_same_num_submobjects_become():
+ a = Square()
+ b = RegularPolygon(n=6)
+ a.become(b)
+ assert np.array_equal(a.points, b.points)
+ assert len(a.submobjects) == len(b.submobjects)
+
+
+def test_vmobject_different_num_points_and_submobjects_become():
+ a = Square()
+ b = VGroup(Circle(), Square())
+ a.become(b)
+ assert np.array_equal(a.points, b.points)
+ assert len(a.submobjects) == len(b.submobjects)