Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.BinPacking.Views/3.3/Container3DView.xaml.cs @ 14978

Last change on this file since 14978 was 14978, checked in by abeham, 7 years ago

#2739: Improved Bin Packing 3D View

  • Fixing an overlap of triangles in the corner points in the wireframe code
  • Removing unnecessary translate transforms and using centerXYZ of scaling and rotation
  • Correctly calculated the center of the bin after scaling
  • When an item is selected, instead of using solid cubes with semi-transparent surfaces, a wireframe model is displayed
    • The selected cube has to be drawn first, otherwise the other wireframe cubes are not transparent to it
File size: 19.3 KB
Line 
1/* HeuristicLab
2 * Copyright (C) 2002-2016 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
3 *
4 * This file is part of HeuristicLab.
5 *
6 * HeuristicLab is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * HeuristicLab is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with HeuristicLab. If not, see<http://www.gnu.org/licenses/> .
18 */
19
20using System;
21using System.Collections.Generic;
22using System.Linq;
23using System.Windows;
24using System.Windows.Controls;
25using System.Windows.Input;
26using System.Windows.Media;
27using System.Windows.Media.Media3D;
28using HeuristicLab.Problems.BinPacking3D;
29
30namespace HeuristicLab.Problems.BinPacking.Views {
31  public partial class Container3DView : UserControl {
32    private static readonly Color[] colors = new[] {
33      Color.FromRgb(0x40, 0x6A, 0xB7),
34      Color.FromRgb(0xB1, 0x6D, 0x01),
35      Color.FromRgb(0x4E, 0x8A, 0x06),
36      Color.FromRgb(0x75, 0x50, 0x7B),
37      Color.FromRgb(0x72, 0x9F, 0xCF),
38      Color.FromRgb(0xA4, 0x00, 0x00),
39      Color.FromRgb(0xAD, 0x7F, 0xA8),
40      Color.FromRgb(0x29, 0x50, 0xCF),
41      Color.FromRgb(0x90, 0xB0, 0x60),
42      Color.FromRgb(0xF5, 0x89, 0x30),
43      Color.FromRgb(0x55, 0x57, 0x53),
44      Color.FromRgb(0xEF, 0x59, 0x59),
45      Color.FromRgb(0xED, 0xD4, 0x30),
46      Color.FromRgb(0x63, 0xC2, 0x16),
47    };
48   
49    private static readonly Color hiddenColor = Color.FromArgb(0x1A, 0xAA, 0xAA, 0xAA);
50    private static readonly Color containerColor = Color.FromArgb(0x7F, 0xAA, 0xAA, 0xAA);
51
52    private Point startPos;
53    private bool mouseDown = false;
54    private bool ctrlDown = false;
55    private double startAngleX;
56    private double startAngleY;
57    private int selectedItemKey;
58
59    private BinPacking<BinPacking3D.PackingPosition, PackingShape, PackingItem> packing;
60    public BinPacking<BinPacking3D.PackingPosition, PackingShape, PackingItem> Packing {
61      get { return packing; }
62      set {
63        if (packing != value) {
64          this.packing = value;
65          ClearSelection(); // also updates visualization
66        }
67      }
68    }
69
70    private Dictionary<int, DiffuseMaterial> materials;
71
72    public Container3DView() {
73      InitializeComponent();
74      camMain.Position = new Point3D(0.5, 3, 3); // for design time we use a different camera position
75      materials = new Dictionary<int, DiffuseMaterial>();
76      Clear();
77    }
78
79
80    protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) {
81      base.OnRenderSizeChanged(sizeInfo);
82      var s = Math.Min(sizeInfo.NewSize.Height, sizeInfo.NewSize.Width);
83      var mySize = new Size(s, s);
84      viewport3D1.RenderSize = mySize;
85    }
86
87    public void SelectItem(int itemKey) {
88      // selection of an item should make all other items semi-transparent
89      selectedItemKey = itemKey;
90      UpdateVisualization();
91    }
92    public void ClearSelection() {
93      // remove all transparency
94      selectedItemKey = -1;
95      UpdateVisualization();
96    }
97
98    private void UpdateVisualization() {
99      Clear();
100      if (packing == null) return; // nothing to display
101
102      var modelGroup = (Model3DGroup)MyModel.Content;
103      var hiddenMaterial = new DiffuseMaterial(new SolidColorBrush(hiddenColor));
104
105      if (selectedItemKey >= 0) {
106        var selectedItem = packing.Items.Single(x => selectedItemKey == x.Key);
107        var selectedPos = packing.Positions[selectedItem.Key];
108        DiffuseMaterial material;
109        if (!materials.TryGetValue(selectedItem.Value.Material, out material)) {
110          var colorIdx = selectedItem.Value.Material;
111          while (colorIdx < 0) colorIdx += colors.Length;
112          colorIdx = colorIdx % colors.Length;
113          var color = colors[colorIdx];
114          material = new DiffuseMaterial { Brush = new SolidColorBrush(color) };
115          materials[selectedItem.Value.Material] = material;
116        }
117        var selectedModel = new GeometryModel3D { Geometry = new MeshGeometry3D(), Material = material };
118        AddSolidCube((MeshGeometry3D)selectedModel.Geometry, selectedPos.X, selectedPos.Y, selectedPos.Z,
119          selectedPos.Rotated ? selectedItem.Value.Depth : selectedItem.Value.Width,
120          selectedItem.Value.Height,
121          selectedPos.Rotated ? selectedItem.Value.Width : selectedItem.Value.Depth);
122        modelGroup.Children.Add(selectedModel);
123
124        foreach (var item in packing.Items.Where(x => selectedItemKey != x.Key)) {
125          var position = packing.Positions[item.Key];
126
127          var w = position.Rotated ? item.Value.Depth : item.Value.Width;
128          var h = item.Value.Height;
129          var d = position.Rotated ? item.Value.Width : item.Value.Depth;
130
131          var model = new GeometryModel3D { Geometry = new MeshGeometry3D(), Material = hiddenMaterial };
132          AddWireframeCube((MeshGeometry3D)model.Geometry, position.X, position.Y, position.Z, w, h, d, 1);
133          modelGroup.Children.Add(model);
134        }
135      } else {
136        foreach (var item in packing.Items) {
137          var position = packing.Positions[item.Key];
138
139          var w = position.Rotated ? item.Value.Depth : item.Value.Width;
140          var h = item.Value.Height;
141          var d = position.Rotated ? item.Value.Width : item.Value.Depth;
142
143          var model = new GeometryModel3D { Geometry = new MeshGeometry3D() };
144          DiffuseMaterial material;
145          if (!materials.TryGetValue(item.Value.Material, out material)) {
146            var colorIdx = item.Value.Material;
147            while (colorIdx < 0) colorIdx += colors.Length;
148            colorIdx = colorIdx % colors.Length;
149            var color = colors[colorIdx];
150            material = new DiffuseMaterial { Brush = new SolidColorBrush(color) };
151            materials[item.Value.Material] = material;
152          }
153          var selectedModel = new GeometryModel3D { Geometry = new MeshGeometry3D(), Material = material };
154          AddSolidCube((MeshGeometry3D)selectedModel.Geometry, position.X, position.Y, position.Z, w, h, d);
155          modelGroup.Children.Add(selectedModel);
156        }
157      }
158
159      var container = packing.BinShape;
160      var containerModel = new GeometryModel3D(new MeshGeometry3D(), new DiffuseMaterial(new SolidColorBrush(containerColor)));
161      modelGroup.Children.Add(containerModel);
162      AddWireframeCube((MeshGeometry3D)containerModel.Geometry, container.Origin.X - .5, container.Origin.Y - .5, container.Origin.Z - .5, container.Width + 1, container.Height + 1, container.Depth + 1);
163
164      var ratio = Math.Max(container.Width, Math.Max(container.Height, container.Depth));
165      scale.ScaleX = 1.0 / ratio;
166      scale.ScaleY = 1.0 / ratio;
167      scale.ScaleZ = 1.0 / ratio;
168
169      scaleZoom.CenterX = rotateX.CenterX = rotateY.CenterX = container.Width / (2.0 * ratio);
170      scaleZoom.CenterY = rotateX.CenterY = rotateY.CenterY = container.Height / (2.0 * ratio);
171      scaleZoom.CenterZ = rotateX.CenterZ = rotateY.CenterZ = container.Depth / (2.0 * ratio);
172    }
173
174
175    private void Clear() {
176      ((Model3DGroup)MyModel.Content).Children.Clear();
177      materials.Clear();
178     
179      mouseDown = false;
180      startAngleX = 0;
181      startAngleY = 0;
182    }
183
184    private void Container3DView_MouseMove(object sender, MouseEventArgs e) {
185      if (!mouseDown) return;
186      var pos = e.GetPosition((IInputElement)this);
187     
188      ((AxisAngleRotation3D)rotateX.Rotation).Angle = startAngleX + (pos.X - startPos.X) / 4;
189      ((AxisAngleRotation3D)rotateY.Rotation).Angle = startAngleY + (pos.Y - startPos.Y) / 4;
190    }
191
192    private void Container3DView_MouseDown(object sender, MouseButtonEventArgs e) {
193      startAngleX = ((AxisAngleRotation3D)rotateX.Rotation).Angle;
194      startAngleY = ((AxisAngleRotation3D)rotateY.Rotation).Angle;
195      this.startPos = e.GetPosition((IInputElement)this);
196      this.mouseDown = true;
197    }
198
199    private void Container3DView_MouseUp(object sender, MouseButtonEventArgs e) {
200      mouseDown = false;
201    }
202
203    private void Container3DView_OnMouseWheel(object sender, MouseWheelEventArgs e) {
204      if (e.Delta > 0) {
205        scaleZoom.ScaleX *= 1.1;
206        scaleZoom.ScaleY *= 1.1;
207        scaleZoom.ScaleZ *= 1.1;
208      } else if (e.Delta < 0) {
209        scaleZoom.ScaleX /= 1.1;
210        scaleZoom.ScaleY /= 1.1;
211        scaleZoom.ScaleZ /= 1.1;
212      }
213    }
214
215    private void Container3DView_OnMouseEnter(object sender, MouseEventArgs e) {
216      Focus(); // for mouse wheel events
217    }
218
219    private void Container3DView_OnKeyDown(object sender, KeyEventArgs e) {
220      ctrlDown = e.Key.HasFlag(Key.LeftCtrl) || e.Key.HasFlag(Key.RightCtrl);
221    }
222
223
224    #region helper for cubes
225    /// <summary>
226    /// Creates a solid cube by adding the respective points and triangles.
227    /// </summary>
228    /// <param name="mesh">The mesh to which points and triangles are added.</param>
229    /// <param name="x">The leftmost point</param>
230    /// <param name="y">The frontmost point</param>
231    /// <param name="z">The lowest point</param>
232    /// <param name="width">The extension to the right</param>
233    /// <param name="height">The extension to the back</param>
234    /// <param name="depth">The extension to the top</param>
235    private void AddSolidCube(MeshGeometry3D mesh, int x, int y, int z, int width, int height, int depth) {
236      // ground
237      mesh.Positions.Add(new Point3D(x, y, z));
238      mesh.Positions.Add(new Point3D(x + width, y, z));
239      mesh.Positions.Add(new Point3D(x + width, y + height, z));
240      mesh.Positions.Add(new Point3D(x, y + height, z));
241      // top
242      mesh.Positions.Add(new Point3D(x, y, z + depth));
243      mesh.Positions.Add(new Point3D(x + width, y, z + depth));
244      mesh.Positions.Add(new Point3D(x + width, y + height, z + depth));
245      mesh.Positions.Add(new Point3D(x, y + height, z + depth));
246
247      // front
248      AddPlane(mesh, 0, 1, 5, 4);
249      // right side
250      AddPlane(mesh, 1, 2, 6, 5);
251      // back
252      AddPlane(mesh, 3, 7, 6, 2);
253      // left side
254      AddPlane(mesh, 0, 4, 7, 3);
255      // top
256      AddPlane(mesh, 4, 5, 6, 7);
257      // bottom
258      AddPlane(mesh, 0, 3, 2, 1);
259    }
260
261    /// <summary>
262    /// Creates a wireframe cube by adding the respective points and triangles.
263    /// </summary>
264    /// <param name="mesh">The mesh to which points and triangles are added.</param>
265    /// <param name="x">The leftmost point</param>
266    /// <param name="y">The frontmost point</param>
267    /// <param name="z">The lowest point</param>
268    /// <param name="width">The extension to the right</param>
269    /// <param name="height">The extension to the back</param>
270    /// <param name="depth">The extension to the top</param>
271    /// <param name="thickness">The thickness of the frame</param>
272    private void AddWireframeCube(MeshGeometry3D mesh, double x, double y, double z, double width, double height, double depth, double thickness = double.NaN) {
273      // default thickness of the wireframe is 5% of smallest dimension
274      if (double.IsNaN(thickness))
275        thickness = Math.Min(width, Math.Min(height, depth)) * 0.05;
276
277      // The cube contains of 8 corner, each corner has 4 points:
278      // 1. The corner point
279      // 2. A point on the edge to the right of the corner
280      // 3. A point on the edge atop or below the corner
281      // 4. A point on the edge to the left of the corner
282
283      // Point 0, Front Left Bottom
284      mesh.Positions.Add(new Point3D(x, y, z));
285      mesh.Positions.Add(new Point3D(x + thickness, y, z));
286      mesh.Positions.Add(new Point3D(x, y, z + thickness));
287      mesh.Positions.Add(new Point3D(x, y + thickness, z));
288
289      // Point 1, Front Right Bottom
290      mesh.Positions.Add(new Point3D(x + width, y, z));
291      mesh.Positions.Add(new Point3D(x + width, y + thickness, z));
292      mesh.Positions.Add(new Point3D(x + width, y, z + thickness));
293      mesh.Positions.Add(new Point3D(x + width - thickness, y, z));
294
295      // Point 2, Back Right Bottom
296      mesh.Positions.Add(new Point3D(x + width, y + height, z));
297      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z));
298      mesh.Positions.Add(new Point3D(x + width, y + height, z + thickness));
299      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z));
300
301      // Point 3, Back Left Bottom
302      mesh.Positions.Add(new Point3D(x, y + height, z));
303      mesh.Positions.Add(new Point3D(x, y + height - thickness, z));
304      mesh.Positions.Add(new Point3D(x, y + height, z + thickness));
305      mesh.Positions.Add(new Point3D(x + thickness, y + height, z));
306
307      // Point 4, Front Left Top
308      mesh.Positions.Add(new Point3D(x, y, z + depth));
309      mesh.Positions.Add(new Point3D(x + thickness, y, z + depth));
310      mesh.Positions.Add(new Point3D(x, y, z + depth - thickness));
311      mesh.Positions.Add(new Point3D(x, y + thickness, z + depth));
312
313      // Point 5, Front Right Top
314      mesh.Positions.Add(new Point3D(x + width, y, z + depth));
315      mesh.Positions.Add(new Point3D(x + width, y + thickness, z + depth));
316      mesh.Positions.Add(new Point3D(x + width, y, z + depth - thickness));
317      mesh.Positions.Add(new Point3D(x + width - thickness, y, z + depth));
318
319      // Point 6, Back Right Top
320      mesh.Positions.Add(new Point3D(x + width, y + height, z + depth));
321      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z + depth));
322      mesh.Positions.Add(new Point3D(x + width, y + height, z + depth - thickness));
323      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z + depth));
324
325      // Point 7, Back Left Top
326      mesh.Positions.Add(new Point3D(x, y + height, z + depth));
327      mesh.Positions.Add(new Point3D(x, y + height - thickness, z + depth));
328      mesh.Positions.Add(new Point3D(x, y + height, z + depth - thickness));
329      mesh.Positions.Add(new Point3D(x + thickness, y + height, z + depth));
330
331      // Point 0, non-edge
332      mesh.Positions.Add(new Point3D(x + thickness, y, z + thickness));
333      mesh.Positions.Add(new Point3D(x, y + thickness, z + thickness));
334      mesh.Positions.Add(new Point3D(x + thickness, y + thickness, z));
335
336      // Point 1, non-edge
337      mesh.Positions.Add(new Point3D(x + width, y + thickness, z + thickness));
338      mesh.Positions.Add(new Point3D(x + width - thickness, y, z + thickness));
339      mesh.Positions.Add(new Point3D(x + width - thickness, y + thickness, z));
340
341      // Point 2, non-edge
342      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z + thickness));
343      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z + thickness));
344      mesh.Positions.Add(new Point3D(x + width - thickness, y + height - thickness, z));
345
346      // Point 3, non-edge
347      mesh.Positions.Add(new Point3D(x, y + height - thickness, z + thickness));
348      mesh.Positions.Add(new Point3D(x + thickness, y + height, z + thickness));
349      mesh.Positions.Add(new Point3D(x + thickness, y + height - thickness, z));
350
351      // Point 4, non-edge
352      mesh.Positions.Add(new Point3D(x + thickness, y, z + depth - thickness));
353      mesh.Positions.Add(new Point3D(x, y + thickness, z + depth - thickness));
354      mesh.Positions.Add(new Point3D(x + thickness, y + thickness, z + depth));
355
356      // Point 5, non-edge
357      mesh.Positions.Add(new Point3D(x + width, y + thickness, z + depth - thickness));
358      mesh.Positions.Add(new Point3D(x + width - thickness, y, z + depth - thickness));
359      mesh.Positions.Add(new Point3D(x + width - thickness, y + thickness, z + depth));
360
361      // Point 6, non-edge
362      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z + depth - thickness));
363      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z + depth - thickness));
364      mesh.Positions.Add(new Point3D(x + width - thickness, y + height - thickness, z + depth));
365
366      // Point 7, non-edge
367      mesh.Positions.Add(new Point3D(x, y + height - thickness, z + depth - thickness));
368      mesh.Positions.Add(new Point3D(x + thickness, y + height, z + depth - thickness));
369      mesh.Positions.Add(new Point3D(x + thickness, y + height - thickness, z + depth));
370
371      // Draw the 24 corner plates
372      AddPlane(mesh, 0, 1, 32, 2);
373      AddPlane(mesh, 0, 2, 33, 3);
374      AddPlane(mesh, 0, 3, 34, 1);
375
376      AddPlane(mesh, 4, 6, 36, 7);
377      AddPlane(mesh, 4, 5, 35, 6);
378      AddPlane(mesh, 4, 7, 37, 5);
379
380      AddPlane(mesh, 8, 10, 39, 11);
381      AddPlane(mesh, 8, 9, 38, 10);
382      AddPlane(mesh, 8, 11, 40, 9);
383
384      AddPlane(mesh, 12, 13, 41, 14);
385      AddPlane(mesh, 12, 14, 42, 15);
386      AddPlane(mesh, 12, 15, 43, 13);
387
388      AddPlane(mesh, 16, 18, 44, 17);
389      AddPlane(mesh, 16, 19, 45, 18);
390      AddPlane(mesh, 16, 17, 46, 19);
391
392      AddPlane(mesh, 20, 23, 48, 22);
393      AddPlane(mesh, 20, 22, 47, 21);
394      AddPlane(mesh, 20, 21, 49, 23);
395
396      AddPlane(mesh, 24, 27, 51, 26);
397      AddPlane(mesh, 24, 26, 50, 25);
398      AddPlane(mesh, 24, 25, 52, 27);
399
400      AddPlane(mesh, 28, 31, 54, 30);
401      AddPlane(mesh, 28, 30, 53, 29);
402      AddPlane(mesh, 28, 29, 55, 31);
403
404      // Draw the connecting plates
405      // on the bottom
406      AddPlane(mesh, 1, 7, 36, 32);
407      AddPlane(mesh, 1, 34, 37, 7);
408
409      AddPlane(mesh, 5, 11, 39, 35);
410      AddPlane(mesh, 5, 37, 40, 11);
411
412      AddPlane(mesh, 9, 15, 42, 38);
413      AddPlane(mesh, 9, 40, 43, 15);
414
415      AddPlane(mesh, 13, 3, 33, 41);
416      AddPlane(mesh, 13, 43, 34, 3);
417
418      // between bottom and top
419      AddPlane(mesh, 2, 32, 44, 18);
420      AddPlane(mesh, 2, 18, 45, 33);
421
422      AddPlane(mesh, 6, 22, 48, 36);
423      AddPlane(mesh, 6, 35, 47, 22);
424
425      AddPlane(mesh, 10, 26, 51, 39);
426      AddPlane(mesh, 10, 38, 50, 26);
427
428      AddPlane(mesh, 14, 30, 54, 42);
429      AddPlane(mesh, 14, 41, 53, 30);
430
431      // on the top
432      AddPlane(mesh, 17, 44, 48, 23);
433      AddPlane(mesh, 17, 23, 49, 46);
434
435      AddPlane(mesh, 21, 47, 51, 27);
436      AddPlane(mesh, 21, 27, 52, 49);
437
438      AddPlane(mesh, 25, 50, 54, 31);
439      AddPlane(mesh, 25, 31, 55, 52);
440
441      AddPlane(mesh, 29, 19, 46, 55);
442      AddPlane(mesh, 29, 53, 45, 19);
443    }
444
445    /// <summary>
446    /// Adds a plane by two triangles. The indices of the points have to be given
447    /// in counter-clockwise sequence.
448    /// </summary>
449    /// <param name="mesh">The mesh to add the triangles to</param>
450    /// <param name="a">The index of the first point</param>
451    /// <param name="b">The index of the second point</param>
452    /// <param name="c">The index of the third point</param>
453    /// <param name="d">The index of the fourth point</param>
454    private void AddPlane(MeshGeometry3D mesh, int a, int b, int c, int d) {
455      // two triangles form a plane
456      mesh.TriangleIndices.Add(a);
457      mesh.TriangleIndices.Add(b);
458      mesh.TriangleIndices.Add(d);
459      mesh.TriangleIndices.Add(c);
460      mesh.TriangleIndices.Add(d);
461      mesh.TriangleIndices.Add(b);
462    }
463    #endregion
464
465  }
466}
Note: See TracBrowser for help on using the repository browser.