Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2870_AutoDiff-nuget/HeuristicLab.Problems.PTSP.Views/3.3/PathPTSPTourView.cs @ 15653

Last change on this file since 15653 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

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