GenerationEdgeInfo GetEdge(CellContentData d) { GenerationEdgeInfo result; if (edges.TryGetValue(d, out result) == false) { result = new GenerationEdgeInfo(d); edges.Add(d, result); } return(result); }
public void ReduceCells() { Dictionary <Cell, GenerationCellInfo> cellsDic = new Dictionary <Cell, GenerationCellInfo>(); foreach (var item in cells) { item.CalculateAngles(); cellsDic.Add(item.cell, item); } //foreach (var item in cells) { // foreach (var c in item.connections) { // Debuger_K.AddLine(c.from.centerV3, c.to.centerV3); // } //} //foreach (var item in cells) { // foreach (var n in item.nodes) { // Debuger_K.AddLabel(n, item.Angle(n)); // } //} int breaker = 0; goto MR_LOOP; MR_LOOP: { breaker++; if (breaker > 50) //no more than 50 iterations //Debug.Log("breaker > 50"); { return; } foreach (var thisCellInfo in cells) { Cell thisCell = thisCellInfo.cell; foreach (var connection in thisCellInfo.connections) { Cell otherCell = connection.connection; GenerationCellInfo otherCellInfo = cellsDic[otherCell]; //connect only of all important properties are equal if (thisCell.area != otherCell.area || thisCell.layer != otherCell.layer || thisCell.graph != otherCell.graph || thisCell.passability != otherCell.passability) { continue; } Vector3 left = connection.leftV3; Vector3 right = connection.rightV3; // merging cells if ((thisCellInfo.Angle(left) + otherCellInfo.Angle(left)) < 180f && (thisCellInfo.Angle(right) + otherCellInfo.Angle(right)) < 180f) { //collecting all edges List <GenerationEdgeInfo> newEdges = new List <GenerationEdgeInfo>(); newEdges.AddRange(thisCellInfo.edges); newEdges.AddRange(otherCellInfo.edges); //we dont need edge where we connect cells so here we remove it GenerationEdgeInfo removeMe = GetEdge(connection.cellData); newEdges.Remove(removeMe); newEdges.Remove(removeMe); HashSet <Vector3> newNodes = new HashSet <Vector3>(); newNodes.UnionWith(thisCellInfo.nodes); newNodes.UnionWith(otherCellInfo.nodes); thisCellInfo.connections.ForEach(con => cellsDic[con.connection].connections.RemoveAll(nb => nb.connection == thisCell)); otherCellInfo.connections.ForEach(con => cellsDic[con.connection].connections.RemoveAll(nb => nb.connection == otherCell)); cells.Remove(thisCellInfo); cells.Remove(otherCellInfo); cellsDic.Remove(thisCell); cellsDic.Remove(otherCell); GenerationCellInfo newInfo = new GenerationCellInfo(thisCell.area, thisCell.passability, thisCell.layer, graph, newNodes, newEdges); newInfo.CalculateAngles(); cellsDic.Add(newInfo.cell, newInfo); cells.Add(newInfo); foreach (var edge in newEdges) { edge.SetCellToEdge(newInfo); } //Debug.Log("simplifyed"); goto MR_LOOP; //begin new cycle if we do something } } } return; } }