blib/lib/Math/Vector/BestRotation.pm

filter: Statementstmt 84.3% 140 / 166 Branchbran 62.5% 35 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.0% 23 / 25 Podpod 100% 14 / 14 totaltotal 81.2% 212 / 261 CC 30 SCAR 16.6
CC
30
Cov
79%
_compute_bases
13.2
rotation_axis
7.6
init
5.7
CRAP 5.3
↑ Summary
1 package Math::Vector::BestRotation;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use List::Util qw(sum max);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Math::MatrixReal;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 C<Math::Vector::BestRotation> - best rotation to match two vector sets
15  
16 =head1 VERSION
17  
18 Version 0.009
19  
20 =cut
21  
22 6 our $VERSION = '0.009';
23  
24  
25 ###########################################################################
26 # #
27 # Init Process #
28 # #
29 ###########################################################################
30  
31 9 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
32 9 my ($class, @args) = @_;
33  
34 9 my $self = bless {}, $class;
35 9 $self->init(@args);
36 9 return $self;
37 }
38  
39 9 ▶sub init {
Subroutine init: called CC=3 SCAR=17.3
Pod documented
40 9 my ($self, %args) = @_;
41  
42 9 $self->{matrix_r} = [0, 0, 0, 0, 0, 0, 0, 0, 0];
43  
44 9 foreach(keys %args) {
45 0 my $meth = $_;
46 0 ▶ if($self->can($meth)) { $self->$meth($args{$meth}) }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($meth)) { } 0 0 0
47 0 else { carp "Unrecognized init parameter $meth.\n" }
48 }
49 }
50  
51 ###########################################################################
52 # #
53 # Accessors #
54 # #
55 ###########################################################################
56  
57 59 ▶sub matrix_r {
Subroutine matrix_r: called CC=2 SCAR=7.1
Pod documented
58 59 my ($self, @args) = @_;
59  
60 59 ▶ croak "Attribute matrix_r is readonly.\n" if(@args);
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 59 59
61 return Math::MatrixReal->new_from_rows
62 ([[$self->{matrix_r}->[0],
63 $self->{matrix_r}->[1],
64 $self->{matrix_r}->[2]],
65 [$self->{matrix_r}->[3],
66 $self->{matrix_r}->[4],
67 $self->{matrix_r}->[5]],
68 [$self->{matrix_r}->[6],
69 $self->{matrix_r}->[7],
70 59 $self->{matrix_r}->[8]]]);
71 }
72  
73 11 ▶sub matrix_u {
Subroutine matrix_u: called CC=2 SCAR=6.9
Pod documented
74 11 my ($self, @args) = @_;
75  
76 11 ▶ croak "Attribute matrix_u is readonly.\n" if(@args);
Branch: 1 branch
BranchTrueFalseTotal
if @args 1 10 11
77 10 return $self->{matrix_u};
78 }
79  
80 ###########################################################################
81 # #
82 # Retrieve Data #
83 # #
84 ###########################################################################
85  
86 27 ▶sub _compute_bases {
Subroutine _compute_bases: called CC=10 SCAR=25.8
87 27 my ($self) = @_;
88 27 my $r = $self->matrix_r;
89 27 my $rtr = (~$r) * $r;
90 27 my ($mu, $a) = $rtr->sym_diagonalize;
91  
92 27 my $sorted = 0;
93 27 ▶ while(!$sorted) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sorted) 62 27 89
94 62 $sorted = 1;
95 62 ▶ if(abs($mu->element(1, 1)) < abs($mu->element(2, 1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $mu->element(1, 1) < abs $mu->element(2, 1)) 32 30 62
96 32 $sorted = 0;
97 32 $mu->swap_row(1, 2);
98 32 $a->swap_col(1, 2);
99 }
100 62 ▶ if(abs($mu->element(2, 1)) < abs($mu->element(3, 1))) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $mu->element(2, 1) < abs $mu->element(3, 1)) 18 44 62
101 18 $sorted = 0;
102 18 $mu->swap_row(2, 3);
103 18 $a->swap_col(2, 3);
104 }
105 }
106  
107 # make sure, eigensystem is right-handed
108 27 my $a3 = $a->column(1)->vector_product($a->column(2));
109 27 $a->assign(1, 3, $a3->element(1, 1));
110 27 $a->assign(2, 3, $a3->element(2, 1));
111 27 $a->assign(3, 3, $a3->element(3, 1));
112  
113 27 ▶ if($mu->element(1, 1) < 1e-12) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mu->element(1, 1) < "1e-12") 0 27 27
114 0 carp("The largest eigenvalue of R^t*R is smaller than ".
115 "1e-12. The code will try to go on, but may die with ".
116 "division by zero later. In any case, numerical ".
117 "instability of the result has to be expected.\n");
118 }
119  
120 27 my $b = []; # matrix of column vectors b_k
121 27 my $v; # vector buffer
122 my $l; # length buffer
123 27 my $col; # column matrix buffer
124  
125 # determine b_1
126 27 $col = $r * $a->column(1);
127 27 $v = [map { $col->element($_, 1) } (1, 2, 3)];
128 27 $l = sqrt(sum(map { $v->[$_]**2 } (0, 1, 2)));
129  
130 # If length is zero all b's are arbitrary. Otherwise normalize.
131 27 ▶ if($l == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l == 0) { } 0 27 27
132 0 carp("R is (too close to) the nullmatrix. ".
133 "The result will be arbitrary.\n");
134 0 $v = [1, 0, 0];
135 }
136 27 else { @$v = map { $_ / $l } @$v }
137 27 @$b[0, 3, 6] = @$v;
138  
139 # determine b_2
140 27 $col = $r * $a->column(2);
141 27 $v = [map { $col->element($_, 1) } (1, 2, 3)];
142 27 $l = sqrt(sum(map { $v->[$_]**2 } (0, 1, 2)));
143  
144 # if length is zero, we choose a vector perpendicular to b_1
145 27 ▶ if($l == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l == 0) { } 0 27 27
146 0 carp("The two smaller eigenvalues are 0. The result is not ".
147 "unique.\n");
148  
149 0 ▶ if(abs($b->[0]) < abs($b->[3])) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $b->[0] < abs $b->[3]) { } 0 0 0
150 0 ▶ if(abs($b->[0]) < abs($b->[6])) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $b->[0] < abs $b->[6]) { } 0 0 0
151 0 $b->[1] = 0;
152 0 $b->[4] = -$b->[6];
153 0 $b->[7] = $b->[3];
154 }
155 else {
156 0 $b->[1] = -$b->[3];
157 0 $b->[4] = $b->[0];
158 0 $b->[7] = 0;
159 }
160 }
161 else {
162 0 ▶ if(abs($b->[3]) < abs($b->[6])) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $b->[3] < abs $b->[6]) { } 0 0 0
163 0 $b->[1] = -$b->[6];
164 0 $b->[4] = 0;
165 0 $b->[7] = $b->[0];
166 }
167 else {
168 0 $b->[1] = -$b->[3];
169 0 $b->[4] = $b->[0];
170 0 $b->[7] = 0;
171 }
172 }
173 }
174 # otherwise we normalize carefully
175 else {
176 27 @$v = map { $_ / $l } @$v;
177  
178 # $l could have been very small, though.
179 # Therefore, we make b_2 orthogonal to b_1 and normalize again.
180 27 my $p = $v->[0] * $b->[0] + $v->[1] * $b->[3] + $v->[2] * $b->[6];
181 27 @$v = map { $v->[$_] - $p * $b->[3*$_] } (0, 1, 2);
182 27 $l = sqrt(sum(map { $v->[$_]**2 } (0, 1, 2)));
183 27 @$b[1, 4, 7] = map { $_ / $l } @$v;
184 }
185  
186 # determine b_3 as by cross product
187 27 $b->[2] = $b->[3] * $b->[7] - $b->[6] * $b->[4];
188 27 $b->[5] = $b->[6] * $b->[1] - $b->[0] * $b->[7];
189 27 $b->[8] = $b->[0] * $b->[4] - $b->[3] * $b->[1];
190  
191 27 return($a, Math::MatrixReal->new_from_rows
192 ([[@$b[0, 1, 2]], [@$b[3, 4, 5]], [@$b[6, 7, 8]]]));
193 }
194  
195 27 ▶sub _matrix_a_to_b {
Subroutine _matrix_a_to_b: called CC=1 SCAR=0.0
196 27 my ($self, $a, $b) = @_;
197  
198 27 return($b * (~$a));
199 }
200  
201 9 ▶sub best_orthogonal {
Subroutine best_orthogonal: called CC=2 SCAR=6.9
Pod documented
202 9 my ($self) = @_;
203 9 my ($a, $b) = $self->_compute_bases;
204  
205 9 ▶ if($self->matrix_r->det < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->matrix_r->det < 0) 2 7 9
206 2 $b = $b * Math::MatrixReal->new_from_rows
207 ([[1, 0, 0], [0, 1, 0], [0, 0, -1]]);
208 }
209  
210 9 $self->{matrix_u} = $self->_matrix_a_to_b($a, $b);
211 9 return $self->{matrix_u};
212 }
213  
214 13 ▶sub best_rotation {
Subroutine best_rotation: called CC=1 SCAR=0.0
Pod documented
215 13 my ($self) = @_;
216 13 my ($a, $b) = $self->_compute_bases;
217  
218 13 $self->{matrix_u} = $self->_matrix_a_to_b($a, $b);
219 13 return $self->{matrix_u};
220 }
221  
222 0 ▶sub best_proper_rotation { shift(@_)->best_rotation(@_) }
Subroutine best_proper_rotation: not called CC=1 SCAR=6.9
Pod documented
223  
224 5 ▶sub best_improper_rotation {
Subroutine best_improper_rotation: called CC=1 SCAR=0.0
Pod documented
225 5 my ($self) = @_;
226 5 my ($a, $b) = $self->_compute_bases;
227  
228 5 $b = $b * Math::MatrixReal->new_diag([1, 1, -1]);
229  
230 5 $self->{matrix_u} = $self->_matrix_a_to_b($a, $b);
231 5 return $self->{matrix_u};
232 }
233  
234 5 ▶sub best_flipped_rotation { shift(@_)->best_improper_rotation(@_) }
Subroutine best_flipped_rotation: called CC=1 SCAR=0.0
Pod documented
235  
236 3 ▶sub rotation_axis {
Subroutine rotation_axis: called CC=7 SCAR=20.2
Pod documented
237 3 my ($self, @args) = @_;
238 3 ▶ my $matrix = @args > 0 ? $args[0] : $self->matrix_u;
Branch: 1 branch
BranchTrueFalseTotal
@args > 0 ? : 0 3 3
239  
240 3 ▶ croak("Unable to find the rotation axis of an undefined matrix. ".
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 3 3
241 "Has any rotation been calculated, yet?.\n")
242 if(!defined($matrix));
243 croak("Wrong type of matrix given to rotation_matrix.\n")
244 3 ▶ if(!eval { $matrix->isa('Math::MatrixReal') } );
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $matrix->isa("Math::MatrixReal") } } 0 3 3
245 3 ▶ croak("The calculation of a rotation axis of an improper rotation ".
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->det < 0 0 3 3
246 "is not supported.\n")
247 if($matrix->det < 0);
248  
249 3 my $unity = Math::MatrixReal->new_diag([1, 1, 1]);
250 3 my $m = $matrix + ~$matrix - ($matrix->trace - 1) * $unity;
251  
252 3 my @col_lengths = map { $m->col($_)->length } (1, 2, 3);
253  
254 3 my $axis;
255 3 ▶ if($col_lengths[0] >= max($col_lengths[1], $col_lengths[2])) {
Branch: 2 branches
BranchTrueFalseTotal
if ($col_lengths[0] >= &max($col_lengths[1], $col_lengths[2])) { } 2 1 3
elsif ($col_lengths[1] >= &max($col_lengths[2], $col_lengths[0])) { } 0 1 1
256 2 ▶ $axis = $m->col(1)->each(sub { $_[0] / $col_lengths[0] });
Subroutine __ANON__: called CC=1 SCAR=0.0
257 }
258 elsif($col_lengths[1] >= max($col_lengths[2], $col_lengths[0])) {
259 0 ▶ $axis = $m->col(2)->each(sub { $_[0] / $col_lengths[1] });
Subroutine __ANON__: not called CC=1 SCAR=6.9
260 }
261 else {
262 1 ▶ $axis = $m->col(3)->each(sub { $_[0] / $col_lengths[2] });
Subroutine __ANON__: called CC=1 SCAR=0.0
263 }
264
265 3 return $axis;
266 }
267  
268 3 ▶sub rotation_angle {
Subroutine rotation_angle: called CC=5 SCAR=16.8
Pod documented
269 3 my ($self, @args) = @_;
270 3 ▶ my $matrix = @args > 0 ? $args[0] : $self->matrix_u;
Branch: 1 branch
BranchTrueFalseTotal
@args > 0 ? : 0 3 3
271  
272 3 ▶ croak("Unable to find the rotation angle of an undefined matrix. ".
Branch: 1 branch
BranchTrueFalseTotal
unless defined $matrix 0 3 3
273 "Has any rotation been calculated, yet?.\n")
274 if(!defined($matrix));
275 croak("Wrong type of matrix given to rotation_matrix.\n")
276 3 ▶ if(!eval { $matrix->isa('Math::MatrixReal') } );
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $matrix->isa("Math::MatrixReal") } } 0 3 3
277 3 ▶ croak("The calculation of a rotation angle of an improper rotation ".
Branch: 1 branch
BranchTrueFalseTotal
if $matrix->det < 0 0 3 3
278 "is not supported.\n")
279 if($matrix->det < 0);
280  
281 3 my $cos = ($matrix->trace - 1) / 2;
282  
283 3 return abs(atan2(sqrt(1 - $cos**2), $cos));
284 }
285  
286 ###########################################################################
287 # #
288 # Change Data #
289 # #
290 ###########################################################################
291  
292 40 ▶sub add_pair {
Subroutine add_pair: called CC=3 SCAR=11.0
Pod documented
293 40 my ($self, $x, $y) = @_;
294 40 my $matrix_r = $self->{matrix_r};
295  
296 40 ▶ for(my $i=0;$i<3;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 3) 120 40 160
297 120 ▶ for(my $j=0;$j<3;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 3) 360 120 480
298 360 $matrix_r->[3*$i+$j] += $y->[$i] * $x->[$j];
299 }
300 }
301 }
302  
303 8 ▶sub add_many_pairs {
Subroutine add_many_pairs: called CC=4 SCAR=13.9
Pod documented
304 8 my ($self, $x, $y) = @_;
305 8 my $matrix_r = $self->{matrix_r};
306  
307 8 ▶ for(my $n=0;$n<@$x;$n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < @$x) 23 8 31
308 23 ▶ for(my $i=0;$i<3;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 3) 69 23 92
309 69 ▶ for(my $j=0;$j<3;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 3) 207 69 276
310 207 $matrix_r->[3*$i+$j] += $y->[$n]->[$i] * $x->[$n]->[$j];
311 }
312 }
313 }
314 }
315  
316 20 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
317 20 my ($self) = @_;
318  
319 20 $self->{matrix_r} = [0, 0, 0, 0, 0, 0, 0, 0, 0];
320 20 $self->{matrix_u} = undef;
321 }
322  
323  
324 6 1;
325  
326 __END__
↑ Summary