Reconfigured ParametricCurve

This commit is contained in:
Grant Sanderson
2020-06-06 11:43:59 -07:00
parent 7cec2e50d5
commit 58fe0c79d8
2 changed files with 41 additions and 45 deletions

View File

@ -84,16 +84,12 @@ class CoordinateSystem():
) )
return self.axis_labels return self.axis_labels
def get_graph(self, function, x_min=None, x_max=None, **kwargs): def get_graph(self, function, x_range=None, **kwargs):
if x_min is None: if x_range is None:
x_min = self.x_min x_range = self.x_range
if x_max is None:
x_max = self.x_max
graph = ParametricCurve( graph = ParametricCurve(
lambda t: self.coords_to_point(t, function(t)), lambda t: self.coords_to_point(t, function(t)),
t_min=x_min, t_range=x_range,
t_max=x_max,
**kwargs **kwargs
) )
graph.underlying_function = function graph.underlying_function = function
@ -102,9 +98,7 @@ class CoordinateSystem():
def get_parametric_curve(self, function, **kwargs): def get_parametric_curve(self, function, **kwargs):
dim = self.dimension dim = self.dimension
graph = ParametricCurve( graph = ParametricCurve(
lambda t: self.coords_to_point( lambda t: self.coords_to_point(*function(t)[:dim]),
*function(t)[:dim]
),
**kwargs **kwargs
) )
graph.underlying_function = function graph.underlying_function = function
@ -119,8 +113,8 @@ class CoordinateSystem():
graph.point_from_proportion(a) graph.point_from_proportion(a)
)[0], )[0],
target=x, target=x,
lower_bound=self.x_min, lower_bound=self.x_range[0],
upper_bound=self.x_max, upper_bound=self.x_range[1],
) )
if alpha is not None: if alpha is not None:
return graph.point_from_proportion(alpha) return graph.point_from_proportion(alpha)

View File

@ -6,29 +6,33 @@ from manimlib.utils.space_ops import get_norm
class ParametricCurve(VMobject): class ParametricCurve(VMobject):
CONFIG = { CONFIG = {
"t_min": 0, "t_range": [0, 1, 0.1],
"t_max": 1,
"step_size": 0.2,
"min_samples": 8, "min_samples": 8,
"dt": 1e-8, "epsilon": 1e-8,
# TODO, automatically figure out discontinuities # TODO, automatically figure out discontinuities
"discontinuities": [], "discontinuities": [],
} }
def __init__(self, function=None, **kwargs): def __init__(self, t_func, t_range=None, **kwargs):
# either get a function from __init__ or from CONFIG digest_config(self, kwargs)
self.function = function or self.function 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) VMobject.__init__(self, **kwargs)
def get_function(self):
return self.function
def get_point_from_function(self, t): def get_point_from_function(self, t):
return self.function(t) return self.t_func(t)
def init_points(self): def init_points(self):
t_min, t_max = self.t_min, self.t_max # TODO, this seems like a mess.
dt = self.dt t_min, t_max, step = self.t_range
epsilon = self.epsilon
discontinuities = filter( discontinuities = filter(
lambda t: t_min <= t <= t_max, lambda t: t_min <= t <= t_max,
@ -36,24 +40,24 @@ class ParametricCurve(VMobject):
) )
discontinuities = np.array(list(discontinuities)) discontinuities = np.array(list(discontinuities))
boundary_times = [ boundary_times = [
self.t_min, self.t_max, t_min, t_max,
*(discontinuities - dt), *(discontinuities - epsilon),
*(discontinuities + dt), *(discontinuities + epsilon),
] ]
boundary_times.sort() boundary_times.sort()
for t1, t2 in zip(boundary_times[0::2], boundary_times[1::2]): for t1, t2 in zip(boundary_times[0::2], boundary_times[1::2]):
# Get an initial sample of points # Get an initial sample of points
t_range = list(np.linspace(t1, t2, self.min_samples + 1)) t_range = list(np.linspace(t1, t2, self.min_samples + 1))
samples = [self.function(t) for t in t_range] samples = np.array([self.t_func(t) for t in t_range])
# Take more samples based on the distances between them # Take more samples based on the distances between them
norms = [get_norm(p2 - p1) for p1, p2 in zip(samples, samples[1:])] norms = [get_norm(p2 - p1) for p1, p2 in zip(samples, samples[1:])]
full_t_range = [t1] full_t_range = [t1]
for s1, s2, norm in zip(t_range, t_range[1:], norms): for s1, s2, norm in zip(t_range, t_range[1:], norms):
n_inserts = int(norm / self.step_size) n_inserts = int(norm / step)
full_t_range += list(np.linspace(s1, s2, n_inserts + 1)[1:]) full_t_range += list(np.linspace(s1, s2, n_inserts + 1)[1:])
points = np.array([self.function(t) for t in full_t_range]) points = np.array([self.t_func(t) for t in full_t_range])
valid_indices = np.isfinite(points).all(1) valid_indices = np.isfinite(points).all(1)
points = points[valid_indices] points = points[valid_indices]
if len(points) > 0: if len(points) > 0:
@ -66,25 +70,23 @@ class ParametricCurve(VMobject):
class FunctionGraph(ParametricCurve): class FunctionGraph(ParametricCurve):
CONFIG = { CONFIG = {
"color": YELLOW, "color": YELLOW,
"x_min": -FRAME_X_RADIUS, "x_range": [-8, 8, 0.1],
"x_max": FRAME_X_RADIUS,
} }
def __init__(self, function, **kwargs): def __init__(self, function, x_range=None, **kwargs):
digest_config(self, kwargs) digest_config(self, kwargs)
self.parametric_function = \
lambda t: np.array([t, function(t), 0])
ParametricCurve.__init__(
self,
self.parametric_function,
t_min=self.x_min,
t_max=self.x_max,
**kwargs
)
self.function = function 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, x_range, **kwargs)
def get_function(self): def get_function(self):
return self.function return self.function
def get_point_from_function(self, x): def get_point_from_function(self, x):
return self.parametric_function(x) return self.t_func(x)