Fix arc handling for VMobjectFromSVGPath
when a matrix transform is present in the SVG
#2322
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Handling arcs in
VMobjectFromSVGPath
is currently flawed. It works well when there is no matrix transformation in the SVG, but when such transformations are present, the behavior becomes problematic. The following comparison illustrates the issue (see the raw SVG file in the Test section):When opening the SVG file in VS Code, the expected rendering is shown on the left, whereas the right image shows the distorted result rendered by Manim. The issue mainly affects the "wing" part of the SVG.
Based on my analysis, this problem is caused by the
svgelements
library. When handling arcs with transformations, the library only transforms a few key points of the arc and assumes that the vectors fromarc.center
toarc.prx
(major axis endpoint) andarc.pry
(minor axis endpoint) remain perpendicular. While this approach correctly handles translations and rotations, it fails for shear matrix transformations, leading to incorrect path calculations.To address this, I devised a solution leveraging
path.values['transform']
, which stores the cumulative effect of all parent group transformations. The proposed approach follows these steps:arc.center
toarc.prx
andarc.pry
remain perpendicular.This method correctly handles arc paths and eliminates the need for
path_obj.approximate_arcs_with_quads()
.See the Test section for detailed comparisons.
Proposed changes
VMobjectFromSVGPath
when handling arcsTest
Code:
to.svg
This is an example from jkjkil4/JAnim#12
rings.svg
This is an example from SVG Element Reference
insect.svg
The rings.svg and insect.svg files serve as controls to ensure that no unintended side effects occur in unrelated cases.
Result:
Performance Comparison:
I conducted a rough performance comparison on my device (not a strict benchmark):