Пример #1
0
        /// <summary>
        /// Combine a list of triangles into a list of convex polygons.
        ///
        /// Note: This only works on triangles.
        /// </summary>
        ///<param name="triangles">The triangles.</param>
        ///<param name="maxPolys">The maximun number of polygons to return.</param>
        ///<param name="tolerance">The tolerance</param>
        public static List <Vertices> PolygonizeTriangles(List <Vertices> triangles, int maxPolys = int.MaxValue, float tolerance = 0.001f)
        {
            if (triangles.Count <= 0)
            {
                return(triangles);
            }

            List <Vertices> polys = new List <Vertices>();

            bool[] covered = new bool[triangles.Count];
            for (int i = 0; i < triangles.Count; ++i)
            {
                covered[i] = false;

                //Check here for degenerate triangles
                Vertices  triangle = triangles[i];
                TSVector2 a        = triangle[0];
                TSVector2 b        = triangle[1];
                TSVector2 c        = triangle[2];

                if ((a.x == b.x && a.y == b.y) || (b.x == c.x && b.y == c.y) || (a.x == c.x && a.y == c.y))
                {
                    covered[i] = true;
                }
            }

            int polyIndex = 0;

            bool notDone = true;

            while (notDone)
            {
                int currTri = -1;
                for (int i = 0; i < triangles.Count; ++i)
                {
                    if (covered[i])
                    {
                        continue;
                    }

                    currTri = i;
                    break;
                }

                if (currTri == -1)
                {
                    notDone = false;
                }
                else
                {
                    Vertices poly = new Vertices(3);

                    for (int i = 0; i < 3; i++)
                    {
                        poly.Add(triangles[currTri][i]);
                    }

                    covered[currTri] = true;
                    int index = 0;
                    for (int i = 0; i < 2 * triangles.Count; ++i, ++index)
                    {
                        while (index >= triangles.Count)
                        {
                            index -= triangles.Count;
                        }
                        if (covered[index])
                        {
                            continue;
                        }
                        Vertices newP = AddTriangle(triangles[index], poly);
                        if (newP == null)
                        {
                            continue; // is this right
                        }
                        if (newP.Count > Settings.MaxPolygonVertices)
                        {
                            continue;
                        }

                        if (newP.IsConvex())
                        {
                            //Or should it be IsUsable?  Maybe re-write IsConvex to apply the angle threshold from Box2d
                            poly           = new Vertices(newP);
                            covered[index] = true;
                        }
                    }

                    //We have a maximum of polygons that we need to keep under.
                    if (polyIndex < maxPolys)
                    {
                        SimplifyTools.MergeParallelEdges(poly, tolerance);

                        //If identical points are present, a triangle gets
                        //borked by the MergeParallelEdges function, hence
                        //the vertex number check
                        if (poly.Count >= 3)
                        {
                            polys.Add(new Vertices(poly));
                        }
                        else
                        {
                            Debug.WriteLine("Skipping corrupt poly.");
                        }
                    }

                    if (poly.Count >= 3)
                    {
                        polyIndex++; //Must be outside (polyIndex < polysLength) test
                    }
                }
            }

            //TODO: Add sanity check
            //Remove empty vertice collections
            for (int i = polys.Count - 1; i >= 0; i--)
            {
                if (polys[i].Count == 0)
                {
                    polys.RemoveAt(i);
                }
            }

            return(polys);
        }
Пример #2
0
        public static List <Vertices> PolygonizeTriangles(List <Vertices> triangles, int maxPolys = 2147483647, float tolerance = 0.001f)
        {
            bool            flag = triangles.Count <= 0;
            List <Vertices> result;

            if (flag)
            {
                result = triangles;
            }
            else
            {
                List <Vertices> list  = new List <Vertices>();
                bool[]          array = new bool[triangles.Count];
                for (int i = 0; i < triangles.Count; i++)
                {
                    array[i] = false;
                    Vertices  vertices  = triangles[i];
                    TSVector2 tSVector  = vertices[0];
                    TSVector2 tSVector2 = vertices[1];
                    TSVector2 tSVector3 = vertices[2];
                    bool      flag2     = (tSVector.x == tSVector2.x && tSVector.y == tSVector2.y) || (tSVector2.x == tSVector3.x && tSVector2.y == tSVector3.y) || (tSVector.x == tSVector3.x && tSVector.y == tSVector3.y);
                    if (flag2)
                    {
                        array[i] = true;
                    }
                }
                int  num   = 0;
                bool flag3 = true;
                while (flag3)
                {
                    int num2 = -1;
                    for (int j = 0; j < triangles.Count; j++)
                    {
                        bool flag4 = array[j];
                        if (!flag4)
                        {
                            num2 = j;
                            break;
                        }
                    }
                    bool flag5 = num2 == -1;
                    if (flag5)
                    {
                        flag3 = false;
                    }
                    else
                    {
                        Vertices vertices2 = new Vertices(3);
                        for (int k = 0; k < 3; k++)
                        {
                            vertices2.Add(triangles[num2][k]);
                        }
                        array[num2] = true;
                        int l = 0;
                        int m = 0;
                        while (m < 2 * triangles.Count)
                        {
                            while (l >= triangles.Count)
                            {
                                l -= triangles.Count;
                            }
                            bool flag6 = array[l];
                            if (!flag6)
                            {
                                Vertices vertices3 = SimpleCombiner.AddTriangle(triangles[l], vertices2);
                                bool     flag7     = vertices3 == null;
                                if (!flag7)
                                {
                                    bool flag8 = vertices3.Count > Settings.MaxPolygonVertices;
                                    if (!flag8)
                                    {
                                        bool flag9 = vertices3.IsConvex();
                                        if (flag9)
                                        {
                                            vertices2 = new Vertices(vertices3);
                                            array[l]  = true;
                                        }
                                    }
                                }
                            }
                            m++;
                            l++;
                        }
                        bool flag10 = num < maxPolys;
                        if (flag10)
                        {
                            SimplifyTools.MergeParallelEdges(vertices2, tolerance);
                            bool flag11 = vertices2.Count >= 3;
                            if (flag11)
                            {
                                list.Add(new Vertices(vertices2));
                            }
                            else
                            {
                                Debug.WriteLine("Skipping corrupt poly.");
                            }
                        }
                        bool flag12 = vertices2.Count >= 3;
                        if (flag12)
                        {
                            num++;
                        }
                    }
                }
                for (int n = list.Count - 1; n >= 0; n--)
                {
                    bool flag13 = list[n].Count == 0;
                    if (flag13)
                    {
                        list.RemoveAt(n);
                    }
                }
                result = list;
            }
            return(result);
        }