public static Dictionary <string, object> ShortestPath(VisibilityGraph visGraph, DSPoint origin, DSPoint destination) { if (visGraph == null) { throw new ArgumentNullException("visGraph"); } if (origin == null) { throw new ArgumentNullException("origin"); } if (destination == null) { throw new ArgumentNullException("destination"); } Vertex gOrigin = Vertex.ByCoordinates(origin.X, origin.Y, origin.Z); Vertex gDestination = Vertex.ByCoordinates(destination.X, destination.Y, destination.Z); Graphical.Graphs.VisibilityGraph visibilityGraph = visGraph.graph as Graphical.Graphs.VisibilityGraph; BaseGraph baseGraph = new BaseGraph() { graph = Graphical.Graphs.VisibilityGraph.ShortestPath(visibilityGraph, gOrigin, gDestination) }; return(new Dictionary <string, object>() { { "graph", baseGraph }, { "length", baseGraph.graph.Edges.Select(e => e.Length).Sum() } }); }
/// <summary> /// Computes the Visibility Graph from a base graph using Lee's algorithm. /// </summary> /// <param name="baseGraph">Base graph</param> /// <param name="reduced">Reduced graph returns edges where its vertices belong to different /// polygons and at least one is not convex/concave to its polygon.</param> /// <returns name="visGraph">Visibility graph</returns> public static VisibilityGraph ByBaseGraph(BaseGraph baseGraph, bool reduced = true) { if (baseGraph == null) { throw new ArgumentNullException("graph"); } var visGraph = new Graphical.Graphs.VisibilityGraph(baseGraph.graph, reduced, true); var visibilityGraph = new VisibilityGraph() { graph = visGraph }; return(visibilityGraph); }
public static VisibilityGraph ConnectGraphs(List <VisibilityGraph> visibilityGraphs, List <Line> lines) { if (visibilityGraphs == null) { throw new ArgumentNullException("visibilityGraphs"); } List <Graphical.Graphs.VisibilityGraph> visGraphs = visibilityGraphs.Select(vg => (Graphical.Graphs.VisibilityGraph)vg.graph).ToList(); Graphical.Graphs.VisibilityGraph mergedGraph = Graphical.Graphs.VisibilityGraph.Merge(visGraphs); var edges = lines.Select(l => l.ToEdge()).ToList(); return(new VisibilityGraph() { graph = Graphical.Graphs.VisibilityGraph.AddEdges(mergedGraph, edges) }); }
/// <summary> /// Adds specific points as gVertices to the VisibilityGraph Graph /// </summary> /// <param name="visibilityGraph">VisibilityGraph Graph</param> /// <param name="vertices">Points to add as gVertices</param> /// <returns></returns> public static VisibilityGraph AddVertices(VisibilityGraph visibilityGraph, List <gVertex> vertices, bool reducedGraph = true) { //TODO: Seems that original graph gets updated as well if (vertices == null) { throw new NullReferenceException("vertices"); } VisibilityGraph newVisGraph = (VisibilityGraph)visibilityGraph.Clone(); List <gVertex> singleVertices = new List <gVertex>(); foreach (gVertex v in vertices) { if (newVisGraph.Contains(v)) { continue; } gEdge closestEdge = newVisGraph.baseGraph.edges.OrderBy(e => e.DistanceTo(v)).First(); if (!gBase.Threshold(closestEdge.DistanceTo(v), 0)) { singleVertices.Add(v); } else if (v.OnEdge(closestEdge.StartVertex, closestEdge.EndVertex)) { v.polygonId = closestEdge.StartVertex.polygonId; newVisGraph.baseGraph.polygons[v.polygonId] = newVisGraph.baseGraph.polygons[v.polygonId].AddVertex(v, closestEdge); singleVertices.Add(v); } } newVisGraph.baseGraph.ResetEdgesFromPolygons(); foreach (gVertex centre in singleVertices) { foreach (gVertex v in VisibleVertices(centre, newVisGraph.baseGraph, null, null, singleVertices, false, reducedGraph)) { newVisGraph.AddEdge(new gEdge(centre, v)); } } return(newVisGraph); }
/// <summary> /// Adds specific points as Vertices to the VisibilityGraph Graph /// </summary> /// <param name="visibilityGraph">VisibilityGraph Graph</param> /// <param name="vertices">Points to add as gVertices</param> /// <returns></returns> public static VisibilityGraph AddVertices(VisibilityGraph visibilityGraph, List <Vertex> vertices, bool reducedGraph = true) { if (vertices == null) { throw new NullReferenceException("Vertices"); } VisibilityGraph newVisGraph = (VisibilityGraph)visibilityGraph.Clone(); List <Vertex> singleVertices = new List <Vertex>(); foreach (Vertex vertex in vertices) { if (newVisGraph.Contains(vertex)) { continue; } Edge closestEdge = newVisGraph.baseGraph.Edges.OrderBy(e => e.DistanceTo(vertex)).First(); if (!closestEdge.DistanceTo(vertex).AlmostEqualTo(0)) { singleVertices.Add(vertex); } else if (vertex.OnEdge(closestEdge.StartVertex, closestEdge.EndVertex)) { int polygonId = closestEdge.StartVertex.Parent.Id; newVisGraph.baseGraph._polygonsDict[polygonId] = newVisGraph.baseGraph._polygonsDict[polygonId].AddVertex(vertex, closestEdge); singleVertices.Add(vertex); } } newVisGraph.baseGraph.ResetEdgesFromPolygons(); foreach (Vertex centre in singleVertices) { foreach (Vertex v in VisibleVertices(centre, newVisGraph.baseGraph, null, null, singleVertices, false, reducedGraph)) { newVisGraph.AddEdge(Edge.ByStartVertexEndVertex(centre, v)); } } return(newVisGraph); }
public static VisibilityGraph Merge(List <VisibilityGraph> vgraphs) { Graph graph = new Graph(); graph._polygonsDict = vgraphs.SelectMany(vg => vg.Polygons).ToDictionary(poly => poly.Id, poly => poly); VisibilityGraph visibilityGraph = new VisibilityGraph() { baseGraph = new Graph(graph.Polygons.ToList()), }; var edges = vgraphs.SelectMany(vg => vg.Edges); for (int i = 0; i < edges.Count(); i++) { visibilityGraph.AddEdge(edges.ElementAt(i)); } return(visibilityGraph); }
public static VisibilityGraph Merge(List <VisibilityGraph> graphs) { Graph graph = new Graph(); List <gEdge> edges = new List <gEdge>(); foreach (VisibilityGraph g in graphs) { Dictionary <int, int> oldNewIds = new Dictionary <int, int>(); foreach (gPolygon p in g.baseGraph.polygons.Values) { int nextId = graph.GetNextId(); oldNewIds.Add(p.id, nextId); gPolygon polygon = (gPolygon)p.Clone(); polygon.id = nextId; graph.polygons.Add(nextId, polygon); } foreach (gEdge e in g.edges) { gVertex start = (gVertex)e.StartVertex.Clone(); gVertex end = (gVertex)e.EndVertex.Clone(); //start.polygonId = oldNewIds[start.polygonId]; //end.polygonId = oldNewIds[end.polygonId]; edges.Add(gEdge.ByStartVertexEndVertex(start, end)); } } VisibilityGraph visibilityGraph = new VisibilityGraph() { baseGraph = new Graph(graph.polygons.Values.ToList()), }; foreach (gEdge edge in edges) { visibilityGraph.AddEdge(edge); } return(visibilityGraph); }
public static Graph ShortestPath(VisibilityGraph visibilityGraph, Vertex origin, Vertex destination) { Graph shortest; bool containsOrigin = visibilityGraph.Contains(origin); bool containsDestination = visibilityGraph.Contains(destination); if (containsOrigin && containsDestination) { shortest = Algorithms.Dijkstra(visibilityGraph, origin, destination); } else { Vertex gO = (!containsOrigin) ? origin : null; Vertex gD = (!containsDestination) ? destination : null; Graph tempGraph = new Graph(); if (!containsOrigin) { foreach (Vertex v in VisibleVertices(origin, visibilityGraph.baseGraph, null, gD, null, false, true)) { tempGraph.AddEdge(Edge.ByStartVertexEndVertex(origin, v)); } } if (!containsDestination) { foreach (Vertex v in VisibleVertices(destination, visibilityGraph.baseGraph, gO, null, null, false, true)) { tempGraph.AddEdge(Edge.ByStartVertexEndVertex(destination, v)); } } shortest = Algorithms.Dijkstra(visibilityGraph, origin, destination, tempGraph); } return(shortest); }
public static Dictionary <string, object> Connectivity( VisibilityGraph visGraph, [DefaultArgument("null")] List <DSCore.Color> colours, [DefaultArgument("null")] List <double> indices) { if (visGraph == null) { throw new ArgumentNullException("visGraph"); } Graphical.Graphs.VisibilityGraph visibilityGraph = visGraph.graph as Graphical.Graphs.VisibilityGraph; VisibilityGraph graph = new VisibilityGraph() { graph = visibilityGraph, Factors = visibilityGraph.ConnectivityFactor() }; if (colours != null && indices != null && colours.Count == indices.Count) { graph.colorRange = new Dictionary <double, DSCore.Color>(); // Create KeyValuePairs and sort them by index in case unordered. var pairs = indices.Zip(colours, (i, c) => new KeyValuePair <double, DSCore.Color>(i, c)).OrderBy(kv => kv.Key); // Adding values to colorRange dictionary foreach (KeyValuePair <double, DSCore.Color> kv in pairs) { graph.colorRange.Add(kv.Key, kv.Value); } } return(new Dictionary <string, object>() { { "visGraph", graph }, { "factors", graph.Factors } }); }