From 8b83c5e27d4c092b7c73a8706658ff9a4edf1346 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Wed, 13 Jan 2021 11:55:45 -1000 Subject: [PATCH] Performance improvement for Write --- manimlib/animation/creation.py | 35 ++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/manimlib/animation/creation.py b/manimlib/animation/creation.py index 4165415c..f888bb73 100644 --- a/manimlib/animation/creation.py +++ b/manimlib/animation/creation.py @@ -16,6 +16,14 @@ class ShowPartial(Animation): """ Abstract class for ShowCreation and ShowPassingFlash """ + CONFIG = { + "should_match_start": False, + } + + def begin(self): + super().begin() + if not self.should_match_start: + self.mobject.lock_matching_data(self.mobject, self.starting_mobject) def interpolate_submobject(self, submob, start_submob, alpha): submob.pointwise_become_partial( @@ -38,7 +46,8 @@ class ShowCreation(ShowPartial): class Uncreate(ShowCreation): CONFIG = { "rate_func": lambda t: smooth(1 - t), - "remover": True + "remover": True, + "should_match_start": True, } @@ -53,18 +62,18 @@ class DrawBorderThenFill(Animation): } def __init__(self, vmobject, **kwargs): - self.check_validity_of_input(vmobject) + assert(isinstance(vmobject, VMobject)) + self.sm_to_index = dict([ + (hash(sm), 0) + for sm in vmobject.get_family() + ]) super().__init__(vmobject, **kwargs) - def check_validity_of_input(self, vmobject): - if not isinstance(vmobject, VMobject): - raise Exception( - "DrawBorderThenFill only works for VMobjects" - ) - def begin(self): self.outline = self.get_outline() super().begin() + self.mobject.match_style(self.outline) + self.mobject.lock_matching_data(self.mobject, self.outline) def get_outline(self): outline = self.mobject.copy() @@ -88,11 +97,17 @@ class DrawBorderThenFill(Animation): def interpolate_submobject(self, submob, start, outline, alpha): index, subalpha = integer_interpolate(0, 2, alpha) + + if index == 1 and self.sm_to_index[hash(submob)] == 0: + # First time crossing over + submob.set_data(outline.data) + submob.unlock_data() + submob.lock_matching_data(submob, start) + self.sm_to_index[hash(submob)] = 1 + if index == 0: submob.pointwise_become_partial(outline, 0, subalpha) - submob.match_style(outline) else: - submob.pointwise_become_partial(outline, 0, 1) submob.interpolate(outline, start, subalpha)