Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Add OpenGLTex

Devin Neal752dce32290c
diff --git a/example_scenes/basic.py b/example_scenes/basic.py
index bc34233c6..ddec8fa1d 100644
--- a/example_scenes/basic.py
+++ b/example_scenes/basic.py
@@ -19,8 +19,9 @@ class TestLatex(Scene):
# mob = SVGMobject("../manim/media/Tex/1e2155e0e4bdc7ad.svg").scale(3)
# mob.set_color(WHITE)
# self.play(FadeIn(mob))
- mob = MathTex("a")
- self.play(FadeIn(mob))
+ mob = Tex("C").set_height(7)
+ self.play(Write(mob))
+ self.wait(2)
class OpeningManim(Scene):
diff --git a/example_scenes/opengl.py b/example_scenes/opengl.py
index 48fcc90d4..c43e33ed8 100644
--- a/example_scenes/opengl.py
+++ b/example_scenes/opengl.py
@@ -10,11 +10,14 @@ from pathlib import Path
class TestLatex(Scene):
def construct(self):
+ config["background_color"] = "#333333"
# mob = OpenGLSVGMobject("../manim/media/Tex/184715fb937073ce.svg").scale(3)
# mob.set_color(WHITE)
- mob = OpenGLTex("Manim Community").set_width(13)
+ mob = OpenGLTex("Manim Community").set_width(12)
+ mob2 = OpenGLTex("v4.0").set_width(12)
self.play(Write(mob))
- self.wait(10)
+ self.play(Transform(mob, mob2))
+ self.interact()
class InteractiveDevelopment(Scene):
diff --git a/manim/animation/transform.py b/manim/animation/transform.py
index 064e71193..c081d8b48 100644
--- a/manim/animation/transform.py
+++ b/manim/animation/transform.py
@@ -34,6 +34,7 @@ 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
+from .. import config
if typing.TYPE_CHECKING:
from ..scene.scene import Scene
@@ -78,7 +79,10 @@ class Transform(Animation):
self.target_copy = self.target_mobject.copy()
# Note, this potentially changes the structure
# of both mobject and target_mobject
- self.mobject.align_data(self.target_copy)
+ if config["use_opengl_renderer"]:
+ self.mobject.align_data_and_family(self.target_copy)
+ else:
+ self.mobject.align_data(self.target_copy)
super().begin()
def create_target(self) -> typing.Union[Mobject, None]:
diff --git a/manim/mobject/svg/opengl_svg_mobject.py b/manim/mobject/svg/opengl_svg_mobject.py
index 38830a1d9..e7d8c659e 100644
--- a/manim/mobject/svg/opengl_svg_mobject.py
+++ b/manim/mobject/svg/opengl_svg_mobject.py
@@ -15,6 +15,8 @@ class OpenGLSVGMobject(OpenGLVMobject, SVGMobject):
unpack_groups=True, # if False, creates a hierarchy of VGroups
stroke_width=DEFAULT_STROKE_WIDTH,
fill_opacity=1.0,
+ should_subdivide_sharp_curves=False,
+ should_remove_null_curves=False,
**kwargs,
):
self.def_map = {}
@@ -22,6 +24,10 @@ class OpenGLSVGMobject(OpenGLVMobject, SVGMobject):
self.ensure_valid_file()
self.should_center = should_center
self.unpack_groups = unpack_groups
+ self.path_string_config = {
+ "should_subdivide_sharp_curves": should_subdivide_sharp_curves,
+ "should_remove_null_curves": should_remove_null_curves,
+ }
OpenGLVMobject.__init__(
self, stroke_width=stroke_width, fill_opacity=fill_opacity, **kwargs
)
@@ -31,4 +37,6 @@ class OpenGLSVGMobject(OpenGLVMobject, SVGMobject):
self.generate_points()
def path_string_to_mobject(self, path_string: str, style: dict):
- return OpenGLSVGPathMobject(path_string, **parse_style(style))
+ return OpenGLSVGPathMobject(
+ path_string, **self.path_string_config, **parse_style(style)
+ )
diff --git a/manim/mobject/svg/opengl_svg_path.py b/manim/mobject/svg/opengl_svg_path.py
index 0fc872430..6cc46e8ca 100644
--- a/manim/mobject/svg/opengl_svg_path.py
+++ b/manim/mobject/svg/opengl_svg_path.py
@@ -4,9 +4,21 @@ from .svg_path import SVGPathMobject
class OpenGLSVGPathMobject(OpenGLVMobject, SVGPathMobject):
- def __init__(self, path_string, **kwargs):
+ def __init__(
+ self,
+ path_string,
+ should_subdivide_sharp_curves=False,
+ should_remove_null_curves=False,
+ **kwargs
+ ):
self.path_string = path_string
- OpenGLVMobject.__init__(self, **kwargs)
+ OpenGLVMobject.__init__(
+ self,
+ long_lines=True,
+ should_subdivide_sharp_curves=should_subdivide_sharp_curves,
+ should_remove_null_curves=should_remove_null_curves,
+ **kwargs
+ )
self.current_path_start = np.zeros((1, self.dim))
def init_points(self):
diff --git a/manim/mobject/svg/opengl_tex_mobject.py b/manim/mobject/svg/opengl_tex_mobject.py
index bc006d441..58bd711aa 100644
--- a/manim/mobject/svg/opengl_tex_mobject.py
+++ b/manim/mobject/svg/opengl_tex_mobject.py
@@ -241,6 +241,8 @@ class OpenGLSingleStringMathTex(OpenGLSVGMobject):
fill_opacity=fill_opacity,
background_stroke_width=background_stroke_width,
background_stroke_color=background_stroke_color,
+ should_subdivide_sharp_curves=True,
+ should_remove_null_curves=True,
**kwargs,
)
if height is None:
@@ -331,7 +333,9 @@ class OpenGLSingleStringMathTex(OpenGLSVGMobject):
def path_string_to_mobject(self, path_string, style):
# Overwrite superclass default to use
# specialized path_string mobject
- return OpenGLTexSymbol(path_string, **parse_style(style))
+ return OpenGLTexSymbol(
+ path_string, **self.path_string_config, **parse_style(style)
+ )
def organize_submobjects_left_to_right(self):
self.sort(lambda p: p[0])
diff --git a/manim/mobject/svg/svg_path.py b/manim/mobject/svg/svg_path.py
index 4ccb34422..21b7090de 100644
--- a/manim/mobject/svg/svg_path.py
+++ b/manim/mobject/svg/svg_path.py
@@ -90,6 +90,12 @@ class SVGPathMobject(VMobject):
for command, coord_string in pairs:
self.handle_command(command, coord_string, prev_command)
prev_command = command
+ if self.should_subdivide_sharp_curves:
+ # For a healthy triangulation later
+ self.subdivide_sharp_curves()
+ if self.should_remove_null_curves:
+ # Get rid of any null curves
+ self.set_points(self.get_points_without_null_curves())
# people treat y-coordinate differently
self.rotate(np.pi, RIGHT, about_point=ORIGIN)
@@ -166,7 +172,10 @@ class SVGPathMobject(VMobject):
raise NotImplementedError()
elif command == "Z": # closepath
- self.add_line_to(self.current_path_start)
+ if config["use_opengl_renderer"]:
+ self.close_path()
+ else:
+ self.add_line_to(self.current_path_start)
return
def string_to_points(self, command, is_relative, coord_string, start_point):
diff --git a/manim/mobject/types/opengl_vectorized_mobject.py b/manim/mobject/types/opengl_vectorized_mobject.py
index e73c896ca..57e9894f8 100644
--- a/manim/mobject/types/opengl_vectorized_mobject.py
+++ b/manim/mobject/types/opengl_vectorized_mobject.py
@@ -23,7 +23,7 @@ from ...utils.color import *
# from manimlib.utils.color import rgb_to_hex
from ...utils.iterables import listify, make_even, resize_with_interpolation
-# from manimlib.utils.space_ops import angle_between_vectors
+from ...utils.space_ops import angle_between_vectors
from ...utils.space_ops import (
cross2d,
earclip_triangulation,
@@ -77,6 +77,8 @@ class OpenGLVMobject(OpenGLMobject):
tolerance_for_point_equality=1e-8,
n_points_per_curve=3,
long_lines=False,
+ should_subdivide_sharp_curves=False,
+ should_remove_null_curves=False,
# Could also be "bevel", "miter", "round"
joint_type="auto",
flat_stroke=True,
@@ -103,6 +105,8 @@ class OpenGLVMobject(OpenGLMobject):
self.tolerance_for_point_equality = tolerance_for_point_equality
self.n_points_per_curve = n_points_per_curve
self.long_lines = long_lines
+ self.should_subdivide_sharp_curves = should_subdivide_sharp_curves
+ self.should_remove_null_curves = should_remove_null_curves
# Could also be "bevel", "miter", "round"
self.joint_type = joint_type
self.flat_stroke = flat_stroke
diff --git a/manim/utils/space_ops.py b/manim/utils/space_ops.py
index 443a6a6a8..5e29e41bb 100644
--- a/manim/utils/space_ops.py
+++ b/manim/utils/space_ops.py
@@ -232,7 +232,11 @@ def angle_between_vectors(v1, v2):
Returns the angle between two 3D vectors.
This angle will always be btw 0 and pi
"""
- return np.arccos(fdiv(np.dot(v1, v2), get_norm(v1) * get_norm(v2)))
+ if config["use_opengl_renderer"]:
+ diff = (angle_of_vector(v2) - angle_of_vector(v1)) % TAU
+ return min(diff, TAU - diff)
+ else:
+ return np.arccos(fdiv(np.dot(v1, v2), get_norm(v1) * get_norm(v2)))
def project_along_vector(point, vector):