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 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 | using System.Windows.Forms;
|
---|
25 | using HeuristicLab.Core.Views;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
28 | using HeuristicLab.MainForm;
|
---|
29 |
|
---|
30 | namespace 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.Enabled = false;
|
---|
72 | } else {
|
---|
73 | pictureBox.Enabled = true;
|
---|
74 | }
|
---|
75 | GenerateImage();
|
---|
76 | }
|
---|
77 |
|
---|
78 | private void GenerateImage() {
|
---|
79 | if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
|
---|
80 | if (Content == null) {
|
---|
81 | pictureBox.Image = null;
|
---|
82 | } else {
|
---|
83 | DoubleMatrix coordinates = Content.Coordinates;
|
---|
84 | Permutation permutation = Content.Permutation;
|
---|
85 |
|
---|
86 | double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
|
---|
87 | for (int i = 0; i < coordinates.Rows; i++) {
|
---|
88 | if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
|
---|
89 | if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
|
---|
90 | if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
|
---|
91 | if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
|
---|
92 | }
|
---|
93 |
|
---|
94 | int border = 20;
|
---|
95 | double xStep = (pictureBox.Width - 2 * border) / (xMax - xMin);
|
---|
96 | double yStep = (pictureBox.Height - 2 * border) / (yMax - yMin);
|
---|
97 |
|
---|
98 | Point[] points = new Point[coordinates.Rows];
|
---|
99 | for (int i = 0; i < coordinates.Rows; i++)
|
---|
100 | points[i] = new Point(border + ((int)((coordinates[permutation[i], 0] - xMin) * xStep)),
|
---|
101 | border + ((int)((coordinates[permutation[i], 1] - yMin) * yStep)));
|
---|
102 |
|
---|
103 | Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
|
---|
104 | Graphics graphics = Graphics.FromImage(bitmap);
|
---|
105 | graphics.DrawPolygon(Pens.Black, points);
|
---|
106 | for (int i = 0; i < points.Length; i++)
|
---|
107 | graphics.FillRectangle(Brushes.Blue, points[i].X - 1, points[i].Y - 1, 4, 4);
|
---|
108 | graphics.Dispose();
|
---|
109 | pictureBox.Image = bitmap;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void Content_CoordinatesChanged(object sender, EventArgs e) {
|
---|
115 | if (InvokeRequired)
|
---|
116 | Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
|
---|
117 | else
|
---|
118 | GenerateImage();
|
---|
119 | }
|
---|
120 | private void Content_PermutationChanged(object sender, EventArgs e) {
|
---|
121 | if (InvokeRequired)
|
---|
122 | Invoke(new EventHandler(Content_PermutationChanged), sender, e);
|
---|
123 | else
|
---|
124 | GenerateImage();
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void pictureBox_SizeChanged(object sender, EventArgs e) {
|
---|
128 | GenerateImage();
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|