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

Last change on this file since 11240 was 11240, checked in by pfleck, 7 years ago

#2208 improved visualization of orienteering solution

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            double xMin = double.MaxValue, yMin = double.MaxValue, xMax = double.MinValue, yMax = double.MinValue;
88            for (int i = 0; i < coordinates.Rows; i++) {
89              if (xMin > coordinates[i, 0]) xMin = coordinates[i, 0];
90              if (yMin > coordinates[i, 1]) yMin = coordinates[i, 1];
91              if (xMax < coordinates[i, 0]) xMax = coordinates[i, 0];
92              if (yMax < coordinates[i, 1]) yMax = coordinates[i, 1];
93            }
94
95            int border = 20;
96            double xStep = xMax != xMin ? (pictureBox.Width - 2 * border) / (xMax - xMin) : 1;
97            double yStep = yMax != yMin ? (pictureBox.Height - 2 * border) / (yMax - yMin) : 1;
98
99            Point[] points = new Point[coordinates.Rows];
100            for (int i = 0; i < coordinates.Rows; i++)
101              points[i] = new Point(border + ((int)((coordinates[i, 0] - xMin) * xStep)),
102                                    bitmap.Height - (border + ((int)((coordinates[i, 1] - yMin) * yStep))));
103
104            using (Graphics graphics = Graphics.FromImage(bitmap)) {
105              if (integerVector != null && integerVector.Length > 1) {
106                Point[] tour = new Point[integerVector.Length];
107                for (int i = 0; i < integerVector.Length; i++) {
108                  tour[i] = points[integerVector[i]];
109                }
110                graphics.DrawPolygon(Pens.Black, tour);
111              }
112
113              double scoreMin = scores.Min();
114              double scoreMax = scores.Max();
115              double scoreRange = scoreMax - scoreMin;
116              for (int i = 0; i < points.Length; i++) {
117                double score = scores[i];
118                int size = (int)Math.Round(((score - scoreMin) / scoreRange) * 9 + 1);
119                graphics.FillRectangle(Brushes.Red, points[i].X - size / 2, points[i].Y - size / 2, size, size);
120              }
121            }
122          } else {
123            using (Graphics graphics = Graphics.FromImage(bitmap)) {
124              graphics.Clear(Color.White);
125              Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
126              string text = "No coordinates defined or in wrong format.";
127              SizeF strSize = graphics.MeasureString(text, font);
128              graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
129            }
130          }
131          pictureBox.Image = bitmap;
132        }
133      }
134    }
135
136    private void Content_QualityChanged(object sender, EventArgs e) {
137      if (InvokeRequired)
138        Invoke(new EventHandler(Content_QualityChanged), sender, e);
139      else
140        qualityViewHost.Content = Content.Quality;
141    }
142    private void Content_CoordinatesChanged(object sender, EventArgs e) {
143      if (InvokeRequired)
144        Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
145      else
146        GenerateImage();
147    }
148    private void Content_ScoresChanged(object sender, EventArgs e) {
149      if (InvokeRequired)
150        Invoke(new EventHandler(Content_ScoresChanged), sender, e);
151      else
152        GenerateImage();
153    }
154    private void Content_IntegerVectorChanged(object sender, EventArgs e) {
155      if (InvokeRequired)
156        Invoke(new EventHandler(Content_IntegerVectorChanged), sender, e);
157      else {
158        GenerateImage();
159        tourViewHost.Content = Content.IntegerVector;
160      }
161    }
162
163    private void pictureBox_SizeChanged(object sender, EventArgs e) {
164      GenerateImage();
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.