Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Problems.BinPacking/3.3/2D/PackingShape.cs @ 16057

Last change on this file since 16057 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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("PackingShape (2d)", "Represents the rectangular measures (width, height) of a two-dimensional bin-packing object.")]
33  [StorableClass]
34  public class PackingShape : PackingShape<PackingPosition>, IComparable<PackingShape> {
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 PackingShape(bool deserializing) : base(deserializing) { }
47    protected PackingShape(PackingShape original, Cloner cloner)
48      : base(original, cloner) {
49    }
50    public PackingShape()
51      : base() {
52      Parameters.Add(new FixedValueParameter<IntValue>("Width"));
53      Parameters.Add(new FixedValueParameter<IntValue>("Height"));
54    }
55    public PackingShape(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 PackingShape(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(PackingShape 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 PackingShape;
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, (PackingShape)checkedShape);
99    }
100    private bool Encloses(PackingPosition checkedPosition, PackingShape 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, (PackingShape)checkedShape);
112    }
113    private bool Overlaps(PackingPosition myPosition, PackingPosition checkedPosition, PackingShape 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(PackingShape myShape) : this(new PackingPosition(0, 0, 0), myShape) { }
139      public RectangleDiagonal(PackingPosition myPosition, PackingShape 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.