Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

OpenGL compatibility via metaclass: TracedPath, ParametricFunction, Brace, VGroup (#1572)

Mohammad Al-Fetyanie882b18e55e1
diff --git a/manim/mobject/changing.py b/manim/mobject/changing.py
index 05e403cb4..235c9c574 100644
--- a/manim/mobject/changing.py
+++ b/manim/mobject/changing.py
@@ -4,10 +4,12 @@ __all__ = ["AnimatedBoundary", "TracedPath"]
import numpy as np
+from .._config import config
from ..constants import *
from ..mobject.types.vectorized_mobject import VGroup, VMobject
from ..utils.color import BLUE_B, BLUE_D, BLUE_E, GREY_BROWN, WHITE
from ..utils.rate_functions import smooth
+from .opengl_compatibility import ConvertToOpenGL
class AnimatedBoundary(VGroup):
@@ -89,7 +91,7 @@ class AnimatedBoundary(VGroup):
return self
-class TracedPath(VMobject):
+class TracedPath(VMobject, metaclass=ConvertToOpenGL):
"""Traces the path of a point returned by a function call.
Examples
@@ -123,15 +125,18 @@ class TracedPath(VMobject):
def update_path(self):
new_point = self.traced_point_func()
- if self.has_no_points():
+ if not self.has_points():
self.start_new_path(new_point)
self.add_line_to(new_point)
else:
# Set the end to be the new point
- self.points[-1] = new_point
+ self.get_points()[-1] = new_point
# Second to last point
- nppcc = self.n_points_per_cubic_curve
- dist = np.linalg.norm(new_point - self.points[-nppcc])
+ if config["renderer"] == "opengl":
+ nppcc = self.n_points_per_curve
+ else:
+ nppcc = self.n_points_per_cubic_curve
+ dist = np.linalg.norm(new_point - self.get_points()[-nppcc])
if dist >= self.min_distance_to_new_point:
self.add_line_to(new_point)
diff --git a/manim/mobject/functions.py b/manim/mobject/functions.py
index f004d3801..738f74f83 100644
--- a/manim/mobject/functions.py
+++ b/manim/mobject/functions.py
@@ -9,9 +9,10 @@ from .. import config
from ..constants import *
from ..mobject.types.vectorized_mobject import VMobject
from ..utils.color import YELLOW
+from .opengl_compatibility import ConvertToOpenGL
-class ParametricFunction(VMobject):
+class ParametricFunction(VMobject, metaclass=ConvertToOpenGL):
"""A parametric curve.
Examples
@@ -65,7 +66,7 @@ class ParametricFunction(VMobject):
self.use_smoothing = use_smoothing
self.t_min, self.t_max, self.t_step = t_range
- VMobject.__init__(self, **kwargs)
+ super().__init__(**kwargs)
def get_function(self):
return self.function
@@ -98,6 +99,8 @@ class ParametricFunction(VMobject):
self.make_smooth()
return self
+ init_points = generate_points
+
class FunctionGraph(ParametricFunction):
def __init__(self, function, x_range=None, color=YELLOW, **kwargs):
diff --git a/manim/mobject/svg/brace.py b/manim/mobject/svg/brace.py
index 6bf3ae278..a2ca19c56 100644
--- a/manim/mobject/svg/brace.py
+++ b/manim/mobject/svg/brace.py
@@ -6,6 +6,9 @@ from typing import Optional, Sequence
import numpy as np
+from manim._config import config
+from manim.mobject.opengl_compatibility import ConvertToOpenGL
+
from ...animation.composition import AnimationGroup
from ...animation.fading import FadeIn
from ...animation.growing import GrowFromCenter
@@ -13,6 +16,7 @@ from ...constants import *
from ...mobject.geometry import Arc, Line
from ...mobject.svg.svg_path import SVGPathMobject
from ...mobject.svg.tex_mobject import MathTex, Tex
+from ...mobject.types.opengl_vectorized_mobject import OpenGLVMobject
from ...mobject.types.vectorized_mobject import VMobject
from ...utils.color import BLACK
@@ -119,14 +123,17 @@ class Brace(SVGPathMobject):
def get_tip(self):
# Returns the position of the seventh point in the path, which is the tip.
- return self.points[28] # = 7*4
+ if config["renderer"] == "opengl":
+ return self.get_points()[34]
+
+ return self.get_points()[28] # = 7*4
def get_direction(self):
vect = self.get_tip() - self.get_center()
return vect / np.linalg.norm(vect)
-class BraceLabel(VMobject):
+class BraceLabel(VMobject, metaclass=ConvertToOpenGL):
def __init__(
self,
obj,
@@ -138,10 +145,11 @@ class BraceLabel(VMobject):
):
self.label_constructor = label_constructor
self.label_scale = label_scale
- VMobject.__init__(self, **kwargs)
+ super().__init__(**kwargs)
+
self.brace_direction = brace_direction
if isinstance(obj, list):
- obj = VMobject(*obj)
+ obj = self.get_group_class()(*obj)
self.brace = Brace(obj, brace_direction, **kwargs)
if isinstance(text, tuple) or isinstance(text, list):
@@ -159,7 +167,7 @@ class BraceLabel(VMobject):
def shift_brace(self, obj, **kwargs):
if isinstance(obj, list):
- obj = VMobject(*obj)
+ obj = self.get_group_class()(*obj)
self.brace = Brace(obj, self.brace_direction, **kwargs)
self.brace.put_at_tip(self.label)
self.submobjects[0] = self.brace
diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.py
index 450ce05ec..d6dae8e0d 100644
--- a/manim/mobject/types/vectorized_mobject.py
+++ b/manim/mobject/types/vectorized_mobject.py
@@ -1468,7 +1468,7 @@ class VMobject(Mobject):
return self
-class VGroup(VMobject):
+class VGroup(VMobject, metaclass=ConvertToOpenGL):
"""A group of vectorized mobjects.
This can be used to group multiple :class:`~.VMobject` instances together
@@ -1520,7 +1520,7 @@ class VGroup(VMobject):
"""
def __init__(self, *vmobjects, **kwargs):
- VMobject.__init__(self, **kwargs)
+ super().__init__(**kwargs)
self.add(*vmobjects)
def __repr__(self):
@@ -1585,7 +1585,7 @@ class VGroup(VMobject):
(gr-circle_red).animate.shift(RIGHT)
)
"""
- if not all(isinstance(m, VMobject) for m in vmobjects):
+ if not all(isinstance(m, (VMobject, OpenGLVMobject)) for m in vmobjects):
raise TypeError("All submobjects must be of type VMobject")
return super().add(*vmobjects)
@@ -1625,7 +1625,7 @@ class VGroup(VMobject):
>>> new_obj = VMobject()
>>> vgroup[0] = new_obj
"""
- if not all(isinstance(m, VMobject) for m in value):
+ if not all(isinstance(m, (VMobject, OpenGLVMobject)) for m in value):
raise TypeError("All submobjects must be of type VMobject")
self.submobjects[key] = value