Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/08/16 20:42:24 (8 years ago)
Author:
gkronber
Message:

#1966 refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Shapes/CuboidPackingShape.cs

    r13574 r13605  
    2121
    2222using System;
    23 using System.Diagnostics;
    2423using HeuristicLab.Problems.BinPacking.Interfaces;
    2524using HeuristicLab.Core;
     
    3433  [StorableClass]
    3534  public abstract class CuboidPackingShape : PackingShape<ThreeDimensionalPacking>, IRegularPackingShape, IComparable<CuboidPackingShape> {
    36     #region Properties
     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
    3745    public int Height {
    38       get { return ((IFixedValueParameter<IntValue>)Parameters["Height"]).Value.Value; }
    39       set { ((IFixedValueParameter<IntValue>)Parameters["Height"]).Value.Value = value; }
     46      get { return HeightParameter.Value.Value; }
     47      set { HeightParameter.Value.Value = value; }
    4048    }
    4149
    4250    public int Width {
    43       get { return ((IFixedValueParameter<IntValue>)Parameters["Width"]).Value.Value; }
    44       set { ((IFixedValueParameter<IntValue>)Parameters["Width"]).Value.Value = value; }
     51      get { return WidthParameter.Value.Value; }
     52      set { WidthParameter.Value.Value = value; }
    4553    }
    4654
    4755    public int Depth {
    48       get { return ((IFixedValueParameter<IntValue>)Parameters["Depth"]).Value.Value; }
    49       set { ((IFixedValueParameter<IntValue>)Parameters["Depth"]).Value.Value = value; }
     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 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");
    50116    }
    51117
    52118    #endregion
    53119
     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
    54139    #region Helpers
    55140    public override ThreeDimensionalPacking Origin { get { return new ThreeDimensionalPacking(0, 0, 0, 0); } }
    56     public override int MultipliedMeasures { get { return Width * Height * Depth; } }
     141    public override int Volume { get { return Width * Height * Depth; } }
    57142
    58143    public override bool EnclosesPoint(ThreeDimensionalPacking myPosition, ThreeDimensionalPacking checkedPoint) {
     
    103188    #endregion
    104189
    105 
    106 
    107     public override void InitializeFromMeasures(int[] measures) {
    108       if (measures.Length != 3)
    109         throw new InvalidOperationException("Nr of measures does not fit shape-dimension.");
    110       this.Width = measures[0];
    111       this.Height = measures[1];
    112       this.Depth = measures[2];
    113     }
    114     public override int[] ToArray() {
    115       return new int[] { Width, Height, Depth };
    116     }
    117 
    118 
    119     protected CuboidPackingShape()
    120       : base() {
    121       Parameters.Add(new FixedValueParameter<IntValue>("Width"));
    122       Parameters.Add(new FixedValueParameter<IntValue>("Height"));
    123       Parameters.Add(new FixedValueParameter<IntValue>("Depth"));
    124     }
    125 
    126     protected CuboidPackingShape(int width, int height, int depth)
    127       : this() {
    128       this.Width = width;
    129       this.Height = height;
    130       this.Depth = depth;
    131     }
    132 
    133     [StorableConstructor]
    134     protected CuboidPackingShape(bool deserializing) : base(deserializing) { }
    135     protected CuboidPackingShape(CuboidPackingShape original, Cloner cloner)
    136       : base(original, cloner) {
    137     }
    138 
    139     public override string ToString() {
    140       return String.Format("CuboidPackingShape ({0}, {1}, {2})", this.Width, this.Height, this.Depth);
    141     }
    142 
    143     #region IComparable Members
    144 
    145     public int CompareTo(CuboidPackingShape other) {
    146       //Using "Clustered-Area-Height"-comparison as descr
    147 
    148       int result = (this.Width * this.Depth).CompareTo(other.Width * other.Depth);
    149 
    150       if (result == 0)
    151         result = this.MultipliedMeasures.CompareTo(other.MultipliedMeasures);
    152       if (result == 0)
    153         result = this.Height.CompareTo(other.Height);
    154       return result;
    155     }
    156 
    157     public int CompareTo(object obj) {
    158       var other = (CuboidPackingShape)obj;
    159       if (other != null) return CompareTo(other);
    160       else throw new ArgumentException(string.Format("Cannot compare with object {0}", obj), "obj");
    161     }
    162 
    163     #endregion
    164 
    165     private struct CuboidDiagonal {
    166       public int x1;
    167       public int y1;
    168       public int z1;
    169       public int x2;
    170       public int y2;
    171       public int z2;
    172       public CuboidDiagonal(CuboidPackingShape myShape) : this(new ThreeDimensionalPacking(0, 0, 0, 0), myShape) { }
    173       public CuboidDiagonal(ThreeDimensionalPacking myPosition, CuboidPackingShape myShape) {
    174         x1 = myPosition.X;
    175         y1 = myPosition.Y;
    176         z1 = myPosition.Z;
    177         x2 = myPosition.X + (myPosition.Rotated ? myShape.Depth : myShape.Width) - 1;
    178         y2 = myPosition.Y + myShape.Height - 1;
    179         z2 = myPosition.Z + (myPosition.Rotated ? myShape.Width : myShape.Depth) - 1;
    180       }
    181     }
    182190  }
    183191}
Note: See TracChangeset for help on using the changeset viewer.