Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fixed error when processing SVG with omitted elliptical arc command (#1574)

André601d32879948
diff --git a/manim/mobject/svg/svg_path.py b/manim/mobject/svg/svg_path.py
index f255a5f2b..5bc60a9e5 100644
--- a/manim/mobject/svg/svg_path.py
+++ b/manim/mobject/svg/svg_path.py
@@ -211,6 +211,11 @@ def string_to_numbers(num_string: str) -> List[float]:
return float_results
+def grouped(iterable, n):
+ """Group iterable into arrays of n items."""
+ return (np.array(v) for v in zip(*[iter(iterable)] * n))
+
+
class SVGPathMobject(VMobject, metaclass=ConvertToOpenGL):
def __init__(self, path_string, **kwargs):
self.path_string = path_string
@@ -380,21 +385,37 @@ class SVGPathMobject(VMobject, metaclass=ConvertToOpenGL):
# arcs are weirdest, handle them first.
if command == "A":
- # We have to handle offsets here because ellipses are complicated.
- if is_relative:
- numbers[5] += start_point[0]
- numbers[6] += start_point[1]
-
- # If the endpoints (x1, y1) and (x2, y2) are identical, then this
- # is equivalent to omitting the elliptical arc segment entirely.
- # for more information of where this math came from visit:
- # http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
- if start_point[0] == numbers[5] and start_point[1] == numbers[6]:
- return
+ result = np.zeros((0, self.dim))
+ last_end_point = None
+ for elliptic_numbers in grouped(numbers, 7):
+ # The startpoint changes with each iteration.
+ if last_end_point is not None:
+ start_point = last_end_point
+
+ # We have to handle offsets here because ellipses are complicated.
+ if is_relative:
+ elliptic_numbers[5] += start_point[0]
+ elliptic_numbers[6] += start_point[1]
+
+ # If the endpoints (x1, y1) and (x2, y2) are identical, then this
+ # is equivalent to omitting the elliptical arc segment entirely.
+ # for more information of where this math came from visit:
+ # http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
+ if (
+ start_point[0] == elliptic_numbers[5]
+ and start_point[1] == elliptic_numbers[6]
+ ):
+ continue
+
+ result = np.append(
+ result,
+ elliptical_arc_to_cubic_bezier(*start_point[:2], *elliptic_numbers),
+ axis=0,
+ )
- result = np.array(
- elliptical_arc_to_cubic_bezier(*start_point[:2], *numbers)
- )
+ # We store the endpoint so that it can be the startpoint for the
+ # next iteration.
+ last_end_point = elliptic_numbers[5:]
return result
diff --git a/tests/control_data/graphical_units_data/img_and_svg/HeartTest.npz b/tests/control_data/graphical_units_data/img_and_svg/HeartTest.npz
new file mode 100644
index 000000000..3cec93b8b
Binary files /dev/null and b/tests/control_data/graphical_units_data/img_and_svg/HeartTest.npz differ
diff --git a/tests/test_graphical_units/img_svg_resources/heart.svg b/tests/test_graphical_units/img_svg_resources/heart.svg
new file mode 100644
index 000000000..33999c6ca
--- /dev/null
+++ b/tests/test_graphical_units/img_svg_resources/heart.svg
@@ -0,0 +1,3 @@
+<svg width="100" height="100">
+ <path d="M 10 30 A 20 20, 0, 0, 1, 50 30 20 20, 0, 0, 1 90 30 Q 90 60 50 90 Q 10 60 10 30 z" style="stroke:black; fill:yellow" />
+</svg>
diff --git a/tests/test_graphical_units/test_img_and_svg.py b/tests/test_graphical_units/test_img_and_svg.py
index c55299ee8..1efc3bc8c 100644
--- a/tests/test_graphical_units/test_img_and_svg.py
+++ b/tests/test_graphical_units/test_img_and_svg.py
@@ -107,6 +107,13 @@ class HalfEllipseTest(Scene):
self.wait()
+class HeartTest(Scene):
+ def construct(self):
+ heart = SVGMobject(get_test_resource("heart.svg"))
+ self.add(heart)
+ self.wait()
+
+
class Arcs01Test(Scene):
# See: https://www.w3.org/TR/SVG11/images/paths/arcs01.svg
def construct(self):