Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.Views/3.3/Shapes/Container3DView.xaml.cs @ 13554

Last change on this file since 13554 was 13532, checked in by gkronber, 9 years ago

#1966 work in progress WPF control to visualize packings

File size: 6.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Windows;
7using System.Windows.Controls;
8using System.Windows.Data;
9using System.Windows.Documents;
10using System.Windows.Input;
11using System.Windows.Media;
12using System.Windows.Media.Imaging;
13using System.Windows.Media.Media3D;
14using System.Windows.Navigation;
15using System.Windows.Shapes;
16using HeuristicLab.Encodings.PackingEncoding.PackingPlan;
17using HeuristicLab.Problems.BinPacking.Dimensions;
18using HeuristicLab.Problems.BinPacking.PackingBin;
19using HeuristicLab.Problems.BinPacking.PackingItem;
20
21namespace HeuristicLab.Problems.BinPacking.Views.Shapes {
22  public partial class Container3DView : UserControl {
23    private Point startPos;
24    private bool mouseDown = false;
25    private double startAngleX;
26    private double startAngleY;
27    private int selectedItemIndex;
28
29    public Container3DView() {
30      InitializeComponent();
31      Clear();
32    }
33
34    public void Clear() {
35      meshMain.Positions.Clear();
36      meshMain.TriangleIndices.Clear();
37
38      mouseDown = false;
39      startAngleX = 0;
40      startAngleY = 0;
41      selectedItemIndex = -1;
42
43      AddCube(0, 0, 0, 1, 1, 1); // add an empty cube (TODO: remove)
44    }
45
46
47    public void ShowContainer(BinPacking<ThreeDimensionalPacking, CuboidPackingBin, CuboidPackingItem> packing) {
48      var container = packing.BinMeasures;
49      // TODO: draw transparent container
50      // packingPlan3D.InitializeContainer(bin.Width, bin.Height, bin.Depth);
51
52      foreach (var item in packing.ItemMeasures) {
53        var position = packing.ItemPositions[item.Key];
54
55        // ignore the item.Key
56        // ignore the item.Material
57
58        AddCube(position.X, position.Y, position.Z,
59          position.Rotated ? item.Value.Depth : item.Value.Width,
60          item.Value.Height,
61          position.Rotated ? item.Value.Width : item.Value.Depth);
62      }
63    }
64
65    public void SelectItem(int itemIndex) {
66      // selection of an item should make all other items semi-transparent
67      selectedItemIndex = itemIndex;
68    }
69    public void ClearSelection() {
70      // remove all transparency
71      selectedItemIndex = -1;
72    }
73
74    private void viewport3D1_MouseWheel(object sender, MouseWheelEventArgs e) {
75      if (e.Delta > 0) {
76        camMain.Position -= 0.5 * camMain.LookDirection;
77      } else if (e.Delta < 0) {
78        camMain.Position += 0.5 * camMain.LookDirection;
79      }
80    }
81
82    private void viewport3D1_MouseMove(object sender, MouseEventArgs e) {
83      if (!mouseDown) return;
84      var pos = e.GetPosition((IInputElement)this);
85      rotateX.Angle = startAngleX + (pos.X - startPos.X) / 4;
86      rotateY.Angle = startAngleY + (pos.Y - startPos.Y) / 4;
87    }
88
89    private void viewport3D1_MouseDown(object sender, MouseButtonEventArgs e) {
90      startAngleX = rotateX.Angle;
91      startAngleY = rotateY.Angle;
92      this.startPos = e.GetPosition((IInputElement)this);
93      this.mouseDown = true;
94    }
95
96    private void viewport3D1_MouseUp(object sender, MouseButtonEventArgs e) {
97      mouseDown = false;
98    }
99
100
101    #region helper for cubes
102
103    private void AddCube(int x, int y, int z, int width, int height, int depth) {
104      AddTriangles(AddPoints(x, y, z, width, height, depth));
105    }
106
107    private void AddTriangles(int[] pointIdx) {
108      // back side
109      meshMain.TriangleIndices.Add(pointIdx[0]);
110      meshMain.TriangleIndices.Add(pointIdx[2]);
111      meshMain.TriangleIndices.Add(pointIdx[1]);
112
113      meshMain.TriangleIndices.Add(pointIdx[0]);
114      meshMain.TriangleIndices.Add(pointIdx[3]);
115      meshMain.TriangleIndices.Add(pointIdx[2]);
116
117      // right side
118      meshMain.TriangleIndices.Add(pointIdx[2]);
119      meshMain.TriangleIndices.Add(pointIdx[6]);
120      meshMain.TriangleIndices.Add(pointIdx[5]);
121
122      meshMain.TriangleIndices.Add(pointIdx[1]);
123      meshMain.TriangleIndices.Add(pointIdx[2]);
124      meshMain.TriangleIndices.Add(pointIdx[5]);
125
126      // left side
127      meshMain.TriangleIndices.Add(pointIdx[7]);
128      meshMain.TriangleIndices.Add(pointIdx[3]);
129      meshMain.TriangleIndices.Add(pointIdx[4]);
130
131      meshMain.TriangleIndices.Add(pointIdx[4]);
132      meshMain.TriangleIndices.Add(pointIdx[3]);
133      meshMain.TriangleIndices.Add(pointIdx[0]);
134
135      // front side
136      meshMain.TriangleIndices.Add(pointIdx[6]);
137      meshMain.TriangleIndices.Add(pointIdx[7]);
138      meshMain.TriangleIndices.Add(pointIdx[4]);
139
140      meshMain.TriangleIndices.Add(pointIdx[6]);
141      meshMain.TriangleIndices.Add(pointIdx[4]);
142      meshMain.TriangleIndices.Add(pointIdx[5]);
143
144      // top side
145      meshMain.TriangleIndices.Add(pointIdx[3]);
146      meshMain.TriangleIndices.Add(pointIdx[7]);
147      meshMain.TriangleIndices.Add(pointIdx[6]);
148
149      meshMain.TriangleIndices.Add(pointIdx[3]);
150      meshMain.TriangleIndices.Add(pointIdx[6]);
151      meshMain.TriangleIndices.Add(pointIdx[2]);
152
153      // bottom side
154      meshMain.TriangleIndices.Add(pointIdx[5]);
155      meshMain.TriangleIndices.Add(pointIdx[4]);
156      meshMain.TriangleIndices.Add(pointIdx[0]);
157
158      meshMain.TriangleIndices.Add(pointIdx[5]);
159      meshMain.TriangleIndices.Add(pointIdx[0]);
160      meshMain.TriangleIndices.Add(pointIdx[1]);
161
162
163    }
164
165    private int[] AddPoints(int x, int y, int z, int w, int h, int d) {
166      // ground
167      meshMain.Positions.Add(new Point3D(x, y, z));
168      meshMain.Positions.Add(new Point3D(x + w, y, z));
169      meshMain.Positions.Add(new Point3D(x + w, y + h, z));
170      meshMain.Positions.Add(new Point3D(x, y + h, z));
171      // top
172      meshMain.Positions.Add(new Point3D(x, y, z + d));
173      meshMain.Positions.Add(new Point3D(x + w, y, z + d));
174      meshMain.Positions.Add(new Point3D(x + w, y + h, z + d));
175      meshMain.Positions.Add(new Point3D(x, y + h, z + d));
176
177      return Enumerable.Range(meshMain.Positions.Count - 8, 8).ToArray();
178    }
179    #endregion
180  }
181}
Note: See TracBrowser for help on using the repository browser.