[9348] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[13032] | 3 | * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9348] | 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
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[13032] | 22 |
|
---|
[9348] | 23 | using System;
|
---|
| 24 | using HeuristicLab.Problems.BinPacking.Interfaces;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Problems.BinPacking.Dimensions;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.BinPacking.Shapes {
|
---|
| 31 | [Item("RectangularPackingShape", "Represents the rectangular measures (width, height) of a two-dimensional bin-packing object.")]
|
---|
| 32 | [StorableClass]
|
---|
| 33 | public abstract class RectangularPackingShape : PackingShape<TwoDimensionalPacking>, IRegularPackingShape, IComparable<RectangularPackingShape> {
|
---|
| 34 | #region Properties
|
---|
| 35 | [Storable]
|
---|
| 36 | public int Height { get; set; }
|
---|
[13497] | 37 |
|
---|
[9348] | 38 | [Storable]
|
---|
| 39 | public int Width { get; set; }
|
---|
| 40 | #endregion
|
---|
| 41 |
|
---|
| 42 | #region Helpers
|
---|
[9593] | 43 | public override TwoDimensionalPacking Origin { get { return new TwoDimensionalPacking(0, 0, 0); } }
|
---|
| 44 | public override int MultipliedMeasures { get { return Height * Width; } }
|
---|
[9348] | 45 |
|
---|
[9596] | 46 | public override bool EnclosesPoint(TwoDimensionalPacking myPosition, TwoDimensionalPacking checkedPoint) {
|
---|
[9599] | 47 | return (myPosition.X <= checkedPoint.X &&
|
---|
| 48 | (myPosition.X + (myPosition.Rotated ? Height : Width) - 1) >= checkedPoint.X &&
|
---|
| 49 | myPosition.Y <= checkedPoint.Y &&
|
---|
| 50 | (myPosition.Y + (myPosition.Rotated ? Width : Height) - 1) >= checkedPoint.Y);
|
---|
[9596] | 51 | }
|
---|
[9348] | 52 | public override bool Encloses(TwoDimensionalPacking checkedPosition, PackingShape<TwoDimensionalPacking> checkedShape) {
|
---|
| 53 | return Encloses(checkedPosition, (RectangularPackingShape)checkedShape);
|
---|
| 54 | }
|
---|
| 55 | private bool Encloses(TwoDimensionalPacking checkedPosition, RectangularPackingShape checkedShape) {
|
---|
| 56 | return Encloses(new RectangleDiagonal(this), new RectangleDiagonal(checkedPosition, checkedShape));
|
---|
[13497] | 57 | }
|
---|
[9348] | 58 | private bool Encloses(RectangleDiagonal r1, RectangleDiagonal r2) {
|
---|
[13497] | 59 | return (r1.x1 <= r2.x1 &&
|
---|
[9348] | 60 | r1.x2 >= r2.x2 &&
|
---|
| 61 | r1.y1 <= r2.y1 &&
|
---|
| 62 | r1.y2 >= r2.y2);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public override bool Overlaps(TwoDimensionalPacking myPosition, TwoDimensionalPacking checkedPosition, PackingShape<TwoDimensionalPacking> checkedShape) {
|
---|
| 66 | return Overlaps(myPosition, checkedPosition, (RectangularPackingShape)checkedShape);
|
---|
| 67 | }
|
---|
[13497] | 68 | private bool Overlaps(TwoDimensionalPacking myPosition, TwoDimensionalPacking checkedPosition, RectangularPackingShape checkedShape) {
|
---|
| 69 | return Overlaps(new RectangleDiagonal(myPosition, this), new RectangleDiagonal(checkedPosition, checkedShape));
|
---|
[9348] | 70 | }
|
---|
| 71 | private bool Overlaps(RectangleDiagonal r1, RectangleDiagonal r2) {
|
---|
[9599] | 72 | return !(r1.x1 > r2.x2 ||
|
---|
| 73 | r1.y1 > r2.y2 ||
|
---|
| 74 | r1.x2 < r2.x1 ||
|
---|
| 75 | r1.y2 < r2.y1);
|
---|
[9348] | 76 | }
|
---|
| 77 |
|
---|
| 78 | public void ApplyHorizontalOrientation() {
|
---|
| 79 | if (Width < Height) {
|
---|
| 80 | var aux = Width;
|
---|
| 81 | Width = Height;
|
---|
| 82 | Height = aux;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | #endregion
|
---|
| 86 |
|
---|
[13497] | 87 | protected RectangularPackingShape() : base() { }
|
---|
| 88 | protected RectangularPackingShape(int width, int height)
|
---|
| 89 | : base() {
|
---|
[9348] | 90 | this.Height = height;
|
---|
| 91 | this.Width = width;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | public override void InitializeFromMeasures(int[] measures) {
|
---|
| 95 | if (measures.Length != 2)
|
---|
| 96 | throw new InvalidOperationException("Nr of measures does not fit shape-dimension.");
|
---|
| 97 | this.Width = measures[0];
|
---|
| 98 | this.Height = measures[1];
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[13497] | 101 |
|
---|
[9348] | 102 | [StorableConstructor]
|
---|
| 103 | protected RectangularPackingShape(bool deserializing) : base(deserializing) { }
|
---|
| 104 | protected RectangularPackingShape(RectangularPackingShape original, Cloner cloner)
|
---|
| 105 | : base(original, cloner) {
|
---|
| 106 | this.Width = original.Width;
|
---|
| 107 | this.Height = original.Height;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | public override string ToString() {
|
---|
| 111 | return String.Format("RectangularPackingShape ({0}, {1})", this.Width, this.Height);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | #region IComparable<RectangularPackingShape> Members
|
---|
| 115 |
|
---|
| 116 | public int CompareTo(RectangularPackingShape other) {
|
---|
| 117 | int result = 0;// this.MultipliedMeasures.CompareTo(other.MultipliedMeasures);
|
---|
| 118 | if (result == 0) {
|
---|
| 119 | result = this.Width.CompareTo(other.Width);
|
---|
[13497] | 120 | if (result == 0)
|
---|
[9348] | 121 | result = this.Height.CompareTo(other.Height);
|
---|
| 122 | }
|
---|
| 123 | return result;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | public int CompareTo(object obj) {
|
---|
[13497] | 127 | var other = obj as RectangularPackingShape;
|
---|
| 128 | if (other != null) return CompareTo(other);
|
---|
| 129 | else throw new ArgumentException(string.Format("Cannot compare to object {0}", obj), "obj");
|
---|
[9348] | 130 | }
|
---|
| 131 |
|
---|
| 132 | #endregion
|
---|
| 133 |
|
---|
[13497] | 134 | public override int[] ToArray() {
|
---|
| 135 | return new int[] { Width, Height };
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[9348] | 138 | private struct RectangleDiagonal {
|
---|
| 139 | public int x1;
|
---|
| 140 | public int y1;
|
---|
| 141 | public int x2;
|
---|
| 142 | public int y2;
|
---|
| 143 | public RectangleDiagonal(RectangularPackingShape myShape) : this(new TwoDimensionalPacking(0, 0, 0), myShape) { }
|
---|
| 144 | public RectangleDiagonal(TwoDimensionalPacking myPosition, RectangularPackingShape myShape) {
|
---|
| 145 | x1 = myPosition.X;
|
---|
| 146 | y1 = myPosition.Y;
|
---|
[9599] | 147 | x2 = myPosition.X + (myPosition.Rotated ? myShape.Height : myShape.Width) - 1;
|
---|
| 148 | y2 = myPosition.Y + (myPosition.Rotated ? myShape.Width : myShape.Height) - 1;
|
---|
[9348] | 149 | }
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 | }
|
---|