public override void HandleMouseMove(MouseEventArgs e)
        {
            base.HandleMouseMove(e);
            if (prePoint == default)
            {
                return;
            }

            var targetPoints = new List <Point>(points);

            var currentPoint = e.GetPosition(Canvas);

            currentPoint = AlignPointToXY(currentPoint);
            targetPoints.Add(currentPoint);

            if (element == null)
            {
                element = new DrawingLineString(targetPoints, targetPoints.First() == targetPoints.Last());
                Canvas.AddVisual(element);
            }
            else
            {
                (element as DrawingLineString).Update(targetPoints, targetPoints.First() == targetPoints.Last());
            }

            element.Render(DrawingCanvas.DefaultPen);
        }
 public static Geometry ToNTSGeometry(this DrawingLineString drawingLineString)
 {
     return(new LineString(drawingLineString.Points.Select(p => p.ToCoordinate()).ToArray()));
 }
Example #3
0
        private void ComputeShorestPath()
        {
            var components = canvas.GetDrawingComponents();
            var points     = components.OfType <DrawingPoint>().ToList();

            if (points.Count < 2)
            {
                if (paths.Count > 0)
                {
                    paths.ForEach(p => canvas.RemoveVisual(p));
                    paths.Clear();
                }
                return;
            }

            if (points.Count % 2 != 0)
            {
                points.RemoveAt(points.Count - 1);
            }

            var pointsCount = points.Count;

            if (preVersion == -1 || (preVersion != canvas._version))
            {
                var geom = GeometryCombiner.Combine(components.Where(c => !c.IsReadOnly && !(c is DrawingPoint)).Select(c => c.ToNTSGeometry()));
                finder = new ShortestAvoidancePathFinder(geom);
                finder.BufferDistance        = finderSettings.BufferDistance;
                finder.InternalPointBehavior = (BreakThroughObstaclesBahavior)finderSettings.ObstacleBehavior;
                finder.BuildVisualGraph();
                preVersion = canvas._version;
            }

            finder.InternalPointBehavior = (BreakThroughObstaclesBahavior)finderSettings.ObstacleBehavior;

            int pathIndex = 0;

            for (int i = 0; i < pointsCount; i += 2, pathIndex++)
            {
                var path = finderSettings.PathCategory == 0 ?
                           finder.FindShortestPath(points[i].Point.ToCoordinate(), points[i + 1].Point.ToCoordinate())
                    : finder.FindAlongShortestPath(points[i].Point.ToCoordinate(), points[i + 1].Point.ToCoordinate());

                if (path.Count == 0)
                {
                    pathIndex--;
                    continue;
                }

                var coos = path.Select(l => l.P0).ToList();
                coos.Add(path.Last().P1);
                var targetPoints = coos.Select(c => c.ToPoint());

                if (pathIndex < paths.Count)
                {
                    paths[pathIndex].Update(targetPoints, false);
                    paths[pathIndex].Render(DrawingCanvas.HighLightPen);
                }
                else
                {
                    var drawingLineString = new DrawingLineString(targetPoints, false);
                    drawingLineString.IsReadOnly = true;
                    drawingLineString.Render(DrawingCanvas.HighLightPen);
                    canvas.AddVisual(drawingLineString);
                    paths.Add(drawingLineString);
                }
            }

            if (pathIndex < paths.Count)
            {
                for (int i = pathIndex; i < paths.Count; i++)
                {
                    canvas.RemoveVisual(paths[i]);
                }
                paths.RemoveRange(pathIndex, paths.Count - pathIndex);
            }
        }