Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9599 was 9599, checked in by jhelm, 11 years ago

#1966: Bugfixing; Refactoring; Performancetuning;

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