Convex decomposition algorithm created by Raimund Seidel Properties: - Decompose the polygon into trapezoids, then triangulate. - To use the trapezoid data, use ConvexPartitionTrapezoid() - Generate a lot of garbage due to incapsulation of the Poly2Tri library. - Running time is O(n log n), n = number of vertices. - Running time is almost linear for most simple polygons. - Does not care about winding order. For more information, see Raimund Seidel's paper "A simple and fast incremental randomized algorithm for computing trapezoidal decompositions and for triangulating polygons" See also: "Computational Geometry", 3rd edition, by Mark de Berg et al, Chapter 6.2 "Computational Geometry in C", 2nd edition, by Joseph O'Rourke Original code from the Poly2Tri project by Mason Green. http://code.google.com/p/poly2tri/source/browse?repo=archive#hg/scala/src/org/poly2tri/seidel This implementation is from Dec 14, 2010
Ejemplo n.º 1
0
        public static List <Vertices> ConvexPartition(Vertices vertices, TriangulationAlgorithm algorithm, bool discardAndFixInvalid = true, float tolerance = 0.001f)
        {
            if (vertices.Count <= 3)
            {
                return new List <Vertices> {
                           vertices
                }
            }
            ;

            List <Vertices> results;

            switch (algorithm)
            {
            case TriangulationAlgorithm.Earclip:
                if (vertices.IsCounterClockWise())
                {
                    Vertices temp = new Vertices(vertices);
                    temp.Reverse();
                    results = EarclipDecomposer.ConvexPartition(temp, tolerance);
                }
                else
                {
                    results = EarclipDecomposer.ConvexPartition(vertices, tolerance);
                }
                break;

            case TriangulationAlgorithm.Bayazit:
                if (!vertices.IsCounterClockWise())
                {
                    Vertices temp = new Vertices(vertices);
                    temp.Reverse();
                    results = BayazitDecomposer.ConvexPartition(temp);
                }
                else
                {
                    results = BayazitDecomposer.ConvexPartition(vertices);
                }
                break;

            case TriangulationAlgorithm.Flipcode:
                if (!vertices.IsCounterClockWise())
                {
                    Vertices temp = new Vertices(vertices);
                    temp.Reverse();
                    results = FlipcodeDecomposer.ConvexPartition(temp);
                }
                else
                {
                    results = FlipcodeDecomposer.ConvexPartition(vertices);
                }
                break;

            case TriangulationAlgorithm.Seidel:
                results = SeidelDecomposer.ConvexPartition(vertices, tolerance);
                break;

            case TriangulationAlgorithm.SeidelTrapezoids:
                results = SeidelDecomposer.ConvexPartitionTrapezoid(vertices, tolerance);
                break;

            case TriangulationAlgorithm.Delauny:
                results = CDTDecomposer.ConvexPartition(vertices);
                break;

            default:
                throw new ArgumentOutOfRangeException("algorithm");
            }

            if (discardAndFixInvalid)
            {
                for (int i = results.Count - 1; i >= 0; i--)
                {
                    Vertices polygon = results[i];

                    if (!ValidatePolygon(polygon))
                    {
                        results.RemoveAt(i);
                    }
                }
            }

            return(results);
        }
Ejemplo n.º 2
0
        public static List <Vertices> ConvexPartition(Vertices vertices, TriangulationAlgorithm algorithm, bool discardAndFixInvalid = true, float tolerance = 0.001f)
        {
            if (vertices.Count <= 3)
            {
                return new List <Vertices> {
                           vertices
                }
            }
            ;

            List <Vertices> results;

            switch (algorithm)
            {
            case TriangulationAlgorithm.Earclip:
                if (Settings.SkipSanityChecks)
                //Debug.Assert(!vertices.IsCounterClockWise(), "The Earclip algorithm expects the polygon to be clockwise.");
                {
                }
                else
                {
                    if (vertices.IsCounterClockWise())
                    {
                        Vertices temp = new Vertices(vertices);
                        temp.Reverse();
                        results = EarclipDecomposer.ConvexPartition(temp, tolerance);
                    }
                    else
                    {
                        results = EarclipDecomposer.ConvexPartition(vertices, tolerance);
                    }
                }
                break;

            case TriangulationAlgorithm.Bayazit:
                if (Settings.SkipSanityChecks)
                //Debug.Assert(vertices.IsCounterClockWise(), "The polygon is not counter clockwise. This is needed for Bayazit to work correctly.");
                {
                }
                else
                {
                    if (!vertices.IsCounterClockWise())
                    {
                        Vertices temp = new Vertices(vertices);
                        temp.Reverse();
                        results = BayazitDecomposer.ConvexPartition(temp);
                    }
                    else
                    {
                        results = BayazitDecomposer.ConvexPartition(vertices);
                    }
                }
                break;

            case TriangulationAlgorithm.Flipcode:
                if (Settings.SkipSanityChecks)
                //Debug.Assert(vertices.IsCounterClockWise(), "The polygon is not counter clockwise. This is needed for Bayazit to work correctly.");
                {
                }
                else
                {
                    if (!vertices.IsCounterClockWise())
                    {
                        Vertices temp = new Vertices(vertices);
                        temp.Reverse();
                        results = FlipcodeDecomposer.ConvexPartition(temp);
                    }
                    else
                    {
                        results = FlipcodeDecomposer.ConvexPartition(vertices);
                    }
                }
                break;

            case TriangulationAlgorithm.Seidel:
                results = SeidelDecomposer.ConvexPartition(vertices, tolerance);
                break;

            case TriangulationAlgorithm.SeidelTrapezoids:
                results = SeidelDecomposer.ConvexPartitionTrapezoid(vertices, tolerance);
                break;

            case TriangulationAlgorithm.Delauny:
                results = CDTDecomposer.ConvexPartition(vertices);
                break;

            default:
                throw new ArgumentOutOfRangeException("algorithm");
            }

            if (discardAndFixInvalid)
            {
                for (int i = results.Count - 1; i >= 0; i--)
                {
                    Vertices polygon = results[i];

                    if (!ValidatePolygon(polygon))
                    {
                        results.RemoveAt(i);
                    }
                }
            }

            return(results);
        }