lib/ICC/Support/spline2.pm

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