Simple/Gesture.pm

filter: Statementstmt 100% 103 / 103 Branchbran 100% 12 / 12 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 15 / 15 Podpod 10.0% 1 / 10 totaltotal 87.6% 135 / 154 CC 17 SCAR 13.0
CC
17
Cov
96%
bounding_box
8.0
resample
4.0
scale_to_square
3.0
CRAP 3.7
↑ Summary Simple.pm »
1 package Gesture::Simple::Gesture;
2 1 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 has points => (
5 is => 'ro',
6 isa => 'ArrayRef',
7 required => 1,
8 );
9  
10 3 ▶sub _set_points { $_[0]->{points} = $_[1] }
Subroutine _set_points: called CC=1 SCAR=0.0
11  
12 3 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
13 3 my $self = shift;
14  
15 3 my $resampled = $self->resample;
16 3 my $rotated = $self->rotate_to_zero($resampled);
17 3 my $scaled = $self->scale_to_square($rotated);
18 3 my $translated = $self->translate_to_origin($scaled);
19  
20 3 $self->_set_points($translated);
21 }
22  
23 1 ▶use constant resample_point_count => 64;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 3 ▶sub resample {
Subroutine resample: called CC=4 SCAR=13.9
Pod undocumented
26 3 my $self = shift;
27 3 ▶ my @points = @{ shift || $self->points };
Condition33%1/3
shift() || $self->points
l!l&&r!l&&!r
030
MC/DC0%0/2
shift() || $self->points
shift() $self->points
Truth table33%1/3
A || B
A= shift()B= $self->points
ABresult
1X1
011
000
28 3 my @new_points = $points[0];
29  
30 3 my $I = $self->path_length(\@points) / ($self->resample_point_count - 1);
31 3 my $D = 0;
32  
33 3 ▶ for (my $i = 1; $i < @points; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @points) 292 3 295
34 292 my ($a, $b) = @points[$i-1, $i];
35 292 my $d = $self->distance($a, $b);
36  
37 292 ▶ if ($D + $d >= $I) {
Branch: 1 branch
BranchTrueFalseTotal
if ($D + $d >= $I) { } 188 104 292
38 188 my $q_x = $a->[0] + (($I - $D) / $d) * ($b->[0] - $a->[0]);
39 188 my $q_y = $a->[1] + (($I - $D) / $d) * ($b->[1] - $a->[1]);
40 188 my $q = [$q_x, $q_y];
41  
42 188 push @new_points, $q;
43 188 splice @points, $i, 0, $q;
44 188 $D = 0;
45 }
46 else {
47 104 $D += $d;
48 }
49 }
50  
51 3 return \@new_points;
52 }
53  
54 3 ▶sub path_length {
Subroutine path_length: called CC=2 SCAR=6.9
Pod undocumented
55 3 my $self = shift;
56 3 my $points = shift;
57  
58 3 my $length = 0;
59 3 for my $i (1 .. @$points - 1) {
60 104 my ($a, $b) = @{$points}[$i-1, $i];
61 104 $length += $self->distance($a, $b);
62 }
63  
64 3 return $length;
65 }
66  
67 8908 ▶sub distance {
Subroutine distance: called CC=1 SCAR=0.0
Pod undocumented
68 8908 my (undef, $a, $b) = @_;
69 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
70 8908 return sqrt( ($a->[0] - $b->[0]) ** 2 + ($a->[1] - $b->[1]) ** 2 );
71 }
72  
73 142 ▶sub centroid {
Subroutine centroid: called CC=2 SCAR=6.9
Pod undocumented
74 142 my $self = shift;
75 142 my $points = shift;
76  
77 142 my ($X, $Y) = (0, 0);
78  
79 142 for my $point (@$points) {
80 9028 $X += $point->[0];
81 9028 $Y += $point->[1];
82 }
83  
84 142 return [ $X / @$points, $Y / @$points ];
85 }
86  
87 3 ▶sub rotate_to_zero {
Subroutine rotate_to_zero: called CC=1 SCAR=0.0
Pod undocumented
88 3 my $self = shift;
89 3 my $points = shift;
90  
91 3 my $c = $self->centroid($points);
92 3 my $theta = atan2($c->[1] - $points->[0][1], $c->[0] - $points->[0][0]);
93  
94 3 return $self->rotate_by($points, -$theta);
95 }
96  
97 136 ▶sub rotate_by {
Subroutine rotate_by: called CC=2 SCAR=6.9
Pod undocumented
98 136 my $self = shift;
99 136 my $points = shift;
100 136 my $theta = shift;
101  
102 136 my $c = $self->centroid($points);
103  
104 136 my @new_points;
105  
106 136 for my $point (@$points) {
107 8646 my $x = ($point->[0] - $c->[0]) * cos($theta)
108 - ($point->[1] - $c->[1]) * sin($theta)
109 + $c->[0];
110  
111 8646 my $y = ($point->[0] - $c->[0]) * sin($theta)
112 + ($point->[1] - $c->[1]) * cos($theta)
113 + $c->[1];
114  
115 8646 push @new_points, [$x, $y];
116 }
117  
118 136 return \@new_points;
119 }
120  
121 3 ▶sub scale_to_square {
Subroutine scale_to_square: called CC=3 SCAR=11.0
Pod undocumented
122 3 my $self = shift;
123 3 my $points = shift;
124 3 ▶ my $size = shift || 100;
Condition50%1/2
shift() || 100
l!l
03
MC/DC0%0/1
shift() || 100
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
125  
126 3 my @new_points;
127  
128 3 my ($width, $height) = $self->bounding_box($points);
129  
130 3 for my $point (@$points) {
131 191 my $x = $point->[0] * ($size / $width);
132 191 my $y = $point->[1] * ($size / $height);
133  
134 191 push @new_points, [$x, $y];
135 }
136  
137 3 return \@new_points;
138 }
139  
140 3 ▶sub bounding_box {
Subroutine bounding_box: called CC=8 SCAR=20.8
Pod undocumented
141 3 my $self = shift;
142 3 my $points = shift;
143  
144 3 my ($min_x, $min_y, $max_x, $max_y) = (@{ $points->[0] }) x 2;
145  
146 3 for my $point (@$points) {
147 191 ▶ $min_x = $point->[0] if $point->[0] < $min_x;
Branch: 1 branch
BranchTrueFalseTotal
if $point->[0] < $min_x 9 182 191
148 191 ▶ $min_y = $point->[1] if $point->[1] < $min_y;
Branch: 1 branch
BranchTrueFalseTotal
if $point->[1] < $min_y 62 129 191
149  
150 191 ▶ $max_x = $point->[0] if $point->[0] > $max_x;
Branch: 1 branch
BranchTrueFalseTotal
if $point->[0] > $max_x 152 39 191
151 191 ▶ $max_y = $point->[1] if $point->[1] > $max_y;
Branch: 1 branch
BranchTrueFalseTotal
if $point->[1] > $max_y 66 125 191
152 }
153  
154 3 ▶ my $width = ($max_x - $min_x) || 1;
Condition50%1/2
$max_x - $min_x || 1
l!l
30
MC/DC0%0/1
$max_x - $min_x || 1
$max_x - $min_x
Truth table50%1/2
A
A= $max_x - $min_x
Aresult
11
00
155 3 ▶ my $height = ($max_y - $min_y) || 1;
Condition50%1/2
$max_y - $min_y || 1
l!l
30
MC/DC0%0/1
$max_y - $min_y || 1
$max_y - $min_y
Truth table50%1/2
A
A= $max_y - $min_y
Aresult
11
00
156  
157 3 return ($width, $height);
158 }
159  
160 3 ▶sub translate_to_origin {
Subroutine translate_to_origin: called CC=2 SCAR=6.9
Pod undocumented
161 3 my $self = shift;
162 3 my $points = shift;
163  
164 3 my $c = $self->centroid($points);
165 3 my @new_points;
166  
167 3 for my $point (@$points) {
168 191 my $x = $point->[0] - $c->[0];
169 191 my $y = $point->[1] - $c->[1];
170 191 push @new_points, [$x + 50, $y + 50];
171 }
172  
173 3 return \@new_points;
174 }
175  
176 1 __PACKAGE__->meta->make_immutable;
177 1 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
178  
179 1 1;
180  
↑ Summary Simple.pm »