blib/lib/Term/Fabulous/Chart/Curve.pm

filter: Statementstmt 92.3% 24 / 26 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.2% 33 / 35 CC 1 SCAR 0.3
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Term/Fabulous/Range.pm ↑ Summary blib/lib/Term/Fabulous/Theme.pm »
1 package Term::Fabulous::Chart::Curve;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 our $VERSION = '0.05';
9  
10 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 our @EXPORT_OK = qw(curve_points curve_names is_curve check_curve y_at);
12  
13 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Term::Fabulous::Chart::Easing qw(easing easing_names is_easing_name);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 ▶use Term::Fabulous::Check qw(describe);
Subroutine BEGIN: called CC=1 SCAR=2.6
17  
18 my %SHAPES = map { $_ => 1 } qw(linear step step-after step-before step-middle monotone catmull-rom natural);
19  
20 sub curve_names () {
21 my @names = ( sort( keys %SHAPES ), grep { $_ ne 'linear' } easing_names() );
22 return @names;
23 }
24  
25 sub is_curve ($curve) {
26 return 1 if ref $curve eq 'CODE';
27 return defined $curve && !ref $curve && ( $SHAPES{$curve} || is_easing_name($curve) ) ? 1 : 0;
28 }
29  
30 sub check_curve ( $owner, $name, $curve ) {
31 croak "$owner: $name must be a curve name ("
32 . join( ', ', sort keys %SHAPES )
33 . ", or an easing such as ease-in-out-sine) or a code reference, got "
34 . ( defined $curve ? ( ref $curve ? ref($curve) . ' reference' : "'$curve'" ) : 'undef' )
35 unless is_curve($curve);
36 return $curve;
37 }
38  
39 # The polyline that draws a curve through points ([x, y], x ascending):
40 # the points themselves for straight lines, the corners of steps, and
41 # for smooth curves a point every $step along x besides the data points.
42 # With between => [ first, last ] only the part from point first to
43 # point last is made; the shape there is the shape of the whole curve.
44 sub curve_points ( $points, $curve, %options ) {
45 my $step = $options{step} // 1;
46 croak "Term::Fabulous::Chart::Curve: step must be positive" unless $step > 0;
47 my ( $first, $last ) = _checked_between( $options{between}, $points );
48 return [ map { [@$_] } @$points[ $first .. $last ] ] if @$points < 2 || $curve eq 'linear';
49 return _steps( $points, $curve, $first, $last ) if !ref $curve && $curve =~ /\Astep/;
50  
51 my $segment
52 = ref $curve eq 'CODE' ? _eased($curve)
53 : is_easing_name($curve) ? _eased( easing($curve) )
54 : $curve eq 'monotone' ? _hermite( $points, _monotone_tangents($points) )
55 : $curve eq 'catmull-rom' ? _hermite( $points, _cardinal_tangents( $points, $options{tension} // 0 ) )
56 : $curve eq 'natural' ? _natural($points)
57 : croak "Term::Fabulous::Chart::Curve: unknown curve '$curve'";
58  
59 my @polyline = ( [ @{ $points->[$first] } ] );
60 foreach my $index ( $first .. $last - 1 ) {
61 my ( $from, $to ) = ( $points->[$index], $points->[ $index + 1 ] );
62 my $width = $to->[0] - $from->[0];
63 my $count = $width > 0 ? int( $width / $step ) : 0;
64 foreach my $sample ( 1 .. $count ) {
65 my $t = $sample * $step / $width;
66 last if $t >= 1;
67 push @polyline, [ $from->[0] + $t * $width, $segment->( $index, $t, $from, $to ) ];
68 }
69 push @polyline, [@$to];
70 }
71 return \@polyline;
72 }
73  
74 # The indexes of the first and last point the polyline runs through: all
75 # points, or the two of between => [ first, last ].
76 sub _checked_between ( $between, $points ) {
77 return ( 0, $#$points ) unless defined $between;
78 my $valid = ref $between eq 'ARRAY' && @$between == 2 && !grep { !defined || ref || !/\A[0-9]+\z/ } @$between;
79 croak "Term::Fabulous::Chart::Curve: between must be [ first, last ], two point indexes, got "
80 . ( ref $between eq 'ARRAY' ? '[ ' . join( ', ', map { describe($_) } @$between ) . ' ]' : describe($between) )
81 unless $valid;
82 my ( $first, $last ) = @$between;
83 croak "Term::Fabulous::Chart::Curve: between [ $first, $last ] must run forward within the points 0 .. $#$points" unless $first <= $last && $last <= $#$points;
84 return ( $first, $last );
85 }
86  
87 sub _steps ( $points, $curve, $first, $last ) {
88 my @polyline = ( [ @{ $points->[$first] } ] );
89 foreach my $index ( $first + 1 .. $last ) {
90 my ( $from, $to ) = ( $points->[ $index - 1 ], $points->[$index] );
91 if ( $curve eq 'step-before' ) {
92 push @polyline, [ $from->[0], $to->[1] ];
93 }
94 elsif ( $curve eq 'step-middle' ) {
95 my $middle = ( $from->[0] + $to->[0] ) / 2;
96 push @polyline, [ $middle, $from->[1] ], [ $middle, $to->[1] ];
97 }
98 else {
99 push @polyline, [ $to->[0], $from->[1] ];
100 }
101 push @polyline, [@$to];
102 }
103 return \@polyline;
104 }
105  
106 sub _eased ($ease) {
107 return sub ( $index, $t, $from, $to ) { $from->[1] + ( $to->[1] - $from->[1] ) * $ease->($t) };
108 }
109  
110 # Cubic Hermite segments through the points with the given slopes.
111 sub _hermite ( $points, $slopes ) {
112 return sub ( $index, $t, $from, $to ) {
113 my $width = $to->[0] - $from->[0];
114 my ( $t2, $t3 ) = ( $t**2, $t**3 );
115 return ( 2 * $t3 - 3 * $t2 + 1 ) * $from->[1] + ( $t3 - 2 * $t2 + $t ) * $width * $slopes->[$index] + ( -2 * $t3 + 3 * $t2 ) * $to->[1] + ( $t3 - $t2 ) * $width * $slopes->[ $index + 1 ];
116 };
117 }
118  
119 sub _secants ($points) {
120 return map {
121 my $width = $points->[ $_ + 1 ][0] - $points->[$_][0];
122 $width ? ( $points->[ $_ + 1 ][1] - $points->[$_][1] ) / $width : 0
123 } 0 .. $#$points - 1;
124 }
125  
126 # Fritsch-Carlson: slopes that keep the curve monotone between any two
127 # points, so it never overshoots the data.
128 sub _monotone_tangents ($points) {
129 my @secant = _secants($points);
130 my @slope = ( $secant[0], ( map { $secant[ $_ - 1 ] * $secant[$_] <= 0 ? 0 : ( $secant[ $_ - 1 ] + $secant[$_] ) / 2 } 1 .. $#secant ), $secant[-1] );
131 foreach my $index ( 0 .. $#secant ) {
132 if ( $secant[$index] == 0 ) {
133 @slope[ $index, $index + 1 ] = ( 0, 0 );
134 next;
135 }
136 my ( $alpha, $beta ) = ( $slope[$index] / $secant[$index], $slope[ $index + 1 ] / $secant[$index] );
137 my $length = $alpha**2 + $beta**2;
138 next if $length <= 9;
139 my $tau = 3 / sqrt $length;
140 @slope[ $index, $index + 1 ] = ( $tau * $alpha * $secant[$index], $tau * $beta * $secant[$index] );
141 }
142 return \@slope;
143 }
144  
145 # Cardinal spline slopes; tension 0 is Catmull-Rom, 1 straight lines.
146 sub _cardinal_tangents ( $points, $tension ) {
147 croak "Term::Fabulous::Chart::Curve: tension must be between 0 and 1, got $tension" unless $tension >= 0 && $tension <= 1;
148 my @secant = _secants($points);
149 my @slope;
150 foreach my $index ( 0 .. $#$points ) {
151 my ( $before, $after ) = ( $points->[ max( 0, $index - 1 ) ], $points->[ min( $#$points, $index + 1 ) ] );
152 my $width = $after->[0] - $before->[0];
153 push @slope, ( 1 - $tension ) * ( $width ? ( $after->[1] - $before->[1] ) / $width : 0 );
154 }
155 return \@slope;
156 }
157  
158 # The natural cubic spline: smooth second derivatives, straight at the
159 # ends. It may overshoot between points. A point between two others at
160 # its x (three or more points at one x) is pinned straight like the ends:
161 # its equation would be all zeros.
162 sub _natural ($points) {
163 my $last = $#$points;
164 my @width = map { $points->[ $_ + 1 ][0] - $points->[$_][0] } 0 .. $last - 1;
165 my ( @lower, @diagonal, @upper, @right );
166 @diagonal[ 0, $last ] = ( 1, 1 );
167 @right[ 0, $last ] = ( 0, 0 );
168 foreach my $index ( 1 .. $last - 1 ) {
169 my ( $before, $after ) = ( $width[ $index - 1 ], $width[$index] );
170 if ( $before == 0 && $after == 0 ) {
171 ( $lower[$index], $diagonal[$index], $upper[$index], $right[$index] ) = ( 0, 1, 0, 0 );
172 next;
173 }
174 ( $lower[$index], $diagonal[$index], $upper[$index] ) = ( $before, 2 * ( $before + $after ), $after );
175 $right[$index] = 6 * ( ( $points->[ $index + 1 ][1] - $points->[$index][1] ) / ( $after || 1 ) - ( $points->[$index][1] - $points->[ $index - 1 ][1] ) / ( $before || 1 ) );
176 }
177  
178 # Thomas algorithm for the second derivatives.
179 my ( @c, @d );
180 ( $c[0], $d[0] ) = ( 0, 0 );
181 foreach my $index ( 1 .. $last ) {
182 my $denominator = $diagonal[$index] - ( $lower[$index] // 0 ) * $c[ $index - 1 ];
183 $c[$index] = ( $upper[$index] // 0 ) / $denominator;
184 $d[$index] = ( $right[$index] - ( $lower[$index] // 0 ) * $d[ $index - 1 ] ) / $denominator;
185 }
186 my @second = (0) x ( $last + 1 );
187 for ( my $index = $last - 1; $index > 0; $index-- ) {
188 $second[$index] = $d[$index] - $c[$index] * $second[ $index + 1 ];
189 }
190 return sub ( $index, $t, $from, $to ) {
191 my $h = $width[$index];
192 my ( $before, $after ) = ( 1 - $t, $t );
193 return $before * $from->[1] + $after * $to->[1] + ( ( $before**3 - $before ) * $second[$index] + ( $after**3 - $after ) * $second[ $index + 1 ] ) * $h**2 / 6;
194 };
195 }
196  
197 # The y of a polyline at x, between the vertices around it; undef
198 # outside it. Vertical runs (steps) give the y where the run ends.
199 sub y_at ( $polyline, $x ) {
200 return undef if !@$polyline || $x < $polyline->[0][0] || $x > $polyline->[-1][0];
201 my ( $low, $high ) = ( 0, $#$polyline );
202 while ( $high - $low > 1 ) {
203 my $middle = int( ( $low + $high ) / 2 );
204 $polyline->[$middle][0] <= $x ? ( $low = $middle ) : ( $high = $middle );
205 }
206 my ( $from, $to ) = @$polyline[ $low, $high ];
207 return $from->[1] if $to->[0] == $from->[0];
208 return $from->[1] + ( $to->[1] - $from->[1] ) * ( $x - $from->[0] ) / ( $to->[0] - $from->[0] );
209 }
210  
211 1;
212  
213 __END__
« blib/lib/Term/Fabulous/Range.pm ↑ Summary blib/lib/Term/Fabulous/Theme.pm »