Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking.Views/3.3/Container3DView.xaml.cs @ 15306

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

#2817:

  • Drawing extreme points in the visualization (will add a checkbox to the view to enable/disable this)
  • Fixing some bugs:
    • Updating residual space of extreme points before generating new extreme points
    • Fixed calculation of residual space for new extreme points by calculating intersections
    • Fixed bug in UpdateResidualSpace regarding > and >=
File size: 19.6 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 double startAngleX;
55    private double startAngleY;
56    private int selectedItemKey;
57
58    private BinPacking<BinPacking3D.PackingPosition, PackingShape, PackingItem> packing;
59    public BinPacking<BinPacking3D.PackingPosition, PackingShape, PackingItem> Packing {
60      get { return packing; }
61      set {
62        if (packing != value) {
63          this.packing = value;
64          ClearSelection(); // also updates visualization
65        }
66      }
67    }
68
69    private Dictionary<int, DiffuseMaterial> materials;
70
71    public Container3DView() {
72      InitializeComponent();
73      camMain.Position = new Point3D(0.5, 3, 3); // for design time we use a different camera position
74      materials = new Dictionary<int, DiffuseMaterial>();
75      Clear();
76    }
77
78
79    protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) {
80      base.OnRenderSizeChanged(sizeInfo);
81      var s = Math.Min(sizeInfo.NewSize.Height, sizeInfo.NewSize.Width);
82      var mySize = new Size(s, s);
83      viewport3D1.RenderSize = mySize;
84    }
85
86    public void SelectItem(int itemKey) {
87      // selection of an item should make all other items semi-transparent
88      selectedItemKey = itemKey;
89      UpdateVisualization();
90    }
91    public void ClearSelection() {
92      // remove all transparency
93      selectedItemKey = -1;
94      UpdateVisualization();
95    }
96
97    private void UpdateVisualization() {
98      Clear();
99      if (packing == null) return; // nothing to display
100
101      var modelGroup = (Model3DGroup)MyModel.Content;
102      var hiddenMaterial = new DiffuseMaterial(new SolidColorBrush(hiddenColor));
103
104      if (selectedItemKey >= 0) {
105        var selectedItem = packing.Items.Single(x => selectedItemKey == x.Key);
106        var selectedPos = packing.Positions[selectedItem.Key];
107
108        var colorIdx = selectedItem.Value.Material;
109        while (colorIdx < 0) colorIdx += colors.Length;
110        colorIdx = colorIdx % colors.Length;
111        var color = colors[colorIdx];
112        var material = new DiffuseMaterial { Brush = new SolidColorBrush(color) };
113        materials[selectedItem.Value.Material] = material;
114
115        var selectedModel = new GeometryModel3D { Geometry = new MeshGeometry3D(), Material = material };
116        AddSolidCube((MeshGeometry3D)selectedModel.Geometry, selectedPos.X, selectedPos.Y, selectedPos.Z,
117          selectedPos.Rotated ? selectedItem.Value.Depth : selectedItem.Value.Width,
118          selectedItem.Value.Height,
119          selectedPos.Rotated ? selectedItem.Value.Width : selectedItem.Value.Depth);
120        modelGroup.Children.Add(selectedModel);
121
122        foreach (var item in packing.Items.Where(x => selectedItemKey != x.Key)) {
123          var position = packing.Positions[item.Key];
124
125          var w = position.Rotated ? item.Value.Depth : item.Value.Width;
126          var h = item.Value.Height;
127          var d = position.Rotated ? item.Value.Width : item.Value.Depth;
128
129          var model = new GeometryModel3D { Geometry = new MeshGeometry3D(), Material = hiddenMaterial };
130          AddWireframeCube((MeshGeometry3D)model.Geometry, position.X, position.Y, position.Z, w, h, d, 1);
131          modelGroup.Children.Add(model);
132        }
133      } else {
134        foreach (var item in packing.Items) {
135          var position = packing.Positions[item.Key];
136
137          var w = position.Rotated ? item.Value.Depth : item.Value.Width;
138          var h = item.Value.Height;
139          var d = position.Rotated ? item.Value.Width : item.Value.Depth;
140
141          var model = new GeometryModel3D { Geometry = new MeshGeometry3D() };
142          DiffuseMaterial material;
143          if (!materials.TryGetValue(item.Value.Material, out material)) {
144            var colorIdx = item.Value.Material;
145            while (colorIdx < 0) colorIdx += colors.Length;
146            colorIdx = colorIdx % colors.Length;
147            var color = colors[colorIdx];
148            material = new DiffuseMaterial { Brush = new SolidColorBrush(color) };
149            materials[item.Value.Material] = material;
150          }
151          var selectedModel = new GeometryModel3D { Geometry = new MeshGeometry3D(), Material = material };
152          AddSolidCube((MeshGeometry3D)selectedModel.Geometry, position.X, position.Y, position.Z, w, h, d);
153          modelGroup.Children.Add(selectedModel);
154        }
155      }
156
157      // draw extreme-points
158      foreach (var ep in packing.ExtremePoints) {
159        var epModel = new GeometryModel3D { Geometry = new MeshGeometry3D(), Material = new DiffuseMaterial() { Brush = new SolidColorBrush(Colors.Red) } };
160        AddSolidCube((MeshGeometry3D)epModel.Geometry, ep.X, ep.Y, ep.Z, 10, 10, 10);
161        modelGroup.Children.Add(epModel);
162      }
163
164      var container = packing.BinShape;
165      var containerModel = new GeometryModel3D(new MeshGeometry3D(), new DiffuseMaterial(new SolidColorBrush(containerColor)));
166      modelGroup.Children.Add(containerModel);
167      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);
168
169      var ratio = Math.Max(container.Width, Math.Max(container.Height, container.Depth));
170      scale.ScaleX = 1.0 / ratio;
171      scale.ScaleY = 1.0 / ratio;
172      scale.ScaleZ = 1.0 / ratio;
173
174      scaleZoom.CenterX = rotateX.CenterX = rotateY.CenterX = container.Width / (2.0 * ratio);
175      scaleZoom.CenterY = rotateX.CenterY = rotateY.CenterY = container.Height / (2.0 * ratio);
176      scaleZoom.CenterZ = rotateX.CenterZ = rotateY.CenterZ = container.Depth / (2.0 * ratio);
177
178      camMain.Position = new Point3D(
179        scaleZoom.CenterX,
180        3,
181        3);
182      camMain.LookDirection = new Vector3D(
183        0,
184        scaleZoom.CenterY - camMain.Position.Y,
185        scaleZoom.CenterZ - camMain.Position.Z);
186    }
187
188
189    private void Clear() {
190      ((Model3DGroup)MyModel.Content).Children.Clear();
191      materials.Clear();
192
193      mouseDown = false;
194      startAngleX = 0;
195      startAngleY = 0;
196    }
197
198    private void Container3DView_MouseMove(object sender, MouseEventArgs e) {
199      if (!mouseDown) return;
200      var pos = e.GetPosition((IInputElement)this);
201
202      ((AxisAngleRotation3D)rotateX.Rotation).Angle = startAngleX + (pos.X - startPos.X) / 4;
203      ((AxisAngleRotation3D)rotateY.Rotation).Angle = startAngleY + (pos.Y - startPos.Y) / 4;
204    }
205
206    private void Container3DView_MouseDown(object sender, MouseButtonEventArgs e) {
207      startAngleX = ((AxisAngleRotation3D)rotateX.Rotation).Angle;
208      startAngleY = ((AxisAngleRotation3D)rotateY.Rotation).Angle;
209      this.startPos = e.GetPosition((IInputElement)this);
210      this.mouseDown = true;
211    }
212
213    private void Container3DView_MouseUp(object sender, MouseButtonEventArgs e) {
214      mouseDown = false;
215    }
216
217    private void Container3DView_OnMouseWheel(object sender, MouseWheelEventArgs e) {
218      if (e.Delta > 0) {
219        scaleZoom.ScaleX *= 1.1;
220        scaleZoom.ScaleY *= 1.1;
221        scaleZoom.ScaleZ *= 1.1;
222      } else if (e.Delta < 0) {
223        scaleZoom.ScaleX /= 1.1;
224        scaleZoom.ScaleY /= 1.1;
225        scaleZoom.ScaleZ /= 1.1;
226      }
227    }
228
229    private void Container3DView_OnMouseEnter(object sender, MouseEventArgs e) {
230      Focus(); // for mouse wheel events
231    }
232
233    #region helper for cubes
234    /// <summary>
235    /// Creates a solid cube by adding the respective points and triangles.
236    /// </summary>
237    /// <param name="mesh">The mesh to which points and triangles are added.</param>
238    /// <param name="x">The leftmost point</param>
239    /// <param name="y">The frontmost point</param>
240    /// <param name="z">The lowest point</param>
241    /// <param name="width">The extension to the right</param>
242    /// <param name="height">The extension to the back</param>
243    /// <param name="depth">The extension to the top</param>
244    private void AddSolidCube(MeshGeometry3D mesh, int x, int y, int z, int width, int height, int depth) {
245      // ground
246      mesh.Positions.Add(new Point3D(x, y, z));
247      mesh.Positions.Add(new Point3D(x + width, y, z));
248      mesh.Positions.Add(new Point3D(x + width, y + height, z));
249      mesh.Positions.Add(new Point3D(x, y + height, z));
250      // top
251      mesh.Positions.Add(new Point3D(x, y, z + depth));
252      mesh.Positions.Add(new Point3D(x + width, y, z + depth));
253      mesh.Positions.Add(new Point3D(x + width, y + height, z + depth));
254      mesh.Positions.Add(new Point3D(x, y + height, z + depth));
255
256      // front
257      AddPlane(mesh, 0, 1, 5, 4);
258      // right side
259      AddPlane(mesh, 1, 2, 6, 5);
260      // back
261      AddPlane(mesh, 3, 7, 6, 2);
262      // left side
263      AddPlane(mesh, 0, 4, 7, 3);
264      // top
265      AddPlane(mesh, 4, 5, 6, 7);
266      // bottom
267      AddPlane(mesh, 0, 3, 2, 1);
268    }
269
270    /// <summary>
271    /// Creates a wireframe cube by adding the respective points and triangles.
272    /// </summary>
273    /// <param name="mesh">The mesh to which points and triangles are added.</param>
274    /// <param name="x">The leftmost point</param>
275    /// <param name="y">The frontmost point</param>
276    /// <param name="z">The lowest point</param>
277    /// <param name="width">The extension to the right</param>
278    /// <param name="height">The extension to the back</param>
279    /// <param name="depth">The extension to the top</param>
280    /// <param name="thickness">The thickness of the frame</param>
281    private void AddWireframeCube(MeshGeometry3D mesh, double x, double y, double z, double width, double height, double depth, double thickness = double.NaN) {
282      // default thickness of the wireframe is 5% of smallest dimension
283      if (double.IsNaN(thickness))
284        thickness = Math.Min(width, Math.Min(height, depth)) * 0.05;
285
286      // The cube contains of 8 corner, each corner has 4 points:
287      // 1. The corner point
288      // 2. A point on the edge to the right of the corner
289      // 3. A point on the edge atop or below the corner
290      // 4. A point on the edge to the left of the corner
291
292      // Point 0, Front Left Bottom
293      mesh.Positions.Add(new Point3D(x, y, z));
294      mesh.Positions.Add(new Point3D(x + thickness, y, z));
295      mesh.Positions.Add(new Point3D(x, y, z + thickness));
296      mesh.Positions.Add(new Point3D(x, y + thickness, z));
297
298      // Point 1, Front Right Bottom
299      mesh.Positions.Add(new Point3D(x + width, y, z));
300      mesh.Positions.Add(new Point3D(x + width, y + thickness, z));
301      mesh.Positions.Add(new Point3D(x + width, y, z + thickness));
302      mesh.Positions.Add(new Point3D(x + width - thickness, y, z));
303
304      // Point 2, Back Right Bottom
305      mesh.Positions.Add(new Point3D(x + width, y + height, z));
306      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z));
307      mesh.Positions.Add(new Point3D(x + width, y + height, z + thickness));
308      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z));
309
310      // Point 3, Back Left Bottom
311      mesh.Positions.Add(new Point3D(x, y + height, z));
312      mesh.Positions.Add(new Point3D(x, y + height - thickness, z));
313      mesh.Positions.Add(new Point3D(x, y + height, z + thickness));
314      mesh.Positions.Add(new Point3D(x + thickness, y + height, z));
315
316      // Point 4, Front Left Top
317      mesh.Positions.Add(new Point3D(x, y, z + depth));
318      mesh.Positions.Add(new Point3D(x + thickness, y, z + depth));
319      mesh.Positions.Add(new Point3D(x, y, z + depth - thickness));
320      mesh.Positions.Add(new Point3D(x, y + thickness, z + depth));
321
322      // Point 5, Front Right Top
323      mesh.Positions.Add(new Point3D(x + width, y, z + depth));
324      mesh.Positions.Add(new Point3D(x + width, y + thickness, z + depth));
325      mesh.Positions.Add(new Point3D(x + width, y, z + depth - thickness));
326      mesh.Positions.Add(new Point3D(x + width - thickness, y, z + depth));
327
328      // Point 6, Back Right Top
329      mesh.Positions.Add(new Point3D(x + width, y + height, z + depth));
330      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z + depth));
331      mesh.Positions.Add(new Point3D(x + width, y + height, z + depth - thickness));
332      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z + depth));
333
334      // Point 7, Back Left Top
335      mesh.Positions.Add(new Point3D(x, y + height, z + depth));
336      mesh.Positions.Add(new Point3D(x, y + height - thickness, z + depth));
337      mesh.Positions.Add(new Point3D(x, y + height, z + depth - thickness));
338      mesh.Positions.Add(new Point3D(x + thickness, y + height, z + depth));
339
340      // Point 0, non-edge
341      mesh.Positions.Add(new Point3D(x + thickness, y, z + thickness));
342      mesh.Positions.Add(new Point3D(x, y + thickness, z + thickness));
343      mesh.Positions.Add(new Point3D(x + thickness, y + thickness, z));
344
345      // Point 1, non-edge
346      mesh.Positions.Add(new Point3D(x + width, y + thickness, z + thickness));
347      mesh.Positions.Add(new Point3D(x + width - thickness, y, z + thickness));
348      mesh.Positions.Add(new Point3D(x + width - thickness, y + thickness, z));
349
350      // Point 2, non-edge
351      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z + thickness));
352      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z + thickness));
353      mesh.Positions.Add(new Point3D(x + width - thickness, y + height - thickness, z));
354
355      // Point 3, non-edge
356      mesh.Positions.Add(new Point3D(x, y + height - thickness, z + thickness));
357      mesh.Positions.Add(new Point3D(x + thickness, y + height, z + thickness));
358      mesh.Positions.Add(new Point3D(x + thickness, y + height - thickness, z));
359
360      // Point 4, non-edge
361      mesh.Positions.Add(new Point3D(x + thickness, y, z + depth - thickness));
362      mesh.Positions.Add(new Point3D(x, y + thickness, z + depth - thickness));
363      mesh.Positions.Add(new Point3D(x + thickness, y + thickness, z + depth));
364
365      // Point 5, non-edge
366      mesh.Positions.Add(new Point3D(x + width, y + thickness, z + depth - thickness));
367      mesh.Positions.Add(new Point3D(x + width - thickness, y, z + depth - thickness));
368      mesh.Positions.Add(new Point3D(x + width - thickness, y + thickness, z + depth));
369
370      // Point 6, non-edge
371      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z + depth - thickness));
372      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z + depth - thickness));
373      mesh.Positions.Add(new Point3D(x + width - thickness, y + height - thickness, z + depth));
374
375      // Point 7, non-edge
376      mesh.Positions.Add(new Point3D(x, y + height - thickness, z + depth - thickness));
377      mesh.Positions.Add(new Point3D(x + thickness, y + height, z + depth - thickness));
378      mesh.Positions.Add(new Point3D(x + thickness, y + height - thickness, z + depth));
379
380      // Draw the 24 corner plates
381      AddPlane(mesh, 0, 1, 32, 2);
382      AddPlane(mesh, 0, 2, 33, 3);
383      AddPlane(mesh, 0, 3, 34, 1);
384
385      AddPlane(mesh, 4, 6, 36, 7);
386      AddPlane(mesh, 4, 5, 35, 6);
387      AddPlane(mesh, 4, 7, 37, 5);
388
389      AddPlane(mesh, 8, 10, 39, 11);
390      AddPlane(mesh, 8, 9, 38, 10);
391      AddPlane(mesh, 8, 11, 40, 9);
392
393      AddPlane(mesh, 12, 13, 41, 14);
394      AddPlane(mesh, 12, 14, 42, 15);
395      AddPlane(mesh, 12, 15, 43, 13);
396
397      AddPlane(mesh, 16, 18, 44, 17);
398      AddPlane(mesh, 16, 19, 45, 18);
399      AddPlane(mesh, 16, 17, 46, 19);
400
401      AddPlane(mesh, 20, 23, 48, 22);
402      AddPlane(mesh, 20, 22, 47, 21);
403      AddPlane(mesh, 20, 21, 49, 23);
404
405      AddPlane(mesh, 24, 27, 51, 26);
406      AddPlane(mesh, 24, 26, 50, 25);
407      AddPlane(mesh, 24, 25, 52, 27);
408
409      AddPlane(mesh, 28, 31, 54, 30);
410      AddPlane(mesh, 28, 30, 53, 29);
411      AddPlane(mesh, 28, 29, 55, 31);
412
413      // Draw the connecting plates
414      // on the bottom
415      AddPlane(mesh, 1, 7, 36, 32);
416      AddPlane(mesh, 1, 34, 37, 7);
417
418      AddPlane(mesh, 5, 11, 39, 35);
419      AddPlane(mesh, 5, 37, 40, 11);
420
421      AddPlane(mesh, 9, 15, 42, 38);
422      AddPlane(mesh, 9, 40, 43, 15);
423
424      AddPlane(mesh, 13, 3, 33, 41);
425      AddPlane(mesh, 13, 43, 34, 3);
426
427      // between bottom and top
428      AddPlane(mesh, 2, 32, 44, 18);
429      AddPlane(mesh, 2, 18, 45, 33);
430
431      AddPlane(mesh, 6, 22, 48, 36);
432      AddPlane(mesh, 6, 35, 47, 22);
433
434      AddPlane(mesh, 10, 26, 51, 39);
435      AddPlane(mesh, 10, 38, 50, 26);
436
437      AddPlane(mesh, 14, 30, 54, 42);
438      AddPlane(mesh, 14, 41, 53, 30);
439
440      // on the top
441      AddPlane(mesh, 17, 44, 48, 23);
442      AddPlane(mesh, 17, 23, 49, 46);
443
444      AddPlane(mesh, 21, 47, 51, 27);
445      AddPlane(mesh, 21, 27, 52, 49);
446
447      AddPlane(mesh, 25, 50, 54, 31);
448      AddPlane(mesh, 25, 31, 55, 52);
449
450      AddPlane(mesh, 29, 19, 46, 55);
451      AddPlane(mesh, 29, 53, 45, 19);
452    }
453
454    /// <summary>
455    /// Adds a plane by two triangles. The indices of the points have to be given
456    /// in counter-clockwise sequence.
457    /// </summary>
458    /// <param name="mesh">The mesh to add the triangles to</param>
459    /// <param name="a">The index of the first point</param>
460    /// <param name="b">The index of the second point</param>
461    /// <param name="c">The index of the third point</param>
462    /// <param name="d">The index of the fourth point</param>
463    private void AddPlane(MeshGeometry3D mesh, int a, int b, int c, int d) {
464      // two triangles form a plane
465      mesh.TriangleIndices.Add(a);
466      mesh.TriangleIndices.Add(b);
467      mesh.TriangleIndices.Add(d);
468      mesh.TriangleIndices.Add(c);
469      mesh.TriangleIndices.Add(d);
470      mesh.TriangleIndices.Add(b);
471    }
472    #endregion
473
474  }
475}
Note: See TracBrowser for help on using the repository browser.