Exemple #1
0
        // TS - public static List<Vertices> ConvexPartition(Vertices vertices, TriangulationAlgorithm algorithm, bool discardAndFixInvalid = true, FP tolerance = 0.001f)
        public static List <Vertices> ConvexPartition(Vertices vertices, TriangulationAlgorithm algorithm, bool discardAndFixInvalid, FP tolerance)
        {
            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);
        }
        private static List <Vertices> TriangulatePolygon(Vertices vertices, FP tolerance)
        {
            bool            flag = vertices.Count < 3;
            List <Vertices> result;

            if (flag)
            {
                result = new List <Vertices>();
            }
            else
            {
                List <Vertices> list = new List <Vertices>();
                Vertices        pin  = new Vertices(vertices);
                Vertices        vertices2;
                Vertices        vertices3;
                bool            flag2 = EarclipDecomposer.ResolvePinchPoint(pin, out vertices2, out vertices3, tolerance);
                if (flag2)
                {
                    List <Vertices> list2 = EarclipDecomposer.TriangulatePolygon(vertices2, tolerance);
                    List <Vertices> list3 = EarclipDecomposer.TriangulatePolygon(vertices3, tolerance);
                    bool            flag3 = list2.Count == -1 || list3.Count == -1;
                    if (flag3)
                    {
                        throw new Exception("Can't triangulate your polygon.");
                    }
                    for (int i = 0; i < list2.Count; i++)
                    {
                        list.Add(new Vertices(list2[i]));
                    }
                    for (int j = 0; j < list3.Count; j++)
                    {
                        list.Add(new Vertices(list3[j]));
                    }
                    result = list;
                }
                else
                {
                    Vertices[] array  = new Vertices[vertices.Count - 2];
                    int        num    = 0;
                    FP[]       array2 = new FP[vertices.Count];
                    FP[]       array3 = new FP[vertices.Count];
                    for (int k = 0; k < vertices.Count; k++)
                    {
                        array2[k] = vertices[k].x;
                        array3[k] = vertices[k].y;
                    }
                    int l = vertices.Count;
                    while (l > 3)
                    {
                        int num2 = -1;
                        FP  y    = -10f;
                        for (int m = 0; m < l; m++)
                        {
                            bool flag4 = EarclipDecomposer.IsEar(m, array2, array3, l);
                            if (flag4)
                            {
                                int       num3      = EarclipDecomposer.Remainder(m - 1, l);
                                int       num4      = EarclipDecomposer.Remainder(m + 1, l);
                                TSVector2 tSVector  = new TSVector2(array2[num4] - array2[m], array3[num4] - array3[m]);
                                TSVector2 tSVector2 = new TSVector2(array2[m] - array2[num3], array3[m] - array3[num3]);
                                TSVector2 tSVector3 = new TSVector2(array2[num3] - array2[num4], array3[num3] - array3[num4]);
                                tSVector.Normalize();
                                tSVector2.Normalize();
                                tSVector3.Normalize();
                                FP fP;
                                MathUtils.Cross(ref tSVector, ref tSVector2, out fP);
                                fP = FP.Abs(fP);
                                FP fP2;
                                MathUtils.Cross(ref tSVector2, ref tSVector3, out fP2);
                                fP2 = FP.Abs(fP2);
                                FP fP3;
                                MathUtils.Cross(ref tSVector3, ref tSVector, out fP3);
                                fP3 = FP.Abs(fP3);
                                FP   fP4   = TSMath.Min(fP, TSMath.Min(fP2, fP3));
                                bool flag5 = fP4 > y;
                                if (flag5)
                                {
                                    num2 = m;
                                    y    = fP4;
                                }
                            }
                        }
                        bool flag6 = num2 == -1;
                        if (flag6)
                        {
                            for (int n = 0; n < num; n++)
                            {
                                list.Add(array[n]);
                            }
                            result = list;
                            return(result);
                        }
                        l--;
                        FP[] array4 = new FP[l];
                        FP[] array5 = new FP[l];
                        int  num5   = 0;
                        for (int num6 = 0; num6 < l; num6++)
                        {
                            bool flag7 = num5 == num2;
                            if (flag7)
                            {
                                num5++;
                            }
                            array4[num6] = array2[num5];
                            array5[num6] = array3[num5];
                            num5++;
                        }
                        int num7 = (num2 == 0) ? l : (num2 - 1);
                        int num8 = (num2 == l) ? 0 : (num2 + 1);
                        EarclipDecomposer.Triangle triangle = new EarclipDecomposer.Triangle(array2[num2], array3[num2], array2[num8], array3[num8], array2[num7], array3[num7]);
                        array[num] = triangle;
                        num++;
                        array2 = array4;
                        array3 = array5;
                    }
                    EarclipDecomposer.Triangle triangle2 = new EarclipDecomposer.Triangle(array2[1], array3[1], array2[2], array3[2], array2[0], array3[0]);
                    array[num] = triangle2;
                    num++;
                    for (int num9 = 0; num9 < num; num9++)
                    {
                        list.Add(new Vertices(array[num9]));
                    }
                    result = list;
                }
            }
            return(result);
        }
        private static bool ResolvePinchPoint(Vertices pin, out Vertices poutA, out Vertices poutB, FP tolerance)
        {
            poutA = new Vertices();
            poutB = new Vertices();
            bool flag = pin.Count < 3;
            bool result;

            if (flag)
            {
                result = false;
            }
            else
            {
                bool flag2 = false;
                int  num   = -1;
                int  num2  = -1;
                for (int i = 0; i < pin.Count; i++)
                {
                    for (int j = i + 1; j < pin.Count; j++)
                    {
                        bool flag3 = FP.Abs(pin[i].x - pin[j].x) < tolerance && FP.Abs(pin[i].y - pin[j].y) < tolerance && j != i + 1;
                        if (flag3)
                        {
                            num   = i;
                            num2  = j;
                            flag2 = true;
                            break;
                        }
                    }
                    bool flag4 = flag2;
                    if (flag4)
                    {
                        break;
                    }
                }
                bool flag5 = flag2;
                if (flag5)
                {
                    int  num3  = num2 - num;
                    bool flag6 = num3 == pin.Count;
                    if (flag6)
                    {
                        result = false;
                        return(result);
                    }
                    for (int k = 0; k < num3; k++)
                    {
                        int index = EarclipDecomposer.Remainder(num + k, pin.Count);
                        poutA.Add(pin[index]);
                    }
                    int num4 = pin.Count - num3;
                    for (int l = 0; l < num4; l++)
                    {
                        int index2 = EarclipDecomposer.Remainder(num2 + l, pin.Count);
                        poutB.Add(pin[index2]);
                    }
                }
                result = flag2;
            }
            return(result);
        }
 public static List <Vertices> ConvexPartition(Vertices vertices, FP tolerance)
 {
     Debug.Assert(vertices.Count > 3);
     Debug.Assert(!vertices.IsCounterClockWise());
     return(EarclipDecomposer.TriangulatePolygon(vertices, tolerance));
 }
Exemple #5
0
        public static List <Vertices> ConvexPartition(Vertices vertices, TriangulationAlgorithm algorithm, bool discardAndFixInvalid, FP tolerance)
        {
            bool            flag = vertices.Count <= 3;
            List <Vertices> result;

            if (flag)
            {
                result = new List <Vertices>
                {
                    vertices
                };
            }
            else
            {
                List <Vertices> list;
                switch (algorithm)
                {
                case TriangulationAlgorithm.Earclip:
                {
                    bool flag2 = vertices.IsCounterClockWise();
                    if (flag2)
                    {
                        Vertices vertices2 = new Vertices(vertices);
                        vertices2.Reverse();
                        list = EarclipDecomposer.ConvexPartition(vertices2, tolerance);
                    }
                    else
                    {
                        list = EarclipDecomposer.ConvexPartition(vertices, tolerance);
                    }
                    break;
                }

                case TriangulationAlgorithm.Bayazit:
                {
                    bool flag3 = !vertices.IsCounterClockWise();
                    if (flag3)
                    {
                        Vertices vertices3 = new Vertices(vertices);
                        vertices3.Reverse();
                        list = BayazitDecomposer.ConvexPartition(vertices3);
                    }
                    else
                    {
                        list = BayazitDecomposer.ConvexPartition(vertices);
                    }
                    break;
                }

                case TriangulationAlgorithm.Flipcode:
                {
                    bool flag4 = !vertices.IsCounterClockWise();
                    if (flag4)
                    {
                        Vertices vertices4 = new Vertices(vertices);
                        vertices4.Reverse();
                        list = FlipcodeDecomposer.ConvexPartition(vertices4);
                    }
                    else
                    {
                        list = FlipcodeDecomposer.ConvexPartition(vertices);
                    }
                    break;
                }

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

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

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

                default:
                    throw new ArgumentOutOfRangeException("algorithm");
                }
                if (discardAndFixInvalid)
                {
                    for (int i = list.Count - 1; i >= 0; i--)
                    {
                        Vertices polygon = list[i];
                        bool     flag5   = !Triangulate.ValidatePolygon(polygon);
                        if (flag5)
                        {
                            list.RemoveAt(i);
                        }
                    }
                }
                result = list;
            }
            return(result);
        }