Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966:

  • removed unused using
  • added/updated license headers
File size: 6.1 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.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    /// <summary>
36    /// Describes the size on the Y-axis
37    /// </summary>
38    [Storable]
39    public int Height { get; set; }
40    /// <summary>
41    /// Describes the size on the X-axis
42    /// </summary> 
43    [Storable]
44    public int Width { get; set; }
45    #endregion
46
47    #region Helpers
[9593]48    public override TwoDimensionalPacking Origin { get { return new TwoDimensionalPacking(0, 0, 0); } }
49    public override int MultipliedMeasures { get { return Height * Width; } }
[9348]50
[9596]51    public override bool EnclosesPoint(TwoDimensionalPacking myPosition, TwoDimensionalPacking checkedPoint) {
[9599]52      return (myPosition.X <= checkedPoint.X &&
53                (myPosition.X + (myPosition.Rotated ? Height : Width) - 1) >= checkedPoint.X &&
54                myPosition.Y <= checkedPoint.Y &&
55                (myPosition.Y + (myPosition.Rotated ? Width : Height) - 1) >= checkedPoint.Y);
[9596]56    }
[9348]57    public override bool Encloses(TwoDimensionalPacking checkedPosition, PackingShape<TwoDimensionalPacking> checkedShape) {
58      return Encloses(checkedPosition, (RectangularPackingShape)checkedShape);
59    }
60    private bool Encloses(TwoDimensionalPacking checkedPosition, RectangularPackingShape checkedShape) {
61      return Encloses(new RectangleDiagonal(this), new RectangleDiagonal(checkedPosition, checkedShape));
62    }       
63    private bool Encloses(RectangleDiagonal r1, RectangleDiagonal r2) {
64      return  ( r1.x1 <= r2.x1 &&
65                r1.x2 >= r2.x2 &&
66                r1.y1 <= r2.y1 &&
67                r1.y2 >= r2.y2);
68    }
69
70    public override bool Overlaps(TwoDimensionalPacking myPosition, TwoDimensionalPacking checkedPosition, PackingShape<TwoDimensionalPacking> checkedShape) {
71      return Overlaps(myPosition, checkedPosition, (RectangularPackingShape)checkedShape);
72    }
73    private bool Overlaps(TwoDimensionalPacking myPosition, TwoDimensionalPacking checkedPosition, RectangularPackingShape checkedShape) {   
74      return Overlaps(new RectangleDiagonal (myPosition, this),new RectangleDiagonal (checkedPosition, checkedShape));
75    }
76    private bool Overlaps(RectangleDiagonal r1, RectangleDiagonal r2) {
[9599]77      return !(r1.x1 > r2.x2 ||
78               r1.y1 > r2.y2 ||
79               r1.x2 < r2.x1 ||
80               r1.y2 < r2.y1);
[9348]81    }
82
83    public void ApplyHorizontalOrientation() {
84      if (Width < Height) {
85        var aux = Width;
86        Width = Height;
87        Height = aux;
88      }
89    }
90    #endregion
91
92    public RectangularPackingShape(int width, int height) : base () {
93      this.Height = height;
94      this.Width = width;
95    }
96
97    public override void InitializeFromMeasures(int[] measures) {
98      if (measures.Length != 2)
99        throw new InvalidOperationException("Nr of measures does not fit shape-dimension.");
100      this.Width = measures[0];
101      this.Height = measures[1];
102    }
103    public override int[] ToArray() {
104      return new int[] { Width, Height };
105    }
106
107    [StorableConstructor]
108    protected RectangularPackingShape(bool deserializing) : base(deserializing) { }
109    protected RectangularPackingShape(RectangularPackingShape original, Cloner cloner)
110      : base(original, cloner) {
111      this.Width = original.Width;
112      this.Height = original.Height;
113    }
114    public RectangularPackingShape() : base() {}
115
116    public override string ToString() {
117      return String.Format("RectangularPackingShape ({0}, {1})", this.Width, this.Height);
118    }
119
120    #region IComparable<RectangularPackingShape> Members
121
122    public int CompareTo(RectangularPackingShape other) {
123      int result = 0;// this.MultipliedMeasures.CompareTo(other.MultipliedMeasures);
124      if (result == 0) {
125        result = this.Width.CompareTo(other.Width);
126        if (result == 0)
127          result = this.Height.CompareTo(other.Height);
128      }
129      return result;
130    }
131
132    public int CompareTo(object obj) {
133      if (obj.GetType().Equals(this.GetType()))
134        return this.CompareTo((RectangularPackingShape)obj);
135      else return 0;
136    }
137
138    #endregion
139
140    private struct RectangleDiagonal {
141      public int x1;
142      public int y1;
143      public int x2;
144      public int y2;
145      public RectangleDiagonal(RectangularPackingShape myShape) : this(new TwoDimensionalPacking(0, 0, 0), myShape) { }
146      public RectangleDiagonal(TwoDimensionalPacking myPosition, RectangularPackingShape myShape) {
147        x1 = myPosition.X;
148        y1 = myPosition.Y;
[9599]149        x2 = myPosition.X + (myPosition.Rotated ? myShape.Height : myShape.Width) - 1;
150        y2 = myPosition.Y + (myPosition.Rotated ? myShape.Width : myShape.Height) - 1;
[9348]151      }
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.