File Coverage

blib/lib/Math/Interpolator/Source.pm
Criterion Covered Total %
statement 17 21 80.9
branch 0 2 0.0
condition n/a
subroutine 8 9 88.8
pod 5 5 100.0
total 30 37 81.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Math::Interpolator::Source - source of points for use in interpolation
4              
5             =head1 SYNOPSIS
6              
7             use Math::Interpolator::Source;
8              
9             $pt = Math::Interpolator::Source->new(sub { ... }, $x);
10             $pt = Math::Interpolator::Source->new(sub { ... }, $x, $y);
11              
12             $x = $pt->x;
13             $y = $pt->y;
14              
15             $role = $pt->role;
16              
17             $points = $pt->expand;
18              
19             =head1 DESCRIPTION
20              
21             An object of this type represents a potential to generate some number of
22             adjacent knots on a one-dimensional curve. It is intended for use with
23             C, which will interpolate a curve between knots.
24             A source is expanded into knots only if required for an attempted
25             interpolation.
26              
27             For interpolation in a particular part of a curve, a number of known knots
28             are required, each one consisting of an x/y coordinate pair. It is not
29             necessary to know all knot coordinates, or even the number of knots,
30             elsewhere on the curve. A source stands in for a group of knots that
31             has not yet been examined in detail.
32              
33             A source covers a contiguous range of x coordinates. It is not necessary
34             to know precisely what that range is; the whole range is represented
35             by a single x coordinate that lies within it. When that range of x
36             coordinates needs to be examined, the source is expanded, replacing it
37             with the entire group of knots that lies within the range. If reverse
38             interpolation is to be performed then the same goes for y coordinates too.
39              
40             The expansion of a source may include more sources for subranges.
41              
42             =cut
43              
44             package Math::Interpolator::Source;
45              
46 4     4   5152 { use 5.006; }
  4         13  
  4         170  
47 4     4   20 use warnings;
  4         8  
  4         100  
48 4     4   19 use strict;
  4         8  
  4         143  
49              
50 4     4   20 use Carp qw(croak);
  4         8  
  4         1081  
51              
52             our $VERSION = "0.005";
53              
54             =head1 CONSTRUCTOR
55              
56             =over
57              
58             =item Math::Interpolator::Source->new(EXPANDER, X[, Y])
59              
60             Creates and returns a new source object. EXPANDER must be a reference
61             to a function, which will be called to implement the C method.
62              
63             A representative x coordinate must be supplied which is within the range
64             of x coordinates that is covered by the source. It is not required
65             for the x coordinate to match any of the knots that will be generated,
66             nor even for it to be between generated knots.
67              
68             If x values are to be interpolated as well as y values, then a
69             representative y coordinate must also be supplied. This works in exactly
70             the same way as the representative x coordinate. If interpolation is
71             only to be performed in one direction then a y coordinate is not required.
72              
73             =cut
74              
75             sub new {
76 12     12 1 2162 my($class, $expander, $x, $y) = @_;
77 12         99 return bless({
78             expander => $expander,
79             x => $x,
80             y => $y,
81             }, $class);
82             }
83              
84             =back
85              
86             =head1 METHODS
87              
88             =over
89              
90             =item $pt->x
91              
92             Returns the representative x coordinate of the source.
93              
94             =cut
95              
96 12     12 1 81 sub x { $_[0]->{x} }
97              
98             =item $pt->y
99              
100             Returns the representative y coordinate of the source, if there is one.
101             Cs if not.
102              
103             =cut
104              
105             sub y {
106 0     0 1 0 my($self) = @_;
107 0         0 my $y = $self->{y};
108 0 0       0 croak "no y coordinate for this source" unless defined $y;
109 0         0 return $y;
110             }
111              
112             =item $pt->role
113              
114             Returns the string "SOURCE". This is used to distinguish sources from
115             other types of entity that could appear in an interpolator's point list.
116              
117             =cut
118              
119 12     12 1 48 sub role { "SOURCE" }
120              
121             =item $pt->expand
122              
123             Returns a reference to an array of point objects represented by this
124             source. The types of objects permitted to be returned are the same
125             as permitted when constructing an interpolator: knots and sources.
126             The array may be empty. If non-empty, the resulting points are sorted
127             in monotonically non-decreasing order of x coordinates.
128              
129             Cs if expansion is presently impossible.
130              
131             =cut
132              
133 12     12 1 40 sub expand { $_[0]->{expander}->() }
134              
135             =back
136              
137             =head1 SUBCLASSING
138              
139             The interpolator uses only this public interface, so it is acceptable
140             to substitute any other class that implements this interface. This may
141             be done by subclassing this class, or by reimplementing all four methods
142             independently. This is useful, for example, to avoid having to package
143             all the data necessary for expansion into a closure.
144              
145             =head1 SEE ALSO
146              
147             L,
148             L
149              
150             =head1 AUTHOR
151              
152             Andrew Main (Zefram)
153              
154             =head1 COPYRIGHT
155              
156             Copyright (C) 2006, 2007, 2009, 2010, 2012
157             Andrew Main (Zefram)
158              
159             =head1 LICENSE
160              
161             This module is free software; you can redistribute it and/or modify it
162             under the same terms as Perl itself.
163              
164             =cut
165              
166             1;