blib/lib/Algorithm/LineSegments.pm

filter: Statementstmt 100% 99 / 99 Branchbran 83.3% 20 / 24 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 91.6% 132 / 144 CC 18 SCAR 22.2
CC
18
Cov
94%
line_segment_points
15.2
_normalised_euclidean
3.0
__ANON__
2.0
CRAP 9.2
↑ Summary
1 package Algorithm::LineSegments;
2 4 ▶use 5.012000;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use List::Util qw/min max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Heap::Priority;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 4 require Exporter;
9
10 4 our @ISA = qw(Exporter);
11
12 4 our %EXPORT_TAGS = ( 'all' => [ qw(
13 line_segment_points
14 ) ] );
15
16 4 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
17
18 4 our @EXPORT = qw(
19 line_segment_points
20 );
21
22 4 our $VERSION = '0.04';
23
24 382 ▶sub _normalised_euclidean {
Subroutine _normalised_euclidean: called CC=3 SCAR=11.0
25 382 my ($left, $right, $p) = @_;
26 382 my $y0 = $left->[0];
27 382 my $y1 = $right->[-1];
28 382 my $cl = @{ $left };
29 382 my $cr = @{ $right };
30 382 my $xx = ($y1 - $y0) / ($cl + $cr - 1);
31
32 382 my $sum = 0;
33 382 for (0 .. $cl - 1) {
34 2572 my $pi = $y0 + $xx * $_;
35 2572 my $pr = $left->[$_];
36 2572 $sum += ($p->($pr) - $p->($pi))**2;
37 }
38
39 382 for (0 .. $cr - 1) {
40 2472 my $pi = $y0 + $xx * ($_ + $cl);
41 2472 my $pr = $right->[$_];
42 2472 $sum += ($p->($pr) - $p->($pi))**2;
43 }
44
45 382 sqrt $sum;
46 }
47
48 1 ▶sub line_segment_points {
Subroutine line_segment_points: called CC=15 SCAR=27.2
Pod documented
49
50 1 my (%o) = @_;
51
52 1 ▶ die unless $o{points};
Branch: 1 branch
BranchTrueFalseTotal
unless $o{"points"} 0 1 1
53 1 my @d = @{ $o{points} };
54
55 1 my @q;
56
57 1 ▶ for (my $ix = 0; $ix < @d - 1; $ix += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < @d - 1) 100 1 101
58 100 push @q, [$d[$ix], $d[$ix+1]];
59 }
60
61 1 my $min = min @d;
62 1 my $max = max @d;
63
64 ###################################################################
65 # This function projects values from $min to $max to 0 to 1. This
66 # is useful in order to keep the cost values similar.
67 ###################################################################
68 10088 ▶ my $scale_to_unit = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 10088 my $v = shift;
70 10088 return ($v - $min) / ($max - $min);
71 1 };
72
73 382 ▶ $o{cost} //= sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 382 my ($left, $right) = @_;
75 382 _normalised_euclidean($q[$left], $q[$right], $scale_to_unit);
76 1 ▶ };
Condition33%1/3
$o{"cost"} //= sub { my($left, $right) = @_; _normalised_euclidean($q[$left], $q[$right], $scale_to_unit); }
l!l&&r!l&&!r
010
MC/DC0%0/2
$o{"cost"} //= sub { my($left, $right) = @_; _normalised_euclidean($q[$left], $q[$right], $scale_to_unit); }
$o{"cost"} sub { my($left, $right) = @_; _normalised_euclidean($q[$left], $q[$right], $scale_to_unit); }
Truth table33%1/3
A //= B
A= $o{"cost"}B= sub { my($left, $right) = @_; _normalised_euclidean($q[$left], $q[$right], $scale_to_unit); }
ABresult
1X1
011
000
77
78 98 ▶ $o{continue} //= sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
79 98 my ($count, $cost) = @_;
80 98 ▶ return 0 if $count <= 3;
Branch: 1 branch
BranchTrueFalseTotal
if $count <= 3 1 97 98
81 97 return 1;
82 1 ▶ };
Condition33%1/3
$o{"continue"} //= sub { my($count, $cost) = @_; return 0 if $count <= 3; return 1; }
l!l&&r!l&&!r
010
MC/DC0%0/2
$o{"continue"} //= sub { my($count, $cost) = @_; return 0 if $count <= 3; return 1; }
$o{"continue"} sub { my($count, $cost) = @_; return 0 if $count <= 3; return 1; }
Truth table33%1/3
A //= B
A= $o{"continue"}B= sub { my($count, $cost) = @_; return 0 if $count <= 3; return 1; }
ABresult
1X1
011
000
83
84 1 my $heap = Heap::Priority->new;
85 1 $heap->lowest_first;
86 1 $heap->add($_, $o{cost}->($_, $_+1)) for 0 .. $#q - 1;
87
88 ###################################################################
89 # I haven't found a good solution to maintain the heap and modify
90 # the list, so as a workaround the heap identifies a mergable pair
91 # with the key and when merging elements of a pair, the second
92 # element is replaced by `undef` to maintain the size of the list,
93 # so the heap keys, indices into the list, remain valid. This has
94 # the consequence of producing gaps in the list, and the variables
95 # below maintain how the gaps can be skipped.
96 ###################################################################
97 1 my %next = map { $_ => $_ + 1 } 0 .. $#q - 1;
98 1 my %prev = map { $_ => $_ - 1 } 1 .. $#q - 1;
99
100 1 for (my $count = @q;;) {
101 98 my $ix = $heap->pop;
102 98 ▶ last unless defined $ix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ix 0 98 98
103
104 #################################################################
105 # Ordinarily it should be possible to obtain the priority of the
106 # element on top of the heap, but the chosen module can report
107 # only the priorities of all elements, which is a bit costly, so
108 # instead the cost is re-computed here for now.
109 #################################################################
110 98 my $cost = $o{cost}->($ix, $next{$ix});
111
112 #################################################################
113 # The callback can be by calling code to stop the merging process
114 #################################################################
115 98 ▶ last unless $o{continue}->($count, $cost);
Branch: 1 branch
BranchTrueFalseTotal
unless $o{"continue"}->($count, $cost) 1 97 98
116
117 97 my $k = $ix;
118 97 my $j = $next{$k};
119
120 97 ▶ next unless defined $j;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $j 0 97 97
121
122 97 my @merged = map { @{ $q[$_] } } $k, $j;
123 97 $q[$k] = undef;
124 97 $q[$j] = undef;
125 97 splice @q, $k, 2, [@merged], undef;
126 97 $count--;
127
128 #################################################################
129 # Now that $k has changed, merging $k with the element before or
130 # after has a different cost, so those elements are removed from
131 # the heap and added again with the newly calculated cost factor.
132 #################################################################
133 97 ▶ $heap->delete_item($next{$k}) if defined $next{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $next{$k} 97 0 97
134 97 ▶ $heap->delete_item($prev{$k}) if defined $prev{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev{$k} 93 4 97
135
136 97 $next{$k} = $next{$j};
137
138 97 ▶ $heap->add($prev{$k}, $o{cost}->($prev{$k}, $k)) if defined $prev{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $prev{$k} 93 4 97
139 97 ▶ $heap->add($k, $o{cost}->($k, $next{$k})) if defined $next{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $next{$k} 92 5 97
140
141 97 ▶ $prev{$next{$j}} = $k if defined $next{$j};
Branch: 1 branch
BranchTrueFalseTotal
if defined $next{$j} 92 5 97
142
143 97 delete $next{$j};
144 97 delete $prev{$j};
145 }
146
147 1 my @temp = grep { defined } @q;
148 1 my @result;
149 1 my $pos = 0;
150 1 ▶ for (my $ix = 0; $ix < @temp; ++$ix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < @temp) 3 1 4
151 push @result, [
152 [ $pos, $temp[$ix][0] ],
153 3 [ $pos + scalar(@{$temp[$ix]}) - 1, $temp[$ix][-1] ]
154 ];
155 3 $pos += scalar(@{$temp[$ix]})
156 }
157
158 1 return @result;
159 }
160
161 4 1;
162
163 __END__
↑ Summary