public Shape Clone() { Shape sret = new Shape(); // Create a map, where we can reference an old element and know what the // equivalent is in the new clone. Dictionary <Vert, Vert> oldToNewVert = new Dictionary <Vert, Vert>(); Dictionary <WEdge, WEdge> oldToNewEdge = new Dictionary <WEdge, WEdge>(); Dictionary <Face, Face> oldToNewFace = new Dictionary <Face, Face>(); // Start copying elements to fill the maps. foreach (Vert v in this.vertices) { oldToNewVert.Add(v, sret.AddVertice(v.position)); } foreach (WEdge e in this.edges) { oldToNewEdge.Add(e, sret.AddBlankEdge()); } foreach (Face f in this.faces) { oldToNewFace.Add(f, sret.AddBlankFace()); } // Now that we have everything that could be referenced when constructing // the clone, start making the equivalent cloned connections. foreach (KeyValuePair <Vert, Vert> kvpv in oldToNewVert) { Vert oldV = kvpv.Key; Vert newV = kvpv.Value; foreach (WEdge e in oldV.edges) { newV.edges.Add(oldToNewEdge[e]); } } foreach (KeyValuePair <WEdge, WEdge> kvpe in oldToNewEdge) { WEdge oldE = kvpe.Key; WEdge newE = kvpe.Value; // Transfer mapped conA stuff if (oldE.conA.edge != null) { newE.conA.edge = oldToNewEdge[oldE.conA.edge]; } if (oldE.conA.face != null) { newE.conA.face = oldToNewFace[oldE.conA.face]; } if (oldE.conA.vert != null) { newE.conA.vert = oldToNewVert[oldE.conA.vert]; } // Transfer mapped conB stuff if (oldE.conB.edge != null) { newE.conB.edge = oldToNewEdge[oldE.conB.edge]; } if (oldE.conB.face != null) { newE.conB.face = oldToNewFace[oldE.conB.face]; } if (oldE.conB.vert != null) { newE.conB.vert = oldToNewVert[oldE.conB.vert]; } } foreach (KeyValuePair <Face, Face> kvpf in oldToNewFace) { Face oldF = kvpf.Key; Face newF = kvpf.Value; foreach (WEdge e in oldF.edges) { newF.edges.Add(oldToNewEdge[e]); } } return(sret); }