blib/lib/Lego/Ldraw/Line.pm

filter: Statementstmt 20.6% 53 / 257 Branchbran 2.5% 2 / 78 Conditioncond 10.0% 1 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 30.0% 15 / 50 Podpod 3.3% 1 / 30 totaltotal 16.7% 72 / 430 SCAR 74.8
CC
54
Cov
16%
format
42.0
eval
42.0
transform
42.0
CRAP 1768.1
↑ Summary blib/lib/Lego/Ldraw/Display.pm »
1 package Lego::Ldraw::Line;
2
3 1 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 1 ▶no warnings qw(uninitialized redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 use overload
9 1 '""' => \&stringify,
10 1 ▶ '*' => \&transform;
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Lego::Ldraw;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Math::MatrixReal;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 1 my $line_formats = [
20 [qw(type command)],
21 [qw(type colour x y z a b c d e f g h i part)],
22 [qw(type colour x1 y1 z1 x2 y2 z2)],
23 [qw(type colour x1 y1 z1 x2 y2 z2 x3 y3 z3)],
24 [qw(type colour x1 y1 z1 x2 y2 z2 x3 y3 z3 x4 y4 z4)],
25 [qw(type colour x1 y1 z1 x2 y2 z2 x3 y3 z3 x4 y4 z4)]
26 ];
27
28 1 our $config;
29 1 our %descriptions;
30
31 #######################################################################
32 # Constructors
33 #######################################################################
34
35 sub new {
36 0 ▶ my $proto = shift;
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
37 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
38 0 my $self = {};
39
40 0 bless ($self, $class);
41 0 return $self;
42 }
43
44 sub new_from_string {
45 0 ▶ my $self = shift->new;
Subroutine new_from_string: not called CC=3 SCAR=24.8
Pod undocumented
46
47 0 my $line = shift;
48 0 for ($line) {
49 0 s/\s+$//; s/^\s+//;
50 }
51
52 0 my @line = split ' ', $line;
53 0 ▶ @line = ($line[0], join (' ', @line[1..$#line])) unless ($line[0]); # handle comment lines
Branch: 1 branch
BranchTrueFalseTotal
unless $line[0] 0 0 0
54
55 0 my @fields = @{$line_formats->[$line[0]]};
56 0 @{$self}{@fields} = @line;
57
58 0 return $self;
59 }
60
61 sub new_from_part_name {
62 0 ▶ my $self = shift;
Subroutine new_from_part_name: not called CC=1 SCAR=6.9
Pod undocumented
63 0 my $part = shift;
64
65 0 return $self->new_from_string('1 16 0 0 0 1 0 0 0 1 0 0 0 1 ' . $part);
66 }
67
68 #######################################################################
69 # Field access functions
70 #######################################################################
71
72 sub BEGIN {
73 #--------------------------------------------------
74 # Use generated functions for clarity and speed,
75 # with exceptions...
76 #--------------------------------------------------
77 1 ▶ my @field_list = ('colour', 'a'..'i', 'x', 'y', 'z',
Subroutine BEGIN: called CC=2 SCAR=10.9
78 'x1', 'y1', 'z1', 'x2', 'y2', 'z2',
79 'x3', 'y3', 'z3', 'x4', 'y4', 'z4');
80 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 for my $field (@field_list) {
82 *$field = sub {
83 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
84 0 ▶ return unless exists $self->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$field} 0 0 0
85 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
86 0 $self->{$field} = shift;
87 0 return $self->{$field};
88 } else {
89 0 return $self->{$field};
90 }
91 }
92 25 }
93 1 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
94 }
95
96 #--------------------------------------------------
97 # ...because of uppercasing
98 #--------------------------------------------------
99 sub part {
100 0 ▶ my $self = shift;
Subroutine part: not called CC=2 SCAR=17.9
Pod undocumented
101 0 ▶ return unless exists $self->{'part'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"part"} 0 0 0
102 0 my $part = $self->{part};
103 0 $part =~ s/\\/\//g;
104 0 return lc $part;
105 }
106
107 sub name {
108 0 ▶ return basename(shift->part);
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
109 }
110
111 #--------------------------------------------------
112 # ...because it's read-only
113 #--------------------------------------------------
114 sub type {
115 0 ▶ return shift->{type};
Subroutine type: not called CC=1 SCAR=6.9
Pod undocumented
116 }
117
118 #--------------------------------------------------
119 # ...because of spelling
120 #--------------------------------------------------
121 sub color {
122 0 ▶ shift->colour(@_);
Subroutine color: not called CC=1 SCAR=6.9
Pod undocumented
123 }
124
125 #######################################################################
126 # other field access functions
127 #######################################################################
128
129 sub copy {
130 0 ▶ my $self = shift;
Subroutine copy: not called CC=1 SCAR=6.9
Pod undocumented
131 0 return bless { %{$self} }, ref $self;
132 }
133
134 sub fields {
135 0 ▶ return @{$line_formats->[ shift->type ]}
Subroutine fields: not called CC=1 SCAR=6.9
Pod undocumented
136 }
137
138 sub model {
139 0 ▶ my $self = shift;
Subroutine model: not called CC=2 SCAR=17.9
Pod undocumented
140 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
141 0 $self->{model} = shift;
142 }
143 0 return $self->{model};
144 }
145
146 sub description {
147 0 ▶ my $self = shift;
Subroutine description: not called CC=2 SCAR=17.9
Pod undocumented
148 0 ▶ return unless $self->type == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type == 1 0 0 0
149
150 0 return $descriptions{$self->part};
151 }
152
153
154 sub values {
155 0 ▶ my $self = shift;
Subroutine values: not called CC=2 SCAR=17.9
Pod undocumented
156 0 my @fields = @_;
157 0 ▶ @fields = $self->fields unless @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 0 0 0
158 0 return @{$self}{ @fields }
159 }
160
161 sub coords {
162 0 ▶ my $self = shift;
Subroutine coords: not called CC=1 SCAR=6.9
Pod undocumented
163 0 my @fields = grep { /^[xyz]/ } $self->fields;
164 0 return $self->values(@fields);
165 }
166
167 sub points {
168 0 ▶ my $points = shift->type;
Subroutine points: not called CC=2 SCAR=17.9
Pod undocumented
169 0 ▶ return $points > 4 ? 4 : $points;
Branch: 1 branch
BranchTrueFalseTotal
$points > 4 ? : 0 0 0
170 }
171
172 sub transform_matrix {
173 0 ▶ my $self = shift;
Subroutine transform_matrix: not called CC=2 SCAR=17.9
Pod undocumented
174 0 ▶ return unless ($self->type == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type == 1 0 0 0
175
176 0 my @fields = grep { /^[a-ixyz]$/ } $self->fields;
177 0 return $self->values(@fields);
178 }
179
180 sub point {
181 0 ▶ my $self = shift;
Subroutine point: not called CC=3 SCAR=24.8
Pod undocumented
182 0 my $point = shift;
183 0 ▶ return unless my $type = $self->type;
Branch: 1 branch
BranchTrueFalseTotal
unless my $type = $self->type 0 0 0
184
185 0 ▶ if ($type == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1) { } 0 0 0
186 0 return $self->values(qw(x y z));
187 } else {
188 0 return $self->values(map { $_ . $point } qw(x y z))
189 }
190 }
191
192 sub format {
193 0 ▶ my $self = shift;
Subroutine format: not called CC=6 SCAR=37.4
Pod undocumented
194
195 0 my @text = $self->values;
196
197 0 for ($self->type) {
198 0 ▶ /^0/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^0/ and do { return "$self" } 0 0 0
199 0 return "$self";
200 };
201 0 ▶ /^1/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^1/ and do { my $string = "%d %7d"; foreach $_ (2 .. $#text - 1) { $string .= '% 8.2f'; } ; $string .= ' %12s'; return sprintf($string, @text) } 0 0 0
202 0 my $string = "%d %7d";
203 0 for (2..$#text-1) {
204 0 $string .= "% 8.2f";
205 }
206 0 $string .= " %12s";
207 0 return sprintf $string, @text;
208 };
209 0 my $string = "%d";
210 0 for (1..$#text) {
211 0 $string .= "% 8.2f";
212 }
213 0 return sprintf $string, @text;
214 }
215 }
216
217 sub eval {
218 0 ▶ my $self = shift;
Subroutine eval: not called CC=6 SCAR=37.4
Pod undocumented
219 0 my $expr = shift;
220
221 0 $expr = lc $expr;
222 0 $expr =~ s/color/colour/g;
223
224 # substitute % strings with field accesses,
225 # and while doing so check if field exists:
226 # if it doesn't return undef
227 0 ▶ while ($expr =~ s/\%([a-z0-9]+)/\$self->{$1}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ s/\%([a-z0-9]+)/\$self->{$1}/) 0 0 0
228 0 ▶ return unless defined $self->$1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->$1 0 0 0
229 }
230
231 # substitute & strings with function calls,
232 # and while doing so check if function exists:
233 # if it doesn't return undef
234 0 ▶ while ($expr =~ s/\&(\w+)/\$self->$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ s/\&(\w+)/\$self->$1/) 0 0 0
235 0 ▶ return unless defined $self->can($1);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->can($1) 0 0 0
236 }
237
238 # now we've got a full eval'uable string, and
239 # we eval it
240 0 ▶ if (eval $expr) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval $expr) { } 0 0 0
241 0 return $self
242 } else {
243 return
244 0 }
245 }
246
247 #######################################################################
248 # inlining
249 #######################################################################
250
251 sub normalize {
252 0 ▶ my $self = shift;
Subroutine normalize: not called CC=2 SCAR=17.9
Pod undocumented
253 0 ▶ return unless $self->type == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type == 1 0 0 0
254
255 0 @{$self}{qw/x y z a b c d e f g h i/} = qw/0 0 0 1 0 0 0 1 0 0 0 1/;
256 0 return $self;
257 }
258
259 sub dir {
260 0 ▶ my $self = shift;
Subroutine dir: not called CC=2 SCAR=17.9
Pod undocumented
261 0 ▶ $self->{dir} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
262 0 return $self->{dir};
263 }
264
265 sub partfile {
266 0 ▶ my $self = shift;
Subroutine partfile: not called CC=5 SCAR=34.0
Pod undocumented
267 0 ▶ return unless $self->type == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type == 1 0 0 0
268
269 0 my $part = $self->part;
270 return $self->config->{partfiles}->{$part}
271 0 ▶ if $self->config->{partfiles}->{$part};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"partfiles"}{$part} 0 0 0
272
273 0 my $base = $self->config->{base};
274
275 0 my @parts = @{$self->config->{parts}};
276 0 @parts = map { $_ = $base . $_ . $part } @parts;
277
278 0 @parts = ('./' . $part, $self->dir . '/' . $part, @parts);
279
280 0 for (@parts) {
281 0 s/\\/\//g;
282 0 ▶ if (-e $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $_) 0 0 0
283 0 $self->config->{partfiles}->{$part} = $_;
284 0 return $_;
285 }
286 }
287 }
288
289 sub explode {
290 0 ▶ my $self = shift;
Subroutine explode: not called CC=3 SCAR=24.8
Pod undocumented
291
292 0 ▶ return unless $self->type == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type == 1 0 0 0
293
294 0 my $file = $self->partfile;
295
296 0 ▶ return unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
297 0 return Lego::Ldraw->new_from_file($file);
298 }
299
300 sub traslate {
301 0 ▶ my $self = shift;
Subroutine traslate: not called CC=4 SCAR=30.0
Pod undocumented
302 0 my %trans;
303
304 0 ▶ if (ref $_[0] eq 'HASH') { %trans = %{ $_[0] } }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
305 0 else { @trans{qw(x y z)} = @_ };
306
307 0 for my $axis (keys %trans) {
308 0 for my $field ( grep { /^$axis/ } $self->fields ) {
309 0 $self->{$field} += $trans{$axis}
310 }
311 }
312 0 return $self;
313 }
314
315 sub transform {
316 0 ▶ my $self = shift;
Subroutine transform: not called CC=6 SCAR=37.4
Pod undocumented
317 0 my $line = shift;
318
319 0 ▶ return unless $self->type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type 0 0 0
320 0 ▶ return unless $line->type == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $line->type == 1 0 0 0
321
322 0 ▶ $self->color($line->color) if $self->color == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $self->color == 16 0 0 0
323 0 my $m = $line->_transform_matrix;
324
325 0 ▶ if ($self->type == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type == 1) { } 0 0 0
326 0 my $x = $self->_transform_matrix();
327 0 $self->_transform_matrix($x * $m);
328 } else {
329 0 for (1..$self->points) {
330 0 my $p = $self->_xyz_matrix(undef, $_);
331 0 $self->_xyz_matrix($p * $m, $_)
332 }
333 }
334 }
335
336 #######################################################################
337 # other stuff
338 #######################################################################
339
340 sub stringify {
341 0 ▶ my $self = shift;
Subroutine stringify: not called CC=1 SCAR=6.9
Pod undocumented
342 0 my $type = $self->type;
343
344 0 my @fields = @{$line_formats->[$self->type]};
345 0 return join ' ', @{$self}{@fields};
346 }
347
348 #######################################################################
349 # matrix calculation
350 #######################################################################
351
352 sub _xyz_matrix {
353 0 ▶ my $self = shift;
Subroutine _xyz_matrix: not called CC=4 SCAR=30.0
354 0 my $matrix = shift;
355
356 0 ▶ if ($matrix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matrix) { } 0 0 0
357 0 ▶ my $point = $self->type == 1 ? undef : shift;
Branch: 1 branch
BranchTrueFalseTotal
$self->type == 1 ? : 0 0 0
358 0 my @fields = map { $_ . $point } ('x', 'y', 'z');
359
360 $matrix->each( sub {
361 0 ▶ my $field = shift @fields;
Subroutine __ANON__: not called CC=2 SCAR=17.9
362 0 ▶ return unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 0 0 0
363 0 $self->$field(shift)
364 0 } );
365
366 0 return $self;
367 } else {
368 0 my @point = $self->point(shift);
369 0 my $matrix = Math::MatrixReal->new(1, 4);
370 0 $matrix->[0] = [ [ @point, 1 ] ];
371 0 ▶ return @point ? $matrix : undef;
Branch: 1 branch
BranchTrueFalseTotal
@point ? : 0 0 0
372 }
373 }
374
375 sub _transform_matrix {
376 0 ▶ my $self = shift;
Subroutine _transform_matrix: not called CC=2 SCAR=17.9
377 0 my $matrix = shift;
378
379 0 ▶ if ($matrix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matrix) { } 0 0 0
380 0 my @fields = (qw(a d g), undef,
381 qw(b e h), undef,
382 qw(c f i), undef,
383 qw(x y z), undef);
384
385 $matrix->each( sub {
386 0 ▶ my $field = shift @fields;
Subroutine __ANON__: not called CC=2 SCAR=17.9
387 0 ▶ return unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 0 0 0
388 0 $self->$field(shift)
389 0 } );
390 0 return $self;
391 } else {
392 0 my $matrix = Math::MatrixReal->new(4, 4);
393 0 $matrix->[0] = [
394 [ $self->values( qw(a d g) ), 0 ],
395 [ $self->values( qw(b e h) ), 0 ],
396 [ $self->values( qw(c f i) ), 0 ],
397 [ $self->values( qw(x y z) ), 1 ]
398 ];
399 0 return $matrix;
400 }
401 }
402
403 ###############################################################
404 # configuration stuff
405 ###############################################################
406
407 sub INIT {
408 1 ▶ return if $config;
Subroutine INIT: called CC=4 SCAR=15.5
Branch: 1 branch
BranchTrueFalseTotal
if $Lego::Ldraw::Line::config 0 1 1
409 1 $config = do { local $/; <DATA> };
410 1 $config = Load($config);
411
412 1 $config->{base} = $ENV{'LDRAWDIR'};
413 1 ▶ open DESCRIPTIONS, $config->{base} . 'parts.lst' || return;
Condition50%1/2
$Lego::Ldraw::Line::config->{'base'} . 'parts.lst' || (return)
l!l
10
MC/DC0%0/1
$Lego::Ldraw::Line::config->{'base'} . 'parts.lst' || (return)
$Lego::Ldraw::Line::config->{'base'} . 'parts.lst'
Truth table50%1/2
A
A= $Lego::Ldraw::Line::config->{'base'} . 'parts.lst'
Aresult
11
00
414 1 ▶ while (<DESCRIPTIONS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DESCRIPTIONS)) 0 1 1
415 0 chop;
416 0 my ($part, $description) = unpack 'A14A*', $_;
417 0 $descriptions{$part} = $description;
418 }
419 }
420
421 sub config {
422 0 ▶ return $config;
Subroutine config: not called CC=1 SCAR=6.9
Pod undocumented
423 }
424
425 sub basedir {
426 0 ▶ local $_ = $config->{base};
Subroutine basedir: not called CC=1 SCAR=6.9
Pod undocumented
427 0 s/\/$//;
428 0 s/\\$//;
429 0 return $_;
430 }
431
432 sub partsdirs {
433 0 ▶ my $self = shift;
Subroutine partsdirs: not called CC=3 SCAR=24.8
Pod undocumented
434 0 my @d = @{$config->{parts}};
435 0 my $base = $self->basedir;
436
437 0 for (@d) {
438 0 ▶ $_ = join ('/', $base, $_)
Branch: 1 branch
BranchTrueFalseTotal
unless /^\./ 0 0 0
439 unless /^\./;
440 0 s/\/$//;
441 0 s/\\$//;
442 }
443 0 return @d;
444 }
445
446 sub primitives {
447 0 ▶ return %{$config->{primitives}}
Subroutine primitives: not called CC=1 SCAR=6.9
Pod undocumented
448 }
449
450 ###############################################################
451 # faster constructor for Matrix::Real
452 ###############################################################
453
454 sub Math::MatrixReal::new {
455 0 ▶ my ($proto, $rows, $cols) = @_;
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
456
457 0 ▶ my $class = ref($proto) || $proto || 'Math::MatrixReal';
Condition0%0/5
ref $proto || $proto || 'Math::MatrixReal'
l!l
00
ref $proto or $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto || 'Math::MatrixReal'
ref $proto $proto
Truth table0%0/3
A or B
A= ref $protoB= $proto
ABresult
1X1
011
000
458 0 my($i, $j, $this);
459
460 0 $this = [ [ ], $rows, $cols ];
461
462 0 bless($this, $class);
463 0 return($this);
464 }
465
466 ###############################################################
467 # end of faster constructor for Matrix::Real
468 ###############################################################
469
470 1 1;
471
472 __DATA__
↑ Summary blib/lib/Lego/Ldraw/Display.pm »