Calc.pm

filter: Statementstmt 89.9% 223 / 248 Branchbran 76.0% 35 / 46 Conditioncond 40.0% 22 / 55 MC/DCmcdc 5.8% 2 / 34 Subroutinesub 91.1% 31 / 34 Podpod 100% 19 / 19 totaltotal 76.1% 332 / 436 SCAR 44.1
CC
57
Cov
80%
sort_items_by_zxy
20.0
pack_items
15.3
sort_items_by_z_desc_A
12.0
CRAP 82.1
↑ Summary Calc/Layer.pm »
1 package Box::Calc;
2 7 $Box::Calc::VERSION = '1.0201';
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Box::Calc::BoxType;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Box::Calc::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Box::Calc::Box;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use List::MoreUtils qw(natatime);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Box::Calc - Packing Algorithm
16  
17 =head1 VERSION
18  
19 version 1.0201
20  
21 =head1 SYNOPSIS
22  
23 use Box::Calc;
24
25 my $box_calc = Box::Calc->new;
26
27 # define the possible box types
28 $box_calc->add_box_type( x => 12, y => 12, z => 18, weight => 16, name => 'big box' );
29 $box_calc->add_box_type( x => 4, y => 6, z => 8, weight => 6, name => 'small box' );
30  
31 # define the items you want to put into boxes
32 $box_calc->add_item( 3, { x => 6, y => 3, z => 3, weight => 12, name => 'soda' });
33 $box_calc->add_item( 1, { x => 3.3, y => 3, z => 4, weight => 4.5, name => 'apple' });
34 $box_calc->add_item( 2, { x => 8, y => 2.5, z => 2.5, weight => 14, name => 'water bottle' });
35  
36 # figure out what you need to pack this stuff
37 $box_calc->pack_items;
38
39 # how many boxes do you need
40 my $box_count = $box_calc->count_boxes; # 2
41
42 # interrogate the boxes
43 my $box = $box_calc->get_box(-1); # the last box
44 my $weight = $box->calculate_weight;
45
46 # get a packing list
47 my $packing_list = $box_calc->packing_list;
48
49 =head1 DESCRIPTION
50  
51 Box::Calc helps you determine what can fit into a box for shipping or storage purposes. It will try to use the smallest box possible of the box types. If every item won't fit into your largest box, then it will span the boxes letting you know how many boxes you'll need.
52  
53 Once it's done packing the boxes, you can get a packing list for each box, as well as the weight of each box.
54  
55 =head2 How The Algorithm Works
56  
57 Box::Calc is intended to pack boxes in the simplest way possible. Here's what it does:
58  
59 =over
60  
61 =item 1
62  
63 Sort all the items by volume.
64  
65 =item 2
66  
67 Eliminate all boxes that won't fit the largest items.
68  
69 =item 3
70  
71 Choose the smallest box still available.
72  
73 =item 4
74  
75 Place the items in a row starting with the largest items.
76  
77 =item 5
78  
79 When the row runs out of space, add another.
80  
81 =item 6
82  
83 When you run out of space to add rows, add a layer.
84  
85 =item 7
86  
87 When you run out of layers either start over with a bigger box, or if there are no bigger boxes span to a second box.
88  
89 =item 8
90  
91 Repeat from step 3 until all items are packed into boxes.
92  
93 =back
94  
95 =head2 Motivation
96  
97 At The Game Crafter (L<http://www.thegamecrafter.com>) we ship a lot of games and game pieces. We tried using a more complicated system for figuring out which size box to use, or how many boxes would be needed in a spanning situation. The problem was that those algorithms made the boxes pack so tightly that our staff spent a lot more time putting the boxes together. This algorithm is relatively dumb, but dumb in a good way. The boxes are easy and fast to pack. By releasing this, we hope it can help those who are either using too complicated a system, or no system at all for figuring out how many boxes they need for shipping/storing materials.
98  
99 =head2 Tips
100  
101 When adding items, be sure to use the outer most dimensions of oddly shaped items, otherwise they may not fit the box.
102  
103 When adding box types, be sure to use the inside dimensions of the box. If you plan to line the box with padding, then subtract the padding from the dimensions, and also add the padding to the weight of the box.
104  
105 What units you use (inches, centimeters, ounces, pounds, grams, kilograms, etc) don't matter as long as you use them consistently.
106  
107 =head1 METHODS
108  
109 =head2 new()
110  
111 Constructor.
112  
113 =head2 box_types()
114  
115 Returns an array reference of the L<Box::Calc::BoxType>s registered.
116  
117 =head2 count_box_types()
118  
119 Returns the number of L<Box::Calc::BoxType>s registered.
120  
121 =head2 get_box_type(index)
122  
123 Returns a specific L<Box::Calc::BoxType> from the list of C<box_types>
124  
125 =over
126  
127 =item index
128  
129 An array index. For example this would return the last box type added:
130  
131 $box_calc->get_box_type(-1)
132  
133 =back
134  
135 =cut
136  
137 has box_types => (
138 is => 'rw',
139 isa => 'ArrayRef[Box::Calc::BoxType]',
140 8 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
141 7 traits => ['Array'],
142 handles => {
143 push_box_types => 'push',
144 count_box_types => 'count',
145 get_box_type => 'get',
146 }
147 );
148  
149 =head2 add_box_type(params)
150  
151 Adds a new L<Box::Calc::BoxType> to the list of C<box_types>. Returns the newly created L<Box::Calc::BoxType> instance.
152  
153 =over
154  
155 =item params
156  
157 The list of constructor parameters for L<Box::Calc::BoxType>.
158  
159 B<NOTE:> You can optionally include an argument of "categories" and a box type will be created for each category so you don't have to do it manually.
160  
161 =back
162  
163 =cut
164  
165 sub add_box_type {
166 28 ▶ my $self = shift;
Subroutine add_box_type: called CC=4 SCAR=13.9
Pod documented
167 28 my $args;
168 28 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 10 18 28
169 10 $args = shift;
170 }
171 else {
172 18 $args = { @_ };
173 }
174 28 my $categories = delete $args->{categories};
175 28 ▶ if (defined $categories) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $categories) { } 3 25 28
176 3 foreach my $category (@{$categories}) {
177 4 my %copy = %{$args};
178 4 $copy{category} = $category;
179 4 $self->push_box_types(Box::Calc::BoxType->new(%copy));
180 }
181 }
182 else {
183 25 $self->push_box_types(Box::Calc::BoxType->new($args));
184 }
185 28 return $self->get_box_type(-1);
186 }
187  
188  
189 =head2 box_type_categories()
190  
191 Returns an array reference of categories associated with the box types.
192  
193 =cut
194  
195 has box_type_categories => (
196 is => 'rw',
197 lazy => 1,
198 isa => 'ArrayRef',
199 default => sub {
200 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
201 1 my %categories = ();
202 1 foreach my $box_type (@{$self->box_types}) {
203 7 ▶ next if $box_type->category eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $box_type->category eq "" 3 4 7
204 4 $categories{$box_type->category} = 1;
205 }
206 1 return [sort keys %categories];
207 },
208 7 );
209  
210  
211 =head2 sort_box_types_by_volume()
212  
213 Sorts the list of C<box_types> by volume and then returns an array reference of that list.
214  
215 =over
216  
217 =item types
218  
219 Optional. Array ref of box types. Will call C<box_types> if not passed in.
220  
221 =back
222  
223 =cut
224  
225 sub sort_box_types_by_volume {
226 17 ▶ my $self = shift;
Subroutine sort_box_types_by_volume: called CC=2 SCAR=7.1
Pod documented
227 17 ▶ my $types = shift || $self->box_types;
Condition33%1/3
shift() || $self->box_types
l!l&&r!l&&!r
0170
MC/DC0%0/2
shift() || $self->box_types
shift() $self->box_types
Truth table33%1/3
A || B
A= shift()B= $self->box_types
ABresult
1X1
011
000
228 17 my @sorted = sort { ($a->volume) <=> ($b->volume ) } @{$types};
229 17 return \@sorted;
230 }
231  
232 =head2 determine_viable_box_types(category)
233  
234 Given the list of C<items> and the list of C<box_types> this method rules out box types that cannot hold the largest item, and returns the list of box types that will work sorted by volume.
235  
236 =over
237  
238 =item category
239  
240 Optional. If this is specified, it will match this category name to the categories attached to the boxes and only provide a list of boxes that match that category.
241  
242 =back
243  
244 =cut
245  
246 sub determine_viable_box_types {
247 16 ▶ my ($self, $category) = @_;
Subroutine determine_viable_box_types: called CC=8 SCAR=20.8
Pod documented
248 16 my ($item_x, $item_y, $item_z) = sort {$b <=> $a} @{$self->find_max_dimensions_of_items};
249 16 my @viable;
250 16 foreach my $box_type (@{$self->sort_box_types_by_volume}) {
251 52 ▶ if (defined $category) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $category) 18 34 52
252 18 ▶ next unless $category eq $box_type->category;
Branch: 1 branch
BranchTrueFalseTotal
unless $category eq $box_type->category 13 5 18
253 }
254 39 my ($box_type_x, $box_type_y, $box_type_z) = @{$box_type->dimensions};
255 39 ▶ if ($item_x <= $box_type_x && $item_y <= $box_type_y && $item_z <= $box_type_z) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item_x <= $box_type_x and $item_y <= $box_type_y and $item_z <= $box_type_z) 33 6 39
Condition66%4/6
$item_x <= $box_type_x and $item_y <= $box_type_y and $item_z <= $box_type_z
!ll&&!rl&&r
6033
$item_x <= $box_type_x and $item_y <= $box_type_y
!ll&&!rl&&r
6033
MC/DC33%1/3
$item_x <= $box_type_x and $item_y <= $box_type_y and $item_z <= $box_type_z
$item_x <= $box_type_x $item_y <= $box_type_y $item_z <= $box_type_z
Truth table50%2/4
A and B and C
A= $item_x <= $box_type_xB= $item_y <= $box_type_yC= $item_z <= $box_type_z
ABCresult
0XX0
10X0
1100
1111
256 33 push @viable, $box_type;
257 }
258 }
259 16 ▶ unless (scalar @viable) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @viable) 1 15 16
260 1 $log->fatal('There are no box types that can fit the items.');
261 1 ouch 'no viable box types', 'There are no box types that can fit the items. ('.join(', ', $item_x, $item_y, $item_z).')', [$item_x, $item_y, $item_z];
262 }
263 15 return \@viable;
264 }
265  
266 =head2 items()
267  
268 Returns an array reference of the L<Box::Calc::Item>s registered.
269  
270 =head2 count_items()
271  
272 Returns the number of L<Box::Calc::Item>s registered.
273  
274 =head2 get_item(index)
275  
276 Returns a specific L<Box::Calc::Item>.
277  
278 =over
279  
280 =item index
281  
282 The array index of the item as it was registered.
283  
284 =back
285  
286 =cut
287  
288 has items => (
289 is => 'rw',
290 isa => 'ArrayRef[Box::Calc::Item]',
291 8 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
292 7 traits => ['Array'],
293 handles => {
294 push_items => 'push',
295 count_items => 'count',
296 get_item => 'get',
297 }
298 );
299  
300 =head2 add_item(quantity, params)
301  
302 Registers a new item. Returns the new item registered.
303  
304 =over
305  
306 =item quantity
307  
308 How many copies of this item should be included in the package?
309  
310 =item params
311  
312 The constructor parameters for the L<Box::Calc::Item>.
313  
314 =back
315  
316 =cut
317  
318 sub add_item {
319 26 ▶ my ($self, $quantity, @params) = @_;
Subroutine add_item: called CC=2 SCAR=6.9
Pod documented
320 26 my $item = Box::Calc::Item->new(@params);
321 26 for (1..$quantity) {
322 10589 $self->push_items($item);
323 }
324 26 return $self->get_item(-1);
325 }
326  
327 =head2 load(payload)
328  
329 Allows the loading of an entire dataset.
330  
331 =over
332  
333 =item payload
334  
335 A hash reference containing the output of the C<dump> method, with two exceptions:
336  
337 =over
338  
339 =item *
340  
341 You can create a C<categories> element that is an array ref in each box type rather than creating duplicate box types for each category.
342  
343 =item *
344  
345 You can create a C<quantity> element in each item rather than creating duplicate items to represent the quantity.
346  
347 =back
348  
349 =back
350  
351 =cut
352  
353 sub load {
354 1 ▶ my ($self, $payload) = @_;
Subroutine load: called CC=4 SCAR=13.9
Pod documented
355 # note that we copy the box type and item to avoid modifying the original
356 1 foreach my $type (@{$payload->{box_types}}) {
357 7 $self->add_box_type(%{$type});
358 }
359 1 foreach my $item (@{$payload->{items}}) {
360 6 ▶ $self->add_item($item->{quantity} || 1, %{$item});
Condition50%1/2
$item->{'quantity'} || 1
l!l
06
MC/DC0%0/1
$item->{'quantity'} || 1
$item->{'quantity'}
Truth table50%1/2
A
A= $item->{'quantity'}
Aresult
11
00
361 }
362 }
363  
364 =head2 dump()
365  
366 =cut
367  
368 sub dump {
369 2 ▶ my ($self) = @_;
Subroutine dump: called CC=3 SCAR=11.0
Pod documented
370 2 my $payload = {};
371 2 foreach my $type (@{$self->box_types}) {
372 14 push @{$payload->{box_types}}, $type->describe;
373 }
374 2 foreach my $item (@{$self->items}) {
375 12 push @{$payload->{items}}, $item->describe;
376 }
377 2 return $payload;
378 }
379  
380  
381 =head2 sort_items_by_volume()
382  
383 Returns an array reference of the list of C<items> registered sorted by volume.
384  
385 =over
386  
387 =item items
388  
389 Optional. An array reference of items. Will call C<items> if not passed in.
390  
391 =back
392  
393 =cut
394  
395 sub sort_items_by_volume {
396 1 ▶ my $self = shift;
Subroutine sort_items_by_volume: called CC=2 SCAR=7.1
Pod documented
397 1 ▶ my $items = shift || $self->items;
Condition33%1/3
shift() || $self->items
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $self->items
shift() $self->items
Truth table33%1/3
A || B
A= shift()B= $self->items
ABresult
1X1
011
000
398 1 my @sorted = sort { ($a->volume) <=> ($b->volume ) } @{$items};
399 1 return \@sorted;
400 }
401  
402 =head2 sort_items_by_zxy()
403  
404 Returns an array reference of the list of C<items> registered sorted by z, then x, then y, ascending.
405  
406 =over
407  
408 =item items
409  
410 Optional. An array reference of items. Will call C<items> if not passed in.
411  
412 =back
413  
414 =cut
415  
416 sub sort_items_by_zxy {
417 0 ▶ my $self = shift;
Subroutine sort_items_by_zxy: not called CC=4 SCAR=30.0
Pod documented
418 0 ▶ my $items = shift || $self->items;
Condition0%0/3
shift() || $self->items
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->items
shift() $self->items
Truth table0%0/3
A || B
A= shift()B= $self->items
ABresult
1X1
011
000
419 my @sorted = sort {
420 0 ▶ $a->z <=> $b->z
Branch: 1 branch
BranchTrueFalseTotal
$a->z <=> $b->z or $a->x <=> $b->x or $a->y <=> $b->y 0 0 0
Condition0%0/3
$a->z <=> $b->z or $a->x <=> $b->x
l!l&&r!l&&!r
000
MC/DC0%0/2
$a->z <=> $b->z or $a->x <=> $b->x
$a->z <=> $b->z $a->x <=> $b->x
Truth table0%0/3
A or B
A= $a->z <=> $b->zB= $a->x <=> $b->x
ABresult
1X1
011
000
421 || $a->x <=> $b->x
422 || $a->y <=> $b->y
423 0 } @{$items};
424 0 return \@sorted;
425 }
426  
427 =head2 sort_items_by_z_desc_A()
428  
429 Returns an array reference of the list of C<items> registered sorted by z DESC, then area DESC
430  
431 =over
432  
433 =item items
434  
435 Optional. An array reference of items. Will call C<items> if not passed in.
436  
437 =back
438  
439 =cut
440  
441 sub sort_items_by_z_desc_A {
442 0 ▶ my $self = shift;
Subroutine sort_items_by_z_desc_A: not called CC=3 SCAR=24.8
Pod documented
443 0 ▶ my $items = shift || $self->items;
Condition0%0/3
shift() || $self->items
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->items
shift() $self->items
Truth table0%0/3
A || B
A= shift()B= $self->items
ABresult
1X1
011
000
444 0 my @sorted = map { $_->[1] }
445 sort {
446 $b->[0]->{z} <=> $a->[0]->{z}
447 || $b->[0]->{A} <=> $a->[0]->{A}
448 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$Box::Calc::b->[0]{"z"} <=> $Box::Calc::a->[0]{"z"} or $Box::Calc::b->[0]{"A"} <=> $Box::Calc::a->[0]{"A"} 0 0 0
449 ##Fetch Z and calculate A
450 0 map { [ { z=>$_->z, A=>$_->x*$_->y }, $_ ] } @{$items};
451 0 return \@sorted;
452 }
453  
454 =head2 sort_items_by_zA()
455  
456 Returns an array reference of the list of C<items> registered sorted by z ASC, then area DESC
457  
458 =over
459  
460 =item items
461  
462 Optional. An array reference of items. Will call C<items> if not passed in.
463  
464 =back
465  
466 =cut
467  
468 sub sort_items_by_zA {
469 10 ▶ my $self = shift;
Subroutine sort_items_by_zA: called CC=3 SCAR=11.0
Pod documented
470 10 ▶ my $items = shift || $self->items;
Condition66%2/3
shift() || $self->items
l!l&&r!l&&!r
190
MC/DC0%0/2
shift() || $self->items
shift() $self->items
Truth table66%2/3
A || B
A= shift()B= $self->items
ABresult
1X1
011
000
471 10552 my @sorted = map { $_->[1] }
472 sort {
473 $a->[0]->{z} <=> $b->[0]->{z}
474 || $b->[0]->{A} <=> $a->[0]->{A}
475 10642 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$Box::Calc::a->[0]{"z"} <=> $Box::Calc::b->[0]{"z"} or $Box::Calc::b->[0]{"A"} <=> $Box::Calc::a->[0]{"A"} 10593 49 10642
476 ##Fetch Z and calculate A
477 10 map { [ { z=>$_->z, A=>$_->x*$_->y }, $_ ] } @{$items};
478 10 return \@sorted;
479 }
480  
481 =head2 sort_items_by_Az()
482  
483 =over
484  
485 =item items
486  
487 Optional. An array reference of items. Will call C<items> if not passed in.
488  
489 =back
490  
491 Returns an array reference of the list of C<items> registered sorted by A DESC, then z ASC
492  
493 =cut
494  
495 sub sort_items_by_Az {
496 0 ▶ my $self = shift;
Subroutine sort_items_by_Az: not called CC=3 SCAR=24.8
Pod documented
497 0 ▶ my $items = shift || $self->items;
Condition0%0/3
shift() || $self->items
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->items
shift() $self->items
Truth table0%0/3
A || B
A= shift()B= $self->items
ABresult
1X1
011
000
498 0 my @sorted = map { $_->[1] }
499 sort {
500 $b->[0]->{A} <=> $a->[0]->{A}
501 || $a->[0]->{z} <=> $b->[0]->{z}
502 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$Box::Calc::b->[0]{"A"} <=> $Box::Calc::a->[0]{"A"} or $Box::Calc::a->[0]{"z"} <=> $Box::Calc::b->[0]{"z"} 0 0 0
503 ##Fetch Z and calculate A
504 0 map { [ { z=>$_->z, A=>$_->x*$_->y }, $_ ] } @{$items};
505 0 return \@sorted;
506 }
507  
508 =head2 find_max_dimensions_of_items()
509  
510 Given the registered C<items>, returns the max C<x>, C<y>, and C<z> of all items registered as an array reference.
511  
512 =cut
513  
514 has find_max_dimensions_of_items => (
515 is => 'rw',
516 lazy => 1,
517 isa => 'ArrayRef',
518 clearer => 'clear_max_dimensions_of_items',
519 default => sub {
520 8 ▶ my $self = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
521 8 my $x = 0;
522 8 my $y = 0;
523 8 my $z = 0;
524 8 foreach my $item (@{$self->items}) {
525 10555 my ($ex, $ey, $ez) = @{$item->dimensions};
526 10555 ▶ $x = $ex if $ex > $x;
Branch: 1 branch
BranchTrueFalseTotal
if $ex > $x 10 10545 10555
527 10555 ▶ $y = $ey if $ey > $y;
Branch: 1 branch
BranchTrueFalseTotal
if $ey > $y 10 10545 10555
528 10555 ▶ $z = $ez if $ez > $z;
Branch: 1 branch
BranchTrueFalseTotal
if $ez > $z 10 10545 10555
529 }
530 8 return [$x, $y, $z];
531 }
532 7 );
533  
534 =head2 boxes()
535  
536 Returns an array reference of the list of L<Box::Calc::Box>es needed to pack up the items.
537  
538 B<NOTE:> This will be empty until you call C<pack_items>.
539  
540 =head2 count_boxes()
541  
542 Returns the number of boxes needed to pack up the items.
543  
544 =head2 get_box(index)
545  
546 Fetches a specific box from the list of <boxes>.
547  
548 =over
549  
550 =item index
551  
552 The array index of the box you wish to fetc.
553  
554 =back
555  
556 =cut
557  
558 has boxes => (
559 is => 'rw',
560 isa => 'ArrayRef[Box::Calc::Box]',
561 8 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
562 7 traits => ['Array'],
563 handles => {
564 push_boxes => 'push',
565 count_boxes => 'count',
566 get_box => 'get',
567 }
568 );
569  
570 =head2 reset_boxes()
571  
572 Deletes the list of C<boxes>.
573  
574 If you wish to rerun the packing you should use this to delete the list of C<boxes> first. This is handy if you needed to add an extra item or extra box type after you already ran C<pack_items>.
575  
576 =cut
577  
578 sub reset_boxes {
579 4 ▶ my $self = shift;
Subroutine reset_boxes: called CC=1 SCAR=0.0
Pod documented
580 4 $self->boxes([]);
581 }
582  
583 =head2 reset_items()
584  
585 Deletes the list of C<items>.
586  
587 For the sake of speed you may wish to reuse a L<Box::Calc> instance with the box types already pre-loaded. In that case you'll want to use this method to remove the items you've already registered. You'll probably also want to call C<reset_boxes>.
588  
589 =cut
590  
591 sub reset_items {
592 1 ▶ my $self = shift;
Subroutine reset_items: called CC=1 SCAR=0.0
Pod documented
593 1 $self->items([]);
594 1 $self->clear_max_dimensions_of_items;
595 }
596  
597 =head2 make_box($box_type)
598  
599 Handy method to create new box using a specified
600 box type.
601  
602 =cut
603  
604 sub make_box {
605 31 ▶ my ($self, $box_type) = @_;
Subroutine make_box: called CC=1 SCAR=0.0
Pod documented
606 31 return Box::Calc::Box->new(
607 swap_xy => 1,
608 mail_service_name => $box_type->mail_service_name,
609 x => $box_type->x,
610 y => $box_type->y,
611 z => $box_type->z,
612 weight => $box_type->weight,
613 max_weight => $box_type->max_weight,
614 name => $box_type->name,
615 outer_x => $box_type->outer_x,
616 outer_y => $box_type->outer_y,
617 outer_z => $box_type->outer_z,
618 );
619 }
620  
621 =head2 find_tallest_z ( [ items ] )
622  
623 Determines the median of z across all items in the list.
624  
625 =over
626  
627 =item items
628  
629 An array reference of items. Optional. Defaults to C<items>.
630  
631 =back
632  
633 =cut
634  
635 sub find_tallest_z {
636 3 ▶ my $self = shift;
Subroutine find_tallest_z: called CC=2 SCAR=7.0
Pod documented
637 3 ▶ my $items = shift || $self->items;
Condition66%2/3
shift() || $self->items
l!l&&r!l&&!r
210
MC/DC0%0/2
shift() || $self->items
shift() $self->items
Truth table66%2/3
A || B
A= shift()B= $self->items
ABresult
1X1
011
000
638 3 return max map { $_->z } @{$items};
639 }
640  
641  
642 =head2 stack_like_items( options )
643  
644 Stacks all like-sized items into stacks of C<stack_height> for denser packing. Could be used as an optimizer before running C<pack_items>.
645  
646 =over
647  
648 =item options
649  
650 A hash.
651  
652 =over
653  
654 =item items
655  
656 Optional. If not specified, will be the C<items> list.
657  
658 =item stack_height
659  
660 Optional. If not specified, will be determined by calling C<find_tallest_z>.
661  
662 =back
663  
664 =back
665  
666 =cut
667  
668 sub stack_like_items {
669 2 ▶ my ($self, %options) = @_;
Subroutine stack_like_items: called CC=9 SCAR=22.6
Pod documented
670 2 ▶ my $items = $options{items} || $self->items;
Condition33%1/3
$options{'items'} || $self->items
l!l&&r!l&&!r
020
MC/DC0%0/2
$options{'items'} || $self->items
$options{'items'} $self->items
Truth table33%1/3
A || B
A= $options{'items'}B= $self->items
ABresult
1X1
011
000
671 2 ▶ my $stack_height = $options{stack_height} || $self->find_tallest_z($items);
Condition33%1/3
$options{'stack_height'} || $self->find_tallest_z($items)
l!l&&r!l&&!r
020
MC/DC0%0/2
$options{'stack_height'} || $self->find_tallest_z($items)
$options{'stack_height'} $self->find_tallest_z($items)
Truth table33%1/3
A || B
A= $options{'stack_height'}B= $self->find_tallest_z($items)
ABresult
1X1
011
000
672 2 my %like;
673 2 foreach my $item (@{$items}) {
674 110 push @{$like{$item->extent}}, $item;
675 }
676 2 my @stacks;
677 2 foreach my $kind (values %like) {
678 6 ▶ if (scalar @{$kind} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$kind;} == 1) { } 0 6 6
679 0 push @stacks, $kind->[0];
680 }
681 else {
682 6 ▶ my $items_per_stack = int($stack_height / $kind->[0]->z) || 1;
Condition50%1/2
int $stack_height / $kind->[0]->z || 1
l!l
60
MC/DC0%0/1
int $stack_height / $kind->[0]->z || 1
int $stack_height / $kind->[0]->z
Truth table50%1/2
A
A= int $stack_height / $kind->[0]->z
Aresult
11
00
683 6 my $iterator = natatime($items_per_stack, @{$kind});
684 6 ▶ while (my @items = $iterator->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@items) = &$iterator()) 12 6 18
685 12 my $count = scalar @items;
686 12 ▶ if ($count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1) { } 6 6 12
687 6 push @stacks, $items[0];
688 }
689 else {
690 6 my $item = $items[0];
691 6 push @stacks, Box::Calc::Item->new(
692 x => $item->x,
693 y => $item->y,
694 z => $item->z * $count,
695 weight => $item->weight * $count,
696 name => 'Stack of '.$count.' '.$item->name,
697 no_sort => 1,
698 );
699 }
700 }
701 }
702 }
703 2 return \@stacks;
704 }
705  
706 =head2 pack_items(options)
707  
708 Uses the list of C<box_types> and the list of C<items> to create the list of boxes to be packed. This method populates the C<boxes> list.
709  
710 =over
711  
712 =item options
713  
714 A hash.
715  
716 =over
717  
718 =item items
719  
720 Optional. If omitted the items list will be populated with whatever the current B<best> general purpose preprocessed item list is. Currently that is C<sort_items_by_zA>.
721  
722 =item category
723  
724 Optional. If this is specified, it will match this category name to the categories attached to the boxes and only pack in boxes that match that category.
725  
726 =back
727  
728 =back
729  
730 =cut
731  
732 sub pack_items {
733 10 ▶ my ($self, %options) = @_;
Subroutine pack_items: called CC=14 SCAR=27.2
Pod documented
734 10 my $category = $options{category};
735 10 ▶ my $items = $options{items} || $self->sort_items_by_zA;
Condition66%2/3
$options{'items'} || $self->sort_items_by_zA
l!l&&r!l&&!r
190
MC/DC0%0/2
$options{'items'} || $self->sort_items_by_zA
$options{'items'} $self->sort_items_by_zA
Truth table33%1/3
A || B
A= $options{'items'}B= $self->sort_items_by_zA
ABresult
1X1
011
000
736 10 my $item_count = scalar(@{$items});
737 10 my @box_types = @{$self->determine_viable_box_types($category)};
738 10 my $countdown = scalar(@box_types);
739 10 BOXTYPE: foreach my $box_type (@box_types) {
740 19 $log->info("Box Type: ".$box_type->name);
741 19 $countdown--;
742 19 my $box = $self->make_box($box_type);
743 19 ITEM: foreach my $item (@{$items}) {
744 17295 $log->info("Item: ".$item->name);
745
746 # swap the item's x & y if it will make the item fit tighter
747 17295 ▶ if ($item->x > 0 && $item->y > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->x > 0 and $item->y > 0) { } 17295 0 17295
Condition33%1/3
$item->x > 0 and $item->y > 0
!ll&&!rl&&r
0017295
MC/DC0%0/2
$item->x > 0 and $item->y > 0
$item->x > 0 $item->y > 0
Truth table33%1/3
A and B
A= $item->x > 0B= $item->y > 0
ABresult
0X0
100
111
748 17295 $log->debug("Item's dimensions are not 0.");
749 17295 ▶ if ($box->x >= $item->y && $box->y >= $item->x) { # see if the item would still fit in the box if it swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($box->x >= $item->y and $box->y >= $item->x) 17295 0 17295
Condition33%1/3
$box->x >= $item->y and $box->y >= $item->x
!ll&&!rl&&r
0017295
MC/DC0%0/2
$box->x >= $item->y and $box->y >= $item->x
$box->x >= $item->y $box->y >= $item->x
Truth table33%1/3
A and B
A= $box->x >= $item->yB= $box->y >= $item->x
ABresult
0X0
100
111
750 17295 $log->debug('Item would still fit in the box if we rotated it.');
751 17295 my $original_x_per_layer = int($box->x / $item->x);
752 17295 my $original_y_per_layer = int($box->y / $item->y);
753 17295 my $original_count_per_layer = $original_x_per_layer * $original_y_per_layer;
754 17295 my $new_count_per_layer = int($box->x / $item->y) * int($box->y / $item->x);
755 17295 ▶ if ( $new_count_per_layer > $original_count_per_layer # you can fit more items per layer in a swap
Branch: 1 branch
BranchTrueFalseTotal
if ($new_count_per_layer > $original_count_per_layer or $original_x_per_layer == 0 or $original_y_per_layer == 0) 0 17290 17290
Condition66%4/6
$new_count_per_layer > $original_count_per_layer or $original_x_per_layer == 0 or $original_y_per_layer == 0
l!l&&r!l&&!r
5017290
$new_count_per_layer > $original_count_per_layer or $original_x_per_layer == 0
l!l&&r!l&&!r
5017290
MC/DC33%1/3
$new_count_per_layer > $original_count_per_layer or $original_x_per_layer == 0 or $original_y_per_layer == 0
$new_count_per_layer > $original_count_per_layer $original_x_per_layer == 0 $original_y_per_layer == 0
Truth table50%2/4
A or B or C
A= $new_count_per_layer > $original_count_per_layerB= $original_x_per_layer == 0C= $original_y_per_layer == 0
ABCresult
1XX1
01X1
0011
0000
756 || $original_x_per_layer == 0 || $original_y_per_layer == 0 # if we keep it the current rotation we definitely won't fit, probably due to previous rotation
757 ) {
758 5 $log->info('Rotating '.$item->{name}.', because we can fit more per layer if we rotate.');
759 5 my $temp_x = $item->x;
760 5 $item->x($item->y);
761 5 $item->y($temp_x);
762 }
763 }
764 }
765 else {
766 0 $log->error('Item has a zero (0) dimension. That should not happen.');
767 }
768  
769 # pack the item into the box
770 17295 ▶ unless ($box->pack_item($item)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($box->pack_item($item)) 21 17274 17295
771 21 ▶ if ($countdown) { # we still have other boxes to try
Branch: 1 branch
BranchTrueFalseTotal
if ($countdown) { } 9 12 21
772 9 $log->info("moving to next box type");
773 9 next BOXTYPE;
774 }
775 else { # no more boxes to try, time for spanning
776 12 ▶ if (scalar(@{$self->boxes}) > $item_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->boxes;} > $item_count) 0 12 12
777 0 $log->warn("More boxes than items.");
778 #ouch 'more boxes than items', 'The number of boxes has exceded the number of items, which should never happen.';
779 }
780 12 $log->info("no more box types, spanning");
781 12 $self->push_boxes($box);
782 12 $box = $self->make_box($box_type);
783 12 redo ITEM;
784 }
785 }
786 }
787
788 # we made it through our entire item list, yay!
789 10 $log->info("finished!");
790 10 $self->push_boxes($box);
791 10 last BOXTYPE;
792 }
793 }
794  
795 =head2 packing_list()
796  
797 Returns a data structure with all the item names and quantities packed into boxes. This can be used to generate manifests.
798  
799 [
800 { # box one
801 id => "xxx",
802 name => "big box",
803 weight => 30.1,
804 packing_list => {
805 "soda" => 3,
806 "apple" => 1,
807 "water bottle" => 2,
808 }
809 }
810 ]
811  
812 =cut
813  
814 sub packing_list {
815 2 ▶ my $self = shift;
Subroutine packing_list: called CC=2 SCAR=6.9
Pod documented
816 2 my @boxes;
817 2 foreach my $box (@{$self->boxes}) {
818 14 my ($weight, $list) = $box->packing_list;
819 14 push @boxes, {
820 id => $box->id,
821 name => $box->name,
822 weight => $weight,
823 packing_list => $list,
824 };
825 }
826 2 return \@boxes;
827 }
828  
829 =head2 packing_instructions()
830  
831 Returns a data structure with all the item names individually packed into rows, layers, and boxes. This can be used to build documentation on how to pack a set of boxes, and to generate a complete build history.
832  
833 [
834 { # box one
835 id => "xxx",
836 name => "big box",
837 layers => [
838 { # layer one
839 rows => [
840 { # row one
841 items => [
842 { # item one
843 name => "apple",
844 ...
845 },
846 ...
847 ],
848 },
849 ...
850 ],
851 ...
852 },
853 ],
854 },
855 ]
856  
857 =cut
858  
859 sub packing_instructions {
860 4 ▶ my $self = shift;
Subroutine packing_instructions: called CC=1 SCAR=0.0
Pod documented
861 4 my @boxes = map { $_->packing_instructions} @{ $self->boxes };
862 4 return \@boxes;
863 }
864  
865 =head1 TODO
866  
867 There are some additional optimizations that could be done to speed things up a bit. We might also be able to get a better fill percentage (less void space), although that's not really the intent of Box::Calc.
868  
869 =head1 PREREQS
870  
871 L<Moose>
872 L<Ouch>
873 L<Log::Any>
874 L<Data::GUID>
875  
876 =head1 SUPPORT
877  
878 =over
879  
880 =item Repository
881  
882 L<http://github.com/rizen/Box-Calc>
883  
884 =item Bug Reports
885  
886 L<http://github.com/rizen/Box-Calc/issues>
887  
888 =back
889  
890  
891 =head1 SEE ALSO
892  
893 Although these modules don't solve the same problem as this module, they may help you build something that does if Box::Calc doesn't quite help you do what you want.
894  
895 =over
896  
897 =item L<Algorithm::Knapsack>
898  
899 =item L<Algorithm::Bucketizer>
900  
901 =item L<Algorithm::Knap01DP>
902  
903 =back
904  
905 =head1 AUTHOR
906  
907 =over
908  
909 =item JT Smith <jt_at_plainblack_dot_com>
910  
911 =item Colin Kuskie <colink_at_plainblack_dot_com>
912  
913 =back
914  
915 =head1 LEGAL
916  
917 Box::Calc is Copyright 2012 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
918  
919 =cut
920  
921 7 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
922 7 __PACKAGE__->meta->make_immutable;
↑ Summary Calc/Layer.pm »