private void _startTrafficFlow_Click(object sender, EventArgs e) { _isTrafficFlowConfigure = true; _currentTrafficFlow = new TrafficFlow(); var roadLine = new RoadLine { Color = GetNewPen() }; _trafficFlowToRoadLineDictionary.Add(_currentTrafficFlow, roadLine); }
private void _tsmiOpenConfig_Click(object sender, EventArgs e) { var dialog = new OpenFileDialog { Filter = "XML file (*.xml)|*.xml", RestoreDirectory = true, DereferenceLinks = false, AutoUpgradeEnabled = false }; if (dialog.ShowDialog() != DialogResult.OK) { return; } _map.ClearElements(); _workField.Controls.Clear(); _trafficManager.TrafficFlows.Clear(); var xmlDocument = new XmlDocument(); xmlDocument.Load(dialog.FileName); var configList = xmlDocument.SelectNodes("Config"); if (configList.Count != 1) { throw new Exception("Invalid config file structure"); } var mapList = configList[0].SelectNodes("Map"); if (mapList.Count != 1) { throw new Exception("Invalid config file structure"); } var elementList = mapList[0].SelectNodes("MapElement"); for (var elementIter = 0; elementIter < elementList.Count; elementIter++) { var mapElementAttributes = elementList[elementIter].Attributes; var row = int.Parse(mapElementAttributes["Row"].Value); var column = int.Parse(mapElementAttributes["Column"].Value); var type = mapElementAttributes["Type"].Value; if (type == "Crossroad") { AddCrossroad(row, column); } else if (type == "Road") { var orientation = mapElementAttributes["Orientation"].Value; if (orientation == "Horizontal") { AddHorizontalRoad(row, column); } else if (orientation == "Vertical") { AddVerticalRoad(row, column); } else { throw new ArgumentException("Invalid road orientation."); } } else if (type == "Turn") { var orientation = mapElementAttributes["Orientation"].Value; if (orientation == "DownToLeft") { AddDownToLeftTurn(row, column); } else if (orientation == "LeftToUp") { AddLeftToUpTurn(row, column); } else if (orientation == "RightToDown") { AddRightToDownTurn(row, column); } else if (orientation == "UpToRight") { AddUpToRightTurn(row, column); } else { throw new ArgumentException("Invalid turn orientation."); } } else { throw new ArgumentException("Unknown element type."); } } var trafficFlowsList = configList[0].SelectNodes("TrafficFlows"); if (trafficFlowsList.Count != 1) { throw new Exception("Invalid config file structure"); } var trafficFlowList = trafficFlowsList[0].SelectNodes("TrafficFlow"); for (var trafficFlowIter = 0; trafficFlowIter < trafficFlowList.Count; trafficFlowIter++) { _currentTrafficFlow = new TrafficFlow(); var roadLine = new RoadLine { Color = GetNewPen() }; _trafficFlowToRoadLineDictionary.Add(_currentTrafficFlow, roadLine); var trafficFlowAttributes = trafficFlowList[trafficFlowIter].Attributes; var density = double.Parse(trafficFlowAttributes["Density"].Value, CultureInfo.InvariantCulture); var speed = double.Parse(trafficFlowAttributes["Speed"].Value, CultureInfo.InvariantCulture); var pathList = trafficFlowList[trafficFlowIter].SelectNodes("Path"); if (pathList.Count != 1) { throw new Exception("Invalid config file structure"); } var locationList = pathList[0].SelectNodes("Location"); for (var locationIter = 0; locationIter < locationList.Count; locationIter++) { var locationAttributes = locationList[locationIter].Attributes; var locationRow = int.Parse(locationAttributes["Row"].Value, CultureInfo.InvariantCulture); var locationColumn = int.Parse(locationAttributes["Column"].Value, CultureInfo.InvariantCulture); _currentTrafficFlow.Path.Add(new Location(locationRow, locationColumn)); } foreach (var location in _currentTrafficFlow.Path) { var control = LocationToControl(location) as ACrossroadControl; if (control == null) { throw new Exception("Something went wrong..."); } control.DeleteLine(roadLine); control.AddLine(roadLine); control.Invalidate(); } _currentTrafficFlow.TrafficSpeed = speed; _currentTrafficFlow.TrafficDensity = density; _trafficManager.AddTrafficFlow(_currentTrafficFlow); } }
private void AddCrossroadTrafficData(ILocation prev, ILocation crossroadLocation, ILocation next, ICrossroad crossroad, ITrafficFlow trafficFlow) { var fromLeft = false; var fromUp = false; var fromRight = false; var fromDown = false; if (prev.Column < crossroadLocation.Column) { fromLeft = true; } else if (prev.Row < crossroadLocation.Row) { fromUp = true; } else if (prev.Column > crossroadLocation.Column) { fromRight = true; } else if (prev.Row > crossroadLocation.Row) { fromDown = true; } var toLeft = false; var toUp = false; var toRight = false; var toDown = false; if (next.Column < crossroadLocation.Column) { toLeft = true; } else if (next.Row < crossroadLocation.Row) { toUp = true; } else if (next.Column > crossroadLocation.Column) { toRight = true; } else if (next.Row > crossroadLocation.Row) { toDown = true; } ITrafficData trafficData = null; if (fromLeft && toUp) { trafficData = crossroad.LeftToUpTrafficData; } else if (fromLeft && toRight) { trafficData = crossroad.LeftToRightTrafficData; } else if (fromLeft && toDown) { trafficData = crossroad.LeftToDownTrafficData; } else if (fromUp && toLeft) { trafficData = crossroad.UpToLeftTrafficData; } else if (fromUp && toDown) { trafficData = crossroad.UpToDownTrafficData; } else if (fromUp && toRight) { trafficData = crossroad.UpToRightTrafficData; } else if (fromRight && toUp) { trafficData = crossroad.RightToUpTrafficData; } else if (fromRight && toLeft) { trafficData = crossroad.RightToLeftTrafficData; } else if (fromRight && toDown) { trafficData = crossroad.RightToDownTrafficData; } else if (fromDown && toLeft) { trafficData = crossroad.DownToLeftTrafficData; } else if (fromDown && toUp) { trafficData = crossroad.DownToUpTrafficData; } else if (fromDown && toRight) { trafficData = crossroad.DownToRightTrafficData; } trafficData.TrafficDensity += trafficFlow.TrafficDensity; /*if (trafficData.TrafficDensity > 1.0) * { * trafficData.TrafficDensity = 1.0; * }*/ trafficData.TrafficSpeed *= trafficData.NofPassingTrafficFlows; trafficData.TrafficSpeed += trafficFlow.TrafficSpeed; trafficData.NofPassingTrafficFlows++; trafficData.TrafficSpeed /= trafficData.NofPassingTrafficFlows; }
public void AddTrafficFlow(ITrafficFlow trafficFlow) { _trafficFlows.Add(trafficFlow); }