Skip to content

Commit

Permalink
Improve performance for :meth:~become (#2508)
Browse files Browse the repository at this point in the history
* Improve become method

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Make match_num_points kwarg

* fix test

* typehint and kwarg

* Add tests for become

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* experiment

* Undo match_num_points

* readd return self

* test fix

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* undo changes to match points

* use skip point alignment

* delete accidental inclusion

* Added documentation

Co-authored-by: Benjamin Hackl <devel@benjamin-hackl.at>

* Minor correction

* fix indentation

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix formatting

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Benjamin Hackl <devel@benjamin-hackl.at>
  • Loading branch information
3 people committed Jan 29, 2022
1 parent 717abfb commit 8d94325
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
23 changes: 19 additions & 4 deletions manim/mobject/mobject.py
Expand Up @@ -2471,10 +2471,26 @@ def construct(self):
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)
Expand Down Expand Up @@ -2644,7 +2660,7 @@ def construct(self):
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)
Expand All @@ -2667,7 +2683,6 @@ def construct(self):
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
Expand Down
26 changes: 25 additions & 1 deletion tests/test_vectorized_mobject.py
Expand Up @@ -3,7 +3,7 @@
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


Expand Down Expand Up @@ -265,3 +265,27 @@ def draw_circle(m: VMobject, n_points, x=0, y=0, r=1):

# 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)

0 comments on commit 8d94325

Please sign in to comment.