3b1b-manim/manimlib/mobject/functions.py

119 lines
3.7 KiB
Python
Raw Normal View History

from __future__ import annotations
2022-02-08 00:24:40 +08:00
from isosurfaces import plot_isoline
2022-04-23 10:16:23 -07:00
import numpy as np
2022-02-08 00:24:40 +08:00
2022-04-12 19:19:59 +08:00
from manimlib.constants import FRAME_X_RADIUS, FRAME_Y_RADIUS
from manimlib.constants import YELLOW
from manimlib.mobject.types.vectorized_mobject import VMobject
2022-04-12 19:19:59 +08:00
from typing import TYPE_CHECKING
if TYPE_CHECKING:
2022-12-15 16:47:03 -08:00
from typing import Callable, Sequence, Tuple
from manimlib.typing import ManimColor, Vect3
2022-04-12 19:19:59 +08:00
class ParametricCurve(VMobject):
def __init__(
self,
t_func: Callable[[float], Sequence[float] | Vect3],
2022-12-15 16:47:03 -08:00
t_range: Tuple[float, float, float] = (0, 1, 0.1),
epsilon: float = 1e-8,
# TODO, automatically figure out discontinuities
discontinuities: Sequence[float] = [],
use_smoothing: bool = True,
**kwargs
):
2020-06-06 11:43:59 -07:00
self.t_func = t_func
2022-12-15 16:47:03 -08:00
self.t_range = t_range
self.epsilon = epsilon
self.discontinuities = discontinuities
self.use_smoothing = use_smoothing
super().__init__(**kwargs)
def get_point_from_function(self, t: float) -> Vect3:
2022-12-15 16:47:03 -08:00
return np.array(self.t_func(t))
2019-04-27 18:39:22 +03:00
2020-02-11 19:55:00 -08:00
def init_points(self):
2020-06-06 11:43:59 -07:00
t_min, t_max, step = self.t_range
2019-04-27 18:39:22 +03:00
2020-06-09 16:57:44 -07:00
jumps = np.array(self.discontinuities)
jumps = jumps[(jumps > t_min) & (jumps < t_max)]
boundary_times = [t_min, t_max, *(jumps - self.epsilon), *(jumps + self.epsilon)]
boundary_times.sort()
for t1, t2 in zip(boundary_times[0::2], boundary_times[1::2]):
2020-06-09 16:57:44 -07:00
t_range = [*np.arange(t1, t2, step), t2]
points = np.array([self.t_func(t) for t in t_range])
self.start_new_path(points[0])
self.add_points_as_corners(points[1:])
2021-02-06 11:39:19 -08:00
if self.use_smoothing:
self.make_approximately_smooth()
if not self.has_points():
2022-12-15 16:47:03 -08:00
self.set_points(np.array([self.t_func(t_min)]))
return self
2022-03-06 13:54:42 +08:00
def get_t_func(self):
return self.t_func
def get_function(self):
if hasattr(self, "underlying_function"):
return self.underlying_function
if hasattr(self, "function"):
return self.function
def get_x_range(self):
if hasattr(self, "x_range"):
return self.x_range
2022-04-27 09:51:43 -07:00
class FunctionGraph(ParametricCurve):
def __init__(
self,
function: Callable[[float], float],
2022-12-15 16:47:03 -08:00
x_range: Tuple[float, float, float] = (-8, 8, 0.25),
color: ManimColor = YELLOW,
**kwargs
):
self.function = function
2022-12-15 16:47:03 -08:00
self.x_range = x_range
2020-06-06 11:43:59 -07:00
def parametric_function(t):
return [t, function(t), 0]
2020-06-09 12:34:43 -07:00
super().__init__(parametric_function, self.x_range, **kwargs)
2020-06-06 11:43:59 -07:00
2022-02-08 00:24:40 +08:00
class ImplicitFunction(VMobject):
def __init__(
self,
func: Callable[[float, float], float],
2022-12-15 16:47:03 -08:00
x_range: Tuple[float, float] = (-FRAME_X_RADIUS, FRAME_X_RADIUS),
y_range: Tuple[float, float] = (-FRAME_Y_RADIUS, FRAME_Y_RADIUS),
min_depth: int = 5,
max_quads: int = 1500,
use_smoothing: bool = True,
**kwargs
):
2022-02-08 00:24:40 +08:00
super().__init__(**kwargs)
p_min, p_max = (
2022-12-15 16:47:03 -08:00
np.array([x_range[0], y_range[0]]),
np.array([x_range[1], y_range[1]]),
2022-02-08 00:24:40 +08:00
)
curves = plot_isoline(
2022-12-15 16:47:03 -08:00
fn=lambda u: func(u[0], u[1]),
2022-02-08 00:24:40 +08:00
pmin=p_min,
pmax=p_max,
2022-12-15 16:47:03 -08:00
min_depth=min_depth,
max_quads=max_quads,
2022-02-08 00:24:40 +08:00
) # returns a list of lists of 2D points
curves = [
np.pad(curve, [(0, 0), (0, 1)]) for curve in curves if curve != []
] # add z coord as 0
for curve in curves:
self.start_new_path(curve[0])
self.add_points_as_corners(curve[1:])
2022-12-15 16:47:03 -08:00
if use_smoothing:
2022-02-08 00:24:40 +08:00
self.make_smooth()