Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

typer bezier.py fading.py half type rate_function.py

Syrus Darke9c2e2956c76
diff --git a/manim/animation/fading.py b/manim/animation/fading.py
index 66fbb1d4f..1436cef8e 100644
--- a/manim/animation/fading.py
+++ b/manim/animation/fading.py
@@ -15,17 +15,23 @@ __all__ = [
"VFadeInThenOut",
]
+import typing
+import numpy as np
+
+from .. import logger
from ..animation.animation import Animation
from ..animation.transform import Transform
from ..constants import DOWN
from ..mobject.types.vectorized_mobject import VMobject
from ..utils.bezier import interpolate
from ..utils.rate_functions import there_and_back
-from .. import logger
+if typing.TYPE_CHECKING:
+ from ..mobject.mobject import Mobject
+ from ..scene.scene import Scene
-DEFAULT_FADE_LAG_RATIO = 0
+DEFAULT_FADE_LAG_RATIO: float = 0
class FadeOut(Transform):
@@ -55,26 +61,32 @@ class FadeOut(Transform):
"""
def __init__(
- self, vmobject, remover=True, lag_ratio=DEFAULT_FADE_LAG_RATIO, **kwargs
- ):
+ self,
+ vmobject: VMobject,
+ remover: bool = True,
+ lag_ratio: float = DEFAULT_FADE_LAG_RATIO,
+ **kwargs
+ ) -> None:
super().__init__(vmobject, remover=remover, lag_ratio=lag_ratio, **kwargs)
- def create_target(self):
+ def create_target(self) -> "Mobject":
return self.mobject.copy().fade(1)
- def clean_up_from_scene(self, scene=None):
+ def clean_up_from_scene(self, scene: "Scene" = None) -> None:
super().clean_up_from_scene(scene)
self.interpolate(0)
class FadeIn(Transform):
- def __init__(self, vmobject, lag_ratio=DEFAULT_FADE_LAG_RATIO, **kwargs):
+ def __init__(
+ self, vmobject: VMobject, lag_ratio: float = DEFAULT_FADE_LAG_RATIO, **kwargs
+ ) -> None:
super().__init__(vmobject, lag_ratio=lag_ratio, **kwargs)
- def create_target(self):
+ def create_target(self) -> "Mobject":
return self.mobject
- def create_starting_mobject(self):
+ def create_starting_mobject(self) -> "Mobject":
start = super().create_starting_mobject()
start.fade(1)
if isinstance(start, VMobject):
@@ -84,14 +96,16 @@ class FadeIn(Transform):
class FadeInFrom(Transform):
- def __init__(self, mobject, direction=DOWN, **kwargs):
+ def __init__(
+ self, mobject: "Mobject", direction: np.ndarray = DOWN, **kwargs
+ ) -> None:
self.direction = direction
super().__init__(mobject, **kwargs)
- def create_target(self):
+ def create_target(self) -> "Mobject":
return self.mobject.copy()
- def begin(self):
+ def begin(self) -> None:
super().begin()
self.starting_mobject.shift(self.direction)
self.starting_mobject.fade(1)
@@ -103,7 +117,9 @@ class FadeInFromDown(FadeInFrom):
communicates the default
"""
- def __init__(self, mobject, direction=DOWN, **kwargs):
+ def __init__(
+ self, mobject: "Mobject", direction: np.ndarray = DOWN, **kwargs
+ ) -> None:
super().__init__(mobject, direction=direction, **kwargs)
logger.warning(
"FadeInFromDown is deprecated and will eventually disappear. Please use FadeInFrom(<mobject>, direction=DOWN, <other_args>) instead."
@@ -111,11 +127,13 @@ class FadeInFromDown(FadeInFrom):
class FadeOutAndShift(FadeOut):
- def __init__(self, mobject, direction=DOWN, **kwargs):
+ def __init__(
+ self, mobject: "Mobject", direction: np.ndarray = DOWN, **kwargs
+ ) -> None:
self.direction = direction
super().__init__(mobject, **kwargs)
- def create_target(self):
+ def create_target(self) -> "Mobject":
target = super().create_target()
target.shift(self.direction)
return target
@@ -127,7 +145,9 @@ class FadeOutAndShiftDown(FadeOutAndShift):
communicates the default
"""
- def __init__(self, mobject, direction=DOWN, **kwargs):
+ def __init__(
+ self, mobject: "Mobject", direction: np.ndarray = DOWN, **kwargs
+ ) -> None:
super().__init__(mobject, direction=direction, **kwargs)
logger.warning(
"FadeOutAndShiftDown is deprecated and will eventually disappear. Please use FadeOutAndShift(<mobject>, direction=DOWN, <other_args>) instead."
@@ -135,11 +155,13 @@ class FadeOutAndShiftDown(FadeOutAndShift):
class FadeInFromPoint(FadeIn):
- def __init__(self, mobject, point, **kwargs):
+ def __init__(
+ self, mobject: "Mobject", point: np.ndarray, **kwargs
+ ) -> None: # is point typed correctly?
self.point = point
super().__init__(mobject, **kwargs)
- def create_starting_mobject(self):
+ def create_starting_mobject(self) -> "Mobject":
start = super().create_starting_mobject()
start.scale(0)
start.move_to(self.point)
@@ -147,11 +169,11 @@ class FadeInFromPoint(FadeIn):
class FadeInFromLarge(FadeIn):
- def __init__(self, mobject, scale_factor=2, **kwargs):
+ def __init__(self, mobject: "Mobject", scale_factor: float = 2, **kwargs) -> None:
self.scale_factor = scale_factor
super().__init__(mobject, **kwargs)
- def create_starting_mobject(self):
+ def create_starting_mobject(self) -> "Mobject":
start = super().create_starting_mobject()
start.scale(self.scale_factor)
return start
@@ -162,24 +184,40 @@ class VFadeIn(Animation):
VFadeIn and VFadeOut only work for VMobjects,
"""
- def __init__(self, mobject, suspend_mobject_updating=False, **kwargs):
+ def __init__(
+ self, mobject: "Mobject", suspend_mobject_updating: bool = False, **kwargs
+ ) -> None:
super().__init__(
mobject, suspend_mobject_updating=suspend_mobject_updating, **kwargs
)
- def interpolate_submobject(self, submob, start, alpha):
- submob.set_stroke(opacity=interpolate(0, start.get_stroke_opacity(), alpha))
- submob.set_fill(opacity=interpolate(0, start.get_fill_opacity(), alpha))
+ def interpolate_submobject(
+ self, submobject: "Mobject", starting_sumobject: "Mobject", alpha: float
+ ) -> None:
+ submobject.set_stroke(
+ opacity=interpolate(0, starting_sumobject.get_stroke_opacity(), alpha)
+ )
+ submobject.set_fill(
+ opacity=interpolate(0, starting_sumobject.get_fill_opacity(), alpha)
+ )
class VFadeOut(VFadeIn):
- def __init__(self, mobject, remover=True, **kwargs):
+ def __init__(self, mobject: "Mobject", remover: bool = True, **kwargs) -> None:
super().__init__(mobject, remover=remover, **kwargs)
- def interpolate_submobject(self, submob, start, alpha):
- super().interpolate_submobject(submob, start, 1 - alpha)
+ def interpolate_submobject(
+ self, submobject: "Mobject", starting_sumobject: "Mobject", alpha: float
+ ) -> None:
+ super().interpolate_submobject(submobject, starting_sumobject, 1 - alpha)
class VFadeInThenOut(VFadeIn):
- def __init__(self, mobject, remover=True, rate_func=there_and_back, **kwargs):
+ def __init__(
+ self,
+ mobject: "Mobject",
+ remover: bool = True,
+ rate_func: typing.Callable[[float, float], float] = there_and_back,
+ **kwargs
+ ):
super().__init__(mobject, remover=remover, rate_func=rate_func, **kwargs)
diff --git a/manim/utils/bezier.py b/manim/utils/bezier.py
index fc8bc07c6..c2aff9854 100644
--- a/manim/utils/bezier.py
+++ b/manim/utils/bezier.py
@@ -14,15 +14,19 @@ __all__ = [
]
-from scipy import linalg
+import typing
+
import numpy as np
-from typing import Union
+from scipy import linalg
+
from ..utils.simple_functions import choose
-CLOSED_THRESHOLD = 0.001
+CLOSED_THRESHOLD: float = 0.001
-def bezier(points):
+def bezier(
+ points: np.ndarray,
+) -> typing.Callable[[float], typing.Union[int, typing.Iterable]]:
n = len(points) - 1
return lambda t: sum(
[
@@ -32,7 +36,7 @@ def bezier(points):
)
-def partial_bezier_points(points, a, b):
+def partial_bezier_points(points: np.ndarray, a: float, b: float) -> np.ndarray:
"""
Given an array of points which define
a bezier curve, and two numbers 0<=a<b<=1,
@@ -57,7 +61,7 @@ def interpolate(start: int, end: int, alpha: float) -> float:
return (1 - alpha) * start + alpha * end
-def integer_interpolate(start, end, alpha):
+def integer_interpolate(start: float, end: float, alpha: float) -> int:
"""
alpha is a float between 0 and 1. This returns
an integer between start and end (inclusive) representing
@@ -78,15 +82,17 @@ def integer_interpolate(start, end, alpha):
return (value, residue)
-def mid(start, end):
+def mid(start: float, end: float) -> float:
return (start + end) / 2.0
-def inverse_interpolate(start, end, value) -> np.ndarray:
+def inverse_interpolate(start: float, end: float, value: float) -> np.ndarray:
return np.true_divide(value - start, end - start)
-def match_interpolate(new_start, new_end, old_start, old_end, old_value):
+def match_interpolate(
+ new_start: float, new_end: float, old_start: float, old_end: float, old_value: float
+) -> np.ndarray:
return interpolate(
new_start, new_end, inverse_interpolate(old_start, old_end, old_value)
)
@@ -95,7 +101,9 @@ def match_interpolate(new_start, new_end, old_start, old_end, old_value):
# Figuring out which bezier curves most smoothly connect a sequence of points
-def get_smooth_handle_points(points):
+def get_smooth_handle_points(
+ points: np.ndarray,
+) -> typing.Tuple[np.ndarray, np.ndarray]:
points = np.array(points)
num_handles = len(points) - 1
dim = points.shape[1]
@@ -126,7 +134,7 @@ def get_smooth_handle_points(points):
b[0] = points[0]
b[-1] = points[-1]
- def solve_func(b):
+ def solve_func(b: np.ndarray) -> np.ndarray:
return linalg.solve_banded((l, u), diag, b)
use_closed_solve_function = is_closed(points)
@@ -141,7 +149,7 @@ def get_smooth_handle_points(points):
b[0] = 2 * points[0]
b[-1] = np.zeros(dim)
- def closed_curve_solve_func(b):
+ def closed_curve_solve_func(b: np.ndarray) -> np.ndarray:
return linalg.solve(matrix, b)
handle_pairs = np.zeros((2 * num_handles, dim))
@@ -153,7 +161,7 @@ def get_smooth_handle_points(points):
return handle_pairs[0::2], handle_pairs[1::2]
-def diag_to_matrix(l_and_u, diag):
+def diag_to_matrix(l_and_u: typing.Tuple[int, int], diag: np.ndarray) -> np.ndarray:
"""
Converts array whose rows represent diagonal
entries of a matrix into the matrix itself.
@@ -169,5 +177,5 @@ def diag_to_matrix(l_and_u, diag):
return matrix
-def is_closed(points):
+def is_closed(points: typing.Iterator[np.ndarray, np.ndarray]) -> bool:
return np.allclose(points[0], points[-1])
diff --git a/manim/utils/rate_functions.py b/manim/utils/rate_functions.py
index 60870b10d..22aece2d4 100644
--- a/manim/utils/rate_functions.py
+++ b/manim/utils/rate_functions.py
@@ -1,5 +1,6 @@
"""A selection of rate functions, i.e., *speed curves* for animations.
-Please find a standard list at https://easings.net/. Here is a picture for the non-standard ones
+Please find a standard list at https://easings.net/. Here is a picture
+for the non-standard ones
.. image:: /_static/non_standard_rate_funcs.png
:alt: Non-standard rate functions
@@ -64,6 +65,7 @@ __all__ = [
]
+import typing
from math import sqrt
import numpy as np
@@ -72,11 +74,11 @@ from ..utils.bezier import bezier
from ..utils.simple_functions import sigmoid
-def linear(t):
+def linear(t: typing.Union[np.ndarray, float]) -> typing.Union[np.ndarray, float]:
return t
-def smooth(t, inflection=10.0):
+def smooth(t: float, inflection: typing.Optional[float] = 10.0) -> np.ndarray:
error = sigmoid(-inflection / 2)
return np.clip(
(sigmoid(inflection * (t - 0.5)) - error) / (1 - 2 * error),
@@ -85,31 +87,31 @@ def smooth(t, inflection=10.0):
)
-def rush_into(t, inflection=10.0):
+def rush_into(t: float, inflection: float = 10.0) -> np.ndarray:
return 2 * smooth(t / 2.0, inflection)
-def rush_from(t, inflection=10.0):
+def rush_from(t: float, inflection: float = 10.0) -> np.ndarray:
return 2 * smooth(t / 2.0 + 0.5, inflection) - 1
-def slow_into(t):
+def slow_into(t: np.ndarray) -> np.ndarray:
return np.sqrt(1 - (1 - t) * (1 - t))
-def double_smooth(t):
+def double_smooth(t: float) -> np.ndarray:
if t < 0.5:
return 0.5 * smooth(2 * t)
else:
return 0.5 * (1 + smooth(2 * t - 1))
-def there_and_back(t, inflection=10.0):
+def there_and_back(t: float, inflection: float = 10.0) -> np.ndarray:
new_t = 2 * t if t < 0.5 else 2 * (1 - t)
return smooth(new_t, inflection)
-def there_and_back_with_pause(t, pause_ratio=1.0 / 3):
+def there_and_back_with_pause(t: float, pause_ratio: float = 1.0 / 3) -> np.ndarray:
a = 1.0 / pause_ratio
if t < 0.5 - pause_ratio / 2:
return smooth(a * t)
@@ -119,22 +121,31 @@ def there_and_back_with_pause(t, pause_ratio=1.0 / 3):
return smooth(a - a * t)
-def running_start(t, pull_factor=-0.5):
+def running_start(t: float, pull_factor: float = -0.5) -> typing.Iterable:
return bezier([0, 0, pull_factor, pull_factor, 1, 1, 1])(t)
-def not_quite_there(func=smooth, proportion=0.7):
+def not_quite_there(
+ func: typing.Callable[[float, typing.Optional[float]], np.ndarray] = smooth,
+ proportion: float = 0.7,
+) -> typing.Callable[[float], np.ndarray]:
def result(t):
return proportion * func(t)
return result
-def wiggle(t, wiggles=2):
+def wiggle(t: float, wiggles: float = 2) -> np.ndarray:
return there_and_back(t) * np.sin(wiggles * np.pi * t)
-def squish_rate_func(func, a=0.4, b=0.6):
+def squish_rate_func(
+ func: typing.Callable[
+ [float],
+ ],
+ a: float = 0.4,
+ b: float = 0.6,
+) -> typing.Callable[[float],]: # what is func return type?
def result(t):
if a == b:
return a
@@ -155,7 +166,7 @@ def squish_rate_func(func, a=0.4, b=0.6):
# "lingering", different from squish_rate_func's default params
-def lingering(t):
+def lingering(t: float) -> typing.Callable: # not sure of return type
return squish_rate_func(lambda t: t, 0, 0.8)(t)