Exemplo n.º 1
0
        public List <WirePath> PathLines(PlacementInfo placements)
        {
            try
            {
                int rerouteCount = 0;
                Dictionary <Line, int> repathCounter = new Dictionary <Line, int>();


                List <LineInfo> sortedLines = new List <LineInfo>();
                foreach ((IOInfo start, IOInfo end) in Connections.GetAllConnectionLines(placements))
                {
                    LineInfo line = new LineInfo(start, end);
                    sortedLines.Add(line);
                    repathCounter.Add(line.GetLine(), 0);
                }

                Queue <LineInfo> linesPriority = new Queue <LineInfo>(sortedLines.OrderBy(x => x.GetScore()));

                RouterBoard board = new RouterBoard(placements.SpaceNeeded);
                board.PrepareBoard(placements.UsedSpace.Values.ToList());
                board.CreateCheckpoint();

                List <WirePath> paths = new List <WirePath>();
                while (linesPriority.Count > 0)
                {
                    //Debug.WriteLine(linesPriority.Count);
                    (IOInfo start, IOInfo end) = linesPriority.Dequeue();

                    Rectangle?startRect = null;
                    Rectangle?endRect   = null;
                    if (placements.UsedSpace.ContainsKey(start.Node))
                    {
                        startRect = placements.UsedSpace[start.Node];
                    }
                    if (placements.UsedSpace.ContainsKey(end.Node))
                    {
                        endRect = placements.UsedSpace[end.Node];
                    }

                    WirePath path = PathLine(board, start, end, startRect, endRect, paths);
                    Debug.Assert(path.StartIO == start && path.EndIO == end);

                    List <WirePath> needsRepathing = new List <WirePath>();
                    foreach (var oldPath in paths)
                    {
                        if (!path.CanCoexist(oldPath))
                        {
                            needsRepathing.Add(oldPath);
                        }
                    }
                    foreach (var repath in needsRepathing)
                    {
                        LineInfo line = new LineInfo(repath.StartIO, repath.EndIO);
                        if (repathCounter[line.GetLine()]++ >= 20)
                        {
                            continue;
                        }

                        paths.Remove(repath);

                        linesPriority.Enqueue(line);
                    }
                    paths.Add(path);
                }

                RefineWirePaths(board, paths);
                return(paths);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
                throw;
            }
        }