private IDictionary <TVertex, KeyValuePair <TVertex, Rect> > getSizesCollection(TEdge ctrl, Point end_point) { var list = VertexSizes.Where(a => a.Key.ID != ctrl.Source.ID && a.Key.ID != ctrl.Target.ID).OrderByDescending(a => MathHelper.GetDistance(VertexPositions[a.Key], end_point)).ToDictionary(a => a.Key); // new Dictionary<TVertex, Rect>(); list.Values.ToList().ForEach(a => a.Value.Inflate(vertex_margin_distance * 2, vertex_margin_distance * 2)); //add margins return(list); }
private IDictionary <TVertex, KeyValuePair <TVertex, Rect> > getSizesCollection(TEdge ctrl, Point end_point) { var list = VertexSizes.Where(a => a.Key.ID != ctrl.Source.ID && a.Key.ID != ctrl.Target.ID).OrderByDescending(a => GetDistance(VertexPositions[a.Key], end_point)).ToDictionary(a => a.Key); // new Dictionary<TVertex, Rect>(); foreach (var item in list.Values) { item.Value.Inflate(vertex_margin_distance * 2, vertex_margin_distance * 2); } return(list); }
public override void Compute(CancellationToken cancellationToken) { VertexPositions.Clear(); var bounds = _parameters == null ? new RandomLayoutAlgorithmParams().Bounds : _parameters.Bounds; var boundsWidth = (int)bounds.Width; var boundsHeight = (int)bounds.Height; var seed = _parameters == null?Guid.NewGuid().GetHashCode() : _parameters.Seed; var rnd = new Random(seed); foreach (var item in VisitedGraph.Vertices) { if (item.SkipProcessing != ProcessingOptionEnum.Freeze || VertexPositions.Count == 0) { var x = (int)bounds.X; var y = (int)bounds.Y; var size = VertexSizes.FirstOrDefault(a => a.Key == item).Value; VertexPositions.Add(item, new Point(rnd.Next(x, x + boundsWidth - (int)size.Width), rnd.Next(y, y + boundsHeight - (int)size.Height))); } } }
private void EdgeRoutingTest(TEdge ctrl, CancellationToken cancellationToken) { //bad edge data check if (ctrl.Source.ID == -1 || ctrl.Target.ID == -1) { throw new GX_InvalidDataException("SimpleEdgeRouting() -> You must assign unique ID for each vertex to use SimpleER algo!"); } if (ctrl.Source.ID == ctrl.Target.ID || !VertexSizes.ContainsKey(ctrl.Source) || !VertexSizes.ContainsKey(ctrl.Target)) { return; } var ss = VertexSizes[ctrl.Source]; var es = VertexSizes[ctrl.Target]; var startPoint = new Point(ss.X + ss.Width * 0.5, ss.Y + ss.Height * 0.5); var endPoint = new Point(es.X + es.Width * 0.5, es.Y + es.Height * 0.5); if (startPoint == endPoint) { return; } var originalSizes = getSizesCollection(ctrl, endPoint); var checklist = new Dictionary <TVertex, KeyValuePair <TVertex, Rect> >(originalSizes); var leftSizes = new Dictionary <TVertex, KeyValuePair <TVertex, Rect> >(originalSizes); var tempList = new List <Point>(); tempList.Add(startPoint); bool haveIntersections = true; //while we have some intersections - proceed while (haveIntersections) { var curDrawback = drawback_distance; while (true) { cancellationToken.ThrowIfCancellationRequested(); var item = checklist.Keys.FirstOrDefault(); //set last route point as current start point startPoint = tempList.Last(); if (item == null) { //checked all vertices and no intersection was found - quit haveIntersections = false; break; } else { var r = originalSizes[item].Value; Point checkpoint; //check for intersection point. if none found - remove vertex from checklist if (GetIntersectionPoint(r, startPoint, endPoint, out checkpoint) == -1) { checklist.Remove(item); continue; } var mainVector = new Vector(endPoint.X - startPoint.X, endPoint.Y - startPoint.Y); double X = 0; double Y = 0; //calculate drawback X coordinate if (startPoint.X == checkpoint.X || Math.Abs(startPoint.X - checkpoint.X) < curDrawback) { X = startPoint.X; } else if (startPoint.X < checkpoint.X) { X = checkpoint.X - curDrawback; } else { X = checkpoint.X + curDrawback; } //calculate drawback Y coordinate if (startPoint.Y == checkpoint.Y || Math.Abs(startPoint.Y - checkpoint.Y) < curDrawback) { Y = startPoint.Y; } else if (startPoint.Y < checkpoint.Y) { Y = checkpoint.Y - curDrawback; } else { Y = checkpoint.Y + curDrawback; } //set drawback checkpoint checkpoint = new Point(X, Y); bool isStartPoint = checkpoint == startPoint; bool routeFound = false; bool viceversa = false; int counter = 1; var joint = new Point(); bool?blocked_direction = null; while (!routeFound) { cancellationToken.ThrowIfCancellationRequested(); //choose opposite vector side each cycle var signedDistance = viceversa ? side_distance : -side_distance; //get new point coordinate joint = new Point( checkpoint.X + signedDistance * counter * (mainVector.Y / mainVector.Length), checkpoint.Y - signedDistance * counter * (mainVector.X / mainVector.Length)); //now check if new point is in some other vertex var iresult = false; var forcedBreak = false; if (originalSizes.Any(sz => sz.Value.Value.Contains(joint))) { iresult = true; //block this side direction if (blocked_direction == null) { blocked_direction = viceversa; } else { //both sides blocked - need to drawback forcedBreak = true; } } if (forcedBreak) { break; } //get vector intersection if its ok if (!iresult) { iresult = IsIntersected(r, joint, endPoint); } //if no vector intersection - we've found it! if (!iresult) { routeFound = true; blocked_direction = null; } else { //still have an intersection with current vertex haveIntersections = true; //skip point search if too many attempts was made (bad logic hack) if (counter > 300) { break; } counter++; //switch vector search side if (blocked_direction == null || (blocked_direction == viceversa)) { viceversa = !viceversa; } } } //if blocked and this is not start point (nowhere to drawback) - then increase drawback distance if (blocked_direction != null && !isStartPoint) { //search has been blocked - need to drawback curDrawback += drawback_distance; } else { //add new route point if we found it // if(routeFound) tempList.Add(joint); leftSizes.Remove(item); } } //remove currently evaded obstacle vertex from the checklist checklist.Remove(item); } //assign possible left vertices as a new checklist if any intersections was found if (haveIntersections) { checklist = new Dictionary <TVertex, KeyValuePair <TVertex, Rect> >(leftSizes); } } //finally, add an end route point tempList.Add(endPoint); if (EdgeRoutes.ContainsKey(ctrl)) { EdgeRoutes[ctrl] = tempList.Count > 2 ? tempList.ToArray() : null; } else { EdgeRoutes.Add(ctrl, tempList.Count > 2 ? tempList.ToArray() : null); } }
public override void UpdateVertexData(TVertex vertex, Point position, Rect size) { VertexPositions.AddOrUpdate(vertex, position); VertexSizes.AddOrUpdate(vertex, size); }
public override void Compute(CancellationToken cancellationToken) { var groups = _params.GroupParametersList.Select(a => a.GroupId).OrderByDescending(a => a).ToList(); var listRect = new Dictionary <object, Rect>(); foreach (var group in groups) { cancellationToken.ThrowIfCancellationRequested(); var groupId = group; var gp = _params.GroupParametersList.First(a => a.GroupId == groupId); //get vertices of the same group //var vertices = new List<TVertex>(); var vertices = VisitedGraph.Vertices.Where(a => a.GroupId == groupId).ToList(); //skip processing if there are no vertices in this group if (vertices.Count == 0) { continue; } //get edges between vertices in the same group var edges = VisitedGraph.Edges.Where(a => a.Source.GroupId == a.Target.GroupId && a.Target.GroupId == groupId).ToList(); //create and compute graph for a group var graph = GenerateGroupGraph(vertices, edges); //inject custom vertex and edge set into existing algorithm gp.LayoutAlgorithm.ResetGraph(graph.Vertices, graph.Edges); //assign vertex sizes to internal algorithm if needed if (gp.LayoutAlgorithm.NeedVertexSizes) { gp.LayoutAlgorithm.VertexSizes = VertexSizes.Where(a => a.Key.GroupId == groupId) .ToDictionary(a => a.Key, b => b.Value); } gp.LayoutAlgorithm.Compute(cancellationToken); //Move vertices to bound box if layout algorithm don't use bounds if (gp.ZoneRectangle.HasValue && !gp.IsAlgorithmBounded) { var offsetX = gp.ZoneRectangle.Value.X; var offsetY = gp.ZoneRectangle.Value.Y; gp.LayoutAlgorithm.VertexPositions.ForEach(a => { a.Value.Offset(offsetX, offsetY); }); } //write results to global positions storage double?[] left = { null }; double?[] top = { null }; double?[] right = { null }; double?[] bottom = { null }; gp.LayoutAlgorithm.VertexPositions.ForEach(a => { left[0] = left[0].HasValue ? (a.Value.X < left[0] ? a.Value.X : left[0]) : a.Value.X; var w = a.Value.X + VertexSizes[a.Key].Width; var h = a.Value.Y + VertexSizes[a.Key].Height; right[0] = right[0].HasValue ? (w > right[0] ? w : right[0]) : w; top[0] = top[0].HasValue ? (a.Value.Y < top[0] ? a.Value.Y : top[0]) : a.Value.Y; bottom[0] = bottom[0].HasValue ? (h > bottom[0] ? h : bottom[0]) : h; if (VertexPositions.ContainsKey(a.Key)) { VertexPositions[a.Key] = a.Value; } else { VertexPositions.Add(a.Key, a.Value); } }); if (_params.ArrangeGroups) { if (left[0] == null) { left[0] = 0; } if (right[0] == null) { right[0] = 0; } if (top[0] == null) { top[0] = 0; } if (bottom[0] == null) { bottom[0] = 0; } listRect.Add(gp.GroupId, gp.ZoneRectangle ?? new Rect(new Point(left[0].Value, top[0].Value), new Point(right[0].Value, bottom[0].Value))); } } if (_params.ArrangeGroups) { cancellationToken.ThrowIfCancellationRequested(); var origList = listRect.ToDictionary(a => a.Key, a => a.Value); var ora = _params != null && _params.OverlapRemovalAlgorithm != null ? _params.OverlapRemovalAlgorithm : new FSAAlgorithm <object>(listRect, new OverlapRemovalParameters { HorizontalGap = 10, VerticalGap = 10 }); ora.Initialize(listRect); ora.Compute(cancellationToken); cancellationToken.ThrowIfCancellationRequested(); ora.Rectangles.ForEach(a => { int group = (int)a.Key; //_params.GroupParametersList.FirstOrDefault(b => b.GroupId == (int)a.Key).ZoneRectangle = origList[a.Key]; ArrangeRectangle(a.Value, group, origList[a.Key]); }); } }
private bool IsOverlapped(Point pt) { var trect = new Rect(pt.X, pt.Y, 2, 2); return(VertexSizes.Select(item => new Rect(item.Value.X - _vertexSafeDistance, item.Value.Y - _vertexSafeDistance, item.Value.Width + _vertexSafeDistance, item.Value.Height + _vertexSafeDistance)).Any(rect => rect.IntersectsWith(trect))); }