lib/ICC/Support/spline.pm

filter: Statementstmt 6.7% 24 / 355 Branchbran 1.3% 2 / 150 Conditioncond 0.6% 1 / 151 MC/DCmcdc 0.0% 0 / 78 Subroutinesub 20.5% 7 / 34 Podpod 6.6% 1 / 15 totaltotal 4.4% 35 / 783 CC 139 SCAR 47.9
CC
139
Cov
4%
_rev
380.0
inverse
240.0
parametric
210.0
CRAP 119.9
« lib/ICC/Support/spline2.pm ↑ Summary blib/lib/ICC/Profile/cvst.pm »
1 package ICC::Support::spline;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = 0.02;
7  
8 # revised 2017-03-29
9 #
10 # Copyright © 2004-2018 by William B. Birkett
11  
12 # add development directory
13 2 ▶use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # inherit from Shared
16 2 ▶use parent qw(ICC::Shared);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # add complex math functions
19 2 ▶use Math::Complex qw(root sqrt cbrt Im Re);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # enable static variables
22 2 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # create new spline object
25 # hash keys are: 'input_range', 'output_values'
26 # parameters: ([ref_to_attribute_hash])
27 # returns: (object_reference)
28 sub new {
29  
30 # get object class
31 1 ▶ my $class = shift();
Subroutine new: called CC=4 SCAR=17.2
Pod undocumented
32  
33 # create empty spline object
34 1 my $self = [
35 {}, # object header
36 [], # input range
37 [], # output values
38 [], # derivative values
39 [], # min/max output values
40 [], # parametric derivative matrix
41 ];
42  
43 # if one parameter, a hash reference
44 1 ▶ if (@_ == 1 && ref($_[0]) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 1 1
elsif (@_) { } 0 1 1
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
45
46 # set object contents from hash
47 0 _new_from_hash($self, $_[0]);
48
49 } elsif (@_) {
50
51 # error
52 0 croak('invalid parameter(s)');
53
54 }
55  
56 # return blessed object
57 1 return(bless($self, $class));
58  
59 }
60  
61 # write spline object to ICC profile
62 # note: writes an equivalent curv object
63 # parameters: (ref_to_parent_object, file_handle, ref_to_tag_table_entry)
64 sub write_fh {
65  
66 # get tag reference
67 0 ▶ my $self = shift();
Subroutine write_fh: not called CC=2 SCAR=17.9
Pod undocumented
68  
69 # verify 3 parameters
70 0 ▶ (@_ == 3) || croak('wrong number of parameters');
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or croak("wrong number of parameters") 0 0 0
71  
72 # write spline data to profile
73 0 _writeICCspline($self, @_);
74  
75 }
76  
77 # get tag size (for writing to profile)
78 # note: writes an equivalent curv object
79 # returns: (tag_size)
80 sub size {
81  
82 # get parameters
83 0 ▶ my ($self) = @_;
Subroutine size: not called CC=2 SCAR=17.9
Pod undocumented
84  
85 # get count value (defaults to 4096, the maximum allowed in an 'mft2' tag)
86 0 ▶ my $n = $self->[0]{'curv_points'} // 4096;
Condition0%0/2
$self->[0]{"curv_points"} // 4096
l!l
00
MC/DC0%0/1
$self->[0]{"curv_points"} // 4096
$self->[0]{"curv_points"}
Truth table0%0/2
A
A= $self->[0]{"curv_points"}
Aresult
11
00
87  
88 # return size
89 0 return(12 + $n * 2);
90  
91 }
92  
93 # compute curve function
94 # parameters: (input_value)
95 # returns: (output_value)
96 sub transform {
97  
98 # get parameters
99 0 ▶ my ($self, $in) = @_;
Subroutine transform: not called CC=9 SCAR=45.0
Pod undocumented
100  
101 # if an extrapolated solution (s < 0)
102 0 ▶ if (($self->[1][1] > $self->[1][0] && $in < $self->[1][0]) || ($self->[1][1] < $self->[1][0] && $in > $self->[1][0])) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->[1][1] > $self->[1][0] and $in < $self->[1][0] or $self->[1][1] < $self->[1][0] and $in > $self->[1][0]) { } 0 0 0
elsif ($self->[1][1] > $self->[1][0] and $in >= $self->[1][1] or $self->[1][1] < $self->[1][0] and $in <= $self->[1][1]) { } 0 0 0
Condition0%0/9
$self->[1][1] > $self->[1][0] and $in < $self->[1][0] or $self->[1][1] < $self->[1][0] and $in > $self->[1][0]
l!l&&r!l&&!r
000
$self->[1][1] > $self->[1][0] and $in < $self->[1][0]
!ll&&!rl&&r
000
$self->[1][1] < $self->[1][0] and $in > $self->[1][0]
!ll&&!rl&&r
000
MC/DC0%0/4
$self->[1][1] > $self->[1][0] and $in < $self->[1][0] or $self->[1][1] < $self->[1][0] and $in > $self->[1][0]
$self->[1][1] > $self->[1][0] $in < $self->[1][0] $self->[1][1] < $self->[1][0] $in > $self->[1][0]
Truth table0%0/7
A and B or C and D
A= $self->[1][1] > $self->[1][0]B= $in < $self->[1][0]C= $self->[1][1] < $self->[1][0]D= $in > $self->[1][0]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
103
104 # return extrapolated value
105 0 return($self->[2][0] + $self->[3][0] * $#{$self->[2]} * ($in - $self->[1][0])/($self->[1][1] - $self->[1][0]));
106
107 # if an extrapolated solution (s >= 1)
108 ▶ } elsif (($self->[1][1] > $self->[1][0] && $in >= $self->[1][1]) || ($self->[1][1] < $self->[1][0] && $in <= $self->[1][1])) {
Condition0%0/9
$self->[1][1] > $self->[1][0] and $in >= $self->[1][1] or $self->[1][1] < $self->[1][0] and $in <= $self->[1][1]
l!l&&r!l&&!r
000
$self->[1][1] > $self->[1][0] and $in >= $self->[1][1]
!ll&&!rl&&r
000
$self->[1][1] < $self->[1][0] and $in <= $self->[1][1]
!ll&&!rl&&r
000
MC/DC0%0/4
$self->[1][1] > $self->[1][0] and $in >= $self->[1][1] or $self->[1][1] < $self->[1][0] and $in <= $self->[1][1]
$self->[1][1] > $self->[1][0] $in >= $self->[1][1] $self->[1][1] < $self->[1][0] $in <= $self->[1][1]
Truth table0%0/7
A and B or C and D
A= $self->[1][1] > $self->[1][0]B= $in >= $self->[1][1]C= $self->[1][1] < $self->[1][0]D= $in <= $self->[1][1]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
109
110 # return extrapolated value
111 0 return($self->[2][-1] + $self->[3][-1] * $#{$self->[2]} * ($self->[1][1] - $in)/($self->[1][0] - $self->[1][1]));
112
113 } else {
114
115 # return interpolated value
116 0 return(_fwd($self, POSIX::modf($#{$self->[2]} * ($in - $self->[1][0])/($self->[1][1] - $self->[1][0]))));
117
118 }
119
120 }
121  
122 # compute inverse curve function
123 # note: there may be multiple solutions
124 # parameters: (input_value)
125 # returns: (output_value -or- array_of_output_values)
126 sub inverse {
127  
128 # get parameters
129 0 ▶ my ($self, $in) = @_;
Subroutine inverse: not called CC=15 SCAR=54.8
Pod undocumented
130  
131 # local variables
132 0 my ($n, $sf, @sol, @t);
133  
134 # get number of segments
135 0 $n = $#{$self->[2]};
136  
137 # compute scale factor
138 0 $sf = ($self->[1][1] - $self->[1][0])/$n;
139  
140 # if an extrapolated solution (s < 0)
141 0 ▶ if (($in < $self->[2][0] && $self->[3][0] > 0) || ($in > $self->[2][0] && $self->[3][0] < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in < $self->[2][0] and $self->[3][0] > 0 or $in > $self->[2][0] and $self->[3][0] < 0) 0 0 0
Condition0%0/9
$in < $self->[2][0] and $self->[3][0] > 0 or $in > $self->[2][0] and $self->[3][0] < 0
l!l&&r!l&&!r
000
$in < $self->[2][0] and $self->[3][0] > 0
!ll&&!rl&&r
000
$in > $self->[2][0] and $self->[3][0] < 0
!ll&&!rl&&r
000
MC/DC0%0/4
$in < $self->[2][0] and $self->[3][0] > 0 or $in > $self->[2][0] and $self->[3][0] < 0
$in < $self->[2][0] $self->[3][0] > 0 $in > $self->[2][0] $self->[3][0] < 0
Truth table0%0/7
A and B or C and D
A= $in < $self->[2][0]B= $self->[3][0] > 0C= $in > $self->[2][0]D= $self->[3][0] < 0
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
142
143 # add solution
144 0 push(@sol, $self->[1][0] + $sf * ($in - $self->[2][0])/$self->[3][0]);
145
146 }
147  
148 # for each segment (0 <= s < 1)
149 0 for my $i (0 .. $n - 1) {
150
151 # if input is lower knot
152 0 ▶ if ($in == $self->[2][$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in == $self->[2][$i]) 0 0 0
153
154 # add solution
155 0 push(@sol, $self->[1][0] + $i * $sf);
156
157 }
158
159 # if input lies within the y-value range
160 0 ▶ if ($in >= $self->[4][$i][0] && $in <= $self->[4][$i][1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in >= $self->[4][$i][0] and $in <= $self->[4][$i][1]) 0 0 0
Condition0%0/3
$in >= $self->[4][$i][0] and $in <= $self->[4][$i][1]
!ll&&!rl&&r
000
MC/DC0%0/2
$in >= $self->[4][$i][0] and $in <= $self->[4][$i][1]
$in >= $self->[4][$i][0] $in <= $self->[4][$i][1]
Truth table0%0/3
A and B
A= $in >= $self->[4][$i][0]B= $in <= $self->[4][$i][1]
ABresult
0X0
100
111
161
162 # compute inverse values (t-values)
163 0 @t = _rev($self, $in, $i);
164
165 # add solution(s)
166 0 push(@sol, map {$self->[1][0] + ($i + $_) * $sf} @t);
167
168 }
169
170 }
171  
172 # if input is last knot (s == 1)
173 0 ▶ if ($in == $self->[2][-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in == $self->[2][-1]) 0 0 0
174
175 # add solution
176 0 push(@sol, $self->[1][1]);
177
178 }
179  
180 # if an extrapolated solution (s > 1)
181 0 ▶ if (($in > $self->[2][-1] && $self->[3][-1] > 0) || ($in < $self->[2][-1] && $self->[3][-1] < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in > $self->[2][-1] and $self->[3][-1] > 0 or $in < $self->[2][-1] and $self->[3][-1] < 0) 0 0 0
Condition0%0/9
$in > $self->[2][-1] and $self->[3][-1] > 0 or $in < $self->[2][-1] and $self->[3][-1] < 0
l!l&&r!l&&!r
000
$in > $self->[2][-1] and $self->[3][-1] > 0
!ll&&!rl&&r
000
$in < $self->[2][-1] and $self->[3][-1] < 0
!ll&&!rl&&r
000
MC/DC0%0/4
$in > $self->[2][-1] and $self->[3][-1] > 0 or $in < $self->[2][-1] and $self->[3][-1] < 0
$in > $self->[2][-1] $self->[3][-1] > 0 $in < $self->[2][-1] $self->[3][-1] < 0
Truth table0%0/7
A and B or C and D
A= $in > $self->[2][-1]B= $self->[3][-1] > 0C= $in < $self->[2][-1]D= $self->[3][-1] < 0
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
182
183 # add solution
184 0 push(@sol, $self->[1][1] + $sf * ($in - $self->[2][-1])/$self->[3][-1]);
185
186 }
187  
188 # return result (array or first solution)
189 0 ▶ return(wantarray ? @sol : $sol[0]);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
190  
191 }
192  
193 # compute curve derivative
194 # parameters: (input_value)
195 # returns: (derivative_value)
196 sub derivative {
197  
198 # get parameters
199 0 ▶ my ($self, $in) = @_;
Subroutine derivative: not called CC=9 SCAR=45.0
Pod undocumented
200  
201 # if an extrapolated solution (s < 0)
202 0 ▶ if (($self->[1][1] > $self->[1][0] && $in < $self->[1][0]) || ($self->[1][1] < $self->[1][0] && $in > $self->[1][0])) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->[1][1] > $self->[1][0] and $in < $self->[1][0] or $self->[1][1] < $self->[1][0] and $in > $self->[1][0]) { } 0 0 0
elsif ($self->[1][1] > $self->[1][0] and $in >= $self->[1][1] or $self->[1][1] < $self->[1][0] and $in <= $self->[1][1]) { } 0 0 0
Condition0%0/9
$self->[1][1] > $self->[1][0] and $in < $self->[1][0] or $self->[1][1] < $self->[1][0] and $in > $self->[1][0]
l!l&&r!l&&!r
000
$self->[1][1] > $self->[1][0] and $in < $self->[1][0]
!ll&&!rl&&r
000
$self->[1][1] < $self->[1][0] and $in > $self->[1][0]
!ll&&!rl&&r
000
MC/DC0%0/4
$self->[1][1] > $self->[1][0] and $in < $self->[1][0] or $self->[1][1] < $self->[1][0] and $in > $self->[1][0]
$self->[1][1] > $self->[1][0] $in < $self->[1][0] $self->[1][1] < $self->[1][0] $in > $self->[1][0]
Truth table0%0/7
A and B or C and D
A= $self->[1][1] > $self->[1][0]B= $in < $self->[1][0]C= $self->[1][1] < $self->[1][0]D= $in > $self->[1][0]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
203
204 # return endpoint derivative
205 0 return($self->[3][0] * $#{$self->[2]}/($self->[1][1] - $self->[1][0]));
206
207 # if an extrapolated solution (s >= 1)
208 ▶ } elsif (($self->[1][1] > $self->[1][0] && $in >= $self->[1][1]) || ($self->[1][1] < $self->[1][0] && $in <= $self->[1][1])) {
Condition0%0/9
$self->[1][1] > $self->[1][0] and $in >= $self->[1][1] or $self->[1][1] < $self->[1][0] and $in <= $self->[1][1]
l!l&&r!l&&!r
000
$self->[1][1] > $self->[1][0] and $in >= $self->[1][1]
!ll&&!rl&&r
000
$self->[1][1] < $self->[1][0] and $in <= $self->[1][1]
!ll&&!rl&&r
000
MC/DC0%0/4
$self->[1][1] > $self->[1][0] and $in >= $self->[1][1] or $self->[1][1] < $self->[1][0] and $in <= $self->[1][1]
$self->[1][1] > $self->[1][0] $in >= $self->[1][1] $self->[1][1] < $self->[1][0] $in <= $self->[1][1]
Truth table0%0/7
A and B or C and D
A= $self->[1][1] > $self->[1][0]B= $in >= $self->[1][1]C= $self->[1][1] < $self->[1][0]D= $in <= $self->[1][1]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
209
210 # return endpoint derivative
211 0 return($self->[3][-1] * $#{$self->[2]}/($self->[1][1] - $self->[1][0]));
212
213 } else {
214
215 # return interpolated derivative value
216 0 return(_derv($self, POSIX::modf($#{$self->[2]} * ($in - $self->[1][0])/($self->[1][1] - $self->[1][0]))) * $#{$self->[2]}/($self->[1][1] - $self->[1][0]));
217
218 }
219
220 }
221  
222 # compute parametric partial derivatives
223 # parameters: (input_value)
224 # returns: (partial_derivative_array)
225 sub parametric {
226  
227 # get parameters
228 0 ▶ my ($self, $in) = @_;
Subroutine parametric: not called CC=14 SCAR=53.5
Pod undocumented
229  
230 # local variables
231 0 my ($low, $t, $tc, $h00, $h01, $h10, $h11, @pj);
232  
233 # update parametric derivative matrix, if necessary
234 0 ▶ _objpara($self) if (! defined($self->[5]) || $#{$self->[5]} != $#{$self->[2]});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->[5] or $#{$$self[5];} != $#{$$self[2];} 0 0 0
Condition0%0/3
not defined $self->[5] or $#{$$self[5];} != $#{$$self[2];}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $self->[5] or $#{$$self[5];} != $#{$$self[2];}
not defined $self->[5] $#{$$self[5];} != $#{$$self[2];}
Truth table0%0/3
not(A) or B
A= not defined $self->[5]B= $#{$$self[5];} != $#{$$self[2];}
ABresult
1X1
011
000
235  
236 # if an extrapolated solution (s < 0)
237 0 ▶ if (($self->[1][1] > $self->[1][0] && $in < $self->[1][0]) || ($self->[1][1] < $self->[1][0] && $in > $self->[1][0])) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->[1][1] > $self->[1][0] and $in < $self->[1][0] or $self->[1][1] < $self->[1][0] and $in > $self->[1][0]) { } 0 0 0
elsif ($self->[1][1] > $self->[1][0] and $in >= $self->[1][1] or $self->[1][1] < $self->[1][0] and $in <= $self->[1][1]) { } 0 0 0
Condition0%0/9
$self->[1][1] > $self->[1][0] and $in < $self->[1][0] or $self->[1][1] < $self->[1][0] and $in > $self->[1][0]
l!l&&r!l&&!r
000
$self->[1][1] > $self->[1][0] and $in < $self->[1][0]
!ll&&!rl&&r
000
$self->[1][1] < $self->[1][0] and $in > $self->[1][0]
!ll&&!rl&&r
000
MC/DC0%0/4
$self->[1][1] > $self->[1][0] and $in < $self->[1][0] or $self->[1][1] < $self->[1][0] and $in > $self->[1][0]
$self->[1][1] > $self->[1][0] $in < $self->[1][0] $self->[1][1] < $self->[1][0] $in > $self->[1][0]
Truth table0%0/7
A and B or C and D
A= $self->[1][1] > $self->[1][0]B= $in < $self->[1][0]C= $self->[1][1] < $self->[1][0]D= $in > $self->[1][0]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
238
239 # for each output value (parameter)
240 0 for my $i (0 .. $#{$self->[2]}) {
241
242 # compute partial derivative
243 0 $pj[$i] = ($i == 0) + $self->[5][0][$i] * $#{$self->[2]} * ($in - $self->[1][0])/($self->[1][1] - $self->[1][0]);
244
245 }
246
247 # if an extrapolated solution (s >= 1)
248 ▶ } elsif (($self->[1][1] > $self->[1][0] && $in >= $self->[1][1]) || ($self->[1][1] < $self->[1][0] && $in <= $self->[1][1])) {
Condition0%0/9
$self->[1][1] > $self->[1][0] and $in >= $self->[1][1] or $self->[1][1] < $self->[1][0] and $in <= $self->[1][1]
l!l&&r!l&&!r
000
$self->[1][1] > $self->[1][0] and $in >= $self->[1][1]
!ll&&!rl&&r
000
$self->[1][1] < $self->[1][0] and $in <= $self->[1][1]
!ll&&!rl&&r
000
MC/DC0%0/4
$self->[1][1] > $self->[1][0] and $in >= $self->[1][1] or $self->[1][1] < $self->[1][0] and $in <= $self->[1][1]
$self->[1][1] > $self->[1][0] $in >= $self->[1][1] $self->[1][1] < $self->[1][0] $in <= $self->[1][1]
Truth table0%0/7
A and B or C and D
A= $self->[1][1] > $self->[1][0]B= $in >= $self->[1][1]C= $self->[1][1] < $self->[1][0]D= $in <= $self->[1][1]
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
249
250 # for each output value (parameter)
251 0 for my $i (0 .. $#{$self->[2]}) {
252
253 # compute partial derivative
254 0 $pj[$i] = ($i == $#{$self->[2]}) + $self->[5][-1][$i] * $#{$self->[2]} * ($in - $self->[1][1])/($self->[1][1] - $self->[1][0]);
255
256 }
257
258 } else {
259
260 # compute position (0 - 1) and lower knot index
261 0 ($t, $low) = POSIX::modf($#{$self->[2]} * ($in - $self->[1][0])/($self->[1][1] - $self->[1][0]));
262
263 # compute Hermite coefficients
264 0 $tc = 1 - $t;
265 0 $h00 = (1 + 2 * $t) * $tc * $tc;
266 0 $h01 = 1 - $h00;
267 0 $h10 = $t * $tc * $tc;
268 0 $h11 = -$t * $t * $tc;
269
270 # for each output value (parameter)
271 0 for my $i (0 .. $#{$self->[2]}) {
272
273 # compute partial derivative
274 0 $pj[$i] = $h00 * ($low == $i) + $h01 * ($low + 1 == $i) + $h10 * $self->[5][$low][$i] + $h11 * $self->[5][$low + 1][$i];
275
276 }
277
278 }
279  
280 # return
281 0 return(@pj);
282  
283 }
284  
285 # get/set reference to header hash
286 # parameters: ([ref_to_new_hash])
287 # returns: (ref_to_hash)
288 sub header {
289  
290 # get object reference
291 0 ▶ my $self = shift();
Subroutine header: not called CC=4 SCAR=30.0
Pod undocumented
292  
293 # if there are parameters
294 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
295
296 # if one parameter, a hash reference
297 0 ▶ if (@_ == 1 && ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
298
299 # set header to copy of hash
300 0 $self->[0] = Storable::dclone(shift());
301
302 } else {
303
304 # error
305 0 croak('parameter must be a hash reference');
306
307 }
308
309 }
310  
311 # return reference
312 0 return($self->[0]);
313  
314 }
315  
316 # get/set input range array reference
317 # parameters: ([ref_to_array])
318 # returns: (ref_to_array)
319 sub range {
320  
321 # get object reference
322 0 ▶ my $self = shift();
Subroutine range: not called CC=7 SCAR=40.3
Pod undocumented
323  
324 # if one parameter supplied
325 0 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@_) { } 0 0 0
326
327 # verify parameter is an array reference
328 0 ▶ (ref($_[0]) eq 'ARRAY') || croak('parameter not an array reference');
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "ARRAY" or croak("parameter not an array reference") 0 0 0
329
330 # verify array contents
331 0 ▶ (@{$_[0]} == 2 && (@{$_[0]} == grep {Scalar::Util::looks_like_number($_)} @{$_[0]}) && $_[0]->[0] != $_[0]->[1]) || croak('array must contain 2 unequal numeric values');
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0];} == 2 and @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];}) and $_[0][0] != $_[0][1] or croak("array must contain 2 unequal numeric values") 0 0 0
Condition0%0/6
@{$_[0];} == 2 and @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];}) and $_[0][0] != $_[0][1]
!ll&&!rl&&r
000
@{$_[0];} == 2 and @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];})
!ll&&!rl&&r
000
MC/DC0%0/3
@{$_[0];} == 2 and @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];}) and $_[0][0] != $_[0][1]
@{$_[0];} == 2 @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];}) $_[0][0] != $_[0][1]
Truth table0%0/4
A and B and C
A= @{$_[0];} == 2B= @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];})C= $_[0][0] != $_[0][1]
ABCresult
0XX0
10X0
1100
1111
332
333 # store input range
334 0 $self->[1] = [@{$_[0]}];
335
336 } elsif (@_) {
337
338 # error
339 0 croak('too many parameters');
340
341 }
342
343 # return array reference
344 0 return($self->[1]);
345  
346 }
347  
348 # get/set output value array reference
349 # parameters: ([ref_to_array])
350 # returns: (ref_to_array)
351 sub array {
352  
353 # get object reference
354 0 ▶ my $self = shift();
Subroutine array: not called CC=6 SCAR=37.4
Pod undocumented
355  
356 # if one parameter supplied
357 0 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@_) { } 0 0 0
358
359 # verify parameter is an array reference
360 0 ▶ (ref($_[0]) eq 'ARRAY') || croak('parameter not an array reference');
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "ARRAY" or croak("parameter not an array reference") 0 0 0
361
362 # verify array contents
363 0 ▶ (@{$_[0]} >= 2 && (@{$_[0]} == grep {Scalar::Util::looks_like_number($_)} @{$_[0]})) || croak('array must contain 2 or more numeric values');
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0];} >= 2 and @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];}) or croak("array must contain 2 or more numeric values") 0 0 0
Condition0%0/3
@{$_[0];} >= 2 and @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];})
!ll&&!rl&&r
000
MC/DC0%0/2
@{$_[0];} >= 2 and @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];})
@{$_[0];} >= 2 @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];})
Truth table0%0/3
A and B
A= @{$_[0];} >= 2B= @{$_[0];} == grep({&Scalar::Util::looks_like_number($_);} @{$_[0];})
ABresult
0X0
100
111
364
365 # store output values
366 0 $self->[2] = [@{$_[0]}];
367
368 # recompute knot derivatives
369 0 _objderv($self);
370
371 # recompute segment min/max y-values
372 0 _minmax($self);
373
374 } elsif (@_) {
375
376 # error
377 0 croak('too many parameters');
378
379 }
380
381 # return array reference
382 0 return($self->[2]);
383  
384 }
385  
386 # check if monotonic
387 # returns min/max values
388 # returns s-values by default
389 # returns input-values if format is 'x'
390 # returns output-values if format is 'y'
391 # curve is monotonic if no min/max values
392 # parameters: ([format])
393 # returns: (array_of_values)
394 sub monotonic {
395  
396 # get object reference
397 0 ▶ my ($self, $fmt) = @_;
Subroutine monotonic: not called CC=5 SCAR=34.0
Pod undocumented
398  
399 # if format undefined or 's'
400 0 ▶ if (! defined($fmt) || $fmt eq 's') {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $fmt or $fmt eq 's') { } 0 0 0
elsif ($fmt eq 'x') { } 0 0 0
elsif ($fmt eq 'y') { } 0 0 0
Condition0%0/3
not defined $fmt or $fmt eq "s"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $fmt or $fmt eq "s"
not defined $fmt $fmt eq "s"
Truth table0%0/3
not(A) or B
A= not defined $fmtB= $fmt eq "s"
ABresult
1X1
011
000
401
402 # return s-values
403 0 return(_minmax($self));
404
405 # if format 'x'
406 } elsif ($fmt eq 'x') {
407
408 # return x-values
409 0 return(map {(1 - $_/$#{$self->[2]}) * $self->[1][0] + $_/$#{$self->[2]} * $self->[1][1]} _minmax($self));
410
411 # if format 'y'
412 } elsif ($fmt eq 'y') {
413
414 # return y-values
415 0 return(map {_fwd($self, POSIX::modf($_))} _minmax($self));
416
417 } else {
418
419 # error
420 0 croak("unsupported format for min/max values");
421
422 }
423
424 }
425  
426 # make table for 'curv' objects
427 # assumes curve domain/range is (0 - 1)
428 # parameters: (number_of_table_entries, [direction])
429 # returns: (ref_to_table_array)
430 sub table {
431  
432 # get parameters
433 0 ▶ my ($self, $n, $dir) = @_;
Subroutine table: not called CC=5 SCAR=34.0
Pod undocumented
434  
435 # local variables
436 0 my ($up, $table);
437  
438 # validate number of table entries
439 0 ▶ ($n == int($n) && $n >= 2) || carp('invalid number of table entries');
Branch: 1 branch
BranchTrueFalseTotal
$n == int $n and $n >= 2 or carp("invalid number of table entries") 0 0 0
Condition0%0/3
$n == int $n and $n >= 2
!ll&&!rl&&r
000
MC/DC0%0/2
$n == int $n and $n >= 2
$n == int $n $n >= 2
Truth table0%0/3
A and B
A= $n == int $nB= $n >= 2
ABresult
0X0
100
111
440  
441 # purify direction flag
442 0 ▶ $dir = ($dir) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 0 0 0
443  
444 # array upper index
445 0 $up = $n - 1;
446  
447 # for each table entry
448 0 for my $i (0 .. $up) {
449
450 # compute table value
451 0 $table->[$i] = _transform($self, $dir, $i/$up);
452
453 }
454  
455 # return table reference
456 0 return($table);
457  
458 }
459  
460 # make 'curv' object
461 # assumes curve domain/range is (0 - 1)
462 # parameters: (number_of_table_entries, [direction])
463 # returns: (ref_to_curv_object)
464 sub curv {
465  
466 # return 'curv' object reference
467 0 ▶ return(ICC::Profile::curv->new(table(@_)));
Subroutine curv: not called CC=1 SCAR=6.9
Pod undocumented
468  
469 }
470  
471 # normalize transform
472 # adjusts object values linearly
473 # so that endpoint values are 0 or 1
474 # adjusts input-range and output-values by default
475 # adjusts input-values if format is 'x'
476 # adjusts output-values if format is 'y'
477 # parameters: ([format])
478 sub normalize {
479  
480 # get parameters
481 0 ▶ my ($self, $fmt) = @_;
Subroutine normalize: not called CC=12 SCAR=50.5
Pod undocumented
482  
483 # local variables
484 0 my ($off, $range);
485  
486 # verify flag parameter
487 0 ▶ (! defined($fmt) || $fmt eq 'x' || $fmt eq 'y') || croak('invalid normalize format parameter');
Branch: 1 branch
BranchTrueFalseTotal
not defined $fmt or $fmt eq "x" or $fmt eq "y" or croak("invalid normalize format parameter") 0 0 0
Condition0%0/6
not defined $fmt or $fmt eq "x" or $fmt eq "y"
l!l&&r!l&&!r
000
not defined $fmt or $fmt eq "x"
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $fmt or $fmt eq "x" or $fmt eq "y"
not defined $fmt $fmt eq "x" $fmt eq "y"
Truth table0%0/4
not(A) or B or C
A= not defined $fmtB= $fmt eq "x"C= $fmt eq "y"
ABCresult
1XX1
01X1
0011
0000
488  
489 # if range selected
490 0 ▶ if (! defined($fmt) || $fmt eq 'x') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fmt or $fmt eq "x") 0 0 0
Condition0%0/3
not defined $fmt or $fmt eq "x"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $fmt or $fmt eq "x"
not defined $fmt $fmt eq "x"
Truth table0%0/3
not(A) or B
A= not defined $fmtB= $fmt eq "x"
ABresult
1X1
011
000
491
492 # if range is increasing
493 0 ▶ if ($self->[1][1] > $self->[1][0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1][1] > $self->[1][0]) { } 0 0 0
494
495 # set range (0 - 1)
496 0 $self->[1][0] = 0;
497 0 $self->[1][1] = 1;
498
499 } else {
500
501 # set range (1 - 0)
502 0 $self->[1][0] = 1;
503 0 $self->[1][1] = 0;
504
505 }
506
507 }
508  
509 # if output values selected
510 0 ▶ if (! defined($fmt) || $fmt eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fmt or $fmt eq "y") 0 0 0
Condition0%0/3
not defined $fmt or $fmt eq "y"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $fmt or $fmt eq "y"
not defined $fmt $fmt eq "y"
Truth table0%0/3
not(A) or B
A= not defined $fmtB= $fmt eq "y"
ABresult
1X1
011
000
511
512 # if output values are increasing
513 0 ▶ if ($self->[2][-1] > $self->[2][0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2][-1] > $self->[2][0]) { } 0 0 0
514
515 # set offset
516 0 $off = $self->[2][0];
517
518 # compute range
519 0 $range = ($self->[2][-1] - $self->[2][0]);
520
521 } else {
522
523 # set offset
524 0 $off = $self->[2][-1];
525
526 # compute range
527 0 $range = ($self->[2][0] - $self->[2][-1]);
528
529 }
530
531 # verify range
532 0 ▶ ($range != 0) || croak('cannot normalize output values');
Branch: 1 branch
BranchTrueFalseTotal
$range != 0 or croak("cannot normalize output values") 0 0 0
533
534 # for each output value
535 0 for my $i (0 .. $#{$self->[2]}) {
536
537 # adjust using offset and scale factor
538 0 $self->[2][$i] = ($self->[2][$i] - $off)/$range;
539
540 }
541
542 # recompute knot derivatives
543 0 _objderv($self);
544
545 # recompute segment min/max y-values
546 0 _minmax($self);
547
548 # clear parametric derivative matrix
549 0 $self->[5] = [];
550
551 }
552
553 }
554  
555 # print object contents to string
556 # format is an array structure
557 # parameter: ([format])
558 # returns: (string)
559 sub sdump {
560  
561 # get parameters
562 0 ▶ my ($self, $p) = @_;
Subroutine sdump: not called CC=5 SCAR=34.0
Pod documented
563  
564 # local variables
565 0 my ($s, $fmt);
566  
567 # resolve parameter to an array reference
568 0 ▶ $p = defined($p) ? ref($p) eq 'ARRAY' ? $p : [$p] : [];
Branch: 2 branches
BranchTrueFalseTotal
defined $p ? : 0 0 0
ref $p eq 'ARRAY' ? : 0 0 0
569  
570 # get format string
571 0 ▶ $fmt = defined($p->[0]) && ! ref($p->[0]) ? $p->[0] : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $p->[0] && !ref($p->[0]) ? : 0 0 0
Condition0%0/3
defined $p->[0] and not ref $p->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p->[0] and not ref $p->[0]
defined $p->[0] not ref $p->[0]
Truth table0%0/3
A and not(B)
A= defined $p->[0]B= not ref $p->[0]
ABresult
0X0
100
111
572  
573 # set string to object ID
574 0 $s = sprintf("'%s' object, (0x%x)\n", ref($self), $self);
575  
576 # return
577 0 return($s);
578  
579 }
580  
581 # compute parametric partial derivatives
582 # direction: 0 - normal, 1 - inverse
583 # parameters: (object_reference, direction, input_value)
584 # returns: (partial_derivative_array)
585 sub _parametric {
586  
587 # get parameters
588 0 ▶ my ($self, $dir, $in) = @_;
Subroutine _parametric: not called CC=2 SCAR=17.9
589  
590 # if inverse direction
591 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 0 0
592
593 # unimplemented function error
594 0 croak('unimplemented function');
595
596 } else {
597
598 # return array of partial derivatives
599 0 return(parametric($self, $in));
600
601 }
602
603 }
604  
605 # combined derivative
606 # direction: 0 - normal, 1 - inverse
607 # parameters: (object_reference, direction, input_value)
608 # returns: (derivative_value)
609 sub _derivative {
610  
611 # get parameters
612 0 ▶ my ($self, $dir, $in) = @_;
Subroutine _derivative: not called CC=2 SCAR=17.9
613  
614 # if inverse direction
615 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 0 0
616
617 # unimplemented function error
618 0 croak('unimplemented function');
619
620 } else {
621
622 # return derivative
623 0 return(derivative($self, $in));
624
625 }
626
627 }
628  
629 # combined transform
630 # direction: 0 - normal, 1 - inverse
631 # parameters: (object_reference, direction, input_value)
632 # returns: (output_value)
633 sub _transform {
634  
635 # get parameters
636 0 ▶ my ($self, $dir, $in) = @_;
Subroutine _transform: not called CC=2 SCAR=17.9
637  
638 # if inverse direction
639 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) { } 0 0 0
640
641 # return inverse
642 0 return(scalar(inverse($self, $in)));
643
644 } else {
645
646 # return transform
647 0 return(transform($self, $in));
648
649 }
650
651 }
652  
653 # compute knot derivatives for natural spline
654 # parameters: (ref_to_object)
655 sub _objderv {
656  
657 # get parameter
658 0 ▶ my $self = shift();
Subroutine _objderv: not called CC=5 SCAR=34.0
659  
660 # local variables
661 0 my ($ix, $rhs, $info, $derv);
662  
663 # verify object has two or more knots
664 0 ▶ (($ix = $#{$self->[2]}) > 0) || croak('object must have two or more knots');
Branch: 1 branch
BranchTrueFalseTotal
($ix = $#{$$self[2];}) > 0 or croak("object must have two or more knots") 0 0 0
665  
666 # check if ICC::Support::Lapack module is loaded
667 0 state $lapack = defined($INC{'ICC/Support/Lapack.pm'});
668  
669 # make empty Math::Matrix object
670 0 $rhs = bless([], 'Math::Matrix');
671  
672 # for each input element
673 0 for my $i (1 .. $ix - 1) {
674
675 # compute rhs (3 * (y[i + 1] - y[i - 1]))
676 0 $rhs->[$i][0] = 3 * ($self->[2][$i + 1] - $self->[2][$i - 1]);
677
678 }
679  
680 # set rhs endpoint values
681 0 $rhs->[0][0] = 3 * ($self->[2][1] - $self->[2][0]);
682 0 $rhs->[$ix][0] = 3 * ($self->[2][$ix] - $self->[2][$ix - 1]);
683  
684 # if ICC::Support::Lapack module is loaded
685 0 ▶ if ($lapack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lapack) { } 0 0 0
686
687 # solve for derivative matrix
688 0 ($info, $derv) = ICC::Support::Lapack::trisolve([(1) x $ix], [2, (4) x ($ix - 1), 2], [(1) x $ix], $rhs);
689
690 # otherwise, use Math::Matrix module
691 } else {
692
693 # solve for derivative matrix
694 0 $derv = Math::Matrix->tridiagonal([2, (4) x ($ix - 1), 2])->concat($rhs)->solve();
695
696 }
697  
698 # for each knot
699 0 for my $i (0 .. $ix) {
700
701 # set derivative value
702 0 $self->[3][$i] = $derv->[$i][0];
703
704 }
705  
706 }
707  
708 # compute partial derivative matrix for natural spline
709 # parameters: (ref_to_object)
710 sub _objpara {
711  
712 # get parameter
713 0 ▶ my $self = shift();
Subroutine _objpara: not called CC=5 SCAR=34.0
714  
715 # local variables
716 0 my ($ix, $rhs, $info, $derv);
717  
718 # verify object has two or more knots
719 0 ▶ (($ix = $#{$self->[2]}) > 0) || croak('object must have two or more knots');
Branch: 1 branch
BranchTrueFalseTotal
($ix = $#{$$self[2];}) > 0 or croak("object must have two or more knots") 0 0 0
720  
721 # check if ICC::Support::Lapack module is loaded
722 0 state $lapack = defined($INC{'ICC/Support/Lapack.pm'});
723  
724 # make rhs matrix (filled with zeros)
725 0 ▶ $rhs = $lapack ? ICC::Support::Lapack::zeros($ix + 1) : bless([map {[(0) x ($ix + 1)]} (0 .. $ix)], 'Math::Matrix');
Branch: 1 branch
BranchTrueFalseTotal
$lapack ? : 0 0 0
726  
727 # for each row
728 0 for my $i (1 .. $ix - 1) {
729
730 # set rhs diagonal values
731 0 $rhs->[$i - 1][$i] = 3;
732 0 $rhs->[$i + 1][$i] = -3;
733
734 }
735  
736 # set rhs endpoint values
737 0 $rhs->[0][0] = -3;
738 0 $rhs->[1][0] = -3;
739 0 $rhs->[$ix][$ix] = 3;
740 0 $rhs->[$ix - 1][$ix] = 3;
741  
742 # if ICC::Support::Lapack module is loaded
743 0 ▶ if ($lapack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lapack) { } 0 0 0
744
745 # solve for derivative matrix
746 0 ($info, $derv) = ICC::Support::Lapack::trisolve([(1) x $ix], [2, (4) x ($ix - 1), 2], [(1) x $ix], $rhs);
747
748 # set object
749 0 $self->[5] = bless($derv, 'Math::Matrix');
750
751 # otherwise, use Math::Matrix module
752 } else {
753
754 # solve for derivative matrix
755 0 $self->[5] = Math::Matrix->tridiagonal([2, (4) x ($ix - 1), 2])->concat($rhs)->solve();
756
757 }
758
759 }
760  
761 # add local min/max values
762 # parameters: (ref_to_object)
763 # returns: (s-value_array)
764 sub _minmax {
765  
766 # get object reference
767 0 ▶ my $self = shift();
Subroutine _minmax: not called CC=2 SCAR=17.9
768  
769 # local variables
770 0 my (@t, @y, @s);
771  
772 # for each interval
773 0 for my $i (0 .. $#{$self->[2]} - 1) {
774
775 # get min/max location(s)
776 0 @t = _local($self, $i);
777
778 # compute local min/max y-values
779 0 @y = map {_fwd($self, $_, $i)} @t;
780
781 # add the knot values
782 0 push(@y, ($self->[2][$i], $self->[2][$i + 1]));
783
784 # sort the values
785 0 @y = sort {$a <=> $b} @y;
786
787 # save y-value min/max span
788 0 $self->[4][$i] = [$y[0], $y[-1]];
789
790 # add min/max s-values
791 0 push(@s, map {$_ + $i} @t);
792
793 }
794  
795 # return min/max s-values
796 0 return(sort {$a <=> $b} @s);
797  
798 }
799  
800 # compute local min/max value(s)
801 # values are within the segment (t)
802 # parameters: (ref_to_object, segment)
803 # returns: (t-value_array)
804 sub _local {
805  
806 # get parameters
807 0 ▶ my ($self, $low) = @_;
Subroutine _local: not called CC=6 SCAR=37.4
808  
809 # local variables
810 0 my ($y1, $y2, $m1, $m2);
811 0 my ($a, $b, $c, $dscr, @t);
812  
813 # get endpoint values
814 0 $y1 = $self->[2][$low];
815 0 $y2 = $self->[2][$low + 1];
816 0 $m1 = $self->[3][$low];
817 0 $m2 = $self->[3][$low + 1];
818  
819 # compute coefficients of quadratic equation (at^2 + bt + c = 0)
820 0 $a = 6 * ($y1 - $y2) + 3 * ($m1 + $m2);
821 0 $b = -6 * ($y1 - $y2) - 2 * $m2 - 4 * $m1;
822 0 $c = $m1;
823  
824 # return if constant
825 0 ▶ return() if (abs($a) < 1E-15 && abs($b) < 1E-15);
Branch: 1 branch
BranchTrueFalseTotal
if abs $a < "1e-15" and abs $b < "1e-15" 0 0 0
Condition0%0/3
abs $a < "1e-15" and abs $b < "1e-15"
!ll&&!rl&&r
000
MC/DC0%0/2
abs $a < "1e-15" and abs $b < "1e-15"
abs $a < "1e-15" abs $b < "1e-15"
Truth table0%0/3
A and B
A= abs $a < "1e-15"B= abs $b < "1e-15"
ABresult
0X0
100
111
826  
827 # if linear equation (a is zero)
828 0 ▶ if (abs($a) < 1E-15) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $a < 1e-15) { } 0 0 0
829
830 # add solution
831 0 push(@t, -$c/$b);
832
833 # if quadratic equation
834 } else {
835
836 # compute discriminant
837 0 $dscr = $b**2 - 4 * $a * $c;
838
839 # if discriminant > zero
840 0 ▶ if ($dscr > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dscr > 0) 0 0 0
841
842 # add solutions (critical points)
843 0 push(@t, -($b + sqrt($dscr))/(2 * $a));
844 0 push(@t, -($b - sqrt($dscr))/(2 * $a));
845
846 }
847
848 }
849  
850 # return solution(s) within interval (0 < t < 0)
851 0 ▶ return (grep {$_ > 0 && $_ < 1} @t);
Branch: 1 branch
BranchTrueFalseTotal
$_ > 0 and $_ < 1 0 0 0
852  
853 }
854  
855 # compute second derivative for unit spline segment
856 # parameters: (ref_to_object, t-value, segment)
857 # returns: (second_derivative)
858 sub _derv2 {
859  
860 # get parameters
861 0 ▶ my ($self, $t, $low) = @_;
Subroutine _derv2: not called CC=1 SCAR=6.9
862  
863 # local variables
864 0 my ($h00, $h01, $h10, $h11);
865  
866 # compute Hermite derivative coefficients
867 0 $h00 = 12 * $t - 6;
868 0 $h01 = - $h00;
869 0 $h10 = 6 * $t - 4;
870 0 $h11 = 6 * $t - 2;
871  
872 # interpolate value
873 0 return($h00 * $self->[2][$low] + $h01 * $self->[2][$low + 1] + $h10 * $self->[3][$low] + $h11 * $self->[3][$low + 1]);
874  
875 }
876  
877 # compute derivative for unit spline segment
878 # parameters: (ref_to_object, t-value, segment)
879 # returns: (derivative)
880 sub _derv {
881  
882 # get parameters
883 0 ▶ my ($self, $t, $low) = @_;
Subroutine _derv: not called CC=2 SCAR=17.9
884  
885 # local variables
886 0 my ($tc, $ttc, $h00, $h01, $h10, $h11);
887  
888 # if position is non-zero
889 0 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) { } 0 0 0
890
891 # compute Hermite derivative coefficients
892 0 $tc = (1 - $t);
893 0 $ttc = -3 * $t * $tc;
894 0 $h00 = 2 * $ttc;
895 0 $h01 = -2 * $ttc;
896 0 $h10 = $ttc + $tc;
897 0 $h11 = $ttc + $t;
898
899 # interpolate value
900 0 return($h00 * $self->[2][$low] + $h01 * $self->[2][$low + 1] + $h10 * $self->[3][$low] + $h11 * $self->[3][$low + 1]);
901
902 } else {
903
904 # use lower knot derivative value
905 0 return($self->[3][$low]);
906
907 }
908
909 }
910  
911 # compute transform value
912 # parameters: (ref_to_object, t-value, segment)
913 # returns: (y-value)
914 sub _fwd {
915  
916 # get parameters
917 0 ▶ my ($self, $t, $low) = @_;
Subroutine _fwd: not called CC=3 SCAR=24.8
918  
919 # local variables
920 0 my ($tc, $h00, $h01, $h10, $h11);
921  
922 # check if ICC::Support::Lapack module is loaded
923 0 state $lapack = defined($INC{'ICC/Support/Lapack.pm'});
924  
925 # if position is non-zero
926 0 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) { } 0 0 0
927
928 # if ICC::Support::Lapack module is loaded
929 0 ▶ if ($lapack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lapack) { } 0 0 0
930
931 # interpolate value (Lapack XS)
932 0 return(ICC::Support::Lapack::hermite($t, $self->[2][$low], $self->[2][$low + 1], $self->[3][$low], $self->[3][$low + 1]));
933
934 } else {
935
936 # compute Hermite coefficients
937 0 $tc = 1 - $t;
938 0 $h00 = (1 + 2 * $t) * $tc * $tc;
939 0 $h01 = 1 - $h00;
940 0 $h10 = $t * $tc * $tc;
941 0 $h11 = -$t * $t * $tc;
942
943 # interpolate value (no Lapack XS)
944 0 return($h00 * $self->[2][$low] + $h01 * $self->[2][$low + 1] + $h10 * $self->[3][$low] + $h11 * $self->[3][$low + 1]);
945
946 }
947
948 } else {
949
950 # use lower knot output value
951 0 return($self->[2][$low]);
952
953 }
954
955 }
956  
957 # compute inverse value
958 # parameters: (ref_to_object, y-value, segment)
959 # there are 0 to 3 solutions in the range 0 < t < 1
960 # returns: (t-value_array)
961 sub _rev {
962  
963 # get parameters
964 0 ▶ my ($self, $in, $low) = @_;
Subroutine _rev: not called CC=19 SCAR=59.4
965  
966 # local variables
967 0 my ($y1, $y2, $m1, $m2);
968 0 my ($a, $b, $c, $d, $dscr, @t);
969 0 my ($d0, $d1, $cs, $cc, @r, $ccr, $sol, $lim0, $lim1);
970  
971 # get endpoint values
972 0 $y1 = $self->[2][$low];
973 0 $y2 = $self->[2][$low + 1];
974 0 $m1 = $self->[3][$low];
975 0 $m2 = $self->[3][$low + 1];
976  
977 # compute coefficients of cubic equation (at^3 + bt^2 + ct + d = 0)
978 0 $a = 2 * ($y1 - $y2) + $m1 + $m2;
979 0 $b = -3 * ($y1 - $y2) -2 * $m1 - $m2;
980 0 $c = $m1;
981 0 $d = $y1 - $in;
982  
983 # constant equation (a, b, c are zero)
984 0 ▶ if (abs($a) < 5E-15 && abs($b) < 5E-15 && abs($c) < 5E-15) {
Branch: 3 branches
BranchTrueFalseTotal
if (abs $a < 5e-15 and abs $b < 5e-15 and abs $c < 5e-15) { } 0 0 0
elsif (abs $a < 5e-15 and abs $b < 5e-15) { } 0 0 0
elsif (abs $a < 5e-15) { } 0 0 0
Condition0%0/6
abs $a < "5e-15" and abs $b < "5e-15" and abs $c < "5e-15"
!ll&&!rl&&r
000
abs $a < "5e-15" and abs $b < "5e-15"
!ll&&!rl&&r
000
MC/DC0%0/3
abs $a < "5e-15" and abs $b < "5e-15" and abs $c < "5e-15"
abs $a < "5e-15" abs $b < "5e-15" abs $c < "5e-15"
Truth table0%0/4
A and B and C
A= abs $a < "5e-15"B= abs $b < "5e-15"C= abs $c < "5e-15"
ABCresult
0XX0
10X0
1100
1111
985
986 # add solution
987 0 ▶ push(@t, 0.5) if ($d == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $d == 0 0 0 0
988
989 # linear equation (a, b are zero)
990 ▶ } elsif (abs($a) < 5E-15 && abs($b) < 5E-15) {
Condition0%0/3
abs $a < "5e-15" and abs $b < "5e-15"
!ll&&!rl&&r
000
MC/DC0%0/2
abs $a < "5e-15" and abs $b < "5e-15"
abs $a < "5e-15" abs $b < "5e-15"
Truth table0%0/3
A and B
A= abs $a < "5e-15"B= abs $b < "5e-15"
ABresult
0X0
100
111
991
992 # add solution
993 0 push(@t, -$d/$c);
994
995 # quadratic equation (a is zero)
996 } elsif (abs($a) < 5E-15) {
997
998 # compute discriminant: > 0 two real, == 0 one real, < 0 two complex
999 0 $dscr = $c**2 - 4 * $b * $d;
1000
1001 # if discriminant is zero
1002 0 ▶ if ($dscr == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dscr == 0) { } 0 0 0
elsif ($dscr > 0) { } 0 0 0
1003
1004 # add solution (double root)
1005 0 push(@t, -$c/(2 * $b));
1006
1007 # if discriminant > zero
1008 } elsif ($dscr > 0) {
1009
1010 # add solutions
1011 0 push(@t, -($c + sqrt($dscr))/(2 * $b));
1012 0 push(@t, -($c - sqrt($dscr))/(2 * $b));
1013
1014 }
1015
1016 # cubic equation
1017 } else {
1018
1019 # compute discriminant: > 0 three real, == 0 one or two real, < 0 one real and two complex
1020 0 $dscr = 18 * $a * $b * $c * $d - 4 * $b**3 * $d + $b**2 * $c**2 - 4 * $a * $c**3 - 27 * $a**2 * $d**2;
1021
1022 # compute ∆0
1023 0 $d0 = $b**2 - 3 * $a * $c;
1024
1025 # if discriminant is zero
1026 0 ▶ if ($dscr == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dscr == 0) { } 0 0 0
1027
1028 # if ∆0 is zero
1029 0 ▶ if ($d0 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d0 == 0) { } 0 0 0
1030
1031 # add solution (triple root)
1032 0 push(@t, -$b/(3 * $a));
1033
1034 } else {
1035
1036 # add solutions (double root and single root)
1037 0 push(@t, (9 * $a * $d - $b * $c)/(2 * $d0));
1038 0 push(@t, (4 * $a * $b * $c - 9 * $a**2 * $d - $b**3)/($a * $d0));
1039
1040 }
1041
1042 } else {
1043
1044 # compute ∆1
1045 0 $d1 = 2 * $b**3 - 9 * $a * $b * $c + 27 * $a**2 * $d;
1046
1047 # compute C (a complex number)
1048 0 $cs = sqrt($d1**2 - 4 * $d0**3);
1049 0 ▶ $cc = cbrt($d1 == $cs ? ($d1 + $cs)/2 : ($d1 - $cs)/2);
Branch: 1 branch
BranchTrueFalseTotal
$d1 == $cs ? : 0 0 0
1050
1051 # compute cube roots of 1 (three complex numbers)
1052 0 @r = root(1, 3);
1053
1054 # for each root
1055 0 for my $i (0 .. 2) {
1056
1057 # multiply by cube root of 1
1058 0 $ccr = $r[$i] * $cc;
1059
1060 # compute solution (a complex number)
1061 0 $sol = ($b + $ccr + $d0/$ccr)/(-3 * $a);
1062
1063 # add solution, if real
1064 0 ▶ push(@t, Re($sol)) if ($dscr > 0 || abs(Im($sol)) < 1E-15);
Branch: 1 branch
BranchTrueFalseTotal
if $dscr > 0 or abs Im($sol) < "1e-15" 0 0 0
Condition0%0/3
$dscr > 0 or abs Im($sol) < "1e-15"
l!l&&r!l&&!r
000
MC/DC0%0/2
$dscr > 0 or abs Im($sol) < "1e-15"
$dscr > 0 abs Im($sol) < "1e-15"
Truth table0%0/3
A or B
A= $dscr > 0B= abs Im($sol) < "1e-15"
ABresult
1X1
011
000
1065
1066 }
1067
1068 }
1069
1070 }
1071  
1072 # set test limits to avoid duplicates at knots
1073 0 ▶ $lim0 = ($in == $y1) ? 1E-14 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$in == $y1 ? : 0 0 0
1074 0 ▶ $lim1 = ($in == $y2) ? (1 - 1E-14) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$in == $y2 ? : 0 0 0
1075  
1076 # return valid solutions
1077 0 ▶ return(sort {$ICC::Support::spline::a <=> $ICC::Support::spline::b} grep {$_ > $lim0 && $_ < $lim1} @t);
Branch: 1 branch
BranchTrueFalseTotal
$_ > $lim0 and $_ < $lim1 0 0 0
1078  
1079 }
1080  
1081 # set object contents from parameter hash
1082 # parameters: (object_reference, ref_to_parameter_hash)
1083 sub _new_from_hash {
1084  
1085 # get parameters
1086 0 ▶ my ($self, $hash) = @_;
Subroutine _new_from_hash: not called CC=10 SCAR=47.0
1087  
1088 # local variables
1089 0 my ($output, $range);
1090  
1091 # if 'output_values' is defined
1092 0 ▶ if (defined($output = $hash->{'output_values'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($output = $hash->{'output_values'})) { } 0 0 0
1093
1094 # verify 'output_values' (two or more numeric values)
1095 0 ▶ (ref($output) eq 'ARRAY' && 2 <= @{$output} && (@{$output} == grep {Scalar::Util::looks_like_number($_)} @{$output})) || croak('invalid output values');
Branch: 1 branch
BranchTrueFalseTotal
ref $output eq "ARRAY" and 2 <= @{$output;} and @{$output;} == grep({&Scalar::Util::looks_like_number($_);} @{$output;}) or croak("invalid output values") 0 0 0
Condition0%0/6
ref $output eq "ARRAY" and 2 <= @{$output;} and @{$output;} == grep({&Scalar::Util::looks_like_number($_);} @{$output;})
!ll&&!rl&&r
000
ref $output eq "ARRAY" and 2 <= @{$output;}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $output eq "ARRAY" and 2 <= @{$output;} and @{$output;} == grep({&Scalar::Util::looks_like_number($_);} @{$output;})
ref $output eq "ARRAY" 2 <= @{$output;} @{$output;} == grep({&Scalar::Util::looks_like_number($_);} @{$output;})
Truth table0%0/4
A and B and C
A= ref $output eq "ARRAY"B= 2 <= @{$output;}C= @{$output;} == grep({&Scalar::Util::looks_like_number($_);} @{$output;})
ABCresult
0XX0
10X0
1100
1111
1096
1097 # set 'output_values'
1098 0 $self->[2] = [@{$output}];
1099
1100 } else {
1101
1102 # use default
1103 0 $self->[2] = [0, 1];
1104
1105 }
1106  
1107 # if 'input_range' is defined
1108 0 ▶ if (defined($range = $hash->{'input_range'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($range = $hash->{'input_range'})) { } 0 0 0
1109
1110 # verify 'input_range' (two unequal numeric values)
1111 0 ▶ (ref($range) eq 'ARRAY' && 2 == @{$range} && (2 == grep {Scalar::Util::looks_like_number($_)} @{$range}) && $range->[0] != $range->[1]) || croak('invalid input range');
Branch: 1 branch
BranchTrueFalseTotal
ref $range eq "ARRAY" and 2 == @{$range;} and 2 == grep({&Scalar::Util::looks_like_number($_);} @{$range;}) and $range->[0] != $range->[1] or croak("invalid input range") 0 0 0
Condition0%0/9
ref $range eq "ARRAY" and 2 == @{$range;} and 2 == grep({&Scalar::Util::looks_like_number($_);} @{$range;}) and $range->[0] != $range->[1]
!ll&&!rl&&r
000
ref $range eq "ARRAY" and 2 == @{$range;} and 2 == grep({&Scalar::Util::looks_like_number($_);} @{$range;})
!ll&&!rl&&r
000
ref $range eq "ARRAY" and 2 == @{$range;}
!ll&&!rl&&r
000
MC/DC0%0/4
ref $range eq "ARRAY" and 2 == @{$range;} and 2 == grep({&Scalar::Util::looks_like_number($_);} @{$range;}) and $range->[0] != $range->[1]
ref $range eq "ARRAY" 2 == @{$range;} 2 == grep({&Scalar::Util::looks_like_number($_);} @{$range;}) $range->[0] != $range->[1]
Truth table0%0/5
A and B and C and D
A= ref $range eq "ARRAY"B= 2 == @{$range;}C= 2 == grep({&Scalar::Util::looks_like_number($_);} @{$range;})D= $range->[0] != $range->[1]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1112
1113 # set 'input_range'
1114 0 $self->[1] = [@{$range}];
1115
1116 } else {
1117
1118 # use default
1119 0 $self->[1] = [0, 1];
1120
1121 }
1122  
1123 # compute knot derivatives
1124 0 _objderv($self);
1125  
1126 # add segment min/max y-values
1127 0 _minmax($self);
1128  
1129 }
1130  
1131 # write spline object to ICC profile
1132 # note: writes an equivalent curv object
1133 # parameters: (ref_to_object, ref_to_parent_object, file_handle, ref_to_tag_table_entry)
1134 sub _writeICCspline {
1135  
1136 # get parameters
1137 0 ▶ my ($self, $parent, $fh, $tag) = @_;
Subroutine _writeICCspline: not called CC=7 SCAR=40.3
1138  
1139 # local variables
1140 0 my ($n, $up, @table);
1141  
1142 # get count value (defaults to 4096, the maximum allowed in an 'mft2' tag)
1143 0 ▶ $n = $self->[0]{'curv_points'} // 4096;
Condition0%0/2
$self->[0]{"curv_points"} // 4096
l!l
00
MC/DC0%0/1
$self->[0]{"curv_points"} // 4096
$self->[0]{"curv_points"}
Truth table0%0/2
A
A= $self->[0]{"curv_points"}
Aresult
11
00
1144  
1145 # validate number of table entries
1146 0 ▶ ($n == int($n) && $n >= 2) || carp('invalid number of table entries');
Branch: 1 branch
BranchTrueFalseTotal
$n == int $n and $n >= 2 or carp("invalid number of table entries") 0 0 0
Condition0%0/3
$n == int $n and $n >= 2
!ll&&!rl&&r
000
MC/DC0%0/2
$n == int $n and $n >= 2
$n == int $n $n >= 2
Truth table0%0/3
A and B
A= $n == int $nB= $n >= 2
ABresult
0X0
100
111
1147  
1148 # array upper index
1149 0 $up = $n - 1;
1150  
1151 # for each table entry
1152 0 for my $i (0 .. $up) {
1153
1154 # transform value
1155 0 $table[$i] = transform($self, $i/$up);
1156
1157 }
1158  
1159 # seek start of tag
1160 0 seek($fh, $tag->[1], 0);
1161  
1162 # write tag type signature and count
1163 0 print $fh pack('a4 x4 N', 'curv', $n);
1164  
1165 # write array
1166 0 ▶ print $fh pack('n*', map {$_ < 0 ? 0 : ($_ > 1 ? 65535 : $_ * 65535 + 0.5)} @table);
Branch: 2 branches
BranchTrueFalseTotal
$_ < 0 ? : 0 0 0
$_ > 1 ? : 0 0 0
1167  
1168 }
1169  
1170 2 1;
« lib/ICC/Support/spline2.pm ↑ Summary blib/lib/ICC/Profile/cvst.pm »