Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.2D/3.3/PackingShape.cs @ 14427

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

#1966: simplified class names

File size: 5.7 KB
RevLine 
[9348]1#region License Information
2/* HeuristicLab
[13032]3 * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9348]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
[13032]22
[9348]23using System;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Common;
[13574]27using HeuristicLab.Data;
28using HeuristicLab.Parameters;
[14046]29using HeuristicLab.Problems.BinPacking;
[9348]30
[14046]31namespace HeuristicLab.Problems.BinPacking2D {
[14049]32  [Item("PackingShape (2d)", "Represents the rectangular measures (width, height) of a two-dimensional bin-packing object.")]
[9348]33  [StorableClass]
[14049]34  public class PackingShape : PackingShape<PackingPosition>, IComparable<PackingShape> {
[13574]35    public int Height {
36      get { return ((IFixedValueParameter<IntValue>)Parameters["Height"]).Value.Value; }
37      set { ((IFixedValueParameter<IntValue>)Parameters["Height"]).Value.Value = value; }
38    }
[13497]39
[13574]40    public int Width {
41      get { return ((IFixedValueParameter<IntValue>)Parameters["Width"]).Value.Value; }
42      set { ((IFixedValueParameter<IntValue>)Parameters["Width"]).Value.Value = value; }
43    }
44
[13605]45    [StorableConstructor]
[14049]46    protected PackingShape(bool deserializing) : base(deserializing) { }
47    protected PackingShape(PackingShape original, Cloner cloner)
[13605]48      : base(original, cloner) {
49    }
[14049]50    public PackingShape()
[13574]51      : base() {
52      Parameters.Add(new FixedValueParameter<IntValue>("Width"));
53      Parameters.Add(new FixedValueParameter<IntValue>("Height"));
54    }
[14049]55    public PackingShape(int width, int height)
[13574]56      : this() {
57      this.Height = height;
58      this.Width = width;
59    }
60
[14045]61    public override IDeepCloneable Clone(Cloner cloner) {
[14049]62      return new PackingShape(this, cloner);
[14045]63    }
64
[13574]65    public override string ToString() {
66      return String.Format("RectangularPackingShape ({0}, {1})", this.Width, this.Height);
67    }
68
69    #region IComparable<RectangularPackingShape> Members
70
[14049]71    public int CompareTo(PackingShape other) {
[13574]72      int result = this.Width.CompareTo(other.Width);
73      if (result == 0)
74        result = this.Height.CompareTo(other.Height);
75      return result;
76    }
77
[14043]78    public override int CompareTo(object obj) {
[14049]79      var other = obj as PackingShape;
[13574]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
[9348]87    #region Helpers
[14048]88    public override PackingPosition Origin { get { return new PackingPosition(0, 0, 0); } }
[13605]89    public override int Volume { get { return Height * Width; } }
[9348]90
[14048]91    public override bool EnclosesPoint(PackingPosition myPosition, PackingPosition checkedPoint) {
[9599]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);
[9596]96    }
[14048]97    public override bool Encloses(PackingPosition checkedPosition, PackingShape<PackingPosition> checkedShape) {
[14049]98      return Encloses(checkedPosition, (PackingShape)checkedShape);
[9348]99    }
[14049]100    private bool Encloses(PackingPosition checkedPosition, PackingShape checkedShape) {
[9348]101      return Encloses(new RectangleDiagonal(this), new RectangleDiagonal(checkedPosition, checkedShape));
[13497]102    }
[9348]103    private bool Encloses(RectangleDiagonal r1, RectangleDiagonal r2) {
[13497]104      return (r1.x1 <= r2.x1 &&
[9348]105                r1.x2 >= r2.x2 &&
106                r1.y1 <= r2.y1 &&
107                r1.y2 >= r2.y2);
108    }
109
[14048]110    public override bool Overlaps(PackingPosition myPosition, PackingPosition checkedPosition, PackingShape<PackingPosition> checkedShape) {
[14049]111      return Overlaps(myPosition, checkedPosition, (PackingShape)checkedShape);
[9348]112    }
[14049]113    private bool Overlaps(PackingPosition myPosition, PackingPosition checkedPosition, PackingShape checkedShape) {
[13497]114      return Overlaps(new RectangleDiagonal(myPosition, this), new RectangleDiagonal(checkedPosition, checkedShape));
[9348]115    }
116    private bool Overlaps(RectangleDiagonal r1, RectangleDiagonal r2) {
[9599]117      return !(r1.x1 > r2.x2 ||
118               r1.y1 > r2.y2 ||
119               r1.x2 < r2.x1 ||
120               r1.y2 < r2.y1);
[9348]121    }
122
[14043]123    public override void ApplyHorizontalOrientation() {
[9348]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;
[14049]138      public RectangleDiagonal(PackingShape myShape) : this(new PackingPosition(0, 0, 0), myShape) { }
139      public RectangleDiagonal(PackingPosition myPosition, PackingShape myShape) {
[9348]140        x1 = myPosition.X;
141        y1 = myPosition.Y;
[9599]142        x2 = myPosition.X + (myPosition.Rotated ? myShape.Height : myShape.Width) - 1;
143        y2 = myPosition.Y + (myPosition.Rotated ? myShape.Width : myShape.Height) - 1;
[9348]144      }
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.