File Coverage

blib/lib/Math/BSpline/Basis.pm
Criterion Covered Total %
statement 106 106 100.0
branch 18 18 100.0
condition 9 9 100.0
subroutine 9 9 100.0
pod 3 3 100.0
total 145 145 100.0


line stmt bran cond sub pod time code
1             package Math::BSpline::Basis;
2             $Math::BSpline::Basis::VERSION = '0.002';
3 5     5   3441 use 5.014;
  5         17  
4 5     5   22 use warnings;
  5         9  
  5         158  
5              
6             # ABSTRACT: B-spline basis functions
7              
8 5     5   2568 use Moo 2.002005;
  5         50512  
  5         29  
9 5     5   6382 use List::Util 1.26 ('min');
  5         86  
  5         458  
10             use Ref::Util 0.010 (
11 5         7271 'is_ref',
12             'is_plain_hashref',
13             'is_blessed_hashref',
14             'is_plain_arrayref',
15 5     5   2512 );
  5         7005  
16              
17              
18             around BUILDARGS => sub {
19             my ($orig, $class, @args) = @_;
20             my $munged_args;
21              
22             if (@args == 1) {
23             if (!is_ref($args[0])) {
24             # We do not understand this and dispatch to Moo (if this
25             # is what $orig does, the docu is very sparse).
26             return $class->$orig(@args);
27             }
28             elsif (
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             $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             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             return $class->$orig(@args);
57             }
58             else {
59             $munged_args = {@args};
60             }
61              
62             if (exists($munged_args->{knot_vector})) {
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             if (exists($munged_args->{degree})) {
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             my $p = $munged_args->{degree};
70             my $U = $munged_args->{knot_vector};
71             my $is_modified = 0;
72              
73             # deal with empty array
74             if (!defined($U) or !is_plain_arrayref($U) or @$U == 0) {
75             $U = [
76             (map { 0 } (0..$p)),
77             (map { 1 } (0..$p)),
78             ];
79             $is_modified = 1;
80             }
81              
82             # deal with unsorted
83             for (my $i=1;$i<@$U;$i++) {
84             if ($U->[$i] < $U->[$i-1]) {
85             $U = [sort { $a <=> $b } @$U];
86             $is_modified = 1;
87             last;
88             }
89             }
90              
91             # deal with first breakpoint
92             for (my $i=1;$i<=$p;$i++) {
93             if ($i == @$U or $U->[$i] != $U->[$i-1]) {
94             $U = [@$U] if (!$is_modified);
95             unshift(@$U, $U->[0]);
96             $is_modified = 1;
97             }
98             }
99              
100             # deal with last breakpoint
101             if ($U->[-1] == $U->[0]) {
102             $U = [@$U] if (!$is_modified);
103             push(@$U, $U->[0] + 1);
104             }
105             for (my $i=-2;$i>=-1-$p;$i--) {
106             if ($U->[$i] != $U->[$i+1]) {
107             $U = [@$U] if (!$is_modified);
108             push(@$U, $U->[-1]);
109             $is_modified = 1;
110             }
111             }
112              
113             # deal with excess multiplicity
114             for (my $i=$p+1;$i<@$U-1;$i++) {
115             while ($i<@$U-1 and $U->[$i] == $U->[$i-$p]) {
116             $U = [@$U] if (!$is_modified);
117             splice(@$U, $i, 1);
118             $is_modified = 1;
119             }
120             }
121              
122             $munged_args->{knot_vector} = $U if ($is_modified);
123             }
124             }
125              
126             return $class->$orig($munged_args);
127             };
128              
129              
130              
131             has 'degree' => (
132             is => 'ro',
133             required => 1,
134             );
135              
136              
137              
138             has 'knot_vector' => (
139             is => 'lazy',
140             builder => sub {
141 2     2   2481 my ($self) = @_;
142 2         6 my $p = $self->degree;
143              
144             return [
145 6         12 (map { 0 } (0..$p)),
146 2         7 (map { 1 } (0..$p)),
  6         20  
147             ]
148             }
149             );
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     209 1 980176 my ($self, $u) = @_;
158 209         511 my $p = $self->degree;
159 209         4371 my $U = $self->knot_vector;
160 209         1558 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 100       725 return $n if ($u >= $U->[$n+1]);
166 194 100       475 return $p if ($u <= $U->[$p]);
167              
168             # binary search
169 178         240 my $low = $p;
170 178         261 my $high = $n + 1;
171 178         354 my $mid = int(($low + $high) / 2);
172 178   100     586 while ($u < $U->[$mid] or $u >= $U->[$mid+1]) {
173 188 100       325 if ($u < $U->[$mid]) { $high = $mid }
  114         134  
174 74         86 else { $low = $mid }
175 188         598 $mid = int(($low + $high) / 2);
176             }
177              
178 178         659 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     43 1 1562 my ($self, $i, $u) = @_;
189 43         63 my $p = $self->degree;
190 43         604 my $U = $self->knot_vector;
191 43         239 my $n = (@$U - 1) - $p - 1;
192              
193 43 100 100     143 if ($u < $U->[$p] or $u > $U->[$n+1]) {
194 2         5 return [map { 0 } (0..$p)];
  6         13  
195             }
196              
197 41         67 my $N = [1];
198 41         60 my $left = [];
199 41         48 my $right = [];
200 41         78 for (my $j=1;$j<=$p;$j++) {
201 122         221 $left->[$j] = $u - $U->[$i+1-$j];
202 122         167 $right->[$j] = $U->[$i+$j] - $u;
203 122         150 my $saved = 0;
204 122         177 for (my $r=0;$r<$j;$r++) {
205 243         363 my $temp = $N->[$r] / ($right->[$r+1] + $left->[$j-$r]);
206 243         313 $N->[$r] = $saved + $right->[$r+1] * $temp;
207 243         390 $saved = $left->[$j-$r] * $temp;
208             }
209 122         223 $N->[$j] = $saved;
210             }
211              
212 41         108 return $N;
213             }
214              
215              
216              
217             sub evaluate_basis_derivatives {
218 98     98 1 994 my ($self, $i, $u, $d) = @_;
219 98         173 my $p = $self->degree;
220 98         1418 my $U = $self->knot_vector;
221 98         558 my $n = (@$U - 1) - $p - 1;
222 98         154 my $result = [];
223              
224 98         301 $d = min($d, $p);
225              
226 98 100 100     461 if ($u < $U->[$p] or $u > $U->[$n+1]) {
227 2         7 for (my $k=0;$k<=$d;$k++) {
228 8         15 push(@$result, [map { 0 } (0..$p)]);
  32         45  
229             }
230 2         4 return $result;
231             }
232              
233 96         180 my $ndu = [[1]];
234 96         146 my $left = [];
235 96         128 my $right = [];
236 96         191 for (my $j=1;$j<=$p;$j++) {
237 359         622 $left->[$j] = $u - $U->[$i+1-$j];
238 359         585 $right->[$j] = $U->[$i+$j] - $u;
239 359         384 my $saved = 0;
240 359         574 for (my $r=0;$r<$j;$r++) {
241 897         1449 $ndu->[$j]->[$r] = $right->[$r+1] + $left->[$j-$r];
242 897         1270 my $temp = $ndu->[$r]->[$j-1] / $ndu->[$j]->[$r];
243 897         1482 $ndu->[$r]->[$j] = $saved + $right->[$r+1] * $temp;
244 897         4176 $saved = $left->[$j-$r] * $temp;
245             }
246 359         654 $ndu->[$j]->[$j] = $saved;
247             }
248              
249             # $result->[0] holds the function values (0th derivatives)
250 96         175 for (my $j=0;$j<=$p;$j++) {
251 455         800 $result->[0]->[$j] = $ndu->[$j]->[$p];
252             }
253              
254 96         180 for (my $r=0;$r<=$p;$r++) {
255 455         672 my $a = [[1]];
256 455         613 my ($l1, $l2) = (0, 1); # alternating indices to address $a
257              
258             # compute $result->[$k] (kth derivative)
259 455         652 for (my $k=1;$k<=$d;$k++) {
260 1414         1821 my $sum = 0;
261 1414         1593 my $rk = $r - $k;
262 1414         1507 my $pk = $p - $k;
263 1414 100       1918 if ($rk >= 0) {
264 820         1340 $a->[$l2]->[0] = $a->[$l1]->[0] / $ndu->[$pk+1]->[$rk];
265 820         1014 $sum = $a->[$l2]->[0] * $ndu->[$rk]->[$pk];
266             }
267              
268 1414 100       1887 my $j_min = $rk >= -1 ? 1 : -$rk;
269 1414 100       1909 my $j_max = $r <= $pk + 1 ? $k - 1 : $p - $r;
270 1414         2047 for (my $j=$j_min;$j<=$j_max;$j++) {
271 731         1369 $a->[$l2]->[$j] = ($a->[$l1]->[$j] - $a->[$l1]->[$j-1])
272             / $ndu->[$pk+1]->[$rk+$j];
273 731         1330 $sum += $a->[$l2]->[$j] * $ndu->[$rk+$j]->[$pk];
274             }
275              
276 1414 100       1849 if ($r <= $pk) {
277 820         1411 $a->[$l2]->[$k] = -$a->[$l1]->[$k-1]
278             / $ndu->[$pk+1]->[$r];
279 820         1049 $sum += $a->[$l2]->[$k] * $ndu->[$r]->[$pk];
280             }
281              
282 1414         1895 $result->[$k]->[$r] = $sum;
283 1414         3071 ($l1, $l2) = ($l2, $l1);
284             }
285             }
286              
287 96         134 my $multiplicity = $p;
288 96         144 for (my $k=1;$k<=$d;$k++) {
289 282         415 for (my $j=0;$j<=$p;$j++) {
290 1414         2040 $result->[$k]->[$j] *= $multiplicity;
291             }
292 282         458 $multiplicity *= ($p - $k);
293             }
294              
295 96         330 return $result;
296             }
297              
298              
299             1;
300              
301             __END__