mirror of
https://github.com/3b1b/manim.git
synced 2025-08-03 04:04:36 +08:00
Merge branch 'master' of https://github.com/techdude/manim
This commit is contained in:
@ -1,106 +1,72 @@
|
||||
from manimlib.constants import *
|
||||
from manimlib.mobject.types.vectorized_mobject import VMobject
|
||||
from manimlib.utils.config_ops import digest_config
|
||||
import math
|
||||
from manimlib.utils.space_ops import get_norm
|
||||
|
||||
|
||||
class ParametricFunction(VMobject):
|
||||
class ParametricCurve(VMobject):
|
||||
CONFIG = {
|
||||
"t_min": 0,
|
||||
"t_max": 1,
|
||||
"step_size": 0.01, # Use "auto" (lowercase) for automatic step size
|
||||
"dt": 1e-8,
|
||||
# TODO, be smarter about figuring these out?
|
||||
"t_range": [0, 1, 0.1],
|
||||
"min_samples": 10,
|
||||
"epsilon": 1e-8,
|
||||
# TODO, automatically figure out discontinuities
|
||||
"discontinuities": [],
|
||||
"smoothing": True,
|
||||
}
|
||||
|
||||
def __init__(self, function=None, **kwargs):
|
||||
# either get a function from __init__ or from CONFIG
|
||||
self.function = function or self.function
|
||||
def __init__(self, t_func, t_range=None, **kwargs):
|
||||
digest_config(self, kwargs)
|
||||
if t_range is not None:
|
||||
self.t_range[:len(t_range)] = t_range
|
||||
# To be backward compatible with all the scenes specifying t_min, t_max, step_size
|
||||
self.t_range = [
|
||||
kwargs.get("t_min", self.t_range[0]),
|
||||
kwargs.get("t_max", self.t_range[1]),
|
||||
kwargs.get("step_size", self.t_range[2]),
|
||||
]
|
||||
self.t_func = t_func
|
||||
VMobject.__init__(self, **kwargs)
|
||||
|
||||
def get_function(self):
|
||||
return self.function
|
||||
|
||||
def get_point_from_function(self, t):
|
||||
return self.function(t)
|
||||
return self.t_func(t)
|
||||
|
||||
def get_step_size(self, t=None):
|
||||
if self.step_size == "auto":
|
||||
"""
|
||||
for x between -1 to 1, return 0.01
|
||||
else, return log10(x) (rounded)
|
||||
e.g.: 10.5 -> 0.1 ; 1040 -> 10
|
||||
"""
|
||||
if t == 0:
|
||||
scale = 0
|
||||
else:
|
||||
scale = math.log10(abs(t))
|
||||
if scale < 0:
|
||||
scale = 0
|
||||
def init_points(self):
|
||||
t_min, t_max, step = self.t_range
|
||||
|
||||
scale = math.floor(scale)
|
||||
|
||||
scale -= 2
|
||||
return math.pow(10, scale)
|
||||
else:
|
||||
return self.step_size
|
||||
|
||||
def generate_points(self):
|
||||
t_min, t_max = self.t_min, self.t_max
|
||||
dt = self.dt
|
||||
|
||||
discontinuities = filter(
|
||||
lambda t: t_min <= t <= t_max,
|
||||
self.discontinuities
|
||||
)
|
||||
discontinuities = np.array(list(discontinuities))
|
||||
boundary_times = [
|
||||
self.t_min, self.t_max,
|
||||
*(discontinuities - dt),
|
||||
*(discontinuities + dt),
|
||||
]
|
||||
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]):
|
||||
t_range = list(np.arange(t1, t2, self.get_step_size(t1)))
|
||||
if t_range[-1] != t2:
|
||||
t_range.append(t2)
|
||||
points = np.array([self.function(t) for t in t_range])
|
||||
valid_indices = np.apply_along_axis(
|
||||
np.all, 1, np.isfinite(points)
|
||||
)
|
||||
points = points[valid_indices]
|
||||
if len(points) > 0:
|
||||
self.start_new_path(points[0])
|
||||
self.add_points_as_corners(points[1:])
|
||||
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:])
|
||||
if self.smoothing:
|
||||
self.make_smooth()
|
||||
return self
|
||||
|
||||
|
||||
class FunctionGraph(ParametricFunction):
|
||||
class FunctionGraph(ParametricCurve):
|
||||
CONFIG = {
|
||||
"color": YELLOW,
|
||||
"x_min": -FRAME_X_RADIUS,
|
||||
"x_max": FRAME_X_RADIUS,
|
||||
"x_range": [-8, 8, 0.25],
|
||||
}
|
||||
|
||||
def __init__(self, function, **kwargs):
|
||||
def __init__(self, function, x_range=None, **kwargs):
|
||||
digest_config(self, kwargs)
|
||||
self.parametric_function = \
|
||||
lambda t: np.array([t, function(t), 0])
|
||||
ParametricFunction.__init__(
|
||||
self,
|
||||
self.parametric_function,
|
||||
t_min=self.x_min,
|
||||
t_max=self.x_max,
|
||||
**kwargs
|
||||
)
|
||||
self.function = function
|
||||
|
||||
if x_range is not None:
|
||||
self.x_range[:len(x_range)] = x_range
|
||||
|
||||
def parametric_function(t):
|
||||
return [t, function(t), 0]
|
||||
|
||||
super().__init__(parametric_function, self.x_range, **kwargs)
|
||||
|
||||
def get_function(self):
|
||||
return self.function
|
||||
|
||||
def get_point_from_function(self, x):
|
||||
return self.parametric_function(x)
|
||||
return self.t_func(x)
|
||||
|
Reference in New Issue
Block a user