2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.constants import *
|
|
|
|
from manimlib.mobject.types.vectorized_mobject import VMobject
|
|
|
|
from manimlib.utils.config_ops import digest_config
|
2020-02-12 12:49:46 -08:00
|
|
|
from manimlib.utils.space_ops import get_norm
|
2015-10-27 21:00:50 -07:00
|
|
|
|
|
|
|
|
2020-06-06 11:03:23 -07:00
|
|
|
class ParametricCurve(VMobject):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2020-06-06 11:43:59 -07:00
|
|
|
"t_range": [0, 1, 0.1],
|
2020-06-06 13:22:48 -07:00
|
|
|
"min_samples": 10,
|
2020-06-06 11:43:59 -07:00
|
|
|
"epsilon": 1e-8,
|
2020-02-11 19:55:00 -08:00
|
|
|
# TODO, automatically figure out discontinuities
|
2019-02-06 15:18:11 -08:00
|
|
|
"discontinuities": [],
|
2015-10-27 21:00:50 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2020-06-06 11:43:59 -07:00
|
|
|
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
|
2016-04-19 00:20:19 -07:00
|
|
|
VMobject.__init__(self, **kwargs)
|
2015-10-27 21:00:50 -07:00
|
|
|
|
2019-02-06 15:18:11 -08:00
|
|
|
def get_point_from_function(self, t):
|
2020-06-06 11:43:59 -07:00
|
|
|
return self.t_func(t)
|
2019-02-06 15:18:11 -08:00
|
|
|
|
2020-02-11 19:55:00 -08:00
|
|
|
def init_points(self):
|
2020-06-06 11:43:59 -07:00
|
|
|
# TODO, this seems like a mess.
|
|
|
|
t_min, t_max, step = self.t_range
|
|
|
|
epsilon = self.epsilon
|
2019-02-06 15:18:11 -08:00
|
|
|
|
|
|
|
discontinuities = filter(
|
|
|
|
lambda t: t_min <= t <= t_max,
|
|
|
|
self.discontinuities
|
2018-01-18 16:46:38 -08:00
|
|
|
)
|
2019-02-06 15:18:11 -08:00
|
|
|
discontinuities = np.array(list(discontinuities))
|
|
|
|
boundary_times = [
|
2020-06-06 11:43:59 -07:00
|
|
|
t_min, t_max,
|
|
|
|
*(discontinuities - epsilon),
|
|
|
|
*(discontinuities + epsilon),
|
2019-02-06 15:18:11 -08:00
|
|
|
]
|
|
|
|
boundary_times.sort()
|
|
|
|
for t1, t2 in zip(boundary_times[0::2], boundary_times[1::2]):
|
2020-02-12 12:49:46 -08:00
|
|
|
# Get an initial sample of points
|
|
|
|
t_range = list(np.linspace(t1, t2, self.min_samples + 1))
|
2020-06-06 11:43:59 -07:00
|
|
|
samples = np.array([self.t_func(t) for t in t_range])
|
2020-02-12 12:49:46 -08:00
|
|
|
|
|
|
|
# Take more samples based on the distances between them
|
|
|
|
norms = [get_norm(p2 - p1) for p1, p2 in zip(samples, samples[1:])]
|
|
|
|
full_t_range = [t1]
|
|
|
|
for s1, s2, norm in zip(t_range, t_range[1:], norms):
|
2020-06-06 11:43:59 -07:00
|
|
|
n_inserts = int(norm / step)
|
2020-02-12 12:49:46 -08:00
|
|
|
full_t_range += list(np.linspace(s1, s2, n_inserts + 1)[1:])
|
|
|
|
|
2020-06-06 11:43:59 -07:00
|
|
|
points = np.array([self.t_func(t) for t in full_t_range])
|
2020-02-19 23:43:33 -08:00
|
|
|
valid_indices = np.isfinite(points).all(1)
|
2019-02-06 15:18:11 -08:00
|
|
|
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
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2015-10-27 21:00:50 -07:00
|
|
|
|
2020-06-06 11:03:23 -07:00
|
|
|
class FunctionGraph(ParametricCurve):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"color": YELLOW,
|
2020-06-09 12:34:43 -07:00
|
|
|
"x_range": [-8, 8, 0.5],
|
2015-10-27 21:00:50 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2020-06-06 11:43:59 -07:00
|
|
|
def __init__(self, function, x_range=None, **kwargs):
|
2018-01-18 16:46:38 -08:00
|
|
|
digest_config(self, kwargs)
|
2015-10-28 16:03:33 -07:00
|
|
|
self.function = function
|
2015-10-27 21:00:50 -07:00
|
|
|
|
2020-06-06 11:43:59 -07:00
|
|
|
if x_range is not None:
|
|
|
|
self.x_range[:len(x_range)] = x_range
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-01-18 16:46:38 -08:00
|
|
|
def get_function(self):
|
|
|
|
return self.function
|
2019-02-06 15:18:11 -08:00
|
|
|
|
|
|
|
def get_point_from_function(self, x):
|
2020-06-06 11:43:59 -07:00
|
|
|
return self.t_func(x)
|