mirror of
https://github.com/3b1b/manim.git
synced 2025-07-28 04:23:16 +08:00
Rewrote ParametricFunction to have less buggy interpolation
This commit is contained in:
@ -7,22 +7,54 @@ class ParametricFunction(VMobject):
|
||||
CONFIG = {
|
||||
"t_min": 0,
|
||||
"t_max": 1,
|
||||
"num_anchor_points": 100,
|
||||
# TODO, be smarter about choosing this number
|
||||
"step_size": 0.01,
|
||||
"dt": 1e-8,
|
||||
# TODO, be smar about figuring these out?
|
||||
"discontinuities": [],
|
||||
}
|
||||
|
||||
def __init__(self, function, **kwargs):
|
||||
self.function = function
|
||||
VMobject.__init__(self, **kwargs)
|
||||
|
||||
def get_function(self):
|
||||
return self.function
|
||||
|
||||
def get_point_from_function(self, t):
|
||||
return self.function(t)
|
||||
|
||||
def generate_points(self):
|
||||
n_points = 3 * self.num_anchor_points - 2
|
||||
self.points = np.zeros((n_points, self.dim))
|
||||
self.points[:, 0] = np.linspace(
|
||||
self.t_min, self.t_max, n_points
|
||||
t_min, t_max = self.t_min, self.t_max
|
||||
dt = self.dt
|
||||
step_size = self.step_size
|
||||
|
||||
discontinuities = filter(
|
||||
lambda t: t_min <= t <= t_max,
|
||||
self.discontinuities
|
||||
)
|
||||
# VMobject.apply_function takes care of preserving
|
||||
# desirable tangent line properties at anchor points
|
||||
self.apply_function(lambda p: self.function(p[0]))
|
||||
discontinuities = np.array(list(discontinuities))
|
||||
boundary_times = [
|
||||
self.t_min, self.t_max,
|
||||
*(discontinuities - dt),
|
||||
*(discontinuities + dt),
|
||||
]
|
||||
boundary_times.sort()
|
||||
print(boundary_times)
|
||||
for t1, t2 in zip(boundary_times[0::2], boundary_times[1::2]):
|
||||
t_range = list(np.arange(t1, t2, step_size))
|
||||
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:])
|
||||
self.make_smooth()
|
||||
return self
|
||||
|
||||
|
||||
class FunctionGraph(ParametricFunction):
|
||||
@ -34,12 +66,11 @@ class FunctionGraph(ParametricFunction):
|
||||
|
||||
def __init__(self, function, **kwargs):
|
||||
digest_config(self, kwargs)
|
||||
|
||||
def parametric_function(t):
|
||||
return t * RIGHT + function(t) * UP
|
||||
self.parametric_function = \
|
||||
lambda t: np.array([t, function(t), 0])
|
||||
ParametricFunction.__init__(
|
||||
self,
|
||||
parametric_function,
|
||||
self.parametric_function,
|
||||
t_min=self.x_min,
|
||||
t_max=self.x_max,
|
||||
**kwargs
|
||||
@ -48,3 +79,6 @@ class FunctionGraph(ParametricFunction):
|
||||
|
||||
def get_function(self):
|
||||
return self.function
|
||||
|
||||
def get_point_from_function(self, x):
|
||||
return self.parametric_function(x)
|
||||
|
Reference in New Issue
Block a user