Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: Implemented new encoding (MultiComponentVector/MCV); Implemented move-operators for MCV and GV encodings; Implemented new decoding-methods for PS, GV and MCV encodings (ExtremePoint-based packing);

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