Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PTSP/HeuristicLab.Problems.PTSP.Views/3.3/PathPTSPTourView.cs @ 12676

Last change on this file since 12676 was 12306, checked in by apolidur, 10 years ago

#2221: Prepared PTSP for instance input

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Drawing;
24using System.Windows.Forms;
25using HeuristicLab.Core.Views;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Problems.PTSP.Views {
31  /// <summary>
32  /// The base class for visual representations of a path tour for a PTSP.
33  /// </summary>
34  [View("PathPTSPTour View")]
35  [Content(typeof(PathPTSPTour), true)]
36  public sealed partial class PathPTSPTourView : ItemView {
37    public new PathPTSPTour Content {
38      get { return (PathPTSPTour)base.Content; }
39      set { base.Content = value; }
40    }
41
42    /// <summary>
43    /// Initializes a new instance of <see cref="PathPTSPTourView"/>.
44    /// </summary>
45    public PathPTSPTourView() {
46      InitializeComponent();
47    }
48
49    protected override void DeregisterContentEvents() {
50      Content.QualityChanged -= new EventHandler(Content_QualityChanged);
51      Content.CoordinatesChanged -= new EventHandler(Content_CoordinatesChanged);
52      Content.PermutationChanged -= new EventHandler(Content_PermutationChanged);
53      Content.ProbabilitiesChanged -= new EventHandler(Content_ProbabilitiesChanged);
54      base.DeregisterContentEvents();
55    }
56    protected override void RegisterContentEvents() {
57      base.RegisterContentEvents();
58      Content.QualityChanged += new EventHandler(Content_QualityChanged);
59      Content.CoordinatesChanged += new EventHandler(Content_CoordinatesChanged);
60      Content.PermutationChanged += new EventHandler(Content_PermutationChanged);
61      Content.ProbabilitiesChanged += new EventHandler(Content_ProbabilitiesChanged);
62    }
63
64    protected override void OnContentChanged() {
65      base.OnContentChanged();
66      if (Content == null) {
67        qualityViewHost.Content = null;
68        pictureBox.Image = null;
69        tourViewHost.Content = null;
70      } else {
71        qualityViewHost.Content = Content.Quality;
72        GenerateImage();
73        tourViewHost.Content = Content.Permutation;
74      }
75    }
76
77    protected override void SetEnabledStateOfControls() {
78      base.SetEnabledStateOfControls();
79      qualityGroupBox.Enabled = Content != null;
80      pictureBox.Enabled = Content != null;
81      tourGroupBox.Enabled = Content != null;
82    }
83
84    private void GenerateImage() {
85      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
86        if (Content == null) {
87          pictureBox.Image = null;
88        } else {
89          DoubleMatrix coordinates = Content.Coordinates;
90          Permutation permutation = Content.Permutation;
91          DoubleArray probabilities = Content.Probabilities;
92          Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
93
94          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2) && (probabilities.Length == coordinates.Rows)) {
95            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
96            for (int i = 0; i < coordinates.Rows; i++) {
97              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
98              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
99              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
100              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
101            }
102
103            int border = 20;
104            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
105            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
106
107            Point[] points = new Point[coordinates.Rows];
108            for (int i = 0; i < coordinates.Rows; i++)
109              points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
110                                    bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
111
112            using (Graphics graphics = Graphics.FromImage(bitmap)) {
113              if (permutation != null && permutation.Length > 1) {
114                Point[] tour = new Point[permutation.Length];
115                for (int i = 0; i < permutation.Length; i++) {
116                  if (permutation[i] >= 0 && permutation[i] < points.Length)
117                    tour[i] = points[permutation[i]];
118                }
119                graphics.DrawPolygon(Pens.Black, tour);
120              }
121              for (int i = 0; i < points.Length; i++)
122                graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, Convert.ToInt32(probabilities[i] * 20), Convert.ToInt32(probabilities[i] * 20));
123            }
124          } else {
125            using (Graphics graphics = Graphics.FromImage(bitmap)) {
126              graphics.Clear(Color.White);
127              Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
128              string text = "No coordinates defined or in wrong format.";
129              SizeF strSize = graphics.MeasureString(text, font);
130              graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
131            }
132          }
133          pictureBox.Image = bitmap;
134        }
135      }
136    }
137
138    private void Content_QualityChanged(object sender, EventArgs e) {
139      if (InvokeRequired)
140        Invoke(new EventHandler(Content_QualityChanged), sender, e);
141      else
142        qualityViewHost.Content = Content.Quality;
143    }
144    private void Content_CoordinatesChanged(object sender, EventArgs e) {
145      if (InvokeRequired)
146        Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
147      else
148        GenerateImage();
149    }
150    private void Content_PermutationChanged(object sender, EventArgs e) {
151      if (InvokeRequired)
152        Invoke(new EventHandler(Content_PermutationChanged), sender, e);
153      else {
154        GenerateImage();
155        tourViewHost.Content = Content.Permutation;
156      }
157    }
158
159    private void Content_ProbabilitiesChanged(object sender, EventArgs e) {
160      if (InvokeRequired)
161        Invoke(new EventHandler(Content_ProbabilitiesChanged), sender, e);
162      else {
163        GenerateImage();
164      }
165    }
166
167    private void pictureBox_SizeChanged(object sender, EventArgs e) {
168      GenerateImage();
169    }
170  }
171}
Note: See TracBrowser for help on using the repository browser.