Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: removed types for *PackingBin because PackingBins and PackingShapes have the same capabilities

File size: 7.5 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 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    public 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    public CuboidPackingShape(int width, int height, int depth)
76      : this() {
77      this.Width = width;
78      this.Height = height;
79      this.Depth = depth;
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new CuboidPackingShape(this, cloner);
84    }
85
86    [StorableHook(HookType.AfterDeserialization)]
87    private void AfterDeserialization() {
88      RegisterEvents();
89    }
90
91    private void RegisterEvents() {
92      // only because of ToString override
93      HeightParameter.Value.ValueChanged += (sender, args) => OnToStringChanged();
94      WidthParameter.Value.ValueChanged += (sender, args) => OnToStringChanged();
95      DepthParameter.Value.ValueChanged += (sender, args) => OnToStringChanged();
96    }
97
98    public override string ToString() {
99      return String.Format("CuboidPackingShape ({0}, {1}, {2})", this.Width, this.Height, this.Depth);
100    }
101
102    #region IComparable Members
103
104    public int CompareTo(CuboidPackingShape other) {
105      //Using "Clustered-Area-Height"-comparison as descr
106
107      int result = (this.Width * this.Depth).CompareTo(other.Width * other.Depth);
108
109      if (result == 0)
110        result = this.Volume.CompareTo(other.Volume);
111      if (result == 0)
112        result = this.Height.CompareTo(other.Height);
113      return result;
114    }
115
116    public override int CompareTo(object obj) {
117      var other = (CuboidPackingShape)obj;
118      if (other != null) return CompareTo(other);
119      else throw new ArgumentException(string.Format("Cannot compare with object {0}", obj), "obj");
120    }
121
122    #endregion
123
124    private struct CuboidDiagonal {
125      public int x1;
126      public int y1;
127      public int z1;
128      public int x2;
129      public int y2;
130      public int z2;
131      public CuboidDiagonal(CuboidPackingShape myShape) : this(new ThreeDimensionalPacking(0, 0, 0, 0), myShape) { }
132      public CuboidDiagonal(ThreeDimensionalPacking myPosition, CuboidPackingShape myShape) {
133        x1 = myPosition.X;
134        y1 = myPosition.Y;
135        z1 = myPosition.Z;
136        x2 = myPosition.X + (myPosition.Rotated ? myShape.Depth : myShape.Width) - 1;
137        y2 = myPosition.Y + myShape.Height - 1;
138        z2 = myPosition.Z + (myPosition.Rotated ? myShape.Width : myShape.Depth) - 1;
139      }
140    }
141
142
143    #region Helpers
144    public override ThreeDimensionalPacking Origin { get { return new ThreeDimensionalPacking(0, 0, 0, 0); } }
145    public override int Volume { get { return Width * Height * Depth; } }
146
147    public override bool EnclosesPoint(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPoint) {
148      return (myPosition.X <= checkedPoint.X &&
149                (myPosition.X + (myPosition.Rotated ? Depth : Width) - 1) >= checkedPoint.X &&
150                myPosition.Y <= checkedPoint.Y &&
151                (myPosition.Y + Height - 1) >= checkedPoint.Y &&
152                myPosition.Z <= checkedPoint.Z &&
153                (myPosition.Z + (myPosition.Rotated ? Width : Depth) - 1) >= checkedPoint.Z);
154    }
155    public override bool Encloses(ThreeDimensionalPacking checkedPosition, PackingShape<ThreeDimensionalPacking> checkedShape) {
156      return Encloses(checkedPosition, (CuboidPackingShape)checkedShape);
157    }
158    private bool Encloses(ThreeDimensionalPacking checkedPosition, CuboidPackingShape checkedShape) {
159      return Encloses(new CuboidDiagonal(this), new CuboidDiagonal(checkedPosition, checkedShape));
160    }
161    private bool Encloses(CuboidDiagonal c1, CuboidDiagonal c2) {
162      return (c1.x1 <= c2.x1 &&
163                c1.x2 >= c2.x2 &&
164                c1.y1 <= c2.y1 &&
165                c1.y2 >= c2.y2 &&
166                c1.z1 <= c2.z1 &&
167                c1.z2 >= c2.z2);
168    }
169
170    public override bool Overlaps(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPosition, PackingShape<ThreeDimensionalPacking> checkedShape) {
171      return Overlaps(myPosition, checkedPosition, (CuboidPackingShape)checkedShape);
172    }
173    private bool Overlaps(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPosition, CuboidPackingShape checkedShape) {
174      return Overlaps(new CuboidDiagonal(myPosition, this), new CuboidDiagonal(checkedPosition, checkedShape));
175    }
176    private bool Overlaps(CuboidDiagonal c1, CuboidDiagonal c2) {
177      return !(c1.x1 > c2.x2 ||
178               c1.y1 > c2.y2 ||
179               c1.z1 > c2.z2 ||
180               c1.x2 < c2.x1 ||
181               c1.y2 < c2.y1 ||
182               c1.z2 < c2.z1);
183    }
184
185    public override void ApplyHorizontalOrientation() {
186      if (Width > Depth) {
187        var aux = Width;
188        Width = Depth;
189        Depth = aux;
190      }
191    }
192    #endregion
193
194  }
195}
Note: See TracBrowser for help on using the repository browser.