Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14709 was 14709, checked in by abeham, 6 years ago

#2739: fixed materials out of range index

File size: 14.0 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      foreach (var item in packing.Items) {
106        var position = packing.Positions[item.Key];
107
108        var w = position.Rotated ? item.Value.Depth : item.Value.Width;
109        var h = item.Value.Height;
110        var d = position.Rotated ? item.Value.Width : item.Value.Depth;
111
112        var model = new GeometryModel3D { Geometry = new MeshGeometry3D() };
113        DiffuseMaterial material;
114        if (selectedItemKey >= 0 && selectedItemKey != item.Key)
115          material = hiddenMaterial;
116        else {
117          if (!materials.TryGetValue(item.Value.Material, out material)) {
118            var colorIdx = item.Value.Material;
119            while (colorIdx < 0) colorIdx += colors.Length;
120            colorIdx = colorIdx % colors.Length;
121            var color = colors[colorIdx];
122            material = new DiffuseMaterial { Brush = new SolidColorBrush(color) };
123            materials[item.Value.Material] = material;
124          }
125        }
126        model.Material = material;
127        modelGroup.Children.Add(model);
128
129        AddSolidCube((MeshGeometry3D)model.Geometry, position.X, position.Y, position.Z, w, h, d);
130      }
131
132      var container = packing.BinShape;
133      var containerModel = new GeometryModel3D(new MeshGeometry3D(), new DiffuseMaterial(new SolidColorBrush(containerColor)));
134      modelGroup.Children.Add(containerModel);
135      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);
136
137      var ratio = Math.Max(container.Width, Math.Max(container.Height, container.Depth));
138      scale.ScaleX = 1.0 / ratio;
139      scale.ScaleY = 1.0 / ratio;
140      scale.ScaleZ = 1.0 / ratio;
141
142      scale.CenterX = .5;
143      scale.CenterY = .5;
144      scale.CenterZ = 0;
145    }
146
147
148    private void Clear() {
149      ((Model3DGroup)MyModel.Content).Children.Clear();
150      materials.Clear();
151     
152      mouseDown = false;
153      startAngleX = 0;
154      startAngleY = 0;
155    }
156
157    private void Container3DView_MouseMove(object sender, MouseEventArgs e) {
158      if (!mouseDown) return;
159      var pos = e.GetPosition((IInputElement)this);
160     
161      rotateX.Angle = startAngleX + (pos.X - startPos.X) / 4;
162      rotateY.Angle = startAngleY + (pos.Y - startPos.Y) / 4;
163    }
164
165    private void Container3DView_MouseDown(object sender, MouseButtonEventArgs e) {
166      startAngleX = rotateX.Angle;
167      startAngleY = rotateY.Angle;
168      this.startPos = e.GetPosition((IInputElement)this);
169      this.mouseDown = true;
170    }
171
172    private void Container3DView_MouseUp(object sender, MouseButtonEventArgs e) {
173      mouseDown = false;
174    }
175
176    private void Container3DView_OnMouseWheel(object sender, MouseWheelEventArgs e) {
177      if (e.Delta > 0) {
178        scaleZoom.ScaleX *= 1.1;
179        scaleZoom.ScaleY *= 1.1;
180        scaleZoom.ScaleZ *= 1.1;
181      } else if (e.Delta < 0) {
182        scaleZoom.ScaleX /= 1.1;
183        scaleZoom.ScaleY /= 1.1;
184        scaleZoom.ScaleZ /= 1.1;
185      }
186    }
187
188    private void Container3DView_OnMouseEnter(object sender, MouseEventArgs e) {
189      Focus(); // for mouse wheel events
190    }
191
192    private void Container3DView_OnKeyDown(object sender, KeyEventArgs e) {
193      ctrlDown = e.Key.HasFlag(Key.LeftCtrl) || e.Key.HasFlag(Key.RightCtrl);
194    }
195
196
197    #region helper for cubes
198    /// <summary>
199    /// Creates a solid cube by adding the respective points and triangles.
200    /// </summary>
201    /// <param name="mesh">The mesh to which points and triangles are added.</param>
202    /// <param name="x">The leftmost point</param>
203    /// <param name="y">The frontmost point</param>
204    /// <param name="z">The lowest point</param>
205    /// <param name="width">The extension to the right</param>
206    /// <param name="height">The extension to the back</param>
207    /// <param name="depth">The extension to the top</param>
208    private void AddSolidCube(MeshGeometry3D mesh, int x, int y, int z, int width, int height, int depth) {
209      // ground
210      mesh.Positions.Add(new Point3D(x, y, z));
211      mesh.Positions.Add(new Point3D(x + width, y, z));
212      mesh.Positions.Add(new Point3D(x + width, y + height, z));
213      mesh.Positions.Add(new Point3D(x, y + height, z));
214      // top
215      mesh.Positions.Add(new Point3D(x, y, z + depth));
216      mesh.Positions.Add(new Point3D(x + width, y, z + depth));
217      mesh.Positions.Add(new Point3D(x + width, y + height, z + depth));
218      mesh.Positions.Add(new Point3D(x, y + height, z + depth));
219
220      // front
221      AddPlane(mesh, 0, 1, 5, 4);
222      // right side
223      AddPlane(mesh, 1, 2, 6, 5);
224      // back
225      AddPlane(mesh, 3, 7, 6, 2);
226      // left side
227      AddPlane(mesh, 0, 4, 7, 3);
228      // top
229      AddPlane(mesh, 4, 5, 6, 7);
230      // bottom
231      AddPlane(mesh, 0, 3, 2, 1);
232    }
233
234    /// <summary>
235    /// Creates a wireframe 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    /// <param name="thickness">The thickness of the frame</param>
245    private void AddWireframeCube(MeshGeometry3D mesh, double x, double y, double z, double width, double height, double depth, double thickness = double.NaN) {
246      // default thickness of the wireframe is 5% of smallest dimension
247      if (double.IsNaN(thickness))
248        thickness = Math.Min(width, Math.Min(height, depth)) * 0.05;
249
250      // The cube contains of 8 corner, each corner has 4 points:
251      // 1. The corner point
252      // 2. A point on the edge to the right of the corner
253      // 3. A point on the edge atop or below the corner
254      // 4. A point on the edge to the left of the corner
255
256      // Point 0, Front Left Bottom
257      mesh.Positions.Add(new Point3D(x, y, z));
258      mesh.Positions.Add(new Point3D(x + thickness, y, z));
259      mesh.Positions.Add(new Point3D(x, y, z + thickness));
260      mesh.Positions.Add(new Point3D(x, y + thickness, z));
261
262      // Point 1, Front Right Bottom
263      mesh.Positions.Add(new Point3D(x + width, y, z));
264      mesh.Positions.Add(new Point3D(x + width, y + thickness, z));
265      mesh.Positions.Add(new Point3D(x + width, y, z + thickness));
266      mesh.Positions.Add(new Point3D(x + width - thickness, y, z));
267
268      // Point 2, Back Right Bottom
269      mesh.Positions.Add(new Point3D(x + width, y + height, z));
270      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z));
271      mesh.Positions.Add(new Point3D(x + width, y + height, z + thickness));
272      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z));
273
274      // Point 3, Back Left Bottom
275      mesh.Positions.Add(new Point3D(x, y + height, z));
276      mesh.Positions.Add(new Point3D(x, y + height - thickness, z));
277      mesh.Positions.Add(new Point3D(x, y + height, z + thickness));
278      mesh.Positions.Add(new Point3D(x + thickness, y + height, z));
279
280      // Point 4, Front Left Top
281      mesh.Positions.Add(new Point3D(x, y, z + depth));
282      mesh.Positions.Add(new Point3D(x + thickness, y, z + depth));
283      mesh.Positions.Add(new Point3D(x, y, z + depth - thickness));
284      mesh.Positions.Add(new Point3D(x, y + thickness, z + depth));
285
286      // Point 5, Front Right Top
287      mesh.Positions.Add(new Point3D(x + width, y, z + depth));
288      mesh.Positions.Add(new Point3D(x + width, y + thickness, z + depth));
289      mesh.Positions.Add(new Point3D(x + width, y, z + depth - thickness));
290      mesh.Positions.Add(new Point3D(x + width - thickness, y, z + depth));
291
292      // Point 6, Back Right Top
293      mesh.Positions.Add(new Point3D(x + width, y + height, z + depth));
294      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z + depth));
295      mesh.Positions.Add(new Point3D(x + width, y + height, z + depth - thickness));
296      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z + depth));
297
298      // Point 7, Back Left Top
299      mesh.Positions.Add(new Point3D(x, y + height, z + depth));
300      mesh.Positions.Add(new Point3D(x, y + height - thickness, z + depth));
301      mesh.Positions.Add(new Point3D(x, y + height, z + depth - thickness));
302      mesh.Positions.Add(new Point3D(x + thickness, y + height, z + depth));
303
304      AddPlane(mesh, 0, 4, 6, 2);
305      AddPlane(mesh, 0, 3, 5, 4);
306
307      AddPlane(mesh, 4, 8, 10, 6);
308      AddPlane(mesh, 4, 7, 9, 8);
309
310      AddPlane(mesh, 8, 12, 14, 10);
311      AddPlane(mesh, 8, 11, 13, 12);
312
313      AddPlane(mesh, 0, 2, 14, 12);
314      AddPlane(mesh, 0, 12, 15, 1);
315
316      AddPlane(mesh, 0, 1, 17, 16);
317      AddPlane(mesh, 0, 16, 19, 3);
318
319      AddPlane(mesh, 4, 20, 23, 7);
320      AddPlane(mesh, 4, 5, 21, 20);
321
322      AddPlane(mesh, 8, 24, 27, 11);
323      AddPlane(mesh, 8, 9, 25, 24);
324
325      AddPlane(mesh, 12, 28, 31, 15);
326      AddPlane(mesh, 12, 13, 29, 28);
327
328      AddPlane(mesh, 16, 18, 22, 20);
329      AddPlane(mesh, 16, 20, 21, 19);
330
331      AddPlane(mesh, 20, 22, 26, 24);
332      AddPlane(mesh, 20, 24, 25, 23);
333
334      AddPlane(mesh, 24, 28, 29, 27);
335      AddPlane(mesh, 24, 26, 30, 28);
336
337      AddPlane(mesh, 28, 30, 18, 16);
338      AddPlane(mesh, 28, 16, 17, 31);
339    }
340
341    /// <summary>
342    /// Adds a plane by two triangles. The indices of the points have to be given
343    /// in counter-clockwise sequence.
344    /// </summary>
345    /// <param name="mesh">The mesh to add the triangles to</param>
346    /// <param name="a">The index of the first point</param>
347    /// <param name="b">The index of the second point</param>
348    /// <param name="c">The index of the third point</param>
349    /// <param name="d">The index of the fourth point</param>
350    private void AddPlane(MeshGeometry3D mesh, int a, int b, int c, int d) {
351      // two triangles form a plane
352      mesh.TriangleIndices.Add(a);
353      mesh.TriangleIndices.Add(b);
354      mesh.TriangleIndices.Add(d);
355      mesh.TriangleIndices.Add(c);
356      mesh.TriangleIndices.Add(d);
357      mesh.TriangleIndices.Add(b);
358    }
359    #endregion
360
361  }
362}
Note: See TracBrowser for help on using the repository browser.