-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
[WIP] Clipper2 (beta) wrapper implementation #23559
Conversation
Clipperlib already exists in Godot at thirdparty/misc, same as
triangulation code.
I guess it would be best to expose what it already exists...
…On Tue, Nov 6, 2018 at 2:42 PM Andrii Doroshenko ***@***.***> wrote:
So there's been some recent demand on implementing some kind of ability to
perform various operations on polygons, and this PR brings many tasty stuff
like:
- Polygon merging/subtraction/intersection/xor (basically CSG but in
2D).
- Polygon deflating/offsetting (grow/shrink boundary).
- Curve clipping with polygon.
- Polygon triangulation (new feature as of Clipper2).
I think it would be good to discuss possible implementations as this one
might not be the most optimal one/overkill.
The underlying library differs from already used version present in the
engine which is stable 6.4.2. Clipper2 is beta as of now and this might not
be acceptable, but in my experience it's fairly stable and even already
been used in production by some other Godot users. Either way it's worth to
wrap and bind some useful methods at least for CSG support.
Relevant issues/feature proposals:
#22275 <#22275>, #3821
<#3821>
------------------------------
You can view, comment on, or merge this pull request online at:
#23559
Commit Summary
- Clipper2 wrapper initial implementation
File Changes
- *A* modules/clipper/SCsub
<https://github.com/godotengine/godot/pull/23559/files#diff-0> (10)
- *A* modules/clipper/clipper.cpp
<https://github.com/godotengine/godot/pull/23559/files#diff-1> (406)
- *A* modules/clipper/clipper.h
<https://github.com/godotengine/godot/pull/23559/files#diff-2> (127)
- *A* modules/clipper/config.py
<https://github.com/godotengine/godot/pull/23559/files#diff-3> (7)
- *A* modules/clipper/lib/LICENSE.md
<https://github.com/godotengine/godot/pull/23559/files#diff-4> (27)
- *A* modules/clipper/lib/clipper.cpp
<https://github.com/godotengine/godot/pull/23559/files#diff-5> (1920)
- *A* modules/clipper/lib/clipper.h
<https://github.com/godotengine/godot/pull/23559/files#diff-6> (251)
- *A* modules/clipper/lib/clipper_offset.cpp
<https://github.com/godotengine/godot/pull/23559/files#diff-7> (466)
- *A* modules/clipper/lib/clipper_offset.h
<https://github.com/godotengine/godot/pull/23559/files#diff-8> (80)
- *A* modules/clipper/lib/clipper_triangulation.cpp
<https://github.com/godotengine/godot/pull/23559/files#diff-9> (323)
- *A* modules/clipper/lib/clipper_triangulation.h
<https://github.com/godotengine/godot/pull/23559/files#diff-10> (58)
- *A* modules/clipper/register_types.cpp
<https://github.com/godotengine/godot/pull/23559/files#diff-11> (11)
- *A* modules/clipper/register_types.h
<https://github.com/godotengine/godot/pull/23559/files#diff-12> (4)
Patch Links:
- https://github.com/godotengine/godot/pull/23559.patch
- https://github.com/godotengine/godot/pull/23559.diff
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#23559>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AF-Z2yHRjeUxlPlg1K-YWabMn-4rs4Ieks5uscnsgaJpZM4YQ8f4>
.
|
@reduz: This version of clipper has more features, including triangulation. |
If it's better than our current one we could aswell replace it too, i guess |
Yeah this is the main concern I have with Clipper. It's currently used by the sprite editor plugin: godot/editor/plugins/sprite_editor_plugin.cpp Lines 53 to 64 in 4cf5bb0
godot/editor/plugins/sprite_editor_plugin.cpp Lines 77 to 82 in 4cf5bb0
This is no longer needed in Clipper2 as it guarantees strictly simple polygons as output. I'll see if I can refactor the existing use of clipper with this one. |
Replaced with new Clipper and refactored It's been one year since Angus Johnson released his beta version... Yet he's been active for the last month or so working on this. If anything we could stick to 6.4.2 and just expose methods as reduz suggested. But that would mean no triangulation unit and twice as fast calculations. I wish Angus to get better ❤ |
@@ -101,8 +103,8 @@ namespace clipperlib { | |||
|
|||
PolyPath& PolyPath::GetChild(unsigned index) | |||
{ | |||
if (index < 0 || index >= childs_.size()) | |||
throw ClipperException("invalid range in PolyPath::GetChild."); | |||
// ERR_FAIL_INDEX_V(index, childs_.size(), PolyPath()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how to return non-const reference here...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct me if I'm wrong, but I'm don't think we're supposed to be touching thirdparty sources unless it's a bugfix or we're updating it to a new version -- any code related to Godot's implementation of a library is supposed to go into the related modules/
files, to my knowledge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure either, it wouldn't compile otherwise for some other platforms with not exception-safe code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seeing thirdparty/README.md gives a hint that it can be done via denoting changes and/or generating an actual diff file against the original source.
@Xrayez thank you |
Seems like Clipper2 development has resumed recently! https://sourceforge.net/p/polyclipping/code/541/
https://sourceforge.net/p/polyclipping/bugs/188/#db79
That means current wrapper code needs to be updated too at some point... Taking this into account, it seams reasonable to wrap the older stable version (6.4.2) for now... So I might as well create another pull request just for 6.4.2 and leave this PR until the newer version comes out. |
Perhaps the new triangulation features could be used to add internal vertices to 2d meshes? #20601 |
@blurymind triangulation in new clipper seems to implement ear-clipping algorithm (just guessing), unfortunately it doesn't have ability to specify internal vertices. It could be made possible to emulate internal vertices as holes in clipper, that way it can triangulate the input taking into account hole's vertices. Not sure if that would interfere with mesh deformation, I haven't dealt with it.
Currently it's only used for sprite editor plugin to expand and simplify the mesh of a sprite when creating mesh instance. I've had experience using poly2tri that does allow to create inner vertices via Steiner points, but the library can't handle degenerate input and throws exceptions:
Meaning If inner vertices are added outside polygon, it would crash. |
will it get merged I wonder |
@balenol not in 3.1 at least. The underlying library's version might be unsatisfactory to be merged (because beta). If that's the case, I'll open a separate pull request with exposed older stable Clipper 6.4.2 with the same interface as in this one (currently present in engine), so the update is smooth in case newer version (10.0.0) is declared stable by Angus Johnson. But that would mean no triangulation unit. Most likely we'll just have to stick to 6.4.2 in this case... |
@blurymind For internal vertices, I am not sure if anything other than painting the internal polygons by hand would work, at least not if you want deformation to happen in any way you expect it.. I was planning to implement it that way. |
@reduz I don't mind if it's done by painting them by hand, for as long as we can do it :) |
Recent update by Angus: We'll have to wait for C++ version, the code in this PR shall be changed too then. |
* Replace 6.4.2 with 10.0.0 beta (?) (dedicated offset and triangulation) * Scons: add thirdparty clipper sources via module, not editor * Refactor `expand` method in sprite editor plugin that is used for mesh instance creation
…tion Open paths clipping is supported via specifying `is_a_open` parameter.
Recently I started to question the idea of exposing I was thinking that maybe it would be a better idea to expose the relevant methods in Array merge_poly_2d(const Vector<Vector2> &poly_a, const Vector<Vector2> &polygon_b, bool is_a_open = false);
Array clip_poly_2d(const Vector<Vector2> &poly_a, const Vector<Vector2> &polygon_b, bool is_a_open = false);
Array intersect_poly_2d(const Vector<Vector2> &poly_a, const Vector<Vector2> &polygon_b, bool is_a_open = false);
Array exclude_poly_2d(const Vector<Vector2> &poly_a, const Vector<Vector2> &polygon_b, bool is_a_open = false); // xor
Array offset_poly_2d(const Vector<Vector2> &poly, real_t p_offset, bool is_open = false, int end_type = 0); // negative to shrink, positive to expand
// Array triangulate(const Vector<Vector2> &poly); Note that clipping operation can be done on Offsettng polylines could also have different flavors which would result in a polygon (end types): What do you think? EDIT: also it's unlikely that a stable version is going to be released anytime soon, so I'm thinking to reverting back to 6.4.2 which is currently used by the engine. |
Thx for your work. I hope somedays, something will get merged, as it's a very useful feature for 2d guys. A thing I can't remember, is polygon2d can have holes in godot? Because, how this will get managed if not? |
@TotCac this is the reason I don't use That's quite a lot of work to rewrite different parts of engine to incorporate this natively, so I'm so far satisfied with my own specific implementation, so at least something could be exposed and generalized as much as possible to be useful in many use cases. I also wasn't satisfied with the computational complexity of the underlying triangulation and decomposition libraries in Godot which are quite slow if you want to triangulate a polygon with, for instance, 100 holes in it, and can't handle degenerate input. I'm not really motivated to implement such system too because generalization could negatively impact the performance which I'm not going to trade off in my project. Clipper 10.0.0 triangulation is quite fast and robust but alas it's in beta/alpha for now, so must stick to 6.4.2. |
This one is unlikely to be merged:
Closing as being (hopefully) superseded by #28987 implementation that should be more compact and uses stable version currently used by the engine (6.4.2). |
Note: even though the branch was deleted, most fixes and additional enhancements were translated to yet work-in-progress module with Clipper 10.0.0 under the hood. Once it's stable enough, some features (like robust triangulation) could be used internally and exposed (or even replaced) in Geometry singleton, see #28987. That would mean no more |
Clipper2 is beta and is undergoing a rewrite by Angus Johnson
This PR is at experimental state. I have stable 6.4.2 exposed with the interface to be forward compatible with the new version (10.0.0) and could open a separate PR for it.
There's been some recent demand on implementing some kind of ability to perform various operations on polygons, and this PR brings many tasty stuff like:
The underlying library differs from already used version present in the engine which is stable 6.4.2.
The new version should have a built-in wrapper for float2int conversion as clipping occurs with integer coordinates to avoid floating point accumulation errors.
Notice that I've already made necessary changes so it works as before in places where Clipper is used.
Relevant issues/feature proposals:
#22275, #3821
Module repo:
https://github.com/Xrayez/godot-clipper
Clipper2 source (beta):
https://sourceforge.net/p/polyclipping/code/HEAD/tree/sandbox/Clipper2/