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