Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Merge :class:`~.OpenGLMobject` and :class:`~.Mobject` (#1167)

Mohammad Al-Fetyanie18db42ae1ea
diff --git a/example_scenes/opengl.py b/example_scenes/opengl.py
index d634e9680..b2389b9ba 100644
--- a/example_scenes/opengl.py
+++ b/example_scenes/opengl.py
@@ -54,13 +54,42 @@ class InteractiveDevelopment(Scene):
# always(circle.move_to, self.mouse_point)
+class SquareToCircle(Scene):
+ def construct(self):
+ square = OpenGLSquare()
+ circle = OpenGLCircle()
+
+ self.add(square)
+ self.wait()
+
+ self.play(Transform(square, circle))
+ self.wait()
+
+
+class UpdaterTest(Scene):
+ def construct(self):
+ squares = OpenGLVGroup()
+ for _ in range(9):
+ squares.add(OpenGLSquare(1, stroke_opacity=0).set_fill(WHITE, 0.5))
+ squares.arrange_in_grid(3, 3, buff=0)
+
+ def line():
+ return OpenGLLine(ORIGIN, squares.get_corner(UL))
+
+ self.add(always_redraw(line))
+ self.play(squares.animate.to_edge(UP))
+ self.play(squares.animate.to_edge(DR))
+ self.play(squares.animate.shift(LEFT * 10))
+ self.wait()
+
+
class SurfaceExample(Scene):
def construct(self):
- # surface_text = Text("For 3d scenes, try using surfaces")
- # surface_text.fix_in_frame()
- # surface_text.to_edge(UP)
- # self.add(surface_text)
- # self.wait(0.1)
+ surface_text = Tex("For 3d scenes, try using surfaces")
+ surface_text.fix_in_frame()
+ surface_text.to_edge(UP)
+ self.add(surface_text)
+ self.wait(0.1)
torus1 = OpenGLTorus(r1=1, r2=1)
torus2 = OpenGLTorus(r1=3, r2=1)
@@ -123,7 +152,6 @@ class SurfaceExample(Scene):
# light_text = Text("You can move around the light source")
# light_text.move_to(surface_text)
# light_text.fix_in_frame()
-
# self.play(FadeTransform(surface_text, light_text))
light = self.camera.light_source
self.add(light)
diff --git a/manim/animation/animation.py b/manim/animation/animation.py
index e0a943f76..aeb16335a 100644
--- a/manim/animation/animation.py
+++ b/manim/animation/animation.py
@@ -18,7 +18,6 @@ from ..mobject import mobject
from ..mobject import opengl_mobject
from ..mobject.mobject import Mobject
from ..utils.rate_functions import smooth
-from ..mobject.opengl_mobject import OpenGLMobject
DEFAULT_ANIMATION_RUN_TIME: float = 1.0
DEFAULT_ANIMATION_LAG_RATIO: float = 0.0
@@ -64,9 +63,7 @@ class Animation:
def _typecheck_input(self, mobject: Mobject) -> None:
if mobject is None:
logger.debug("creating dummy animation")
- elif not isinstance(mobject, Mobject) and not isinstance(
- mobject, OpenGLMobject
- ):
+ elif not isinstance(mobject, Mobject):
raise TypeError("Animation only works on Mobjects")
def __str__(self) -> str:
@@ -232,9 +229,6 @@ def prepare_animation(
if isinstance(anim, mobject._AnimationBuilder):
return anim.build()
- if isinstance(anim, opengl_mobject._AnimationBuilder):
- return anim.build()
-
if isinstance(anim, Animation):
return anim
diff --git a/manim/animation/transform.py b/manim/animation/transform.py
index 064e71193..58bf92525 100644
--- a/manim/animation/transform.py
+++ b/manim/animation/transform.py
@@ -31,7 +31,6 @@ import numpy as np
from ..animation.animation import Animation
from ..constants import DEFAULT_POINTWISE_FUNCTION_RUN_TIME, DEGREES, OUT
from ..mobject.mobject import Group, Mobject
-from ..mobject.opengl_mobject import OpenGLMobject
from ..utils.paths import path_along_arc, straight_path
from ..utils.rate_functions import smooth, squish_rate_func
@@ -218,7 +217,7 @@ class ApplyMethod(Transform):
"Whoops, looks like you accidentally invoked "
"the method you want to animate"
)
- assert isinstance(method.__self__, (Mobject, OpenGLMobject))
+ assert isinstance(method.__self__, Mobject)
def create_target(self) -> Mobject:
method = self.method
diff --git a/manim/mobject/mobject.py b/manim/mobject/mobject.py
index 13a654403..639b2487e 100644
--- a/manim/mobject/mobject.py
+++ b/manim/mobject/mobject.py
@@ -4,7 +4,7 @@
__all__ = ["Mobject", "Group", "override_animate"]
-from functools import reduce
+from functools import reduce, wraps
import copy
import itertools as it
import operator as op
@@ -16,15 +16,33 @@ import warnings
from pathlib import Path
from colour import Color
+from manim.utils.bezier import integer_interpolate
+import moderngl
import numpy as np
from .. import config
from ..constants import *
from ..container import Container
-from ..utils.color import Colors, color_gradient, WHITE, BLACK, YELLOW_C
+from ..utils.color import (
+ Colors,
+ color_gradient,
+ WHITE,
+ BLACK,
+ YELLOW_C,
+ color_to_rgb,
+ rgb_to_hex,
+)
from ..utils.color import interpolate_color
-from ..utils.iterables import list_update
+from ..utils.iterables import (
+ batch_by_property,
+ list_update,
+ listify,
+ make_even,
+ resize_preserving_order,
+ resize_with_interpolation,
+)
from ..utils.iterables import remove_list_redundancies
+from ..utils.iterables import resize_array
from ..utils.paths import straight_path
from ..utils.simple_functions import get_parameters
from ..utils.space_ops import angle_of_vector
@@ -37,6 +55,21 @@ from ..utils.space_ops import rotation_matrix_transpose
Updater = Union[Callable[["Mobject"], None], Callable[["Mobject", float], None]]
+def affects_shader_info_id(func):
+ @wraps(func)
+ def wrapper(self):
+ for mob in self.get_family():
+ func(mob)
+ # mob.refresh_shader_wrapper_id()
+ return self
+
+ return wrapper
+
+
+def interpolate(start: int, end: int, alpha: float) -> float:
+ return (1 - alpha) * start + alpha * end
+
+
class Mobject(Container):
"""Mathematical Object: base class for objects that can be displayed on screen.
@@ -53,44 +86,101 @@ class Mobject(Container):
"""
- def __init__(self, color=WHITE, name=None, dim=3, target=None, z_index=0, **kwargs):
- self.color = Color(color)
- self.name = self.__class__.__name__ if name is None else name
+ shader_dtype = [
+ ("point", np.float32, (3,)),
+ ]
+ shader_folder = ""
+
+ def __init__(
+ self,
+ color=WHITE,
+ opacity=1,
+ name=None,
+ dim=3,
+ target=None,
+ z_index=0,
+ gloss=0.0,
+ # Positive shadow up to 1 makes a side opposite the light darker
+ shadow=0.0,
+ # For shaders
+ render_primitive=moderngl.TRIANGLE_STRIP,
+ texture_paths=None,
+ depth_test=False,
+ # If true, the mobject will not get rotated according to camera position
+ is_fixed_in_frame=False,
+ # Must match in attributes of vert shader
+ # Event listener
+ listen_to_events=False,
+ **kwargs,
+ ):
+ # OpenGL data.
+
+ # If true, the mobject will not get rotated according to camera position
+ self.is_fixed_in_frame = is_fixed_in_frame
+ self.gloss = gloss
+ self.shadow = shadow
+ self.needs_new_bounding_box = True
+ self.locked_data_keys = set()
+
+ self.opengl = config["use_opengl_renderer"]
+ self.opacity = opacity
+ # For shaders
+ self.render_primitive = render_primitive
+ self.texture_paths = texture_paths
+ self.depth_test = depth_test
+
+ # Must match in attributes of vert shader
+ # Event listener
+ self.listen_to_events = listen_to_events
self.dim = dim
- self.target = target
- self.z_index = z_index
- self.point_hash = None
self.submobjects = []
+ self.parents = []
self.updaters = []
self.updating_suspended = False
+ self.family = [self]
+ self.name = self.__class__.__name__ if name is None else name
+ self.target = target
+ self.z_index = z_index
+ self.point_hash = None
+
+ self.init_data()
+ self.init_uniforms()
self.reset_points()
+
+ self.color = Color(color)
+
+ # self.init_event_listners()
self.generate_points()
self.init_colors()
- # OpenGL data.
- self.data = {}
- self.depth_test = False
- self.is_fixed_in_frame = False
- self.gloss = 0.0
- self.shadow = 0.0
- self.needs_new_bounding_box = True
- self.parents = []
- self.family = [self]
-
- self.init_gl_data()
- self.init_gl_points()
- self.init_gl_colors()
+ self.shader_indices = None
+ if self.depth_test:
+ self.apply_depth_test()
Container.__init__(self, **kwargs)
- def init_gl_data(self):
- pass
-
- def init_gl_points(self):
- pass
-
- def init_gl_colors(self):
- pass
+ def get_points(self):
+ return self.points
+
+ def init_data(self):
+ self.data = {
+ "points": np.zeros((0, 3)),
+ "bounding_box": np.zeros((3, 3)),
+ "rgbas": np.zeros((1, 4)),
+ }
+
+ def init_uniforms(self):
+ self.uniforms = {
+ "is_fixed_in_frame": float(self.is_fixed_in_frame),
+ "gloss": self.gloss,
+ "shadow": self.shadow,
+ }
+
+ def resize_points(self, new_length, resize_func=resize_array):
+ if new_length != len(self.points):
+ self.points = resize_func(self.points, new_length)
+ self.refresh_bounding_box()
+ return self
def get_bounding_box(self):
if self.needs_new_bounding_box:
@@ -101,7 +191,7 @@ class Mobject(Container):
def compute_bounding_box(self):
all_points = np.vstack(
[
- self.data["points"],
+ self.get_points(),
*(
mob.get_bounding_box()
for mob in self.get_family()[1:]
@@ -217,7 +307,8 @@ class Mobject(Container):
Gets called upon creation. This is an empty method that can be implemented by subclasses.
"""
- pass
+ if self.opengl:
+ self.set_color(self.color, self.opacity)
def generate_points(self):
"""Initializes :attr:`points` and therefore the shape.
@@ -226,6 +317,33 @@ class Mobject(Container):
"""
pass
+ @property
+ def points(self):
+ return self.data["points"]
+
+ @points.setter
+ def points(self, value):
+ self.data["points"] = value
+
+ def match_points(self, mobject):
+ self.set_points(mobject.get_points())
+
+ def set_points(self, points):
+ if len(points) == len(self.points):
+ self.points[:] = points
+ elif isinstance(points, np.ndarray):
+ self.points = points.copy()
+ else:
+ self.points = np.array(points)
+ self.refresh_bounding_box()
+
+ return self
+
+ def set_data(self, data):
+ for key in data:
+ self.data[key] = data[key].copy()
+ return self
+
def refresh_bounding_box(self, recurse_down=False, recurse_up=True):
for mob in self.get_family(recurse_down):
mob.needs_new_bounding_box = True
@@ -234,6 +352,12 @@ class Mobject(Container):
parent.refresh_bounding_box()
return self
+ def is_point_touching(self, point, buff=MED_SMALL_BUFF):
+ bb = self.get_bounding_box()
+ mins = bb[0] - buff
+ maxs = bb[2] + buff
+ return (point >= mins).all() and (point <= maxs).all()
+
def add(self, *mobjects: "Mobject") -> "Mobject":
"""Add mobjects as submobjects.
@@ -293,24 +417,17 @@ class Mobject(Container):
ValueError: Mobject cannot contain self
"""
- if config["use_opengl_renderer"]:
+ for mobject in mobjects:
if self in mobjects:
- raise Exception("Mobject cannot contain self")
- for mobject in mobjects:
- if mobject not in self.submobjects:
- self.submobjects.append(mobject)
- if self not in mobject.parents:
- mobject.parents.append(self)
- self.assemble_family()
- return self
- else:
- for m in mobjects:
- if not isinstance(m, Mobject):
- raise TypeError("All submobjects must be of type Mobject")
- if m is self:
- raise ValueError("Mobject cannot contain self")
- self.submobjects = list_update(self.submobjects, mobjects)
- return self
+ raise ValueError("Mobject cannot contain self")
+ if not isinstance(mobject, Mobject):
+ raise TypeError("All submobjects must be of type Mobject")
+ if mobject not in self.submobjects:
+ self.submobjects = list_update(self.submobjects, mobjects)
+ # if self not in mobject.parents:
+ # mobject.parents = list_update(self.submobjects, mobject.parents)
+ self.assemble_family()
+ return self
def __add__(self, mobject):
raise NotImplementedError
@@ -318,6 +435,30 @@ class Mobject(Container):
def __iadd__(self, mobject):
raise NotImplementedError
+ def set_submobjects(self, submobject_list):
+ self.remove(*self.submobjects)
+ self.add(*submobject_list)
+ return self
+
+ def replace_submobject(self, index, new_submob):
+ old_submob = self.submobjects[index]
+ if self in old_submob.parents:
+ old_submob.parents.remove(self)
+ self.submobjects[index] = new_submob
+ self.assemble_family()
+ return self
+
+ def digest_mobject_attrs(self):
+ """
+ Ensures all attributes which are mobjects are included
+ in the submobjects list.
+ """
+ mobject_attrs = [
+ x for x in list(self.__dict__.values()) if isinstance(x, Mobject)
+ ]
+ self.set_submobjects(list_update(self.submobjects, mobject_attrs))
+ return self
+
def add_to_back(self, *mobjects: "Mobject") -> "Mobject":
"""Add all passed mobjects to the back of the submobjects.
@@ -341,8 +482,7 @@ class Mobject(Container):
first, which places the corresponding mobjects behind the
subsequent list members.
"""
- self.remove(*mobjects)
- self.submobjects = list(mobjects) + self.submobjects
+ self.set_submobjects(list_update(mobjects, self.submobjects))
return self
def remove(self, *mobjects: "Mobject") -> "Mobject":
@@ -370,6 +510,9 @@ class Mobject(Container):
for mobject in mobjects:
if mobject in self.submobjects:
self.submobjects.remove(mobject)
+ # if self in mobject.parents:
+ # mobject.parents.remove(self)
+ self.assemble_family()
return self
def __sub__(self, other):
@@ -613,12 +756,8 @@ class Mobject(Container):
"""
return copy.deepcopy(self)
- def generate_target(self, use_deepcopy=False):
- self.target = None # Prevent unbounded linear recursion
- if use_deepcopy:
- self.target = copy.deepcopy(self)
- else:
- self.target = self.copy()
+ def generate_target(self):
+ self.target = self.copy()
return self.target
# Updating
@@ -780,7 +919,8 @@ class Mobject(Container):
else:
self.updaters.insert(index, update_function)
if call_updater:
- update_function(self, 0)
+ # update_function(self, 0)
+ self.update()
return self
def remove_updater(self, update_function: Updater) -> "Mobject":
@@ -915,6 +1055,8 @@ class Mobject(Container):
if recursive:
for submob in self.submobjects:
submob.resume_updating(recursive)
+ for parent in self.parents:
+ parent.resume_updating(recurse=False, call_updater=False)
self.update(dt=0, recursive=recursive)
return self
@@ -959,21 +1101,18 @@ class Mobject(Container):
:meth:`move_to`
"""
- if config["use_opengl_renderer"]:
+ if self.opengl:
self.apply_points_function(
lambda points: points + vectors[0],
about_edge=None,
works_on_bounding_box=True,
)
- return self
else:
total_vector = reduce(op.add, vectors)
for mob in self.family_members_with_points():
mob.points = mob.points.astype("float")
mob.points += total_vector
- if hasattr(mob, "data") and "points" in mob.data:
- mob.data["points"] += total_vector
- return self
+ return self
def scale(self, scale_factor: float, **kwargs) -> "Mobject":
"""Scale the size by a factor.
@@ -997,35 +1136,33 @@ class Mobject(Container):
:meth:`move_to`
"""
- if config["use_opengl_renderer"]:
+ if self.opengl:
self.apply_points_function(
lambda points: scale_factor * points,
works_on_bounding_box=True,
**kwargs,
)
- return self
else:
self.apply_points_function_about_point(
lambda points: scale_factor * points, **kwargs
)
- return self
+ return self
- def rotate_about_origin(self, angle, axis=OUT, axes=[]):
+ def rotate_about_origin(self, angle, axis=OUT):
return self.rotate(angle, axis, about_point=ORIGIN)
def rotate(self, angle, axis=OUT, **kwargs):
- if config["use_opengl_renderer"]:
+ if self.opengl:
rot_matrix_T = rotation_matrix_transpose(angle, axis)
self.apply_points_function(
lambda points: np.dot(points, rot_matrix_T), **kwargs
)
- return self
else:
rot_matrix = rotation_matrix(angle, axis)
self.apply_points_function_about_point(
lambda points: np.dot(points, rot_matrix.T), **kwargs
)
- return self
+ return self
def flip(self, axis=UP, **kwargs):
return self.rotate(TAU / 2, axis, **kwargs)
@@ -1035,14 +1172,14 @@ class Mobject(Container):
points[:, dim] *= factor
return points
- self.apply_points_function_about_point(func, **kwargs)
+ self.apply_function_to_points(func, works_on_bounding_box=True, **kwargs)
return self
def apply_function(self, function, **kwargs):
# Default to applying matrix about the origin, not mobjects center
if len(kwargs) == 0:
kwargs["about_point"] = ORIGIN
- self.apply_points_function_about_point(
+ self.apply_function_to_points(
lambda points: np.apply_along_axis(function, 1, points), **kwargs
)
return self
@@ -1056,6 +1193,12 @@ class Mobject(Container):
submob.apply_function_to_position(function)
return self
+ def apply_function_to_points(self, function, *args, **kwargs):
+ if self.opengl:
+ self.apply_points_function(function, *args, **kwargs)
+ else:
+ self.apply_points_function_about_point(function, *args, **kwargs)
+
def apply_matrix(self, matrix, **kwargs):
# Default to applying matrix about the origin, not mobjects center
if ("about_point" not in kwargs) and ("about_edge" not in kwargs):
@@ -1063,7 +1206,7 @@ class Mobject(Container):
full_matrix = np.identity(self.dim)
matrix = np.array(matrix)
full_matrix[: matrix.shape[0], : matrix.shape[1]] = matrix
- self.apply_points_function_about_point(
+ self.apply_function_to_points(
lambda points: np.dot(points, full_matrix.T), **kwargs
)
return self
@@ -1089,8 +1232,14 @@ class Mobject(Container):
return self
def reverse_points(self):
- for mob in self.family_members_with_points():
- mob.apply_over_attr_arrays(lambda arr: np.array(list(reversed(arr))))
+ if self.opengl:
+ for mob in self.get_family():
+ for key in mob.data:
+ mob.data[key] = mob.data[key][::-1]
+ else:
+ for mob in self.family_members_with_points():
+ mob.apply_over_attr_arrays(lambda arr: np.array(list(reversed(arr))))
+
return self
def repeat(self, count: int):
@@ -1107,15 +1256,20 @@ class Mobject(Container):
# Note, much of these are now redundant with default behavior of
# above methods
def apply_points_function(
- self, func, about_point=None, about_edge=ORIGIN, works_on_bounding_box=False
+ self,
+ func,
+ about_point=None,
+ about_edge=ORIGIN,
+ works_on_bounding_box=False,
+ **kwargs,
):
if about_point is None and about_edge is not None:
- about_point = self.get_bounding_box_point(about_edge)
+ about_point = self.get_critical_point(about_edge)
for mob in self.get_family():
arrs = []
- if len(self.data["points"]):
- arrs.append(mob.data["points"])
+ if mob.has_points():
+ arrs.append(mob.get_points())
if works_on_bounding_box:
arrs.append(mob.get_bounding_box())
@@ -1133,7 +1287,7 @@ class Mobject(Container):
return self
def apply_points_function_about_point(
- self, func, about_point=None, about_edge=None
+ self, func, about_point=None, about_edge=None, **kwargs
):
if about_point is None:
if about_edge is None:
@@ -1397,6 +1551,15 @@ class Mobject(Container):
self.shift(shift_vect)
return self
+ def set_width(self, width, stretch=False, **kwargs):
+ return self.rescale_to_fit(width, 0, stretch=stretch, **kwargs)
+
+ def set_height(self, height, stretch=False, **kwargs):
+ return self.rescale_to_fit(height, 1, stretch=stretch, **kwargs)
+
+ def set_depth(self, depth, stretch=False, **kwargs):
+ return self.rescale_to_fit(depth, 2, stretch=stretch, **kwargs)
+
def set_x(self, x, direction=ORIGIN):
return self.set_coord(x, 0, direction)
@@ -1426,7 +1589,7 @@ class Mobject(Container):
def replace(self, mobject, dim_to_match=0, stretch=False):
if not mobject.get_num_points() and not mobject.submobjects:
raise Warning("Attempting to replace mobject with no points")
- return self
+
if stretch:
self.stretch_to_fit_width(mobject.width)
self.stretch_to_fit_height(mobject.height)
@@ -1460,6 +1623,32 @@ class Mobject(Container):
self.shift(start - curr_start)
return self
+ # TODO: This should be moved to VM object for consistency
+ def set_rgba_array(self, color=None, opacity=None, name="rgbas", recurse=True):
+ if color is not None:
+ rgbs = np.array([color_to_rgb(c) for c in listify(color)])
+ if opacity is not None:
+ opacities = listify(opacity)
+
+ # Color only
+ if color is not None and opacity is None:
+ for mob in self.get_family(recurse):
+ mob.data[name] = resize_array(mob.data[name], len(rgbs))
+ mob.data[name][:, :3] = rgbs
+
+ # Opacity only
+ if color is None and opacity is not None:
+ for mob in self.get_family(recurse):
+ mob.data[name] = resize_array(mob.data[name], len(opacities))
+ mob.data[name][:, 3] = opacities
+
+ # Color and opacity
+ if color is not None and opacity is not None:
+ rgbas = np.array([[*rgb, o] for rgb, o in zip(*make_even(rgbs, opacities))])
+ for mob in self.get_family(recurse):
+ mob.data[name] = rgbas.copy()
+ return self
+
# Background rectangle
def add_background_rectangle(
self, color: Colors = BLACK, opacity: float = 0.75, **kwargs
@@ -1514,15 +1703,18 @@ class Mobject(Container):
# Color functions
- def set_color(self, color: Color = YELLOW_C, family: bool = True):
+ def set_color(
+ self, color: Color = YELLOW_C, opacity: float = None, family: bool = True
+ ):
"""Condition is function which takes in one arguments, (x, y, z).
Here it just recurses to submobjects, but in subclasses this
should be further implemented based on the the inner workings
of color
"""
+ self.set_rgba_array(color, opacity, recurse=False)
if family:
for submob in self.submobjects:
- submob.set_color(color, family=family)
+ submob.set_color(color, opacity, family=family)
self.color = Color(color)
return self
@@ -1545,6 +1737,7 @@ class Mobject(Container):
return self.set_color(*colors)
mobs = self.family_members_with_points()
+ # mobs = self.submobjects
new_colors = color_gradient(colors, len(mobs))
for mob, color in zip(mobs, new_colors):
@@ -1579,14 +1772,35 @@ class Mobject(Container):
return self
def fade(self, darkness=0.5, family=True):
+ if self.opengl:
+ self.set_opacity(1.0 - darkness, recurse=family)
+ return
if family:
for submob in self.submobjects:
submob.fade(darkness, family)
return self
def get_color(self):
+ if self.opengl:
+ return rgb_to_hex(self.data["rgbas"][0, :3])
return self.color
+ def get_gloss(self):
+ return self.gloss
+
+ def set_gloss(self, gloss, recurse=True):
+ for mob in self.get_family(recurse):
+ mob.gloss = gloss
+ return self
+
+ def get_shadow(self):
+ return self.shadow
+
+ def set_shadow(self, shadow, recurse=True):
+ for mob in self.get_family(recurse):
+ mob.shadow = shadow
+ return self
+
##
def save_state(self):
@@ -1637,10 +1851,7 @@ class Mobject(Container):
return self.get_all_points()
def get_num_points(self):
- if config["use_opengl_renderer"]:
- return len(self.data["points"])
- else:
- return len(self.points)
+ return len(self.points)
def get_extremum_along_dim(self, points=None, dim=0, key=0):
if points is None:
@@ -1693,10 +1904,32 @@ class Mobject(Container):
return np.apply_along_axis(np.mean, 0, self.get_all_points())
def get_boundary_point(self, direction):
+ if self.opengl:
+ all_points = self.get_all_points()
+ boundary_directions = all_points - self.get_center()
+ norms = np.linalg.norm(boundary_directions, axis=1)
+ boundary_directions /= np.repeat(norms, 3).reshape((len(norms), 3))
+ index = np.argmax(np.dot(boundary_directions, np.array(direction).T))
+ return all_points[index]
+
all_points = self.get_points_defining_boundary()
index = np.argmax(np.dot(all_points, np.array(direction).T))
return all_points[index]
+ def get_continuous_bounding_box_point(self, direction):
+ dl, center, ur = self.get_bounding_box()
+ corner_vect = ur - center
+ return center + direction / np.max(
+ np.abs(
+ np.true_divide(
+ direction,
+ corner_vect,
+ out=np.zeros(len(direction)),
+ where=((corner_vect) != 0),
+ )
+ )
+ )
+
def get_top(self):
return self.get_edge_center(UP)
@@ -1716,6 +1949,10 @@ class Mobject(Container):
return self.get_edge_center(IN)
def length_over_dim(self, dim):
+ if self.opengl:
+ bb = self.get_bounding_box()
+ return abs((bb[2] - bb[0])[dim])
+
return self.reduce_across_dimension(
np.max, np.max, dim
) - self.reduce_across_dimension(np.min, np.min, dim)
@@ -1724,6 +1961,15 @@ class Mobject(Container):
"""Meant to generalize get_x, get_y, get_z"""
return self.get_extremum_along_dim(dim=dim, key=direction[dim])
+ def get_width(self):
+ return self.length_over_dim(0)
+
+ def get_height(self):
+ return self.length_over_dim(1)
+
+ def get_depth(self):
+ return self.length_over_dim(2)
+
def get_x(self, direction=ORIGIN):
return self.get_coord(0, direction)
@@ -1735,27 +1981,27 @@ class Mobject(Container):
def get_start(self):
self.throw_error_if_no_points()
- if config["use_opengl_renderer"]:
- return np.array(self.data["points"][0])
- else:
- return np.array(self.points[0])
+ return np.array(self.points[0])
def get_end(self):
self.throw_error_if_no_points()
- if config["use_opengl_renderer"]:
- return np.array(self.data["points"][-1])
- else:
- return np.array(self.points[-1])
+ return np.array(self.points[-1])
def get_start_and_end(self):
return self.get_start(), self.get_end()
def point_from_proportion(self, alpha):
- raise NotImplementedError("Please override in a child class.")
+ points = self.get_points()
+ i, subalpha = integer_interpolate(0, len(points) - 1, alpha)
+ return interpolate(points[i], points[i + 1], subalpha)
+
+ def pfp(self, alpha):
+ """Abbreviation fo point_from_proportion"""
+ return self.point_from_proportion(alpha)
def get_pieces(self, n_pieces):
template = self.copy()
- template.submobjects = []
+ template.set_submobjects([])
alphas = np.linspace(0, 1, n_pieces + 1)
return Group(
*[
@@ -1775,6 +2021,11 @@ class Mobject(Container):
def has_no_points(self):
return not self.has_points()
+ def set_uniforms(self, uniforms):
+ for key in uniforms:
+ self.uniforms[key] = uniforms[key] # Copy?
+ return self
+
# Match other mobject properties
def match_color(self, mobject):
@@ -1850,7 +2101,7 @@ class Mobject(Container):
return result + self.submobjects
def get_family(self, recurse=True):
- if config["use_opengl_renderer"]:
+ if self.opengl:
if recurse:
return self.family
else:
@@ -1860,8 +2111,18 @@ class Mobject(Container):
all_mobjects = [self] + list(it.chain(*sub_families))
return remove_list_redundancies(all_mobjects)
+ def assemble_family(self):
+ if not self.opengl:
+ return
+ sub_families = (sm.get_family() for sm in self.submobjects)
+ self.family = [self, *it.chain(*sub_families)]
+ self.refresh_bounding_box()
+ for parent in self.parents:
+ parent.assemble_family()
+ return self
+
def family_members_with_points(self):
- return [m for m in self.get_family() if m.get_num_points() > 0]
+ return [m for m in self.get_family() if m.has_points()]
def arrange(
self,
@@ -1893,27 +2154,63 @@ class Mobject(Container):
self.center()
return self
- def arrange_in_grid(self, n_rows=None, n_cols=None, **kwargs):
+ def arrange_in_grid(
+ self,
+ n_rows=None,
+ n_cols=None,
+ buff=None,
+ h_buff=None,
+ v_buff=None,
+ buff_ratio=None,
+ h_buff_ratio=0.5,
+ v_buff_ratio=0.5,
+ aligned_edge=ORIGIN,
+ fill_rows_first=True,
+ ):
submobs = self.submobjects
if n_rows is None and n_cols is None:
- n_cols = int(np.sqrt(len(submobs)))
-
- if n_rows is not None:
- v1 = RIGHT
- v2 = DOWN
- n = len(submobs) // n_rows
- elif n_cols is not None:
- v1 = DOWN
- v2 = RIGHT
- n = len(submobs) // n_cols
- Group(
- *[
- Group(*submobs[i : i + n]).arrange(v1, **kwargs)
- for i in range(0, len(submobs), n)
- ]
- ).arrange(v2, **kwargs)
+ n_rows = int(np.sqrt(len(submobs)))
+ if n_rows is None:
+ n_rows = len(submobs) // n_cols
+ elif n_cols is None:
+ n_cols = len(submobs) // n_rows
+
+ if buff is not None:
+ h_buff = buff
+ v_buff = buff
+ else:
+ if buff_ratio is not None:
+ v_buff_ratio = buff_ratio
+ h_buff_ratio = buff_ratio
+ if h_buff is None:
+ h_buff = h_buff_ratio * self[0].get_width()
+ if v_buff is None:
+ v_buff = v_buff_ratio * self[0].get_height()
+
+ x_unit = h_buff + max([sm.get_width() for sm in submobs])
+ y_unit = v_buff + max([sm.get_height() for sm in submobs])
+
+ for index, sm in enumerate(submobs):
+ if fill_rows_first:
+ x, y = index % n_cols, index // n_cols
+ else:
+ x, y = index // n_rows, index % n_rows
+ sm.move_to(ORIGIN, aligned_edge)
+ sm.shift(x * x_unit * RIGHT + y * y_unit * DOWN)
+ self.center()
return self
+ def get_grid(self, n_rows, n_cols, height=None, **kwargs):
+ """
+ Returns a new mobject containing multiple copies of this one
+ arranged in a grid
+ """
+ grid = self.get_group_class()(*(self.copy() for n in range(n_rows * n_cols)))
+ grid.arrange_in_grid(n_rows, n_cols, **kwargs)
+ if height is not None:
+ grid.set_height(height)
+ return grid
+
def sort(self, point_to_num_func=lambda p: p[0], submob_func=None):
if submob_func is None:
submob_func = lambda m: point_to_num_func(m.get_center())
@@ -1944,12 +2241,29 @@ class Mobject(Container):
# Alignment
def align_data(self, mobject):
- self.null_point_align(mobject)
- self.align_submobjects(mobject)
- self.align_points(mobject)
- # Recurse
- for m1, m2 in zip(self.submobjects, mobject.submobjects):
- m1.align_data(m2)
+ if self.opengl:
+ # In case any data arrays get resized when aligned to shader data
+ # self.refresh_shader_data()
+ for mob1, mob2 in zip(self.get_family(), mobject.get_family()):
+ # Separate out how points are treated so that subclasses
+ # can handle that case differently if they choose
+ mob1.align_points(mob2)
+ for key in mob1.data.keys() & mob2.data.keys():
+ if key == "points":
+ continue
+ arr1 = mob1.data[key]
+ arr2 = mob2.data[key]
+ if len(arr2) > len(arr1):
+ mob1.data[key] = resize_preserving_order(arr1, len(arr2))
+ elif len(arr1) > len(arr2):
+ mob2.data[key] = resize_preserving_order(arr2, len(arr1))
+ else:
+ self.null_point_align(mobject)
+ self.align_submobjects(mobject)
+ self.align_points(mobject)
+ # Recurse
+ for m1, m2 in zip(self.submobjects, mobject.submobjects):
+ m1.align_data(m2)
def get_point_mobject(self, center=None):
"""The simplest mobject to be transformed to or from self.
@@ -1959,6 +2273,11 @@ class Mobject(Container):
raise NotImplementedError(msg)
def align_points(self, mobject):
+ if self.opengl:
+ max_len = max(self.get_num_points(), mobject.get_num_points())
+ for mob in (self, mobject):
+ mob.resize_points(max_len, resize_func=resize_preserving_order)
+ return self
count1 = self.get_num_points()
count2 = mobject.get_num_points()
if count1 < count2:
@@ -1992,7 +2311,7 @@ class Mobject(Container):
def push_self_into_submobjects(self):
copy = self.copy()
- copy.submobjects = []
+ copy.set_submobjects([])
self.reset_points()
self.add(copy)
return self
@@ -2004,8 +2323,13 @@ class Mobject(Container):
curr = len(self.submobjects)
if curr == 0:
# If empty, simply add n point mobjects
- self.submobjects = [self.get_point_mobject() for k in range(n)]
- return
+ if self.opengl:
+ null_mob = self.copy()
+ null_mob.set_points([self.get_center()])
+ self.set_submobjects([null_mob.copy() for k in range(n)])
+ else:
+ self.submobjects = [self.get_point_mobject() for k in range(n)]
+ return self
target = curr + n
# TODO, factor this out to utils so as to reuse
@@ -2016,10 +2340,26 @@ class Mobject(Container):
for submob, sf in zip(self.submobjects, split_factors):
new_submobs.append(submob)
for _ in range(1, sf):
+ if self.opengl:
+ new_submob = submob.copy()
+ # If the submobject is at all transparent, then
+ # make the copy completely transparent
+ if submob.get_opacity() < 1:
+ new_submob.set_opacity(0)
new_submobs.append(submob.copy().fade(1))
- self.submobjects = new_submobs
+ self.set_submobjects(new_submobs)
return self
+ def set_opacity(self, opacity, recurse=True):
+ self.set_rgba_array(color=None, opacity=opacity, recurse=False)
+ if recurse:
+ for submob in self.submobjects:
+ submob.set_opacity(opacity, recurse=True)
+ return self
+
+ def get_opacity(self):
+ return self.data["rgbas"][0, 3]
+
def repeat_submobject(self, submob):
return submob.copy()
@@ -2044,13 +2384,28 @@ class Mobject(Container):
self.add(dotL, dotR, dotMiddle)
"""
- if config["use_opengl_renderer"]:
- self.data["points"][:] = path_func(
- mobject1.data["points"], mobject2.data["points"], alpha
- )
+ if self.opengl:
+ for key in self.data:
+ if key in self.locked_data_keys:
+ continue
+ if len(self.data[key]) == 0:
+ continue
+ if key not in mobject1.data or key not in mobject2.data:
+ continue
+
+ if key in ("points", "bounding_box"):
+ func = path_func
+ else:
+ func = interpolate
+
+ self.data[key][:] = func(mobject1.data[key], mobject2.data[key], alpha)
+ for key in self.uniforms:
+ self.uniforms[key] = interpolate(
+ mobject1.uniforms[key], mobject2.uniforms[key], alpha
+ )
else:
self.points = path_func(mobject1.points, mobject2.points, alpha)
- self.interpolate_color(mobject1, mobject2, alpha)
+ self.interpolate_color(mobject1, mobject2, alpha)
return self
def interpolate_color(self, mobject1, mobject2, alpha):
@@ -2059,6 +2414,19 @@ class Mobject(Container):
def pointwise_become_partial(self, mobject, a, b):
raise NotImplementedError("Please override in a child class.")
+ def align_family(self, mobject):
+ mob1 = self
+ mob2 = mobject
+ n1 = len(mob1)
+ n2 = len(mob2)
+ if n1 != n2:
+ mob1.add_n_more_submobjects(max(0, n2 - n1))
+ mob2.add_n_more_submobjects(max(0, n1 - n2))
+ # Recurse
+ for sm1, sm2 in zip(mob1.submobjects, mob2.submobjects):
+ sm1.align_family(sm2)
+ return self
+
def become(self, mobject: "Mobject", copy_submobjects: bool = True):
"""Edit points, colors and submobjects to be identical
to another mobject
@@ -2076,26 +2444,270 @@ class Mobject(Container):
circ.become(square)
self.wait(0.5)
"""
- self.align_data(mobject)
- for sm1, sm2 in zip(self.get_family(), mobject.get_family()):
- sm1.points = np.array(sm2.points)
- sm1.interpolate_color(sm1, sm2, 1)
+ if self.opengl:
+ self.align_family(mobject)
+ for sm1, sm2 in zip(self.get_family(), mobject.get_family()):
+ sm1.set_data(sm2.data)
+ sm1.set_uniforms(sm2.uniforms)
+ self.refresh_bounding_box(recurse_down=True)
+ else:
+ self.align_data(mobject)
+ for sm1, sm2 in zip(self.get_family(), mobject.get_family()):
+ sm1.points = np.array(sm2.points)
+ sm1.interpolate_color(sm1, sm2, 1)
return self
+ # Locking data
+
+ def lock_data(self, keys):
+ """
+ To speed up some animations, particularly transformations,
+ it can be handy to acknowledge which pieces of data
+ won't change during the animation so that calls to
+ interpolate can skip this, and so that it's not
+ read into the shader_wrapper objects needlessly
+ """
+ if self.updaters:
+ return
+ # Be sure shader data has most up to date information
+ self.refresh_shader_data()
+ self.locked_data_keys = set(keys)
+
+ def lock_matching_data(self, mobject1, mobject2):
+ for sm, sm1, sm2 in zip(
+ self.get_family(), mobject1.get_family(), mobject2.get_family()
+ ):
+ keys = sm.data.keys() & sm1.data.keys() & sm2.data.keys()
+ sm.lock_data(
+ list(
+ filter(
+ lambda key: np.all(sm1.data[key] == sm2.data[key]),
+ keys,
+ )
+ )
+ )
+ return self
+
+ def unlock_data(self):
+ for mob in self.get_family():
+ mob.locked_data_keys = set()
+
# Errors
def throw_error_if_no_points(self):
- if config["use_opengl_renderer"]:
- if len(self.data["points"]) == 0:
- caller_name = sys._getframe(1).f_code.co_name
- raise Exception(
- f"Cannot call Mobject.{caller_name} for a Mobject with no points"
- )
- else:
- if self.has_no_points():
- caller_name = sys._getframe(1).f_code.co_name
- raise Exception(
- f"Cannot call Mobject.{caller_name} for a Mobject with no points"
- )
+ if self.has_no_points():
+ caller_name = sys._getframe(1).f_code.co_name
+ raise Exception(
+ f"Cannot call Mobject.{caller_name} for a Mobject with no points"
+ )
+
+ # Operations touching shader uniforms
+
+ @affects_shader_info_id
+ def fix_in_frame(self):
+ self.uniforms["is_fixed_in_frame"] = 1.0
+ return self
+
+ @affects_shader_info_id
+ def unfix_from_frame(self):
+ self.uniforms["is_fixed_in_frame"] = 0.0
+ return self
+
+ @affects_shader_info_id
+ def apply_depth_test(self):
+ self.depth_test = True
+ return self
+
+ @affects_shader_info_id
+ def deactivate_depth_test(self):
+ self.depth_test = False
+ return self
+
+ # Shader code manipulation
+
+ def replace_shader_code(self, old, new):
+ # TODO, will this work with VMobject structure, given
+ # that it does not simpler return shader_wrappers of
+ # family?
+ for wrapper in self.get_shader_wrapper_list():
+ wrapper.replace_code(old, new)
+ return self
+
+ def set_color_by_code(self, glsl_code):
+ """
+ Takes a snippet of code and inserts it into a
+ context which has the following variables:
+ vec4 color, vec3 point, vec3 unit_normal.
+ The code should change the color variable
+ """
+ self.replace_shader_code("///// INSERT COLOR FUNCTION HERE /////", glsl_code)
+ return self
+
+ # For shader data
+
+ # def refresh_shader_wrapper_id(self):
+ # self.shader_wrapper.refresh_id()
+ # return self
+
+ def get_shader_wrapper(self):
+ from ..renderer.shader_wrapper import ShaderWrapper
+
+ self.shader_wrapper = ShaderWrapper(
+ vert_data=self.get_shader_data(),
+ vert_indices=self.get_shader_vert_indices(),
+ uniforms=self.get_shader_uniforms(),
+ depth_test=self.depth_test,
+ texture_paths=self.texture_paths,
+ render_primitive=self.render_primitive,
+ shader_folder=self.__class__.shader_folder,
+ )
+ return self.shader_wrapper
+
+ def get_shader_wrapper_list(self):
+ shader_wrappers = it.chain(
+ [self.get_shader_wrapper()],
+ *[sm.get_shader_wrapper_list() for sm in self.submobjects],
+ )
+ batches = batch_by_property(shader_wrappers, lambda sw: sw.get_id())
+
+ result = []
+ for wrapper_group, _ in batches:
+ shader_wrapper = wrapper_group[0]
+ if not shader_wrapper.is_valid():
+ continue
+ shader_wrapper.combine_with(*wrapper_group[1:])
+ if len(shader_wrapper.vert_data) > 0:
+ result.append(shader_wrapper)
+ return result
+
+ def append_points(self, new_points):
+ self.points = np.vstack([self.points, new_points])
+ self.refresh_bounding_box()
+ return self
+
+ def clear_points(self):
+ self.resize_points(0)
+
+ def check_data_alignment(self, array, data_key):
+ # Makes sure that self.data[key] can be brodcast into
+ # the given array, meaning its length has to be either 1
+ # or the length of the array
+ d_len = len(self.data[data_key])
+ if d_len != 1 and d_len != len(array):
+ self.data[data_key] = resize_with_interpolation(
+ self.data[data_key], len(array)
+ )
+ return self
+
+ def get_resized_shader_data_array(self, length):
+ # If possible, try to populate an existing array, rather
+ # than recreating it each frame
+ shader_data = np.zeros(len(self.points), dtype=self.shader_dtype)
+ return shader_data
+
+ def read_data_to_shader(self, shader_data, shader_data_key, data_key):
+ if data_key in self.locked_data_keys:
+ return
+ self.check_data_alignment(shader_data, data_key)
+ shader_data[shader_data_key] = self.data[data_key]
+
+ def get_shader_data(self):
+ shader_data = self.get_resized_shader_data_array(self.get_num_points())
+ self.read_data_to_shader(shader_data, "point", "points")
+ return shader_data
+
+ def refresh_shader_data(self):
+ self.get_shader_data()
+
+ def get_shader_uniforms(self):
+ return self.uniforms
+
+ def get_shader_vert_indices(self):
+ return self.shader_indices
+
+ # Event Handlers
+ """
+ Event handling follows the Event Bubbling model of DOM in javascript.
+ Return false to stop the event bubbling.
+ To learn more visit https://www.quirksmode.org/js/events_order.html
+
+ Event Callback Argument is a callable function taking two arguments:
+ 1. Mobject
+ 2. EventData
+ """
+
+ def init_event_listners(self):
+ self.event_listners = []
+
+ def add_event_listner(self, event_type, event_callback):
+ event_listner = EventListner(self, event_type, event_callback)
+ self.event_listners.append(event_listner)
+ EVENT_DISPATCHER.add_listner(event_listner)
+ return self
+
+ def remove_event_listner(self, event_type, event_callback):
+ event_listner = EventListner(self, event_type, event_callback)
+ while event_listner in self.event_listners:
+ self.event_listners.remove(event_listner)
+ EVENT_DISPATCHER.remove_listner(event_listner)
+ return self
+
+ def clear_event_listners(self, recurse=True):
+ self.event_listners = []
+ if recurse:
+ for submob in self.submobjects:
+ submob.clear_event_listners(recurse=recurse)
+ return self
+
+ def get_event_listners(self):
+ return self.event_listners
+
+ def get_family_event_listners(self):
+ return list(it.chain(*[sm.get_event_listners() for sm in self.get_family()]))
+
+ def get_has_event_listner(self):
+ return any(mob.get_event_listners() for mob in self.get_family())
+
+ def add_mouse_motion_listner(self, callback):
+ self.add_event_listner(EventType.MouseMotionEvent, callback)
+
+ def remove_mouse_motion_listner(self, callback):
+ self.remove_event_listner(EventType.MouseMotionEvent, callback)
+
+ def add_mouse_press_listner(self, callback):
+ self.add_event_listner(EventType.MousePressEvent, callback)
+
+ def remove_mouse_press_listner(self, callback):
+ self.remove_event_listner(EventType.MousePressEvent, callback)
+
+ def add_mouse_release_listner(self, callback):
+ self.add_event_listner(EventType.MouseReleaseEvent, callback)
+
+ def remove_mouse_release_listner(self, callback):
+ self.remove_event_listner(EventType.MouseReleaseEvent, callback)
+
+ def add_mouse_drag_listner(self, callback):
+ self.add_event_listner(EventType.MouseDragEvent, callback)
+
+ def remove_mouse_drag_listner(self, callback):
+ self.remove_event_listner(EventType.MouseDragEvent, callback)
+
+ def add_mouse_scroll_listner(self, callback):
+ self.add_event_listner(EventType.MouseScrollEvent, callback)
+
+ def remove_mouse_scroll_listner(self, callback):
+ self.remove_event_listner(EventType.MouseScrollEvent, callback)
+
+ def add_key_press_listner(self, callback):
+ self.add_event_listner(EventType.KeyPressEvent, callback)
+
+ def remove_key_press_listner(self, callback):
+ self.remove_event_listner(EventType.KeyPressEvent, callback)
+
+ def add_key_release_listner(self, callback):
+ self.add_event_listner(EventType.KeyReleaseEvent, callback)
+
+ def remove_key_release_listner(self, callback):
+ self.remove_event_listner(EventType.KeyReleaseEvent, callback)
# About z-index
def set_z_index(self, z_index_value: Union[int, float]):
diff --git a/manim/mobject/opengl_geometry.py b/manim/mobject/opengl_geometry.py
index ae34c8fae..657c416e4 100644
--- a/manim/mobject/opengl_geometry.py
+++ b/manim/mobject/opengl_geometry.py
@@ -214,7 +214,7 @@ class OpenGLArc(OpenGLTipableVMobject):
self.arc_center = arc_center
OpenGLVMobject.__init__(self, **kwargs)
- def init_points(self):
+ def generate_points(self):
self.set_points(
OpenGLArc.create_quadratic_bezier_points(
angle=self.angle,
@@ -383,7 +383,7 @@ class OpenGLAnnularSector(OpenGLArc):
**kwargs
)
- def init_points(self):
+ def generate_points(self):
inner_arc, outer_arc = [
OpenGLArc(
start_angle=self.start_angle,
@@ -429,7 +429,7 @@ class OpenGLAnnulus(OpenGLCircle):
**kwargs
)
- def init_points(self):
+ def generate_points(self):
self.radius = self.outer_radius
outer_circle = OpenGLCircle(radius=self.outer_radius)
inner_circle = OpenGLCircle(radius=self.inner_radius)
@@ -447,7 +447,7 @@ class OpenGLLine(OpenGLTipableVMobject):
self.set_start_and_end_attrs(start, end)
super().__init__(**kwargs)
- def init_points(self):
+ def generate_points(self):
self.set_points_by_ends(self.start, self.end, self.buff, self.path_arc)
def set_points_by_ends(self, start, end, buff=0, path_arc=0):
@@ -460,7 +460,7 @@ class OpenGLLine(OpenGLTipableVMobject):
def set_path_arc(self, new_value):
self.path_arc = new_value
- self.init_points()
+ self.generate_points()
def account_for_buff(self, buff):
if buff == 0:
@@ -773,7 +773,7 @@ class OpenGLPolygon(OpenGLVMobject):
self.vertices = vertices
super().__init__(**kwargs)
- def init_points(self):
+ def generate_points(self):
verts = self.vertices
self.set_points_as_corners([*verts, verts[0]])
diff --git a/manim/mobject/opengl_mobject.py b/manim/mobject/opengl_mobject.py
index f84ccc682..0afcbd045 100644
--- a/manim/mobject/opengl_mobject.py
+++ b/manim/mobject/opengl_mobject.py
@@ -1,1570 +1,15 @@
-import copy
-import itertools as it
-import random
-import sys
-import moderngl
-from functools import wraps
-from ..utils.color import *
-
+from manim.mobject.mobject import Mobject
import numpy as np
+from ..constants import ORIGIN
-from .. import config
-from ..constants import *
-
-# from ..utils.iterables import batch_by_property
-from ..utils.iterables import list_update
-from ..utils.iterables import resize_array
-from ..utils.iterables import resize_preserving_order
-from ..utils.iterables import resize_with_interpolation
-from ..utils.iterables import make_even
-from ..utils.iterables import listify
-from ..utils.iterables import batch_by_property
-from ..utils.bezier import interpolate
-from ..utils.paths import straight_path
-from ..utils.simple_functions import get_parameters
-from ..utils.space_ops import angle_of_vector
-from ..utils.space_ops import get_norm
-from ..utils.space_ops import rotation_matrix_transpose
-
-
-class OpenGLMobject:
- """
- Mathematical Object
- """
-
- shader_dtype = [
- ("point", np.float32, (3,)),
- ]
- shader_folder = ""
- def __init__(
- self,
- color=WHITE,
- opacity=1,
- dim=3, # TODO, get rid of this
- # Lighting parameters
- # Positive gloss up to 1 makes it reflect the light.
- gloss=0.0,
- # Positive shadow up to 1 makes a side opposite the light darker
- shadow=0.0,
- # For shaders
- render_primitive=moderngl.TRIANGLE_STRIP,
- texture_paths=None,
- depth_test=False,
- # If true, the mobject will not get rotated according to camera position
- is_fixed_in_frame=False,
- # Must match in attributes of vert shader
- # Event listener
- listen_to_events=False,
- **kwargs
- ):
-
- self.color = color
- self.opacity = opacity
- self.dim = dim # TODO, get rid of this
- # Lighting parameters
- # Positive gloss up to 1 makes it reflect the light.
- self.gloss = gloss
- # Positive shadow up to 1 makes a side opposite the light darker
- self.shadow = shadow
- # For shaders
- self.render_primitive = render_primitive
- self.texture_paths = texture_paths
- self.depth_test = depth_test
- # If true, the mobject will not get rotated according to camera position
- self.is_fixed_in_frame = is_fixed_in_frame
- # Must match in attributes of vert shader
- # Event listener
- self.listen_to_events = listen_to_events
-
- self.submobjects = []
- self.parents = []
- self.family = [self]
- self.locked_data_keys = set()
- self.needs_new_bounding_box = True
-
- self.init_data()
- self.init_uniforms()
- self.init_updaters()
- # self.init_event_listners()
- self.init_points()
- self.init_colors()
-
- self.shader_indices = None
-
- if self.depth_test:
- self.apply_depth_test()
-
- def __str__(self):
- return self.__class__.__name__
-
- def init_data(self):
- self.data = {
- "points": np.zeros((0, 3)),
- "bounding_box": np.zeros((3, 3)),
- "rgbas": np.zeros((1, 4)),
- }
-
- def init_uniforms(self):
- self.uniforms = {
- "is_fixed_in_frame": float(self.is_fixed_in_frame),
- "gloss": self.gloss,
- "shadow": self.shadow,
- }
-
- def init_colors(self):
- self.set_color(self.color, self.opacity)
-
- def init_points(self):
- # Typically implemented in subclass, unlpess purposefully left blank
- pass
-
- def set_data(self, data):
- for key in data:
- self.data[key] = data[key].copy()
- return self
-
- def set_uniforms(self, uniforms):
- for key in uniforms:
- self.uniforms[key] = uniforms[key] # Copy?
- return self
-
- @property
- def animate(self):
- # Borrowed from https://github.com/ManimCommunity/manim/
- return _AnimationBuilder(self)
-
- # Only these methods should directly affect points
-
- def resize_points(self, new_length, resize_func=resize_array):
- if new_length != len(self.data["points"]):
- self.data["points"] = resize_func(self.data["points"], new_length)
- self.refresh_bounding_box()
- return self
-
- def set_points(self, points):
- if len(points) == len(self.data["points"]):
- self.data["points"][:] = points
- elif isinstance(points, np.ndarray):
- self.data["points"] = points.copy()
- else:
- self.data["points"] = np.array(points)
- self.refresh_bounding_box()
- return self
-
- def append_points(self, new_points):
- self.data["points"] = np.vstack([self.data["points"], new_points])
- self.refresh_bounding_box()
- return self
-
- def reverse_points(self):
- for mob in self.get_family():
- for key in mob.data:
- mob.data[key] = mob.data[key][::-1]
- return self
-
- def apply_points_function(
- self, func, about_point=None, about_edge=ORIGIN, works_on_bounding_box=False
- ):
- if about_point is None and about_edge is not None:
- about_point = self.get_bounding_box_point(about_edge)
-
- for mob in self.get_family():
- arrs = []
- if mob.has_points():
- arrs.append(mob.get_points())
- if works_on_bounding_box:
- arrs.append(mob.get_bounding_box())
-
- for arr in arrs:
- if about_point is None:
- arr[:] = func(arr)
- else:
- arr[:] = func(arr - about_point) + about_point
-
- if not works_on_bounding_box:
- self.refresh_bounding_box(recurse_down=True)
- else:
- for parent in self.parents:
- parent.refresh_bounding_box()
- return self
-
- # Others related to points
-
- def match_points(self, mobject):
- self.set_points(mobject.get_points())
-
- def get_points(self):
- return self.data["points"]
-
- def clear_points(self):
- self.resize_points(0)
-
- def get_num_points(self):
- return len(self.data["points"])
-
- def get_all_points(self):
- if self.submobjects:
- return np.vstack([sm.get_points() for sm in self.get_family()])
- else:
- return self.get_points()
-
- def has_points(self):
- return self.get_num_points() > 0
-
- def get_bounding_box(self):
- if self.needs_new_bounding_box:
- self.data["bounding_box"] = self.compute_bounding_box()
- self.needs_new_bounding_box = False
- return self.data["bounding_box"]
-
- def compute_bounding_box(self):
- all_points = np.vstack(
- [
- self.get_points(),
- *(
- mob.get_bounding_box()
- for mob in self.get_family()[1:]
- if mob.has_points()
- ),
- ]
- )
- if len(all_points) == 0:
- return np.zeros((3, self.dim))
- else:
- # Lower left and upper right corners
- mins = all_points.min(0)
- maxs = all_points.max(0)
- mids = (mins + maxs) / 2
- return np.array([mins, mids, maxs])
-
- def refresh_bounding_box(self, recurse_down=False, recurse_up=True):
- for mob in self.get_family(recurse_down):
- mob.needs_new_bounding_box = True
- if recurse_up:
- for parent in self.parents:
- parent.refresh_bounding_box()
- return self
-
- def is_point_touching(self, point, buff=MED_SMALL_BUFF):
- bb = self.get_bounding_box()
- mins = bb[0] - buff
- maxs = bb[2] + buff
- return (point >= mins).all() and (point <= maxs).all()
-
- # Family matters
-
- def __getitem__(self, value):
- if isinstance(value, slice):
- GroupClass = self.get_group_class()
- return GroupClass(*self.split().__getitem__(value))
- return self.split().__getitem__(value)
-
- def __iter__(self):
- return iter(self.split())
-
- def __len__(self):
- return len(self.split())
-
- def split(self):
- return self.submobjects
-
- def assemble_family(self):
- sub_families = (sm.get_family() for sm in self.submobjects)
- self.family = [self, *it.chain(*sub_families)]
- self.refresh_has_updater_status()
- self.refresh_bounding_box()
- for parent in self.parents:
- parent.assemble_family()
- return self
-
- def get_family(self, recurse=True):
- if recurse:
- return self.family
- else:
- return [self]
-
- def family_members_with_points(self):
- return [m for m in self.get_family() if m.has_points()]
-
- def add(self, *mobjects):
- if self in mobjects:
- raise Exception("Mobject cannot contain self")
- for mobject in mobjects:
- if mobject not in self.submobjects:
- self.submobjects.append(mobject)
- if self not in mobject.parents:
- mobject.parents.append(self)
- self.assemble_family()
- return self
-
- def remove(self, *mobjects):
- for mobject in mobjects:
- if mobject in self.submobjects:
- self.submobjects.remove(mobject)
- if self in mobject.parents:
- mobject.parents.remove(self)
- self.assemble_family()
- return self
-
- def add_to_back(self, *mobjects):
- self.set_submobjects(list_update(mobjects, self.submobjects))
- return self
-
- def replace_submobject(self, index, new_submob):
- old_submob = self.submobjects[index]
- if self in old_submob.parents:
- old_submob.parents.remove(self)
- self.submobjects[index] = new_submob
- self.assemble_family()
- return self
-
- def set_submobjects(self, submobject_list):
- self.remove(*self.submobjects)
- self.add(*submobject_list)
- return self
-
- def digest_mobject_attrs(self):
- """
- Ensures all attributes which are mobjects are included
- in the submobjects list.
- """
- mobject_attrs = [
- x for x in list(self.__dict__.values()) if isinstance(x, OpenGLMobject)
- ]
- self.set_submobjects(list_update(self.submobjects, mobject_attrs))
- return self
-
- # Submobject organization
-
- def arrange(self, direction=RIGHT, center=True, **kwargs):
- for m1, m2 in zip(self.submobjects, self.submobjects[1:]):
- m2.next_to(m1, direction, **kwargs)
- if center:
- self.center()
- return self
-
- def arrange_in_grid(
- self,
- n_rows=None,
- n_cols=None,
- buff=None,
- h_buff=None,
- v_buff=None,
- buff_ratio=None,
- h_buff_ratio=0.5,
- v_buff_ratio=0.5,
- aligned_edge=ORIGIN,
- fill_rows_first=True,
- ):
- submobs = self.submobjects
- if n_rows is None and n_cols is None:
- n_rows = int(np.sqrt(len(submobs)))
- if n_rows is None:
- n_rows = len(submobs) // n_cols
- if n_cols is None:
- n_cols = len(submobs) // n_rows
-
- if buff is not None:
- h_buff = buff
- v_buff = buff
- else:
- if buff_ratio is not None:
- v_buff_ratio = buff_ratio
- h_buff_ratio = buff_ratio
- if h_buff is None:
- h_buff = h_buff_ratio * self[0].get_width()
- if v_buff is None:
- v_buff = v_buff_ratio * self[0].get_height()
-
- x_unit = h_buff + max([sm.get_width() for sm in submobs])
- y_unit = v_buff + max([sm.get_height() for sm in submobs])
-
- for index, sm in enumerate(submobs):
- if fill_rows_first:
- x, y = index % n_cols, index // n_cols
- else:
- x, y = index // n_rows, index % n_rows
- sm.move_to(ORIGIN, aligned_edge)
- sm.shift(x * x_unit * RIGHT + y * y_unit * DOWN)
- self.center()
- return self
-
- def get_grid(self, n_rows, n_cols, height=None, **kwargs):
- """
- Returns a new mobject containing multiple copies of this one
- arranged in a grid
- """
- grid = self.get_group_class()(*(self.copy() for n in range(n_rows * n_cols)))
- grid.arrange_in_grid(n_rows, n_cols, **kwargs)
- if height is not None:
- grid.set_height(height)
- return grid
-
- def sort(self, point_to_num_func=lambda p: p[0], submob_func=None):
- if submob_func is not None:
- self.submobjects.sort(key=submob_func)
- else:
- self.submobjects.sort(key=lambda m: point_to_num_func(m.get_center()))
- return self
-
- def shuffle(self, recurse=False):
- if recurse:
- for submob in self.submobjects:
- submob.shuffle(recurse=True)
- random.shuffle(self.submobjects)
- return self
-
- # Copying
-
- def copy(self):
- # TODO, either justify reason for shallow copy, or
- # remove this redundancy everywhere
- # return self.deepcopy()
-
- parents = self.parents
- self.parents = []
- copy_mobject = copy.copy(self)
- self.parents = parents
-
- copy_mobject.data = dict(self.data)
- for key in self.data:
- copy_mobject.data[key] = self.data[key].copy()
-
- # TODO, are uniforms ever numpy arrays?
- copy_mobject.uniforms = dict(self.uniforms)
-
- copy_mobject.submobjects = []
- copy_mobject.add(*[sm.copy() for sm in self.submobjects])
- copy_mobject.match_updaters(self)
-
- copy_mobject.needs_new_bounding_box = self.needs_new_bounding_box
-
- # Make sure any mobject or numpy array attributes are copied
- family = self.get_family()
- for attr, value in list(self.__dict__.items()):
- if (
- isinstance(value, OpenGLMobject)
- and value in family
- and value is not self
- ):
- setattr(copy_mobject, attr, value.copy())
- if isinstance(value, np.ndarray):
- setattr(copy_mobject, attr, value.copy())
- # if isinstance(value, ShaderWrapper):
- # setattr(copy_mobject, attr, value.copy())
- return copy_mobject
-
- def deepcopy(self):
- parents = self.parents
- self.parents = []
- result = copy.deepcopy(self)
- self.parents = parents
- return result
-
- def generate_target(self, use_deepcopy=False):
- self.target = None # Prevent exponential explosion
- if use_deepcopy:
- self.target = self.deepcopy()
- else:
- self.target = self.copy()
- return self.target
-
- def save_state(self, use_deepcopy=False):
- if hasattr(self, "saved_state"):
- # Prevent exponential growth of data
- self.saved_state = None
- if use_deepcopy:
- self.saved_state = self.deepcopy()
- else:
- self.saved_state = self.copy()
- return self
-
- def restore(self):
- if not hasattr(self, "saved_state") or self.save_state is None:
- raise Exception("Trying to restore without having saved")
- self.become(self.saved_state)
- return self
-
- # Updating
-
- def init_updaters(self):
- self.time_based_updaters = []
- self.non_time_updaters = []
- self.has_updaters = False
- self.updating_suspended = False
-
- def update(self, dt=0, recurse=True):
- if not self.has_updaters or self.updating_suspended:
- return self
- for updater in self.time_based_updaters:
- updater(self, dt)
- for updater in self.non_time_updaters:
- updater(self)
- if recurse:
- for submob in self.submobjects:
- submob.update(dt, recurse)
- return self
-
- def get_time_based_updaters(self):
- return self.time_based_updaters
-
- def has_time_based_updater(self):
- return len(self.time_based_updaters) > 0
-
- def get_updaters(self):
- return self.time_based_updaters + self.non_time_updaters
-
- def get_family_updaters(self):
- return list(it.chain(*[sm.get_updaters() for sm in self.get_family()]))
-
- def add_updater(self, update_function, index=None, call_updater=True):
- if "dt" in get_parameters(update_function):
- updater_list = self.time_based_updaters
- else:
- updater_list = self.non_time_updaters
-
- if index is None:
- updater_list.append(update_function)
- else:
- updater_list.insert(index, update_function)
-
- self.refresh_has_updater_status()
- if call_updater:
- self.update()
- return self
-
- def remove_updater(self, update_function):
- for updater_list in [self.time_based_updaters, self.non_time_updaters]:
- while update_function in updater_list:
- updater_list.remove(update_function)
- self.refresh_has_updater_status()
- return self
-
- def clear_updaters(self, recurse=True):
- self.time_based_updaters = []
- self.non_time_updaters = []
- self.refresh_has_updater_status()
- if recurse:
- for submob in self.submobjects:
- submob.clear_updaters()
- return self
-
- def match_updaters(self, mobject):
- self.clear_updaters()
- for updater in mobject.get_updaters():
- self.add_updater(updater)
- return self
-
- def suspend_updating(self, recurse=True):
- self.updating_suspended = True
- if recurse:
- for submob in self.submobjects:
- submob.suspend_updating(recurse)
- return self
-
- def resume_updating(self, recurse=True, call_updater=True):
- self.updating_suspended = False
- if recurse:
- for submob in self.submobjects:
- submob.resume_updating(recurse)
- for parent in self.parents:
- parent.resume_updating(recurse=False, call_updater=False)
- if call_updater:
- self.update(dt=0, recurse=recurse)
- return self
-
- def refresh_has_updater_status(self):
- self.has_updaters = any(mob.get_updaters() for mob in self.get_family())
- return self
-
- # Transforming operations
-
- def shift(self, vector):
- self.apply_points_function(
- lambda points: points + vector,
- about_edge=None,
- works_on_bounding_box=True,
- )
- return self
-
- def scale(self, scale_factor, **kwargs):
- """
- Default behavior is to scale about the center of the mobject.
- The argument about_edge can be a vector, indicating which side of
- the mobject to scale about, e.g., mob.scale(about_edge = RIGHT)
- scales about mob.get_right().
-
- Otherwise, if about_point is given a value, scaling is done with
- respect to that point.
- """
- self.apply_points_function(
- lambda points: scale_factor * points, works_on_bounding_box=True, **kwargs
- )
- return self
-
- def stretch(self, factor, dim, **kwargs):
- def func(points):
- points[:, dim] *= factor
- return points
-
- self.apply_points_function(func, works_on_bounding_box=True, **kwargs)
- return self
-
- def rotate_about_origin(self, angle, axis=OUT):
- return self.rotate(angle, axis, about_point=ORIGIN)
-
- def rotate(self, angle, axis=OUT, **kwargs):
- rot_matrix_T = rotation_matrix_transpose(angle, axis)
- self.apply_points_function(
- lambda points: np.dot(points, rot_matrix_T), **kwargs
- )
- return self
-
- def flip(self, axis=UP, **kwargs):
- return self.rotate(TAU / 2, axis, **kwargs)
-
- def apply_function(self, function, **kwargs):
- # Default to applying matrix about the origin, not mobjects center
- if len(kwargs) == 0:
- kwargs["about_point"] = ORIGIN
- self.apply_points_function(
- lambda points: np.array([function(p) for p in points]), **kwargs
- )
- return self
-
- def apply_function_to_position(self, function):
- self.move_to(function(self.get_center()))
- return self
-
- def apply_function_to_submobject_positions(self, function):
- for submob in self.submobjects:
- submob.apply_function_to_position(function)
- return self
-
- def apply_matrix(self, matrix, **kwargs):
- # Default to applying matrix about the origin, not mobjects center
- if ("about_point" not in kwargs) and ("about_edge" not in kwargs):
- kwargs["about_point"] = ORIGIN
- full_matrix = np.identity(self.dim)
- matrix = np.array(matrix)
- full_matrix[: matrix.shape[0], : matrix.shape[1]] = matrix
- self.apply_points_function(
- lambda points: np.dot(points, full_matrix.T), **kwargs
- )
- return self
-
- def apply_complex_function(self, function, **kwargs):
- def R3_func(point):
- x, y, z = point
- xy_complex = function(complex(x, y))
- return [xy_complex.real, xy_complex.imag, z]
-
- return self.apply_function(R3_func)
-
- def wag(self, direction=RIGHT, axis=DOWN, wag_factor=1.0):
- for mob in self.family_members_with_points():
- alphas = np.dot(mob.get_points(), np.transpose(axis))
- alphas -= min(alphas)
- alphas /= max(alphas)
- alphas = alphas ** wag_factor
- mob.set_points(
- mob.get_points()
- + np.dot(
- alphas.reshape((len(alphas), 1)),
- np.array(direction).reshape((1, mob.dim)),
- )
- )
- return self
-
- # Positioning methods
-
- def center(self):
- self.shift(-self.get_center())
- return self
-
- def align_on_border(self, direction, buff=DEFAULT_MOBJECT_TO_EDGE_BUFFER):
- """
- Direction just needs to be a vector pointing towards side or
- corner in the 2d plane.
- """
- target_point = np.sign(direction) * (
- config["frame_x_radius"],
- config["frame_y_radius"],
- 0,
- )
- point_to_align = self.get_bounding_box_point(direction)
- shift_val = target_point - point_to_align - buff * np.array(direction)
- shift_val = shift_val * abs(np.sign(direction))
- self.shift(shift_val)
- return self
-
- def to_corner(self, corner=LEFT + DOWN, buff=DEFAULT_MOBJECT_TO_EDGE_BUFFER):
- return self.align_on_border(corner, buff)
-
- def to_edge(self, edge=LEFT, buff=DEFAULT_MOBJECT_TO_EDGE_BUFFER):
- return self.align_on_border(edge, buff)
-
- def next_to(
- self,
- mobject_or_point,
- direction=RIGHT,
- buff=DEFAULT_MOBJECT_TO_MOBJECT_BUFFER,
- aligned_edge=ORIGIN,
- submobject_to_align=None,
- index_of_submobject_to_align=None,
- coor_mask=np.array([1, 1, 1]),
- ):
- if isinstance(mobject_or_point, OpenGLMobject):
- mob = mobject_or_point
- if index_of_submobject_to_align is not None:
- target_aligner = mob[index_of_submobject_to_align]
- else:
- target_aligner = mob
- target_point = target_aligner.get_bounding_box_point(
- aligned_edge + direction
- )
- else:
- target_point = mobject_or_point
- if submobject_to_align is not None:
- aligner = submobject_to_align
- elif index_of_submobject_to_align is not None:
- aligner = self[index_of_submobject_to_align]
- else:
- aligner = self
- point_to_align = aligner.get_bounding_box_point(aligned_edge - direction)
- self.shift((target_point - point_to_align + buff * direction) * coor_mask)
- return self
-
- def shift_onto_screen(self, **kwargs):
- space_lengths = [config["frame_x_radius"], config["frame_y_radius"]]
- for vect in UP, DOWN, LEFT, RIGHT:
- dim = np.argmax(np.abs(vect))
- buff = kwargs.get("buff", DEFAULT_MOBJECT_TO_EDGE_BUFFER)
- max_val = space_lengths[dim] - buff
- edge_center = self.get_edge_center(vect)
- if np.dot(edge_center, vect) > max_val:
- self.to_edge(vect, **kwargs)
- return self
-
- def is_off_screen(self):
- if self.get_left()[0] > config["frame_x_radius"]:
- return True
- if self.get_right()[0] < -config["frame_x_radius"]:
- return True
- if self.get_bottom()[1] > config["frame_y_radius"]:
- return True
- if self.get_top()[1] < -config["frame_y_radius"]:
- return True
- return False
-
- def stretch_about_point(self, factor, dim, point):
- return self.stretch(factor, dim, about_point=point)
-
- def stretch_in_place(self, factor, dim):
- # Now redundant with stretch
- return self.stretch(factor, dim)
-
- def rescale_to_fit(self, length, dim, stretch=False, **kwargs):
- old_length = self.length_over_dim(dim)
- if old_length == 0:
- return self
- if stretch:
- self.stretch(length / old_length, dim, **kwargs)
- else:
- self.scale(length / old_length, **kwargs)
- return self
-
- def stretch_to_fit_width(self, width, **kwargs):
- return self.rescale_to_fit(width, 0, stretch=True, **kwargs)
-
- def stretch_to_fit_height(self, height, **kwargs):
- return self.rescale_to_fit(height, 1, stretch=True, **kwargs)
-
- def stretch_to_fit_depth(self, depth, **kwargs):
- return self.rescale_to_fit(depth, 1, stretch=True, **kwargs)
-
- def set_width(self, width, stretch=False, **kwargs):
- return self.rescale_to_fit(width, 0, stretch=stretch, **kwargs)
-
- def set_height(self, height, stretch=False, **kwargs):
- return self.rescale_to_fit(height, 1, stretch=stretch, **kwargs)
-
- def set_depth(self, depth, stretch=False, **kwargs):
- return self.rescale_to_fit(depth, 2, stretch=stretch, **kwargs)
-
- def set_coord(self, value, dim, direction=ORIGIN):
- curr = self.get_coord(dim, direction)
- shift_vect = np.zeros(self.dim)
- shift_vect[dim] = value - curr
- self.shift(shift_vect)
- return self
-
- def set_x(self, x, direction=ORIGIN):
- return self.set_coord(x, 0, direction)
-
- def set_y(self, y, direction=ORIGIN):
- return self.set_coord(y, 1, direction)
-
- def set_z(self, z, direction=ORIGIN):
- return self.set_coord(z, 2, direction)
-
- def space_out_submobjects(self, factor=1.5, **kwargs):
- self.scale(factor, **kwargs)
- for submob in self.submobjects:
- submob.scale(1.0 / factor)
- return self
-
- def move_to(
- self, point_or_mobject, aligned_edge=ORIGIN, coor_mask=np.array([1, 1, 1])
- ):
- if isinstance(point_or_mobject, OpenGLMobject):
- target = point_or_mobject.get_bounding_box_point(aligned_edge)
- else:
- target = point_or_mobject
- point_to_align = self.get_bounding_box_point(aligned_edge)
- self.shift((target - point_to_align) * coor_mask)
- return self
-
- def replace(self, mobject, dim_to_match=0, stretch=False):
- if not mobject.get_num_points() and not mobject.submobjects:
- self.scale(0)
- return self
- if stretch:
- for i in range(self.dim):
- self.rescale_to_fit(mobject.length_over_dim(i), i, stretch=True)
- else:
- self.rescale_to_fit(
- mobject.length_over_dim(dim_to_match), dim_to_match, stretch=False
- )
- self.shift(mobject.get_center() - self.get_center())
- return self
-
- def surround(self, mobject, dim_to_match=0, stretch=False, buff=MED_SMALL_BUFF):
- self.replace(mobject, dim_to_match, stretch)
- length = mobject.length_over_dim(dim_to_match)
- self.scale((length + buff) / length)
- return self
-
- def put_start_and_end_on(self, start, end):
- # TODO, this doesn't currently work in 3d
- curr_start, curr_end = self.get_start_and_end()
- curr_vect = curr_end - curr_start
- if np.all(curr_vect == 0):
- raise Exception("Cannot position endpoints of closed loop")
- target_vect = end - start
- self.scale(
- get_norm(target_vect) / get_norm(curr_vect),
- about_point=curr_start,
- )
- self.rotate(
- angle_of_vector(target_vect) - angle_of_vector(curr_vect),
- about_point=curr_start,
- )
- self.shift(start - curr_start)
- return self
-
- # Color functions
-
- def set_rgba_array(self, color=None, opacity=None, name="rgbas", recurse=True):
- if color is not None:
- rgbs = np.array([color_to_rgb(c) for c in listify(color)])
- if opacity is not None:
- opacities = listify(opacity)
-
- # Color only
- if color is not None and opacity is None:
- for mob in self.get_family(recurse):
- mob.data[name] = resize_array(mob.data[name], len(rgbs))
- mob.data[name][:, :3] = rgbs
-
- # Opacity only
- if color is None and opacity is not None:
- for mob in self.get_family(recurse):
- mob.data[name] = resize_array(mob.data[name], len(opacities))
- mob.data[name][:, 3] = opacities
-
- # Color and opacity
- if color is not None and opacity is not None:
- rgbas = np.array([[*rgb, o] for rgb, o in zip(*make_even(rgbs, opacities))])
- for mob in self.get_family(recurse):
- mob.data[name] = rgbas.copy()
- return self
-
- def set_color(self, color, opacity=None, recurse=True):
- self.set_rgba_array(color, opacity, recurse=False)
- # Recurse to submobjects differently from how set_rgba_array
- # in case they implement set_color differently
- if recurse:
- for submob in self.submobjects:
- submob.set_color(color, recurse=True)
- return self
-
- def set_opacity(self, opacity, recurse=True):
- self.set_rgba_array(color=None, opacity=opacity, recurse=False)
- if recurse:
- for submob in self.submobjects:
- submob.set_opacity(opacity, recurse=True)
- return self
-
- def get_color(self):
- return rgb_to_hex(self.data["rgbas"][0, :3])
-
- def get_opacity(self):
- return self.data["rgbas"][0, 3]
-
- def set_color_by_gradient(self, *colors):
- self.set_submobject_colors_by_gradient(*colors)
- return self
-
- def set_submobject_colors_by_gradient(self, *colors):
- if len(colors) == 0:
- raise Exception("Need at least one color")
- elif len(colors) == 1:
- return self.set_color(*colors)
-
- # mobs = self.family_members_with_points()
- mobs = self.submobjects
- new_colors = color_gradient(colors, len(mobs))
-
- for mob, color in zip(mobs, new_colors):
- mob.set_color(color)
- return self
-
- def fade(self, darkness=0.5, recurse=True):
- self.set_opacity(1.0 - darkness, recurse=recurse)
-
- def get_gloss(self):
- return self.uniforms["gloss"]
-
- def set_gloss(self, gloss, recurse=True):
- for mob in self.get_family(recurse):
- mob.uniforms["gloss"] = gloss
- return self
-
- def get_shadow(self):
- return self.uniforms["shadow"]
-
- def set_shadow(self, shadow, recurse=True):
- for mob in self.get_family(recurse):
- mob.uniforms["shadow"] = shadow
- return self
-
- # Background rectangle
-
- def add_background_rectangle(self, color=None, opacity=0.75, **kwargs):
- # TODO, this does not behave well when the mobject has points,
- # since it gets displayed on top
- from manimlib.mobject.shape_matchers import BackgroundRectangle
-
- self.background_rectangle = BackgroundRectangle(
- self, color=color, fill_opacity=opacity, **kwargs
- )
- self.add_to_back(self.background_rectangle)
- return self
-
- def add_background_rectangle_to_submobjects(self, **kwargs):
- for submobject in self.submobjects:
- submobject.add_background_rectangle(**kwargs)
- return self
-
- def add_background_rectangle_to_family_members_with_points(self, **kwargs):
- for mob in self.family_members_with_points():
- mob.add_background_rectangle(**kwargs)
- return self
-
- # Getters
-
- def get_bounding_box_point(self, direction):
- bb = self.get_bounding_box()
- indices = (np.sign(direction) + 1).astype(int)
- return np.array([bb[indices[i]][i] for i in range(3)])
-
- def get_edge_center(self, direction):
- return self.get_bounding_box_point(direction)
-
- def get_corner(self, direction):
- return self.get_bounding_box_point(direction)
-
- def get_center(self):
- return self.get_bounding_box()[1]
-
- def get_center_of_mass(self):
- return self.get_all_points().mean(0)
-
- def get_boundary_point(self, direction):
- all_points = self.get_all_points()
- boundary_directions = all_points - self.get_center()
- norms = np.linalg.norm(boundary_directions, axis=1)
- boundary_directions /= np.repeat(norms, 3).reshape((len(norms), 3))
- index = np.argmax(np.dot(boundary_directions, np.array(direction).T))
- return all_points[index]
-
- def get_continuous_bounding_box_point(self, direction):
- dl, center, ur = self.get_bounding_box()
- corner_vect = ur - center
- return center + direction / np.max(
- np.abs(
- np.true_divide(
- direction,
- corner_vect,
- out=np.zeros(len(direction)),
- where=((corner_vect) != 0),
- )
- )
- )
-
- def get_top(self):
- return self.get_edge_center(UP)
-
- def get_bottom(self):
- return self.get_edge_center(DOWN)
-
- def get_right(self):
- return self.get_edge_center(RIGHT)
-
- def get_left(self):
- return self.get_edge_center(LEFT)
-
- def get_zenith(self):
- return self.get_edge_center(OUT)
-
- def get_nadir(self):
- return self.get_edge_center(IN)
-
- def length_over_dim(self, dim):
- bb = self.get_bounding_box()
- return abs((bb[2] - bb[0])[dim])
-
- def get_width(self):
- return self.length_over_dim(0)
-
- def get_height(self):
- return self.length_over_dim(1)
-
- def get_depth(self):
- return self.length_over_dim(2)
-
- def get_coord(self, dim, direction=ORIGIN):
- """
- Meant to generalize get_x, get_y, get_z
- """
- return self.get_bounding_box_point(direction)[dim]
-
- def get_x(self, direction=ORIGIN):
- return self.get_coord(0, direction)
-
- def get_y(self, direction=ORIGIN):
- return self.get_coord(1, direction)
-
- def get_z(self, direction=ORIGIN):
- return self.get_coord(2, direction)
-
- def get_start(self):
- self.throw_error_if_no_points()
- return np.array(self.get_points()[0])
-
- def get_end(self):
- self.throw_error_if_no_points()
- return np.array(self.get_points()[-1])
-
- def get_start_and_end(self):
- return self.get_start(), self.get_end()
-
- def point_from_proportion(self, alpha):
- points = self.get_points()
- i, subalpha = integer_interpolate(0, len(points) - 1, alpha)
- return interpolate(points[i], points[i + 1], subalpha)
-
- def pfp(self, alpha):
- """Abbreviation fo point_from_proportion"""
- return self.point_from_proportion(alpha)
-
- def get_pieces(self, n_pieces):
- template = self.copy()
- template.set_submobjects([])
- alphas = np.linspace(0, 1, n_pieces + 1)
- return OpenGLGroup(
- *[
- template.copy().pointwise_become_partial(self, a1, a2)
- for a1, a2 in zip(alphas[:-1], alphas[1:])
- ]
- )
-
- def get_z_index_reference_point(self):
- # TODO, better place to define default z_index_group?
- z_index_group = getattr(self, "z_index_group", self)
- return z_index_group.get_center()
-
- # Match other mobject properties
-
- def match_color(self, mobject):
- return self.set_color(mobject.get_color())
-
- def match_dim_size(self, mobject, dim, **kwargs):
- return self.rescale_to_fit(mobject.length_over_dim(dim), dim, **kwargs)
-
- def match_width(self, mobject, **kwargs):
- return self.match_dim_size(mobject, 0, **kwargs)
-
- def match_height(self, mobject, **kwargs):
- return self.match_dim_size(mobject, 1, **kwargs)
-
- def match_depth(self, mobject, **kwargs):
- return self.match_dim_size(mobject, 2, **kwargs)
-
- def match_coord(self, mobject, dim, direction=ORIGIN):
- return self.set_coord(
- mobject.get_coord(dim, direction),
- dim=dim,
- direction=direction,
- )
-
- def match_x(self, mobject, direction=ORIGIN):
- return self.match_coord(mobject, 0, direction)
-
- def match_y(self, mobject, direction=ORIGIN):
- return self.match_coord(mobject, 1, direction)
-
- def match_z(self, mobject, direction=ORIGIN):
- return self.match_coord(mobject, 2, direction)
-
- def align_to(self, mobject_or_point, direction=ORIGIN):
- """
- Examples:
- mob1.align_to(mob2, UP) moves mob1 vertically so that its
- top edge lines ups with mob2's top edge.
-
- mob1.align_to(mob2, alignment_vect = RIGHT) moves mob1
- horizontally so that it's center is directly above/below
- the center of mob2
- """
- if isinstance(mobject_or_point, OpenGLMobject):
- point = mobject_or_point.get_bounding_box_point(direction)
- else:
- point = mobject_or_point
-
- for dim in range(self.dim):
- if direction[dim] != 0:
- self.set_coord(point[dim], dim, direction)
- return self
-
- def get_group_class(self):
- return OpenGLGroup
-
- # Alignment
-
- def align_data_and_family(self, mobject):
- self.align_family(mobject)
- self.align_data(mobject)
-
- def align_data(self, mobject):
- # In case any data arrays get resized when aligned to shader data
- # self.refresh_shader_data()
- for mob1, mob2 in zip(self.get_family(), mobject.get_family()):
- # Separate out how points are treated so that subclasses
- # can handle that case differently if they choose
- mob1.align_points(mob2)
- for key in mob1.data.keys() & mob2.data.keys():
- if key == "points":
- continue
- arr1 = mob1.data[key]
- arr2 = mob2.data[key]
- if len(arr2) > len(arr1):
- mob1.data[key] = resize_preserving_order(arr1, len(arr2))
- elif len(arr1) > len(arr2):
- mob2.data[key] = resize_preserving_order(arr2, len(arr1))
-
- def align_points(self, mobject):
- max_len = max(self.get_num_points(), mobject.get_num_points())
- for mob in (self, mobject):
- mob.resize_points(max_len, resize_func=resize_preserving_order)
- return self
-
- def align_family(self, mobject):
- mob1 = self
- mob2 = mobject
- n1 = len(mob1)
- n2 = len(mob2)
- if n1 != n2:
- mob1.add_n_more_submobjects(max(0, n2 - n1))
- mob2.add_n_more_submobjects(max(0, n1 - n2))
- # Recurse
- for sm1, sm2 in zip(mob1.submobjects, mob2.submobjects):
- sm1.align_family(sm2)
- return self
-
- def push_self_into_submobjects(self):
- copy = self.deepcopy()
- copy.set_submobjects([])
- self.resize_points(0)
- self.add(copy)
- return self
-
- def add_n_more_submobjects(self, n):
- if n == 0:
- return self
-
- curr = len(self.submobjects)
- if curr == 0:
- # If empty, simply add n point mobjects
- null_mob = self.copy()
- null_mob.set_points([self.get_center()])
- self.set_submobjects([null_mob.copy() for k in range(n)])
- return self
- target = curr + n
- repeat_indices = (np.arange(target) * curr) // target
- split_factors = [(repeat_indices == i).sum() for i in range(curr)]
- new_submobs = []
- for submob, sf in zip(self.submobjects, split_factors):
- new_submobs.append(submob)
- for _ in range(1, sf):
- new_submob = submob.copy()
- # If the submobject is at all transparent, then
- # make the copy completely transparent
- if submob.get_opacity() < 1:
- new_submob.set_opacity(0)
- new_submobs.append(new_submob)
- self.set_submobjects(new_submobs)
- return self
-
- # Interpolate
-
- def interpolate(self, mobject1, mobject2, alpha, path_func=straight_path):
- for key in self.data:
- if key in self.locked_data_keys:
- continue
- if len(self.data[key]) == 0:
- continue
- if key not in mobject1.data or key not in mobject2.data:
- continue
-
- if key in ("points", "bounding_box"):
- func = path_func
- else:
- func = interpolate
-
- self.data[key][:] = func(mobject1.data[key], mobject2.data[key], alpha)
- for key in self.uniforms:
- self.uniforms[key] = interpolate(
- mobject1.uniforms[key], mobject2.uniforms[key], alpha
- )
- return self
-
- def pointwise_become_partial(self, mobject, a, b):
- """
- Set points in such a way as to become only
- part of mobject.
- Inputs 0 <= a < b <= 1 determine what portion
- of mobject to become.
- """
- pass # To implement in subclass
-
- def become(self, mobject):
- """
- Edit all data and submobjects to be idential
- to another mobject
- """
- self.align_family(mobject)
- for sm1, sm2 in zip(self.get_family(), mobject.get_family()):
- sm1.set_data(sm2.data)
- sm1.set_uniforms(sm2.uniforms)
- self.refresh_bounding_box(recurse_down=True)
- return self
-
- # Locking data
-
- def lock_data(self, keys):
- """
- To speed up some animations, particularly transformations,
- it can be handy to acknowledge which pieces of data
- won't change during the animation so that calls to
- interpolate can skip this, and so that it's not
- read into the shader_wrapper objects needlessly
- """
- if self.has_updaters:
- return
- # Be sure shader data has most up to date information
- self.refresh_shader_data()
- self.locked_data_keys = set(keys)
-
- def lock_matching_data(self, mobject1, mobject2):
- for sm, sm1, sm2 in zip(
- self.get_family(), mobject1.get_family(), mobject2.get_family()
- ):
- keys = sm.data.keys() & sm1.data.keys() & sm2.data.keys()
- sm.lock_data(
- list(
- filter(
- lambda key: np.all(sm1.data[key] == sm2.data[key]),
- keys,
- )
- )
- )
- return self
-
- def unlock_data(self):
- for mob in self.get_family():
- mob.locked_data_keys = set()
-
- # Operations touching shader uniforms
-
- def affects_shader_info_id(func):
- @wraps(func)
- def wrapper(self):
- for mob in self.get_family():
- func(mob)
- # mob.refresh_shader_wrapper_id()
- return self
-
- return wrapper
-
- @affects_shader_info_id
- def fix_in_frame(self):
- self.uniforms["is_fixed_in_frame"] = 1.0
- return self
-
- @affects_shader_info_id
- def unfix_from_frame(self):
- self.uniforms["is_fixed_in_frame"] = 0.0
- return self
-
- @affects_shader_info_id
- def apply_depth_test(self):
- self.depth_test = True
- return self
-
- @affects_shader_info_id
- def deactivate_depth_test(self):
- self.depth_test = False
- return self
-
- # Shader code manipulation
-
- def replace_shader_code(self, old, new):
- # TODO, will this work with VMobject structure, given
- # that it does not simpler return shader_wrappers of
- # family?
- for wrapper in self.get_shader_wrapper_list():
- wrapper.replace_code(old, new)
- return self
-
- def set_color_by_code(self, glsl_code):
- """
- Takes a snippet of code and inserts it into a
- context which has the following variables:
- vec4 color, vec3 point, vec3 unit_normal.
- The code should change the color variable
- """
- self.replace_shader_code("///// INSERT COLOR FUNCTION HERE /////", glsl_code)
- return self
-
- def set_color_by_xyz_func(
- self, glsl_snippet, min_value=-5.0, max_value=5.0, colormap="viridis"
- ):
- """
- Pass in a glsl expression in terms of x, y and z which returns
- a float.
- """
- # TODO, add a version of this which changes the point data instead
- # of the shader code
- for char in "xyz":
- glsl_snippet = glsl_snippet.replace(char, "point." + char)
- rgb_list = get_colormap_list(colormap)
- self.set_color_by_code(
- "color.rgb = float_to_color({}, {}, {}, {});".format(
- glsl_snippet,
- float(min_value),
- float(max_value),
- get_colormap_code(rgb_list),
- )
- )
- return self
-
- # For shader data
-
- # def refresh_shader_wrapper_id(self):
- # self.shader_wrapper.refresh_id()
- # return self
-
- def get_shader_wrapper(self):
- from ..renderer.shader_wrapper import ShaderWrapper
-
- self.shader_wrapper = ShaderWrapper(
- vert_data=self.get_shader_data(),
- vert_indices=self.get_shader_vert_indices(),
- uniforms=self.get_shader_uniforms(),
- depth_test=self.depth_test,
- texture_paths=self.texture_paths,
- render_primitive=self.render_primitive,
- shader_folder=self.__class__.shader_folder,
- )
- return self.shader_wrapper
-
- def get_shader_wrapper_list(self):
- shader_wrappers = it.chain(
- [self.get_shader_wrapper()],
- *[sm.get_shader_wrapper_list() for sm in self.submobjects]
- )
- batches = batch_by_property(shader_wrappers, lambda sw: sw.get_id())
-
- result = []
- for wrapper_group, _ in batches:
- shader_wrapper = wrapper_group[0]
- if not shader_wrapper.is_valid():
- continue
- shader_wrapper.combine_with(*wrapper_group[1:])
- if len(shader_wrapper.vert_data) > 0:
- result.append(shader_wrapper)
- return result
-
- def check_data_alignment(self, array, data_key):
- # Makes sure that self.data[key] can be brodcast into
- # the given array, meaning its length has to be either 1
- # or the length of the array
- d_len = len(self.data[data_key])
- if d_len != 1 and d_len != len(array):
- self.data[data_key] = resize_with_interpolation(
- self.data[data_key], len(array)
- )
- return self
-
- def get_resized_shader_data_array(self, length):
- # If possible, try to populate an existing array, rather
- # than recreating it each frame
- points = self.data["points"]
- shader_data = np.zeros(len(points), dtype=self.shader_dtype)
- return shader_data
-
- def read_data_to_shader(self, shader_data, shader_data_key, data_key):
- if data_key in self.locked_data_keys:
- return
- self.check_data_alignment(shader_data, data_key)
- shader_data[shader_data_key] = self.data[data_key]
-
- def get_shader_data(self):
- shader_data = self.get_resized_shader_data_array(self.get_num_points())
- self.read_data_to_shader(shader_data, "point", "points")
- return shader_data
-
- def refresh_shader_data(self):
- self.get_shader_data()
-
- def get_shader_uniforms(self):
- return self.uniforms
-
- def get_shader_vert_indices(self):
- return self.shader_indices
-
- # Event Handlers
- """
- Event handling follows the Event Bubbling model of DOM in javascript.
- Return false to stop the event bubbling.
- To learn more visit https://www.quirksmode.org/js/events_order.html
-
- Event Callback Argument is a callable function taking two arguments:
- 1. Mobject
- 2. EventData
- """
-
- def init_event_listners(self):
- self.event_listners = []
-
- def add_event_listner(self, event_type, event_callback):
- event_listner = EventListner(self, event_type, event_callback)
- self.event_listners.append(event_listner)
- EVENT_DISPATCHER.add_listner(event_listner)
- return self
-
- def remove_event_listner(self, event_type, event_callback):
- event_listner = EventListner(self, event_type, event_callback)
- while event_listner in self.event_listners:
- self.event_listners.remove(event_listner)
- EVENT_DISPATCHER.remove_listner(event_listner)
- return self
-
- def clear_event_listners(self, recurse=True):
- self.event_listners = []
- if recurse:
- for submob in self.submobjects:
- submob.clear_event_listners(recurse=recurse)
- return self
-
- def get_event_listners(self):
- return self.event_listners
-
- def get_family_event_listners(self):
- return list(it.chain(*[sm.get_event_listners() for sm in self.get_family()]))
-
- def get_has_event_listner(self):
- return any(mob.get_event_listners() for mob in self.get_family())
-
- def add_mouse_motion_listner(self, callback):
- self.add_event_listner(EventType.MouseMotionEvent, callback)
-
- def remove_mouse_motion_listner(self, callback):
- self.remove_event_listner(EventType.MouseMotionEvent, callback)
-
- def add_mouse_press_listner(self, callback):
- self.add_event_listner(EventType.MousePressEvent, callback)
-
- def remove_mouse_press_listner(self, callback):
- self.remove_event_listner(EventType.MousePressEvent, callback)
-
- def add_mouse_release_listner(self, callback):
- self.add_event_listner(EventType.MouseReleaseEvent, callback)
-
- def remove_mouse_release_listner(self, callback):
- self.remove_event_listner(EventType.MouseReleaseEvent, callback)
-
- def add_mouse_drag_listner(self, callback):
- self.add_event_listner(EventType.MouseDragEvent, callback)
-
- def remove_mouse_drag_listner(self, callback):
- self.remove_event_listner(EventType.MouseDragEvent, callback)
-
- def add_mouse_scroll_listner(self, callback):
- self.add_event_listner(EventType.MouseScrollEvent, callback)
-
- def remove_mouse_scroll_listner(self, callback):
- self.remove_event_listner(EventType.MouseScrollEvent, callback)
-
- def add_key_press_listner(self, callback):
- self.add_event_listner(EventType.KeyPressEvent, callback)
-
- def remove_key_press_listner(self, callback):
- self.remove_event_listner(EventType.KeyPressEvent, callback)
-
- def add_key_release_listner(self, callback):
- self.add_event_listner(EventType.KeyReleaseEvent, callback)
-
- def remove_key_release_listner(self, callback):
- self.remove_event_listner(EventType.KeyReleaseEvent, callback)
-
- # Errors
-
- def throw_error_if_no_points(self):
- if not self.has_points():
- message = "Cannot call Mobject.{} " + "for a Mobject with no points"
- caller_name = sys._getframe(1).f_code.co_name
- raise Exception(message.format(caller_name))
-
-
-class OpenGLGroup(OpenGLMobject):
- def __init__(self, *mobjects, **kwargs):
- if not all([isinstance(m, OpenGLMobject) for m in mobjects]):
- raise Exception("All submobjects must be of type Mobject")
- OpenGLMobject.__init__(self, **kwargs)
- self.add(*mobjects)
-
-
-class OpenGLPoint(OpenGLMobject):
+class OpenGLPoint(Mobject):
def __init__(
self, location=ORIGIN, artificial_width=1e-6, artificial_height=1e-6, **kwargs
):
self.artificial_width = artificial_width
self.artificial_height = artificial_height
- OpenGLMobject.__init__(self, **kwargs)
+ super().__init__(self, **kwargs)
self.set_location(location)
def get_width(self):
@@ -1574,58 +19,10 @@ class OpenGLPoint(OpenGLMobject):
return self.artificial_height
def get_location(self):
- return self.get_points()[0].copy()
+ return self.points[0].copy()
def get_bounding_box_point(self, *args, **kwargs):
return self.get_location()
def set_location(self, new_loc):
self.set_points(np.array(new_loc, ndmin=2, dtype=float))
-
-
-class _AnimationBuilder:
- def __init__(self, mobject):
- self.mobject = mobject
- self.overridden_animation = None
- self.mobject.generate_target()
- self.is_chaining = False
- self.methods = []
-
- def __getattr__(self, method_name):
- method = getattr(self.mobject.target, method_name)
- self.methods.append(method)
- has_overridden_animation = hasattr(method, "_override_animate")
-
- if (self.is_chaining and has_overridden_animation) or self.overridden_animation:
- raise NotImplementedError(
- "Method chaining is currently not supported for "
- "overridden animations"
- )
-
- def update_target(*method_args, **method_kwargs):
- if has_overridden_animation:
- self.overridden_animation = method._override_animate(
- self.mobject, *method_args, **method_kwargs
- )
- else:
- method(*method_args, **method_kwargs)
- return self
-
- self.is_chaining = True
- return update_target
-
- def build(self):
- from ..animation.transform import _MethodAnimation
-
- if self.overridden_animation:
- return self.overridden_animation
-
- return _MethodAnimation(self.mobject, self.methods)
-
-
-def override_animate(method):
- def decorator(animation_method):
- method._override_animate = animation_method
- return animation_method
-
- return decorator
diff --git a/manim/mobject/opengl_three_dimensions.py b/manim/mobject/opengl_three_dimensions.py
index a494f759a..072979844 100644
--- a/manim/mobject/opengl_three_dimensions.py
+++ b/manim/mobject/opengl_three_dimensions.py
@@ -32,7 +32,7 @@ class OpenGLSurfaceMesh(OpenGLVGroup):
**kwargs
)
- def init_points(self):
+ def generate_points(self):
uv_surface = self.uv_surface
full_nu, full_nv = uv_surface.resolution
diff --git a/manim/mobject/types/opengl_surface.py b/manim/mobject/types/opengl_surface.py
index 60253f0ef..fa936a8ad 100644
--- a/manim/mobject/types/opengl_surface.py
+++ b/manim/mobject/types/opengl_surface.py
@@ -1,8 +1,8 @@
+from manim.mobject.mobject import Mobject
import numpy as np
import moderngl
from ...constants import *
-from ...mobject.opengl_mobject import OpenGLMobject
from ...utils.bezier import integer_interpolate
from ...utils.bezier import interpolate
from ...utils.images import get_full_raster_image_path
@@ -11,7 +11,7 @@ from ...utils.color import *
from ...utils.space_ops import normalize_along_axis
-class OpenGLSurface(OpenGLMobject):
+class OpenGLSurface(Mobject):
shader_dtype = [
("point", np.float32, (3,)),
("du_point", np.float32, (3,)),
@@ -67,12 +67,11 @@ class OpenGLSurface(OpenGLMobject):
self.compute_triangle_indices()
def uv_func(self, u, v):
- # To be implemented in subclasses
if self.passed_uv_func:
return self.passed_uv_func(u, v)
return (u, v, 0.0)
- def init_points(self):
+ def generate_points(self):
dim = self.dim
nu, nv = self.resolution
u_range = np.linspace(*self.u_range, nu)
@@ -211,7 +210,7 @@ class OpenGLSurfaceGroup(OpenGLSurface):
super().__init__(uv_func=None, **kwargs)
self.add(*parametric_surfaces)
- def init_points(self):
+ def generate_points(self):
pass # Needed?
@@ -254,7 +253,7 @@ class OpenGLTexturedSurface(OpenGLSurface):
self.data["im_coords"] = np.zeros((0, 2))
self.data["opacity"] = np.zeros((0, 1))
- def init_points(self):
+ def generate_points(self):
nu, nv = self.uv_surface.resolution
self.set_points(self.uv_surface.get_points())
self.data["im_coords"] = np.array(
diff --git a/manim/mobject/types/opengl_vectorized_mobject.py b/manim/mobject/types/opengl_vectorized_mobject.py
index e73c896ca..4f700b347 100644
--- a/manim/mobject/types/opengl_vectorized_mobject.py
+++ b/manim/mobject/types/opengl_vectorized_mobject.py
@@ -6,7 +6,8 @@ import moderngl
import numpy as np
from ...constants import *
-from ...mobject.opengl_mobject import OpenGLMobject, OpenGLPoint
+from ...mobject.mobject import Mobject
+from ...mobject.opengl_mobject import OpenGLPoint
# from manimlib.utils.bezier import get_smooth_quadratic_bezier_handle_points
from ...utils.bezier import (
@@ -40,7 +41,7 @@ JOINT_TYPE_MAP = {
}
-class OpenGLVMobject(OpenGLMobject):
+class OpenGLVMobject(Mobject):
fill_dtype = [
("point", np.float32, (3,)),
("unit_normal", np.float32, (3,)),
@@ -115,11 +116,6 @@ class OpenGLVMobject(OpenGLMobject):
super().__init__(**kwargs)
self.refresh_unit_normal()
- #
- # def get_group_class(self):
- # return VGroup
- #
-
def init_data(self):
super().init_data()
self.data.pop("rgbas")
@@ -645,7 +641,7 @@ class OpenGLVMobject(OpenGLMobject):
if not recompute:
return self.data["unit_normal"][0]
- if len(self.data["points"]) < 3:
+ if len(self.points) < 3:
return OUT
area_vect = self.get_area_vector()
@@ -1007,7 +1003,7 @@ class OpenGLVMobject(OpenGLMobject):
}
def get_stroke_shader_data(self):
- points = self.data["points"]
+ points = self.points
stroke_data = np.zeros(len(points), dtype=OpenGLVMobject.stroke_dtype)
nppc = self.n_points_per_curve
@@ -1024,7 +1020,7 @@ class OpenGLVMobject(OpenGLMobject):
return stroke_data
def get_fill_shader_data(self):
- points = self.data["points"]
+ points = self.points
fill_data = np.zeros(len(points), dtype=OpenGLVMobject.fill_dtype)
fill_data["vert_index"][:, 0] = range(len(points))
diff --git a/manim/renderer/opengl_renderer.py b/manim/renderer/opengl_renderer.py
index d008e4952..bbdcf2fb0 100644
--- a/manim/renderer/opengl_renderer.py
+++ b/manim/renderer/opengl_renderer.py
@@ -1,20 +1,15 @@
+from manim.mobject.mobject import Mobject
from manim.utils.exceptions import EndSceneEarlyException
from manim.utils.caching import handle_caching_play
from manim.renderer.cairo_renderer import handle_play_like_call
from manim.utils.color import color_to_rgba
import moderngl
from .opengl_renderer_window import Window
-from .shader_wrapper import ShaderWrapper
import numpy as np
-from ..mobject.types.vectorized_mobject import VMobject
import itertools as it
import time
-from .. import logger
from ..constants import *
from ..utils.space_ops import (
- cross2d,
- earclip_triangulation,
- z_to_vector,
quaternion_mult,
quaternion_from_angle_axis,
rotation_matrix_transpose_from_quaternion,
@@ -24,13 +19,13 @@ from ..utils.space_ops import (
from ..utils.simple_functions import clip
from ..mobject import opengl_geometry
-from ..mobject.opengl_mobject import OpenGLMobject, OpenGLPoint
+from ..mobject.opengl_mobject import OpenGLPoint
from PIL import Image
from manim import config
from ..scene.scene_file_writer import SceneFileWriter
-class OpenGLCamera(OpenGLMobject):
+class OpenGLCamera(Mobject):
def __init__(
self,
frame_shape=None,
@@ -74,7 +69,7 @@ class OpenGLCamera(OpenGLMobject):
self.data["euler_angles"] = np.array(self.euler_angles, dtype=float)
self.refresh_rotation_matrix()
- def init_points(self):
+ def generate_points(self):
self.set_points([ORIGIN, LEFT, RIGHT, DOWN, UP])
self.set_width(self.frame_shape[0], stretch=True)
self.set_height(self.frame_shape[1], stretch=True)