2022-02-15 18:39:45 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
from abc import ABC, abstractmethod
|
2022-02-15 18:39:45 +08:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.animation.animation import Animation
|
2022-03-31 10:36:14 +08:00
|
|
|
from manimlib.mobject.svg.labelled_string import LabelledString
|
2022-04-23 17:17:43 +08:00
|
|
|
from manimlib.mobject.types.vectorized_mobject import VGroup
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.mobject.types.vectorized_mobject import VMobject
|
2019-02-09 08:59:02 -08:00
|
|
|
from manimlib.utils.bezier import integer_interpolate
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.utils.config_ops import digest_config
|
2019-02-05 15:39:58 -08:00
|
|
|
from manimlib.utils.rate_functions import linear
|
2019-02-09 08:59:02 -08:00
|
|
|
from manimlib.utils.rate_functions import double_smooth
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.utils.rate_functions import smooth
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
from typing import TYPE_CHECKING
|
2022-02-16 21:08:25 +08:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
if TYPE_CHECKING:
|
2022-04-23 17:17:43 +08:00
|
|
|
from manimlib.mobject.mobject import Mobject
|
2019-06-13 09:25:14 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
class ShowPartial(Animation, ABC):
|
2019-02-08 15:44:58 -08:00
|
|
|
"""
|
|
|
|
Abstract class for ShowCreation and ShowPassingFlash
|
|
|
|
"""
|
2021-01-13 11:55:45 -10:00
|
|
|
CONFIG = {
|
|
|
|
"should_match_start": False,
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def begin(self) -> None:
|
2021-01-13 11:55:45 -10:00
|
|
|
super().begin()
|
|
|
|
if not self.should_match_start:
|
|
|
|
self.mobject.lock_matching_data(self.mobject, self.starting_mobject)
|
2019-02-08 15:44:58 -08:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def finish(self) -> None:
|
2021-03-18 17:43:15 -07:00
|
|
|
super().finish()
|
|
|
|
self.mobject.unlock_data()
|
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def interpolate_submobject(
|
|
|
|
self,
|
|
|
|
submob: VMobject,
|
|
|
|
start_submob: VMobject,
|
|
|
|
alpha: float
|
|
|
|
) -> None:
|
2019-02-08 15:44:58 -08:00
|
|
|
submob.pointwise_become_partial(
|
|
|
|
start_submob, *self.get_bounds(alpha)
|
2018-03-31 15:11:35 -07:00
|
|
|
)
|
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
@abstractmethod
|
|
|
|
def get_bounds(self, alpha: float) -> tuple[float, float]:
|
2018-03-31 15:11:35 -07:00
|
|
|
raise Exception("Not Implemented")
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class ShowCreation(ShowPartial):
|
|
|
|
CONFIG = {
|
2019-02-08 15:44:58 -08:00
|
|
|
"lag_ratio": 1,
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def get_bounds(self, alpha: float) -> tuple[float, float]:
|
2018-03-31 15:11:35 -07:00
|
|
|
return (0, alpha)
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class Uncreate(ShowCreation):
|
|
|
|
CONFIG = {
|
2018-04-06 13:08:57 -07:00
|
|
|
"rate_func": lambda t: smooth(1 - t),
|
2021-01-13 11:55:45 -10:00
|
|
|
"remover": True,
|
|
|
|
"should_match_start": True,
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
|
|
|
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2019-02-09 08:59:02 -08:00
|
|
|
class DrawBorderThenFill(Animation):
|
2018-03-31 15:11:35 -07:00
|
|
|
CONFIG = {
|
2018-04-06 13:08:57 -07:00
|
|
|
"run_time": 2,
|
2019-02-09 08:59:02 -08:00
|
|
|
"rate_func": double_smooth,
|
2018-04-06 13:08:57 -07:00
|
|
|
"stroke_width": 2,
|
|
|
|
"stroke_color": None,
|
2019-02-08 16:53:37 -08:00
|
|
|
"draw_border_animation_config": {},
|
|
|
|
"fill_animation_config": {},
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def __init__(self, vmobject: VMobject, **kwargs):
|
2021-01-13 11:55:45 -10:00
|
|
|
assert(isinstance(vmobject, VMobject))
|
|
|
|
self.sm_to_index = dict([
|
|
|
|
(hash(sm), 0)
|
|
|
|
for sm in vmobject.get_family()
|
|
|
|
])
|
2019-02-09 10:56:51 -08:00
|
|
|
super().__init__(vmobject, **kwargs)
|
2019-02-08 16:53:37 -08:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def begin(self) -> None:
|
2021-02-02 16:44:04 -08:00
|
|
|
# Trigger triangulation calculation
|
|
|
|
for submob in self.mobject.get_family():
|
|
|
|
submob.get_triangulation()
|
|
|
|
|
2019-02-09 08:59:02 -08:00
|
|
|
self.outline = self.get_outline()
|
2019-02-08 20:19:16 -08:00
|
|
|
super().begin()
|
2021-01-13 11:55:45 -10:00
|
|
|
self.mobject.match_style(self.outline)
|
|
|
|
self.mobject.lock_matching_data(self.mobject, self.outline)
|
2019-02-08 20:19:16 -08:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def finish(self) -> None:
|
2021-02-03 14:17:55 -08:00
|
|
|
super().finish()
|
|
|
|
self.mobject.unlock_data()
|
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def get_outline(self) -> VMobject:
|
2019-02-09 08:59:02 -08:00
|
|
|
outline = self.mobject.copy()
|
|
|
|
outline.set_fill(opacity=0)
|
2021-01-13 00:07:58 -10:00
|
|
|
for sm in outline.get_family():
|
2019-03-24 11:32:12 -07:00
|
|
|
sm.set_stroke(
|
|
|
|
color=self.get_stroke_color(sm),
|
2021-01-13 00:07:58 -10:00
|
|
|
width=float(self.stroke_width)
|
2019-03-24 11:32:12 -07:00
|
|
|
)
|
2019-02-09 08:59:02 -08:00
|
|
|
return outline
|
2019-02-08 16:53:37 -08:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def get_stroke_color(self, vmobject: VMobject) -> str:
|
2019-02-08 16:53:37 -08:00
|
|
|
if self.stroke_color:
|
|
|
|
return self.stroke_color
|
|
|
|
elif vmobject.get_stroke_width() > 0:
|
|
|
|
return vmobject.get_stroke_color()
|
|
|
|
return vmobject.get_color()
|
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def get_all_mobjects(self) -> list[VMobject]:
|
2019-02-09 08:59:02 -08:00
|
|
|
return [*super().get_all_mobjects(), self.outline]
|
2019-02-08 16:53:37 -08:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def interpolate_submobject(
|
|
|
|
self,
|
|
|
|
submob: VMobject,
|
|
|
|
start: VMobject,
|
|
|
|
outline: VMobject,
|
|
|
|
alpha: float
|
|
|
|
) -> None:
|
2019-02-09 08:59:02 -08:00
|
|
|
index, subalpha = integer_interpolate(0, 2, alpha)
|
2021-01-13 11:55:45 -10:00
|
|
|
|
|
|
|
if index == 1 and self.sm_to_index[hash(submob)] == 0:
|
|
|
|
# First time crossing over
|
|
|
|
submob.set_data(outline.data)
|
|
|
|
submob.unlock_data()
|
2021-02-10 16:45:23 -08:00
|
|
|
if not self.mobject.has_updaters:
|
|
|
|
submob.lock_matching_data(submob, start)
|
2021-02-02 16:44:04 -08:00
|
|
|
submob.needs_new_triangulation = False
|
2021-01-13 11:55:45 -10:00
|
|
|
self.sm_to_index[hash(submob)] = 1
|
|
|
|
|
2019-02-09 08:59:02 -08:00
|
|
|
if index == 0:
|
2020-02-18 22:40:12 -08:00
|
|
|
submob.pointwise_become_partial(outline, 0, subalpha)
|
2019-02-09 08:59:02 -08:00
|
|
|
else:
|
|
|
|
submob.interpolate(outline, start, subalpha)
|
2018-04-06 13:08:57 -07:00
|
|
|
|
2018-08-12 12:47:16 -07:00
|
|
|
|
2019-02-09 08:59:02 -08:00
|
|
|
class Write(DrawBorderThenFill):
|
2018-08-12 12:47:16 -07:00
|
|
|
CONFIG = {
|
2019-02-08 20:19:16 -08:00
|
|
|
# To be figured out in
|
|
|
|
# set_default_config_from_lengths
|
|
|
|
"run_time": None,
|
|
|
|
"lag_ratio": None,
|
2019-02-09 08:59:02 -08:00
|
|
|
"rate_func": linear,
|
2018-08-12 12:47:16 -07:00
|
|
|
}
|
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def __init__(self, vmobject: VMobject, **kwargs):
|
2018-08-12 12:47:16 -07:00
|
|
|
digest_config(self, kwargs)
|
2022-02-15 18:39:45 +08:00
|
|
|
self.set_default_config_from_length(vmobject)
|
|
|
|
super().__init__(vmobject, **kwargs)
|
2019-02-08 20:19:16 -08:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def set_default_config_from_length(self, vmobject: VMobject) -> None:
|
|
|
|
length = len(vmobject.family_members_with_points())
|
2019-02-08 20:19:16 -08:00
|
|
|
if self.run_time is None:
|
|
|
|
if length < 15:
|
|
|
|
self.run_time = 1
|
|
|
|
else:
|
|
|
|
self.run_time = 2
|
|
|
|
if self.lag_ratio is None:
|
2022-02-13 20:04:05 -08:00
|
|
|
self.lag_ratio = min(4.0 / (length + 1.0), 0.2)
|
2019-02-08 20:19:16 -08:00
|
|
|
|
2019-01-17 14:09:15 -08:00
|
|
|
|
|
|
|
class ShowIncreasingSubsets(Animation):
|
2019-02-09 10:56:51 -08:00
|
|
|
CONFIG = {
|
|
|
|
"suspend_mobject_updating": False,
|
2020-06-22 15:21:25 -07:00
|
|
|
"int_func": np.round,
|
2019-02-09 10:56:51 -08:00
|
|
|
}
|
|
|
|
|
2022-04-23 17:17:43 +08:00
|
|
|
def __init__(self, group: Mobject, **kwargs):
|
2019-02-09 09:08:57 -08:00
|
|
|
self.all_submobs = list(group.submobjects)
|
2019-02-09 10:56:51 -08:00
|
|
|
super().__init__(group, **kwargs)
|
2019-01-17 14:09:15 -08:00
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def interpolate_mobject(self, alpha: float) -> None:
|
2019-01-17 14:09:15 -08:00
|
|
|
n_submobs = len(self.all_submobs)
|
2019-06-13 09:25:14 -07:00
|
|
|
index = int(self.int_func(alpha * n_submobs))
|
2019-09-19 13:38:03 -07:00
|
|
|
self.update_submobject_list(index)
|
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def update_submobject_list(self, index: int) -> None:
|
2020-02-21 10:56:40 -08:00
|
|
|
self.mobject.set_submobjects(self.all_submobs[:index])
|
2019-09-19 13:38:03 -07:00
|
|
|
|
|
|
|
|
|
|
|
class ShowSubmobjectsOneByOne(ShowIncreasingSubsets):
|
2019-12-19 19:42:28 -08:00
|
|
|
CONFIG = {
|
|
|
|
"int_func": np.ceil,
|
|
|
|
}
|
|
|
|
|
2022-02-15 18:39:45 +08:00
|
|
|
def update_submobject_list(self, index: int) -> None:
|
2019-09-19 13:38:03 -07:00
|
|
|
# N = len(self.all_submobs)
|
|
|
|
if index == 0:
|
2020-02-21 10:56:40 -08:00
|
|
|
self.mobject.set_submobjects([])
|
2019-09-19 13:38:03 -07:00
|
|
|
else:
|
2021-11-22 08:05:59 -08:00
|
|
|
self.mobject.set_submobjects([self.all_submobs[index - 1]])
|
2019-10-03 13:58:00 -07:00
|
|
|
|
|
|
|
|
2022-03-31 10:36:14 +08:00
|
|
|
class AddTextWordByWord(ShowIncreasingSubsets):
|
2019-10-03 13:58:00 -07:00
|
|
|
CONFIG = {
|
|
|
|
# If given a value for run_time, it will
|
2022-03-31 10:36:14 +08:00
|
|
|
# override the time_per_word
|
2019-10-03 13:58:00 -07:00
|
|
|
"run_time": None,
|
2022-03-31 10:36:14 +08:00
|
|
|
"time_per_word": 0.2,
|
|
|
|
"rate_func": linear,
|
2019-10-03 13:58:00 -07:00
|
|
|
}
|
|
|
|
|
2022-03-31 10:36:14 +08:00
|
|
|
def __init__(self, string_mobject, **kwargs):
|
|
|
|
assert isinstance(string_mobject, LabelledString)
|
2022-05-03 23:39:37 +08:00
|
|
|
grouped_mobject = string_mobject.build_parts_from_indices_lists([
|
|
|
|
indices_list
|
|
|
|
for _, indices_list in string_mobject.get_group_part_items()
|
2022-04-23 17:17:43 +08:00
|
|
|
])
|
2019-10-03 13:58:00 -07:00
|
|
|
digest_config(self, kwargs)
|
2022-03-31 10:36:14 +08:00
|
|
|
if self.run_time is None:
|
|
|
|
self.run_time = self.time_per_word * len(grouped_mobject)
|
|
|
|
super().__init__(grouped_mobject, **kwargs)
|