Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15554 was 15554, checked in by rhanghof, 6 years ago

#2817:

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