| 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 | use Math::Trig; | ||
| 40 | use Math::Trig ':radial'; | ||
| 41 | use strict; | ||
| 42 | use warnings; | ||
| 43 | |||
| 44 | use Exporter; | ||
| 45 | use vars qw(@ISA @EXPORT); | ||
| 46 | @ISA = qw(Exporter); | ||
| 47 | @EXPORT = qw(rollpitchyaw2matrix matrix2rollpitchyaw multiply); | ||
| 48 | |||
| 49 | sub rollpitchyaw2matrix { | ||
| 50 | my ($roll, $pitch, $yaw) = @_; | ||
| 51 | |||
| 52 | my $cosr = cos ($roll); | ||
| 53 | my $sinr = sin ($roll); | ||
| 54 | my $cosp = cos ($pitch); | ||
| 55 | my $sinp = sin (0 - $pitch); | ||
| 56 | my $cosy = cos ($yaw); | ||
| 57 | my $siny = sin (0 - $yaw); | ||
| 58 | |||
| 59 | my $rollm = [[ 1, 0, 0 ], | ||
| 60 | [ 0, $cosr,-1*$sinr ], | ||
| 61 | [ 0, $sinr, $cosr ]]; | ||
| 62 | |||
| 63 | my $pitchm = [[ $cosp, 0, $sinp ], | ||
| 64 | [ 0, 1, 0 ], | ||
| 65 | [ -1*$sinp, 0, $cosp ]]; | ||
| 66 | |||
| 67 | my $yawm = [[ $cosy,-1*$siny, 0 ], | ||
| 68 | [ $siny, $cosy, 0 ], | ||
| 69 | [ 0, 0, 1 ]]; | ||
| 70 | |||
| 71 | my $foo = multiply ($yawm, $pitchm); | ||
| 72 | multiply ($foo, $rollm); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub transpose | ||
| 76 | { | ||
| 77 | my $matrix_in = shift; | ||
| 78 | my $matrix_out; | ||
| 79 | |||
| 80 | my $n = 0; | ||
| 81 | for my $row (@{$matrix_in}) | ||
| 82 | { | ||
| 83 | my $m = 0; | ||
| 84 | for my $column (@{$row}) | ||
| 85 | { | ||
| 86 | $matrix_out->[$m]->[$n] = $matrix_in->[$n]->[$m]; | ||
| 87 | $m++; | ||
| 88 | } | ||
| 89 | $n++; | ||
| 90 | } | ||
| 91 | return $matrix_out; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub multiply | ||
| 95 | { | ||
| 96 | my $matrix_a = shift; | ||
| 97 | my $transposed_b = transpose (shift); | ||
| 98 | my $matrix_out; | ||
| 99 | |||
| 100 | return undef if (scalar @{$matrix_a->[0]} != scalar @{$transposed_b->[0]}); | ||
| 101 | for my $row (@{$matrix_a}) | ||
| 102 | { | ||
| 103 | my $rescol = []; | ||
| 104 | for my $column (@{$transposed_b}) | ||
| 105 | { | ||
| 106 | push (@{$rescol}, vekpro ($row, $column)); | ||
| 107 | } | ||
| 108 | push (@{$matrix_out}, $rescol); | ||
| 109 | } | ||
| 110 | return $matrix_out; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub vekpro | ||
| 114 | { | ||
| 115 | my ($a, $b) = @_; | ||
| 116 | my $result = 0; | ||
| 117 | |||
| 118 | for my $i (0 .. scalar @{$a} - 1) | ||
| 119 | { | ||
| 120 | $result += $a->[$i] * $b->[$i]; | ||
| 121 | } | ||
| 122 | $result; | ||
| 123 | } | ||
| 124 | |||
| 125 | # following copied from a spreadsheet by Stuart Milne | ||
| 126 | |||
| 127 | sub matrix2rollpitchyaw | ||
| 128 | { | ||
| 129 | my $matrix = shift; | ||
| 130 | my $roll = atan2 ($matrix->[2]->[1], $matrix->[2]->[2]); | ||
| 131 | my $pitch = -1 * asin (-1 * $matrix->[2]->[0]); | ||
| 132 | my $yaw = atan2 (-1 * $matrix->[1]->[0], $matrix->[0]->[0]); | ||
| 133 | return ($roll, $pitch, $yaw); | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; | ||
| 137 |