lib/Panotools/Matrix.pm

filter: Statementstmt 100% 72 / 72 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 5 totaltotal 93.3% 84 / 90 CC 7 SCAR 7.8
CC
7
Cov
99%
multiply
4.0
transpose
3.0
vekpro
2.0
CRAP 2.2
« lib/Panotools/Script/Line.pm ↑ Summary lib/Panotools/Makefile/Variable.pm »
1 package Panotools::Matrix;
2  
3 =head1 NAME
4  
5 Panotools::Matrix - Miscellaneous math for panoramic images
6  
7 =head1 SYNOPSIS
8  
9 $matrix = rollpitchyaw2matrix ($roll, $pitch, $yaw);
10  
11 All angles are in radians not degrees.
12  
13 =head1 DESCRIPTION
14  
15 rollpitchyaw2matrix returns a matrix arrayref that encapsulates a
16 transformation suitable for rotating a vector/point by three degrees of freedom
17 (roll, pitch and yaw).
18  
19 roll is positive rotation around the x-axis
20  
21 pitch is negative rotation around the y-axis
22  
23 yaw is negative rotation around the z axis
24  
25 =head1 USAGE
26  
27 use Panotools::Matrix qw(matrix2rollpitchyaw rollpitchyaw2matrix multiply);
28  
29 my $point = [[$x1], [$y1], [$z1]];
30  
31 my $matrix = rollpitchyaw2matrix ($roll, $pitch, $yaw);
32  
33 my $result = multiply ($matrix, $point);
34  
35 ($x2, $y2, $z2) = ($result->[0][0], $result->[1][0], $result->[2][0]);
36  
37 =cut
38  
39 11 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 11 ▶use Math::Trig ':radial';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 11 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 11 ▶use vars qw(@ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 11 @ISA = qw(Exporter);
47 11 @EXPORT = qw(rollpitchyaw2matrix matrix2rollpitchyaw multiply);
48  
49 28 ▶sub rollpitchyaw2matrix {
Subroutine rollpitchyaw2matrix: called CC=1 SCAR=0.0
Pod undocumented
50 28 my ($roll, $pitch, $yaw) = @_;
51  
52 28 my $cosr = cos ($roll);
53 28 my $sinr = sin ($roll);
54 28 my $cosp = cos ($pitch);
55 28 my $sinp = sin (0 - $pitch);
56 28 my $cosy = cos ($yaw);
57 28 my $siny = sin (0 - $yaw);
58  
59 28 my $rollm = [[ 1, 0, 0 ],
60 [ 0, $cosr,-1*$sinr ],
61 [ 0, $sinr, $cosr ]];
62  
63 28 my $pitchm = [[ $cosp, 0, $sinp ],
64 [ 0, 1, 0 ],
65 [ -1*$sinp, 0, $cosp ]];
66  
67 28 my $yawm = [[ $cosy,-1*$siny, 0 ],
68 [ $siny, $cosy, 0 ],
69 [ 0, 0, 1 ]];
70  
71 28 my $foo = multiply ($yawm, $pitchm);
72 28 multiply ($foo, $rollm);
73 }
74  
75 sub transpose
76 77 ▶{
Subroutine transpose: called CC=3 SCAR=11.0
Pod undocumented
77 77 my $matrix_in = shift;
78 77 my $matrix_out;
79  
80 77 my $n = 0;
81 77 for my $row (@{$matrix_in})
82 {
83 231 my $m = 0;
84 231 for my $column (@{$row})
85 {
86 621 $matrix_out->[$m]->[$n] = $matrix_in->[$n]->[$m];
87 621 $m++;
88 }
89 231 $n++;
90 }
91 77 return $matrix_out;
92 }
93  
94 sub multiply
95 76 ▶{
Subroutine multiply: called CC=4 SCAR=13.9
Pod undocumented
96 76 my $matrix_a = shift;
97 76 my $transposed_b = transpose (shift);
98 76 my $matrix_out;
99  
100 76 ▶ return undef if (scalar @{$matrix_a->[0]} != scalar @{$transposed_b->[0]});
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$matrix_a->[0];} != scalar @{$transposed_b->[0];} 0 76 76
101 76 for my $row (@{$matrix_a})
102 {
103 228 my $rescol = [];
104 228 for my $column (@{$transposed_b})
105 {
106 612 push (@{$rescol}, vekpro ($row, $column));
107 }
108 228 push (@{$matrix_out}, $rescol);
109 }
110 76 return $matrix_out;
111 }
112  
113 sub vekpro
114 612 ▶{
Subroutine vekpro: called CC=2 SCAR=6.9
Pod undocumented
115 612 my ($a, $b) = @_;
116 612 my $result = 0;
117  
118 612 for my $i (0 .. scalar @{$a} - 1)
119 {
120 1836 $result += $a->[$i] * $b->[$i];
121 }
122 612 $result;
123 }
124  
125 # following copied from a spreadsheet by Stuart Milne
126  
127 sub matrix2rollpitchyaw
128 8 ▶{
Subroutine matrix2rollpitchyaw: called CC=1 SCAR=0.0
Pod undocumented
129 8 my $matrix = shift;
130 8 my $roll = atan2 ($matrix->[2]->[1], $matrix->[2]->[2]);
131 8 my $pitch = -1 * asin (-1 * $matrix->[2]->[0]);
132 8 my $yaw = atan2 (-1 * $matrix->[1]->[0], $matrix->[0]->[0]);
133 8 return ($roll, $pitch, $yaw);
134 }
135  
136 11 1;
137  
« lib/Panotools/Script/Line.pm ↑ Summary lib/Panotools/Makefile/Variable.pm »