Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman.Views/3.3/TSPSolutionView.cs @ 17260

Last change on this file since 17260 was 17260, checked in by abeham, 4 years ago

#2521: Worked on PTSP refactoring

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.ComponentModel;
24using System.Drawing;
25using System.Windows.Forms;
26using HeuristicLab.Core.Views;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Problems.TravelingSalesman.Views {
32  /// <summary>
33  /// The base class for visual representations of a path tour for a TSP.
34  /// </summary>
35  [View("TSP Solution View")]
36  [Content(typeof(ITSPSolution), true)]
37  public partial class TSPSolutionView : ItemView {
38    public new ITSPSolution Content {
39      get { return (ITSPSolution)base.Content; }
40      set { base.Content = value; }
41    }
42
43    /// <summary>
44    /// Initializes a new instance of <see cref="TSPSolutionView"/>.
45    /// </summary>
46    public TSPSolutionView() {
47      InitializeComponent();
48    }
49
50    protected override void DeregisterContentEvents() {
51      Content.PropertyChanged -= ContentOnPropertyChanged;
52      base.DeregisterContentEvents();
53    }
54    protected override void RegisterContentEvents() {
55      base.RegisterContentEvents();
56      Content.PropertyChanged += ContentOnPropertyChanged;
57    }
58
59    protected override void OnContentChanged() {
60      base.OnContentChanged();
61      if (Content == null) {
62        qualityViewHost.Content = null;
63        pictureBox.Image = null;
64        tourViewHost.Content = null;
65      } else {
66        qualityViewHost.Content = Content.TourLength;
67        GenerateImage();
68        tourViewHost.Content = Content.Tour;
69      }
70    }
71
72    protected override void SetEnabledStateOfControls() {
73      base.SetEnabledStateOfControls();
74      qualityGroupBox.Enabled = Content != null;
75      pictureBox.Enabled = Content != null;
76      tourGroupBox.Enabled = Content != null;
77    }
78
79    protected virtual void GenerateImage() {
80      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
81        if (Content == null) {
82          pictureBox.Image = null;
83        } else {
84          DoubleMatrix coordinates = Content.Coordinates;
85          Permutation permutation = Content.Tour;
86          Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
87
88          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
89            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
90            for (int i = 0; i < coordinates.Rows; i++) {
91              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
92              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
93              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
94              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
95            }
96
97            int border = 20;
98            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
99            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
100
101            Point[] points = new Point[coordinates.Rows];
102            for (int i = 0; i < coordinates.Rows; i++)
103              points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
104                                    bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
105
106            using (Graphics graphics = Graphics.FromImage(bitmap)) {
107              if (permutation != null && permutation.Length > 1) {
108                Point[] tour = new Point[permutation.Length];
109                for (int i = 0; i < permutation.Length; i++) {
110                  if (permutation[i] >= 0 && permutation[i] < points.Length)
111                    tour[i] = points[permutation[i]];
112                }
113                graphics.DrawPolygon(Pens.Black, tour);
114              }
115              for (int i = 0; i < points.Length; i++)
116                graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, 6, 6);
117            }
118          } else {
119            using (Graphics graphics = Graphics.FromImage(bitmap)) {
120              graphics.Clear(Color.White);
121              Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
122              string text = "No coordinates defined or in wrong format.";
123              SizeF strSize = graphics.MeasureString(text, font);
124              graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
125            }
126          }
127          pictureBox.Image = bitmap;
128        }
129      }
130    }
131
132    protected virtual void ContentOnPropertyChanged(object sender, PropertyChangedEventArgs e) {
133      if (InvokeRequired)
134        Invoke((Action<object, PropertyChangedEventArgs>)ContentOnPropertyChanged, sender, e);
135      else {
136        switch (e.PropertyName) {
137          case nameof(Content.Coordinates):
138            GenerateImage();
139            break;
140          case nameof(Content.Tour):
141            GenerateImage();
142            tourViewHost.Content = Content.Tour;
143            break;
144          case nameof(Content.TourLength):
145            qualityViewHost.Content = Content.TourLength;
146            break;
147        }
148      }
149    }
150
151    private void pictureBox_SizeChanged(object sender, EventArgs e) {
152      GenerateImage();
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.