示例#1
0
    // Extracts edge and vertex info from TriangletNet Delauney data structure.
    public void ObtainVerticesEdgesAndFaces()
    {
        print("CONVERTING TRIANGLENET OUTPUT TO UNITY MESH.");

        TriangleNet.Topology.DCEL.DcelMesh mesh2 = this.mesh2;

        vertices = new Vector3[mesh2.Vertices.Count];

        for (int indver = 0; indver < mesh2.Vertices.Count; ++indver)
        {
            vertices[indver].x = (float)mesh2.Vertices[indver].X;
            vertices[indver].z = (float)mesh2.Vertices[indver].Y;
        }

        halfedges = new HalfEdge[mesh2.HalfEdges.Count];

        for (int indhe = 0; indhe < mesh2.HalfEdges.Count; ++indhe)
        {
            halfedges[indhe].id      = mesh2.HalfEdges[indhe].ID;
            halfedges[indhe].twin_id = mesh2.HalfEdges[indhe].Twin.ID;
            halfedges[indhe].start   = (int)mesh2.HalfEdges[indhe].Origin.id;
            halfedges[indhe].end     = (int)mesh2.HalfEdges[indhe].Twin.Origin.id;
            halfedges[indhe].face    = mesh2.HalfEdges[indhe].Face.ID;
        }

        faces = new VoronoiFace[mesh2.Faces.Count];

        for (int indf = 0; indf < mesh2.Faces.Count; ++indf)
        {
            faces[indf].merged   = false;
            faces[indf].origin_x = (float)mesh2.Faces[indf].generator.X;
            faces[indf].origin_y = (float)mesh2.Faces[indf].generator.Y;
            faces[indf].id       = mesh2.Faces[indf].ID;

            faces[indf].half_edges = new List <HalfEdge>();
            faces[indf].neighbors  = new List <int>();
            for (int indhe = 0; indhe < halfedges.Length; ++indhe)
            {
                if (halfedges[indhe].face == mesh2.Faces[indf].ID)
                {
                    faces[indf].half_edges.Add(halfedges[indhe]);
                    faces[indf].neighbors.Add(halfedges[halfedges[indhe].twin_id].face);
                }
            }
        }
    }
示例#2
0
    public virtual void GenerateMap()
    {
        print("GENERATING TRANGULATION.");

        UnityEngine.Random.InitState(0);

        // What does this do?
        float[] seed = new float[octaves];

        for (int i = 0; i < octaves; i++)
        {
            seed[i] = UnityEngine.Random.Range(0.0f, 100.0f);
        }

        Polygon polygon = new Polygon();

        for (int i = 0; i < randomPoints; i++)
        {
            polygon.Add(new Vertex(UnityEngine.Random.Range(0.0f, xsize), UnityEngine.Random.Range(0.0f, ysize)));
        }

        // Generate Delauney triangulation from points.
        TriangleNet.Meshing.ConstraintOptions options = new TriangleNet.Meshing.ConstraintOptions()
        {
            ConformingDelaunay = true
        };
        TriangleNet.Mesh mesh = (TriangleNet.Mesh)polygon.Triangulate(options);
        // Generate Voronoi Tesselation from Delauney triangulation.
        //mesh2 = (TriangleNet.Topology.DCEL.DcelMesh)(new StandardVoronoi(mesh, new TriangleNet.Geometry.Rectangle(0f, 0f, (float)xsize, (float)ysize)));
        mesh2 = (TriangleNet.Topology.DCEL.DcelMesh)(new BoundedVoronoi(mesh));

        // Post-processing and object-generation.
        ObtainVerticesEdgesAndFaces();
        //RemoveShortEdges();
        GenerateMesh();
        MergeSmallFaces();
        PopulateFacesManagers();

        // Drawing calls. (Explicit or implicit.)
        DrawEdges();
        DrawEdgeLabels();
        DrawFaceLabels();
        //GenerateVertexSpheres();
        GenerateFaceCenterSpheres();
    }
示例#3
0
        public static DcelMesh ToDCEL(Mesh mesh)
        {
            var dcel = new DcelMesh();

            var vertices = new HVertex[mesh.vertices.Count];
            var faces = new Face[mesh.triangles.Count];

            dcel.HalfEdges.Capacity = 2 * mesh.NumberOfEdges;

            mesh.Renumber();

            HVertex vertex;

            foreach (var v in mesh.vertices.Values)
            {
                vertex = new HVertex(v.x, v.y);
                vertex.id = v.id;
                vertex.label = v.label;

                vertices[v.id] = vertex;
            }

            // Maps a triangle to its 3 edges (used to set next pointers).
            var map = new List<HalfEdge>[mesh.triangles.Count];

            Face face;

            foreach (var t in mesh.triangles)
            {
                face = new Face(null);
                face.id = t.id;

                faces[t.id] = face;

                map[t.id] = new List<HalfEdge>(3);
            }

            Otri tri = default(Otri), neighbor = default(Otri);
            TriangleNet.Geometry.Vertex org, dest;

            int id, nid, count = mesh.triangles.Count;

            HalfEdge edge, twin, next;

            var edges = dcel.HalfEdges;

            // Count half-edges (edge ids).
            int k = 0;

            // Maps a vertex to its leaving boundary edge.
            var boundary = new Dictionary<int, HalfEdge>();

            foreach (var t in mesh.triangles)
            {
                id = t.id;

                tri.tri = t;

                for (int i = 0; i < 3; i++)
                {
                    tri.orient = i;
                    tri.Sym(ref neighbor);

                    nid = neighbor.tri.id;

                    if (id < nid || nid < 0)
                    {
                        face = faces[id];

                        // Get the endpoints of the current triangle edge.
                        org = tri.Org();
                        dest = tri.Dest();

                        // Create half-edges.
                        edge = new HalfEdge(vertices[org.id], face);
                        twin = new HalfEdge(vertices[dest.id], nid < 0 ? Face.Empty : faces[nid]);

                        map[id].Add(edge);

                        if (nid >= 0)
                        {
                            map[nid].Add(twin);
                        }
                        else
                        {
                            boundary.Add(dest.id, twin);
                        }

                        // Set leaving edges.
                        edge.origin.leaving = edge;
                        twin.origin.leaving = twin;

                        // Set twin edges.
                        edge.twin = twin;
                        twin.twin = edge;

                        edge.id = k++;
                        twin.id = k++;

                        edges.Add(edge);
                        edges.Add(twin);
                    }
                }
            }

            // Set next pointers for each triangle face.
            foreach (var t in map)
            {
                edge = t[0];
                next = t[1];

                if (edge.twin.origin.id == next.origin.id)
                {
                    edge.next = next;
                    next.next = t[2];
                    t[2].next = edge;
                }
                else
                {
                    edge.next = t[2];
                    next.next = edge;
                    t[2].next = next;
                }
            }

            // Resolve boundary edges.
            foreach (var e in boundary.Values)
            {
                e.next = boundary[e.twin.origin.id];
            }

            dcel.Vertices.AddRange(vertices);
            dcel.Faces.AddRange(faces);

            return dcel;
        }