Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3157 was 3153, checked in by swagner, 15 years ago

Added TSP instance visualization (#924).

File size: 5.3 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.TSP.Views {
31  /// <summary>
32  /// The base class for visual representations of items.
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="ItemBaseView"/>.
44    /// </summary>
45    public PathTSPTourView() {
46      InitializeComponent();
47    }
48    /// <summary>
49    /// Intializes a new instance of <see cref="ItemBaseView"/> with the given <paramref name="item"/>.
50    /// </summary>
51    /// <param name="item">The item that should be displayed.</param>
52    public PathTSPTourView(PathTSPTour content)
53      : this() {
54      Content = content;
55    }
56
57    protected override void DeregisterContentEvents() {
58      Content.CoordinatesChanged -= new EventHandler(Content_CoordinatesChanged);
59      Content.PermutationChanged -= new EventHandler(Content_PermutationChanged);
60      base.DeregisterContentEvents();
61    }
62    protected override void RegisterContentEvents() {
63      base.RegisterContentEvents();
64      Content.CoordinatesChanged += new EventHandler(Content_CoordinatesChanged);
65      Content.PermutationChanged += new EventHandler(Content_PermutationChanged);
66    }
67
68    protected override void OnContentChanged() {
69      base.OnContentChanged();
70      if (Content == null) {
71        pictureBox.Image = null;
72        pictureBox.Enabled = false;
73      } else {
74        pictureBox.Enabled = true;
75        GenerateImage();
76      }
77    }
78
79    private 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.Permutation;
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 = (pictureBox.Width - 2 * border) / (xMax - xMin);
99            double yStep = (pictureBox.Height - 2 * border) / (yMax - yMin);
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                                    border + ((int)((coordinates[i, 1] - yMin) * yStep)));
105
106            Graphics graphics = Graphics.FromImage(bitmap);
107            if ((permutation != null) && (permutation.Length == coordinates.Rows) && (permutation.Validate())) {
108              Point[] tour = new Point[permutation.Length];
109              for (int i = 0; i < permutation.Length; i++) {
110                tour[i] = points[permutation[i]];
111              }
112              graphics.DrawPolygon(Pens.Black, tour);
113            }
114            for (int i = 0; i < points.Length; i++)
115              graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, 6, 6);
116            graphics.Dispose();
117          }
118          pictureBox.Image = bitmap;
119        }
120      }
121    }
122
123    private void Content_CoordinatesChanged(object sender, EventArgs e) {
124      if (InvokeRequired)
125        Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
126      else
127        GenerateImage();
128    }
129    private void Content_PermutationChanged(object sender, EventArgs e) {
130      if (InvokeRequired)
131        Invoke(new EventHandler(Content_PermutationChanged), sender, e);
132      else
133        GenerateImage();
134    }
135
136    private void pictureBox_SizeChanged(object sender, EventArgs e) {
137      GenerateImage();
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.