source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/RectangularPackingShape.cs @ 14048

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

#1966: renamed *PackingDimension -> PackingPosition

File size: 5.8 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.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Common;
27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Problems.BinPacking;
30
31namespace HeuristicLab.Problems.BinPacking2D {
32  [Item("RectangularPackingShape", "Represents the rectangular measures (width, height) of a two-dimensional bin-packing object.")]
33  [StorableClass]
34  public class RectangularPackingShape : PackingShape<PackingPosition>, IComparable<RectangularPackingShape> {
35    public int Height {
36      get { return ((IFixedValueParameter<IntValue>)Parameters["Height"]).Value.Value; }
37      set { ((IFixedValueParameter<IntValue>)Parameters["Height"]).Value.Value = value; }
38    }
39
40    public int Width {
41      get { return ((IFixedValueParameter<IntValue>)Parameters["Width"]).Value.Value; }
42      set { ((IFixedValueParameter<IntValue>)Parameters["Width"]).Value.Value = value; }
43    }
44
45    [StorableConstructor]
46    protected RectangularPackingShape(bool deserializing) : base(deserializing) { }
47    protected RectangularPackingShape(RectangularPackingShape original, Cloner cloner)
48      : base(original, cloner) {
49    }
50    public RectangularPackingShape()
51      : base() {
52      Parameters.Add(new FixedValueParameter<IntValue>("Width"));
53      Parameters.Add(new FixedValueParameter<IntValue>("Height"));
54    }
55    public RectangularPackingShape(int width, int height)
56      : this() {
57      this.Height = height;
58      this.Width = width;
59    }
60
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new RectangularPackingShape(this, cloner);
63    }
64
65    public override string ToString() {
66      return String.Format("RectangularPackingShape ({0}, {1})", this.Width, this.Height);
67    }
68
69    #region IComparable<RectangularPackingShape> Members
70
71    public int CompareTo(RectangularPackingShape other) {
72      int result = this.Width.CompareTo(other.Width);
73      if (result == 0)
74        result = this.Height.CompareTo(other.Height);
75      return result;
76    }
77
78    public override int CompareTo(object obj) {
79      var other = obj as RectangularPackingShape;
80      if (other != null) return CompareTo(other);
81      else throw new ArgumentException(string.Format("Cannot compare to object {0}", obj), "obj");
82    }
83
84    #endregion
85
86
87    #region Helpers
88    public override PackingPosition Origin { get { return new PackingPosition(0, 0, 0); } }
89    public override int Volume { get { return Height * Width; } }
90
91    public override bool EnclosesPoint(PackingPosition myPosition, PackingPosition checkedPoint) {
92      return (myPosition.X <= checkedPoint.X &&
93                (myPosition.X + (myPosition.Rotated ? Height : Width) - 1) >= checkedPoint.X &&
94                myPosition.Y <= checkedPoint.Y &&
95                (myPosition.Y + (myPosition.Rotated ? Width : Height) - 1) >= checkedPoint.Y);
96    }
97    public override bool Encloses(PackingPosition checkedPosition, PackingShape<PackingPosition> checkedShape) {
98      return Encloses(checkedPosition, (RectangularPackingShape)checkedShape);
99    }
100    private bool Encloses(PackingPosition checkedPosition, RectangularPackingShape checkedShape) {
101      return Encloses(new RectangleDiagonal(this), new RectangleDiagonal(checkedPosition, checkedShape));
102    }
103    private bool Encloses(RectangleDiagonal r1, RectangleDiagonal r2) {
104      return (r1.x1 <= r2.x1 &&
105                r1.x2 >= r2.x2 &&
106                r1.y1 <= r2.y1 &&
107                r1.y2 >= r2.y2);
108    }
109
110    public override bool Overlaps(PackingPosition myPosition, PackingPosition checkedPosition, PackingShape<PackingPosition> checkedShape) {
111      return Overlaps(myPosition, checkedPosition, (RectangularPackingShape)checkedShape);
112    }
113    private bool Overlaps(PackingPosition myPosition, PackingPosition checkedPosition, RectangularPackingShape checkedShape) {
114      return Overlaps(new RectangleDiagonal(myPosition, this), new RectangleDiagonal(checkedPosition, checkedShape));
115    }
116    private bool Overlaps(RectangleDiagonal r1, RectangleDiagonal r2) {
117      return !(r1.x1 > r2.x2 ||
118               r1.y1 > r2.y2 ||
119               r1.x2 < r2.x1 ||
120               r1.y2 < r2.y1);
121    }
122
123    public override void ApplyHorizontalOrientation() {
124      if (Width < Height) {
125        var aux = Width;
126        Width = Height;
127        Height = aux;
128      }
129    }
130    #endregion
131
132
133    private struct RectangleDiagonal {
134      public int x1;
135      public int y1;
136      public int x2;
137      public int y2;
138      public RectangleDiagonal(RectangularPackingShape myShape) : this(new PackingPosition(0, 0, 0), myShape) { }
139      public RectangleDiagonal(PackingPosition myPosition, RectangularPackingShape myShape) {
140        x1 = myPosition.X;
141        y1 = myPosition.Y;
142        x2 = myPosition.X + (myPosition.Rotated ? myShape.Height : myShape.Width) - 1;
143        y2 = myPosition.Y + (myPosition.Rotated ? myShape.Width : myShape.Height) - 1;
144      }
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.