Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TravelingSalesman.Views/3.3/PathTSPTourView.cs @ 4373

Last change on this file since 4373 was 4373, checked in by swinkler, 14 years ago

Corrected comment and description in MultiPermutationManipulator and PathTSPTourView. (#1182)

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.TravelingSalesman.Views {
31  /// <summary>
32  /// The base class for visual representations of a path tour for a TSP.
33  /// </summary>
34  [View("PathTSPTour View")]
35  [Content(typeof(PathTSPTour), true)]
36  public sealed partial class PathTSPTourView : ItemView {
37    public new PathTSPTour Content {
38      get { return (PathTSPTour)base.Content; }
39      set { base.Content = value; }
40    }
41
42    /// <summary>
43    /// Initializes a new instance of <see cref="PathTSPTourView"/>.
44    /// </summary>
45    public PathTSPTourView() {
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      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    }
61
62    protected override void OnContentChanged() {
63      base.OnContentChanged();
64      if (Content == null) {
65        qualityViewHost.Content = null;
66        pictureBox.Image = null;
67        tourViewHost.Content = null;
68      } else {
69        qualityViewHost.Content = Content.Quality;
70        GenerateImage();
71        tourViewHost.Content = Content.Permutation;
72      }
73    }
74
75    protected override void OnReadOnlyChanged() {
76      base.OnReadOnlyChanged();
77      SetEnabledStateOfControls();
78    }
79
80    protected override void SetEnabledStateOfControls() {
81      base.SetEnabledStateOfControls();
82      qualityGroupBox.Enabled = Content != null;
83      pictureBox.Enabled = Content != null;
84      tourGroupBox.Enabled = Content != null;
85    }
86
87    private void GenerateImage() {
88      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
89        if (Content == null) {
90          pictureBox.Image = null;
91        } else {
92          DoubleMatrix coordinates = Content.Coordinates;
93          Permutation permutation = Content.Permutation;
94          Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
95
96          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
97            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
98            for (int i = 0; i < coordinates.Rows; i++) {
99              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
100              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
101              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
102              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
103            }
104
105            int border = 20;
106            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
107            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
108
109            Point[] points = new Point[coordinates.Rows];
110            for (int i = 0; i < coordinates.Rows; i++)
111              points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
112                                    bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
113
114            using (Graphics graphics = Graphics.FromImage(bitmap)) {
115              if ((permutation != null) && (permutation.Length == coordinates.Rows) && (permutation.Validate())) {
116                Point[] tour = new Point[permutation.Length];
117                for (int i = 0; i < permutation.Length; i++) {
118                  tour[i] = points[permutation[i]];
119                }
120                graphics.DrawPolygon(Pens.Black, tour);
121              }
122              for (int i = 0; i < points.Length; i++)
123                graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, 6, 6);
124            }
125          }
126          pictureBox.Image = bitmap;
127        }
128      }
129    }
130
131    private void Content_QualityChanged(object sender, EventArgs e) {
132      if (InvokeRequired)
133        Invoke(new EventHandler(Content_QualityChanged), sender, e);
134      else
135        qualityViewHost.Content = Content.Quality;
136    }
137    private void Content_CoordinatesChanged(object sender, EventArgs e) {
138      if (InvokeRequired)
139        Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
140      else
141        GenerateImage();
142    }
143    private void Content_PermutationChanged(object sender, EventArgs e) {
144      if (InvokeRequired)
145        Invoke(new EventHandler(Content_PermutationChanged), sender, e);
146      else {
147        GenerateImage();
148        tourViewHost.Content = Content.Permutation;
149      }
150    }
151
152    private void pictureBox_SizeChanged(object sender, EventArgs e) {
153      GenerateImage();
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.