functions.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from __future__ import annotations
  2. from isosurfaces import plot_isoline
  3. import numpy as np
  4. from manimlib.constants import FRAME_X_RADIUS, FRAME_Y_RADIUS
  5. from manimlib.constants import YELLOW
  6. from manimlib.mobject.types.vectorized_mobject import VMobject
  7. from typing import TYPE_CHECKING
  8. if TYPE_CHECKING:
  9. from typing import Callable, Sequence, Tuple
  10. from manimlib.typing import ManimColor, Vect3
  11. class ParametricCurve(VMobject):
  12. def __init__(
  13. self,
  14. t_func: Callable[[float], Sequence[float] | Vect3],
  15. t_range: Tuple[float, float, float] = (0, 1, 0.1),
  16. epsilon: float = 1e-8,
  17. # TODO, automatically figure out discontinuities
  18. discontinuities: Sequence[float] = [],
  19. use_smoothing: bool = True,
  20. **kwargs
  21. ):
  22. self.t_func = t_func
  23. self.t_range = t_range
  24. self.epsilon = epsilon
  25. self.discontinuities = discontinuities
  26. self.use_smoothing = use_smoothing
  27. super().__init__(**kwargs)
  28. def get_point_from_function(self, t: float) -> Vect3:
  29. return np.array(self.t_func(t))
  30. def init_points(self):
  31. t_min, t_max, step = self.t_range
  32. jumps = np.array(self.discontinuities)
  33. jumps = jumps[(jumps > t_min) & (jumps < t_max)]
  34. boundary_times = [t_min, t_max, *(jumps - self.epsilon), *(jumps + self.epsilon)]
  35. boundary_times.sort()
  36. for t1, t2 in zip(boundary_times[0::2], boundary_times[1::2]):
  37. t_range = [*np.arange(t1, t2, step), t2]
  38. points = np.array([self.t_func(t) for t in t_range])
  39. self.start_new_path(points[0])
  40. self.add_points_as_corners(points[1:])
  41. if self.use_smoothing:
  42. self.make_smooth(approx=True)
  43. if not self.has_points():
  44. self.set_points(np.array([self.t_func(t_min)]))
  45. return self
  46. def get_t_func(self):
  47. return self.t_func
  48. def get_function(self):
  49. if hasattr(self, "underlying_function"):
  50. return self.underlying_function
  51. if hasattr(self, "function"):
  52. return self.function
  53. def get_x_range(self):
  54. if hasattr(self, "x_range"):
  55. return self.x_range
  56. class FunctionGraph(ParametricCurve):
  57. def __init__(
  58. self,
  59. function: Callable[[float], float],
  60. x_range: Tuple[float, float, float] = (-8, 8, 0.25),
  61. color: ManimColor = YELLOW,
  62. **kwargs
  63. ):
  64. self.function = function
  65. self.x_range = x_range
  66. def parametric_function(t):
  67. return [t, function(t), 0]
  68. super().__init__(parametric_function, self.x_range, **kwargs)
  69. class ImplicitFunction(VMobject):
  70. def __init__(
  71. self,
  72. func: Callable[[float, float], float],
  73. x_range: Tuple[float, float] = (-FRAME_X_RADIUS, FRAME_X_RADIUS),
  74. y_range: Tuple[float, float] = (-FRAME_Y_RADIUS, FRAME_Y_RADIUS),
  75. min_depth: int = 5,
  76. max_quads: int = 1500,
  77. use_smoothing: bool = False,
  78. joint_type: str = 'no_joint',
  79. **kwargs
  80. ):
  81. super().__init__(joint_type=joint_type, **kwargs)
  82. p_min, p_max = (
  83. np.array([x_range[0], y_range[0]]),
  84. np.array([x_range[1], y_range[1]]),
  85. )
  86. curves = plot_isoline(
  87. fn=lambda u: func(u[0], u[1]),
  88. pmin=p_min,
  89. pmax=p_max,
  90. min_depth=min_depth,
  91. max_quads=max_quads,
  92. ) # returns a list of lists of 2D points
  93. curves = [
  94. np.pad(curve, [(0, 0), (0, 1)])
  95. for curve in curves
  96. if curve != []
  97. ] # add z coord as 0
  98. for curve in curves:
  99. self.start_new_path(curve[0])
  100. self.add_points_as_corners(curve[1:])
  101. if use_smoothing:
  102. self.make_smooth()