Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2208 fixed OrienteeringSolutionView when score range is zero

File size: 8.2 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.CoordinatesChanged -= new EventHandler(Content_CoordinatesChanged);
45      Content.StartingPointChanged -= new EventHandler(Content_StartingPointChanged);
46      Content.TerminusPointChanged -= new EventHandler(Content_TerminusPointChanged);
47      Content.ScoresChanged -= new EventHandler(Content_ScoresChanged);
48      Content.IntegerVectorChanged -= new EventHandler(Content_IntegerVectorChanged);
49      base.DeregisterContentEvents();
50    }
51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      Content.QualityChanged += new EventHandler(Content_QualityChanged);
54      Content.CoordinatesChanged += new EventHandler(Content_CoordinatesChanged);
55      Content.StartingPointChanged += new EventHandler(Content_StartingPointChanged);
56      Content.TerminusPointChanged += new EventHandler(Content_TerminusPointChanged);
57      Content.ScoresChanged += new EventHandler(Content_ScoresChanged);
58      Content.IntegerVectorChanged += new EventHandler(Content_IntegerVectorChanged);
59    }
60
61    protected override void OnContentChanged() {
62      base.OnContentChanged();
63      if (Content == null) {
64        qualityViewHost.Content = null;
65        pictureBox.Image = null;
66        tourViewHost.Content = null;
67      } else {
68        qualityViewHost.Content = Content.Quality;
69        GenerateImage();
70        tourViewHost.Content = Content.IntegerVector;
71      }
72    }
73
74    protected override void SetEnabledStateOfControls() {
75      base.SetEnabledStateOfControls();
76      qualityGroupBox.Enabled = Content != null;
77      pictureBox.Enabled = Content != null;
78      tourGroupBox.Enabled = Content != null;
79    }
80
81    private void GenerateImage() {
82      if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
83        if (Content == null) {
84          pictureBox.Image = null;
85        } else {
86          var coordinates = Content.Coordinates;
87          var scores = Content.Scores;
88          var integerVector = Content.IntegerVector;
89          var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
90
91          if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)
92            && (scores != null) && (coordinates.Rows == scores.Length)) {
93            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
94            for (int i = 0; i < coordinates.Rows; i++) {
95              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
96              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
97              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
98              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
99            }
100
101            int border = 20;
102            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
103            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
104
105            Point[] points = new Point[coordinates.Rows];
106            for (int i = 0; i < coordinates.Rows; i++)
107              points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
108                                    bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
109
110            using (Graphics graphics = Graphics.FromImage(bitmap)) {
111              if (integerVector != null && integerVector.Length > 1) {
112                Point[] tour = new Point[integerVector.Length];
113                for (int i = 0; i < integerVector.Length; i++) {
114                  tour[i] = points[integerVector[i]];
115                }
116                graphics.DrawLines(Pens.Black, tour);
117              }
118
119              double scoreMin = scores.Min();
120              double scoreMax = scores.Max();
121              double scoreRange = scoreMax - scoreMin;
122              for (int i = 0; i < points.Length; i++) {
123                double score = scores[i];
124                int size = scoreRange.IsAlmost(0.0)
125                  ? 6
126                  : (int)Math.Round(((score - scoreMin) / scoreRange) * 8 + 2);
127                graphics.FillRectangle(Brushes.Red, points[i].X - size / 2, points[i].Y - size / 2, size, size);
128              }
129
130              int startingPoint = Content.StartingPoint.Value;
131              int terminusPoint = Content.TerminusPoint.Value;
132              Font font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
133              graphics.DrawString("Begin", font, Brushes.Black, points[startingPoint].X, points[startingPoint].Y);
134              graphics.DrawString("End", font, Brushes.Black, points[terminusPoint].X, points[terminusPoint].Y);
135            }
136          } else {
137            using (Graphics graphics = Graphics.FromImage(bitmap)) {
138              graphics.Clear(Color.White);
139              Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
140              string text = "No coordinates defined or in wrong format.";
141              SizeF strSize = graphics.MeasureString(text, font);
142              graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
143            }
144          }
145          pictureBox.Image = bitmap;
146        }
147      }
148    }
149
150    private void Content_QualityChanged(object sender, EventArgs e) {
151      if (InvokeRequired)
152        Invoke(new EventHandler(Content_QualityChanged), sender, e);
153      else
154        qualityViewHost.Content = Content.Quality;
155    }
156    private void Content_CoordinatesChanged(object sender, EventArgs e) {
157      if (InvokeRequired)
158        Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
159      else
160        GenerateImage();
161    }
162    private void Content_StartingPointChanged(object sender, EventArgs e) {
163      if (InvokeRequired)
164        Invoke(new EventHandler(Content_StartingPointChanged), sender, e);
165      else
166        GenerateImage();
167    }
168    private void Content_TerminusPointChanged(object sender, EventArgs e) {
169      if (InvokeRequired)
170        Invoke(new EventHandler(Content_TerminusPointChanged), sender, e);
171      else
172        GenerateImage();
173    }
174    private void Content_ScoresChanged(object sender, EventArgs e) {
175      if (InvokeRequired)
176        Invoke(new EventHandler(Content_ScoresChanged), sender, e);
177      else
178        GenerateImage();
179    }
180    private void Content_IntegerVectorChanged(object sender, EventArgs e) {
181      if (InvokeRequired)
182        Invoke(new EventHandler(Content_IntegerVectorChanged), sender, e);
183      else {
184        GenerateImage();
185        tourViewHost.Content = Content.IntegerVector;
186      }
187    }
188
189    private void pictureBox_SizeChanged(object sender, EventArgs e) {
190      GenerateImage();
191    }
192  }
193}
Note: See TracBrowser for help on using the repository browser.