Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2208

  • Fixed OrienteeringSolution visualization when start or endpoint are out of range
  • Changed some parameters in OP to FixedValueParameter
  • Added start/terminal index check in OP
File size: 9.0 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.Common;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28
29namespace HeuristicLab.Problems.Orienteering.Views {
30
31  [View("OrienteeringSolution View")]
32  [Content(typeof(OrienteeringSolution), true)]
33  public partial class OrienteeringSolutionView : ItemView {
34    public new OrienteeringSolution Content {
35      get { return (OrienteeringSolution)base.Content; }
36      set { base.Content = value; }
37    }
38    public OrienteeringSolutionView() {
39      InitializeComponent();
40    }
41
42    protected override void DeregisterContentEvents() {
43      Content.QualityChanged -= new EventHandler(Content_QualityChanged);
44      Content.PenaltyChanged -= new EventHandler(Content_PenaltyChanged);
45      Content.CoordinatesChanged -= new EventHandler(Content_CoordinatesChanged);
46      Content.StartingPointChanged -= new EventHandler(Content_StartingPointChanged);
47      Content.TerminalPointChanged -= new EventHandler(Content_TerminalPointChanged);
48      Content.ScoresChanged -= new EventHandler(Content_ScoresChanged);
49      Content.IntegerVectorChanged -= new EventHandler(Content_IntegerVectorChanged);
50      base.DeregisterContentEvents();
51    }
52    protected override void RegisterContentEvents() {
53      base.RegisterContentEvents();
54      Content.QualityChanged += new EventHandler(Content_QualityChanged);
55      Content.PenaltyChanged += new EventHandler(Content_PenaltyChanged);
56      Content.CoordinatesChanged += new EventHandler(Content_CoordinatesChanged);
57      Content.StartingPointChanged += new EventHandler(Content_StartingPointChanged);
58      Content.TerminalPointChanged += new EventHandler(Content_TerminalPointChanged);
59      Content.ScoresChanged += new EventHandler(Content_ScoresChanged);
60      Content.IntegerVectorChanged += new EventHandler(Content_IntegerVectorChanged);
61    }
62
63    protected override void OnContentChanged() {
64      base.OnContentChanged();
65      if (Content == null) {
66        qualityViewHost.Content = null;
67        pictureBox.Image = null;
68        tourViewHost.Content = null;
69      } else {
70        qualityViewHost.Content = Content.Quality;
71        GenerateImage();
72        tourViewHost.Content = Content.IntegerVector;
73      }
74    }
75
76    protected override void SetEnabledStateOfControls() {
77      base.SetEnabledStateOfControls();
78      qualityGroupBox.Enabled = Content != null;
79      pictureBox.Enabled = Content != null;
80      tourGroupBox.Enabled = Content != null;
81    }
82
83    private void GenerateImage() {
84      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
85        if (Content == null) {
86          pictureBox.Image = null;
87        } else {
88          var coordinates = Content.Coordinates;
89          var scores = Content.Scores;
90          var integerVector = Content.IntegerVector;
91          var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
92
93          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)
94            && (scores != null) && (coordinates.Rows == scores.Length)) {
95            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
96            for (int i = 0; i < coordinates.Rows; i++) {
97              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
98              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
99              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
100              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
101            }
102
103            int border = 20;
104            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
105            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
106
107            Point[] points = new Point[coordinates.Rows];
108            for (int i = 0; i < coordinates.Rows; i++)
109              points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
110                                    bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
111
112            using (Graphics graphics = Graphics.FromImage(bitmap)) {
113              if (integerVector != null && integerVector.Length > 1) {
114                Point[] tour = new Point[integerVector.Length];
115                for (int i = 0; i < integerVector.Length; i++) {
116                  tour[i] = points[integerVector[i]];
117                }
118                bool visualizePenalty = Content.Penalty != null && Content.Penalty.Value > 0;
119                graphics.DrawLines(visualizePenalty ? Pens.Red : Pens.Black, tour);
120              }
121
122              double scoreMin = scores.Min();
123              double scoreMax = scores.Max();
124              double scoreRange = scoreMax - scoreMin;
125              for (int i = 0; i < points.Length; i++) {
126                double score = scores[i];
127                int size = scoreRange.IsAlmost(0.0)
128                  ? 6
129                  : (int)Math.Round(((score - scoreMin) / scoreRange) * 8 + 2);
130                graphics.FillRectangle(Brushes.Red, points[i].X - size / 2, points[i].Y - size / 2, size, size);
131              }
132              int startingPoint = Content.StartingPoint.Value;
133              int terminalPoint = Content.TerminalPoint.Value;
134              Font font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
135              var beginSize = graphics.MeasureString("Begin", font);
136              if (startingPoint >= 0 && startingPoint < points.Length)
137                graphics.DrawString("Begin", font, Brushes.Black, points[startingPoint].X - beginSize.Width, points[startingPoint].Y - beginSize.Height);
138              if (terminalPoint >= 0 && terminalPoint < points.Length)
139                graphics.DrawString("End", font, Brushes.Black, points[terminalPoint].X, points[terminalPoint].Y);
140            }
141          } else {
142            using (Graphics graphics = Graphics.FromImage(bitmap)) {
143              graphics.Clear(Color.White);
144              Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
145              string text = "No coordinates defined or in wrong format.";
146              SizeF strSize = graphics.MeasureString(text, font);
147              graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
148            }
149          }
150          pictureBox.Image = bitmap;
151        }
152      }
153    }
154
155    private void Content_QualityChanged(object sender, EventArgs e) {
156      if (InvokeRequired)
157        Invoke(new EventHandler(Content_QualityChanged), sender, e);
158      else
159        qualityViewHost.Content = Content.Quality;
160    }
161    private void Content_PenaltyChanged(object sender, EventArgs e) {
162      if (InvokeRequired)
163        Invoke(new EventHandler(Content_PenaltyChanged), sender, e);
164      else
165        GenerateImage();
166    }
167    private void Content_CoordinatesChanged(object sender, EventArgs e) {
168      if (InvokeRequired)
169        Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
170      else
171        GenerateImage();
172    }
173    private void Content_StartingPointChanged(object sender, EventArgs e) {
174      if (InvokeRequired)
175        Invoke(new EventHandler(Content_StartingPointChanged), sender, e);
176      else
177        GenerateImage();
178    }
179    private void Content_TerminalPointChanged(object sender, EventArgs e) {
180      if (InvokeRequired)
181        Invoke(new EventHandler(Content_TerminalPointChanged), sender, e);
182      else
183        GenerateImage();
184    }
185    private void Content_ScoresChanged(object sender, EventArgs e) {
186      if (InvokeRequired)
187        Invoke(new EventHandler(Content_ScoresChanged), sender, e);
188      else
189        GenerateImage();
190    }
191    private void Content_IntegerVectorChanged(object sender, EventArgs e) {
192      if (InvokeRequired)
193        Invoke(new EventHandler(Content_IntegerVectorChanged), sender, e);
194      else {
195        GenerateImage();
196        tourViewHost.Content = Content.IntegerVector;
197      }
198    }
199
200    private void pictureBox_SizeChanged(object sender, EventArgs e) {
201      GenerateImage();
202    }
203  }
204}
Note: See TracBrowser for help on using the repository browser.