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

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

#1966:

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