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