Beispiel #1
0
        /// <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);
            }
        }
Beispiel #2
0
        //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();
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        /// <summary>
        /// Tries to to merge neighbor lines.
        /// </summary>
        public void Optimize()
        {
            var optimizedLines        = new List <LineSegment>();
            var optimizedSwitchPoints = new SerializableDictionary <Vector2, SwitchPoint>();

            Nodes.Clear();
            var     currentDirection = Direction.None;
            Vector2?start            = null;
            Vector2?end = null;

            for (var i = 0; i < Lines.Count; i++)
            {
                var directedLine = GetLine(Lines[i].Start, Lines[i].End, true);
                if (end != null && !HasConnection(end.Value, directedLine.Start))
                {
                    currentDirection = Direction.None;
                }

                var isLast                = i == Lines.Count - 1;
                var isSwitchPoint         = start != null && SwitchPoints.ContainsKey(start.Value);
                var isSwitchPointInvented = end != null && SwitchPoints.ContainsKey(end.Value);

                if (directedLine.Direction != currentDirection || isSwitchPointInvented)
                {
                    if (start != null)
                    {
                        AddLine(start.Value, end.Value, optimizedLines);
                        if (isSwitchPoint)
                        {
                            AddSwitchPoint(start.Value, end.Value, optimizedSwitchPoints);
                        }
                        if (isSwitchPointInvented)
                        {
                            AddSwitchPoint(end.Value, start.Value, optimizedSwitchPoints);
                        }
                    }
                    currentDirection = directedLine.Direction;
                    start            = directedLine.Start;
                }

                if (isLast && start != null && end != null) //ToDo: not sure if these SwitchPoints here are correct. Create a simple figure to test it.
                {
                    AddLine(start.Value, directedLine.End, optimizedLines);
                    if (isSwitchPoint)
                    {
                        AddSwitchPoint(start.Value, directedLine.End, optimizedSwitchPoints);
                    }
                    if (isSwitchPointInvented)
                    {
                        AddSwitchPoint(directedLine.End, start.Value, optimizedSwitchPoints);
                    }
                    if (SwitchPoints.ContainsKey(directedLine.Start))
                    {
                        AddSwitchPoint(directedLine.Start, end.Value, optimizedSwitchPoints);
                    }
                    if (SwitchPoints.ContainsKey(directedLine.End))
                    {
                        AddSwitchPoint(directedLine.End, start.Value, optimizedSwitchPoints);
                    }
                    //if (SwitchPoints.ContainsKey(directedLine.Start))
                    //    AddSwitchPoint(directedLine.Start, directedLine.End, optimizedSwitchPoints);
                    //if (SwitchPoints.ContainsKey(directedLine.End))
                    //    AddSwitchPoint(directedLine.End, directedLine.Start, optimizedSwitchPoints);
                }
                end = directedLine.End;
            }
            Lines        = optimizedLines;
            SwitchPoints = optimizedSwitchPoints;
        }