/// <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 <Vertex> 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 <Vertex> singleVertices = new List <Vertex>(); foreach (Vertex v in vertices) { if (newVisGraph.Contains(v)) { continue; } Edge closestEdge = newVisGraph.baseGraph.edges.OrderBy(e => e.DistanceTo(v)).First(); if (!closestEdge.DistanceTo(v).AlmostEqualTo(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 (Vertex centre in singleVertices) { foreach (Vertex v in VisibleVertices(centre, newVisGraph.baseGraph, null, null, singleVertices, false, reducedGraph)) { newVisGraph.AddEdge(new Edge(centre, v)); } } return(newVisGraph); }
public static VisibilityGraph Merge(List <VisibilityGraph> graphs) { Graph graph = new Graph(); List <Edge> edges = new List <Edge>(); foreach (VisibilityGraph g in graphs) { Dictionary <int, int> oldNewIds = new Dictionary <int, int>(); foreach (Polygon p in g.baseGraph.polygons.Values) { int nextId = graph.GetNextId(); oldNewIds.Add(p.id, nextId); Polygon polygon = (Polygon)p.Clone(); polygon.id = nextId; graph.polygons.Add(nextId, polygon); } foreach (Edge e in g.edges) { Vertex start = (Vertex)e.StartVertex.Clone(); Vertex end = (Vertex)e.EndVertex.Clone(); //start.polygonId = oldNewIds[start.polygonId]; //end.polygonId = oldNewIds[end.polygonId]; edges.Add(Edge.ByStartVertexEndVertex(start, end)); } } VisibilityGraph visibilityGraph = new VisibilityGraph() { baseGraph = new Graph(graph.polygons.Values.ToList()), }; foreach (Edge 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.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(new Edge(origin, v)); } } if (!containsDestination) { foreach (Vertex v in VisibleVertices(destination, visibilityGraph.baseGraph, gO, null, null, false, true)) { tempGraph.AddEdge(new Edge(destination, v)); } } shortest = Algorithms.Algorithms.Dijkstra(visibilityGraph, origin, destination, tempGraph); } return(shortest); }