Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Added 3D support for :class:`~.ArrowVectorField` and :class:`~.StreamLines` (#2118)

icedcoffeeee094e983d7726
diff --git a/manim/mobject/vector_field.py b/manim/mobject/vector_field.py
index 173d330eb..57abbb0f6 100644
--- a/manim/mobject/vector_field.py
+++ b/manim/mobject/vector_field.py
@@ -20,12 +20,13 @@ from ..animation.composition import AnimationGroup, Succession
from ..animation.creation import Create
from ..animation.indication import ShowPassingFlash
from ..animation.update import UpdateFromAlphaFunc
-from ..constants import RIGHT, UP
-from ..mobject.geometry import Line, Vector
+from ..constants import OUT, RIGHT, UP
+from ..mobject.geometry import Vector
from ..mobject.mobject import Mobject
from ..mobject.types.vectorized_mobject import VGroup, VMobject
from ..utils.bezier import interpolate, inverse_interpolate
from ..utils.color import BLUE_E, GREEN, RED, YELLOW, color_to_rgb, rgb_to_color
+from ..utils.deprecation import deprecated_params
from ..utils.rate_functions import ease_out_sine, linear
from ..utils.simple_functions import sigmoid
from .types.opengl_vectorized_mobject import OpenGLVMobject
@@ -148,7 +149,7 @@ class VectorField(VGroup):
class ScaleVectorFieldFunction(Scene):
def construct(self):
- func = lambda pos: np.sin(pos[1])*RIGHT+np.cos(pos[0])*UP
+ func = lambda pos: np.sin(pos[1]) * RIGHT + np.cos(pos[0]) * UP
vector_field = ArrowVectorField(func)
self.add(vector_field)
self.wait()
@@ -185,9 +186,9 @@ class VectorField(VGroup):
The amount of steps the whole nudge is divided into. Higher values
give more accurate approximations.
pointwise
- Whether to move the mobject along the vector field. If `True` the
+ Whether to move the mobject along the vector field. If `False` the
vector field takes effect on the center of the given
- :class:`~.Mobject`. If `False` the vector field takes effect on the
+ :class:`~.Mobject`. If `True` the vector field takes effect on the
points of the individual points of the :class:`~.Mobject`,
potentially distorting it.
@@ -203,8 +204,10 @@ class VectorField(VGroup):
class Nudging(Scene):
def construct(self):
- func = lambda pos: np.sin(pos[1]/2)*RIGHT+np.cos(pos[0]/2)*UP
- vector_field = ArrowVectorField(func, delta_x=1, delta_y=1, length_func=lambda x:x/2)
+ func = lambda pos: np.sin(pos[1] / 2) * RIGHT + np.cos(pos[0] / 2) * UP
+ vector_field = ArrowVectorField(
+ func, x_range=[-7, 7, 1], y_range=[-4, 4, 1], length_func=lambda x: x / 2
+ )
self.add(vector_field)
circle = Circle(radius=2).shift(LEFT)
self.add(circle.copy().set_color(GRAY))
@@ -447,18 +450,15 @@ class ArrowVectorField(VectorField):
The value of the color_scheme function to be mapped to the last color in `colors`. Higher values also result in the last color of the gradient.
colors
The colors defining the color gradient of the vector field.
- x_min
- The minimum x value for which to draw vectors.
- x_max
- The maximum x value for which to draw vectors.
- y_min
- The minimum y value for which to draw vectors.
- y_max
- The maximum y value for which to draw vectors.
- delta_x
- The distance in x direction between two vectors.
- delta_y
- The distance in y direction between two vectors.
+ x_range
+ A sequence of x_min, x_max, delta_x
+ y_range
+ A sequence of y_min, y_max, delta_y
+ z_range
+ A sequence of z_min, z_max, delta_z
+ three_dimensions
+ Enables three_dimensions. Default set to False, automatically turns True if
+ z_range is not None.
length_func
The function determining the displayed size of the vectors. The actual size
of the vector is passed, the returned value will be used as display size for the
@@ -478,20 +478,20 @@ class ArrowVectorField(VectorField):
class BasicUsage(Scene):
def construct(self):
- func = lambda pos: ((pos[0]*UR+pos[1]*LEFT)-pos)/3
+ func = lambda pos: ((pos[0] * UR + pos[1] * LEFT) - pos) / 3
self.add(ArrowVectorField(func))
.. manim:: SizingAndSpacing
class SizingAndSpacing(Scene):
def construct(self):
- func = lambda pos: np.sin(pos[0]/2)*UR+np.cos(pos[1]/2)*LEFT
- vf = ArrowVectorField(func, delta_x=1)
+ func = lambda pos: np.sin(pos[0] / 2) * UR + np.cos(pos[1] / 2) * LEFT
+ vf = ArrowVectorField(func, x_range=[-7, 7, 1])
self.add(vf)
self.wait()
length_func = lambda x: x / 3
- vf2 = ArrowVectorField(func, delta_x=1, length_func=length_func)
+ vf2 = ArrowVectorField(func, x_range=[-7, 7, 1], length_func=length_func)
self.play(vf.animate.become(vf2))
self.wait()
@@ -500,15 +500,23 @@ class ArrowVectorField(VectorField):
class Coloring(Scene):
def construct(self):
- func = lambda pos: pos-LEFT*5
+ func = lambda pos: pos - LEFT * 5
colors = [RED, YELLOW, BLUE, DARK_GRAY]
- min_radius = Circle(radius=2, color=colors[0]).shift(LEFT*5)
- max_radius = Circle(radius=10, color=colors[-1]).shift(LEFT*5)
- vf = ArrowVectorField(func, min_color_scheme_value=2, max_color_scheme_value=10, colors=colors)
+ min_radius = Circle(radius=2, color=colors[0]).shift(LEFT * 5)
+ max_radius = Circle(radius=10, color=colors[-1]).shift(LEFT * 5)
+ vf = ArrowVectorField(
+ func, min_color_scheme_value=2, max_color_scheme_value=10, colors=colors
+ )
self.add(vf, min_radius, max_radius)
"""
+ @deprecated_params(
+ params="x_min, x_max, delta_x, y_min, y_max, delta_y",
+ since="v0.10.0",
+ until="v0.11.0",
+ message="Please use x_range and y_range instead.",
+ )
def __init__(
self,
func: Callable[[np.ndarray], np.ndarray],
@@ -518,18 +526,46 @@ class ArrowVectorField(VectorField):
max_color_scheme_value: float = 2,
colors: Sequence[Color] = DEFAULT_SCALAR_FIELD_COLORS,
# Determining Vector positions:
- x_min: float = -(config["frame_width"] + 1) / 2,
- x_max: float = (config["frame_width"] + 1) / 2,
- y_min: float = -(config["frame_height"] + 1) / 2,
- y_max: float = (config["frame_height"] + 1) / 2,
- delta_x: float = 0.5,
- delta_y: float = 0.5,
+ x_range: Sequence[float] = None,
+ y_range: Sequence[float] = None,
+ z_range: Sequence[float] = None,
+ three_dimensions: bool = False, # Automatically True if z_range is set
# Takes in actual norm, spits out displayed norm
length_func: Callable[[float], float] = lambda norm: 0.45 * sigmoid(norm),
opacity: float = 1.0,
vector_config: Optional[dict] = None,
**kwargs
):
+ self.x_range = x_range or [
+ kwargs.pop("x_min", None) or floor(-config["frame_width"] / 2),
+ kwargs.pop("x_max", None) or ceil(config["frame_width"] / 2),
+ ]
+ self.y_range = y_range or [
+ kwargs.pop("y_min", None) or floor(-config["frame_height"] / 2),
+ kwargs.pop("y_max", None) or ceil(config["frame_height"] / 2),
+ ]
+ self.ranges = [self.x_range, self.y_range]
+
+ if three_dimensions or z_range:
+ self.z_range = z_range or self.y_range.copy()
+ self.ranges += [self.z_range]
+ else:
+ self.ranges += [[0, 0]]
+
+ for i in range(len(self.ranges)):
+ if len(self.ranges[i]) == 2:
+ self.ranges[i] += [0.5]
+ self.ranges[i][1] += self.ranges[i][2]
+
+ if "delta_x" in kwargs:
+ self.ranges[0][2] = kwargs.pop("delta_x")
+ self.ranges[0][1] += self.ranges[0][2] - 0.5
+ if "delta_y" in kwargs:
+ self.ranges[1][2] = kwargs.pop("delta_y")
+ self.ranges[1][1] += self.ranges[1][2] - 0.5
+
+ self.x_range, self.y_range, self.z_range = self.ranges
+
super().__init__(
func,
color,
@@ -539,13 +575,6 @@ class ArrowVectorField(VectorField):
colors,
**kwargs,
)
- # Rounding min and max values to fit delta value
- self.x_min = floor(x_min / delta_x) * delta_x
- self.x_max = ceil(x_max / delta_x) * delta_x
- self.y_min = floor(y_min / delta_y) * delta_y
- self.y_max = ceil(y_max / delta_y) * delta_y
- self.delta_x = delta_x
- self.delta_y = delta_y
self.length_func = length_func
self.opacity = opacity
@@ -554,10 +583,11 @@ class ArrowVectorField(VectorField):
self.vector_config = vector_config
self.func = func
- x_range = np.arange(self.x_min, self.x_max, self.delta_x)
- y_range = np.arange(self.y_min, self.y_max, self.delta_y)
- for x, y in it.product(x_range, y_range):
- self.add(self.get_vector(x * RIGHT + y * UP))
+ x_range = np.arange(*self.x_range)
+ y_range = np.arange(*self.y_range)
+ z_range = np.arange(*self.z_range)
+ for x, y, z in it.product(x_range, y_range, z_range):
+ self.add(self.get_vector(x * RIGHT + y * UP + z * OUT))
self.set_opacity(self.opacity)
def get_vector(self, point: np.ndarray):
@@ -609,18 +639,15 @@ class StreamLines(VectorField):
The value of the color_scheme function to be mapped to the last color in `colors`. Higher values also result in the last color of the gradient.
colors
The colors defining the color gradient of the vector field.
- x_min
- The minimum x value at which agends are spawned
- x_max
- The maximum x value at which agends are spawned
- y_min
- The minimum y value at which agends are spawned
- y_max
- The maximum y value at which agends are spawned
- delta_x
- The distance in x direction between two agents.
- delta_y
- The distance in y direction between two agents.
+ x_range
+ A sequence of x_min, x_max, delta_x
+ y_range
+ A sequence of y_min, y_max, delta_y
+ z_range
+ A sequence of z_min, z_max, delta_z
+ three_dimensions
+ Enables three_dimensions. Default set to False, automatically turns True if
+ z_range is not None.
noise_factor
The amount by which the starting position of each agent is altered along each axis. Defaults to :code:`delta_y / 2` if not defined.
n_repeats
@@ -646,7 +673,7 @@ class StreamLines(VectorField):
class BasicUsage(Scene):
def construct(self):
- func = lambda pos: ((pos[0]*UR+pos[1]*LEFT) - pos)/3
+ func = lambda pos: ((pos[0] * UR + pos[1] * LEFT) - pos) / 3
self.add(StreamLines(func))
.. manim:: SpawningAndFlowingArea
@@ -654,20 +681,14 @@ class StreamLines(VectorField):
class SpawningAndFlowingArea(Scene):
def construct(self):
- func = lambda pos: np.sin(pos[0])*UR+np.cos(pos[1])*LEFT+pos/5
+ func = lambda pos: np.sin(pos[0]) * UR + np.cos(pos[1]) * LEFT + pos / 5
stream_lines = StreamLines(
- func,
- x_min=-3, x_max=3, delta_x=0.2,
- y_min=-2, y_max=2, delta_y=0.2,
- padding=1
+ func, x_range=[-3, 3, 0.2], y_range=[-2, 2, 0.2], padding=1
)
spawning_area = Rectangle(width=6, height=4)
flowing_area = Rectangle(width=8, height=6)
- labels = [
- Tex("Spawning Area"),
- Tex("Flowing Area").shift(DOWN*2.5)
- ]
+ labels = [Tex("Spawning Area"), Tex("Flowing Area").shift(DOWN * 2.5)]
for lbl in labels:
lbl.add_background_rectangle(opacity=0.6, buff=0.05)
@@ -675,6 +696,12 @@ class StreamLines(VectorField):
"""
+ @deprecated_params(
+ params="x_min, x_max, delta_x, y_min, y_max, delta_y",
+ since="v0.10.0",
+ until="v0.11.0",
+ message="Please use x_range and y_range instead.",
+ )
def __init__(
self,
func: Callable[[np.ndarray], np.ndarray],
@@ -684,12 +711,10 @@ class StreamLines(VectorField):
max_color_scheme_value: float = 2,
colors: Sequence[Color] = DEFAULT_SCALAR_FIELD_COLORS,
# Determining stream line starting positions:
- x_min: float = -(config["frame_width"] + 1) / 2,
- x_max: float = (config["frame_width"] + 1) / 2,
- y_min: float = -(config["frame_height"] + 1) / 2,
- y_max: float = (config["frame_height"] + 1) / 2,
- delta_x: float = 0.5,
- delta_y: float = 0.5,
+ x_range: Sequence[float] = None,
+ y_range: Sequence[float] = None,
+ z_range: Sequence[float] = None,
+ three_dimensions: bool = False,
noise_factor: Optional[float] = None,
n_repeats=1,
# Determining how lines are drawn
@@ -702,6 +727,36 @@ class StreamLines(VectorField):
opacity=1,
**kwargs
):
+ self.x_range = x_range or [
+ kwargs.pop("x_min", None) or floor(-config["frame_width"] / 2),
+ kwargs.pop("x_max", None) or ceil(config["frame_width"] / 2),
+ ]
+ self.y_range = y_range or [
+ kwargs.pop("y_min", None) or floor(-config["frame_height"] / 2),
+ kwargs.pop("y_max", None) or ceil(config["frame_height"] / 2),
+ ]
+ self.ranges = [self.x_range, self.y_range]
+
+ if three_dimensions or z_range:
+ self.z_range = z_range or self.y_range.copy()
+ self.ranges += [self.z_range]
+ else:
+ self.ranges += [[0, 0]]
+
+ for i in range(len(self.ranges)):
+ if len(self.ranges[i]) == 2:
+ self.ranges[i] += [0.5]
+ self.ranges[i][1] += self.ranges[i][2]
+
+ if "delta_x" in kwargs:
+ self.ranges[0][2] = kwargs.pop("delta_x")
+ self.ranges[0][1] += self.ranges[0][2] - 0.5
+ if "delta_y" in kwargs:
+ self.ranges[1][2] = kwargs.pop("delta_y")
+ self.ranges[1][1] += self.ranges[1][2] - 0.5
+
+ self.x_range, self.y_range, self.z_range = self.ranges
+
super().__init__(
func,
color,
@@ -711,13 +766,10 @@ class StreamLines(VectorField):
colors,
**kwargs,
)
- self.x_min = x_min
- self.x_max = x_max
- self.y_min = y_min
- self.y_max = y_max
- self.delta_x = delta_x
- self.delta_y = delta_y
- self.noise_factor = noise_factor if noise_factor is not None else delta_y / 2
+
+ self.noise_factor = (
+ noise_factor if noise_factor is not None else self.y_range[2] / 2
+ )
self.n_repeats = n_repeats
self.virtual_time = virtual_time
self.max_anchors_per_line = max_anchors_per_line
@@ -730,19 +782,23 @@ class StreamLines(VectorField):
[
(x - half_noise) * RIGHT
+ (y - half_noise) * UP
+ + (z - half_noise) * OUT
+ self.noise_factor * np.random.random(3)
for n in range(self.n_repeats)
- for x in np.arange(self.x_min, self.x_max + self.delta_x, self.delta_x)
- for y in np.arange(self.y_min, self.y_max + self.delta_y, self.delta_y)
+ for x in np.arange(*self.x_range)
+ for y in np.arange(*self.y_range)
+ for z in np.arange(*self.z_range)
],
)
def outside_box(p):
return (
- p[0] < self.x_min - self.padding
- or p[0] > self.x_max + self.padding
- or p[1] < self.y_min - self.padding
- or p[1] > self.y_max + self.padding
+ p[0] < self.x_range[0] - self.padding
+ or p[0] > self.x_range[1] + self.padding - self.x_range[2]
+ or p[1] < self.y_range[0] - self.padding
+ or p[1] > self.y_range[1] + self.padding - self.y_range[2]
+ or p[2] < self.z_range[0] - self.padding
+ or p[2] > self.z_range[1] + self.padding - self.z_range[2]
)
max_steps = ceil(virtual_time / dt) + 1
@@ -786,7 +842,12 @@ class StreamLines(VectorField):
name="stroke_rgba",
)
else:
- line.color_using_background_image(self.background_img)
+ if np.any(self.z_range != np.array([0, 0.5, 0.5])):
+ line.set_stroke(
+ [self.pos_to_color(p) for p in line.get_anchors()],
+ )
+ else:
+ line.color_using_background_image(self.background_img)
line.set_stroke(width=self.stroke_width, opacity=opacity)
self.add(line)
self.stream_lines = [*self.submobjects]
@@ -822,15 +883,17 @@ class StreamLines(VectorField):
class StreamLineCreation(Scene):
def construct(self):
- func = lambda pos: (pos[0]*UR+pos[1]*LEFT) - pos
+ func = lambda pos: (pos[0] * UR + pos[1] * LEFT) - pos
stream_lines = StreamLines(
func,
color=YELLOW,
- delta_x=1, delta_y=1, stroke_width=3,
- virtual_time=1, # use shorter lines
- max_anchors_per_line=5, #better performance with fewer anchors
+ x_range=[-7, 7, 1],
+ y_range=[-4, 4, 1],
+ stroke_width=3,
+ virtual_time=1, # use shorter lines
+ max_anchors_per_line=5, # better performance with fewer anchors
)
- self.play(stream_lines.create()) # uses virtual_time as run_time
+ self.play(stream_lines.create()) # uses virtual_time as run_time
self.wait()
"""
@@ -878,11 +941,8 @@ class StreamLines(VectorField):
class ContinuousMotion(Scene):
def construct(self):
- func = lambda pos: np.sin(pos[0]/2)*UR+np.cos(pos[1]/2)*LEFT
- stream_lines = StreamLines(
- func, stroke_width=3,
- max_anchors_per_line=30
- )
+ func = lambda pos: np.sin(pos[0] / 2) * UR + np.cos(pos[1] / 2) * LEFT
+ stream_lines = StreamLines(func, stroke_width=3, max_anchors_per_line=30)
self.add(stream_lines)
stream_lines.start_animation(warm_up=False, flow_speed=1.5)
self.wait(stream_lines.virtual_time / stream_lines.flow_speed)
@@ -938,18 +998,12 @@ class StreamLines(VectorField):
class EndAnimation(Scene):
def construct(self):
- func = lambda pos: np.sin(pos[0]/2)*UR+np.cos(pos[1]/2)*LEFT
+ func = lambda pos: np.sin(pos[0] / 2) * UR + np.cos(pos[1] / 2) * LEFT
stream_lines = StreamLines(
- func, stroke_width=3,
- max_anchors_per_line=5,
- virtual_time=1, color=BLUE
+ func, stroke_width=3, max_anchors_per_line=5, virtual_time=1, color=BLUE
)
self.add(stream_lines)
- stream_lines.start_animation(
- warm_up=False,
- flow_speed=1.5,
- time_width=0.5
- )
+ stream_lines.start_animation(warm_up=False, flow_speed=1.5, time_width=0.5)
self.wait(1)
self.play(stream_lines.end_animation())