Beispiel #1
0
 public BinPackShelfAlgorithm(
     BinPackParameter parameter,
     FreeRectChoiceHeuristic rectChoice,
     GuillotineSplitHeuristic splitMethod,
     ShelfChoiceHeuristic shelfChoice)
 {
     _parameter     = parameter;
     _rectChoice    = rectChoice;
     _splitMethod   = splitMethod;
     _shelfChoice   = shelfChoice;
     _currentY      = 0;
     _shelves       = new List <Shelf>();
     _packedCuboids = new List <Cuboid>();
     StartNewShelf(0);
 }
 public BinPackShelfAlgorithm(
     decimal binWidth,
     decimal binHeight,
     decimal binDepth,
     FreeRectChoiceHeuristic rectChoice,
     GuillotineSplitHeuristic splitMethod,
     ShelfChoiceHeuristic shelfChoice)
 {
     _binWidth    = binWidth;
     _binHeight   = binHeight;
     _binDepth    = binDepth;
     _rectChoice  = rectChoice;
     _splitMethod = splitMethod;
     _shelfChoice = shelfChoice;
     _currentY    = 0;
     _shelves     = new List <Shelf>();
     StartNewShelf(0);
 }
        private void Insert(Cuboid cuboid, ShelfChoiceHeuristic method)
        {
            switch (method)
            {
            case ShelfChoiceHeuristic.ShelfNextFit:
                PutOnShelf(_shelves.Last(), cuboid);
                if (cuboid.IsPlaced)
                {
                    AddToShelf(_shelves.Last(), cuboid);
                    return;
                }
                break;

            case ShelfChoiceHeuristic.ShelfFirstFit:
                foreach (var shelf in _shelves)
                {
                    PutOnShelf(shelf, cuboid);
                    if (cuboid.IsPlaced)
                    {
                        AddToShelf(shelf, cuboid);
                        return;
                    }
                }
                break;
            }

            // The rectangle did not fit on any of the shelves. Open a new shelf.

            // Sort edges in decreasing order
            var edges = new List <decimal>()
            {
                cuboid.Width, cuboid.Height, cuboid.Depth
            };

            edges.Sort();
            var max    = edges[2];
            var middle = edges[1];
            var min    = edges[0];

            var whdSet = new[]
            {
                new { w = middle, h = max, d = min },
                new { w = max, h = middle, d = min },
                new { w = middle, h = min, d = max }
            };

            foreach (var whd in whdSet)
            {
                cuboid.Width  = whd.w;
                cuboid.Height = whd.h;
                cuboid.Depth  = whd.d;
                if (CanStartNewShelf(cuboid.Height))
                {
                    StartNewShelf(cuboid.Height);
                    PutOnShelf(_shelves.Last(), cuboid);
                    if (cuboid.IsPlaced)
                    {
                        AddToShelf(_shelves.Last(), cuboid);
                        return;
                    }
                }
            }

            // The rectangle didn't fit.
        }
Beispiel #4
0
        private void Insert(Cuboid cuboid, ShelfChoiceHeuristic method)
        {
            // Check is overweight
            if (cuboid.Weight + _packedCuboids.Sum(x => x.Weight) > _parameter.BinWeight)
            {
                return;
            }

            switch (method)
            {
            case ShelfChoiceHeuristic.ShelfNextFit:
                PutOnShelf(_shelves.Last(), cuboid);
                if (cuboid.IsPlaced)
                {
                    AddToShelf(_shelves.Last(), cuboid);
                    return;
                }
                break;

            case ShelfChoiceHeuristic.ShelfFirstFit:
                foreach (var shelf in _shelves)
                {
                    PutOnShelf(shelf, cuboid);
                    if (cuboid.IsPlaced)
                    {
                        AddToShelf(shelf, cuboid);
                        return;
                    }
                }
                break;

            default:
                throw new NotSupportedException($"shelf choice is unsupported: {method}");
            }

            // The rectangle did not fit on any of the shelves. Open a new shelf.

            // Sort edges in decreasing order
            var edges = new List <decimal>()
            {
                cuboid.Width, cuboid.Height, cuboid.Depth
            };

            edges.Sort();
            var max    = edges[2];
            var middle = edges[1];
            var min    = edges[0];

            var whdSet = new[]
            {
                new { w = middle, h = max, d = min },
                new { w = max, h = middle, d = min },
                new { w = middle, h = min, d = max }
            };

            foreach (var whd in whdSet)
            {
                if (_parameter.AllowRotateVertically || cuboid.Height == whd.h)
                {
                    cuboid.Width  = whd.w;
                    cuboid.Height = whd.h;
                    cuboid.Depth  = whd.d;
                    if (CanStartNewShelf(cuboid.Height))
                    {
                        StartNewShelf(cuboid.Height);
                        PutOnShelf(_shelves.Last(), cuboid);
                        if (cuboid.IsPlaced)
                        {
                            AddToShelf(_shelves.Last(), cuboid);
                            return;
                        }
                    }
                }
            }
            // The rectangle didn't fit.
        }