public static IEnumerable <Poly> GetTriangles(this IEnumerable <IPoint> points)
    {
        var delauney = new Delaunator(points.ToArray());

        var triangles = delauney.GetTriangles();

        return(triangles.Select(tri => new Poly()
        {
            Points = tri.Points.ToPointCollection(),
            Index = tri.Index
        }));
    }
Esempio n. 2
0
        /// <summary>
        /// Makes the actual mesh
        /// </summary>
        protected void _redraw()
        {
            if (lines.Count > 0)
            {
                Polygon = new List <DCurve3>();
                foreach (Dataline ring in lines)
                {
                    foreach (VertexLookup v in ring.VertexTable)
                    {
                        VertexTable.Add(v);
                    }
                    DCurve3 curve = new DCurve3();
                    curve.Vector3(ring.GetVertexPositions(), true);
                    Polygon.Add(curve);
                }
            }

            MeshFilter mf = Shape.GetComponent <MeshFilter>();

            MeshCollider[] mc = Shape.GetComponents <MeshCollider>();
            mf.mesh = null;
            Mesh mesh  = new Mesh();
            Mesh imesh = new Mesh();

            mesh.indexFormat  = UnityEngine.Rendering.IndexFormat.UInt32;
            imesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
            Frame3f frame = new Frame3f();

            Vector3[]        vertices;
            GeneralPolygon2d polygon2d;
            Delaunator       delaunator;
            List <int>       triangles = new List <int>();

            try {
                //
                // Map 3d Polygon to the bext fit 2d polygon and also return the frame used for the mapping
                //
                polygon2d = Polygon.ToPolygon(ref frame);

                //
                // calculate the dalaunay triangulation of the 2d polygon
                //
                delaunator = new Delaunator(polygon2d.AllVerticesItr().ToPoints());

                IEnumerable <Vector2d> vlist = delaunator.Points.ToVectors2d();
                vertices = new Vector3[vlist.Count()];

                //
                // for each vertex in the dalaunay triangulatin - map back to a 3d point and also populate the vertex table
                //
                for (int i = 0; i < vlist.Count(); i++)
                {
                    Vector2d v = vlist.ElementAt(i);
                    try {
                        Vector3d v1 = Polygon.AllVertexItr().Find(item => v.Distance(frame.ToPlaneUV((Vector3f)item, 2)) < 0.001);
                        vertices[i] = Shape.transform.InverseTransformPoint((Vector3)v1);
                        VertexLookup vl = VertexTable.Find(item => v1.Distance(item.Com.transform.position) < 0.001);
                        if (vl != null)
                        {
                            vl.pVertex = i;
                        }
                    } catch {
                        Debug.Log("Mesh Error");
                    }
                }

                //
                // eaxtract the triangles from the delaunay triangulation
                //
                IEnumerable <ITriangle> tris = delaunator.GetTriangles();
                for (int i = 0; i < tris.Count(); i++)
                {
                    ITriangle tri = tris.ElementAt(i);

                    if (polygon2d.Contains(tri.CetIncenter()))
                    {
                        int index = 3 * i;
                        triangles.Add(delaunator.Triangles[index]);
                        triangles.Add(delaunator.Triangles[index + 1]);
                        triangles.Add(delaunator.Triangles[index + 2]);
                    }
                }
            } catch (Exception e) {
                throw new Exception("feature is not a valid Polygon : " + e.ToString());
            }

            //
            // build the mesh entity
            //
            mesh.vertices  = vertices;
            mesh.triangles = triangles.ToArray();
            mesh.uv        = BuildUVs(vertices);

            mesh.RecalculateBounds();
            mesh.RecalculateNormals();

            imesh.vertices  = mesh.vertices;
            imesh.triangles = triangles.Reverse <int>().ToArray();
            imesh.uv        = mesh.uv;

            imesh.RecalculateBounds();
            imesh.RecalculateNormals();

            mf.mesh = mesh;
            try {
                mc[0].sharedMesh = mesh;
                mc[1].sharedMesh = imesh;
            } catch (Exception e) {
                Debug.Log(e.ToString());
            }
        }