| 1 | package Math::BSpline::Curve; | ||||||||||||||
| 2 | 4 | $Math::BSpline::Curve::VERSION = '0.001'; | |||||||||||||
| 3 | 4 | ▶ | use 5.014; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | # ABSTRACT: B-spline curves | ||||||||||||||
| 7 | |||||||||||||||
| 8 | 4 | ▶ | use Moo 2.002005; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 4 | ▶ | use List::Util 1.26 ('min'); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | use Ref::Util 0.010 ( | ||||||||||||||
| 11 | 4 | 'is_plain_arrayref', | |||||||||||||
| 12 | 4 | ▶ | ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 4 | ▶ | use Math::BSpline::Basis 0.001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 4 | ▶ | use Math::Matrix::Banded 0.004; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | |||||||||||||||
| 17 | 4 | has '_degree' => ( | |||||||||||||
| 18 | is => 'ro', | ||||||||||||||
| 19 | required => 1, | ||||||||||||||
| 20 | init_arg => 'degree', | ||||||||||||||
| 21 | ); | ||||||||||||||
| 22 | |||||||||||||||
| 23 | |||||||||||||||
| 24 | |||||||||||||||
| 25 | 4 | has '_knot_vector' => ( | |||||||||||||
| 26 | is => 'ro', | ||||||||||||||
| 27 | init_arg => 'knot_vector', | ||||||||||||||
| 28 | predicate => 1, | ||||||||||||||
| 29 | ); | ||||||||||||||
| 30 | |||||||||||||||
| 31 | |||||||||||||||
| 32 | |||||||||||||||
| 33 | has 'control_points' => ( | ||||||||||||||
| 34 | is => 'lazy', | ||||||||||||||
| 35 | 0 | ▶ | builder => sub { return [] }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
_build_control_points: not called CC=1 SCAR=6.9
| |||||||||||||||
| 36 | 4 | ); | |||||||||||||
| 37 | |||||||||||||||
| 38 | |||||||||||||||
| 39 | |||||||||||||||
| 40 | has 'basis' => ( | ||||||||||||||
| 41 | is => 'lazy', | ||||||||||||||
| 42 | handles => [ | ||||||||||||||
| 43 | 'degree', | ||||||||||||||
| 44 | 'knot_vector', | ||||||||||||||
| 45 | ], | ||||||||||||||
| 46 | builder => sub { | ||||||||||||||
| 47 | 16 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
Subroutine
_build_basis: called CC=2 SCAR=7.0
| |||||||||||||||
| 48 | |||||||||||||||
| 49 | 16 | ▶ | return Math::BSpline::Basis->new( | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 50 | degree => $self->_degree, | ||||||||||||||
| 51 | ( | ||||||||||||||
| 52 | $self->_has_knot_vector | ||||||||||||||
| 53 | ? (knot_vector => $self->_knot_vector) | ||||||||||||||
| 54 | : (), | ||||||||||||||
| 55 | ), | ||||||||||||||
| 56 | ) | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | 4 | ); | |||||||||||||
| 59 | |||||||||||||||
| 60 | |||||||||||||||
| 61 | |||||||||||||||
| 62 | sub evaluate { | ||||||||||||||
| 63 | 54 | ▶ | my ($self, $u) = @_; | ||||||||||||
|
Subroutine
evaluate: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||
| 64 | 54 | my $basis = $self->basis; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 54 | my $p = $self->degree; | |||||||||||||
| 67 | 54 | my $P = $self->control_points; | |||||||||||||
| 68 | 54 | my $s = $basis->find_knot_span($u); | |||||||||||||
| 69 | 54 | my $Nip = $basis->evaluate_basis_functions($s, $u); | |||||||||||||
| 70 | |||||||||||||||
| 71 | 54 | ▶ | return undef if (!@$P); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 54 | my $value; | |||||||||||||
| 73 | 54 | ▶ | if (is_plain_arrayref($P->[0])) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | # The control points are plain arrayrefs, hence we have no | ||||||||||||||
| 75 | # overloaded scalar multiplication at our disposal and have | ||||||||||||||
| 76 | # to manipulate the components individually. | ||||||||||||||
| 77 | 54 | my $dim = scalar(@{$P->[0]}); | |||||||||||||
| 78 | 54 | $value = [map { 0 } (1..$dim)]; | |||||||||||||
| 79 | 54 | ▶ | for (my $i=0;$i<=$p;$i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 192 | my $c = $Nip->[$i]; | |||||||||||||
| 81 | 192 | my $this_P = $P->[$s-$p+$i]; | |||||||||||||
| 82 | 192 | ▶ | for (my $j=0;$j<$dim;$j++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | 384 | $value->[$j] += $c * $this_P->[$j]; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | } | ||||||||||||||
| 86 | } | ||||||||||||||
| 87 | else { | ||||||||||||||
| 88 | # We use the first control point to initialize the value in | ||||||||||||||
| 89 | # order to support all objects that overload addition and | ||||||||||||||
| 90 | # scalar multiplication. | ||||||||||||||
| 91 | 0 | $value = 0 * $P->[0]; | |||||||||||||
| 92 | 0 | ▶ | for (my $i=0;$i<=$p;$i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 0 | $value += $Nip->[$i] * $P->[$s-$p+$i]; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 54 | return $value; | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | |||||||||||||||
| 100 | |||||||||||||||
| 101 | |||||||||||||||
| 102 | sub evaluate_derivatives { | ||||||||||||||
| 103 | 13 | ▶ | my ($self, $u, $d) = @_; | ||||||||||||
|
Subroutine
evaluate_derivatives: called CC=10 SCAR=25.8
Pod
documented
| |||||||||||||||
| 104 | 13 | my $basis = $self->basis; | |||||||||||||
| 105 | |||||||||||||||
| 106 | 13 | my $p = $self->degree; | |||||||||||||
| 107 | 13 | my $P = $self->control_points; | |||||||||||||
| 108 | 13 | my $s = $basis->find_knot_span($u); | |||||||||||||
| 109 | 13 | my $D = $basis->evaluate_basis_derivatives($s, $u, min($d, $p)); | |||||||||||||
| 110 | |||||||||||||||
| 111 | 13 | ▶ | return undef if (!@$P); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 112 | 13 | my $value = []; | |||||||||||||
| 113 | 13 | ▶ | if (is_plain_arrayref($P->[0])) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | # The control points are plain arrayrefs, hence we have no | ||||||||||||||
| 115 | # overloaded scalar multiplication at our disposal and have | ||||||||||||||
| 116 | # to manipulate the components individually. | ||||||||||||||
| 117 | 13 | my $dim = scalar(@{$P->[0]}); | |||||||||||||
| 118 | 13 | ▶ | for (my $k=0;$k<=$d;$k++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | 50 | $value->[$k] = [map { 0 } (1..$dim)]; | |||||||||||||
| 120 | |||||||||||||||
| 121 | 50 | ▶ | if ($k <= $p) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 122 | 50 | ▶ | for (my $i=0;$i<=$p;$i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | 246 | my $c = $D->[$k]->[$i]; | |||||||||||||
| 124 | 246 | my $this_P = $P->[$s-$p+$i]; | |||||||||||||
| 125 | 246 | ▶ | for (my $j=0;$j<$dim;$j++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | 492 | $value->[$k]->[$j] += $c * $this_P->[$j]; | |||||||||||||
| 127 | } | ||||||||||||||
| 128 | } | ||||||||||||||
| 129 | } | ||||||||||||||
| 130 | } | ||||||||||||||
| 131 | } | ||||||||||||||
| 132 | else { | ||||||||||||||
| 133 | # We use the first control point to initialize the value in | ||||||||||||||
| 134 | # order to support all objects that overload addition and | ||||||||||||||
| 135 | # scalar multiplication. | ||||||||||||||
| 136 | 0 | ▶ | for (my $k=0;$k<=$d;$k++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 137 | 0 | $value->[$k] = 0 * $P->[0]; | |||||||||||||
| 138 | |||||||||||||||
| 139 | 0 | ▶ | if ($k <= $p) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 140 | 0 | ▶ | for (my $i=0;$i<=$p;$i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 141 | 0 | $value->[$k] += $D->[$k]->[$i] * $P->[$s-$p+$i]; | |||||||||||||
| 142 | } | ||||||||||||||
| 143 | } | ||||||||||||||
| 144 | } | ||||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | 13 | return $value; | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | |||||||||||||||
| 151 | sub derivative { | ||||||||||||||
| 152 | 6 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
derivative: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||||
| 153 | 6 | my $p = $self->degree; | |||||||||||||
| 154 | 6 | my $P = $self->control_points; | |||||||||||||
| 155 | 6 | my $U = $self->knot_vector; | |||||||||||||
| 156 | |||||||||||||||
| 157 | 6 | ▶ | return undef if (!@$P); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 158 | |||||||||||||||
| 159 | 6 | my $q = $p - 1; | |||||||||||||
| 160 | 6 | my $V = [@$U[1..($#$U-1)]]; | |||||||||||||
| 161 | 6 | my $Q = []; | |||||||||||||
| 162 | 6 | ▶ | if (is_plain_arrayref($P->[0])) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | # The control points are plain arrayrefs, hence we have no | ||||||||||||||
| 164 | # overloaded scalar multiplication at our disposal and have | ||||||||||||||
| 165 | # to manipulate the components individually. | ||||||||||||||
| 166 | 6 | my $dim = scalar(@{$P->[0]}); | |||||||||||||
| 167 | 6 | ▶ | for (my $i=0;$i<@$P-1;$i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 168 | 33 | my $c = $p / ($U->[$i+$p+1] - $U->[$i+1]); | |||||||||||||
| 169 | 33 | $Q->[$i] = []; | |||||||||||||
| 170 | 33 | ▶ | for (my $j=0;$j<$dim;$j++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | 66 | $Q->[$i]->[$j] = $c * ($P->[$i+1]->[$j] - $P->[$i]->[$j]); | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | } | ||||||||||||||
| 174 | } | ||||||||||||||
| 175 | else { | ||||||||||||||
| 176 | 0 | ▶ | for (my $i=0;$i<@$P-1;$i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 177 | 0 | my $c = $p / ($U->[$i+$p+1] - $U->[$i+1]); | |||||||||||||
| 178 | 0 | $Q->[$i] = $c * ($P->[$i+1] - $P->[$i]); | |||||||||||||
| 179 | } | ||||||||||||||
| 180 | } | ||||||||||||||
| 181 | |||||||||||||||
| 182 | 6 | return Math::BSpline::Curve->new( | |||||||||||||
| 183 | degree => $q, | ||||||||||||||
| 184 | knot_vector => $V, | ||||||||||||||
| 185 | control_points => $Q, | ||||||||||||||
| 186 | ); | ||||||||||||||
| 187 | } | ||||||||||||||
| 188 | |||||||||||||||
| 189 | |||||||||||||||
| 190 | 4 | 1; | |||||||||||||
| 191 | |||||||||||||||
| 192 | __END__ | ||||||||||||||