Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/PackingPlanVisualizations/2D/PackingPlan2D.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: 4.5 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;
23using SharpDX.Direct2D1;
24using Factory2D1 = SharpDX.Direct2D1.Factory;
25using FactoryType2D1 = SharpDX.Direct2D1.FactoryType;
26using System.Windows.Forms;
27using SharpDX.DXGI;
28using SharpDX;
29using Colors = SharpDX.Color;
30using SharpDX.DirectWrite;
31
32
33namespace PackingPlanVisualizations {
34  public partial class PackingPlan2D : UserControl {
35    WindowRenderTarget wndRender = null;
36    Factory2D1 factory = new Factory2D1(FactoryType2D1.SingleThreaded);
37    SharpDX.DirectWrite.Factory dwFactory = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared);
38    RenderTargetProperties rndTargProperties = new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied));
39    HwndRenderTargetProperties hwndProperties = new HwndRenderTargetProperties();
40
41
42    #region Packing members BEGIN
43    SolidColorBrush containerBrush;
44    SolidColorBrush containerFillingBrush;
45    SolidColorBrush itemBrush;
46    SolidColorBrush itemFillingBrush;
47    SolidColorBrush labelBrush;
48    TextFormat textFormat;
49
50    CenteredContainer2D container;
51    #endregion
52
53    //Initialization-Methods
54
55    public PackingPlan2D() {
56      InitializeComponent();
57      this.SetStyle(
58        ControlStyles.ResizeRedraw, true);
59    }
60
61    private void InitializeRenderTarget() {
62      hwndProperties.Hwnd = this.Handle;
63      hwndProperties.PixelSize = new Size2(this.Width, this.Height);
64      hwndProperties.PresentOptions = PresentOptions.None;
65      textFormat = new TextFormat(dwFactory, "arial", 14);
66
67      wndRender = new WindowRenderTarget(factory, rndTargProperties, hwndProperties);
68      wndRender.AntialiasMode = AntialiasMode.Aliased;
69      containerBrush = new SolidColorBrush(wndRender, Colors.Black);
70      containerFillingBrush = new SolidColorBrush(wndRender, Colors.DarkGray);
71      itemBrush = new SolidColorBrush(wndRender, Colors.Black);
72      itemFillingBrush = new SolidColorBrush(wndRender, new Color4(Color3.White, 0.5f));
73      labelBrush = new SolidColorBrush(wndRender, Colors.Black);
74    }
75
76
77
78
79
80    //Packing-Methods
81    public void InitializeContainer(float width, float height) {
82      container = new CenteredContainer2D(new Vector2(Math.Max(this.Width - 12, 1), Math.Max(this.Height - 12, 1)), new Vector2(width, height));
83    }
84
85    public void AddItemToContainer(float width, float height, float x, float y, string label) {
86      container.AddItem(new Vector2(width, height), new Vector2(x, y), label);
87    }
88
89    //Event-Listeners
90
91    private void PackingPlan2D_Load(object sender, EventArgs e) {
92      InitializeRenderTarget();
93    }
94
95    private void PackingPlan2D_Paint(object sender, PaintEventArgs e) {
96      wndRender.BeginDraw();
97      var controlColor = Colors.White;
98      wndRender.Clear(new Colors(controlColor.R, controlColor.G, controlColor.B));
99
100      if (container != null) {
101        container.UpdateContainer(new Vector2(this.Width, this.Height));
102        wndRender.FillRectangle(container.GetContainerData(), containerFillingBrush);
103
104        foreach (var item in container.GetItemRectangles()) {
105          wndRender.DrawRectangle(item.rectangle, itemBrush, 2f);
106          wndRender.FillRectangle(item.rectangle, itemFillingBrush);
107          wndRender.DrawText(item.label, textFormat, item.rectangle, labelBrush);
108        }
109
110        wndRender.DrawRectangle(container.GetContainerData(), containerBrush, 2.0f);
111      }
112
113      wndRender.Flush();
114      wndRender.EndDraw();
115    }
116
117    private void PackingPlan2D_Resize(object sender, EventArgs e) {
118      if (wndRender != null)
119        wndRender.Resize(new Size2(this.Width, this.Height));
120    }
121
122  }
123}
Note: See TracBrowser for help on using the repository browser.