Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering.Views/3.3/OrienteeringSolutionView.cs @ 11245

Last change on this file since 11245 was 11245, checked in by pfleck, 10 years ago

#2208

  • Added visualization in the visualization tab of the problem
  • Fixed bug in shaking operator when tour only consists of start and end point
File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Linq;
25using HeuristicLab.Core.Views;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.Problems.Orienteering.Views {
29
30  [View("OrienteeringSolution View")]
31  [Content(typeof(OrienteeringSolution), true)]
32  public partial class OrienteeringSolutionView : ItemView {
33    public new OrienteeringSolution Content {
34      get { return (OrienteeringSolution)base.Content; }
35      set { base.Content = value; }
36    }
37    public OrienteeringSolutionView() {
38      InitializeComponent();
39    }
40
41    protected override void DeregisterContentEvents() {
42      Content.QualityChanged -= new EventHandler(Content_QualityChanged);
43      Content.CoordinatesChanged -= new EventHandler(Content_CoordinatesChanged);
44      Content.ScoresChanged -= new EventHandler(Content_ScoresChanged);
45      Content.IntegerVectorChanged -= new EventHandler(Content_IntegerVectorChanged);
46      base.DeregisterContentEvents();
47    }
48    protected override void RegisterContentEvents() {
49      base.RegisterContentEvents();
50      Content.QualityChanged += new EventHandler(Content_QualityChanged);
51      Content.CoordinatesChanged += new EventHandler(Content_CoordinatesChanged);
52      Content.ScoresChanged += new EventHandler(Content_ScoresChanged);
53      Content.IntegerVectorChanged += new EventHandler(Content_IntegerVectorChanged);
54    }
55
56    protected override void OnContentChanged() {
57      base.OnContentChanged();
58      if (Content == null) {
59        qualityViewHost.Content = null;
60        pictureBox.Image = null;
61        tourViewHost.Content = null;
62      } else {
63        qualityViewHost.Content = Content.Quality;
64        GenerateImage();
65        tourViewHost.Content = Content.IntegerVector;
66      }
67    }
68
69    protected override void SetEnabledStateOfControls() {
70      base.SetEnabledStateOfControls();
71      qualityGroupBox.Enabled = Content != null;
72      pictureBox.Enabled = Content != null;
73      tourGroupBox.Enabled = Content != null;
74    }
75
76    private void GenerateImage() {
77      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
78        if (Content == null) {
79          pictureBox.Image = null;
80        } else {
81          var coordinates = Content.Coordinates;
82          var scores = Content.Scores;
83          var integerVector = Content.IntegerVector;
84          var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
85
86          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)
87            && (scores != null) && (coordinates.Rows == scores.Length)) {
88            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
89            for (int i = 0; i < coordinates.Rows; i++) {
90              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
91              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
92              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
93              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
94            }
95
96            int border = 20;
97            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
98            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
99
100            Point[] points = new Point[coordinates.Rows];
101            for (int i = 0; i < coordinates.Rows; i++)
102              points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
103                                    bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
104
105            using (Graphics graphics = Graphics.FromImage(bitmap)) {
106              if (integerVector != null && integerVector.Length > 1) {
107                Point[] tour = new Point[integerVector.Length];
108                for (int i = 0; i < integerVector.Length; i++) {
109                  tour[i] = points[integerVector[i]];
110                }
111                graphics.DrawPolygon(Pens.Black, tour);
112              }
113
114              double scoreMin = scores.Min();
115              double scoreMax = scores.Max();
116              double scoreRange = scoreMax - scoreMin;
117              for (int i = 0; i < points.Length; i++) {
118                double score = scores[i];
119                int size = (int)Math.Round(((score - scoreMin) / scoreRange) * 8 + 2);
120                graphics.FillRectangle(Brushes.Red, points[i].X - size / 2, points[i].Y - size / 2, size, size);
121              }
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_ScoresChanged(object sender, EventArgs e) {
150      if (InvokeRequired)
151        Invoke(new EventHandler(Content_ScoresChanged), sender, e);
152      else
153        GenerateImage();
154    }
155    private void Content_IntegerVectorChanged(object sender, EventArgs e) {
156      if (InvokeRequired)
157        Invoke(new EventHandler(Content_IntegerVectorChanged), sender, e);
158      else {
159        GenerateImage();
160        tourViewHost.Content = Content.IntegerVector;
161      }
162    }
163
164    private void pictureBox_SizeChanged(object sender, EventArgs e) {
165      GenerateImage();
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.