blib/lib/Math/Vector/Real/Neighbors.pm

filter: Statementstmt 16.3% 15 / 92 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 30.7% 4 / 13 Podpod 20.0% 1 / 5 totaltotal 16.1% 20 / 124 CC 14 SCAR 38.9
CC
14
Cov
14%
_neighbors
110.0
_neighbors_bruteforce
30.0
neighbors_slow
2.0
CRAP 48.7
↑ Summary
1 package Math::Vector::Real::Neighbors;
2  
3 1 our $VERSION = '0.02';
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  
8 1 ▶use Math::Vector::Real::kdTree;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Sort::Key::Radix qw(nkeysort_inplace);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶sub neighbors_slow {
Subroutine neighbors_slow: not called CC=1 SCAR=6.9
Pod undocumented
12 0 my $class = shift;
13 0 my ($bottom, $top) = Math::Vector::Real->box(@_);
14 0 my $box = $top - $bottom;
15 0 my $v = [map $_ - $bottom, @_];
16 0 my $ixs = [0..$#_];
17 0 my $dist2 = [($box->abs2 * 10) x @_];
18 0 my $neighbors = [(undef) x @_];
19 0 _neighbors($v, $ixs, $dist2, $neighbors, $box, 0);
20 0 return @$neighbors;
21 }
22  
23 0 ▶sub neighbors_bruteforce {
Subroutine neighbors_bruteforce: not called CC=1 SCAR=6.9
Pod documented
24 0 my $class = shift;
25 0 my ($bottom, $top) = Math::Vector::Real->box(@_);
26 0 my $box = $top - $bottom;
27 0 my $v = [map $_ - $bottom, @_];
28 0 my $ixs = [0..$#_];
29 0 my $dist2 = [($box->abs2 * 10) x @_];
30 0 my $neighbors = [(undef) x @_];
31 0 _neighbors_bruteforce($v, $ixs, $dist2, $neighbors, $box, 0);
32 0 return @$neighbors;
33 }
34  
35 0 ▶sub neighbors_kdtree {
Subroutine neighbors_kdtree: not called CC=1 SCAR=6.9
Pod undocumented
36 0 shift;
37 0 my $tree = Math::Vector::Real::kdTree->new(@_);
38 0 map scalar($tree->find_nearest_neighbor_internal($_)), 0..$#_
39 }
40  
41 0 ▶sub neighbors_kdtree2 {
Subroutine neighbors_kdtree2: not called CC=1 SCAR=6.9
Pod undocumented
42 0 shift;
43 0 ( Math::Vector::Real::kdTree
44 -> new(@_)
45 -> find_nearest_neighbor_all_internal );
46 }
47  
48 1 *neighbors = \&neighbors_kdtree2;
49  
50 0 ▶sub _neighbors_bruteforce {
Subroutine _neighbors_bruteforce: not called CC=5 SCAR=34.0
51 0 my ($v, $ixs, $dist2, $neighbors) = @_;
52 0 my $ixix = 0;
53 0 for my $i (@$ixs) {
54 0 $ixix++;
55 0 my $v0 = $v->[$i];
56 0 for my $j (@$ixs[$ixix..$#$ixs]) {
57 0 my $d2 = $v0->dist2($v->[$j]);
58 0 ▶ if ($dist2->[$i] > $d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dist2->[$i] > $d2) 0 0 0
59 0 $dist2->[$i] = $d2;
60 0 $neighbors->[$i] = $j;
61 }
62 0 ▶ if ($dist2->[$j] > $d2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dist2->[$j] > $d2) 0 0 0
63 0 $dist2->[$j] = $d2;
64 0 $neighbors->[$j] = $i;
65 }
66 }
67 }
68 }
69  
70 0 ▶sub _neighbors {
Subroutine _neighbors: not called CC=10 SCAR=47.0
71 0 ▶ if (@{$_[1]} < 6) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$_[1];} < 6) { } 0 0 0
72 0 _neighbors_bruteforce(@_);
73 }
74 else {
75 0 my ($v, $ixs, $dist2, $neighbors, $box) = @_;
76 0 my $dim = $box->max_component_index;
77 0 ▶ nkeysort_inplace { $v->[$_][$dim] } @$ixs;
Subroutine __ANON__: not called CC=1 SCAR=6.9
78  
79 0 my $bfirst = @$ixs >> 1;
80 0 my $alast = $bfirst - 1;
81  
82 0 my $abox = $box->clone;
83 0 $abox->[$dim] = $v->[$ixs->[$alast]][$dim] - $v->[$ixs->[0]][$dim];
84 0 my $bbox = $box->clone;
85 0 $bbox->[$dim] = $v->[$ixs->[$#$ixs]][$dim] - $v->[$ixs->[$bfirst]][$dim];
86  
87 0 _neighbors($v, [@$ixs[0..$alast]], $dist2, $neighbors, $abox);
88 0 _neighbors($v, [@$ixs[$bfirst..$#$ixs]], $dist2, $neighbors, $bbox);
89  
90 0 for my $i (@$ixs[0..$alast]) {
91 0 my $vi = $v->[$i];
92 0 my $mind2 = $dist2->[$i];
93 0 for my $j (@$ixs[$bfirst..$#$ixs]) {
94 0 my $vj = $v->[$j];
95 0 my $dc = $vj->[$dim] - $vi->[$dim];
96 0 ▶ last unless ($mind2 > $dc * $dc);
Branch: 1 branch
BranchTrueFalseTotal
unless $mind2 > $dc * $dc 0 0 0
97 0 my $d2 = $vi->dist2($vj);
98 0 ▶ if ($d2 < $mind2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 < $mind2) 0 0 0
99 0 $mind2 = $dist2->[$i] = $d2;
100 0 $neighbors->[$i] = $j;
101 }
102 }
103 }
104  
105 0 for my $i (@$ixs[$bfirst..$#$ixs]) {
106 0 my $vi = $v->[$i];
107 0 my $mind2 = $dist2->[$i];
108 0 for my $j (reverse @$ixs[0..$alast]) {
109 0 my $vj = $v->[$j];
110 0 my $dc = $vj->[$dim] - $vi->[$dim];
111 0 ▶ last unless ($mind2 > $dc * $dc);
Branch: 1 branch
BranchTrueFalseTotal
unless $mind2 > $dc * $dc 0 0 0
112 0 my $d2 = $vi->dist2($vj);
113 0 ▶ if ($d2 < $mind2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d2 < $mind2) 0 0 0
114 0 $mind2 = $dist2->[$i] = $d2;
115 0 $neighbors->[$i] = $j;
116 }
117 }
118 }
119  
120 # my @dist2_cp = @$dist2;
121 # my @neighbors_cp = @$neighbors;
122 # _neighbors_bruteforce($v, $ixs, $dist2, $neighbors, $abox);
123 # use 5.010;
124 # say "ixs : @$ixs";
125 # say "neighbors_cp: @neighbors_cp[@$ixs]";
126 # say "neighbors : @$neighbors[@$ixs]";
127 }
128 }
129  
130 0 ▶sub neighbors_bubble {
Subroutine neighbors_bubble: not called CC=1 SCAR=6.9
Pod undocumented
131 0 my $class = shift;
132 0 my @v = @_;
133 0 my $n = sqrt(@v);
134 0 my (@c, @r, @p); # bubbles centers, radius and points
135 }
136  
137 0 ▶sub _neighbors_bubble {}
Subroutine _neighbors_bubble: not called CC=1 SCAR=0.0
138  
139 1 1;
140 __END__
↑ Summary