Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.Views/3.3/Container2DView.xaml.cs @ 13578

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

#1966: implemented visualization for 2d packings

File size: 3.0 KB
Line 
1using System;
2using System.Linq;
3using System.Windows;
4using System.Windows.Forms;
5using System.Windows.Input;
6using System.Windows.Media;
7using System.Windows.Media.Media3D;
8using HeuristicLab.Encodings.PackingEncoding.PackingPlan;
9using HeuristicLab.Problems.BinPacking.Dimensions;
10using HeuristicLab.Problems.BinPacking.PackingBin;
11using HeuristicLab.Problems.BinPacking.PackingItem;
12using Point = System.Windows.Point;
13using Size = System.Windows.Size;
14using UserControl = System.Windows.Controls.UserControl;
15
16namespace HeuristicLab.Problems.BinPacking.Views {
17  public partial class Container2DView : UserControl {
18    private int selectedItemKey;
19    private Size renderSize;
20    private BinPacking<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> packing;
21    public BinPacking<TwoDimensionalPacking, RectangularPackingBin, RectangularPackingItem> Packing {
22      get { return packing; }
23      set {
24        if (packing != value) {
25          this.packing = value;
26          ClearSelection(); // also updates visualization
27        }
28      }
29    }
30
31    public Container2DView() {
32      InitializeComponent();
33    }
34
35
36    protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) {
37      base.OnRenderSizeChanged(sizeInfo);
38      var s = Math.Min(sizeInfo.NewSize.Height, sizeInfo.NewSize.Width);
39      renderSize = new Size(s, s);
40      InvalidateVisual();
41    }
42
43    // similar implementation to Container3DView (even though item selection is not supported for 2d packings TODO)
44    public void SelectItem(int itemKey) {
45      // selection of an item should make all other items semi-transparent
46      selectedItemKey = itemKey;
47      InvalidateVisual();
48    }
49    public void ClearSelection() {
50      // remove all transparency
51      selectedItemKey = -1;
52      InvalidateVisual();
53    }
54
55    protected override void OnRender(DrawingContext drawingContext) {
56      base.OnRender(drawingContext);
57      if (packing == null) return;
58      // the container should fill the whole size
59      var scalingX = renderSize.Width / Packing.BinMeasures.Width;
60      var scalingY = renderSize.Height / Packing.BinMeasures.Width;
61      // draw container
62      drawingContext.DrawRectangle(Brushes.LightGray, new Pen(Brushes.Black, 1), new Rect(new Point(0, 0), renderSize));
63
64      var selectedBrush = Brushes.MediumSeaGreen;
65      var unselectedBrush = selectedItemKey < 0 ? selectedBrush : Brushes.DarkGray;
66
67      foreach (var t in Packing.ItemMeasures) {
68        var key = t.Key;
69        var item = t.Value;
70        var pos = Packing.ItemPositions[key];
71
72        var scaledPos = new Point(pos.X * scalingX, pos.Y * scalingY);
73
74        var scaledSize = pos.Rotated ?
75          new Size(item.Height * scalingX, item.Width * scalingY) :
76          new Size(item.Width * scalingX, item.Height * scalingY);
77
78        var brush = key == selectedItemKey ? selectedBrush : unselectedBrush;
79        drawingContext.DrawRectangle(brush, new Pen(Brushes.Black, 1), new Rect(scaledPos, scaledSize));
80      }
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.