Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

fix: renderer comparisons against "opengl" string never matched (#4929)

pjfo31a619ff432d
diff --git a/manim/mobject/svg/brace.py b/manim/mobject/svg/brace.py
index af6989c91..83896aeb3 100644
--- a/manim/mobject/svg/brace.py
+++ b/manim/mobject/svg/brace.py
@@ -9,7 +9,6 @@ from typing import TYPE_CHECKING, Any, Self
import numpy as np
import svgelements as se
-from manim._config import config
from manim.mobject.geometry.arc import Arc
from manim.mobject.geometry.line import Line
from manim.mobject.mobject import Mobject
@@ -123,6 +122,9 @@ class Brace(VMobjectFromSVGPath):
**kwargs,
)
self.flip(RIGHT)
+ self._tip_point_index = np.argmin(
+ np.linalg.norm(self.points - self.get_bottom(), axis=1)
+ )
self.stretch_to_fit_width(target_width)
self.shift(left - self.get_corner(UP + LEFT) + self.buff * DOWN)
@@ -192,11 +194,7 @@ class Brace(VMobjectFromSVGPath):
def get_tip(self) -> Point3D:
"""Returns the point at the brace tip."""
- # Returns the position of the seventh point in the path, which is the tip.
- if config["renderer"] == "opengl":
- return self.points[34]
-
- return self.points[28] # = 7*4
+ return self.points[self._tip_point_index]
def get_direction(self) -> Vector3D:
"""Returns the direction from the center to the brace tip."""
diff --git a/manim/mobject/svg/svg_mobject.py b/manim/mobject/svg/svg_mobject.py
index e0079e35b..75cfd621d 100644
--- a/manim/mobject/svg/svg_mobject.py
+++ b/manim/mobject/svg/svg_mobject.py
@@ -13,7 +13,7 @@ import svgelements as se
from manim import config, logger
from manim.utils.color import ManimColor, ParsableManimColor
-from ...constants import RIGHT
+from ...constants import RIGHT, RendererType
from ...utils.bezier import get_quadratic_approximation_of_cubic
from ...utils.images import get_full_vector_image_path
from ...utils.iterables import hash_obj
@@ -543,6 +543,15 @@ class VMobjectFromSVGPath(VMobject, metaclass=ConvertToOpenGL):
self.should_subdivide_sharp_curves = should_subdivide_sharp_curves
self.should_remove_null_curves = should_remove_null_curves
+ if config.renderer == RendererType.OPENGL:
+ # OpenGLVMobject.__init__ takes these as parameters and would
+ # otherwise reset the attributes above to its own defaults.
+ kwargs.update(
+ long_lines=long_lines,
+ should_subdivide_sharp_curves=should_subdivide_sharp_curves,
+ should_remove_null_curves=should_remove_null_curves,
+ )
+
super().__init__(**kwargs)
def generate_points(self) -> Self:
@@ -550,7 +559,7 @@ class VMobjectFromSVGPath(VMobject, metaclass=ConvertToOpenGL):
self.handle_commands()
- if config.renderer == "opengl":
+ if config.renderer == RendererType.OPENGL:
if self.should_subdivide_sharp_curves:
# For a healthy triangulation later
self.subdivide_sharp_curves()
@@ -621,6 +630,10 @@ class VMobjectFromSVGPath(VMobject, metaclass=ConvertToOpenGL):
move_pen(end)
def add_line(start: np.ndarray, end: np.ndarray) -> None:
+ if self.long_lines:
+ midpoint = (start + end) / 2
+ add_quad(start, (start + midpoint) / 2, midpoint)
+ start = midpoint
add_quad(start, (start + end) / 2, end)
move_pen(end)
diff --git a/tests/opengl/test_brace_opengl.py b/tests/opengl/test_brace_opengl.py
new file mode 100644
index 000000000..a04c01c98
--- /dev/null
+++ b/tests/opengl/test_brace_opengl.py
@@ -0,0 +1,36 @@
+from __future__ import annotations
+
+import numpy as np
+import pytest
+
+from manim import *
+
+
+def test_brace_tip(config):
+ brace = Brace(Square())
+ # For the default DOWN brace the tip is its lowest point.
+ assert np.allclose(brace.get_tip(), brace.get_bottom())
+
+
+ ("width", "path_config"),
+ [
+ (2, {}),
+ (2, {"long_lines": True}),
+ (2, {"should_subdivide_sharp_curves": True}),
+ (0.1, {"should_remove_null_curves": True}),
+ (
+ 0.1,
+ {
+ "long_lines": True,
+ "should_subdivide_sharp_curves": True,
+ "should_remove_null_curves": True,
+ },
+ ),
+ ],
+ ids=["default", "long-lines", "subdivided", "null-curves", "all-flags"],
+)
+def test_brace_tip_under_opengl(using_opengl_renderer, width, path_config):
+ brace = Brace(Rectangle(width=width, height=1), **path_config)
+ # For a DOWN brace the tip is its lowest point.
+ assert np.allclose(brace.get_tip(), brace.get_bottom())
diff --git a/tests/opengl/test_svg_mobject_opengl.py b/tests/opengl/test_svg_mobject_opengl.py
index c1d11f42e..7e0e50d0d 100644
--- a/tests/opengl/test_svg_mobject_opengl.py
+++ b/tests/opengl/test_svg_mobject_opengl.py
@@ -1,6 +1,10 @@
from __future__ import annotations
+import numpy as np
+import svgelements as se
+
from manim import *
+from manim.mobject.svg.svg_mobject import VMobjectFromSVGPath
from tests.helpers.path_utils import get_svg_resource
@@ -54,3 +58,46 @@ def test_stroke_overrides_color(using_opengl_renderer):
stroke_color=expected_color,
)
assert svg.stroke_color.to_hex() == expected_color
+
+
+def test_subdivide_sharp_curves_under_opengl(using_opengl_renderer):
+ # A fresh se.Path per construction: the constructor mutates path_obj in
+ # place via approximate_arcs_with_quads.
+ plain = VMobjectFromSVGPath(se.Path("M 0 0 Q 0 4 4 4 Z"))
+ subdivided = VMobjectFromSVGPath(
+ se.Path("M 0 0 Q 0 4 4 4 Z"),
+ should_subdivide_sharp_curves=True,
+ )
+ assert len(subdivided.points) > len(plain.points)
+
+
+def test_remove_null_curves_under_opengl(using_opengl_renderer):
+ # The repeated "L 4 0" encodes a zero-length curve.
+ plain = VMobjectFromSVGPath(se.Path("M 0 0 L 4 0 L 4 0 L 4 4 Z"))
+ stripped = VMobjectFromSVGPath(
+ se.Path("M 0 0 L 4 0 L 4 0 L 4 4 Z"),
+ should_remove_null_curves=True,
+ )
+ assert len(stripped.points) < len(plain.points)
+
+
+def test_long_lines_under_opengl(using_opengl_renderer):
+ plain = VMobjectFromSVGPath(se.Path("M 0 0 L 4 0"))
+ split = VMobjectFromSVGPath(
+ se.Path("M 0 0 L 4 0"),
+ long_lines=True,
+ )
+ assert len(split.points) == 2 * len(plain.points)
+
+
+def test_path_flags_ignored_under_cairo(config):
+ # The flags are OpenGL-only: under Cairo they must be accepted without
+ # error and leave the generated points untouched.
+ plain = VMobjectFromSVGPath(se.Path("M 0 0 Q 0 4 4 4 Z"))
+ flagged = VMobjectFromSVGPath(
+ se.Path("M 0 0 Q 0 4 4 4 Z"),
+ long_lines=True,
+ should_subdivide_sharp_curves=True,
+ should_remove_null_curves=True,
+ )
+ assert np.array_equal(flagged.points, plain.points)