Ignore:
Timestamp:
11/28/17 16:10:31 (3 years ago)
Author:
rhanghof
Message:

#2817:

  • Added line projection based bin packing
  • Added residual spaces to the view
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2817-BinPackingSpeedup/HeuristicLab.Problems.BinPacking.Views/3.3/Container3DView.xaml.cs

    r15307 r15488  
    2727using System.Windows.Media.Media3D;
    2828using HeuristicLab.Problems.BinPacking3D;
     29using HeuristicLab.Collections;
    2930
    3031namespace HeuristicLab.Problems.BinPacking.Views {
     
    4950    private static readonly Color hiddenColor = Color.FromArgb(0x1A, 0xAA, 0xAA, 0xAA);
    5051    private static readonly Color containerColor = Color.FromArgb(0x7F, 0xAA, 0xAA, 0xAA);
     52    private static readonly Color residualSpaceColor = Color.FromArgb(0x7F, 0xAA, 0xAA, 0x00);
    5153
    5254    private Point startPos;
     
    5557    private double startAngleY;
    5658    private int selectedItemKey;
     59    private int selectedExtremePointIndex;
    5760    private bool showExtremePoints;
     61    private bool showResidualSpaces;
    5862
    5963    private BinPacking<BinPacking3D.PackingPosition, PackingShape, PackingItem> packing;
     
    7074    private Dictionary<int, DiffuseMaterial> materials;
    7175
     76    public ObservableDictionary<BinPacking3D.PackingPosition, Tuple<int, int, int>> ResidualSpaces { get; set; }
     77    public ObservableCollection<BinPacking3D.PackingPosition> ExtremePoints { get; set; }
     78
    7279    public Container3DView() {
    7380      InitializeComponent();
    7481      camMain.Position = new Point3D(0.5, 3, 3); // for design time we use a different camera position
    7582      materials = new Dictionary<int, DiffuseMaterial>();
     83      ResidualSpaces = new ObservableDictionary<BinPacking3D.PackingPosition, Tuple<int, int, int>>();
     84      ExtremePoints = new ObservableCollection<BinPacking3D.PackingPosition>();
     85      selectedExtremePointIndex = -1;
    7686      Clear();
    7787    }
     
    8595    }
    8696
     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
    87106    public void SelectItem(int itemKey) {
    88107      // selection of an item should make all other items semi-transparent
     
    90109      UpdateVisualization();
    91110    }
     111
    92112    public void ClearSelection() {
    93113      // remove all transparency
    94114      selectedItemKey = -1;
     115      selectedExtremePointIndex = -1;
    95116      UpdateVisualization();
    96117    }
     
    98119    private void UpdateVisualization() {
    99120      Clear();
    100       if (packing == null) return; // nothing to display
     121      if (packing == null)
     122        return; // nothing to display
    101123
    102124      var modelGroup = (Model3DGroup)MyModel.Content;
     
    108130
    109131        var colorIdx = selectedItem.Value.Material;
    110         while (colorIdx < 0) colorIdx += colors.Length;
     132        while (colorIdx < 0)
     133          colorIdx += colors.Length;
    111134        colorIdx = colorIdx % colors.Length;
    112135        var color = colors[colorIdx];
     
    132155          modelGroup.Children.Add(model);
    133156        }
     157
     158
    134159      } else {
    135160        foreach (var item in packing.Items) {
     
    144169          if (!materials.TryGetValue(item.Value.Material, out material)) {
    145170            var colorIdx = item.Value.Material;
    146             while (colorIdx < 0) colorIdx += colors.Length;
     171            while (colorIdx < 0)
     172              colorIdx += colors.Length;
    147173            colorIdx = colorIdx % colors.Length;
    148174            var color = colors[colorIdx];
     
    156182      }
    157183
    158       if (showExtremePoints) {
    159         // draw extreme-points
    160         var maxMag = (int)Math.Log10(Math.Max(packing.BinShape.Depth, Math.Max(packing.BinShape.Height, packing.BinShape.Width)));
    161         var cubeSize = (int)Math.Max(Math.Pow(10, maxMag - 2), 1);
    162         foreach (var ep in packing.ExtremePoints) {
    163           var epModel = new GeometryModel3D { Geometry = new MeshGeometry3D(), Material = new DiffuseMaterial() { Brush = new SolidColorBrush(Colors.Red) } };
    164           AddSolidCube((MeshGeometry3D)epModel.Geometry, ep.X, ep.Y, ep.Z, cubeSize, cubeSize, cubeSize);
    165           modelGroup.Children.Add(epModel);
    166         }
    167       }
     184
     185      AddExtremePoints(modelGroup);
    168186
    169187      var container = packing.BinShape;
     
    191209    }
    192210
     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        var rs = ResidualSpaces[extremePoint];
     243        var containerModel1 = new GeometryModel3D(new MeshGeometry3D(), new DiffuseMaterial(new SolidColorBrush(residualSpaceColor)));
     244
     245        modelGroup.Children.Add(containerModel1);
     246        AddWireframeCube((MeshGeometry3D)containerModel1.Geometry,
     247          extremePoint.X,
     248          extremePoint.Y,
     249          extremePoint.Z,
     250          rs.Item1,
     251          rs.Item2,
     252          rs.Item3);
     253
     254      }
     255    }
     256
    193257
    194258    private void Clear() {
     
    202266
    203267    private void Container3DView_MouseMove(object sender, MouseEventArgs e) {
    204       if (!mouseDown) return;
     268      if (!mouseDown)
     269        return;
    205270      var pos = e.GetPosition((IInputElement)this);
    206271
     
    235300      Focus(); // for mouse wheel events
    236301    }
    237     private void showExtremePointsCheckBoxOnChecked(object sender, RoutedEventArgs e) {
     302    private void ShowExtremePointsCheckBoxOnChecked(object sender, RoutedEventArgs e) {
    238303      showExtremePoints = true;
    239304      UpdateVisualization();
    240305    }
    241     private void showExtremePointsCheckBoxOnUnchecked(object sender, RoutedEventArgs e) {
     306    private void ShowExtremePointsCheckBoxOnUnchecked(object sender, RoutedEventArgs e) {
    242307      showExtremePoints = false;
     308      UpdateVisualization();
     309    }
     310
     311    private void ShowResidualSpacesCheckBoxOnChecked(object sender, RoutedEventArgs e) {
     312      showResidualSpaces = true;
     313      UpdateVisualization();
     314    }
     315    private void ShowResidualSpacesCheckBoxOnUnchecked(object sender, RoutedEventArgs e) {
     316      showResidualSpaces = false;
    243317      UpdateVisualization();
    244318    }
Note: See TracChangeset for help on using the changeset viewer.