The animation engine for agents: blazingly fast math and 3D videos with Manim's Python API, on a Rust GPU core.
Python
Implement animation clock and easing
Sina Atalaye826a22e19de
diff --git a/src/manimgx/animation/__init__.py b/src/manimgx/animation/__init__.pynew file mode 100644index 00000000..e69de29bdiff --git a/src/manimgx/animation/clock.py b/src/manimgx/animation/clock.pynew file mode 100644index 00000000..99bb1d92--- /dev/null+++ b/src/manimgx/animation/clock.py@@ -0,0 +1,75 @@+"""Scene time: one exact clock, and the clocks that run off it.++A frame shows the world at one instant (frame k at k/fps). An animation is evaluated at its own+window of scene time; a time-based updater gets the time its clock ran since it last stepped; a+per-frame updater re-establishes its relation. A flow (an updater whose steps compose) is exact+at any instant; any other time-based updater is simulated on the simulation clock+(`config.simulation_rate` ticks a second, plus the scene's events), so it behaves the same at+every frame rate. `speed` runs off scene time, warped by ChangeSpeed."""++from collections.abc import Callable+from fractions import Fraction+from itertools import pairwise++now = Fraction(0)+"""The instant being computed — or, between plays, the end of the last one (where the scene's+own code runs)."""+stepping: bool = True+"""Do simulated updaters (time-based, not flows) step at `now`? At every instant — unless the+scene is simulating: then at the ticks of its simulation clock and at its events only."""+framing: bool = True+"""Do per-frame updaters run at `now`? At frames and events — not at a tick between frames: an+updater that takes no time is called once a frame, whatever else the scene simulates."""+++class Clock:+ """Local time as a function of scene time: scene time, except over its warps (from `start`, for+ `duration`, covering `span` of local time along `progress`, a monotone map of [0, 1] onto+ itself). Where warps overlap, the one begun last rules while it lasts, so the clock only ever+ runs forward."""++ def __init__(self) -> None:+ self.warps: list[tuple[float, float, float, Callable[[float], float]]] = []++ def warp(+ self,+ start: float,+ duration: float,+ span: float,+ progress: Callable[[float], float],+ ) -> None:+ self.warps.append((start, duration, span, progress))+ self.warps.sort(key=lambda w: w[0])++ def at(self, t: float) -> float:+ # scene time cut at every warp's start and end; each piece runs at the pace of the+ # warp begun last among those covering it, or at scene time's own+ cuts = sorted({0.0, t, *(x for w in self.warps for x in (w[0], w[0] + w[1]))})+ local = 0.0+ for a, b in pairwise(cuts):+ if b > t:+ break+ ruling = None+ for w in self.warps: # sorted by start: the last one covering (a, b) rules+ if w[0] <= a and b <= w[0] + w[1]:+ ruling = w+ if ruling is None:+ local += b - a+ else:+ start, duration, span, progress = ruling+ if duration > 0:+ local += span * (+ progress((b - start) / duration)+ - progress((a - start) / duration)+ )+ return local+++speed = Clock()+++def reset() -> None:+ """A new scene: its clock at 0, no warps."""+ global now, stepping, framing+ now, stepping, framing = Fraction(0), True, True+ speed.warps.clear()diff --git a/src/manimgx/animation/easing.py b/src/manimgx/animation/easing.pynew file mode 100644index 00000000..4d960296--- /dev/null+++ b/src/manimgx/animation/easing.py@@ -0,0 +1,558 @@+# SPDX-FileCopyrightText: 2026 Academa, Inc.+# SPDX-FileCopyrightText: 2024 the Manim Community Developers+# SPDX-FileCopyrightText: 2018 3Blue1Brown LLC+# SPDX-License-Identifier: MIT++"""Rate functions, ported from Manim CE 0.21 `utils/rate_functions.py` (MIT)."""++import functools+import math+from collections.abc import Callable+from typing import Concatenate++import numpy as np++from manimgx.typing import RateFunc++__all__ = [+ "RateFunction",+ "double_smooth",+ "ease_in_back",+ "ease_in_bounce",+ "ease_in_circ",+ "ease_in_cubic",+ "ease_in_elastic",+ "ease_in_expo",+ "ease_in_out_back",+ "ease_in_out_bounce",+ "ease_in_out_circ",+ "ease_in_out_cubic",+ "ease_in_out_elastic",+ "ease_in_out_expo",+ "ease_in_out_quad",+ "ease_in_out_quart",+ "ease_in_out_quint",+ "ease_in_out_sine",+ "ease_in_quad",+ "ease_in_quart",+ "ease_in_quint",+ "ease_in_sine",+ "ease_out_back",+ "ease_out_bounce",+ "ease_out_circ",+ "ease_out_cubic",+ "ease_out_elastic",+ "ease_out_expo",+ "ease_out_quad",+ "ease_out_quart",+ "ease_out_quint",+ "ease_out_sine",+ "exponential_decay",+ "linear",+ "lingering",+ "not_quite_there",+ "running_start",+ "rush_from",+ "rush_into",+ "slow_into",+ "smooth",+ "smootherstep",+ "smoothstep",+ "squish_rate_func",+ "there_and_back",+ "there_and_back_with_pause",+ "unit_interval",+ "wiggle",+ "zero",+]++RateFunction = RateFunc+"""The type of a rate function: a function of the time an animation has run, from 0 to+1, giving how far along it is."""+++def unit_interval[**P](+ function: Callable[Concatenate[float, P], float],+) -> Callable[Concatenate[float, P], float]:+ """Make a rate function start exactly at 0 and end exactly at 1, and hold its ends+ outside [0, 1].++ Args:+ function: The rate function, as it runs from 0 to 1.++ Returns:+ The rate function: 0 up to 0, 1 from 1 on.+ """++ @functools.wraps(function)+ def wrapper(t: float, /, *args: P.args, **kwargs: P.kwargs) -> float:+ # its ends are exact, whatever rounding its formula does there: an animation lands+ if 0 < t < 1:+ return function(t, *args, **kwargs)+ return 0 if t <= 0 else 1++ return wrapper+++def zero[**P](+ function: Callable[Concatenate[float, P], float],+) -> Callable[Concatenate[float, P], float]:+ """Make a rate function that comes back to 0 start and end exactly at 0, and hold 0+ outside [0, 1].++ Args:+ function: The rate function, as it runs from 0 to 1.++ Returns:+ The rate function, 0 up to 0 and from 1 on.+ """++ @functools.wraps(function)+ def wrapper(t: float, /, *args: P.args, **kwargs: P.kwargs) -> float:+ return function(t, *args, **kwargs) if 0 < t < 1 else 0++ return wrapper+++@unit_interval+def linear(t: float) -> float:+ """Progress in step with time: t."""+ return t+++@unit_interval+def smooth(t: float, inflection: float = 10.0) -> float:+ """Slow, fast, slow: a sigmoid curve, stretched to run from 0 to 1.++ The default rate function of animations.++ Args:+ t: The time, from 0 to 1.+ inflection: How steep its middle is: higher starts and ends slower and runs+ faster between.++ Returns:+ The progress, from 0 to 1.+ """+ error = 1.0 / (1 + np.exp(inflection / 2)) # the logistic curve's value at t = 0+ logistic = 1.0 / (1 + np.exp(-inflection * (t - 0.5)))+ return min(max((logistic - error) / (1 - 2 * error), 0), 1)+++@unit_interval+def smoothstep(t: float) -> float:+ """3t² − 2t³: starts and ends at rest; gentler than [smooth][manimgx.smooth]."""+ return 3 * t**2 - 2 * t**3+++@unit_interval+def smootherstep(t: float) -> float:+ """6t⁵ − 15t⁴ + 10t³: starts and ends at rest, and without acceleration there."""+ return 6 * t**5 - 15 * t**4 + 10 * t**3+++@unit_interval+def rush_into(t: float, inflection: float = 10.0) -> float:+ """The first half of [smooth][manimgx.smooth], stretched over the whole time:+ starts at rest and ends at full speed.++ Args:+ t: The time, from 0 to 1.+ inflection: How steep [smooth][manimgx.smooth] is.++ Returns:+ The progress, from 0 to 1.+ """+ return 2 * smooth(t / 2.0, inflection)+++@unit_interval+def rush_from(t: float, inflection: float = 10.0) -> float:+ """The second half of [smooth][manimgx.smooth], stretched over the whole time:+ starts at full speed and comes to rest.++ Args:+ t: The time, from 0 to 1.+ inflection: How steep [smooth][manimgx.smooth] is.++ Returns:+ The progress, from 0 to 1.+ """+ return 2 * smooth(t / 2.0 + 0.5, inflection) - 1+++@unit_interval+def slow_into(t: float) -> float:+ """√(1 − (1 − t)²), a quarter circle: starts at once at full speed and slows to a+ stop."""+ return float(np.sqrt(1 - (1 - t) * (1 - t)))+++@unit_interval+def double_smooth(t: float) -> float:+ """Two [smooth][manimgx.smooth] moves, each half the way in half the time: comes to+ rest at the middle."""+ return 0.5 * smooth(2 * t) if t < 0.5 else 0.5 * (1 + smooth(2 * t - 1))+++@zero+def there_and_back(t: float, inflection: float = 10.0) -> float:+ """There and back: [smooth][manimgx.smooth] to the end by the middle, then back to+ the start.++ Args:+ t: The time, from 0 to 1.+ inflection: How steep [smooth][manimgx.smooth] is.++ Returns:+ The progress, 0 at the start and the end and 1 at the middle.+ """+ return smooth(2 * t if t < 0.5 else 2 * (1 - t), inflection)+++@zero+def there_and_back_with_pause(t: float, pause_ratio: float = 1.0 / 3) -> float:+ """There and back with a pause: [smooth][manimgx.smooth] to the end, a pause there,+ then back to the start.++ Args:+ t: The time, from 0 to 1.+ pause_ratio: The fraction of the time spent at the end, in the middle.++ Returns:+ The progress, 0 at the start and the end and 1 during the pause.+ """+ a = 2.0 / (1.0 - pause_ratio)+ if t < 0.5 - pause_ratio / 2:+ return smooth(a * t)+ if t < 0.5 + pause_ratio / 2:+ return 1+ return smooth(a - a * t)+++@unit_interval+def running_start(t: float, pull_factor: float = -0.5) -> float:+ """Backs up for a running start, then runs to the end: with the default pull, back+ to −0.18 at t ≈ 0.29.++ Args:+ t: The time, from 0 to 1.+ pull_factor: How far it pulls back: the third and fourth of its Bézier curve's+ seven control values (0, 0, pull, pull, 1, 1, 1).++ Returns:+ The progress, from 0 to 1, after dipping below 0.+ """+ mt = 1 - t+ return (+ 15 * t**2 * mt**4 * pull_factor+ + 20 * t**3 * mt**3 * pull_factor+ + 15 * t**4 * mt**2+ + 6 * t**5 * mt+ + t**6+ )+++@zero+def wiggle(t: float, wiggles: float = 2) -> float:+ """Swings to either side of the start and comes back: sin(`wiggles`·πt), swelling+ and fading as [there_and_back][manimgx.there_and_back] does.++ With the default 2 wiggles, it swings to 0.73 and then to −0.73.++ Args:+ t: The time, from 0 to 1.+ wiggles: How many swings, to one side or the other.++ Returns:+ The progress, 0 at the start and the end.+ """+ return there_and_back(t) * float(np.sin(wiggles * np.pi * t))+++def squish_rate_func(func: RateFunc, a: float = 0.4, b: float = 0.6) -> RateFunc:+ """Squeeze a rate function into part of the time: it runs between `a` and `b`, and+ holds its ends before and after.++ Args:+ func: The rate function.+ a: When it starts, from 0 to 1.+ b: When it ends, from `a` to 1; if it is `a`, the function steps from its start to+ its end at `a`.++ Returns:+ The squeezed rate function.+ """++ def result(t: float) -> float:+ if t < a:+ return func(0.0)+ return func(1.0) if t >= b else func((t - a) / (b - a))++ return result+++def not_quite_there(func: RateFunc = smooth, proportion: float = 0.7) -> RateFunc:+ """Scale a rate function so it goes only part of the way.++ Args:+ func: The rate function.+ proportion: How far it goes: its progress is scaled by this.++ Returns:+ The scaled rate function.+ """+ return lambda t: proportion * func(t)+++@unit_interval+def lingering(t: float) -> float:+ """Runs evenly to the end by 0.8 of the time, then lingers there."""+ return squish_rate_func(lambda x: x, 0, 0.8)(t)+++@unit_interval+def exponential_decay(t: float, half_life: float = 0.1) -> float:+ """1 − e^(−t / `half_life`), scaled to end at 1: shoots off, then closes in on the+ end, ever slower.++ It is about 1 − 1/e ≈ 0.63 at `half_life`.++ Args:+ t: The time, from 0 to 1.+ half_life: The time constant, as a fraction of the time.++ Returns:+ The progress, from 0 to 1.+ """+ return float(np.expm1(-t / half_life) / np.expm1(-1 / half_life))+++# ── the ease family, verbatim from CE ──+@unit_interval+def ease_in_sine(t: float) -> float:+ """1 − cos(πt/2): starts slow and speeds up."""+ val: float = 1 - np.cos(t * np.pi / 2)+ return val+++@unit_interval+def ease_out_sine(t: float) -> float:+ """sin(πt/2): starts fast and slows to a stop."""+ val: float = np.sin(t * np.pi / 2)+ return val+++@unit_interval+def ease_in_out_sine(t: float) -> float:+ """(1 − cos(πt))/2: slow, fast, slow."""+ val: float = -(np.cos(np.pi * t) - 1) / 2+ return val+++@unit_interval+def ease_in_quad(t: float) -> float:+ """t²: starts slow and speeds up."""+ return t * t+++@unit_interval+def ease_out_quad(t: float) -> float:+ """1 − (1 − t)²: starts fast and slows to a stop."""+ return 1 - (1 - t) * (1 - t)+++@unit_interval+def ease_in_out_quad(t: float) -> float:+ """[ease_in_quad][manimgx.ease_in_quad] up to the middle, then its mirror image:+ slow, fast, slow."""+ return 2 * t * t if t < 0.5 else 1 - pow(-2 * t + 2, 2) / 2+++@unit_interval+def ease_in_cubic(t: float) -> float:+ """t³: starts slow and speeds up."""+ return t * t * t+++@unit_interval+def ease_out_cubic(t: float) -> float:+ """1 − (1 − t)³: starts fast and slows to a stop."""+ return 1 - pow(1 - t, 3)+++@unit_interval+def ease_in_out_cubic(t: float) -> float:+ """[ease_in_cubic][manimgx.ease_in_cubic] up to the middle, then its mirror image:+ slow, fast, slow."""+ return 4 * t * t * t if t < 0.5 else 1 - pow(-2 * t + 2, 3) / 2+++@unit_interval+def ease_in_quart(t: float) -> float:+ """t⁴: starts slow and speeds up."""+ return t * t * t * t+++@unit_interval+def ease_out_quart(t: float) -> float:+ """1 − (1 − t)⁴: starts fast and slows to a stop."""+ return 1 - pow(1 - t, 4)+++@unit_interval+def ease_in_out_quart(t: float) -> float:+ """[ease_in_quart][manimgx.ease_in_quart] up to the middle, then its mirror image:+ slow, fast, slow."""+ return 8 * t * t * t * t if t < 0.5 else 1 - pow(-2 * t + 2, 4) / 2+++@unit_interval+def ease_in_quint(t: float) -> float:+ """t⁵: starts slow and speeds up."""+ return t * t * t * t * t+++@unit_interval+def ease_out_quint(t: float) -> float:+ """1 − (1 − t)⁵: starts fast and slows to a stop."""+ return 1 - pow(1 - t, 5)+++@unit_interval+def ease_in_out_quint(t: float) -> float:+ """[ease_in_quint][manimgx.ease_in_quint] up to the middle, then its mirror image:+ slow, fast, slow."""+ return 16 * t * t * t * t * t if t < 0.5 else 1 - pow(-2 * t + 2, 5) / 2+++# Penner's exponential eases, 2^(10t − 10), jump by 2^−10 at their ends: here the exponential+# runs from 0 at 0 to 1 at 1, (2^(10t) − 1) / 1023 (written out in each: the module's functions+# are its rate functions)+@unit_interval+def ease_in_expo(t: float) -> float:+ """(2^(10t) − 1) / 1023: barely moves at first, then shoots to the end."""+ return (pow(2, 10 * t) - 1) / 1023+++@unit_interval+def ease_out_expo(t: float) -> float:+ """[ease_in_expo][manimgx.ease_in_expo] turned around: shoots off, then creeps to+ the end."""+ return 1 - (pow(2, 10 - 10 * t) - 1) / 1023+++@unit_interval+def ease_in_out_expo(t: float) -> float:+ """[ease_in_expo][manimgx.ease_in_expo] up to the middle, then its mirror image."""+ if t < 0.5:+ return (pow(2, 20 * t) - 1) / 2046+ return 1 - (pow(2, 20 - 20 * t) - 1) / 2046+++@unit_interval+def ease_in_circ(t: float) -> float:+ """1 − √(1 − t²), a quarter circle: starts at rest and ends at full speed."""+ return 1 - math.sqrt(1 - pow(t, 2))+++@unit_interval+def ease_out_circ(t: float) -> float:+ """√(1 − (t − 1)²), a quarter circle: starts at full speed and comes to rest."""+ return math.sqrt(1 - pow(t - 1, 2))+++@unit_interval+def ease_in_out_circ(t: float) -> float:+ """[ease_in_circ][manimgx.ease_in_circ] up to the middle, then its mirror image."""+ return (+ (1 - math.sqrt(1 - pow(2 * t, 2))) / 2+ if t < 0.5+ else (math.sqrt(1 - pow(-2 * t + 2, 2)) + 1) / 2+ )+++@unit_interval+def ease_in_back(t: float) -> float:+ """Pulls back first, to −0.1, then speeds to the end."""+ c1 = 1.70158+ c3 = c1 + 1+ return c3 * t * t * t - c1 * t * t+++@unit_interval+def ease_out_back(t: float) -> float:+ """Overshoots the end, to 1.1, then settles back to it."""+ c1 = 1.70158+ c3 = c1 + 1+ return 1 + c3 * pow(t - 1, 3) + c1 * pow(t - 1, 2)+++@unit_interval+def ease_in_out_back(t: float) -> float:+ """Pulls back to −0.1, then overshoots to 1.1 and settles at the end."""+ c1 = 1.70158+ c2 = c1 * 1.525+ return (+ pow(2 * t, 2) * ((c2 + 1) * 2 * t - c2) / 2+ if t < 0.5+ else (pow(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2+ )+++@unit_interval+def ease_in_elastic(t: float) -> float:+ """Swings about the start, more and more widely (down to −0.37), then springs to the+ end: a sine under the envelope of [ease_in_expo][manimgx.ease_in_expo]."""+ return float(+ -(pow(2, 10 * t) - 1) / 1023 * np.sin((t * 10 - 10.75) * 2 * np.pi / 3)+ )+++@unit_interval+def ease_out_elastic(t: float) -> float:+ """Springs past the end (to 1.37), then swings about it, less and less widely, like+ a spring: [ease_in_elastic][manimgx.ease_in_elastic] turned around."""+ return 1 - ease_in_elastic(1 - t)+++@unit_interval+def ease_in_out_elastic(t: float) -> float:+ """Swings about the start, more and more widely (down to −0.12), springs across the+ middle, then swings about the end, less and less widely (up to 1.12)."""+ u = 2 * t if t < 0.5 else 2 - 2 * t # the half's own time, from its outer end+ half = -(pow(2, 10 * u) - 1) / 1023 * np.sin((10 * u - 11.125) * 2 * np.pi / 4.5)+ return float(half / 2 if t < 0.5 else 1 - half / 2)+++@unit_interval+def ease_in_bounce(t: float) -> float:+ """[ease_out_bounce][manimgx.ease_out_bounce] turned around: bounces off the start,+ higher each time (to 0.02, 0.06 and 0.25), then rises to the end."""+ return 1 - ease_out_bounce(1 - t)+++@unit_interval+def ease_out_bounce(t: float) -> float:+ """Falls to the end and bounces off it like a ball: reaches it at t ≈ 0.36, then+ bounces back to 0.75, 0.94 and 0.98."""+ n1 = 7.5625+ d1 = 2.75+ if t < 1 / d1:+ return n1 * t * t+ elif t < 2 / d1:+ return n1 * (t - 1.5 / d1) * (t - 1.5 / d1) + 0.75+ elif t < 2.5 / d1:+ return n1 * (t - 2.25 / d1) * (t - 2.25 / d1) + 0.9375+ else:+ return n1 * (t - 2.625 / d1) * (t - 2.625 / d1) + 0.984375+++@unit_interval+def ease_in_out_bounce(t: float) -> float:+ """[ease_in_bounce][manimgx.ease_in_bounce] up to the middle, then+ [ease_out_bounce][manimgx.ease_out_bounce]."""+ if t < 0.5:+ return (1 - ease_out_bounce(1 - 2 * t)) / 2+ else:+ return (1 + ease_out_bounce(2 * t - 1)) / 2diff --git a/tests/unit/animation/test_clock.py b/tests/unit/animation/test_clock.pynew file mode 100644index 00000000..0698ff6f--- /dev/null+++ b/tests/unit/animation/test_clock.py@@ -0,0 +1,79 @@+"""A clock's local time is scene time, warped where its warps say, and it only runs forward.++- With no warps it is scene time; over a warp it covers the warp's span, and outside every+ warp it runs at scene time's pace.+- However its warps overlap, it never runs backward and never jumps: where warps overlap, the+ one begun last rules while it lasts.+"""++import pytest+from hypothesis import given+from hypothesis import strategies as st++from manimgx.animation import easing as rf+from manimgx.animation.clock import Clock++# monotone maps of [0, 1] onto itself, of bounded slope (at most 10)+PROGRESSES = [rf.linear, rf.smooth, rf.rush_into, rf.rush_from, rf.ease_out_quad, rf.double_smooth,+ rf.ease_in_sine, rf.ease_out_sine, rf.ease_in_out_cubic, rf.ease_in_expo] # fmt: skip++Warp = tuple[float, float, float, rf.RateFunction]+warps = st.tuples(+ st.floats(0, 5), st.floats(0.05, 3), st.floats(0, 3), st.sampled_from(PROGRESSES)+)+times = st.floats(0, 12)+++def clock(*ws: Warp) -> Clock:+ c = Clock()+ for w in ws:+ c.warp(*w)+ return c+++@given(t=times)+def test_without_warps_it_is_scene_time(t: float) -> None:+ assert Clock().at(t) == t+++@given(w=warps, t=times)+def test_over_a_warp_it_covers_the_warps_span(w: Warp, t: float) -> None:+ start, duration, span, progress = w+ local = clock(w).at(t)+ if t <= start:+ assert local == pytest.approx(t)+ elif t >= start + duration:+ assert local == pytest.approx(t - duration + span)+ else:+ assert local == pytest.approx(start + span * progress((t - start) / duration))+++@given(ws=st.lists(warps, max_size=4), a=times, b=times)+def test_it_never_runs_backward(ws: list[Warp], a: float, b: float) -> None:+ a, b = sorted((a, b))+ c = clock(*ws)+ assert c.at(a) <= c.at(b) + 1e-12+++@given(ws=st.lists(warps, max_size=4), t=times)+def test_it_never_jumps(ws: list[Warp], t: float) -> None:+ c, h = clock(*ws), 1e-7+ pace = max([1.0, *(10 * span / duration for _, duration, span, _ in ws)])+ assert abs(c.at(t + h) - c.at(t)) <= pace * h + 1e-12+++@given(first=warps, second=warps, a=st.floats(0, 1), b=st.floats(0, 1))+def test_where_warps_overlap_the_one_begun_last_rules(+ first: Warp, second: Warp, a: float, b: float+) -> None:+ s1, d1, span1, p1 = first+ _, _, span2, p2 = second+ s2 = s1 + d1 / 4 # the second begins inside the first, and ends before it+ d2 = d1 / 2+ c = clock(first, (s2, d2, span2, p2))+ a, b = sorted((a, b))+ inside = c.at(s2 + b * d2) - c.at(s2 + a * d2)+ assert inside == pytest.approx(span2 * (p2(b) - p2(a)), abs=1e-9)+ # after it, the first rules again, at its own pace+ after = c.at(s1 + d1) - c.at(s2 + d2)+ assert after == pytest.approx(span1 * (p1(1.0) - p1(0.75)), abs=1e-9)diff --git a/tests/unit/animation/test_easing.py b/tests/unit/animation/test_easing.pynew file mode 100644index 00000000..747a9083--- /dev/null+++ b/tests/unit/animation/test_easing.py@@ -0,0 +1,136 @@+"""Rate functions map an animation's time, from 0 to 1, to its progress.++- Each starts exactly at 0 and ends exactly at 1 (the there-and-back family at 0), and holds+ its ends outside [0, 1]: an animation lands exactly where it is going.+- Each is continuous, the exponential and elastic eases at their ends too, and the easing+ family is monotone.+- An ease-out is its ease-in turned around, and an ease-in-out runs its ease-in at double+ speed up to the middle: the thirty formulas are one family.+- The extremes their docstrings give are their extremes.+- `squish_rate_func` runs a rate function in a window of the time and holds its ends outside it.+- `not_quite_there` scales a rate function to part of the way: seven tenths, unless told.+"""++from collections.abc import Callable++import numpy as np+import pytest+from hypothesis import assume, given+from hypothesis import strategies as st+from tests.strategies import MONOTONE, RATE_FUNCTIONS++from manimgx.animation import easing as rf++RETURNING = {rf.there_and_back, rf.there_and_back_with_pause, rf.wiggle}+KINDS = ("sine", "quad", "cubic", "quart", "quint", "expo", "circ", "back", "elastic")+MIRRORED = ("sine", "quad", "cubic", "quart", "quint", "expo", "circ")+by_name = pytest.mark.parametrize("f", RATE_FUNCTIONS, ids=lambda f: f.__name__)+grid = np.linspace(0, 1, 100_001)+++class TestEveryRateFunction:+ @by_name+ def test_it_starts_and_ends_exactly(self, f: Callable[[float], float]) -> None:+ assert f(0.0) == 0+ assert f(1.0) == (0 if f in RETURNING else 1)++ @by_name+ @given(beyond=st.floats(0, 1e6, exclude_min=True))+ def test_it_holds_its_ends_outside_the_unit_interval(+ self, f: Callable[[float], float], beyond: float+ ) -> None:+ assert f(-beyond) == f(0.0)+ assert f(1 + beyond) == f(1.0)++ @by_name+ @given(t=st.floats(0, 1 - 1e-10))+ def test_it_is_continuous(self, f: Callable[[float], float], t: float) -> None:+ # a quarter circle rises as √h at its vertical ends; the steepest of the rest+ # climbs 10 per unit of time (exponential_decay at 0)+ h = 1e-10+ assert abs(f(t + h) - f(t)) <= 2 * h**0.5 + 40 * h++[email protected]("f", MONOTONE, ids=lambda f: f.__name__)+def test_the_easing_family_never_goes_back(f: Callable[[float], float]) -> None:+ values = np.array([f(t) for t in grid[::10]])+ assert np.all(np.diff(values) >= -1e-15)++[email protected]("kind", KINDS)+@given(t=st.floats(0, 1))+def test_an_ease_out_is_its_ease_in_turned_around(kind: str, t: float) -> None:+ ease_in, ease_out = getattr(rf, f"ease_in_{kind}"), getattr(rf, f"ease_out_{kind}")+ assert ease_out(t) == pytest.approx(1 - ease_in(1 - t), abs=1e-12)++[email protected]("kind", MIRRORED)+@given(t=st.floats(0, 1))+def test_an_ease_in_out_runs_its_ease_in_at_double_speed(kind: str, t: float) -> None:+ ease_in, both = getattr(rf, f"ease_in_{kind}"), getattr(rf, f"ease_in_out_{kind}")+ expected = ease_in(2 * t) / 2 if t < 0.5 else 1 - ease_in(2 - 2 * t) / 2+ assert both(t) == pytest.approx(expected, abs=1e-12)+++ ("f", "lowest", "highest"),+ [+ (rf.running_start, -0.18, 1),+ (rf.wiggle, -0.73, 0.73),+ (rf.ease_in_back, -0.10, 1),+ (rf.ease_out_back, 0, 1.10),+ (rf.ease_in_out_back, -0.10, 1.10),+ (rf.ease_in_elastic, -0.37, 1),+ (rf.ease_out_elastic, 0, 1.37),+ (rf.ease_in_out_elastic, -0.12, 1.12),+ ],+ ids=lambda v: getattr(v, "__name__", ""),+)+def test_the_extremes_its_docstring_gives_are_its_extremes(+ f: Callable[[float], float], lowest: float, highest: float+) -> None:+ values = np.array([f(t) for t in grid])+ assert round(values.min(), 2) == lowest+ assert round(values.max(), 2) == highest+++class TestSquishRateFunc:+ @given(a=st.floats(0, 1), b=st.floats(0, 1), s=st.floats(0, 1))+ def test_it_runs_the_function_through_its_window(+ self, a: float, b: float, s: float+ ) -> None:+ a, b = min(a, b), max(a, b)+ assume(b - a > 1e-6)+ squished = rf.squish_rate_func(rf.smooth, a, b)+ assert squished(a + s * (b - a)) == pytest.approx(rf.smooth(s), abs=1e-9)++ @given(a=st.floats(0, 1), b=st.floats(0, 1), t=st.floats(-1, 2))+ def test_it_holds_its_ends_outside_its_window(+ self, a: float, b: float, t: float+ ) -> None:+ a, b = min(a, b), max(a, b)+ assume(t < a or t >= b)+ assert rf.squish_rate_func(rf.smooth, a, b)(t) == (0 if t < a else 1)++ def test_its_window_is_the_middle_fifth_unless_given(self) -> None:+ squished = rf.squish_rate_func(rf.linear)+ assert [squished(t) for t in (0.3, 0.4, 0.5, 0.6)] == pytest.approx(+ [0, 0, 0.5, 1]+ )++ def test_an_empty_window_is_a_step(self) -> None:+ squished = rf.squish_rate_func(rf.smooth, 0.3, 0.3)+ assert [squished(t) for t in (0.0, 0.29, 0.3, 0.8)] == [0, 0, 1, 1]+++@given(t=st.floats(0, 1), proportion=st.none() | st.floats(0, 1))+def test_not_quite_there_goes_part_of_the_way(+ t: float, proportion: float | None+) -> None:+ """Seven tenths of the way unless told."""+ scaled = (+ rf.not_quite_there(rf.linear)+ if proportion is None+ else rf.not_quite_there(rf.linear, proportion)+ )+ assert scaled(t) == (0.7 if proportion is None else proportion) * t