TriangleInfo AddTriangle(VertexInfo[] verticesToAdd) { if (!IsRightOfVector(verticesToAdd [0], verticesToAdd [1], verticesToAdd [2])) { VertexInfo tmp = verticesToAdd[2]; verticesToAdd[2] = verticesToAdd[1]; verticesToAdd[1] = tmp; } TriangleInfo triangle = new TriangleInfo(); triangle.vertices = new List <int> (); triangle.edges = new List <string> (); triangle.index = TriangleInfo.nextIndex++; for (int i = 0; i < 3; i++) { triangle.vertices.Add(verticesToAdd[i].index); EdgeInfo edge = _AddEdge(verticesToAdd[i], verticesToAdd[(i + 1) % 3]); triangle.edges.Add(edge.GetSelfEdgeString()); edge.belongToTriangleIndex.Add(triangle.index); } triangles.Add(triangle.index, triangle); return(triangle); }
Dictionary <string, EdgeInfo> DelaunayDivideAndConquer(List <VertexInfo> verticesList) { Dictionary <string, EdgeInfo> newGeneratedEdges = new Dictionary <string, EdgeInfo> (StringComparer.Ordinal); if (verticesList.Count == 2) { EdgeInfo edge = _AddEdge(verticesList [0], verticesList [1]); newGeneratedEdges.Add(edge.GetSelfEdgeString(), edge); } else if (verticesList.Count == 3) { for (int i = 0; i < 3; i++) { EdgeInfo edge = _AddEdge(verticesList [i], verticesList [(i + 1) % 3]); newGeneratedEdges.Add(edge.GetSelfEdgeString(), edge); } } else if (verticesList.Count > 3) { List <VertexInfo> leftVertices = verticesList.Take(verticesList.Count / 2).ToList(); List <VertexInfo> rightVertices = verticesList.Skip(verticesList.Count / 2).ToList(); Dictionary <string, EdgeInfo> leftEdges = DelaunayDivideAndConquer(leftVertices); Dictionary <string, EdgeInfo> rightEdges = DelaunayDivideAndConquer(rightVertices); foreach (KeyValuePair <string, EdgeInfo> pair in leftEdges) { Debug.DrawLine(victim.TransformPoint(vertices[pair.Value.vertexAIndex].vertex), victim.TransformPoint(vertices[pair.Value.vertexBIndex].vertex), Color.blue, 1000); } foreach (KeyValuePair <string, EdgeInfo> pair in rightEdges) { Debug.DrawLine(victim.TransformPoint(vertices[pair.Value.vertexAIndex].vertex), victim.TransformPoint(vertices[pair.Value.vertexBIndex].vertex), Color.red, 1000); } KeyValuePair <VertexInfo, VertexInfo> lowBoundEdge = FindHullEdge(leftVertices, leftEdges, rightVertices, rightEdges, false); KeyValuePair <VertexInfo, VertexInfo> upperBoundEdge = FindHullEdge(leftVertices, leftEdges, rightVertices, rightEdges, true); EdgeInfo lowEdge = _AddEdge(lowBoundEdge.Key, lowBoundEdge.Value); newGeneratedEdges.Add(lowEdge.GetSelfEdgeString(), lowEdge); Debug.DrawLine(victim.TransformPoint(lowBoundEdge.Key.vertex), victim.TransformPoint(lowBoundEdge.Value.vertex), Color.magenta, 1000); VertexInfo L = lowBoundEdge.Key; VertexInfo R = lowBoundEdge.Value; int interIterationNum = 0; while (!(lowBoundEdge.Key.index == upperBoundEdge.Key.index && lowBoundEdge.Value.index == upperBoundEdge.Value.index)) { interIterationNum++; VertexInfo candidateR = R; VertexInfo R1 = FindPrevVertex(R, L, true, rightEdges); while (IsRightOfVector(R, L, R1)) { VertexInfo R2 = FindPrevVertex(R, R1, true, rightEdges); if (!TriangleInfo.IsAPointOutsideTrianglesCircumcircle(R2.vertex, R1.vertex, L.vertex, R.vertex)) { string edgeString = RemoveEdge(R, R1); rightEdges.Remove(edgeString); Debug.DrawLine(victim.TransformPoint(R.vertex), victim.TransformPoint(R1.vertex), Color.gray, 1000); R1 = R2; } else { candidateR = R1; break; } } VertexInfo candidateL = L; VertexInfo L1 = FindPrevVertex(L, R, false, leftEdges); while (IsLeftOfVector(L, R, L1)) { VertexInfo L2 = FindPrevVertex(L, L1, false, leftEdges); if (!TriangleInfo.IsAPointOutsideTrianglesCircumcircle(victim.TransformPoint(L2.vertex), victim.TransformPoint(L1.vertex), victim.TransformPoint(L.vertex), victim.TransformPoint(R.vertex))) { string edgeString = RemoveEdge(L, L1); leftEdges.Remove(edgeString); Debug.DrawLine(victim.TransformPoint(L.vertex), victim.TransformPoint(L1.vertex), Color.gray, 1000); L1 = L2; } else { candidateL = L1; break; } } if (R.index == candidateR.index) { L = candidateL; } else if (L.index == candidateL.index) { R = candidateR; } else if (TriangleInfo.IsAPointOutsideTrianglesCircumcircle(candidateL.vertex, L.vertex, R.vertex, candidateR.vertex)) { R = candidateR; } else { L = candidateL; } EdgeInfo edge = _AddEdge(L, R); if (newGeneratedEdges.ContainsKey(edge.GetSelfEdgeString())) { //Debug.DrawLine(victim.TransformPoint(L.vertex), victim.TransformPoint(R.vertex), Color.cyan, 1000); Debug.DrawLine(victim.TransformPoint(L.vertex), victim.TransformPoint(R.vertex), Color.cyan, 1000); Debug.Log(L.index.ToString()); Debug.Log(upperBoundEdge.Key.index.ToString()); Debug.Log(R.index.ToString()); Debug.Log(upperBoundEdge.Value.index.ToString()); } else { Debug.DrawLine(victim.TransformPoint(L.vertex), victim.TransformPoint(R.vertex), Color.magenta, 1000); } newGeneratedEdges.Add(edge.GetSelfEdgeString(), edge); lowBoundEdge = new KeyValuePair <VertexInfo, VertexInfo>(L, R); } foreach (KeyValuePair <string, EdgeInfo> pair in leftEdges) { newGeneratedEdges.Add(pair.Key, pair.Value); } foreach (KeyValuePair <string, EdgeInfo> pair in rightEdges) { newGeneratedEdges.Add(pair.Key, pair.Value); } iterationNum++; } return(newGeneratedEdges); }