/// <summary> /// Adds specific lines as Edges to the visibility graph /// </summary> /// <param name="visibilityGraph">VisibilityGraph Graph</param> /// <param name="edges">Lines to add as new Edges</param> /// <returns></returns> public static VisibilityGraph AddEdges(VisibilityGraph visibilityGraph, List <Edge> edges) { if (edges == null) { throw new NullReferenceException("Edges"); } List <Vertex> singleVertices = new List <Vertex>(); foreach (Edge e in edges) { if (!singleVertices.Contains(e.StartVertex)) { singleVertices.Add(e.StartVertex); } if (!singleVertices.Contains(e.EndVertex)) { singleVertices.Add(e.EndVertex); } } VisibilityGraph updatedGraph = (VisibilityGraph)visibilityGraph.Clone(); if (singleVertices.Any()) { updatedGraph = AddVertices(visibilityGraph, singleVertices); } foreach (Edge e in edges) { updatedGraph.AddEdge(e); } return(updatedGraph); }
/// <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); }