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

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

#1966 fixed visualization of 2d packing

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