Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/CuboidPackingShape.cs @ 14043

Last change on this file since 14043 was 14043, checked in by gkronber, 8 years ago

#1966: removed interface IRegularPacking shape (=> all our packing shapes are regular)

File size: 7.4 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.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Problems.BinPacking.Dimensions;
30
31namespace HeuristicLab.Problems.BinPacking.Shapes {
32  [Item("CuboidPackingShape", "Represents the cuboid measures (width, height, depth) of a three-dimensional cuboidic bin-packing object.")]
33  [StorableClass]
34  public abstract class CuboidPackingShape : PackingShape<ThreeDimensionalPacking>, IComparable<CuboidPackingShape> {
35    public IFixedValueParameter<IntValue> HeightParameter {
36      get { return (IFixedValueParameter<IntValue>)Parameters["Height"]; }
37    }
38    public IFixedValueParameter<IntValue> WidthParameter {
39      get { return (IFixedValueParameter<IntValue>)Parameters["Width"]; }
40    }
41    public IFixedValueParameter<IntValue> DepthParameter {
42      get { return (IFixedValueParameter<IntValue>)Parameters["Depth"]; }
43    }
44
45    public int Height {
46      get { return HeightParameter.Value.Value; }
47      set { HeightParameter.Value.Value = value; }
48    }
49
50    public int Width {
51      get { return WidthParameter.Value.Value; }
52      set { WidthParameter.Value.Value = value; }
53    }
54
55    public int Depth {
56      get { return DepthParameter.Value.Value; }
57      set { DepthParameter.Value.Value = value; }
58    }
59
60    [StorableConstructor]
61    protected CuboidPackingShape(bool deserializing) : base(deserializing) { }
62    protected CuboidPackingShape(CuboidPackingShape original, Cloner cloner)
63      : base(original, cloner) {
64      RegisterEvents();
65    }
66    protected CuboidPackingShape()
67      : base() {
68      Parameters.Add(new FixedValueParameter<IntValue>("Width"));
69      Parameters.Add(new FixedValueParameter<IntValue>("Height"));
70      Parameters.Add(new FixedValueParameter<IntValue>("Depth"));
71
72      RegisterEvents();
73    }
74
75    protected CuboidPackingShape(int width, int height, int depth)
76      : this() {
77      this.Width = width;
78      this.Height = height;
79      this.Depth = depth;
80    }
81
82    [StorableHook(HookType.AfterDeserialization)]
83    private void AfterDeserialization() {
84      RegisterEvents();
85    }
86
87    private void RegisterEvents() {
88      // only because of ToString override
89      HeightParameter.Value.ValueChanged += (sender, args) => OnToStringChanged();
90      WidthParameter.Value.ValueChanged += (sender, args) => OnToStringChanged();
91      DepthParameter.Value.ValueChanged += (sender, args) => OnToStringChanged();
92    }
93
94    public override string ToString() {
95      return String.Format("CuboidPackingShape ({0}, {1}, {2})", this.Width, this.Height, this.Depth);
96    }
97
98    #region IComparable Members
99
100    public int CompareTo(CuboidPackingShape other) {
101      //Using "Clustered-Area-Height"-comparison as descr
102
103      int result = (this.Width * this.Depth).CompareTo(other.Width * other.Depth);
104
105      if (result == 0)
106        result = this.Volume.CompareTo(other.Volume);
107      if (result == 0)
108        result = this.Height.CompareTo(other.Height);
109      return result;
110    }
111
112    public override int CompareTo(object obj) {
113      var other = (CuboidPackingShape)obj;
114      if (other != null) return CompareTo(other);
115      else throw new ArgumentException(string.Format("Cannot compare with object {0}", obj), "obj");
116    }
117
118    #endregion
119
120    private struct CuboidDiagonal {
121      public int x1;
122      public int y1;
123      public int z1;
124      public int x2;
125      public int y2;
126      public int z2;
127      public CuboidDiagonal(CuboidPackingShape myShape) : this(new ThreeDimensionalPacking(0, 0, 0, 0), myShape) { }
128      public CuboidDiagonal(ThreeDimensionalPacking myPosition, CuboidPackingShape myShape) {
129        x1 = myPosition.X;
130        y1 = myPosition.Y;
131        z1 = myPosition.Z;
132        x2 = myPosition.X + (myPosition.Rotated ? myShape.Depth : myShape.Width) - 1;
133        y2 = myPosition.Y + myShape.Height - 1;
134        z2 = myPosition.Z + (myPosition.Rotated ? myShape.Width : myShape.Depth) - 1;
135      }
136    }
137
138
139    #region Helpers
140    public override ThreeDimensionalPacking Origin { get { return new ThreeDimensionalPacking(0, 0, 0, 0); } }
141    public override int Volume { get { return Width * Height * Depth; } }
142
143    public override bool EnclosesPoint(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPoint) {
144      return (myPosition.X <= checkedPoint.X &&
145                (myPosition.X + (myPosition.Rotated ? Depth : Width) - 1) >= checkedPoint.X &&
146                myPosition.Y <= checkedPoint.Y &&
147                (myPosition.Y + Height - 1) >= checkedPoint.Y &&
148                myPosition.Z <= checkedPoint.Z &&
149                (myPosition.Z + (myPosition.Rotated ? Width : Depth) - 1) >= checkedPoint.Z);
150    }
151    public override bool Encloses(ThreeDimensionalPacking checkedPosition, PackingShape<ThreeDimensionalPacking> checkedShape) {
152      return Encloses(checkedPosition, (CuboidPackingShape)checkedShape);
153    }
154    private bool Encloses(ThreeDimensionalPacking checkedPosition, CuboidPackingShape checkedShape) {
155      return Encloses(new CuboidDiagonal(this), new CuboidDiagonal(checkedPosition, checkedShape));
156    }
157    private bool Encloses(CuboidDiagonal c1, CuboidDiagonal c2) {
158      return (c1.x1 <= c2.x1 &&
159                c1.x2 >= c2.x2 &&
160                c1.y1 <= c2.y1 &&
161                c1.y2 >= c2.y2 &&
162                c1.z1 <= c2.z1 &&
163                c1.z2 >= c2.z2);
164    }
165
166    public override bool Overlaps(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPosition, PackingShape<ThreeDimensionalPacking> checkedShape) {
167      return Overlaps(myPosition, checkedPosition, (CuboidPackingShape)checkedShape);
168    }
169    private bool Overlaps(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPosition, CuboidPackingShape checkedShape) {
170      return Overlaps(new CuboidDiagonal(myPosition, this), new CuboidDiagonal(checkedPosition, checkedShape));
171    }
172    private bool Overlaps(CuboidDiagonal c1, CuboidDiagonal c2) {
173      return !(c1.x1 > c2.x2 ||
174               c1.y1 > c2.y2 ||
175               c1.z1 > c2.z2 ||
176               c1.x2 < c2.x1 ||
177               c1.y2 < c2.y1 ||
178               c1.z2 < c2.z1);
179    }
180
181    public override void ApplyHorizontalOrientation() {
182      if (Width > Depth) {
183        var aux = Width;
184        Width = Depth;
185        Depth = aux;
186      }
187    }
188    #endregion
189
190  }
191}
Note: See TracBrowser for help on using the repository browser.