Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking.3D/3.3/PackingShape.cs @ 14821

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

#1966: simplified class names

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