/// <summary> /// Finds removed switchpoints which now describe forlorn ends. Add these to ForlornStartNodes. Afterwards all unneeded SwitchPoints are removed. /// </summary> protected virtual void UpdateSwitchPoints() { var forlornStartNodes = (from switchPoint in SwitchPoints where switchPoint.Value.EndPoints.Count == 1 select switchPoint.Key).ToList(); foreach (var forlornStartNode in forlornStartNodes) { ForlornStartNodes.Add(forlornStartNode); } var listToRemove = (from switchPoint in SwitchPoints where switchPoint.Value.EndPoints.Count < 3 select switchPoint.Key).ToList(); foreach (var key in listToRemove) { SwitchPoints.Remove(key); } }
//Forlorn stuff /// <summary> /// Removes Forlorn out of the figure and converts them to LineObstacles. /// </summary> /// <param name="obstacles"></param> protected virtual void GetForlorn(ObstacleCollection obstacles) { var forlornNodes = new Queue <Vector2>(); foreach (var forlornNodeStart in ForlornStartNodes) { forlornNodes.Enqueue(forlornNodeStart); } while (forlornNodes.Count > 0) { var currentItem = forlornNodes.Dequeue(); List <LineSegment> forlornLines; Vector2 lastItemEndPoint; //this is needed for Switch Points below var forlornRoot = GetforlornRoot(currentItem, out forlornLines, out lastItemEndPoint); foreach (var line in forlornLines) { obstacles.Add(new LineObstacle(line.Start, line.End, Layer)); Lines.Remove(line); } if (SwitchPoints.Count == 0) { continue; } SwitchPoint switchPoint; if (!SwitchPoints.TryGetValue(forlornRoot, out switchPoint)) { continue; } if (switchPoint.EndPoints.Count > 0) { switchPoint.EndPoints.Remove(lastItemEndPoint); } if (switchPoint.EndPoints.Count == 1) { forlornNodes.Enqueue(forlornRoot); SwitchPoints.Remove(forlornRoot); } } ForlornStartNodes.Clear(); UpdateSwitchPoints(); }
protected List <LineSegment> WalkFigure(LineNodeDictionary nodes) { var lines = new List <LineSegment>(); var visitedItems = new List <Vector2>(); var nodesToVisit = new Stack <LineSegment>(); var origin = nodes.Keys.First(); var lineSegment = new LineSegment(origin, origin); nodesToVisit.Push(lineSegment); while (nodesToVisit.Count > 0) { var currentItem = nodesToVisit.Pop(); var connectedNodes = GetConnectedNodes(currentItem.End, nodes); if (connectedNodes.Count == 1) //only the item I come from { ForlornStartNodes.Add(currentItem.End); } if (connectedNodes.Count > 2 && !SwitchPoints.ContainsKey(currentItem.End)) { SwitchPoints.Add(currentItem.End, new SwitchPoint(new List <Vector2>(connectedNodes))); } connectedNodes.Remove(currentItem.Start); foreach (var connectedNode in connectedNodes) { if (visitedItems.Contains(connectedNode)) { continue; } lineSegment = AddLine(currentItem.End, connectedNode, lines); nodesToVisit.Push(lineSegment); } visitedItems.Add(currentItem.End); } if (GetConnectedNodes(origin, nodes).Count <= 2) { SwitchPoints.Remove(origin); //remove start point, it's not really a switch point } return(lines); }