Skip to content
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

simplify behaves weirdly with fairly simple shapes #920

Closed
ludwick opened this issue Jan 21, 2016 · 6 comments
Closed

simplify behaves weirdly with fairly simple shapes #920

ludwick opened this issue Jan 21, 2016 · 6 comments

Comments

@ludwick
Copy link

ludwick commented Jan 21, 2016

It's not clear when one should use simplify to be honest, but it does some pretty surprising things. Take this example sketch.

Screenshot as well:

screen shot 2016-01-21 at 1 18 05 pm

The top is the original display of CompoundPath objects creating from svg path data. The bottom is applying simplify to a copy each child path of the original (re-positioned down obviously).

Now obviously simplify is more useful on complicated curvy paths, but if you're processing arbitrary inputs you don't know in advance if it would make sense to simplify. In our code we do guard this by checking for the number of child paths and count of segments in those child paths, but it seems weird to just arbitrarily threshold.

@ludwick
Copy link
Author

ludwick commented Jan 21, 2016

Also note that changing the parameter to simplify doesn't substantially improve this.

@ludwick
Copy link
Author

ludwick commented Jan 22, 2016

Digging into source, I think it's just not suitable to use simplify as it is currently written on things that aren't similar to Bezier curves. In PathFitter it seems to be trying to fit everything to a cubic bezier.

@lehni
Copy link
Member

lehni commented Jan 22, 2016

No, that's not really what it's doing. The algorithm was originally developed to fit bezier curves to large datasets of points connected by straight lines, the way you would get them from a pointing device, e.g. the mouse. So it takes these points and tries to create similar lines by fitting as few curves through them as possible. The parameter controls the allowed error.

It is very similar to what Adobe Illustrator is doing in the Pencil Tool when you release the mouse button.

In your example, the curve information (the handles / control points) is actually ignored, only the points that the curves pass through (the anchor points) are taken, and passed through the algorithm. And since there are so few and they are wide apart, you get unpredictable results.

You could try to use flatten() with a low maxDistance first, creating a path with many points, and then use simplify() on that.

@lehni
Copy link
Member

lehni commented Jan 22, 2016

So I don't think this is a bug... But perhaps the issue here is that the documentation is not clear about this?

@ludwick
Copy link
Author

ludwick commented Jan 22, 2016

Thanks that's what we basically concluded once we started looking into it. How do you normally document this kind of thing? Is there some kind of jsdoc on the actual simplify call in the correct js file? I could send a PR when I get a chance (and maybe also update the part of the tutorial that demonstrates simplify.)

@lehni
Copy link
Member

lehni commented Jan 23, 2016

Yes the reference is built out of JSDoc comments, which are translated into this by a customized version of JSDoc 2 that we're using. You can find the source to the simplify() documentation here: https://github.com/paperjs/paper.js/blob/develop/src/path/Path.js#L1045

The current description is indeed very vague. We're always happy to accept PRs with improvements, of course!

@lehni lehni closed this as completed in 3965dd9 Feb 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants