Menu

ClipperLib2 : SimplifyPolygon not available?

2022-02-25
2022-02-26
  • Phil Stopford

    Phil Stopford - 2022-02-25

    Just noticed this isn't available, that I can find. Is this unavailable; if so, what's the best way to reproduce what it did?

     
  • Angus Johnson

    Angus Johnson - 2022-02-25

    Nope, Clipper1 didn't do this well, and the code was complicated and messy, so it's been removed. There are currently no functions or methods to do this, at least until I can figure out how to do this better than it was done in Clipper1.

     
  • Phil Stopford

    Phil Stopford - 2022-02-25

    Naively, I thought this might be done by looking for angles ~180 degrees (where epsilon is the tolerance).

     
  • Angus Johnson

    Angus Johnson - 2022-02-25

    Naively, I thought this might be done by looking for angles ~180 degrees (where epsilon is the tolerance).

    I'm very open to colaboration and incorporating useful code. 😁

     
  • Phil Stopford

    Phil Stopford - 2022-02-25

    I'll play with it when I get the migration sorted and start running tests. I'm limited to C# but hopefully that could still be useful; this is also why I pointed at the github repository earlier. You'll see some customizations there already :)

     
  • Phil Stopford

    Phil Stopford - 2022-02-25

    This is what I was thinking of - I had the code in my DesignLibs_GPL project so pulled this out as an idea. From inspection of ClipperLib1, the SimplifyOption was really a Union (to tidy self-intersections) and then a colinear removal. I'd assume the colinear removal is the missing part to get this going in ClipperLib2, but admit to being uncertain. This code also assumes a closed shape because that's my use case

        public static Paths stripColinear(Paths source, double angularTolerance = 0.0f)
        {
            return source.Select(t => pStripColinear(t, angularTolerance)).ToList();
        }
    
        public static Path stripColinear(Path source, double angularTolerance = 0.0f)
        {
            return pStripColinear(source, angularTolerance);
        }
    
        private static Path pStripColinear(Path source, double angularTolerance = 0.0f)
        {
            switch (source.Count)
            {
                case < 3:
                    return source;
            }
    
            Path ret = new();
    
            for (int pt = 0; pt < source.Count; pt++)
            {
                Point64 interSection_A, interSection_B, interSection_C;
                switch (pt)
                {
                    // Assess angle.
                    case 0:
                        interSection_B = source[^1]; // map to last point
                        interSection_C = source[pt];
                        interSection_A = source[pt + 1];
                        break;
                    default:
                    {
                        if (pt == source.Count - 1) // last point in the list
                        {
                            interSection_B = source[pt - 1];
                            interSection_C = source[pt];
                            interSection_A = source[0]; // map to the first point
                        }
                        else
                        {
                            interSection_B = source[pt - 1];
                            interSection_C = source[pt];
                            interSection_A = source[pt + 1];
                        }
    
                        break;
                    }
                }
    
                double theta = pAngleBetweenPoints(interSection_A, interSection_B, interSection_C, false);
    
                if (pt == 0 || Math.Abs(theta - 180) > angularTolerance)
                {
                    ret.Add(new Point64(source[pt]));
                }
            }
            return ret;
        }
    
     
  • Angus Johnson

    Angus Johnson - 2022-02-26

    SimplifyOption was really a Union (to tidy self-intersections) and then a colinear removal.

    Neither self-intersections or colinear spikes should be an issue in Clipper2.
    It's the joining of touching polygons in solutions that was unsatisfactory in Clipper1 and left out of Clipper2.

     
MongoDB Logo MongoDB