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

Last change on this file since 13497 was 13497, checked in by gkronber, 5 years ago

#1966: fixed various problems: bugs in cloning, bugs in persistence, method names, various minor improvements of source code for readability.

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