blib/lib/Geo/Spline.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 129 SCAR 40.3
CC
7
Cov
0%
pointlist
12.0
initialize
6.0
ellipsoid
6.0
CRAP 56.0
1 package Geo::Spline;
2  
3 =head1 NAME
4  
5 Geo::Spline - Calculate geographic locations between GPS fixes.
6  
7 =head1 SYNOPSIS
8  
9 use Geo::Spline;
10 my $p0={time=>1160449100.67, #seconds
11 lat=>39.197807, #degrees
12 lon=>-77.263510, #degrees
13 speed=>31.124, #m/s
14 heading=>144.8300}; #degrees clockwise from North
15 my $p1={time=>1160449225.66,
16 lat=>39.167718,
17 lon=>-77.242278,
18 speed=>30.615,
19 heading=>150.5300};
20 my $spline=Geo::Spline->new($p0, $p1);
21 my %point=$spline->point(1160449150);
22 print "Lat:", $point{"lat"}, ", Lon:", $point{"lon"}, "\n\n";
23  
24 my @points=$spline->pointlist();
25 foreach (@points) {
26 print "Lat:", $_->{"lat"}, ", Lon:", $_->{"lon"}, "\n";
27 }
28  
29 =head1 DESCRIPTION
30  
31 This program was developed to be able to calculate the position between two GPS fixes using a 2-dimensional 3rd order polynomial spline.
32  
33 f(t) = A + B(t-t0) + C(t-t0)^2 + D(t-t0)^3 #position in X and Y
34 f'(t) = B + 2C(t-t0) + 3D(t-t0)^2 #velocity in X and Y
35  
36 I did some simple Math (for an engineer with a math minor) to come up with these formulas to calculate the unknowns from our knowns.
37  
38 A = x0 # when (t-t0)=0 in f(t)
39 B = v0 # when (t-t0)=0 in f'(t)
40 C = (x1-A-B(t1-t0)-D(t1-t0)^3)/(t1-t0)^2 # solve for C from f(t)
41 C = (v1-B-3D(t1-t0)^2)/2(t1-t0) # solve for C from f'(t)
42 D = (v1(t1-t0)+B(t1-t0)-2x1+2A)/(t1-t0)^3 # equate C=C then solve for D
43  
44 =cut
45  
46 use strict;
47 use vars qw($VERSION);
48 use Geo::Constants qw{PI};
49 use Geo::Functions qw{deg_rad rad_deg round};
50  
51 $VERSION = sprintf("%d.%02d", q{Revision: 0.16} =~ /(\d+)\.(\d+)/);
52  
53 =head1 CONSTRUCTOR
54  
55 =head2 new
56  
57 my $spline=Geo::Spline->new($p0, $p1);
58  
59 =cut
60  
61 sub new {
62 my $this = shift();
63 my $class = ref($this) || $this;
64 my $self = {};
65 bless $self, $class;
66 $self->initialize(@_);
67 return $self;
68 }
69  
70 =head1 METHODS
71  
72 =cut
73  
74 sub initialize {
75 my $self = shift();
76 $self->{'pt0'}=shift();
77 $self->{'pt1'}=shift();
78 my $ellipsoid=$self->ellipsoid("WGS84");
79 my $dt=$self->{'pt1'}->{'time'} - $self->{'pt0'}->{'time'};
80 die ("Delta time must be greater than zero.") if ($dt<=0);
81 my ($A, $B, $C, $D)=$self->ABCD(
82 $self->{'pt0'}->{'time'},
83 $self->{'pt0'}->{'lat'} * $ellipsoid->polar_circumference / 360,
84 $self->{'pt0'}->{'speed'} * cos(rad_deg($self->{'pt0'}->{'heading'})),
85 $self->{'pt1'}->{'time'},
86 $self->{'pt1'}->{'lat'} * $ellipsoid->polar_circumference / 360,
87 $self->{'pt1'}->{'speed'} * cos(rad_deg($self->{'pt1'}->{'heading'})));
88 $self->{'Alat'}=$A;
89 $self->{'Blat'}=$B;
90 $self->{'Clat'}=$C;
91 $self->{'Dlat'}=$D;
92 ($A, $B, $C, $D)=$self->ABCD(
93 $self->{'pt0'}->{'time'},
94 $self->{'pt0'}->{'lon'} * $ellipsoid->equatorial_circumference / 360,
95 $self->{'pt0'}->{'speed'} * sin(rad_deg($self->{'pt0'}->{'heading'})),
96 $self->{'pt1'}->{'time'},
97 $self->{'pt1'}->{'lon'} * $ellipsoid->equatorial_circumference / 360,
98 $self->{'pt1'}->{'speed'} * sin(rad_deg($self->{'pt1'}->{'heading'})));
99 $self->{'Alon'}=$A;
100 $self->{'Blon'}=$B;
101 $self->{'Clon'}=$C;
102 $self->{'Dlon'}=$D;
103 }
104  
105 =head2 ellipsoid
106  
107 Method to set or retrieve the current ellipsoid object. The ellipsoid is a Geo::Ellipsoids object.
108  
109 my $ellipsoid=$obj->ellipsoid; #Default is WGS84
110  
111 $obj->ellipsoid('Clarke 1866'); #Built in ellipsoids from Geo::Ellipsoids
112 $obj->ellipsoid({a=>1}); #Custom Sphere 1 unit radius
113  
114 =cut
115  
116 sub ellipsoid {
117 my $self = shift();
118 if (@_) {
119 my $param=shift();
120 use Geo::Ellipsoids;
121 my $obj=Geo::Ellipsoids->new($param);
122 $self->{'ellipsoid'}=$obj;
123 }
124 return $self->{'ellipsoid'};
125 }
126  
127 sub ABCD {
128 my $self = shift();
129 my $t0 = shift();
130 my $x0 = shift();
131 my $v0 = shift();
132 my $t1 = shift();
133 my $x1 = shift();
134 my $v1 = shift();
135 #x=f(t)=A+B(t-t0)+C(t-t0)^2+D(t-t0)^3
136 #v=f'(t)=B+2C(t-t0)+3D(t-t0)^2
137 #A=x0
138 #B=v0
139 #C=(x1-A-B(t1-t0)-D(t1-t0)^3)/((t1-t0)^2) # from f(t)
140 #C=(v1-B-3D(t1-t0)^2)/2(t1-t0) # from f'(t)
141 #D=(v1t+Bt-2x1+2A)/t^3 # from C=C
142 my $A=$x0;
143 my $B=$v0;
144 #=(C3*(A3-A2)+B6*(A3-A2)-2*B3+2*B5)/(A3-A2)^3 # for Excel
145 my $D=($v1*($t1-$t0)+$B*($t1-$t0)-2*$x1+2*$A)/($t1-$t0)**3;
146 #=(B3-B5-B6*(A3-A2)-B8*(A3-A2)^3)/(A3-A2)^2 # for Excel
147 my $C=($x1-$A-$B*($t1-$t0)-$D*($t1-$t0)**3)/($t1-$t0)**2;
148 return($A,$B,$C,$D);
149 }
150  
151 =head2 point
152  
153 Method returns a single point from a single time.
154  
155 my $point=$spline->point($t1);
156 my %point=$spline->point($t1);
157  
158 =cut
159  
160 sub point {
161 my $self=shift();
162 my $timereal=shift();
163 my $ellipsoid=$self->ellipsoid;
164 my $t=$timereal-$self->{'pt0'}->{'time'};
165 my ($Alat, $Blat, $Clat, $Dlat)=($self->{'Alat'}, $self->{'Blat'},$self->{'Clat'},$self->{'Dlat'});
166 my ($Alon, $Blon, $Clon, $Dlon)=($self->{'Alon'}, $self->{'Blon'},$self->{'Clon'},$self->{'Dlon'});
167 my $lat=$Alat + $Blat * $t + $Clat * $t ** 2 + $Dlat * $t ** 3;
168 my $lon=$Alon + $Blon * $t + $Clon * $t ** 2 + $Dlon * $t ** 3;
169 my $vlat=$Blat + 2 * $Clat * $t + 3 * $Dlat * $t ** 2;
170 my $vlon=$Blon + 2 * $Clon * $t + 3 * $Dlon * $t ** 2;
171 my $speed=sqrt($vlat ** 2 + $vlon ** 2);
172 my $heading=PI()/2 - atan2($vlat,$vlon);
173 $heading=deg_rad($heading);
174 $lat/=$ellipsoid->polar_circumference / 360;
175 $lon/=$ellipsoid->equatorial_circumference / 360;
176 my %pt=(time=>$timereal,
177 lat=>$lat,
178 lon=>$lon,
179 speed=>$speed,
180 heading=>$heading);
181 return wantarray ? %pt : \%pt;
182 }
183  
184 =head2 pointlist
185  
186 Method returns a list of points from a list of times.
187  
188 my $list=$spline->pointlist($t1,$t2,$t3);
189 my @list=$spline->pointlist($t1,$t2,$t3);
190  
191 =cut
192  
193 sub pointlist {
194 my $self=shift();
195 my @list=@_;
196 @list=$self->timelist() if (scalar(@list)== 0);
197 my @points=();
198 foreach (@list) {
199 push @points, {$self->point($_)};
200 }
201 return wantarray ? @points : \@points;
202 }
203  
204 =head2 timelist
205  
206 Method returns a list of times (n+1). The default will return a list with an integer number of seconds between spline end points.
207  
208 my $list=$spline->timelist($samples);
209 my @list=$spline->timelist();
210  
211 =cut
212  
213 sub timelist {
214 my $self=shift();
215 my $t0=$self->{'pt0'}->{'time'};
216 my $t1=$self->{'pt1'}->{'time'};
217 my $dt=$t1-$t0;
218 my $count=shift() || round($dt);
219 my @list;
220 foreach(0..$count) {
221 my $t=$t0+$dt*($_/$count);
222 push @list, $t;
223 }
224 return wantarray ? @list : \@list;
225 }
226  
227 1;
228  
229 __END__