Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966 fixed visualization of 2d packing

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