Exemplo n.º 1
0
        public MeshGraph Clone()
        {
            MeshGraph mg = new MeshGraph();

            mg.triangles = this.triangles;

            foreach (MeshVertex v in vertices)
            {
                MeshVertex new_v = new MeshVertex(v.Position);
                new_v.SetGraphInfo(mg, mg.vertices.Count);
                mg.vertices.Add(new_v);
            }

            foreach (MeshEdge e in edges)
            {
                MeshEdge new_e = new MeshEdge(mg.Eqv(e.V1), mg.Eqv(e.V2));
                new_e.SetGraphInfo(mg, mg.edges.Count);
                mg.edges.Add(new_e);
            }

            foreach (MeshFacet f in facets)
            {
                MeshFacet new_f = new MeshFacet();
                foreach (MeshVertex v in f.vertices)
                {
                    new_f.vertices.Add(mg.Eqv(v));
                }
                new_f.SetGraphInfo(mg, mg.facets.Count);
                mg.facets.Add(new_f);
            }

            // Maintain adjacency cache
            foreach (MeshEdge e in mg.edges)
            {
                MeshEdge old_e = this.Eqv(e);
                e.f1 = mg.Eqv(old_e.f1);
                e.f2 = mg.Eqv(old_e.f2);
            }

            foreach (MeshVertex v in mg.vertices)
            {
                MeshVertex old_v = this.Eqv(v);
                foreach (var entry in old_v.adjacency)
                {
                    v.adjacency.Add(mg.Eqv(entry.Key), mg.Eqv(entry.Value));
                }
            }

            return(mg);
        }
Exemplo n.º 2
0
        public MeshEdge AddEdge(MeshVertex p1, MeshVertex p2,
                                bool check_facet = false)
        {
            MeshEdge e = p1.EdgeConnecting(p2);

            if (e != null)
            {
                return(e);
            }
            if (p1 == p2)
            {
                return(null);
            }

            e = new MeshEdge(p1, p2);
            p1.adjacency.Add(p2, e);
            p2.adjacency.Add(p1, e);

            int i = edges.Count;

            edges.Add(e);
            e.SetGraphInfo(this, i);

            if (check_facet)
            {
                // check the intersection
                MeshFacet           common_facet = null;
                HashSet <MeshFacet> p1f          = new HashSet <MeshFacet>(
                    p1.AdjacencyFacets);
                foreach (MeshFacet f in p2.AdjacencyFacets)
                {
                    if (p1f.Contains(f))
                    {
                        common_facet = f;
                        break;
                    }
                }

                if (common_facet != null)
                {
                    List <MeshVertex> f1 = new List <MeshVertex>();
                    List <MeshVertex> f2 = new List <MeshVertex>();

                    List <MeshVertex> current = f1;
                    foreach (MeshVertex v in common_facet.Vertices)
                    {
                        // if current vertex matches p1 or p2, add this vertex
                        // to both vertex list and switch current to the other one
                        current.Add(v);

                        if (v == p1)
                        {
                            current = current == f1 ? f2 : f1;
                            current.Add(p1);
                        }
                        else if (v == p2)
                        {
                            current = current == f1 ? f2 : f1;
                            current.Add(p2);
                        }
                    }

                    this.RemoveFacet(common_facet);
                    this.AddFacet(f1.ToArray());
                    this.AddFacet(f2.ToArray());
                }
            }

            return(e);
        }