Calc/Layer.pm

filter: Statementstmt 95.5% 86 / 90 Branchbran 93.7% 15 / 16 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 4 / 4 Subroutinesub 94.7% 18 / 19 Podpod 90.9% 10 / 11 totaltotal 95.2% 141 / 148 SCAR 28.3
CC
17
Cov
96%
pack_item
9.1
fill_x
3.0
fill_z
3.0
CRAP 17.0
« Calc.pm ↑ Summary Calc/Box.pm »
1 package Box::Calc::Layer;
2 11 $Box::Calc::Layer::VERSION = '1.0201';
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Box::Calc::Row;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use List::Util qw/sum/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Box::Calc::Layer - A box is packed with multiple layers.
13  
14 =head1 VERSION
15  
16 version 1.0201
17  
18 =head1 SYNOPSIS
19  
20 my $row = Box::Calc::Row->new(max_x => 6);
21
22 =head1 METHODS
23  
24 =head2 new(params)
25  
26 Constructor.
27  
28 B<NOTE:> A layer is automatically created containing a single empty L<Box::Calc::Row>.
29  
30 =over
31  
32 =item max_x
33  
34 The maximimum width of the layer. This is equivalent to the C<x> or longest dimension of the containing box.
35  
36 =item max_y
37  
38 The maximimum depth of the layer. This is equivalent to the C<y> or middle dimension of the containing box.
39  
40 =back
41  
42 =head2 fill_x()
43  
44 Returns how full the layer is in the C<x> dimension.
45  
46 =cut
47  
48 sub fill_x {
49 573 ▶ my $self = shift;
Subroutine fill_x: called CC=3 SCAR=11.0
Pod documented
50 573 my $value = 0;
51 573 foreach my $row (@{$self->rows}) {
52 4050 ▶ $value = $row->fill_x if $row->fill_x > $value;
Branch: 1 branch
BranchTrueFalseTotal
if $row->fill_x > $value 575 3475 4050
53 }
54 573 return sprintf ("%.4f", $value);
55 }
56  
57 =head2 fill_y()
58  
59 Returns how full the layer is in the C<y> dimension.
60  
61 =cut
62  
63 sub fill_y {
64 24992 ▶ my $self = shift;
Subroutine fill_y: called CC=2 SCAR=6.9
Pod documented
65 24992 my $value = 0;
66 24992 foreach my $row (@{$self->rows}) {
67 230737 $value += $row->fill_y;
68 }
69 24992 return sprintf ("%.4f", $value);
70 }
71  
72 =head2 fill_z()
73  
74 Returns how full the layer is in the C<z> dimension.
75  
76 =cut
77  
78 sub fill_z {
79 382855 ▶ my $self = shift;
Subroutine fill_z: called CC=3 SCAR=11.0
Pod documented
80 382855 my $value = 0;
81 382855 foreach my $row (@{$self->rows}) {
82 2728496 ▶ $value = $row->fill_z if $row->fill_z > $value;
Branch: 1 branch
BranchTrueFalseTotal
if $row->fill_z > $value 381438 2347058 2728496
83 }
84 382855 return sprintf ("%.4f", $value);
85 }
86  
87 =head2 max_x()
88  
89 Returns the maximum C<x> dimension of this layer. See C<new> for details.
90  
91 =cut
92  
93 11 has max_x => (
94 is => 'ro',
95 required => 1,
96 isa => 'Num',
97 );
98  
99 =head2 max_y()
100  
101 Returns the maximum C<y> dimension of this layer. See C<new> for details.
102  
103 =cut
104  
105 11 has max_y => (
106 is => 'ro',
107 required => 1,
108 isa => 'Num',
109 );
110  
111 =head2 rows()
112  
113 Returns an array reference of the list of L<Box::Calc::Row> contained in this layer.
114  
115 =head2 count_rows()
116  
117 Returns the number of rows contained in this layer.
118  
119 =cut
120  
121 has rows => (
122 is => 'rw',
123 isa => 'ArrayRef[Box::Calc::Row]',
124 547 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
125 11 traits => ['Array'],
126 handles => {
127 count_rows => 'count',
128 }
129 );
130  
131 =head2 create_row()
132  
133 Adds a new L<Box::Calc::Row> to this layer.
134  
135 =cut
136  
137 sub create_row {
138 3579 ▶ my $self = shift;
Subroutine create_row: called CC=1 SCAR=0.0
Pod documented
139 3579 push @{$self->rows}, Box::Calc::Row->new(max_x => $self->max_x);
140 }
141  
142 sub BUILD {
143 547 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
144 547 $self->create_row;
145 }
146  
147 =head2 calculate_weight()
148  
149 Calculates and returns the weight of all the rows in this layer.
150  
151 =cut
152  
153 sub calculate_weight {
154 278 ▶ my $self = shift;
Subroutine calculate_weight: called CC=2 SCAR=6.9
Pod documented
155 278 my $weight = 0;
156 278 foreach my $row (@{$self->rows}) {
157 1969 $weight += $row->calculate_weight;
158 }
159 278 return $weight;
160 }
161  
162 =head2 pack_item(item)
163  
164 Add a L<Box::Calc::Item> to this layer.
165  
166 Returns 1 on success or 0 on failure.
167  
168 =over
169  
170 =item item
171  
172 The L<Box::Calc::Item> instance you want to add to this layer.
173  
174 =back
175  
176 =cut
177  
178 sub pack_item {
179 20861 ▶ my ($self, $item, $count) = @_;
Subroutine pack_item: called CC=9 SCAR=22.1
Pod documented
180 20861 ▶ $count ||= 1;
Condition100%2/2
$count ||= 1
l!l
303217829
MC/DC100%1/1
$count ||= 1
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
181 20861 ▶ if ($count > 99) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 99) 0 20861 20861
182 0 $log->warn($item->{name}.' is causing infinite recursion in Box::Calc::Layer');
183 0 $log->debug(Dumper($item));
184 0 return 0;
185 }
186 20861 my $fill_z = $self->fill_z;
187 20861 my $fill_y = $self->fill_y;
188 20861 ▶ if ($item->y > $self->max_y - $fill_y + $self->rows->[-1]->fill_y # item would make the layer too wide
Branch: 1 branch
BranchTrueFalseTotal
if ($item->y > $self->max_y - $fill_y + $self->rows->[-1]->fill_y) 3 20858 20861
189 ) {
190 3 $log->info($item->{name}.' would make the layer too wide, requesting new layer.');
191 3 return 0;
192 }
193 20858 ▶ if ($fill_z > 0 && $item->z > $fill_z * 1.75 && $item->y < $fill_y) { # item would make the layer substantially taller, unless the layer is currently pretty narrow
Branch: 1 branch
BranchTrueFalseTotal
if ($fill_z > 0 and $item->z > $fill_z * "1.75" and $item->y < $fill_y) 4 20854 20858
Condition100%6/6
$fill_z > 0 and $item->z > $fill_z * "1.75" and $item->y < $fill_y
!ll&&!rl&&r
2085314
$fill_z > 0 and $item->z > $fill_z * "1.75"
!ll&&!rl&&r
546203075
MC/DC100%3/3
$fill_z > 0 and $item->z > $fill_z * "1.75" and $item->y < $fill_y
$fill_z > 0 $item->z > $fill_z * "1.75" $item->y < $fill_y
Truth table100%4/4
A and B and C
A= $fill_z > 0B= $item->z > $fill_z * "1.75"C= $item->y < $fill_y
ABCresult
0XX0
10X0
1100
1111
194 4 $log->info($item->{name}.' would make this layer substantially taller, requesting new layer.');
195 4 return 0;
196 }
197 20854 ▶ if ($self->rows->[-1]->pack_item($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->rows->[-1]->pack_item($item)) { } 17296 3558 20854
198 17296 return 1;
199 }
200 else {
201 3558 ▶ if ($item->y > $self->max_y - $self->fill_y) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->y > $self->max_y - $self->fill_y) { } 526 3032 3558
202 526 $log->info($item->{name}.' will not fit in a new row in this layer, requesting new layer.');
203 526 return 0;
204 }
205 else {
206 3032 $self->create_row;
207 3032 return $self->pack_item($item, $count + 1);
208 }
209 }
210 }
211  
212 =head2 packing_list(weight, list)
213  
214 Updates a scalar reference with the weight of the layer and a hash reference of all the items in this layer.
215  
216 =cut
217  
218 sub packing_list {
219 265 ▶ my ($self, $weight, $list) = @_;
Subroutine packing_list: called CC=2 SCAR=6.9
Pod documented
220 265 foreach my $row (@{$self->rows}) {
221 1947 $row->packing_list($weight, $list)
222 }
223 }
224  
225 =head2 packing_instructions()
226  
227 Returns a description of the layer.
228  
229 {
230 fill_x => 3,
231 fill_y => 3,
232 fill_z => 1,
233 rows => [ ... ],
234 }
235  
236 =cut
237  
238 sub packing_instructions {
239 272 ▶ my $self = shift;
Subroutine packing_instructions: called CC=1 SCAR=0.0
Pod documented
240 return {
241 272 rows => [ map { $_->packing_instructions } @{ $self->rows } ],
242 fill_x => $self->fill_x,
243 fill_y => $self->fill_y,
244 fill_z => $self->fill_z,
245 calculated_weight => $self->calculate_weight,
246 }
247 }
248  
249  
250 =head2 used_volume
251  
252 Returns the real used volume for this layer.
253  
254 =head2 volume
255  
256 Returns the exact volume needed for this layer.
257  
258 =cut
259  
260  
261 sub used_volume {
262 272 ▶ my $self = shift;
Subroutine used_volume: called CC=2 SCAR=6.9
Pod documented
263 272 ▶ return sum map { $_->used_volume || 0 } @{ $self->rows };
Branch: 1 branch
BranchTrueFalseTotal
$_->used_volume or 0 1 1959 1960
264 }
265  
266  
267 sub volume {
268 0 ▶ return $_[0]->fill_x * $_[0]->fill_y * $_[0]->fill_z;
Subroutine volume: not called CC=1 SCAR=6.9
Pod documented
269 }
270  
271 11 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
272 11 __PACKAGE__->meta->make_immutable;
273  
274 =for Pod::Coverage BUILD
« Calc.pm ↑ Summary Calc/Box.pm »