Real.pm

filter: Statementstmt 25.6% 120 / 468 Branchbran 13.2% 23 / 174 Conditioncond 5.8% 1 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 34.3% 23 / 67 Podpod 55.9% 33 / 59 totaltotal 25.1% 200 / 796 CC 134 SCAR 36.5
CC
134
Cov
22%
complementary_base
240.0
dist2_between_segments
90.0
nearest_in_box_border
72.0
CRAP 38.6
↑ Summary Real/Test.pm »
1 package Math::Vector::Real;
2  
3 1 our $VERSION = '0.18';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our @EXPORT = qw(V);
12  
13 1 our $dont_use_XS;
14 1 ▶unless ($dont_use_XS) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Math::Vector::Real::dont_use_XS) 0 1 1
15 0 my $xs_version = do {
16 0 local ($@, $!, $SIG{__DIE__});
17 0 eval {
18 0 require Math::Vector::Real::XS;
19 0 $Math::Vector::Real::XS::VERSION;
20 }
21 };
22  
23 0 ▶ if (defined $xs_version and $xs_version < 0.07) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xs_version and $xs_version < "0.07") 0 0 0
Condition0%0/3
defined $xs_version and $xs_version < "0.07"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $xs_version and $xs_version < "0.07"
defined $xs_version $xs_version < "0.07"
Truth table0%0/3
A and B
A= defined $xs_versionB= $xs_version < "0.07"
ABresult
0X0
100
111
24 0 croak "Old and buggy version of Math::Vector::Real::XS detected, update it!";
25 }
26 }
27  
28  
29 1 our %op = (add => '+',
30 neg => 'neg',
31 sub => '-',
32 mul => '*',
33 div => '/',
34 cross => 'x',
35 add_me => '+=',
36 sub_me => '-=',
37 mul_me => '*=',
38 div_me => '/=',
39 abs => 'abs',
40 atan2 => 'atan2',
41 equal => '==',
42 nequal => '!=',
43 clone => '=',
44 as_string => '""');
45  
46 1 our %ol;
47 1 $ol{$op{$_}} = \&{${Math::Vector::Real::}{$_}} for keys %op;
48  
49 1 require overload;
50 1 overload->import(%ol);
51  
52 6 ▶sub V { bless [@_] }
Subroutine V: called CC=1 SCAR=0.0
Pod undocumented
53  
54 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
55 0 my $class = shift;
56 0 bless [@_], $class
57 }
58  
59 0 ▶sub new_ref {
Subroutine new_ref: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my $class = shift;
61 0 bless [@{shift()}], $class;
62 }
63  
64 0 ▶sub zero {
Subroutine zero: not called CC=2 SCAR=17.9
Pod documented
65 0 my ($class, $dim) = @_;
66 0 ▶ $dim >= 0 or croak "negative dimension";
Branch: 1 branch
BranchTrueFalseTotal
$dim >= 0 or croak("negative dimension") 0 0 0
67 0 bless [(0) x $dim], $class
68 }
69  
70 0 ▶sub is_zero {
Subroutine is_zero: not called CC=3 SCAR=24.8
Pod undocumented
71 0 ▶ $_ and return 0 for @$_[0];
Branch: 1 branch
BranchTrueFalseTotal
$_ and return 0 0 0 0
72 0 return 1
73 }
74  
75 0 ▶sub cube {
Subroutine cube: not called CC=1 SCAR=6.9
Pod documented
76 0 my ($class, $dim, $size) = @_;
77 0 bless [($size) x $dim], $class;
78 }
79  
80 0 ▶sub axis_versor {
Subroutine axis_versor: not called CC=5 SCAR=34.0
Pod documented
81 0 my ($class, $dim, $ix);
82 0 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
83 0 my ($self, $ix) = @_;
84 0 $class = ref $self;
85 0 $dim = @$self;
86 }
87 else {
88 0 ($class, $dim, $ix) = @_;
89 0 ▶ $dim >= 0 or croak "negative dimension";
Branch: 1 branch
BranchTrueFalseTotal
$dim >= 0 or croak("negative dimension") 0 0 0
90 }
91 0 ▶ ($ix >= 0 and $ix < $dim) or croak "axis index out of range";
Branch: 1 branch
BranchTrueFalseTotal
$ix >= 0 and $ix < $dim or croak("axis index out of range") 0 0 0
Condition0%0/3
$ix >= 0 and $ix < $dim
!ll&&!rl&&r
000
MC/DC0%0/2
$ix >= 0 and $ix < $dim
$ix >= 0 $ix < $dim
Truth table0%0/3
A and B
A= $ix >= 0B= $ix < $dim
ABresult
0X0
100
111
92 0 my $self = [(0) x $dim];
93 0 $self->[$ix] = 1;
94 0 bless $self, $class
95 }
96  
97 0 ▶sub _caller_op {
Subroutine _caller_op: not called CC=3 SCAR=24.8
98 0 ▶ my $level = (shift||1) + 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
99 0 my $sub = (caller $level)[3];
100 0 $sub =~ s/.*:://;
101 0 my $op = $op{$sub};
102 0 ▶ (defined $op ? $op : $sub);
Branch: 1 branch
BranchTrueFalseTotal
defined $op ? : 0 0 0
103 }
104  
105 78 ▶sub _check_dim {
Subroutine _check_dim: called CC=4 SCAR=20.1
106 78 local ($@, $SIG{__DIE__});
107 78 ▶ eval { @{$_[0]} == @{$_[1]} } and return;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { @{$_[0];} == @{$_[1];} } } and return 78 0 78
108 0 my $op = _caller_op(1);
109 0 ▶ my $loc = ($_[2] ? 'first' : 'second');
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
110 0 ▶ UNIVERSAL::isa($_[1], 'ARRAY') or croak "$loc argument to vector operator '$op' is not a vector";
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[1], "ARRAY") or croak("$loc argument to vector operator '${op}' is not a vector") 0 0 0
111 0 croak "vector dimensions do not match";
112 }
113  
114 0 ▶sub clone { bless [@{$_[0]}] }
Subroutine clone: not called CC=1 SCAR=6.9
Pod undocumented
115  
116 0 ▶sub set {
Subroutine set: not called CC=2 SCAR=17.9
Pod documented
117 0 &_check_dim;
118 0 my ($v0, $v1) = @_;
119 0 $v0->[$_] = $v1->[$_] for 0..$#$v1;
120 }
121  
122 13 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod undocumented
123 13 &_check_dim;
124 13 my ($v0, $v1) = @_;
125 13 bless [map $v0->[$_] + $v1->[$_], 0..$#$v0]
126 }
127  
128 0 ▶sub add_me {
Subroutine add_me: not called CC=2 SCAR=17.9
Pod undocumented
129 0 &_check_dim;
130 0 my ($v0, $v1) = @_;
131 0 $v0->[$_] += $v1->[$_] for 0..$#$v0;
132 0 $v0;
133 }
134  
135 2 ▶sub neg { bless [map -$_, @{$_[0]}] }
Subroutine neg: called CC=1 SCAR=0.0
Pod undocumented
136  
137 16 ▶sub sub {
Subroutine sub: called CC=2 SCAR=6.9
Pod undocumented
138 16 &_check_dim;
139 16 ▶ my ($v0, $v1) = ($_[2] ? @_[1, 0] : @_);
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 4 12 16
140 16 bless [map $v0->[$_] - $v1->[$_], 0..$#$v0]
141 }
142  
143 0 ▶sub sub_me {
Subroutine sub_me: not called CC=2 SCAR=17.9
Pod undocumented
144 0 &_check_dim;
145 0 my ($v0, $v1) = @_;
146 0 $v0->[$_] -= $v1->[$_] for 0..$#$v0;
147 0 $v0;
148 }
149  
150 46 ▶sub mul {
Subroutine mul: called CC=3 SCAR=11.0
Pod undocumented
151 46 ▶ if (ref $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1]) { } 17 29 46
152 17 &_check_dim;
153 17 my ($v0, $v1) = @_;
154 17 my $acu = 0;
155 17 $acu += $v0->[$_] * $v1->[$_] for 0..$#$v0;
156 17 $acu;
157 }
158 else {
159 29 my ($v, $s) = @_;
160 29 bless [map $s * $_, @$v];
161 }
162 }
163  
164 0 ▶sub mul_me {
Subroutine mul_me: not called CC=3 SCAR=24.8
Pod undocumented
165 0 ▶ ref $_[1] and croak "can not multiply by a vector in place as the result is not a vector";
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] and croak("can not multiply by a vector in place as the result is not a vector") 0 0 0
166 0 my ($v, $s) = @_;
167 0 $_ *= $s for @$v;
168 0 $v
169 }
170  
171 6 ▶sub div {
Subroutine div: called CC=4 SCAR=15.2
Pod undocumented
172 6 ▶ croak "can't use vector as dividend"
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] or ref $_[1] 0 6 6
Condition33%1/3
$_[2] or ref $_[1]
l!l&&r!l&&!r
006
MC/DC0%0/2
$_[2] or ref $_[1]
$_[2] ref $_[1]
Truth table33%1/3
A or B
A= $_[2]B= ref $_[1]
ABresult
1X1
011
000
173 if ($_[2] or ref $_[1]);
174 6 my ($v, $div) = @_;
175 6 ▶ $div == 0 and croak "illegal division by zero";
Branch: 1 branch
BranchTrueFalseTotal
$div == 0 and croak("illegal division by zero") 0 6 6
176 6 my $i = 1 / $div;
177 6 bless [map $i * $_, @$v]
178 }
179  
180 1 ▶sub div_me {
Subroutine div_me: called CC=3 SCAR=11.1
Pod undocumented
181 1 ▶ croak "can't use vector as dividend" if ref $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] 0 1 1
182 1 my $v = shift;
183 1 my $i = 1.0 / shift;
184 1 $_ *= $i for @$v;
185 1 $v;
186 }
187  
188 16 ▶sub equal {
Subroutine equal: called CC=3 SCAR=11.1
Pod undocumented
189 16 &_check_dim;
190 16 my ($v0, $v1) = @_;
191 16 ▶ $v0->[$_] == $v1->[$_] || return 0 for 0..$#$v0;
Branch: 1 branch
BranchTrueFalseTotal
$v0->[$_] == $v1->[$_] or return 0 48 0 48
192 16 1;
193 }
194  
195 1 ▶sub nequal {
Subroutine nequal: called CC=3 SCAR=11.1
Pod undocumented
196 1 &_check_dim;
197 1 my ($v0, $v1) = @_;
198 1 ▶ $v0->[$_] == $v1->[$_] || return 1 for 0..$#$v0;
Branch: 1 branch
BranchTrueFalseTotal
$v0->[$_] == $v1->[$_] or return 1 1 1 2
199 0 0;
200 }
201  
202 15 ▶sub cross {
Subroutine cross: called CC=4 SCAR=16.6
Pod undocumented
203 15 &_check_dim;
204 15 ▶ my ($v0, $v1) = ($_[2] ? @_[1, 0] : @_);
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 4 11 15
205 15 my $dim = @$v0;
206 15 ▶ if ($dim == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dim == 3) 15 0 15
207 15 return bless [$v0->[1] * $v1->[2] - $v0->[2] * $v1->[1],
208 $v0->[2] * $v1->[0] - $v0->[0] * $v1->[2],
209 $v0->[0] * $v1->[1] - $v0->[1] * $v1->[0]]
210 }
211 0 ▶ if ($dim == 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dim == 7) { } 0 0 0
212 0 croak "cross product for dimension 7 not implemented yet, patches welcome!";
213 }
214 else {
215 0 croak "cross product not defined for dimension $dim"
216 }
217 }
218  
219 0 ▶sub as_string { "{" . join(", ", @{$_[0]}). "}" }
Subroutine as_string: not called CC=1 SCAR=6.9
Pod undocumented
220  
221 11 ▶sub abs {
Subroutine abs: called CC=2 SCAR=6.9
Pod undocumented
222 11 my $acu = 0;
223 11 $acu += $_ * $_ for @{$_[0]};
224 11 sqrt $acu;
225 }
226  
227 4 ▶sub abs2 {
Subroutine abs2: called CC=2 SCAR=6.9
Pod undocumented
228 4 my $acu = 0;
229 4 $acu += $_ * $_ for @{$_[0]};
230 4 $acu;
231 }
232  
233 0 ▶sub dist {
Subroutine dist: not called CC=2 SCAR=17.9
Pod documented
234 0 &_check_dim;
235 0 my ($v0, $v1) = @_;
236 0 my $d2 = 0;
237 0 for (0..$#$v0) {
238 0 my $d = $v0->[$_] - $v1->[$_];
239 0 $d2 += $d * $d;
240 }
241 0 sqrt($d2);
242 }
243  
244 0 ▶sub dist2 {
Subroutine dist2: not called CC=2 SCAR=17.9
Pod documented
245 0 &_check_dim;
246 0 my ($v0, $v1) = @_;
247 0 my $d2 = 0;
248 0 for (0..$#$v0) {
249 0 my $d = $v0->[$_] - $v1->[$_];
250 0 $d2 += $d * $d;
251 }
252 0 $d2;
253 }
254  
255 0 ▶sub max_component {
Subroutine max_component: not called CC=3 SCAR=24.8
Pod documented
256 0 my $max = 0;
257 0 for (@{shift()}) {
258 0 my $abs = CORE::abs($_);
259 0 ▶ $abs > $max and $max = $abs;
Branch: 1 branch
BranchTrueFalseTotal
$abs > $max and $max = $abs 0 0 0
260 }
261 $max
262 0 }
263  
264 0 ▶sub min_component {
Subroutine min_component: not called CC=3 SCAR=24.8
Pod documented
265 0 my $self = shift;
266 0 my $min = CORE::abs($self->[0]);
267 0 for (@$self) {
268 0 my $abs = CORE::abs($_);
269 0 ▶ $abs < $min and $min = $abs;
Branch: 1 branch
BranchTrueFalseTotal
$abs < $min and $min = $abs 0 0 0
270 }
271 $min
272 0 }
273  
274 0 ▶sub manhattan_norm {
Subroutine manhattan_norm: not called CC=2 SCAR=17.9
Pod documented
275 0 my $n = 0;
276 0 $n += CORE::abs($_) for @{$_[0]};
277 0 return $n;
278 }
279  
280 0 ▶sub manhattan_dist {
Subroutine manhattan_dist: not called CC=2 SCAR=17.9
Pod documented
281 0 &_check_dim;
282 0 my ($v0, $v1) = @_;
283 0 my $d = 0;
284 0 $d += CORE::abs($v0->[$_] - $v1->[$_]) for 0..$#$v0;
285 0 return $d;
286 }
287  
288 0 ▶sub chebyshev_dist {
Subroutine chebyshev_dist: not called CC=3 SCAR=24.8
Pod documented
289 0 &_check_dim;
290 0 my ($v0, $v1) = @_;
291 0 my $max = 0;
292 0 for (0..$#$v0) {
293 0 my $d = CORE::abs($v0->[$_] - $v1->[$_]);
294 0 ▶ $max = $d if $d > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $d > $max 0 0 0
295 }
296 0 $max;
297 }
298  
299 0 ▶sub _upgrade {
Subroutine _upgrade: not called CC=5 SCAR=34.0
300 0 my $dim;
301 map {
302 0 my $d = eval { @{$_} };
303 0 ▶ defined $d or croak "argument is not a vector or array";
Branch: 1 branch
BranchTrueFalseTotal
defined $d or croak("argument is not a vector or array") 0 0 0
304 0 ▶ if (defined $dim) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dim) { } 0 0 0
305 0 ▶ $d == $dim or croak "dimensions do not match";
Branch: 1 branch
BranchTrueFalseTotal
$d == $dim or croak("dimensions do not match") 0 0 0
306 }
307 else {
308 0 $dim = $d;
309 }
310 0 ▶ UNIVERSAL::isa($_, __PACKAGE__) ? $_ : clone($_);
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_, __PACKAGE__) ? : 0 0 0
311 } @_;
312 }
313  
314 1 ▶sub atan2 {
Subroutine atan2: called CC=3 SCAR=12.3
Pod undocumented
315 1 my ($v0, $v1) = @_;
316 1 ▶ if (@$v0 == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$v0 == 2) { } 0 1 1
317 0 my $dot = $v0->[0] * $v1->[0] + $v0->[1] * $v1->[1];
318 0 my $cross = $v0->[0] * $v1->[1] - $v0->[1] * $v1->[0];
319 0 return CORE::atan2($cross, $dot);
320 }
321 else {
322 1 my $a0 = &abs($v0);
323 1 ▶ return 0 unless $a0;
Branch: 1 branch
BranchTrueFalseTotal
unless $a0 0 1 1
324 1 my $u0 = $v0 / $a0;
325 1 my $p = $v1 * $u0;
326 1 CORE::atan2(&abs($v1 - $p * $u0), $p);
327 }
328 }
329  
330 8 ▶sub versor {
Subroutine versor: called CC=3 SCAR=11.0
Pod documented
331 8 my $self = shift;
332 8 my $f = 0;
333 8 $f += $_ * $_ for @$self;
334 8 ▶ $f == 0 and croak "Illegal division by zero";
Branch: 1 branch
BranchTrueFalseTotal
$f == 0 and croak("Illegal division by zero") 0 8 8
335 8 $f = 1/sqrt $f;
336 8 bless [map $f * $_, @$self]
337 }
338  
339 0 ▶sub wrap {
Subroutine wrap: not called CC=1 SCAR=6.9
Pod documented
340 0 my ($self, $v) = @_;
341 0 &_check_dim;
342  
343 0 bless [map { my $s = $self->[$_];
344 0 my $c = $v->[$_];
345 0 $c - $s * POSIX::floor($c/$s) } (0..$#$self)];
346 }
347  
348 0 ▶sub first_orthant_reflection {
Subroutine first_orthant_reflection: not called CC=1 SCAR=6.9
Pod documented
349 0 my $self = shift;
350 0 bless [map CORE::abs, @$self];
351 }
352  
353 0 ▶sub sum {
Subroutine sum: not called CC=4 SCAR=30.0
Pod documented
354 0 ▶ ref $_[0] or shift; # works both as a class and as an instance method
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] or shift() 0 0 0
355 0 my $sum;
356 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
357 0 $sum = V(@{shift()});
358 0 $sum += $_ for @_;
359 }
360 0 return $sum;
361 }
362  
363 0 ▶sub box {
Subroutine box: not called CC=7 SCAR=40.3
Pod documented
364 0 shift;
365 0 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
366 0 my $min = clone(shift);
367 0 my $max = clone($min);
368 0 my $dim = $#$min;
369 0 for (@_) {
370 0 for my $ix (0..$dim) {
371 0 my $c = $_->[$ix];
372 0 ▶ if ($max->[$ix] < $c) {
Branch: 2 branches
BranchTrueFalseTotal
if ($max->[$ix] < $c) { } 0 0 0
elsif ($min->[$ix] > $c) { } 0 0 0
373 0 $max->[$ix] = $c;
374 }
375 elsif ($min->[$ix] > $c) {
376 0 $min->[$ix] = $c
377 }
378 }
379 }
380 0 ▶ wantarray ? ($min, $max) : $max - $min;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
381 }
382  
383 0 ▶sub nearest_in_box {
Subroutine nearest_in_box: not called CC=4 SCAR=30.0
Pod documented
384 0 my $p = shift->clone;
385 0 my ($min, $max) = Math::Vector::Real->box(@_);
386 0 for (0..$#$p) {
387 0 ▶ if ($p->[$_] < $min->[$_]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p->[$_] < $min->[$_]) { } 0 0 0
elsif ($p->[$_] > $max->[$_]) { } 0 0 0
388 0 $p->[$_] = $min->[$_];
389 }
390 elsif ($p->[$_] > $max->[$_]) {
391 0 $p->[$_] = $max->[$_];
392 }
393 }
394 $p
395 0 }
396  
397 0 ▶sub dist2_to_box {
Subroutine dist2_to_box: not called CC=5 SCAR=34.0
Pod documented
398 0 ▶ @_ > 1 or croak 'Usage: $v->dist2_to_box($w0, ...)';
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 or croak("Usage: \$v->dist2_to_box(\$w0, ...)") 0 0 0
399 0 my $p = shift;
400 0 my $d2 = 0;
401 0 my ($min, $max) = Math::Vector::Real->box(@_);
402 0 for (0..$#$p) {
403 0 ▶ if ($p->[$_] < $min->[$_]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p->[$_] < $min->[$_]) { } 0 0 0
elsif ($p->[$_] > $max->[$_]) { } 0 0 0
404 0 my $d = $p->[$_] - $min->[$_];
405 0 $d2 += $d * $d;
406 }
407 elsif ($p->[$_] > $max->[$_]) {
408 0 my $d = $p->[$_] - $max->[$_];
409 0 $d2 += $d * $d;
410 }
411 }
412 0 $d2;
413 }
414  
415 0 ▶sub chebyshev_dist_to_box {
Subroutine chebyshev_dist_to_box: not called CC=7 SCAR=40.3
Pod documented
416 0 ▶ @_ > 1 or croak 'Usage $v->chebyshev_dist_to_box($w0, ...)';
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 or croak("Usage \$v->chebyshev_dist_to_box(\$w0, ...)") 0 0 0
417 0 my $p = shift;
418 0 my $d = 0;
419 0 my ($min, $max) = Math::Vector::Real->box(@_);
420 0 for (0..$#$p) {
421 0 ▶ if ($p->[$_] < $min->[$_]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p->[$_] < $min->[$_]) { } 0 0 0
elsif ($p->[$_] > $max->[$_]) { } 0 0 0
422 0 my $delta = CORE::abs($p->[$_] - $min->[$_]);
423 0 ▶ $d = $delta if $delta > $d;
Branch: 1 branch
BranchTrueFalseTotal
if $delta > $d 0 0 0
424 }
425 elsif ($p->[$_] > $max->[$_]) {
426 0 my $delta = CORE::abs($p->[$_] - $min->[$_]);
427 0 ▶ $d = $delta if $delta > $d;
Branch: 1 branch
BranchTrueFalseTotal
if $delta > $d 0 0 0
428 }
429 }
430 0 $d;
431 }
432  
433 0 ▶sub chebyshev_cut_box {
Subroutine chebyshev_cut_box: not called CC=7 SCAR=40.3
Pod undocumented
434 0 ▶ @_ > 2 or croak 'Usage $v->chebyshev_cut_box($cd, $w0, ...)';
Branch: 1 branch
BranchTrueFalseTotal
@_ > 2 or croak("Usage \$v->chebyshev_cut_box(\$cd, \$w0, ...)") 0 0 0
435 0 my $p = shift;
436 0 my $cd = shift;
437 0 my ($min, $max) = Math::Vector::Real->box(@_);
438 0 for (0..$#$p) {
439 0 my $a = $p->[$_];
440 0 my $a_min = $a - $cd;
441 0 my $a_max = $a + $cd;
442 0 my $b_min = $min->[$_];
443 0 my $b_max = $max->[$_];
444 0 ▶ return if $b_min > $a_max or $b_max < $a_min;
Branch: 1 branch
BranchTrueFalseTotal
if $b_min > $a_max or $b_max < $a_min 0 0 0
Condition0%0/3
$b_min > $a_max or $b_max < $a_min
l!l&&r!l&&!r
000
MC/DC0%0/2
$b_min > $a_max or $b_max < $a_min
$b_min > $a_max $b_max < $a_min
Truth table0%0/3
A or B
A= $b_min > $a_maxB= $b_max < $a_min
ABresult
1X1
011
000
445 0 ▶ $min->[$_] = $a_min if $b_min < $a_min;
Branch: 1 branch
BranchTrueFalseTotal
if $b_min < $a_min 0 0 0
446 0 ▶ $max->[$_] = $a_max if $b_min > $a_max;
Branch: 1 branch
BranchTrueFalseTotal
if $b_min > $a_max 0 0 0
447 }
448 0 ($min, $max);
449 }
450  
451 0 ▶sub nearest_in_box_border {
Subroutine nearest_in_box_border: not called CC=8 SCAR=42.8
Pod undocumented
452 # TODO: this method can be optimized
453 0 my $p = shift->clone;
454 0 my ($b0, $b1) = Math::Vector::Real->box(@_);
455 0 my $in = 0;
456 0 for (0..$#$p) {
457 0 ▶ if ($p->[$_] < $b0->[$_]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p->[$_] < $b0->[$_]) { } 0 0 0
elsif ($p->[$_] > $b1->[$_]) { } 0 0 0
458 0 $p->[$_] = $b0->[$_];
459 }
460 elsif ($p->[$_] > $b1->[$_]) {
461 0 $p->[$_] = $b1->[$_];
462 }
463 else {
464 0 $in++;
465 }
466 }
467 0 ▶ if ($in == @$p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in == @$p) 0 0 0
468 # vector was inside the box
469 0 my $min_d = 'inf';
470 0 my ($comp, $comp_ix);
471 0 for my $q ($b0, $b1) {
472 0 for (0..$#$p) {
473 0 my $d = CORE::abs($p->[$_] - $q->[$_]);
474 0 ▶ if ($min_d > $d) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_d > $d) 0 0 0
475 0 $min_d = $d;
476 0 $comp = $q->[$_];
477 0 $comp_ix = $_;
478 }
479 }
480 }
481 0 $p->[$comp_ix] = $comp;
482 }
483 0 $p;
484 }
485  
486 0 ▶sub max_dist2_to_box {
Subroutine max_dist2_to_box: not called CC=4 SCAR=30.0
Pod documented
487 0 ▶ @_ > 1 or croak 'Usage: $v->max_dist2_to_box($w0, ...)';
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 or croak("Usage: \$v->max_dist2_to_box(\$w0, ...)") 0 0 0
488 0 my $p = shift;
489 0 my ($c0, $c1) = Math::Vector::Real->box(@_);
490 0 my $d2 = 0;
491 0 for (0..$#$p) {
492 0 my $d0 = CORE::abs($c0->[$_] - $p->[$_]);
493 0 my $d1 = CORE::abs($c1->[$_] - $p->[$_]);
494 0 ▶ $d2 += ($d0 >= $d1 ? $d0 * $d0 : $d1 * $d1);
Branch: 1 branch
BranchTrueFalseTotal
$d0 >= $d1 ? : 0 0 0
495 }
496 0 return $d2;
497 }
498  
499 0 ▶sub dist2_between_boxes {
Subroutine dist2_between_boxes: not called CC=4 SCAR=30.0
Pod documented
500 0 my ($class, $a0, $a1, $b0, $b1) = @_;
501 0 my ($c0, $c1) = $class->box($a0, $a1);
502 0 my ($d0, $d1) = $class->box($b0, $b1);
503 0 my $d2 = 0;
504 0 for (0..$#$c0) {
505 0 my $e0 = $d0->[$_] - $c1->[$_];
506 0 ▶ if ($e0 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e0 >= 0) { } 0 0 0
507 0 $d2 += $e0 * $e0;
508 }
509 else {
510 0 my $e1 = $c0->[$_] - $d1->[$_];
511 0 ▶ if ($e1 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e1 > 0) 0 0 0
512 0 $d2 += $e1 * $e1;
513 }
514 }
515 }
516 0 $d2;
517 }
518  
519 1 *min_dist2_between_boxes = \&dist2_between_boxes;
520  
521 0 ▶sub max_dist2_between_boxes {
Subroutine max_dist2_between_boxes: not called CC=3 SCAR=24.8
Pod documented
522 0 my ($class, $a0, $a1, $b0, $b1) = @_;
523 0 my ($c0, $c1) = $class->box($a0, $a1);
524 0 my ($d0, $d1) = $class->box($b0, $b1);
525 0 my $d2 = 0;
526 0 for (0..$#$c0) {
527 0 my $e0 = $d1->[$_] - $c0->[$_];
528 0 my $e1 = $d0->[$_] - $c1->[$_];
529 0 $e0 *= $e0;
530 0 $e1 *= $e1;
531 0 ▶ $d2 += ($e0 > $e1 ? $e0 : $e1);
Branch: 1 branch
BranchTrueFalseTotal
$e0 > $e1 ? : 0 0 0
532 }
533 0 $d2;
534 }
535  
536 0 ▶sub max_component_index {
Subroutine max_component_index: not called CC=4 SCAR=30.0
Pod documented
537 0 my $self = shift;
538 0 ▶ return unless @$self;
Branch: 1 branch
BranchTrueFalseTotal
unless @$self 0 0 0
539 0 my $max = 0;
540 0 my $max_ix = 0;
541 0 for my $ix (0..$#$self) {
542 0 my $c = CORE::abs($self->[$ix]);
543 0 ▶ if ($c > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c > $max) 0 0 0
544 0 $max = $c;
545 0 $max_ix = $ix;
546 }
547 }
548 0 $max_ix;
549 }
550  
551 0 ▶sub min_component_index {
Subroutine min_component_index: not called CC=4 SCAR=30.0
Pod undocumented
552 0 my $self = shift;
553 0 ▶ return unless @$self;
Branch: 1 branch
BranchTrueFalseTotal
unless @$self 0 0 0
554 0 my $min = CORE::abs($self->[0]);
555 0 my $min_ix = 0;
556 0 for my $ix (1..$#$self) {
557 0 my $c = CORE::abs($self->[$ix]);
558 0 ▶ if ($c < $min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $min) 0 0 0
559 0 $min = $c;
560 0 $min_ix = $ix
561 }
562 }
563 0 $min_ix;
564 }
565  
566 4 ▶sub decompose {
Subroutine decompose: called CC=2 SCAR=7.0
Pod documented
567 4 my ($u, $v) = @_;
568 4 my $p = $u * ($u * $v)/abs2($u);
569 4 my $n = $v - $p;
570 4 ▶ wantarray ? ($p, $n) : $n;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
571 }
572  
573 0 ▶sub canonical_base {
Subroutine canonical_base: not called CC=2 SCAR=17.9
Pod documented
574 0 my ($class, $dim) = @_;
575 0 my @base = map { bless [(0) x $dim], $class } 1..$dim;
576 0 $base[$_][$_] = 1 for 0..$#base;
577 0 return @base;
578 }
579  
580 4 ▶sub rotation_base_3d {
Subroutine rotation_base_3d: called CC=4 SCAR=14.0
Pod documented
581 4 my $v = shift;
582 4 ▶ @$v == 3 or croak "rotation_base_3d requires a vector with three dimensions";
Branch: 1 branch
BranchTrueFalseTotal
@$v == 3 or croak("rotation_base_3d requires a vector with three dimensions") 4 0 4
583 4 $v = $v->versor;
584 4 my $n = [0, 0, 0];
585 4 for (0..2) {
586 7 ▶ if (CORE::abs($v->[$_]) > 0.57) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::abs $v->[$_] > "0.57") 4 3 7
587 4 $n->[($_ + 1) % 3] = 1;
588 4 $n = $v->decompose($n)->versor;
589 4 return ($v, $n, $v x $n);
590 }
591 }
592 0 die "internal error, all the components where smaller than 0.57!";
593 }
594  
595 3 ▶sub rotate_3d {
Subroutine rotate_3d: called CC=2 SCAR=7.1
Pod documented
596 3 my $v = shift;
597 3 my $angle = shift;
598 3 my $c = cos($angle); my $s = sin($angle);
599 3 my ($i, $j, $k) = $v->rotation_base_3d;
600 3 my $rj = $c * $j + $s * $k;
601 3 my $rk = $c * $k - $s * $j;
602 3 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 3 3
603 0 return map { ($_ * $i) * $i + ($_ * $j) * $rj + ($_ * $k) * $rk } @_;
604 }
605 else {
606 3 my $a = shift;
607 3 return (($a * $i) * $i + ($a * $j) * $rj + ($a * $k) * $rk);
608 }
609 }
610  
611 0 ▶sub normal_base { __PACKAGE__->complementary_base(@_) }
Subroutine normal_base: not called CC=1 SCAR=6.9
Pod documented
612  
613 0 ▶sub complementary_base {
Subroutine complementary_base: not called CC=15 SCAR=54.8
Pod documented
614 0 shift;
615 0 ▶ @_ or croak "complementaty_base requires at least one argument in order to determine the dimension";
Branch: 1 branch
BranchTrueFalseTotal
@_ or croak("complementaty_base requires at least one argument in order to determine the dimension") 0 0 0
616 0 my $dim = @{$_[0]};
617 0 ▶ if ($dim == 2 and @_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dim == 2 and @_ == 1) 0 0 0
Condition0%0/3
$dim == 2 and @_ == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$dim == 2 and @_ == 1
$dim == 2 @_ == 1
Truth table0%0/3
A and B
A= $dim == 2B= @_ == 1
ABresult
0X0
100
111
618 0 my $u = versor($_[0]);
619 0 @$u = ($u->[1], -$u->[0]);
620 0 return $u;
621 }
622  
623 0 my @v = map clone($_), @_;
624 0 my @base = Math::Vector::Real->canonical_base($dim);
625 0 for my $i (0..$#v) {
626 0 my $u = versor($v[$i]);
627 0 $_ = decompose($u, $_) for @v[$i+1 .. $#v];
628 0 $_ = decompose($u, $_) for @base;
629 }
630  
631 0 my $last = $#base - @v;
632 0 ▶ return if $last < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $last < 0 0 0 0
633 0 for my $i (0 .. $last) {
634 0 my $max = abs2($base[$i]);
635 0 ▶ if ($max < 0.3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max < "0.3") 0 0 0
636 0 for my $j ($i+1 .. $#base) {
637 0 my $d2 = abs2($base[$j]);
638 0 ▶ if ($d2 > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 > $max) 0 0 0
639 0 @base[$i, $j] = @base[$j, $i];
640 0 ▶ last unless $d2 < 0.3;
Branch: 1 branch
BranchTrueFalseTotal
unless $d2 < "0.3" 0 0 0
641 0 $max = $d2;
642 }
643 }
644 }
645 0 my $versor = $base[$i] = versor($base[$i]);
646 0 $_ = decompose($versor, $_) for @base[$i+1..$#base];
647 }
648 0 ▶ wantarray ? @base[0..$last] : $base[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
649 }
650  
651 0 ▶sub select_in_ball {
Subroutine select_in_ball: not called CC=1 SCAR=6.9
Pod documented
652 0 my $v = shift;
653 0 my $r = shift;
654 0 my $r2 = $r * $r;
655 0 grep $v->dist2($_) <= $r2, @_;
656 }
657  
658 0 ▶sub select_in_ball_ref2bitmap {
Subroutine select_in_ball_ref2bitmap: not called CC=3 SCAR=24.8
Pod undocumented
659 0 my $v = shift;
660 0 my $r = shift;
661 0 my $p = shift;
662 0 my $r2 = $r * $r;
663 0 my $bm = "\0" x int((@$p + 7) / 8);
664 0 for my $ix (0..$#$p) {
665 0 ▶ vec($bm, $ix, 1) = 1 if $v->dist2($p->[$ix]) <= $r2;
Branch: 1 branch
BranchTrueFalseTotal
if $v->dist2($p->[$ix]) <= $r2 0 0 0
666 }
667 0 return $bm;
668 }
669  
670 0 ▶sub dist2_to_segment {
Subroutine dist2_to_segment: not called CC=3 SCAR=24.8
Pod documented
671 0 my ($p, $a, $b) = @_;
672 0 my $ab = $a - $b;
673 0 my $ap = $a - $p;
674 0 my $ap_ab = $ap * $ab;
675 0 ▶ return norm2($ap) if $ap_ab <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ap_ab <= 0 0 0 0
676 0 my $x = $ap * $ab / ($ab * $ab);
677 0 ▶ return dist2($ap, $ab) if $x >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 1 0 0 0
678 0 return dist2($ap, $x * $ab);
679 }
680  
681 0 ▶sub dist_to_segment { sqrt(&dist_to_segment) }
Subroutine dist_to_segment: not called CC=1 SCAR=6.9
Pod undocumented
682  
683 0 ▶sub dist2_between_segments {
Subroutine dist2_between_segments: not called CC=9 SCAR=45.0
Pod documented
684 0 my ($class, $a, $b, $c, $d) = @_;
685  
686 0 my $ab = $a - $b;
687 0 my $cd = $c - $d;
688 0 my $bd = $b - $d;
689  
690 0 ▶ if (@$a > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$a > 2) 0 0 0
691 0 my $ab_ab = $ab * $ab;
692 0 my $ab_cd = $ab * $cd;
693 0 my $cd_cd = $cd * $cd;
694  
695 0 ▶ if (CORE::abs(1.0 - ($ab_cd * $ab_cd) / ($ab_ab * $cd_cd)) > 1e-10) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::abs 1 - $ab_cd * $ab_cd / ($ab_ab * $cd_cd) > "1e-10") 0 0 0
696 # This method works for non-parallel segments
697 0 my $ab_bd = $ab * $bd;
698 0 my $bd_cd = $bd * $cd;
699  
700 0 my $D01 = $ab_cd * $ab_cd - $ab_ab * $cd_cd;
701 0 my $D21 = $cd_cd * $ab_bd - $bd_cd * $ab_cd;
702 0 my $x = $D21 / $D01;
703 0 ▶ return dist2_to_segment($b, $c, $d) if $x < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $x < 0 0 0 0
704 0 ▶ return dist2_to_segment($a, $c, $d) if $x > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x > 1 0 0 0
705  
706 0 my $D02 = $ab_cd * $ab_bd - $bd_cd * $ab_ab;
707 0 my $y = $D02 / $D01;
708 0 ▶ return dist2_to_segment($d, $a, $b) if $y < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $y < 0 0 0 0
709 0 ▶ return dist2_to_segment($c, $a, $b) if $y > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $y > 1 0 0 0
710  
711 0 my $p = $b + $ab * $x;
712 0 my $q = $d + $cd * $y;
713  
714 0 return $p->dist2($q);
715 }
716 }
717  
718 # We are in 2D or lines are parallel, we consider the distance
719 # between one segment to the vertices of the other one and
720 # viceverse and return the minimum.
721 0 my $min_d2 = dist2_to_segment($a, $c, $d);
722 0 my $d2 = dist2_to_segment($b, $c, $d);
723 0 $d2 = dist2_to_segment($c, $a, $b);
724 0 ▶ $min_d2 = $d2 if $d2 < $min_d2;
Branch: 1 branch
BranchTrueFalseTotal
if $d2 < $min_d2 0 0 0
725 0 $d2 = dist2_to_segment($d, $a, $b);
726 0 ▶ $min_d2 = $d2 if $d2 < $min_d2;
Branch: 1 branch
BranchTrueFalseTotal
if $d2 < $min_d2 0 0 0
727 0 return $min_d2;
728 }
729  
730 0 ▶sub dist_between_segments { sqrt(&dist2_between_segments) }
Subroutine dist_between_segments: not called CC=1 SCAR=6.9
Pod undocumented
731  
732 # This is run *after* Math::Vector::Real::XS is loaded!
733 1 *norm = \&abs;
734 1 *norm2 = \&abs2;
735 1 *max = \&max_component;
736 1 *min = \&min_component;
737 1 *chebyshev_norm = \&max_component;
738  
739 1 1;
740 __END__
↑ Summary Real/Test.pm »