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