Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Shapes/CuboidPackingShape.cs @ 11291

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

#1966: Bugfixing; Refactoring; Performancetuning;

File size: 7.0 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("CuboidPackingShape", "Represents the cuboid measures (width, height, depth) of a three-dimensional cuboidic bin-packing object.")]
34  [StorableClass]
35  public abstract class CuboidPackingShape : PackingShape<ThreeDimensionalPacking>, IRegularPackingShape, IComparable<CuboidPackingShape> {
36    #region Properties
37    /// <summary>
38    /// Describes the size on the X-axis
39    /// </summary>   
40    [Storable]
41    public int Width { get; set; }
42    /// <summary>
43    /// Describes the size on the Y-axis
44    /// </summary>   
45    [Storable]
46    public int Height { get; set; }
47    /// <summary>
48    /// Describes the size on the Z-axis
49    /// </summary> 
50    [Storable]
51    public int Depth { get; set; }
52    #endregion
53
54    #region Helpers
55    public override ThreeDimensionalPacking Origin { get { return new ThreeDimensionalPacking(0, 0, 0, 0); } }
56    public override int MultipliedMeasures { get { return Width * Height * Depth; } }
57
58    public override bool EnclosesPoint(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPoint) {
59      return (myPosition.X <= checkedPoint.X &&
60                (myPosition.X + (myPosition.Rotated ? Depth : Width) - 1) >= checkedPoint.X &&
61                myPosition.Y <= checkedPoint.Y &&
62                (myPosition.Y + Height - 1) >= checkedPoint.Y &&
63                myPosition.Z <= checkedPoint.Z &&
64                (myPosition.Z + (myPosition.Rotated ? Width : Depth) - 1) >= checkedPoint.Z);
65    }
66    public override bool Encloses(ThreeDimensionalPacking checkedPosition, PackingShape<ThreeDimensionalPacking> checkedShape) {
67      return Encloses(checkedPosition, (CuboidPackingShape) checkedShape);
68    }
69    private bool Encloses(ThreeDimensionalPacking checkedPosition, CuboidPackingShape checkedShape) {
70      return Encloses(new CuboidDiagonal(this), new CuboidDiagonal(checkedPosition, checkedShape));
71    } 
72    private bool Encloses(CuboidDiagonal c1, CuboidDiagonal c2) {
73      return (c1.x1 <= c2.x1 &&
74                c1.x2 >= c2.x2 &&
75                c1.y1 <= c2.y1 &&
76                c1.y2 >= c2.y2 &&
77                c1.z1 <= c2.z1 &&
78                c1.z2 >= c2.z2);
79    }
80
81    public override bool Overlaps(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPosition, PackingShape<ThreeDimensionalPacking> checkedShape) {
82      return Overlaps(myPosition, checkedPosition, (CuboidPackingShape)checkedShape);
83    }
84    private bool Overlaps(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPosition, CuboidPackingShape checkedShape) {
85      return Overlaps(new CuboidDiagonal(myPosition, this), new CuboidDiagonal(checkedPosition, checkedShape));
86    }
87    private bool Overlaps(CuboidDiagonal c1, CuboidDiagonal c2) {
88      return !(c1.x1 > c2.x2 ||
89               c1.y1 > c2.y2 || 
90               c1.z1 > c2.z2 ||
91               c1.x2 < c2.x1 ||
92               c1.y2 < c2.y1 ||
93               c1.z2 < c2.z1);
94    }
95
96    public void ApplyHorizontalOrientation() {
97      if (Width > Depth) {
98        var aux = Width;
99        Width = Depth;
100        Depth = aux;
101      }
102    }
103    #endregion       
104
105    public CuboidPackingShape(int width, int height, int depth) : base () {
106      this.Width = width;
107      this.Height = height;
108      this.Depth = depth;
109    }
110
111    public override void InitializeFromMeasures(int[] measures) {
112      if (measures.Length != 3)
113        throw new InvalidOperationException("Nr of measures does not fit shape-dimension.");       
114      this.Width = measures[0];
115      this.Height = measures[1];
116      this.Depth = measures[2];
117    }
118    public override int[] ToArray() {
119      return new int[] { Width, Height, Depth };
120    }
121
122    [StorableConstructor]
123    protected CuboidPackingShape(bool deserializing) : base(deserializing) { }
124    protected CuboidPackingShape(CuboidPackingShape original, Cloner cloner)
125      : base(original, cloner) {
126      this.Width = original.Width;
127      this.Height = original.Height;
128      this.Depth = original.Depth;
129    }
130    public CuboidPackingShape() : base(){ }
131
132    public override string ToString() {
133      return String.Format("CuboidPackingShape ({0}, {1}, {2})", this.Width, this.Height, this.Depth);
134    }
135
136    #region IComparable Members
137
138    public int CompareTo(CuboidPackingShape other) {
139      //Using "Clustered-Area-Height"-comparison as descr
140
141      int result = (this.Width * this.Depth).CompareTo (other.Width * other.Depth);
142   
143    if (result == 0)
144    result = this.MultipliedMeasures.CompareTo(other.MultipliedMeasures);
145      //if (result == 0) {
146      //  result = this.Depth.CompareTo(other.Depth) + this.Width.CompareTo(other.Width);
147      //  if (result == 0) {       
148      //    result = this.Width.CompareTo(other.Width);
149    if (result == 0)               
150    result = this.Height.CompareTo(other.Height);
151      //  }
152      //}
153      return result;
154    }       
155
156    public int CompareTo(object obj) {
157      if (obj.GetType().Equals(this.GetType()))
158        return this.CompareTo((CuboidPackingShape)obj);
159      else return 0;
160    }
161
162    #endregion
163
164    private struct CuboidDiagonal {
165      public int x1;
166      public int y1;
167      public int z1;
168      public int x2;
169      public int y2;
170      public int z2;
171      public CuboidDiagonal(CuboidPackingShape myShape) : this(new ThreeDimensionalPacking(0, 0, 0, 0), myShape) { }
172      public CuboidDiagonal(ThreeDimensionalPacking myPosition, CuboidPackingShape myShape) {
173        x1 = myPosition.X;
174        y1 = myPosition.Y;
175        z1 = myPosition.Z;
176        x2 = myPosition.X + (myPosition.Rotated ? myShape.Depth : myShape.Width) - 1;
177        y2 = myPosition.Y + myShape.Height - 1;
178        z2 = myPosition.Z + (myPosition.Rotated ? myShape.Width : myShape.Depth) - 1;
179      }
180    }
181  }
182}
Note: See TracBrowser for help on using the repository browser.