internal bool IsJoinedTo(Side otherSide, bool checkVertices = true, LevelVertex[] vertexList = null) { if (otherSide.ConnectedSegment != Segment || GetNumVertices() != otherSide.GetNumVertices()) { return(false); } if (checkVertices) { // Do a vertex test to handle cases where multiple sides are joined (the segment will be illegal, // but we still want predictable behavior) var vertices = vertexList ?? GetAllVertices(); for (int v = 0; v < otherSide.GetNumVertices(); v++) { if (!vertices.Contains(otherSide.GetVertex(v))) { return(false); } } } return(true); }
private void RemoveDuplicateVertices() { var processedSides = new SortedSet <(int segmentNum, int sideNum)>(); for (int segmentNum = 0; segmentNum < Segments.Count; segmentNum++) { var segment = Segments[segmentNum]; for (int sideNum = 0; sideNum < Segments[segmentNum].Sides.Length; sideNum++) { var side = segment.Sides[sideNum]; if (side.ConnectedSegment == null) { // Nothing to do continue; } if (processedSides.Contains((segmentNum, sideNum))) { // Already handled this side continue; } // Find the connected side. GetJoinedSide won't work yet so we have to do it the long way var otherSegment = side.ConnectedSegment; var otherSegmentNum = Segments.IndexOf(otherSegment); Side otherSide = null; int otherSideNum; for (otherSideNum = 0; otherSideNum < otherSegment.Sides.Length; otherSideNum++) { otherSide = otherSegment.Sides[otherSideNum]; if (side.IsJoinedTo(otherSide, checkVertices: false)) { break; } } if (otherSideNum >= otherSegment.Sides.Length) { // This means the sides are only connected in one direction. That could be a problem continue; } // Now match up the vertices // Find the vertex of the other side that matches the first vertex of this side int?matchingVertexNum = null; for (int vertexNum = 0; vertexNum < otherSide.GetNumVertices(); vertexNum++) { if (side.GetVertex(0).Location == otherSide.GetVertex(vertexNum).Location) { matchingVertexNum = vertexNum; break; } } if (!matchingVertexNum.HasValue) { // We could try to find another match... but the level geometry is broken // anyway, so don't worry about it too much continue; } // Walk through vertices in opposite directions - vertex n+1 of this side is joined // to vertex n-1 of the other side for (int vertexNum = 0; vertexNum < side.GetNumVertices(); vertexNum++) { int otherVertexNum = matchingVertexNum.Value - vertexNum; if (otherVertexNum < 0) { otherVertexNum += otherSide.GetNumVertices(); } var vertex = side.GetVertex(vertexNum); var otherVertex = otherSide.GetVertex(otherVertexNum); if (vertex.Location == otherVertex.Location) { MergeVertices(vertex, otherVertex); } } // Add the other side to the processed set (this one won't be seen again anyway) processedSides.Add((otherSegmentNum, otherSideNum)); } } }