Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2817: updated to trunk r15680

File size: 23.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 System.Collections.Generic;
24using System.Linq;
25using System.Windows;
26using System.Windows.Controls;
27using System.Windows.Input;
28using System.Windows.Media;
29using System.Windows.Media.Media3D;
30using HeuristicLab.Problems.BinPacking3D;
31using HeuristicLab.Collections;
32
33namespace HeuristicLab.Problems.BinPacking.Views {
34  public partial class Container3DView : UserControl {
35    private static readonly Color[] colors = new[] {
36      Color.FromRgb(0x40, 0x6A, 0xB7),
37      Color.FromRgb(0xB1, 0x6D, 0x01),
38      Color.FromRgb(0x4E, 0x8A, 0x06),
39      Color.FromRgb(0x75, 0x50, 0x7B),
40      Color.FromRgb(0x72, 0x9F, 0xCF),
41      Color.FromRgb(0xA4, 0x00, 0x00),
42      Color.FromRgb(0xAD, 0x7F, 0xA8),
43      Color.FromRgb(0x29, 0x50, 0xCF),
44      Color.FromRgb(0x90, 0xB0, 0x60),
45      Color.FromRgb(0xF5, 0x89, 0x30),
46      Color.FromRgb(0x55, 0x57, 0x53),
47      Color.FromRgb(0xEF, 0x59, 0x59),
48      Color.FromRgb(0xED, 0xD4, 0x30),
49      Color.FromRgb(0x63, 0xC2, 0x16),
50    };
51
52    private static readonly Color hiddenColor = Color.FromArgb(0x1A, 0xAA, 0xAA, 0xAA);
53    private static readonly Color containerColor = Color.FromArgb(0x7F, 0xAA, 0xAA, 0xAA);
54    private static readonly Color residualSpaceColor = Color.FromArgb(0x7F, 0xAA, 0xAA, 0x00);
55
56    private Point startPos;
57    private bool mouseDown = false;
58    private double startAngleX;
59    private double startAngleY;
60    private int selectedItemKey;
61    private int selectedExtremePointIndex;
62    private bool showExtremePoints;
63    private bool showResidualSpaces;
64
65    private BinPacking<BinPacking3D.PackingPosition, PackingShape, PackingItem> packing;
66    public BinPacking<BinPacking3D.PackingPosition, PackingShape, PackingItem> Packing {
67      get { return packing; }
68      set {
69        if (packing != value) {
70          this.packing = value;
71          ClearSelection(); // also updates visualization
72        }
73      }
74    }
75
76    private Dictionary<int, DiffuseMaterial> materials;
77
78    public ObservableDictionary<BinPacking3D.PackingPosition, IList<ResidualSpace>> ResidualSpaces { get; set; }
79    public ObservableCollection<BinPacking3D.PackingPosition> ExtremePoints { get; set; }
80
81    public Container3DView() {
82      InitializeComponent();
83      camMain.Position = new Point3D(0.5, 3, 3); // for design time we use a different camera position
84      materials = new Dictionary<int, DiffuseMaterial>();
85      ResidualSpaces = new ObservableDictionary<BinPacking3D.PackingPosition, IList<ResidualSpace>>();
86      ExtremePoints = new ObservableCollection<BinPacking3D.PackingPosition>();
87      selectedExtremePointIndex = -1;
88      Clear();
89    }
90
91
92    protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) {
93      base.OnRenderSizeChanged(sizeInfo);
94      var s = Math.Min(sizeInfo.NewSize.Height, sizeInfo.NewSize.Width);
95      var mySize = new Size(s, s);
96      viewport3D1.RenderSize = mySize;
97    }
98
99    /// <summary>
100    /// Selects another extreme point
101    /// </summary>
102    /// <param name="index"></param>
103    public void SelectExtremePoint(int index) {
104      selectedExtremePointIndex = index;
105      UpdateVisualization();
106    }
107
108    public void SelectItem(int itemKey) {
109      // selection of an item should make all other items semi-transparent
110      selectedItemKey = itemKey;
111      UpdateVisualization();
112    }
113
114    public void ClearSelection() {
115      // remove all transparency
116      selectedItemKey = -1;
117      selectedExtremePointIndex = -1;
118      UpdateVisualization();
119    }
120
121    private void DisplaySelectedItemText() {
122      string text = "";
123      if (selectedItemKey < 0) {
124        text = $"{packing.BinShape.ToString()}";
125      } else {
126        var packingItem = packing.Items.Single(x => selectedItemKey == x.Key);
127        var position = packing.Positions.Single(x => x.Key == packingItem.Key);
128        text = $"{packingItem.Value.ToString()}, {position.ToString()}";
129        if (packing is BinPacking3D.BinPacking3D) {
130          var stackedWeight = ((BinPacking3D.BinPacking3D)packing).GetStackedWeightForItemId(packingItem.Key);
131          text += $", stacked weight: {stackedWeight}";
132        }
133      }
134      selectedItemTextBlock.Text = text;
135    }
136
137    private void UpdateVisualization() {
138      Clear();
139      if (packing == null)
140        return; // nothing to display
141
142      var modelGroup = (Model3DGroup)MyModel.Content;
143      var hiddenMaterial = new DiffuseMaterial(new SolidColorBrush(hiddenColor));
144
145      DisplaySelectedItemText();
146
147      if (selectedItemKey >= 0) {
148        var selectedItem = packing.Items.Single(x => selectedItemKey == x.Key);
149        var selectedPos = packing.Positions[selectedItem.Key];
150
151        var colorIdx = selectedItem.Value.Layer;
152        while (colorIdx < 0)
153          colorIdx += colors.Length;
154        colorIdx = colorIdx % colors.Length;
155        var color = colors[colorIdx];
156        var material = new DiffuseMaterial { Brush = new SolidColorBrush(color) };
157        materials[selectedItem.Value.Layer] = material;
158
159        var selectedModel = new GeometryModel3D { Geometry = new MeshGeometry3D(), Material = material };
160        AddSolidCube((MeshGeometry3D)selectedModel.Geometry, selectedPos.X, selectedPos.Y, selectedPos.Z,
161          selectedItem.Value.WidthInView,
162          selectedItem.Value.HeightInView,
163          selectedItem.Value.DepthInView);
164        modelGroup.Children.Add(selectedModel);
165
166        foreach (var item in packing.Items.Where(x => selectedItemKey != x.Key)) {
167          var position = packing.Positions[item.Key];
168
169          var w = item.Value.WidthInView;
170          var h = item.Value.HeightInView;
171          var d = item.Value.DepthInView;
172
173          var model = new GeometryModel3D { Geometry = new MeshGeometry3D(), Material = hiddenMaterial };
174          AddWireframeCube((MeshGeometry3D)model.Geometry, position.X, position.Y, position.Z, w, h, d, 1);
175          modelGroup.Children.Add(model);
176        }
177
178
179      } else {
180        foreach (var item in packing.Items) {
181          var position = packing.Positions[item.Key];
182
183          var w = item.Value.WidthInView;
184          var h = item.Value.HeightInView;
185          var d = item.Value.DepthInView;
186
187          var model = new GeometryModel3D { Geometry = new MeshGeometry3D() };
188          DiffuseMaterial material;
189          if (!materials.TryGetValue(item.Value.Layer, out material)) {
190            var colorIdx = item.Value.Layer;
191            while (colorIdx < 0)
192              colorIdx += colors.Length;
193            colorIdx = colorIdx % colors.Length;
194            var color = colors[colorIdx];
195            material = new DiffuseMaterial { Brush = new SolidColorBrush(color) };
196            materials[item.Value.Layer] = material;
197          }
198          var selectedModel = new GeometryModel3D { Geometry = new MeshGeometry3D(), Material = material };
199          AddSolidCube((MeshGeometry3D)selectedModel.Geometry, position.X, position.Y, position.Z, w, h, d);
200          modelGroup.Children.Add(selectedModel);
201        }
202      }
203
204
205      AddExtremePoints(modelGroup);
206
207      var container = packing.BinShape;
208      var containerModel = new GeometryModel3D(new MeshGeometry3D(), new DiffuseMaterial(new SolidColorBrush(containerColor)));
209      modelGroup.Children.Add(containerModel);
210      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);
211
212      var ratio = Math.Max(container.Width, Math.Max(container.Height, container.Depth));
213      scale.ScaleX = 1.0 / ratio;
214      scale.ScaleY = 1.0 / ratio;
215      scale.ScaleZ = 1.0 / ratio;
216
217      scaleZoom.CenterX = rotateX.CenterX = rotateY.CenterX = container.Width / (2.0 * ratio);
218      scaleZoom.CenterY = rotateX.CenterY = rotateY.CenterY = container.Height / (2.0 * ratio);
219      scaleZoom.CenterZ = rotateX.CenterZ = rotateY.CenterZ = container.Depth / (2.0 * ratio);
220
221      camMain.Position = new Point3D(
222        scaleZoom.CenterX,
223        3,
224        3);
225      camMain.LookDirection = new Vector3D(
226        0,
227        scaleZoom.CenterY - camMain.Position.Y,
228        scaleZoom.CenterZ - camMain.Position.Z);
229    }
230
231    private void AddExtremePoints(Model3DGroup modelGroup) {
232      if (showExtremePoints) {
233        // draw extreme-points
234        var maxMag = (int)Math.Log10(Math.Max(packing.BinShape.Depth, Math.Max(packing.BinShape.Height, packing.BinShape.Width)));
235        var cubeSize = (int)Math.Max(Math.Pow(10, maxMag - 2), 1);
236        BinPacking3D.PackingPosition selectedExtremePoint = null;
237        if (selectedExtremePointIndex < 0) {
238          foreach (var ep in ExtremePoints) {
239            AddResidualSpacesForExtremePoint(modelGroup, ep);
240          }
241        } else {
242          selectedExtremePoint = ExtremePoints.ToList()[selectedExtremePointIndex];
243          AddResidualSpacesForExtremePoint(modelGroup, selectedExtremePoint);
244        }
245
246        foreach (var ep in ExtremePoints) {
247          Color color;
248          if (ep.Equals(selectedExtremePoint)) {
249            color = Colors.Yellow;
250          } else {
251            color = Colors.Red;
252          }
253          var epModel = new GeometryModel3D { Geometry = new MeshGeometry3D(), Material = new DiffuseMaterial() { Brush = new SolidColorBrush(color) } };
254          AddSolidCube((MeshGeometry3D)epModel.Geometry, ep.X, ep.Y, ep.Z, cubeSize, cubeSize, cubeSize);
255          modelGroup.Children.Add(epModel);
256        }
257      }
258    }
259
260    private void AddResidualSpacesForExtremePoint(Model3DGroup modelGroup, BinPacking3D.PackingPosition extremePoint) {
261      if (showResidualSpaces) {
262        foreach (var rs in ResidualSpaces[extremePoint]) {
263          var containerModel1 = new GeometryModel3D(new MeshGeometry3D(), new DiffuseMaterial(new SolidColorBrush(residualSpaceColor)));
264
265          modelGroup.Children.Add(containerModel1);
266
267          AddWireframeCube((MeshGeometry3D)containerModel1.Geometry,
268            extremePoint.X,
269            extremePoint.Y,
270            extremePoint.Z,
271            rs.Width,
272            rs.Height,
273            rs.Depth);
274        }
275      }
276       
277    }
278
279
280    private void Clear() {
281      ((Model3DGroup)MyModel.Content).Children.Clear();
282      materials.Clear();
283
284      mouseDown = false;
285      startAngleX = 0;
286      startAngleY = 0;
287    }
288
289    private void Container3DView_MouseMove(object sender, MouseEventArgs e) {
290      if (!mouseDown)
291        return;
292      var pos = e.GetPosition((IInputElement)this);
293
294      ((AxisAngleRotation3D)rotateX.Rotation).Angle = startAngleX + (pos.X - startPos.X) / 4;
295      ((AxisAngleRotation3D)rotateY.Rotation).Angle = startAngleY + (pos.Y - startPos.Y) / 4;
296    }
297
298    private void Container3DView_MouseDown(object sender, MouseButtonEventArgs e) {
299      startAngleX = ((AxisAngleRotation3D)rotateX.Rotation).Angle;
300      startAngleY = ((AxisAngleRotation3D)rotateY.Rotation).Angle;
301      this.startPos = e.GetPosition((IInputElement)this);
302      this.mouseDown = true;
303    }
304
305    private void Container3DView_MouseUp(object sender, MouseButtonEventArgs e) {
306      mouseDown = false;
307    }
308
309    private void Container3DView_OnMouseWheel(object sender, MouseWheelEventArgs e) {
310      if (e.Delta > 0) {
311        scaleZoom.ScaleX *= 1.1;
312        scaleZoom.ScaleY *= 1.1;
313        scaleZoom.ScaleZ *= 1.1;
314      } else if (e.Delta < 0) {
315        scaleZoom.ScaleX /= 1.1;
316        scaleZoom.ScaleY /= 1.1;
317        scaleZoom.ScaleZ /= 1.1;
318      }
319    }
320
321    private void Container3DView_OnMouseEnter(object sender, MouseEventArgs e) {
322      Focus(); // for mouse wheel events
323    }
324    private void ShowExtremePointsCheckBoxOnChecked(object sender, RoutedEventArgs e) {
325      showExtremePoints = true;
326      UpdateVisualization();
327    }
328    private void ShowExtremePointsCheckBoxOnUnchecked(object sender, RoutedEventArgs e) {
329      showExtremePoints = false;
330      UpdateVisualization();
331    }
332
333    private void ShowResidualSpacesCheckBoxOnChecked(object sender, RoutedEventArgs e) {
334      showResidualSpaces = true;
335      UpdateVisualization();
336    }
337    private void ShowResidualSpacesCheckBoxOnUnchecked(object sender, RoutedEventArgs e) {
338      showResidualSpaces = false;
339      UpdateVisualization();
340    }
341
342    #region helper for cubes
343    /// <summary>
344    /// Creates a solid cube by adding the respective points and triangles.
345    /// </summary>
346    /// <param name="mesh">The mesh to which points and triangles are added.</param>
347    /// <param name="x">The leftmost point</param>
348    /// <param name="y">The frontmost point</param>
349    /// <param name="z">The lowest point</param>
350    /// <param name="width">The extension to the right</param>
351    /// <param name="height">The extension to the back</param>
352    /// <param name="depth">The extension to the top</param>
353    private void AddSolidCube(MeshGeometry3D mesh, int x, int y, int z, int width, int height, int depth) {
354      // ground
355      mesh.Positions.Add(new Point3D(x, y, z));
356      mesh.Positions.Add(new Point3D(x + width, y, z));
357      mesh.Positions.Add(new Point3D(x + width, y + height, z));
358      mesh.Positions.Add(new Point3D(x, y + height, z));
359      // top
360      mesh.Positions.Add(new Point3D(x, y, z + depth));
361      mesh.Positions.Add(new Point3D(x + width, y, z + depth));
362      mesh.Positions.Add(new Point3D(x + width, y + height, z + depth));
363      mesh.Positions.Add(new Point3D(x, y + height, z + depth));
364
365      // front
366      AddPlane(mesh, 0, 1, 5, 4);
367      // right side
368      AddPlane(mesh, 1, 2, 6, 5);
369      // back
370      AddPlane(mesh, 3, 7, 6, 2);
371      // left side
372      AddPlane(mesh, 0, 4, 7, 3);
373      // top
374      AddPlane(mesh, 4, 5, 6, 7);
375      // bottom
376      AddPlane(mesh, 0, 3, 2, 1);
377    }
378
379    /// <summary>
380    /// Creates a wireframe cube by adding the respective points and triangles.
381    /// </summary>
382    /// <param name="mesh">The mesh to which points and triangles are added.</param>
383    /// <param name="x">The leftmost point</param>
384    /// <param name="y">The frontmost point</param>
385    /// <param name="z">The lowest point</param>
386    /// <param name="width">The extension to the right</param>
387    /// <param name="height">The extension to the back</param>
388    /// <param name="depth">The extension to the top</param>
389    /// <param name="thickness">The thickness of the frame</param>
390    private void AddWireframeCube(MeshGeometry3D mesh, double x, double y, double z, double width, double height, double depth, double thickness = double.NaN) {
391      // default thickness of the wireframe is 5% of smallest dimension
392      if (double.IsNaN(thickness))
393        thickness = Math.Min(width, Math.Min(height, depth)) * 0.05;
394
395      // The cube contains of 8 corner, each corner has 4 points:
396      // 1. The corner point
397      // 2. A point on the edge to the right of the corner
398      // 3. A point on the edge atop or below the corner
399      // 4. A point on the edge to the left of the corner
400
401      // Point 0, Front Left Bottom
402      mesh.Positions.Add(new Point3D(x, y, z));
403      mesh.Positions.Add(new Point3D(x + thickness, y, z));
404      mesh.Positions.Add(new Point3D(x, y, z + thickness));
405      mesh.Positions.Add(new Point3D(x, y + thickness, z));
406
407      // Point 1, Front Right Bottom
408      mesh.Positions.Add(new Point3D(x + width, y, z));
409      mesh.Positions.Add(new Point3D(x + width, y + thickness, z));
410      mesh.Positions.Add(new Point3D(x + width, y, z + thickness));
411      mesh.Positions.Add(new Point3D(x + width - thickness, y, z));
412
413      // Point 2, Back Right Bottom
414      mesh.Positions.Add(new Point3D(x + width, y + height, z));
415      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z));
416      mesh.Positions.Add(new Point3D(x + width, y + height, z + thickness));
417      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z));
418
419      // Point 3, Back Left Bottom
420      mesh.Positions.Add(new Point3D(x, y + height, z));
421      mesh.Positions.Add(new Point3D(x, y + height - thickness, z));
422      mesh.Positions.Add(new Point3D(x, y + height, z + thickness));
423      mesh.Positions.Add(new Point3D(x + thickness, y + height, z));
424
425      // Point 4, Front Left Top
426      mesh.Positions.Add(new Point3D(x, y, z + depth));
427      mesh.Positions.Add(new Point3D(x + thickness, y, z + depth));
428      mesh.Positions.Add(new Point3D(x, y, z + depth - thickness));
429      mesh.Positions.Add(new Point3D(x, y + thickness, z + depth));
430
431      // Point 5, Front Right Top
432      mesh.Positions.Add(new Point3D(x + width, y, z + depth));
433      mesh.Positions.Add(new Point3D(x + width, y + thickness, z + depth));
434      mesh.Positions.Add(new Point3D(x + width, y, z + depth - thickness));
435      mesh.Positions.Add(new Point3D(x + width - thickness, y, z + depth));
436
437      // Point 6, Back Right Top
438      mesh.Positions.Add(new Point3D(x + width, y + height, z + depth));
439      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z + depth));
440      mesh.Positions.Add(new Point3D(x + width, y + height, z + depth - thickness));
441      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z + depth));
442
443      // Point 7, Back Left Top
444      mesh.Positions.Add(new Point3D(x, y + height, z + depth));
445      mesh.Positions.Add(new Point3D(x, y + height - thickness, z + depth));
446      mesh.Positions.Add(new Point3D(x, y + height, z + depth - thickness));
447      mesh.Positions.Add(new Point3D(x + thickness, y + height, z + depth));
448
449      // Point 0, non-edge
450      mesh.Positions.Add(new Point3D(x + thickness, y, z + thickness));
451      mesh.Positions.Add(new Point3D(x, y + thickness, z + thickness));
452      mesh.Positions.Add(new Point3D(x + thickness, y + thickness, z));
453
454      // Point 1, non-edge
455      mesh.Positions.Add(new Point3D(x + width, y + thickness, z + thickness));
456      mesh.Positions.Add(new Point3D(x + width - thickness, y, z + thickness));
457      mesh.Positions.Add(new Point3D(x + width - thickness, y + thickness, z));
458
459      // Point 2, non-edge
460      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z + thickness));
461      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z + thickness));
462      mesh.Positions.Add(new Point3D(x + width - thickness, y + height - thickness, z));
463
464      // Point 3, non-edge
465      mesh.Positions.Add(new Point3D(x, y + height - thickness, z + thickness));
466      mesh.Positions.Add(new Point3D(x + thickness, y + height, z + thickness));
467      mesh.Positions.Add(new Point3D(x + thickness, y + height - thickness, z));
468
469      // Point 4, non-edge
470      mesh.Positions.Add(new Point3D(x + thickness, y, z + depth - thickness));
471      mesh.Positions.Add(new Point3D(x, y + thickness, z + depth - thickness));
472      mesh.Positions.Add(new Point3D(x + thickness, y + thickness, z + depth));
473
474      // Point 5, non-edge
475      mesh.Positions.Add(new Point3D(x + width, y + thickness, z + depth - thickness));
476      mesh.Positions.Add(new Point3D(x + width - thickness, y, z + depth - thickness));
477      mesh.Positions.Add(new Point3D(x + width - thickness, y + thickness, z + depth));
478
479      // Point 6, non-edge
480      mesh.Positions.Add(new Point3D(x + width - thickness, y + height, z + depth - thickness));
481      mesh.Positions.Add(new Point3D(x + width, y + height - thickness, z + depth - thickness));
482      mesh.Positions.Add(new Point3D(x + width - thickness, y + height - thickness, z + depth));
483
484      // Point 7, non-edge
485      mesh.Positions.Add(new Point3D(x, y + height - thickness, z + depth - thickness));
486      mesh.Positions.Add(new Point3D(x + thickness, y + height, z + depth - thickness));
487      mesh.Positions.Add(new Point3D(x + thickness, y + height - thickness, z + depth));
488
489      // Draw the 24 corner plates
490      AddPlane(mesh, 0, 1, 32, 2);
491      AddPlane(mesh, 0, 2, 33, 3);
492      AddPlane(mesh, 0, 3, 34, 1);
493
494      AddPlane(mesh, 4, 6, 36, 7);
495      AddPlane(mesh, 4, 5, 35, 6);
496      AddPlane(mesh, 4, 7, 37, 5);
497
498      AddPlane(mesh, 8, 10, 39, 11);
499      AddPlane(mesh, 8, 9, 38, 10);
500      AddPlane(mesh, 8, 11, 40, 9);
501
502      AddPlane(mesh, 12, 13, 41, 14);
503      AddPlane(mesh, 12, 14, 42, 15);
504      AddPlane(mesh, 12, 15, 43, 13);
505
506      AddPlane(mesh, 16, 18, 44, 17);
507      AddPlane(mesh, 16, 19, 45, 18);
508      AddPlane(mesh, 16, 17, 46, 19);
509
510      AddPlane(mesh, 20, 23, 48, 22);
511      AddPlane(mesh, 20, 22, 47, 21);
512      AddPlane(mesh, 20, 21, 49, 23);
513
514      AddPlane(mesh, 24, 27, 51, 26);
515      AddPlane(mesh, 24, 26, 50, 25);
516      AddPlane(mesh, 24, 25, 52, 27);
517
518      AddPlane(mesh, 28, 31, 54, 30);
519      AddPlane(mesh, 28, 30, 53, 29);
520      AddPlane(mesh, 28, 29, 55, 31);
521
522      // Draw the connecting plates
523      // on the bottom
524      AddPlane(mesh, 1, 7, 36, 32);
525      AddPlane(mesh, 1, 34, 37, 7);
526
527      AddPlane(mesh, 5, 11, 39, 35);
528      AddPlane(mesh, 5, 37, 40, 11);
529
530      AddPlane(mesh, 9, 15, 42, 38);
531      AddPlane(mesh, 9, 40, 43, 15);
532
533      AddPlane(mesh, 13, 3, 33, 41);
534      AddPlane(mesh, 13, 43, 34, 3);
535
536      // between bottom and top
537      AddPlane(mesh, 2, 32, 44, 18);
538      AddPlane(mesh, 2, 18, 45, 33);
539
540      AddPlane(mesh, 6, 22, 48, 36);
541      AddPlane(mesh, 6, 35, 47, 22);
542
543      AddPlane(mesh, 10, 26, 51, 39);
544      AddPlane(mesh, 10, 38, 50, 26);
545
546      AddPlane(mesh, 14, 30, 54, 42);
547      AddPlane(mesh, 14, 41, 53, 30);
548
549      // on the top
550      AddPlane(mesh, 17, 44, 48, 23);
551      AddPlane(mesh, 17, 23, 49, 46);
552
553      AddPlane(mesh, 21, 47, 51, 27);
554      AddPlane(mesh, 21, 27, 52, 49);
555
556      AddPlane(mesh, 25, 50, 54, 31);
557      AddPlane(mesh, 25, 31, 55, 52);
558
559      AddPlane(mesh, 29, 19, 46, 55);
560      AddPlane(mesh, 29, 53, 45, 19);
561    }
562
563    /// <summary>
564    /// Adds a plane by two triangles. The indices of the points have to be given
565    /// in counter-clockwise sequence.
566    /// </summary>
567    /// <param name="mesh">The mesh to add the triangles to</param>
568    /// <param name="a">The index of the first point</param>
569    /// <param name="b">The index of the second point</param>
570    /// <param name="c">The index of the third point</param>
571    /// <param name="d">The index of the fourth point</param>
572    private void AddPlane(MeshGeometry3D mesh, int a, int b, int c, int d) {
573      // two triangles form a plane
574      mesh.TriangleIndices.Add(a);
575      mesh.TriangleIndices.Add(b);
576      mesh.TriangleIndices.Add(d);
577      mesh.TriangleIndices.Add(c);
578      mesh.TriangleIndices.Add(d);
579      mesh.TriangleIndices.Add(b);
580    }
581    #endregion
582  }
583}
Note: See TracBrowser for help on using the repository browser.