1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Joseph Helm and 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.Collections.Generic;
|
---|
23 | using SharpDX;
|
---|
24 |
|
---|
25 |
|
---|
26 | namespace PackingPlanVisualizations {
|
---|
27 | public class CenteredContainer2D {
|
---|
28 | private Vector2 containerSize;
|
---|
29 | private Vector2 controlSize;
|
---|
30 | private float normalizingFactor;
|
---|
31 |
|
---|
32 | public Vector2 ContainerCenter {
|
---|
33 | get { return new Vector2(controlSize.X / 2, controlSize.Y / 2); }
|
---|
34 | }
|
---|
35 |
|
---|
36 | private struct PackingItem {
|
---|
37 | private Vector2 position;
|
---|
38 | private Vector2 size;
|
---|
39 | private string label;
|
---|
40 | public PackingItem(Vector2 size, Vector2 position, string label, CenteredContainer2D parentContainer) {
|
---|
41 | this.size = size;
|
---|
42 | this.position = position;
|
---|
43 | this.label = label;
|
---|
44 | }
|
---|
45 | public RectangleF GetRectangle(Vector2 topLeft, float normalizingFactor) {
|
---|
46 | return new RectangleF(
|
---|
47 | topLeft.X + position.X * normalizingFactor + 2,
|
---|
48 | topLeft.Y + position.Y * normalizingFactor + 2,
|
---|
49 | topLeft.X + position.X * normalizingFactor + size.X * normalizingFactor - 2,
|
---|
50 | topLeft.Y + position.Y * normalizingFactor + size.Y * normalizingFactor - 2
|
---|
51 | );
|
---|
52 | }
|
---|
53 | public string GetLabel() {
|
---|
54 | return label;
|
---|
55 | }
|
---|
56 | }
|
---|
57 | private List<PackingItem> packingItems = new List<PackingItem>();
|
---|
58 |
|
---|
59 | //public struct LabelStructure {
|
---|
60 | // private string text;
|
---|
61 | // public string Text { get { return text; } }
|
---|
62 | // private RectangleF layoutRect;
|
---|
63 | // public RectangleF LayoutRectangle { get { return layoutRect; } }
|
---|
64 | // public LabelStructure(string text, RectangleF layout) {
|
---|
65 | // this.text = text;
|
---|
66 | // this.layoutRect = layout;
|
---|
67 | // }
|
---|
68 | //}
|
---|
69 |
|
---|
70 | public CenteredContainer2D (Vector2 controlSize, Vector2 containerSize) {
|
---|
71 | this.containerSize = containerSize;
|
---|
72 | UpdateContainer(controlSize);
|
---|
73 | }
|
---|
74 | public CenteredContainer2D(float controlWidth, float controlHeight, float containerWidth, float containerHeight)
|
---|
75 | : this(new Vector2 (controlWidth, controlHeight), new Vector2(containerWidth, containerHeight)) {
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void AddItem(Vector2 size, Vector2 position, string label) {
|
---|
79 | packingItems.Add(new PackingItem (size, position, label, this));
|
---|
80 | }
|
---|
81 | public void AddItem(float width, float height, float x, float y, string label) {
|
---|
82 | AddItem(new Vector2 (width, height), new Vector2 (x, y), label);
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void UpdateContainer(Vector2 controlSize) {
|
---|
86 | this.controlSize = controlSize;
|
---|
87 | if (controlSize.X / controlSize.Y > containerSize.X / containerSize.Y)
|
---|
88 | this.normalizingFactor = controlSize.Y / (containerSize.Y + 5);
|
---|
89 | else if (controlSize.X / controlSize.Y <= containerSize.X / containerSize.Y)
|
---|
90 | this.normalizingFactor = controlSize.X / (containerSize.X + 5);
|
---|
91 | }
|
---|
92 | public void UpdateContainer(float controlWidth, float controlHeight) {
|
---|
93 | UpdateContainer(new Vector2 (controlWidth, controlHeight));
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | public RectangleF GetContainerData() {
|
---|
98 | //Compute centered rectangle
|
---|
99 | SharpDX.RectangleF result = new SharpDX.RectangleF(
|
---|
100 | ContainerCenter.X - containerSize.X * normalizingFactor / 2 - 2,
|
---|
101 | ContainerCenter.Y - containerSize.Y * normalizingFactor / 2 - 2,
|
---|
102 | ContainerCenter.X + containerSize.X * normalizingFactor / 2 + 2,
|
---|
103 | ContainerCenter.Y + containerSize.Y * normalizingFactor / 2 + 2);
|
---|
104 |
|
---|
105 | return result;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public struct LabeledRectangle {
|
---|
109 | public string label;
|
---|
110 | public RectangleF rectangle;
|
---|
111 |
|
---|
112 | public LabeledRectangle(string label, RectangleF rectangle) {
|
---|
113 | this.label = label;
|
---|
114 | this.rectangle = rectangle;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | public List<LabeledRectangle> GetItemRectangles() {
|
---|
118 | List<LabeledRectangle> result = new List<LabeledRectangle> ();
|
---|
119 |
|
---|
120 | foreach (PackingItem item in packingItems) {
|
---|
121 | result.Add(new LabeledRectangle(item.GetLabel(), item.GetRectangle(new Vector2(
|
---|
122 | ContainerCenter.X - containerSize.X * normalizingFactor / 2,
|
---|
123 | ContainerCenter.Y - containerSize.Y * normalizingFactor / 2), normalizingFactor)));
|
---|
124 | }
|
---|
125 |
|
---|
126 | return result;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|