-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Path parsing floating point issue #206
Comments
My guess is this comes from floating point error the gets picked up when figuring out the absolute coordinates of the points. One workaround might be to remove the z from the end and do some hacky logic like: from svgpathtools import parse_path, Line
import numpy as np
def parse_path_hack(d_string):
ends_with_z = d_string.lower().endswith('z')
_d_string = d_string[:-1] if ends_with_z else d_string
path = parse_path(_d_string)
if ends_with_z:
path._closed = True
if np.isclose(path[0].start, path[-1].end):
path[-1].end = path[0].start
else:
path._segments.append(Line(path[-1].end, path[0].start))
return path I think I've seen this issue before and there must be some issue with this type of logic as I never fixed it. I hope that helps. I liked your website by the way. Really cool projects. |
@mathandy Thanks for looking into it. Your solution makes sense. In our case we actually have arbitrary paths coming in (with any number of different commands and points). But we're actually just looking to test "is this path rectangular?" to a certain tolerance. We don't really care how many points there are. So I'm wondering if this would be better for our purposes: def path_is_rectangular(path, tolerance=0.1):
area = path.area()
xmin, xmax, ymin, ymax = path.bbox()
bbox_area = abs((xmax - xmin) * (ymax - ymin))
return np.isclose(area, bbox_area, atol=tolerance) It seems to be working for all of our test cases. Just not sure if we're asking for trouble by hitting all the more complex bits of your path code (working out the area etc.). But maybe it's more resilient for us not to be worrying about the actual points. What do you think? And thanks! I appreciate it 👍 EDIT: I guess that's not a general solution. Since you could have rectangular paths that are "rotated" with respect to their bounding box. Back to the old way I suppose! |
Hi @mathandy. Thanks for your work on this library! We use it for all kinds of stuff.
Just wondering if you had any insight on this issue we're experiencing parsing some very simple path elements.
Parsing this path works fine:
We correctly detect the four line segments with four unique points total representing a closed rectangle.
But this very similar path gives us a different result:
You can see here that at some point in the parsing we pick up a floating point error which results in the end of the fourth line not matching the starting point. We then seem to get a fifth additional line correcting the difference and closing the path due to the final
Z
command.Below is an example SVG which shows that both paths seem to be rendering fine and are detected in tools like Inkscape as having four points only. The red path is
bad
above and the green one isgood
.Do you know where this discrepancy might be coming from? I wonder if it is potentially related to #198. But I've tried your latest commit which includes your fix and the result is the same.
For reference this is running in Python 3.9.7 in Ubuntu.
The text was updated successfully, but these errors were encountered: