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
|
2019-04-27 18:39:22 +03:00
|
|
|
import math
|
2015-10-27 21:00:50 -07:00
|
|
|
|
|
|
|
|
2018-01-18 16:46:38 -08:00
|
|
|
class ParametricFunction(VMobject):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"t_min": 0,
|
|
|
|
"t_max": 1,
|
2019-05-27 19:48:14 -07:00
|
|
|
"step_size": 0.01, # Use "auto" (lowercase) for automatic step size
|
2019-02-06 15:18:11 -08:00
|
|
|
"dt": 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
|
|
|
|
2019-04-27 18:39:22 +03:00
|
|
|
def __init__(self, function=None, **kwargs):
|
|
|
|
# either get a function from __init__ or from CONFIG
|
|
|
|
self.function = function or self.function
|
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_function(self):
|
|
|
|
return self.function
|
|
|
|
|
|
|
|
def get_point_from_function(self, t):
|
|
|
|
return self.function(t)
|
|
|
|
|
2019-04-27 18:39:22 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
scale = math.floor(scale)
|
|
|
|
|
|
|
|
scale -= 2
|
|
|
|
return math.pow(10, scale)
|
|
|
|
else:
|
|
|
|
return self.step_size
|
|
|
|
|
2020-02-11 19:55:00 -08:00
|
|
|
def init_points(self):
|
2019-02-06 15:18:11 -08:00
|
|
|
t_min, t_max = self.t_min, self.t_max
|
|
|
|
dt = self.dt
|
|
|
|
|
|
|
|
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 = [
|
|
|
|
self.t_min, self.t_max,
|
|
|
|
*(discontinuities - dt),
|
|
|
|
*(discontinuities + dt),
|
|
|
|
]
|
|
|
|
boundary_times.sort()
|
|
|
|
for t1, t2 in zip(boundary_times[0::2], boundary_times[1::2]):
|
2019-04-27 18:39:22 +03:00
|
|
|
t_range = list(np.arange(t1, t2, self.get_step_size(t1)))
|
2019-02-06 15:18:11 -08:00
|
|
|
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
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2015-10-27 21:00:50 -07:00
|
|
|
|
2018-01-18 16:46:38 -08:00
|
|
|
class FunctionGraph(ParametricFunction):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2018-04-06 13:58:59 -07:00
|
|
|
"color": YELLOW,
|
|
|
|
"x_min": -FRAME_X_RADIUS,
|
|
|
|
"x_max": FRAME_X_RADIUS,
|
2015-10-27 21:00:50 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2015-10-27 21:00:50 -07:00
|
|
|
def __init__(self, function, **kwargs):
|
2018-01-18 16:46:38 -08:00
|
|
|
digest_config(self, kwargs)
|
2019-02-06 15:18:11 -08:00
|
|
|
self.parametric_function = \
|
|
|
|
lambda t: np.array([t, function(t), 0])
|
2018-01-18 16:46:38 -08:00
|
|
|
ParametricFunction.__init__(
|
2018-04-06 13:58:59 -07:00
|
|
|
self,
|
2019-02-06 15:18:11 -08:00
|
|
|
self.parametric_function,
|
2018-04-06 13:58:59 -07:00
|
|
|
t_min=self.x_min,
|
|
|
|
t_max=self.x_max,
|
2018-01-18 16:46:38 -08:00
|
|
|
**kwargs
|
|
|
|
)
|
2015-10-28 16:03:33 -07:00
|
|
|
self.function = function
|
2015-10-27 21:00:50 -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):
|
|
|
|
return self.parametric_function(x)
|