source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Shapes/RectangularPackingShape.cs @ 13497

Last change on this file since 13497 was 13497, checked in by gkronber, 5 years ago

#1966: fixed various problems: bugs in cloning, bugs in persistence, method names, various minor improvements of source code for readability.

File size: 6.0 KB
Line 
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
23using System;
24using HeuristicLab.Problems.BinPacking.Interfaces;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Common;
28using HeuristicLab.Problems.BinPacking.Dimensions;
29
30namespace 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; }
37
38    [Storable]
39    public int Width { get; set; }
40    #endregion
41
42    #region Helpers
43    public override TwoDimensionalPacking Origin { get { return new TwoDimensionalPacking(0, 0, 0); } }
44    public override int MultipliedMeasures { get { return Height * Width; } }
45
46    public override bool EnclosesPoint(TwoDimensionalPacking myPosition, TwoDimensionalPacking checkedPoint) {
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);
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    protected RectangularPackingShape() : base() { }
88    protected RectangularPackingShape(int width, int height)
89      : base() {
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
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
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);
120        if (result == 0)
121          result = this.Height.CompareTo(other.Height);
122      }
123      return result;
124    }
125
126    public int CompareTo(object obj) {
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");
130    }
131
132    #endregion
133
134    public override int[] ToArray() {
135      return new int[] { Width, Height };
136    }
137
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;
147        x2 = myPosition.X + (myPosition.Rotated ? myShape.Height : myShape.Width) - 1;
148        y2 = myPosition.Y + (myPosition.Rotated ? myShape.Width : myShape.Height) - 1;
149      }
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.