using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using HeuristicLab.Core.Views; using HeuristicLab.MainForm; using HeuristicLab.PDPSimulation.DomainModel; using HeuristicLab.MainForm.WindowsForms; namespace HeuristicLab.PDPSimulation.Views { [View("PickupDeliveryVisualization View")] [Content(typeof(PickupDeliveryVisualization), true)] public partial class PickupDeliveryVisualizationView : ItemView { public new PickupDeliveryVisualization Content { get { return (PickupDeliveryVisualization)base.Content; } set { base.Content = value; } } public PickupDeliveryVisualizationView() { InitializeComponent(); } protected override void DeregisterContentEvents() { Content.ContentChanged -= new EventHandler(Content_ContentChanged); base.DeregisterContentEvents(); } protected override void RegisterContentEvents() { base.RegisterContentEvents(); Content.ContentChanged += new EventHandler(Content_ContentChanged); } void Content_ContentChanged(object sender, EventArgs e) { GenerateImage(); } private bool drawRoutes = false; void pictureBox_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Right) { drawRoutes = !drawRoutes; GenerateImage(); } } private void GenerateImage() { if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) { PickupDeliveryVisualization content = Content; if (content == null) { pictureBox.Image = null; } else { int customerSize = Resources.source.Width; int depotSize = Resources.depot.Width; int truckSize = Resources.truck.Width; int border = customerSize * 2; Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height); double width = bitmap.Width - border * 2; double height = bitmap.Height - border * 2; double rangeX = content.OrderRangeX; double rangeY = content.OrderRangeY; using (Graphics graphics = Graphics.FromImage(bitmap)) { if (drawRoutes) { float completedWidth = 1; Pen[] pastPens = {new Pen(Color.FromArgb(92,20,237), completedWidth), new Pen(Color.FromArgb(237,183,20), completedWidth), new Pen(Color.FromArgb(237,20,219), completedWidth), new Pen(Color.FromArgb(20,237,76), completedWidth), new Pen(Color.FromArgb(237,61,20), completedWidth), new Pen(Color.FromArgb(115,78,26), completedWidth), new Pen(Color.FromArgb(20,237,229), completedWidth), new Pen(Color.FromArgb(39,101,19), completedWidth), new Pen(Color.FromArgb(230,170,229), completedWidth), new Pen(Color.FromArgb(142,136,89), completedWidth), new Pen(Color.FromArgb(157,217,166), completedWidth), new Pen(Color.FromArgb(31,19,101), completedWidth), new Pen(Color.FromArgb(173,237,20), completedWidth), new Pen(Color.FromArgb(230,231,161), completedWidth), new Pen(Color.FromArgb(142,89,89), completedWidth), new Pen(Color.FromArgb(93,89,142), completedWidth), new Pen(Color.FromArgb(146,203,217), completedWidth), new Pen(Color.FromArgb(101,19,75), completedWidth), new Pen(Color.FromArgb(198,20,237), completedWidth), new Pen(Color.FromArgb(185,185,185), completedWidth), new Pen(Color.FromArgb(179,32,32), completedWidth), new Pen(Color.FromArgb(18,119,115), completedWidth), new Pen(Color.FromArgb(104,158,239), completedWidth), new Pen(Color.Black, completedWidth)}; float toBeServedWidth = 2; Pen[] futurePens = {new Pen(Color.FromArgb(92,20,237), toBeServedWidth), new Pen(Color.FromArgb(237,183,20), toBeServedWidth), new Pen(Color.FromArgb(237,20,219), toBeServedWidth), new Pen(Color.FromArgb(20,237,76), toBeServedWidth), new Pen(Color.FromArgb(237,61,20), toBeServedWidth), new Pen(Color.FromArgb(115,78,26), toBeServedWidth), new Pen(Color.FromArgb(20,237,229), toBeServedWidth), new Pen(Color.FromArgb(39,101,19), toBeServedWidth), new Pen(Color.FromArgb(230,170,229), toBeServedWidth), new Pen(Color.FromArgb(142,136,89), toBeServedWidth), new Pen(Color.FromArgb(157,217,166), toBeServedWidth), new Pen(Color.FromArgb(31,19,101), toBeServedWidth), new Pen(Color.FromArgb(173,237,20), toBeServedWidth), new Pen(Color.FromArgb(230,231,161), toBeServedWidth), new Pen(Color.FromArgb(142,89,89), toBeServedWidth), new Pen(Color.FromArgb(93,89,142), toBeServedWidth), new Pen(Color.FromArgb(146,203,217), toBeServedWidth), new Pen(Color.FromArgb(101,19,75), toBeServedWidth), new Pen(Color.FromArgb(198,20,237), toBeServedWidth), new Pen(Color.FromArgb(185,185,185), toBeServedWidth), new Pen(Color.FromArgb(179,32,32), toBeServedWidth), new Pen(Color.FromArgb(18,119,115), toBeServedWidth), new Pen(Color.FromArgb(104,158,239), toBeServedWidth), new Pen(Color.Black, toBeServedWidth)}; int currentTour = 0; foreach (Vehicle vehicle in content.Vehicles) { PDAction action = content.GetAction(vehicle); List tourPoints = new List(); while (action != null && action.ActionCompleted()) { if (action is MoveVehicleAction) { MoveVehicleAction moveAction = action as MoveVehicleAction; if (tourPoints.Count == 0) { tourPoints.Add(new Point((int)(border + moveAction.SourceX / rangeX * width), (int)(height + border - moveAction.SourceY / rangeY * height))); } Point locationPoint = new Point((int)(border + moveAction.TargetX / rangeX * width), (int)(height + border - moveAction.TargetY / rangeY * height)); tourPoints.Add(locationPoint); } action = action.Successor; } if (tourPoints.Count > 1) { graphics.DrawLines(pastPens[((currentTour >= pastPens.Length) ? (pastPens.Length - 1) : (currentTour))], tourPoints.ToArray()); Point last = tourPoints.Last(); tourPoints.Clear(); tourPoints.Add(last); } while (action != null) { if (action is MoveVehicleAction) { MoveVehicleAction moveAction = action as MoveVehicleAction; if (tourPoints.Count > 0 || (moveAction.SourceX >= 0 && moveAction.SourceY >= 0)) { if (tourPoints.Count == 0) { tourPoints.Add(new Point((int)(border + moveAction.SourceX / rangeX * width), (int)(height + border - moveAction.SourceY / rangeY * height))); } Point locationPoint = new Point((int)(border + moveAction.TargetX / rangeX * width), (int)(height + border - moveAction.TargetY / rangeY * height)); tourPoints.Add(locationPoint); } } action = action.Successor; } if (tourPoints.Count > 1) { graphics.DrawLines(futurePens[((currentTour >= futurePens.Length) ? (futurePens.Length - 1) : (currentTour))], tourPoints.ToArray()); currentTour++; } } for (int i = 0; i < pastPens.Length; i++) pastPens[i].Dispose(); for (int i = 0; i < futurePens.Length; i++) futurePens[i].Dispose(); } else { using (Pen flowPen = new Pen(Brushes.Green)) { flowPen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor; //flowPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot; foreach (Order order in content.Orders) { if (order.OrderState != OrderState.Cancelled && order.OrderState != OrderState.Delivered) { Point pickup = new Point( (int)(border + order.PickupXCoord / rangeX * width), (int)(height + border - order.PickupYCoord / rangeY * height)); graphics.DrawImage(Resources.source, pickup.X - customerSize / 2, pickup.Y - customerSize / 2); if (!order.ServiceRequest) { Point delivery = new Point( (int)(border + order.DeliveryXCoord / rangeX * width), (int)(height + border - order.DeliveryYCoord / rangeY * height)); graphics.DrawImage(Resources.target, delivery.X - customerSize / 2, delivery.Y - customerSize / 2); graphics.DrawLine(flowPen, pickup, delivery); } } } } } foreach (Vehicle vehicle in content.Vehicles) { Point depotPoint = new Point( (int)(border + vehicle.DepotX / rangeX * width), (int)(height + border - vehicle.DepotY / rangeY * height)); graphics.DrawImage(Resources.depot, depotPoint.X - depotSize / 2, depotPoint.Y - depotSize / 2); } foreach (Vehicle vehicle in content.Vehicles) { Point vehiclePoint = new Point( (int)(border + vehicle.PositionX / rangeX * width), (int)(height + border - vehicle.PositionY / rangeY * height)); if (vehicle.VehicleState == VehicleState.Waiting || vehicle.VehicleState == VehicleState.Parked || vehicle.VehicleState == VehicleState.InvalidAction) { graphics.DrawImage(Resources.truck_idle, vehiclePoint.X - truckSize / 2, vehiclePoint.Y - truckSize / 2); } else { graphics.DrawImage(Resources.truck, vehiclePoint.X - truckSize / 2, vehiclePoint.Y - truckSize / 2); } } } pictureBox.Image = bitmap; } } } private void PickupDeliveryVisualizationView_SizeChanged(object sender, EventArgs e) { GenerateImage(); } protected override void OnContentChanged() { base.OnContentChanged(); GenerateImage(); } } }