source: branches/HeuristicLab.BinPacking/PackingPlanVisualizations/2D/CenteredContainer2D.cs @ 13497

Last change on this file since 13497 was 13497, checked in by gkronber, 5 years ago

#1966: fixed various problems: bugs in cloning, bugs in persistence, method names, various minor improvements of source code for readability.

File size: 3.3 KB
Line 
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
22using System.Collections.Generic;
23using System.Linq;
24using SharpDX;
25
26
27namespace PackingPlanVisualizations {
28  public class CenteredContainer2D {
29    private Vector2 containerSize;
30    private float xScaling;
31    private float yScaling;
32    private readonly List<PackingItem> packingItems = new List<PackingItem>();
33
34    private struct PackingItem {
35      internal readonly Vector2 position;
36      internal readonly Vector2 size;
37      internal readonly string label;
38      public PackingItem(Vector2 size, Vector2 position, string label) {
39        this.size = size;
40        this.position = position;
41        this.label = label;
42      }
43    }
44
45    public CenteredContainer2D(Vector2 controlSize, Vector2 containerSize) {
46      this.containerSize = containerSize;
47      UpdateContainer(controlSize);
48    }
49    public CenteredContainer2D(float controlWidth, float controlHeight, float containerWidth, float containerHeight)
50      : this(new Vector2(controlWidth, controlHeight), new Vector2(containerWidth, containerHeight)) {
51    }
52
53    public void AddItem(Vector2 size, Vector2 position, string label) {
54      packingItems.Add(new PackingItem(size, position, label));
55    }
56    public void AddItem(float width, float height, float x, float y, string label) {
57      AddItem(new Vector2(width, height), new Vector2(x, y), label);
58    }
59
60    public void UpdateContainer(Vector2 controlSize) {
61      xScaling = (controlSize.X - 4) / (containerSize.X); // leave 2 pixel space on each side
62      yScaling = (controlSize.Y - 4) / (containerSize.Y);
63    }
64    public void UpdateContainer(float controlWidth, float controlHeight) {
65      UpdateContainer(new Vector2(controlWidth, controlHeight));
66    }
67
68
69    public RectangleF GetContainerData() {
70      //Compute centered rectangle
71      return new RectangleF(2, 2, containerSize.X * xScaling, containerSize.Y * yScaling);
72    }
73
74    public struct LabeledRectangle {
75      public string label;
76      public RectangleF rectangle;
77
78      public LabeledRectangle(string label, RectangleF rectangle) {
79        this.label = label;
80        this.rectangle = rectangle;
81      }
82    }
83
84    public List<LabeledRectangle> GetItemRectangles() {
85      return packingItems.Select(item =>
86        new LabeledRectangle(item.label,
87          new RectangleF(item.position.X * xScaling + 2, item.position.Y * yScaling + 2, item.size.X * xScaling, item.size.Y * yScaling)))
88      .ToList();
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.