/// <summary> /// Initialise an RoadNetwork with map, edges and vertices identical to another RoadNetwork /// </summary> /// <param name="network">The network to copy from</param> /// <param name="keepEdges">Copy the edges from the network</param> /// <param name="keepVertices">Copy the vertices from the network</param> public RoadNetwork(RoadNetwork network, bool keepEdges = true, bool keepVertices = true) { map = network.map; if (keepVertices) { CopyVertices(network.vertices); } if (keepEdges) { CopyEdges(network.edges); } }
/// <summary> /// Load map file, if the file was successfully loaded by the OpenFileDialog Object. /// Check if it is an XML file. If not, ask user to select another file. /// If a file is not selected, then the user is notified to select a map file. /// </summary> private void loadMapFile() { OpenMap.Title = "Select Map File"; OpenMap.Filter = "XML Files (*.XML)|*.xml"; OpenMap.FilterIndex = 0; if (!String.IsNullOrWhiteSpace(tbMapFile.Text)) { OpenMap.InitialDirectory = Path.GetDirectoryName(tbMapFile.Text); } if (OpenMap.ShowDialog() == DialogResult.OK) { tbMapFile.Text = OpenMap.FileName; string e = Path.GetExtension(tbMapFile.Text); if (String.Equals(e, ".xml")) { map = Map.FromFile(tbMapFile.Text); visualiser1.Network = new RoadNetwork(map); } else { MessageBox.Show("Map file should be in xml form. Please select another file.\n"); } } else if (OpenMap.ShowDialog() != DialogResult.OK && String.IsNullOrEmpty(tbMapFile.Text)) { MessageBox.Show("Select a file so a map can be created on the visualiser\n"); } }
/// <summary> /// Initialise a new StepPopulator /// </summary> /// <param name="config">A string with the path to the map file</param> public StepPopulator(object config) { map = Map.FromFile((string)config); }
/// <summary> /// Deserealise the vertices and edges from an <see cref="XmlReader"/> /// </summary> /// <param name="reader"> /// <see cref="XmlReader"/> containing data /// </param> public RoadNetwork(XmlReader reader) { Dictionary<string, Vertex> verticesById = new Dictionary<string, Vertex>(); int depth = 1; while (depth > 0 && reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: switch (reader.Name) { case "map": map = new Map(reader); break; case "vertex": string id = reader.GetAttribute("id"); verticesById.Add(id, AddVertex(new Coordinates(reader))); break; case "edge": string startId = reader.GetAttribute("start"); string endId = reader.GetAttribute("end"); Vertex start = verticesById[startId]; Vertex end = verticesById[endId]; AddEdge(start, end); break; } if (!reader.IsEmptyElement) { depth++; } break; case XmlNodeType.EndElement: depth--; break; } } }
/// <summary> /// Initialise an empty RoadNetwork /// </summary> /// <param name="map">The map this Roadnetwork exists within.</param> public RoadNetwork(Map map) { this.map = map; }
static RoadNetwork RandomNetwork(Map map) { Random random = new Random(); RoadNetwork network = new RoadNetwork(map); network.AddVertex(map.Start); network.AddVertex(map.End); Vertex start = network.Start; Vertex end = network.End; for (int i = 0; i < 2; i++) { Vertex startPoint = start; for (int j = 0; j < 10; j++) { Vertex endPoint = network.AddVertex(random.Next(map.Width), random.Next(map.Height)); network.AddEdge(startPoint, endPoint); startPoint = endPoint; } network.AddEdge(startPoint, end); } for (int k = 0; k < 80; k++) { Vertex startPoint = network.GetVertex(random.Next(network.VertexCount)); Vertex endPoint = network.AddVertex(random.Next(map.Width), random.Next(map.Height)); network.AddEdge(startPoint, endPoint); } for (int l = 0; l < 20; l++) { Vertex startPoint = network.GetVertex(random.Next(network.VertexCount)); Vertex endPoint = network.GetVertex(random.Next(network.VertexCount)); network.AddEdge(startPoint, endPoint); } network.SetEnd(1); return network; }
/// <summary> /// Load a Map from a file. /// </summary> /// <param name="filename">The file to load from.</param> /// <returns>The Map loaded.</returns> public static Map FromFile(string filename) { XmlTextReader reader = new XmlTextReader(filename); reader.MoveToContent(); if (reader.Name != "map") { throw new Exception("Map XML file must have <map> element as root."); } Map map = new Map(reader); reader.Close(); return map; }