1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Problems.BinPacking.Interfaces;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Common;
|
---|
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 | /// <summary>
|
---|
38 | /// Describes the size on the Y-axis
|
---|
39 | /// </summary>
|
---|
40 | [Storable]
|
---|
41 | public int Height { get; set; }
|
---|
42 | /// <summary>
|
---|
43 | /// Describes the size on the X-axis
|
---|
44 | /// </summary>
|
---|
45 | [Storable]
|
---|
46 | public int Width { get; set; }
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 | #region Helpers
|
---|
50 | public int MultipliedMeasures { get { return Height * Width; } }
|
---|
51 |
|
---|
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));
|
---|
57 | }
|
---|
58 | private bool Encloses(RectangleDiagonal r1, RectangleDiagonal r2) {
|
---|
59 | return ( r1.x1 <= r2.x1 &&
|
---|
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 | }
|
---|
68 | private bool Overlaps(TwoDimensionalPacking myPosition, TwoDimensionalPacking checkedPosition, RectangularPackingShape checkedShape) {
|
---|
69 | return Overlaps(new RectangleDiagonal (myPosition, this),new RectangleDiagonal (checkedPosition, checkedShape));
|
---|
70 | }
|
---|
71 | private bool Overlaps(RectangleDiagonal r1, RectangleDiagonal r2) {
|
---|
72 | return !(r1.x1 >= r2.x2 ||
|
---|
73 | r1.y1 >= r2.y2 ||
|
---|
74 | r1.x2 <= r2.x1 ||
|
---|
75 | r1.y2 <= r2.y1);
|
---|
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 |
|
---|
87 | public RectangularPackingShape(int width, int height) : base () {
|
---|
88 | this.Height = height;
|
---|
89 | this.Width = width;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public override void InitializeFromMeasures(int[] measures) {
|
---|
93 | if (measures.Length != 2)
|
---|
94 | throw new InvalidOperationException("Nr of measures does not fit shape-dimension.");
|
---|
95 | this.Width = measures[0];
|
---|
96 | this.Height = measures[1];
|
---|
97 | }
|
---|
98 | public override int[] ToArray() {
|
---|
99 | return new int[] { Width, Height };
|
---|
100 | }
|
---|
101 |
|
---|
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 | public RectangularPackingShape() : base() {}
|
---|
110 |
|
---|
111 | public override string ToString() {
|
---|
112 | return String.Format("RectangularPackingShape ({0}, {1})", this.Width, this.Height);
|
---|
113 | }
|
---|
114 |
|
---|
115 | #region IComparable<RectangularPackingShape> Members
|
---|
116 |
|
---|
117 | public int CompareTo(RectangularPackingShape other) {
|
---|
118 | int result = 0;// this.MultipliedMeasures.CompareTo(other.MultipliedMeasures);
|
---|
119 | if (result == 0) {
|
---|
120 | result = this.Width.CompareTo(other.Width);
|
---|
121 | if (result == 0)
|
---|
122 | result = this.Height.CompareTo(other.Height);
|
---|
123 | }
|
---|
124 | return result;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public int CompareTo(object obj) {
|
---|
128 | if (obj.GetType().Equals(this.GetType()))
|
---|
129 | return this.CompareTo((RectangularPackingShape)obj);
|
---|
130 | else return 0;
|
---|
131 | }
|
---|
132 |
|
---|
133 | #endregion
|
---|
134 |
|
---|
135 | private struct RectangleDiagonal {
|
---|
136 | public int x1;
|
---|
137 | public int y1;
|
---|
138 | public int x2;
|
---|
139 | public int y2;
|
---|
140 | public RectangleDiagonal(RectangularPackingShape myShape) : this(new TwoDimensionalPacking(0, 0, 0), myShape) { }
|
---|
141 | public RectangleDiagonal(TwoDimensionalPacking myPosition, RectangularPackingShape myShape) {
|
---|
142 | x1 = myPosition.X;
|
---|
143 | y1 = myPosition.Y;
|
---|
144 | x2 = myPosition.X + (myPosition.Rotated ? myShape.Height : myShape.Width);
|
---|
145 | y2 = myPosition.Y + (myPosition.Rotated ? myShape.Width : myShape.Height);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|