#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.Problems.BinPacking.Interfaces;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Core;
using HeuristicLab.Common;
using HeuristicLab.Problems.BinPacking.Shapes;
using HeuristicLab.Problems.BinPacking.PackingItem;
using HeuristicLab.Problems.BinPacking.PackingBin;
namespace HeuristicLab.Problems.BinPacking.Dimensions {
[Item("Occupated Points 2D", "A datastructure holding all currently occupied points of a 2D packing-bin.")]
[StorableClass]
public class OccupiedPoints2D : OccupiedPoints {
[Storable]
private int[,] occupiedPoints { get; set; }
public OccupiedPoints2D(RectangularPackingBin binMeasures) : base(binMeasures) {
occupiedPoints = new int[binMeasures.Width, binMeasures.Height];
for (int w = 0; w < binMeasures.Width; w++) {
for (int h = 0; h < binMeasures.Height; h++) {
occupiedPoints[w, h] = -1;
}
}
}
[StorableConstructor]
protected OccupiedPoints2D(bool deserializing) : base(deserializing) { }
protected OccupiedPoints2D(OccupiedPoints2D original, Cloner cloner)
: base(original, cloner) {
this.occupiedPoints = (int[,])original.occupiedPoints.Clone();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new OccupiedPoints2D(this, cloner);
}
public override bool IsPositionFeasible(RectangularPackingItem measures, TwoDimensionalPacking position) {
for (int w = position.X; w < position.X + measures.Width; w++) {
for (int h = position.Y; h < position.Y + measures.Height; h++) {
if (occupiedPoints[w, h] != -1)
return false;
}
}
return true;
}
public override void OccupyPoints(RectangularPackingItem measures, TwoDimensionalPacking position, int itemID) {
int width = position.Rotated ? measures.Height : measures.Width;
int height = position.Rotated ? measures.Width : measures.Height;
for (int w = position.X; w < position.X + width; w++) {
for (int h = position.Y; h < position.Y + height; h++) {
occupiedPoints[w, h] = itemID;
}
}
}
public override bool IsPointOccupied(TwoDimensionalPacking point) {
return occupiedPoints[point.X, point.Y] != -1;
}
public override int ShortestPossibleSideFromEP(TwoDimensionalPacking ep) {
int shortestSide = int.MaxValue;
int width = occupiedPoints.GetLength(0);
int height = occupiedPoints.GetLength(1);
if (ep.X >= width || ep.Y >= height)
return shortestSide;
int currentX = ep.X;
while (currentX < width && occupiedPoints[currentX, ep.Y] == -1) { currentX++; }
if (currentX - ep.X < shortestSide)
shortestSide = currentX - ep.X;
int currentY = ep.Y;
while (currentY < height && occupiedPoints[ep.X, currentY] == -1) { currentY++; }
if (currentY - ep.Y < shortestSide)
shortestSide = currentY - ep.Y;
return shortestSide;
}
public override bool IsStaticStable(RectangularPackingItem item, TwoDimensionalPacking ep) {
//For the current implementation there was no need for stacking-constraints in the 2D-case.
//But it could be interesting for shelf-packing applications!
throw new NotImplementedException();
}
public override bool WeightIsSupported(RectangularPackingItem item, TwoDimensionalPacking ep, ItemList items) {
return true;
}
public override void ChangeBinMeasures(RectangularPackingBin binMeasures) {
throw new NotImplementedException();
}
}
}