A community-maintained Python framework for creating mathematical animations.
Fixed rendering SVG paths with multiple move commands (#3019)
Benjamin Hackl93f916587b1e
diff --git a/manim/mobject/types/vectorized_mobject.py b/manim/mobject/types/vectorized_mobject.pyindex 3aa6dfe40..55f69e5cc 100644--- a/manim/mobject/types/vectorized_mobject.py+++ b/manim/mobject/types/vectorized_mobject.py@@ -14,7 +14,7 @@ __all__ = [import itertools as itimport sysimport typing-from typing import Optional, Sequence, Union+from typing import Callable, Optional, Sequence, Unionimport colourimport numpy as np@@ -39,7 +39,7 @@ from ...utils.bezier import ()from ...utils.color import BLACK, WHITE, color_to_rgbafrom ...utils.deprecation import deprecated-from ...utils.iterables import make_even, stretch_array_to_length, tuplify+from ...utils.iterables import make_even, resize_array, stretch_array_to_length, tuplifyfrom ...utils.space_ops import rotate_vector, shoelace_direction# TODO@@ -599,6 +599,27 @@ class VMobject(Mobject):self.points = np.array(points)return self+ def resize_points(+ self,+ new_length: int,+ resize_func: Callable[[np.ndarray, int], np.ndarray] = resize_array,+ ):+ """Resize the array of anchor points and handles to have+ the specified size.++ Parameters+ ----------+ new_length+ The new (total) number of points.+ resize_func+ A function mapping a Numpy array (the points) and an integer+ (the target size) to a Numpy array. The default implementation+ is based on Numpy's ``resize`` function.+ """+ if new_length != len(self.points):+ self.points = resize_func(self.points, new_length)+ return self+def set_anchors_and_handles(self,anchors1: Sequence[float],@@ -644,7 +665,12 @@ class VMobject(Mobject):return selfdef start_new_path(self, point):- # TODO, make sure that len(self.points) % 4 == 0?+ if len(self.points) % 4 != 0:+ # close the open path by appending the last+ # start anchor sufficiently often+ last_anchor = self.get_start_anchors()[-1]+ for _ in range(4 - (len(self.points) % 4)):+ self.append_points([last_anchor])self.append_points([point])return selfdiff --git a/manim/utils/iterables.py b/manim/utils/iterables.pyindex 1d8c0a536..cc432d9e0 100644--- a/manim/utils/iterables.py+++ b/manim/utils/iterables.py@@ -261,21 +261,16 @@ def resize_array(nparray: np.ndarray, length: int) -> np.ndarray:--------Normal usage::- nparray = np.array([[1, 2],- [3, 4]])-- resize_array(nparray, 1)- # np.array([[1, 2]])-- resize_array(nparray, 3)- # np.array([[1, 2],- # [3, 4],- # [1, 2]])-- nparray = np.array([[[1, 2],[3, 4]]])- resize_array(nparray, 2)- # np.array([[[1, 2], [3, 4]],- # [[1, 2], [3, 4]]])+ >>> points = np.array([[1, 2], [3, 4]])+ >>> resize_array(points, 1)+ array([[1, 2]])+ >>> resize_array(points, 3)+ array([[1, 2],+ [3, 4],+ [1, 2]])+ >>> resize_array(points, 2)+ array([[1, 2],+ [3, 4]])"""if len(nparray) == length:return nparraydiff --git a/tests/test_graphical_units/control_data/img_and_svg/path_multiple_moves.npz b/tests/test_graphical_units/control_data/img_and_svg/path_multiple_moves.npznew file mode 100644index 000000000..9f8839930Binary files /dev/null and b/tests/test_graphical_units/control_data/img_and_svg/path_multiple_moves.npz differdiff --git a/tests/test_graphical_units/img_svg_resources/path_multiple_moves.svg b/tests/test_graphical_units/img_svg_resources/path_multiple_moves.svgnew file mode 100644index 000000000..9f12c3059--- /dev/null+++ b/tests/test_graphical_units/img_svg_resources/path_multiple_moves.svg@@ -0,0 +1,71 @@+<?xml version='1.0' encoding='UTF-8'?>+<!-- This file was generated by dvisvgm 2.11.1 -->+<svg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='113.784279pt' height='26.093569pt' viewBox='-57.056044 -72.000003 113.784279 26.093569'>+<defs>+<path id='g1-49' d='M2.336239-4.435367C2.336239-4.623661 2.322291-4.630635 2.127024-4.630635C1.680697-4.191283 1.046077-4.184309 .760149-4.184309V-3.93325C.927522-3.93325 1.387796-3.93325 1.771357-4.128518V-.571856C1.771357-.341719 1.771357-.251059 1.073973-.251059H.808966V0C.934496-.006974 1.792279-.027895 2.050311-.027895C2.266501-.027895 3.145205-.006974 3.29863 0V-.251059H3.033624C2.336239-.251059 2.336239-.341719 2.336239-.571856V-4.435367Z'/>+<path id='g1-50' d='M3.521793-1.26924H3.284682C3.263761-1.115816 3.194022-.704359 3.103362-.63462C3.047572-.592777 2.510585-.592777 2.412951-.592777H1.129763C1.862017-1.241345 2.106102-1.436613 2.524533-1.764384C3.040598-2.175841 3.521793-2.608219 3.521793-3.270735C3.521793-4.11457 2.782565-4.630635 1.889913-4.630635C1.025156-4.630635 .439352-4.02391 .439352-3.382316C.439352-3.02665 .739228-2.991781 .808966-2.991781C.976339-2.991781 1.17858-3.110336 1.17858-3.361395C1.17858-3.486924 1.129763-3.731009 .767123-3.731009C.983313-4.226152 1.457534-4.379577 1.785305-4.379577C2.48269-4.379577 2.84533-3.835616 2.84533-3.270735C2.84533-2.66401 2.412951-2.182814 2.189788-1.931756L.509091-.27198C.439352-.209215 .439352-.195268 .439352 0H3.312578L3.521793-1.26924Z'/>+<path id='g0-82' d='M3.73599-6.117061C3.795766-6.356164 3.825654-6.455791 4.014944-6.485679C4.104608-6.495641 4.423412-6.495641 4.622665-6.495641C5.330012-6.495641 6.435866-6.495641 6.435866-5.50934C6.435866-5.17061 6.276463-4.483188 5.88792-4.094645C5.628892-3.835616 5.100872-3.516812 4.204234-3.516812H3.088418L3.73599-6.117061ZM5.17061-3.387298C6.176837-3.606476 7.362391-4.303861 7.362391-5.310087C7.362391-6.166874 6.465753-6.804483 5.160648-6.804483H2.321295C2.122042-6.804483 2.032379-6.804483 2.032379-6.60523C2.032379-6.495641 2.122042-6.495641 2.311333-6.495641C2.331258-6.495641 2.520548-6.495641 2.689913-6.475716C2.86924-6.455791 2.958904-6.445828 2.958904-6.316314C2.958904-6.276463 2.948941-6.246575 2.919054-6.127024L1.58406-.777086C1.484433-.388543 1.464508-.308842 .67746-.308842C.498132-.308842 .408468-.308842 .408468-.109589C.408468 0 .52802 0 .547945 0C.826899 0 1.524284-.029888 1.803238-.029888S2.789539 0 3.068493 0C3.148194 0 3.267746 0 3.267746-.199253C3.267746-.308842 3.178082-.308842 2.988792-.308842C2.620174-.308842 2.34122-.308842 2.34122-.488169C2.34122-.547945 2.361146-.597758 2.371108-.657534L3.028643-3.297634H4.214197C5.120797-3.297634 5.300125-2.739726 5.300125-2.391034C5.300125-2.241594 5.220423-1.932752 5.160648-1.703611C5.090909-1.424658 5.001245-1.05604 5.001245-.856787C5.001245 .219178 6.196762 .219178 6.326276 .219178C7.173101 .219178 7.521793-.787049 7.521793-.926526C7.521793-1.046077 7.412204-1.046077 7.402242-1.046077C7.312578-1.046077 7.292653-.976339 7.272727-.9066C7.023661-.169365 6.595268 0 6.366127 0C6.03736 0 5.967621-.219178 5.967621-.607721C5.967621-.916563 6.027397-1.424658 6.067248-1.743462C6.087173-1.882939 6.107098-2.072229 6.107098-2.211706C6.107098-2.978829 5.439601-3.287671 5.17061-3.387298Z'/>+</defs>+<g id='page1'>+<g stroke-miterlimit='10' transform='translate(-56.856794,-52.257708)scale(0.996264,-0.996264)'>+<g fill='#000' stroke='#000'>+<g stroke-width='0.4'>+<path d='M 0.0 0.0 L 0.0 0.0 L 12.51923 0.0 M 28.45274 6.57263 M 28.45274 6.57263 M 28.45274 9.80179 M 28.45274 9.80179 M 44.38626 0.0 L 56.90549 0.0' fill='none'/>+<g fill='#000' stroke='#000'>+<g stroke-width='0.79999'>+<g stroke-linejoin='bevel'>+<path d='M 12.11923 0.0 L 15.20815 5.9751 L 20.58598 -5.9751 L 25.96382 5.9751 L 31.34166 -5.9751 L 36.7195 5.9751 L 42.09734 -5.9751 L 44.78625 0.0' fill='none'/>+<g stroke-width='0.4'>+<g transform='translate(28.45274,0.0)'>+<g stroke='none' transform='scale(-1.00375,1.00375)translate(-56.856794,-52.257708)scale(-1,-1)'>+<g fill='#000'>+<g stroke='none'/>+</g>+</g>+</g>+</g>+</g>+</g>+</g>+<g transform='translate(22.41324,9.80179)'>+<g stroke='none' transform='scale(-1.00375,1.00375)translate(-56.856794,-52.257708)scale(-1,-1)'>+<g fill='#000'>+<g stroke='none'>+<use x='-56.856794' y='-52.257708' xlink:href='#g0-82'/>+<use x='-49.292264' y='-50.763327' xlink:href='#g1-49'/>+</g>+</g>+</g>+</g>+<path d='M 56.90549 0.0 L 56.90549 0.0 L 69.42471 0.0 M 85.35823 6.57263 M 85.35823 6.57263 M 85.35823 9.80179 M 85.35823 9.80179 M 101.29175 0.0 L 113.81097 0.0' fill='none'/>+<g fill='#000' stroke='#000'>+<g stroke-width='0.79999'>+<g stroke-linejoin='bevel'>+<path d='M 69.02472 0.0 L 72.11363 5.9751 L 77.49147 -5.9751 L 82.86931 5.9751 L 88.24715 -5.9751 L 93.62498 5.9751 L 99.00282 -5.9751 L 101.69174 0.0' fill='none'/>+<g stroke-width='0.4'>+<g transform='translate(85.35823,0.0)'>+<g stroke='none' transform='scale(-1.00375,1.00375)translate(-56.856794,-52.257708)scale(-1,-1)'>+<g fill='#000'>+<g stroke='none'/>+</g>+</g>+</g>+</g>+</g>+</g>+</g>+<g transform='translate(79.31873,9.80179)'>+<g stroke='none' transform='scale(-1.00375,1.00375)translate(-56.856794,-52.257708)scale(-1,-1)'>+<g fill='#000'>+<g stroke='none'>+<use x='-56.856794' y='-52.257708' xlink:href='#g0-82'/>+<use x='-49.292264' y='-50.763327' xlink:href='#g1-50'/>+</g>+</g>+</g>+</g>+</g>+</g>+</g>+</g>+</svg>diff --git a/tests/test_graphical_units/test_img_and_svg.py b/tests/test_graphical_units/test_img_and_svg.pyindex 1a94ddcf2..b234431ab 100644--- a/tests/test_graphical_units/test_img_and_svg.py+++ b/tests/test_graphical_units/test_img_and_svg.py@@ -242,6 +242,17 @@ def test_RotateTransform(scene):scene.wait()+@frames_comparison+def test_path_multiple_moves(scene):+ svg_obj = SVGMobject(+ get_svg_resource("path_multiple_moves.svg"),+ fill_color=WHITE,+ stroke_color=WHITE,+ stroke_width=3,+ )+ scene.add(svg_obj)++@frames_comparisondef test_ImageMobject(scene):file_path = get_svg_resource("tree_img_640x351.png")