I'm trying to clean up some self-intersecting polygons by splitting them up into multiple simple polygons using Clipper. The results I get with SimplifyPolygons() seems to indicate that clipper allows vertices with the same position. See (http://i.imgur.com/pTg8RvW.png)
Is there any way to force Clipper to return separate polygons as opposed to the above behaviour in the general case? I noticed that I could run SimplifyPolygons twice, once with a positive filltype and once with a negative filltype and this seemed to give me polygons separately, but I don't know if thats reliable in the general case.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This seems to work great! Thank you for the quick reply and example. I have a follow up question. In some cases, the input polygon can't be decomposed any further, but it still has non adjacent duplicate points... see http://i.imgur.com/ZHqTrz2.png for example. Can Clipper detect these cases?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Would you prefer an inner polygon touching an outer polygon instead? I believe that for everyone who wants that, there is at least another who prefers the current implementation.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
My Clipper version is Javascript Clipper 5.0.2.1 (with the above changes) and the code that produced error:
var cpr = new ClipperLib.Clipper();
var weak = [{"X":100,"Y":200},{"X":150,"Y":150},{"X":100,"Y":100},{"X":200,"Y":100},{"X":150,"Y":150},{"X":200,"Y":200}];
// console.log(JSON.stringify(weak));
var simple = cpr.SimplifyPolygon(weak);
// console.log(JSON.stringify(simple));
The example polygon has one weak duplicate point: {"X":150,"Y":150}.
Here is a more complex "simple" polygon (after simplifying in Clipper).
All the points that seem to be together are exactly identical, but there are not duplicate sequential points nor self-intersections on face level, but self-intersections are on vertex level.
To divide this to truly simple polygons, would require it to be divided into pieces.
My eyes say that here is four bottom polygons and one (bigger) upper polygon that has a hole, so after truly-simplifying there should be totally five outer polygons and one hole. On a second look it seems that here is only 1 outer polygon with 5 hole polygons.
How this like truly simplifying could be achieved using Clipper? If not in the current implementation, is it possible to modify the code in some way and how this could be done?
Hi,
I'm trying to clean up some self-intersecting polygons by splitting them up into multiple simple polygons using Clipper. The results I get with SimplifyPolygons() seems to indicate that clipper allows vertices with the same position. See (http://i.imgur.com/pTg8RvW.png)
Is there any way to force Clipper to return separate polygons as opposed to the above behaviour in the general case? I noticed that I could run SimplifyPolygons twice, once with a positive filltype and once with a negative filltype and this seemed to give me polygons separately, but I don't know if thats reliable in the general case.
Hi Preet.
Not without modifying the underlying code.
In the IntersectEdges() method, change the follow from ...
to ...
and I think that you'll get separate polygons at these points of intersection (though I haven't tested this).
Last edit: Angus Johnson 2013-02-28
Hi Angus,
This seems to work great! Thank you for the quick reply and example. I have a follow up question. In some cases, the input polygon can't be decomposed any further, but it still has non adjacent duplicate points... see http://i.imgur.com/ZHqTrz2.png for example. Can Clipper detect these cases?
Detect for what purpose?
Would you prefer an inner polygon touching an outer polygon instead? I believe that for everyone who wants that, there is at least another who prefers the current implementation.
I made a question on
http://stackoverflow.com/questions/16158931/polygon-triangulation-fails-in-pseudo-duplicate-points regarding polygon simplifying. Anyone who knows about terms Simple Polygon and Triangulation are welcome to give an answer.
Clipper does seem to be permissive what it comes to unioning (simplifying) pseudo duplicate points, which causes failures in triangulation routines.
I wonder, why Clipper accepts pseudo duplicates (see the attached image) in Union and Simplify operations?
I tested the suggestion by changing the lines that were provided in the comment at https://sourceforge.net/p/polyclipping/discussion/1148419/thread/4e614a38/#b81a, but there comes "ProcessHorizontal error" and the output has only one polygon, although there should be two separate ones.
My Clipper version is Javascript Clipper 5.0.2.1 (with the above changes) and the code that produced error:
The example polygon has one weak duplicate point: {"X":150,"Y":150}.
The test is at: http://jsbin.com/owivew/1
Have anyone tested that solution in C# or C++?
Last edit: Timo Kähkönen 2013-04-23
Here is a more complex "simple" polygon (after simplifying in Clipper).
All the points that seem to be together are exactly identical, but there are not duplicate sequential points nor self-intersections on face level, but self-intersections are on vertex level.
To divide this to truly simple polygons, would require it to be divided into pieces.
My eyes say that here is four bottom polygons and one (bigger) upper polygon that has a hole, so after truly-simplifying there should be totally five outer polygons and one hole. On a second look it seems that here is only 1 outer polygon with 5 hole polygons.
The polygon can be seen as SVG at http://jsbin.com/ugimab/3. The points of the polygon are:
How this like truly simplifying could be achieved using Clipper? If not in the current implementation, is it possible to modify the code in some way and how this could be done?
Last edit: Timo Kähkönen 2013-04-24