[3107] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11208] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3107] | 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 |
|
---|
[3158] | 30 | namespace HeuristicLab.Problems.TravelingSalesman.Views {
|
---|
[3107] | 31 | /// <summary>
|
---|
[4373] | 32 | /// The base class for visual representations of a path tour for a TSP.
|
---|
[3107] | 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>
|
---|
[4373] | 43 | /// Initializes a new instance of <see cref="PathTSPTourView"/>.
|
---|
[3107] | 44 | /// </summary>
|
---|
| 45 | public PathTSPTourView() {
|
---|
| 46 | InitializeComponent();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | protected override void DeregisterContentEvents() {
|
---|
[3693] | 50 | Content.QualityChanged -= new EventHandler(Content_QualityChanged);
|
---|
[3107] | 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();
|
---|
[3693] | 57 | Content.QualityChanged += new EventHandler(Content_QualityChanged);
|
---|
[3107] | 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) {
|
---|
[3693] | 65 | qualityViewHost.Content = null;
|
---|
[3153] | 66 | pictureBox.Image = null;
|
---|
[3693] | 67 | tourViewHost.Content = null;
|
---|
[3107] | 68 | } else {
|
---|
[3693] | 69 | qualityViewHost.Content = Content.Quality;
|
---|
[3153] | 70 | GenerateImage();
|
---|
[3693] | 71 | tourViewHost.Content = Content.Permutation;
|
---|
[3107] | 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[3904] | 75 | protected override void SetEnabledStateOfControls() {
|
---|
| 76 | base.SetEnabledStateOfControls();
|
---|
[3693] | 77 | qualityGroupBox.Enabled = Content != null;
|
---|
[3454] | 78 | pictureBox.Enabled = Content != null;
|
---|
[3693] | 79 | tourGroupBox.Enabled = Content != null;
|
---|
[3454] | 80 | }
|
---|
| 81 |
|
---|
[3107] | 82 | private void GenerateImage() {
|
---|
| 83 | if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
|
---|
| 84 | if (Content == null) {
|
---|
| 85 | pictureBox.Image = null;
|
---|
| 86 | } else {
|
---|
| 87 | DoubleMatrix coordinates = Content.Coordinates;
|
---|
| 88 | Permutation permutation = Content.Permutation;
|
---|
[3139] | 89 | Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
|
---|
[3107] | 90 |
|
---|
[3153] | 91 | if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)) {
|
---|
[3139] | 92 | double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
|
---|
| 93 | for (int i = 0; i < coordinates.Rows; i++) {
|
---|
| 94 | if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
|
---|
| 95 | if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
|
---|
| 96 | if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
|
---|
| 97 | if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
|
---|
| 98 | }
|
---|
[3107] | 99 |
|
---|
[3139] | 100 | int border = 20;
|
---|
[3502] | 101 | double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
|
---|
| 102 | double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
|
---|
[3107] | 103 |
|
---|
[3139] | 104 | Point[] points = new Point[coordinates.Rows];
|
---|
[3153] | 105 | for (int i = 0; i < coordinates.Rows; i++)
|
---|
| 106 | points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
|
---|
[3213] | 107 | bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
|
---|
[3107] | 108 |
|
---|
[3502] | 109 | using (Graphics graphics = Graphics.FromImage(bitmap)) {
|
---|
[11208] | 110 | if (permutation != null && permutation.Length > 1) {
|
---|
[3502] | 111 | Point[] tour = new Point[permutation.Length];
|
---|
| 112 | for (int i = 0; i < permutation.Length; i++) {
|
---|
| 113 | tour[i] = points[permutation[i]];
|
---|
| 114 | }
|
---|
| 115 | graphics.DrawPolygon(Pens.Black, tour);
|
---|
[3153] | 116 | }
|
---|
[3502] | 117 | for (int i = 0; i < points.Length; i++)
|
---|
| 118 | graphics.FillRectangle(Brushes.Red, points[i].X - 2, points[i].Y - 2, 6, 6);
|
---|
[3153] | 119 | }
|
---|
[7621] | 120 | } else {
|
---|
| 121 | using (Graphics graphics = Graphics.FromImage(bitmap)) {
|
---|
| 122 | graphics.Clear(Color.White);
|
---|
| 123 | Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
|
---|
| 124 | string text = "No coordinates defined or in wrong format.";
|
---|
| 125 | SizeF strSize = graphics.MeasureString(text, font);
|
---|
| 126 | graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
|
---|
| 127 | }
|
---|
[3139] | 128 | }
|
---|
[3107] | 129 | pictureBox.Image = bitmap;
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[3693] | 134 | private void Content_QualityChanged(object sender, EventArgs e) {
|
---|
| 135 | if (InvokeRequired)
|
---|
| 136 | Invoke(new EventHandler(Content_QualityChanged), sender, e);
|
---|
| 137 | else
|
---|
| 138 | qualityViewHost.Content = Content.Quality;
|
---|
| 139 | }
|
---|
[3107] | 140 | private void Content_CoordinatesChanged(object sender, EventArgs e) {
|
---|
| 141 | if (InvokeRequired)
|
---|
| 142 | Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
|
---|
| 143 | else
|
---|
| 144 | GenerateImage();
|
---|
| 145 | }
|
---|
| 146 | private void Content_PermutationChanged(object sender, EventArgs e) {
|
---|
| 147 | if (InvokeRequired)
|
---|
| 148 | Invoke(new EventHandler(Content_PermutationChanged), sender, e);
|
---|
[3693] | 149 | else {
|
---|
[3107] | 150 | GenerateImage();
|
---|
[3693] | 151 | tourViewHost.Content = Content.Permutation;
|
---|
| 152 | }
|
---|
[3107] | 153 | }
|
---|
| 154 |
|
---|
| 155 | private void pictureBox_SizeChanged(object sender, EventArgs e) {
|
---|
| 156 | GenerateImage();
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | }
|
---|