Skip to content

Commit 4783cc8

Browse files
committed
Refactor box rotation
1 parent bc9da5e commit 4783cc8

File tree

3 files changed

+52
-103
lines changed

3 files changed

+52
-103
lines changed

include/packingsolver/box/instance.hpp

Lines changed: 25 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ struct Box
5151

5252
/** Get the length of the largest size of the box. */
5353
Length max() const { return std::max(std::max(x, y), z); }
54+
55+
Box rotate(int rotation) const
56+
{
57+
switch (rotation) {
58+
case 0: {
59+
return {this->x, this->y, this->z};
60+
} case 1: {
61+
return {this->y, this->x, this->z};
62+
} case 2: {
63+
return {this->z, this->y, this->x};
64+
} case 3: {
65+
return {this->y, this->z, this->x};
66+
} case 4: {
67+
return {this->x, this->z, this->y};
68+
} case 5: {
69+
return {this->z, this->x, this->y};
70+
} default: {
71+
throw std::invalid_argument(
72+
FUNC_SIGNATURE + ": "
73+
"incorrect rotation value: '"
74+
+ std::to_string(rotation) + "'");
75+
}
76+
}
77+
}
78+
5479
};
5580

5681
std::ostream& operator<<(
@@ -102,78 +127,6 @@ struct ItemType
102127
* Computed attributes
103128
*/
104129

105-
Length x(int rotation) const
106-
{
107-
switch (rotation) {
108-
case 0: {
109-
return box.x;
110-
} case 1: {
111-
return box.y;
112-
} case 2: {
113-
return box.z;
114-
} case 3: {
115-
return box.y;
116-
} case 4: {
117-
return box.x;
118-
} case 5: {
119-
return box.z;
120-
} default: {
121-
throw std::invalid_argument(
122-
FUNC_SIGNATURE + ": "
123-
"incorrect rotation value: '"
124-
+ std::to_string(rotation) + "'");
125-
}
126-
}
127-
}
128-
129-
Length y(int rotation) const
130-
{
131-
switch (rotation) {
132-
case 0: {
133-
return box.y;
134-
} case 1: {
135-
return box.x;
136-
} case 2: {
137-
return box.y;
138-
} case 3: {
139-
return box.z;
140-
} case 4: {
141-
return box.z;
142-
} case 5: {
143-
return box.x;
144-
} default: {
145-
throw std::invalid_argument(
146-
FUNC_SIGNATURE + ": "
147-
"incorrect rotation value: '"
148-
+ std::to_string(rotation) + "'");
149-
}
150-
}
151-
}
152-
153-
Length z(int rotation) const
154-
{
155-
switch (rotation) {
156-
case 0: {
157-
return box.z;
158-
} case 1: {
159-
return box.z;
160-
} case 2: {
161-
return box.x;
162-
} case 3: {
163-
return box.x;
164-
} case 4: {
165-
return box.y;
166-
} case 5: {
167-
return box.y;
168-
} default: {
169-
throw std::invalid_argument(
170-
FUNC_SIGNATURE + ": "
171-
"incorrect rotation value: '"
172-
+ std::to_string(rotation) + "'");
173-
}
174-
}
175-
}
176-
177130
/** Get the volume of the item type. */
178131
inline Volume volume() const { return box.volume(); }
179132

src/box/branching_scheme.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -306,24 +306,22 @@ BranchingScheme::Node BranchingScheme::child_tmp(
306306
const BinType& bin_type = instance.bin_type(bin_type_id);
307307
const ItemType& item_type = instance.item_type(insertion.item_type_id);
308308

309-
Length xj = item_type.x(insertion.rotation);
310-
Length yj = item_type.y(insertion.rotation);
311-
Length zj = item_type.z(insertion.rotation);
309+
Box box = item_type.box.rotate(insertion.rotation);
312310
Length xs = insertion.x;
313311
Length ys = insertion.y;
314312
Length zs = insertion.z;
315-
Length xe = xs + xj;
316-
Length ye = ys + yj;
317-
Length ze = zs + zj;
313+
Length xe = xs + box.x;
314+
Length ye = ys + box.y;
315+
Length ze = zs + box.z;
318316
Length xi = bin_type.box.x;
319317
Length yi = bin_type.box.y;
320318
Length zi = bin_type.box.z;
321-
Volume item_volume = xj * yj * zj;
322-
if (insertion.z + zj > zi) {
319+
Volume item_volume = box.volume();
320+
if (insertion.z + box.z > zi) {
323321
throw std::runtime_error(
324322
FUNC_SIGNATURE + "; "
325323
"insertion.z: " + std::to_string(insertion.z)
326-
+ "; zj: " + std::to_string(zj)
324+
+ "; zj: " + std::to_string(box.z)
327325
+ "; zi: " + std::to_string(zi)
328326
+ ".");
329327
}
@@ -609,9 +607,7 @@ void BranchingScheme::insertion_item(
609607
instance.bin_type_id(parent->number_of_bins - 1):
610608
instance.bin_type_id(parent->number_of_bins);
611609
const BinType& bin_type = instance.bin_type(bin_type_id);
612-
Length xj = item_type.x(rotation);
613-
Length yj = item_type.y(rotation);
614-
Length zj = item_type.z(rotation);
610+
Box box = item_type.box.rotate(rotation);
615611
Length xi = bin_type.box.x;
616612
Length yi = bin_type.box.y;
617613
Length zi = bin_type.box.z;
@@ -624,8 +620,8 @@ void BranchingScheme::insertion_item(
624620
ys = 0;
625621
zs = 0;
626622
}
627-
Length ye = ys + yj;
628-
Length ze = zs + zj;
623+
Length ye = ys + box.y;
624+
Length ze = zs + box.z;
629625
// Check bin y.
630626
if (ye > yi) {
631627
//std::cout << "ye " << ye << " > yi " << yi << std::endl;
@@ -668,8 +664,8 @@ void BranchingScheme::insertion_item(
668664
// Compute xs.
669665
Length xs = 0;
670666
if (new_bin == 0) {
671-
xs = std::max(xs, parent->y_uncovered_items[y_uncovered_item_pos].xs - xj);
672-
xs = std::max(xs, parent->z_uncovered_items[z_uncovered_item_pos].xs - xj);
667+
xs = std::max(xs, parent->y_uncovered_items[y_uncovered_item_pos].xs - box.x);
668+
xs = std::max(xs, parent->z_uncovered_items[z_uncovered_item_pos].xs - box.x);
673669
for (const UncoveredItem& uncovered_item: parent->uncovered_items) {
674670
if (uncovered_item.ye <= ys || uncovered_item.ys >= ye)
675671
continue;
@@ -679,7 +675,7 @@ void BranchingScheme::insertion_item(
679675
xs = uncovered_item.x;
680676
}
681677
}
682-
Length xe = xs + xj;
678+
Length xe = xs + box.x;
683679

684680
// Check bin x.
685681
if (xe > xi) {
@@ -921,11 +917,12 @@ nlohmann::json BranchingScheme::json_export_init() const
921917
continue;
922918

923919
json_items_init_ids_[item_type_id][rotation] = i;
920+
Box box = item_type.box.rotate(rotation);
924921
json_init[i] = {
925922
{"Type", "Box"},
926-
{"X", item_type.x(rotation)},
927-
{"Y", item_type.y(rotation)},
928-
{"Z", item_type.z(rotation)},
923+
{"X", box.x},
924+
{"Y", box.y},
925+
{"Z", box.z},
929926
{"FillColor", "blue"},
930927
};
931928
i++;

src/box/solution.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,10 @@ void Solution::add_item(
6363
const BinType& bin_type = instance().bin_type(bin.bin_type_id);
6464

6565
const ItemType& item_type = instance().item_type(item_type_id);
66-
Length xj = item_type.x(rotation);
67-
Length yj = item_type.y(rotation);
68-
Length zj = item_type.z(rotation);
69-
Length xe = bl_corner.x + xj;
70-
Length ye = bl_corner.y + yj;
71-
Length ze = bl_corner.z + zj;
66+
Box box = item_type.box.rotate(rotation);
67+
Length xe = bl_corner.x + box.x;
68+
Length ye = bl_corner.y + box.y;
69+
Length ze = bl_corner.z + box.z;
7270
//std::cout
7371
// << "j " << j
7472
// << " x " << stack.x_start
@@ -86,8 +84,8 @@ void Solution::add_item(
8684
"item_type_id: " + std::to_string(item_type_id) + "; "
8785
"item_type.rotations: " + std::to_string(item_type.rotations) + "; "
8886
"rot: " + std::to_string(rotation) + "; "
89-
"xj: " + std::to_string(xj) + "; "
90-
"yj: " + std::to_string(yj) + ".");
87+
"xj: " + std::to_string(box.x) + "; "
88+
"yj: " + std::to_string(box.y) + ".");
9189
}
9290

9391
SolutionItem item;
@@ -312,6 +310,7 @@ void Solution::write(
312310

313311
for (const SolutionItem& item: bin.items) {
314312
const ItemType& item_type = instance().item_type(item.item_type_id);
313+
Box box = item_type.box.rotate(item.rotation);
315314
file
316315
<< "ITEM,"
317316
<< item.item_type_id << ","
@@ -320,9 +319,9 @@ void Solution::write(
320319
<< item.bl_corner.x << ","
321320
<< item.bl_corner.y << ","
322321
<< item.bl_corner.z << ","
323-
<< item_type.x(item.rotation) << ","
324-
<< item_type.y(item.rotation) << ","
325-
<< item_type.z(item.rotation) << ","
322+
<< box.x << ","
323+
<< box.y << ","
324+
<< box.z << ","
326325
<< item.rotation << std::endl;
327326
}
328327
}

0 commit comments

Comments
 (0)