2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.animation.animation import Animation
|
2019-02-05 15:39:58 -08:00
|
|
|
from manimlib.utils.rate_functions import linear
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class Homotopy(Animation):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"run_time": 3,
|
|
|
|
"apply_function_kwargs": {},
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
def __init__(self, homotopy, mobject, **kwargs):
|
|
|
|
"""
|
2019-02-09 15:12:35 -08:00
|
|
|
Homotopy is a function from
|
|
|
|
(x, y, z, t) to (x', y', z')
|
2018-03-31 15:11:35 -07:00
|
|
|
"""
|
2019-02-09 15:12:35 -08:00
|
|
|
self.homotopy = homotopy
|
|
|
|
super().__init__(mobject, **kwargs)
|
|
|
|
|
|
|
|
def function_at_time_t(self, t):
|
|
|
|
return lambda p: self.homotopy(*p, t)
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2019-02-08 12:00:51 -08:00
|
|
|
def interpolate_submobject(self, submob, start, alpha):
|
2021-01-12 07:27:32 -10:00
|
|
|
submob.match_points(start)
|
2018-03-31 15:11:35 -07:00
|
|
|
submob.apply_function(
|
|
|
|
self.function_at_time_t(alpha),
|
|
|
|
**self.apply_function_kwargs
|
|
|
|
)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class SmoothedVectorizedHomotopy(Homotopy):
|
2021-02-05 19:58:29 -08:00
|
|
|
CONFIG = {
|
|
|
|
"apply_function_kwargs": {"make_smooth": True},
|
|
|
|
}
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-31 18:57:21 -07:00
|
|
|
class ComplexHomotopy(Homotopy):
|
|
|
|
def __init__(self, complex_homotopy, mobject, **kwargs):
|
|
|
|
"""
|
2021-02-05 19:58:29 -08:00
|
|
|
Given a function form (z, t) -> w, where z and w
|
|
|
|
are complex numbers and t is time, this animates
|
|
|
|
the state over time
|
2018-03-31 18:57:21 -07:00
|
|
|
"""
|
2018-05-18 16:53:46 -07:00
|
|
|
def homotopy(x, y, z, t):
|
|
|
|
c = complex_homotopy(complex(x, y), t)
|
2018-03-31 18:57:21 -07:00
|
|
|
return (c.real, c.imag, z)
|
2021-02-05 19:58:29 -08:00
|
|
|
super().__init__(homotopy, mobject, **kwargs)
|
2018-03-31 18:57:21 -07:00
|
|
|
|
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class PhaseFlow(Animation):
|
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"virtual_time": 1,
|
2019-02-05 15:39:58 -08:00
|
|
|
"rate_func": linear,
|
2019-02-09 15:12:35 -08:00
|
|
|
"suspend_mobject_updating": False,
|
2018-03-31 15:11:35 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
def __init__(self, function, mobject, **kwargs):
|
2019-02-09 15:12:35 -08:00
|
|
|
self.function = function
|
|
|
|
super().__init__(mobject, **kwargs)
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2019-02-08 11:57:27 -08:00
|
|
|
def interpolate_mobject(self, alpha):
|
2018-03-31 15:11:35 -07:00
|
|
|
if hasattr(self, "last_alpha"):
|
2018-04-06 13:58:59 -07:00
|
|
|
dt = self.virtual_time * (alpha - self.last_alpha)
|
2018-03-31 15:11:35 -07:00
|
|
|
self.mobject.apply_function(
|
2018-04-06 13:58:59 -07:00
|
|
|
lambda p: p + dt * self.function(p)
|
2018-03-31 15:11:35 -07:00
|
|
|
)
|
|
|
|
self.last_alpha = alpha
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
class MoveAlongPath(Animation):
|
2019-02-09 15:12:35 -08:00
|
|
|
CONFIG = {
|
|
|
|
"suspend_mobject_updating": False,
|
|
|
|
}
|
|
|
|
|
2018-03-31 15:11:35 -07:00
|
|
|
def __init__(self, mobject, path, **kwargs):
|
2019-02-09 15:12:35 -08:00
|
|
|
self.path = path
|
|
|
|
super().__init__(mobject, **kwargs)
|
2018-03-31 15:11:35 -07:00
|
|
|
|
2019-02-08 11:57:27 -08:00
|
|
|
def interpolate_mobject(self, alpha):
|
2018-03-31 15:11:35 -07:00
|
|
|
point = self.path.point_from_proportion(alpha)
|
|
|
|
self.mobject.move_to(point)
|