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