Simple/Template.pm

filter: Statementstmt 100% 70 / 70 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 5 totaltotal 95.0% 95 / 100 CC 5 SCAR 5.1
CC
5
Cov
100%
distance_at_best_angle
4.0
path_distance
2.0
BEGIN
1.0
CRAP 1.7
« Simple.pm ↑ Summary Simple/Match.pm »
1 package Gesture::Simple::Template;
2 1 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 extends 'Gesture::Simple::Gesture';
4 1 ▶use Scalar::Defer qw/defer force/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Gesture::Simple::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use constant match_class => 'Gesture::Simple::Match';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 has name => (
11 is => 'ro',
12 isa => 'Str',
13 required => 1,
14 );
15  
16 7 ▶sub match {
Subroutine match: called CC=1 SCAR=0.0
Pod undocumented
17 7 my $self = shift;
18 7 my $gesture = shift;
19  
20 7 my $raw_score = $self->score_match($gesture);
21  
22 7 return $self->match_class->new(
23 template => $self,
24 gesture => $gesture,
25 score => $raw_score,
26 );
27 }
28  
29 7 ▶sub score_match {
Subroutine score_match: called CC=1 SCAR=0.0
Pod undocumented
30 7 my $self = shift;
31 7 my $gesture = shift;
32 7 my $distance = $self->distance_at_best_angle($gesture);
33  
34 7 my $score = 1 - $distance / (.5 * sqrt(100 ** 2 + 100 ** 2));
35 7 return $score * 100;
36 }
37  
38 1 ▶use constant minimum_angle => -0.785398163; # -45 degrees
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use constant maximum_angle => 0.785398163; # 45 degrees
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use constant angle_threshold => 0.034906585; # 2 degrees
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 7 ▶sub distance_at_best_angle {
Subroutine distance_at_best_angle: called CC=4 SCAR=13.9
Pod undocumented
43 7 my $self = shift;
44 7 my $gesture = shift;
45  
46 7 my $theta_a = $self->minimum_angle;
47 7 my $theta_b = $self->maximum_angle;
48 7 my $threshold = $self->angle_threshold;
49  
50 7 my $phi = .61803399; # golden ratio
51  
52 7 ▶ my $x1 = defer { $phi * $theta_a + (1 - $phi) * $theta_b };
Subroutine __ANON__: called CC=1 SCAR=0.0
53 7 ▶ my $x2 = defer { (1 - $phi) * $theta_a + $phi * $theta_b };
Subroutine __ANON__: called CC=1 SCAR=0.0
54  
55 7 ▶ my $f1 = defer { $self->distance_at_angle($gesture, force $x1) };
Subroutine __ANON__: called CC=1 SCAR=0.0
56 7 ▶ my $f2 = defer { $self->distance_at_angle($gesture, force $x2) };
Subroutine __ANON__: called CC=1 SCAR=0.0
57  
58 7 ▶ while (abs($theta_b - $theta_a) > $threshold) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $theta_b - $theta_a > $threshold) 56 7 63
59 56 ▶ if ($f1 < $f2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f1 < $f2) { } 34 22 56
60 34 $theta_b = force $x2;
61 }
62 else {
63 22 $theta_a = force $x1;
64 }
65 }
66  
67 7 ▶ return $f1 < $f2 ? $f1 : $f2;
Branch: 1 branch
BranchTrueFalseTotal
$f1 < $f2 ? : 3 4 7
68 }
69  
70 133 ▶sub distance_at_angle {
Subroutine distance_at_angle: called CC=1 SCAR=0.0
Pod undocumented
71 133 my $self = shift;
72 133 my $gesture = shift;
73 133 my $theta = shift;
74  
75 133 my $rotated = $gesture->rotate_by($gesture->points, $theta);
76 133 return $self->path_distance($rotated);
77 }
78  
79 133 ▶sub path_distance {
Subroutine path_distance: called CC=2 SCAR=6.9
Pod undocumented
80 133 my $self = shift;
81 133 my $points = shift;
82 133 my $template = $self->points;
83  
84 133 my $distance = 0;
85  
86 133 ▶ for (my $i = 0; $i < @$template; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$template) 8512 133 8645
87 8512 my $d = $self->distance($template->[$i], $points->[$i]);
88 8512 $distance += $d;
89 }
90  
91 133 $distance /= @$template;
92  
93 133 return $distance;
94 }
95  
96 1 __PACKAGE__->meta->make_immutable;
97 1 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 1 1;
100  
« Simple.pm ↑ Summary Simple/Match.pm »