Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13606 was 13606, checked in by gkronber, 8 years ago

#1966 refactoring (moved 2d-specific classes into separate project)

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