[11235] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Drawing;
|
---|
[11240] | 24 | using System.Linq;
|
---|
[11235] | 25 | using HeuristicLab.Core.Views;
|
---|
| 26 | using HeuristicLab.MainForm;
|
---|
| 27 |
|
---|
| 28 | namespace 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);
|
---|
[11240] | 44 | Content.ScoresChanged -= new EventHandler(Content_ScoresChanged);
|
---|
[11235] | 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);
|
---|
[11240] | 52 | Content.ScoresChanged += new EventHandler(Content_ScoresChanged);
|
---|
[11235] | 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;
|
---|
[11240] | 82 | var scores = Content.Scores;
|
---|
[11235] | 83 | var integerVector = Content.IntegerVector;
|
---|
| 84 | var bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
|
---|
| 85 |
|
---|
[11245] | 86 | if ((coordinates != null) && (coordinates.Rows > 0) && (coordinates.Columns == 2)
|
---|
| 87 | && (scores != null) && (coordinates.Rows == scores.Length)) {
|
---|
[11235] | 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 | }
|
---|
[11266] | 111 | graphics.DrawLines(Pens.Black, tour);
|
---|
[11235] | 112 | }
|
---|
[11240] | 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];
|
---|
[11245] | 119 | int size = (int)Math.Round(((score - scoreMin) / scoreRange) * 8 + 2);
|
---|
[11240] | 120 | graphics.FillRectangle(Brushes.Red, points[i].X - size / 2, points[i].Y - size / 2, size, size);
|
---|
| 121 | }
|
---|
[11265] | 122 |
|
---|
| 123 | int startingPoint = Content.StartingPoint.Value;
|
---|
| 124 | int terminusPoint = Content.TerminusPoint.Value;
|
---|
| 125 | Font font = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular);
|
---|
| 126 | graphics.DrawString("Begin", font, Brushes.Black, points[startingPoint].X, points[startingPoint].Y);
|
---|
| 127 | graphics.DrawString("End", font, Brushes.Black, points[terminusPoint].X, points[terminusPoint].Y);
|
---|
[11235] | 128 | }
|
---|
| 129 | } else {
|
---|
| 130 | using (Graphics graphics = Graphics.FromImage(bitmap)) {
|
---|
| 131 | graphics.Clear(Color.White);
|
---|
| 132 | Font font = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);
|
---|
| 133 | string text = "No coordinates defined or in wrong format.";
|
---|
| 134 | SizeF strSize = graphics.MeasureString(text, font);
|
---|
| 135 | graphics.DrawString(text, font, Brushes.Black, (float)(pictureBox.Width - strSize.Width) / 2.0f, (float)(pictureBox.Height - strSize.Height) / 2.0f);
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | pictureBox.Image = bitmap;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | private void Content_QualityChanged(object sender, EventArgs e) {
|
---|
| 144 | if (InvokeRequired)
|
---|
| 145 | Invoke(new EventHandler(Content_QualityChanged), sender, e);
|
---|
| 146 | else
|
---|
| 147 | qualityViewHost.Content = Content.Quality;
|
---|
| 148 | }
|
---|
| 149 | private void Content_CoordinatesChanged(object sender, EventArgs e) {
|
---|
| 150 | if (InvokeRequired)
|
---|
| 151 | Invoke(new EventHandler(Content_CoordinatesChanged), sender, e);
|
---|
| 152 | else
|
---|
| 153 | GenerateImage();
|
---|
| 154 | }
|
---|
[11240] | 155 | private void Content_ScoresChanged(object sender, EventArgs e) {
|
---|
| 156 | if (InvokeRequired)
|
---|
| 157 | Invoke(new EventHandler(Content_ScoresChanged), sender, e);
|
---|
| 158 | else
|
---|
| 159 | GenerateImage();
|
---|
| 160 | }
|
---|
[11235] | 161 | private void Content_IntegerVectorChanged(object sender, EventArgs e) {
|
---|
| 162 | if (InvokeRequired)
|
---|
| 163 | Invoke(new EventHandler(Content_IntegerVectorChanged), sender, e);
|
---|
| 164 | else {
|
---|
| 165 | GenerateImage();
|
---|
| 166 | tourViewHost.Content = Content.IntegerVector;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | private void pictureBox_SizeChanged(object sender, EventArgs e) {
|
---|
| 171 | GenerateImage();
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | }
|
---|