blib/lib/Math/BSpline/Basis.pm

filter: Statementstmt 95.0% 154 / 162 Branchbran 92.8% 78 / 84 Conditioncond 87.5% 21 / 24 MC/DCmcdc 80.0% 12 / 15 Subroutinesub 90.9% 10 / 11 Podpod 100% 3 / 3 totaltotal 92.9% 278 / 299 SCAR 39.4
CC
51
Cov
94%
__ANON__
28.2
evaluate_basis_derivatives
16.0
find_knot_span
6.0
CRAP 51.6
↑ Summary
1 package Math::BSpline::Basis;
2 5 $Math::BSpline::Basis::VERSION = '0.001';
3 5 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: B-spline basis functions
7  
8 5 ▶use Moo 2.002005;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use List::Util 1.26 ('min');
Subroutine BEGIN: called CC=1 SCAR=0.0
10 use Ref::Util 0.010 (
11 5 'is_ref',
12 'is_plain_hashref',
13 'is_blessed_hashref',
14 'is_plain_arrayref',
15 5 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 around BUILDARGS => sub {
19 31 ▶ my ($orig, $class, @args) = @_;
Subroutine __ANON__: called CC=27 SCAR=33.4
20 31 my $munged_args;
21  
22 31 ▶ if (@args == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1) { } 16 15 31
elsif (@args % 2 == 1) { } 0 15 15
23 16 ▶ if (!is_ref($args[0])) {
Branch: 2 branches
BranchTrueFalseTotal
if (not is_ref($args[0])) { } 0 16 16
elsif (is_plain_hashref($args[0]) or is_blessed_hashref($args[0])) { } 16 0 16
24 # We do not understand this and dispatch to Moo (if this
25 # is what $orig does, the docu is very sparse).
26 0 return $class->$orig(@args);
27 }
28 ▶ elsif (
Condition33%1/3
is_plain_hashref($args[0]) or is_blessed_hashref($args[0])
l!l&&r!l&&!r
1600
MC/DC0%0/2
is_plain_hashref($args[0]) or is_blessed_hashref($args[0])
is_plain_hashref($args[0]) is_blessed_hashref($args[0])
Truth table33%1/3
A or B
A= is_plain_hashref($args[0])B= is_blessed_hashref($args[0])
ABresult
1X1
011
000
29 is_plain_hashref($args[0])
30 or
31 is_blessed_hashref($args[0])
32 ) {
33 # I am trying to stay as close to Moo's default behavior
34 # as I can, this is the only reason why I am supporing
35 # hashrefs at all. And since Moo apparently accepts
36 # blessed references, I do the same. However, I make a
37 # copy, blessed or not.
38 #
39 # The ugly test is due to an announced change in the
40 # behavior of Ref::Util. is_hashref is going to behave
41 # like is_plain_hashref does now. However, the planned
42 # replacement called is_any_hashref is not there. So the
43 # only future-safe implementation seems to be to use
44 # both explicit functions.
45 16 $munged_args = {%{$args[0]}};
46 }
47 else {
48 # We do not understand this and dispatch to Moo (if this
49 # is what $orig does, the docu is very sparse).
50 0 return $class->$orig(@args);
51 }
52 }
53 elsif (@args % 2 == 1) {
54 # We do not understand this and dispatch to Moo (if this
55 # is what $orig does, the docu is very sparse).
56 0 return $class->$orig(@args);
57 }
58 else {
59 15 $munged_args = {@args};
60 }
61  
62 31 ▶ if (exists($munged_args->{knot_vector})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $munged_args->{"knot_vector"}) 29 2 31
63 # degree is mandatory, so we only deal with the case when it
64 # is there. Otherwise we just let Moo do its job.
65 29 ▶ if (exists($munged_args->{degree})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $munged_args->{"degree"}) 29 0 29
66 # We do not perform any type validation etc, if the
67 # attributes are there, we use them assuming that they
68 # are valid.
69 29 my $p = $munged_args->{degree};
70 29 my $U = $munged_args->{knot_vector};
71 29 my $is_modified = 0;
72  
73 # deal with empty array
74 29 ▶ if (!defined($U) or !is_plain_arrayref($U) or @$U == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $U && is_plain_arrayref($U) or @$U == 0) 2 27 29
Condition83%5/6
not defined $U && is_plain_arrayref($U) or @$U == 0
l!l&&r!l&&!r
1127
defined $U && is_plain_arrayref($U)
!ll&&!rl&&r
1028
MC/DC66%2/3
not defined $U && is_plain_arrayref($U) or @$U == 0
defined $U is_plain_arrayref($U) @$U == 0
Truth table75%3/4
not(A && B) or C
A= defined $UB= is_plain_arrayref($U)C= @$U == 0
ABCresult
0XX1
10X1
1111
1100
75 $U = [
76 6 (map { 0 } (0..$p)),
77 2 (map { 1 } (0..$p)),
78 ];
79 2 $is_modified = 1;
80 }
81  
82 # deal with unsorted
83 29 ▶ for (my $i=1;$i<@$U;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$U) 186 27 213
84 186 ▶ if ($U->[$i] < $U->[$i-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($U->[$i] < $$U[$i - 1]) 2 184 186
85 2 $U = [sort { $a <=> $b } @$U];
86 2 $is_modified = 1;
87 2 last;
88 }
89 }
90  
91 # deal with first breakpoint
92 29 ▶ for (my $i=1;$i<=$p;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $p) 73 29 102
93 73 ▶ if ($i == @$U or $U->[$i] != $U->[$i-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == @$U or $U->[$i] != $$U[$i - 1]) 28 45 73
Condition100%3/3
$i == @$U or $U->[$i] != $$U[$i - 1]
l!l&&r!l&&!r
42445
MC/DC100%2/2
$i == @$U or $U->[$i] != $$U[$i - 1]
$i == @$U $U->[$i] != $$U[$i - 1]
Truth table100%3/3
A or B
A= $i == @$UB= $U->[$i] != $$U[$i - 1]
ABresult
1X1
011
000
94 28 ▶ $U = [@$U] if (!$is_modified);
Branch: 1 branch
BranchTrueFalseTotal
unless $is_modified 10 18 28
95 28 unshift(@$U, $U->[0]);
96 28 $is_modified = 1;
97 }
98 }
99  
100 # deal with last breakpoint
101 29 ▶ if ($U->[-1] == $U->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($U->[-1] == $U->[0]) 2 27 29
102 2 ▶ $U = [@$U] if (!$is_modified);
Branch: 1 branch
BranchTrueFalseTotal
unless $is_modified 0 2 2
103 2 push(@$U, $U->[0] + 1);
104 }
105 29 ▶ for (my $i=-2;$i>=-1-$p;$i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= -1 - $p) 73 29 102
106 73 ▶ if ($U->[$i] != $U->[$i+1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($U->[$i] != $$U[$i + 1]) 28 45 73
107 28 ▶ $U = [@$U] if (!$is_modified);
Branch: 1 branch
BranchTrueFalseTotal
unless $is_modified 0 28 28
108 28 push(@$U, $U->[-1]);
109 28 $is_modified = 1;
110 }
111 }
112  
113 # deal with excess multiplicity
114 29 ▶ for (my $i=$p+1;$i<@$U-1;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$U - 1) 135 29 164
115 135 ▶ while ($i<@$U-1 and $U->[$i] == $U->[$i-$p]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$U - 1 and $U->[$i] == $$U[$i - $p]) 11 135 146
Condition100%3/3
$i < @$U - 1 and $U->[$i] == $$U[$i - $p]
!ll&&!rl&&r
413111
MC/DC100%2/2
$i < @$U - 1 and $U->[$i] == $$U[$i - $p]
$i < @$U - 1 $U->[$i] == $$U[$i - $p]
Truth table100%3/3
A and B
A= $i < @$U - 1B= $U->[$i] == $$U[$i - $p]
ABresult
0X0
100
111
116 11 ▶ $U = [@$U] if (!$is_modified);
Branch: 1 branch
BranchTrueFalseTotal
unless $is_modified 4 7 11
117 11 splice(@$U, $i, 1);
118 11 $is_modified = 1;
119 }
120 }
121  
122 29 ▶ $munged_args->{knot_vector} = $U if ($is_modified);
Branch: 1 branch
BranchTrueFalseTotal
if $is_modified 18 11 29
123 }
124 }
125  
126 31 return $class->$orig($munged_args);
127 5 };
128  
129  
130  
131 5 has 'degree' => (
132 is => 'ro',
133 required => 1,
134 );
135  
136  
137  
138 has 'knot_vector' => (
139 is => 'lazy',
140 builder => sub {
141 0 ▶ my ($self) = @_;
Subroutine _build_knot_vector: not called CC=1 SCAR=1.2
Subroutine __ANON__: called CC=1 SCAR=1.2
142 0 my $p = $self->degree;
143  
144 return [
145 0 (map { 0 } (0..$p)),
146 0 (map { 1 } (0..$p)),
147 ]
148 }
149 5 );
150  
151  
152  
153 # I use the same variable names as in the NURBS book, although some
154 # of them are very generic. The use of $p, $U, $P, and $n is
155 # consistent throughout the relevant chapters of the book.
156 sub find_knot_span {
157 209 ▶ my ($self, $u) = @_;
Subroutine find_knot_span: called CC=6 SCAR=17.9
Pod documented
158 209 my $p = $self->degree;
159 209 my $U = $self->knot_vector;
160 209 my $n = (@$U - 1) - $p - 1;
161  
162 # We expect $u in [$U->[$p], $U->[$n+1]]. We only support
163 # values outside this range for rounding errors, do not assume
164 # that the result makes sense otherwise.
165 209 ▶ return $n if ($u >= $U->[$n+1]);
Branch: 1 branch
BranchTrueFalseTotal
if $u >= $$U[$n + 1] 15 194 209
166 194 ▶ return $p if ($u <= $U->[$p]);
Branch: 1 branch
BranchTrueFalseTotal
if $u <= $U->[$p] 16 178 194
167  
168 # binary search
169 178 my $low = $p;
170 178 my $high = $n + 1;
171 178 my $mid = int(($low + $high) / 2);
172 178 ▶ while ($u < $U->[$mid] or $u >= $U->[$mid+1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u < $U->[$mid] or $u >= $$U[$mid + 1]) 188 178 366
Condition100%3/3
$u < $U->[$mid] or $u >= $$U[$mid + 1]
l!l&&r!l&&!r
11474178
MC/DC100%2/2
$u < $U->[$mid] or $u >= $$U[$mid + 1]
$u < $U->[$mid] $u >= $$U[$mid + 1]
Truth table100%3/3
A or B
A= $u < $U->[$mid]B= $u >= $$U[$mid + 1]
ABresult
1X1
011
000
173 188 ▶ if ($u < $U->[$mid]) { $high = $mid }
Branch: 1 branch
BranchTrueFalseTotal
if ($u < $U->[$mid]) { } 114 74 188
174 74 else { $low = $mid }
175 188 $mid = int(($low + $high) / 2);
176 }
177  
178 178 return $mid;
179 }
180  
181  
182  
183 # The variable names are inspired by the theory as laid out in the
184 # NURBS book. We want to calculate N_{i,p}, that inspires $N and
185 # $p. U is the knot vector, left and right are inspired by the
186 # terms in the formulas used in the theoretical derivation.
187 sub evaluate_basis_functions {
188 43 ▶ my ($self, $i, $u) = @_;
Subroutine evaluate_basis_functions: called CC=5 SCAR=16.1
Pod documented
189 43 my $p = $self->degree;
190 43 my $U = $self->knot_vector;
191 43 my $n = (@$U - 1) - $p - 1;
192  
193 43 ▶ if ($u < $U->[$p] or $u > $U->[$n+1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u < $U->[$p] or $u > $$U[$n + 1]) 2 41 43
Condition100%3/3
$u < $U->[$p] or $u > $$U[$n + 1]
l!l&&r!l&&!r
1141
MC/DC100%2/2
$u < $U->[$p] or $u > $$U[$n + 1]
$u < $U->[$p] $u > $$U[$n + 1]
Truth table100%3/3
A or B
A= $u < $U->[$p]B= $u > $$U[$n + 1]
ABresult
1X1
011
000
194 2 return [map { 0 } (0..$p)];
195 }
196  
197 41 my $N = [1];
198 41 my $left = [];
199 41 my $right = [];
200 41 ▶ for (my $j=1;$j<=$p;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $p) 122 41 163
201 122 $left->[$j] = $u - $U->[$i+1-$j];
202 122 $right->[$j] = $U->[$i+$j] - $u;
203 122 my $saved = 0;
204 122 ▶ for (my $r=0;$r<$j;$r++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < $j) 243 122 365
205 243 my $temp = $N->[$r] / ($right->[$r+1] + $left->[$j-$r]);
206 243 $N->[$r] = $saved + $right->[$r+1] * $temp;
207 243 $saved = $left->[$j-$r] * $temp;
208 }
209 122 $N->[$j] = $saved;
210 }
211  
212 41 return $N;
213 }
214  
215  
216  
217 sub evaluate_basis_derivatives {
218 98 ▶ my ($self, $i, $u, $d) = @_;
Subroutine evaluate_basis_derivatives: called CC=16 SCAR=27.7
Pod documented
219 98 my $p = $self->degree;
220 98 my $U = $self->knot_vector;
221 98 my $n = (@$U - 1) - $p - 1;
222 98 my $result = [];
223  
224 98 $d = min($d, $p);
225  
226 98 ▶ if ($u < $U->[$p] or $u > $U->[$n+1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u < $U->[$p] or $u > $$U[$n + 1]) 2 96 98
Condition100%3/3
$u < $U->[$p] or $u > $$U[$n + 1]
l!l&&r!l&&!r
1196
MC/DC100%2/2
$u < $U->[$p] or $u > $$U[$n + 1]
$u < $U->[$p] $u > $$U[$n + 1]
Truth table100%3/3
A or B
A= $u < $U->[$p]B= $u > $$U[$n + 1]
ABresult
1X1
011
000
227 2 ▶ for (my $k=0;$k<=$d;$k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $d) 8 2 10
228 8 push(@$result, [map { 0 } (0..$p)]);
229 }
230 2 return $result;
231 }
232  
233 96 my $ndu = [[1]];
234 96 my $left = [];
235 96 my $right = [];
236 96 ▶ for (my $j=1;$j<=$p;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $p) 359 96 455
237 359 $left->[$j] = $u - $U->[$i+1-$j];
238 359 $right->[$j] = $U->[$i+$j] - $u;
239 359 my $saved = 0;
240 359 ▶ for (my $r=0;$r<$j;$r++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r < $j) 897 359 1256
241 897 $ndu->[$j]->[$r] = $right->[$r+1] + $left->[$j-$r];
242 897 my $temp = $ndu->[$r]->[$j-1] / $ndu->[$j]->[$r];
243 897 $ndu->[$r]->[$j] = $saved + $right->[$r+1] * $temp;
244 897 $saved = $left->[$j-$r] * $temp;
245 }
246 359 $ndu->[$j]->[$j] = $saved;
247 }
248  
249 # $result->[0] holds the function values (0th derivatives)
250 96 ▶ for (my $j=0;$j<=$p;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $p) 455 96 551
251 455 $result->[0]->[$j] = $ndu->[$j]->[$p];
252 }
253  
254 96 ▶ for (my $r=0;$r<=$p;$r++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r <= $p) 455 96 551
255 455 my $a = [[1]];
256 455 my ($l1, $l2) = (0, 1); # alternating indices to address $a
257  
258 # compute $result->[$k] (kth derivative)
259 455 ▶ for (my $k=1;$k<=$d;$k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $d) 1414 455 1869
260 1414 my $sum = 0;
261 1414 my $rk = $r - $k;
262 1414 my $pk = $p - $k;
263 1414 ▶ if ($rk >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rk >= 0) 820 594 1414
264 820 $a->[$l2]->[0] = $a->[$l1]->[0] / $ndu->[$pk+1]->[$rk];
265 820 $sum = $a->[$l2]->[0] * $ndu->[$rk]->[$pk];
266 }
267  
268 1414 ▶ my $j_min = $rk >= -1 ? 1 : -$rk;
Branch: 1 branch
BranchTrueFalseTotal
$rk >= -1 ? : 1102 312 1414
269 1414 ▶ my $j_max = $r <= $pk + 1 ? $k - 1 : $p - $r;
Branch: 1 branch
BranchTrueFalseTotal
$r <= $pk + 1 ? : 1102 312 1414
270 1414 ▶ for (my $j=$j_min;$j<=$j_max;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $j_max) 731 1414 2145
271 731 $a->[$l2]->[$j] = ($a->[$l1]->[$j] - $a->[$l1]->[$j-1])
272 / $ndu->[$pk+1]->[$rk+$j];
273 731 $sum += $a->[$l2]->[$j] * $ndu->[$rk+$j]->[$pk];
274 }
275  
276 1414 ▶ if ($r <= $pk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r <= $pk) 820 594 1414
277 820 $a->[$l2]->[$k] = -$a->[$l1]->[$k-1]
278 / $ndu->[$pk+1]->[$r];
279 820 $sum += $a->[$l2]->[$k] * $ndu->[$r]->[$pk];
280 }
281  
282 1414 $result->[$k]->[$r] = $sum;
283 1414 ($l1, $l2) = ($l2, $l1);
284 }
285 }
286  
287 96 my $multiplicity = $p;
288 96 ▶ for (my $k=1;$k<=$d;$k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k <= $d) 282 96 378
289 282 ▶ for (my $j=0;$j<=$p;$j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $p) 1414 282 1696
290 1414 $result->[$k]->[$j] *= $multiplicity;
291 }
292 282 $multiplicity *= ($p - $k);
293 }
294  
295 96 return $result;
296 }
297  
298  
299 5 1;
300  
301 __END__
↑ Summary