]> git.mxchange.org Git - simgear.git/blob - simgear/math/polar3d.cxx
Mathias Fröhlich:
[simgear.git] / simgear / math / polar3d.cxx
1 // polar.cxx -- routines to deal with polar math and transformations
2 //
3 // Written by Curtis Olson, started June 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public
18 // License along with this library; if not, write to the
19 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 // Boston, MA  02111-1307, USA.
21 //
22 // $Id$
23
24
25 #include <math.h>
26
27 #include <simgear/constants.h>
28
29 #include "polar3d.hxx"
30
31 /**
32  * Calculate new lon/lat given starting lon/lat, and offset radial, and
33  * distance.  NOTE: starting point is specifed in radians, distance is
34  * specified in meters (and converted internally to radians)
35  * ... assumes a spherical world.
36  * @param orig specified in polar coordinates
37  * @param course offset radial
38  * @param dist offset distance
39  * @return destination point in polar coordinates
40  */
41 Point3D calc_gc_lon_lat( const Point3D& orig, double course,
42                                 double dist ) {
43     Point3D result;
44
45     // lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
46     // IF (cos(lat)=0)
47     //   lon=lon1      // endpoint a pole
48     // ELSE
49     //   lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
50     // ENDIF
51
52     // printf("calc_lon_lat()  offset.theta = %.2f offset.dist = %.2f\n",
53     //        offset.theta, offset.dist);
54
55     dist *= SG_METER_TO_NM * SG_NM_TO_RAD;
56
57     result.sety( asin( sin(orig.y()) * cos(dist) +
58                        cos(orig.y()) * sin(dist) * cos(course) ) );
59
60     if ( cos(result.y()) < SG_EPSILON ) {
61         result.setx( orig.x() );      // endpoint a pole
62     } else {
63         result.setx(
64             fmod(orig.x() - asin( sin(course) * sin(dist) /
65                                   cos(result.y()) )
66                  + SGD_PI, SGD_2PI) - SGD_PI );
67     }
68
69     return result;
70 }
71
72 /**
73  * Calculate course/dist given two spherical points.
74  * @param start starting point
75  * @param dest ending point
76  * @param course resulting course
77  * @param dist resulting distance
78  */
79 void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
80                                  double *course, double *dist )
81 {
82     if ( start == dest) {
83             *dist=0;
84             *course=0;
85             return;
86     }
87     // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
88     //            cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
89     double cos_start_y = cos( start.y() );
90     double tmp1 = sin( (start.y() - dest.y()) * 0.5 );
91     double tmp2 = sin( (start.x() - dest.x()) * 0.5 );
92     double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
93                                  cos_start_y * cos(dest.y()) * tmp2 * tmp2));
94
95     *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
96
97 #if 1
98     double c1 = atan2(
99                 cos(dest.y())*sin(dest.x()-start.x()),
100                 cos(start.y())*sin(dest.y())-
101                 sin(start.y())*cos(dest.y())*cos(dest.x()-start.x()));
102     if (c1 >= 0)
103       *course = SGD_2PI-c1;
104     else
105       *course = -c1;
106 #else
107     // We obtain the initial course, tc1, (at point 1) from point 1 to
108     // point 2 by the following. The formula fails if the initial
109     // point is a pole. We can special case this with:
110     //
111     // IF (cos(lat1) < EPS)   // EPS a small number ~ machine precision
112     //   IF (lat1 > 0)
113     //     tc1= pi        //  starting from N pole
114     //   ELSE
115     //     tc1= 0         //  starting from S pole
116     //   ENDIF
117     // ENDIF
118     //
119     // For starting points other than the poles:
120     //
121     // IF sin(lon2-lon1)<0
122     //   tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
123     // ELSE
124     //   tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
125     // ENDIF
126
127     // if ( cos(start.y()) < SG_EPSILON ) {
128     // doing it this way saves a transcendental call
129     double sin_start_y = sin( start.y() );
130     if ( fabs(1.0-sin_start_y) < SG_EPSILON ) {
131         // EPS a small number ~ machine precision
132         if ( start.y() > 0 ) {
133             *course = SGD_PI;   // starting from N pole
134         } else {
135             *course = 0;        // starting from S pole
136         }
137     } else {
138         // For starting points other than the poles:
139         // double tmp3 = sin(d)*cos_start_y);
140         // double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
141         // double tmp5 = acos(tmp4/tmp3);
142
143         // Doing this way gaurentees that the temps are
144         // not stored into memory
145         double tmp5 = acos( (sin(dest.y()) - sin_start_y * cos(d)) /
146                             (sin(d) * cos_start_y) );
147
148         // if ( sin( dest.x() - start.x() ) < 0 ) {
149         // the sin of the negative angle is just the opposite sign
150         // of the sin of the angle  so tmp2 will have the opposite
151         // sign of sin( dest.x() - start.x() )
152         if ( tmp2 >= 0 ) {
153             *course = tmp5;
154         } else {
155             *course = SGD_2PI - tmp5;
156         }
157     }
158 #endif
159 }
160
161
162 #if 0
163 /**
164  * Calculate course/dist given two spherical points.
165  * @param start starting point
166  * @param dest ending point
167  * @param course resulting course
168  * @param dist resulting distance
169  */
170 void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
171                                  double *course, double *dist ) {
172     // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
173     //            cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
174     double tmp1 = sin( (start.y() - dest.y()) / 2 );
175     double tmp2 = sin( (start.x() - dest.x()) / 2 );
176     double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
177                                  cos(start.y()) * cos(dest.y()) * tmp2 * tmp2));
178     // We obtain the initial course, tc1, (at point 1) from point 1 to
179     // point 2 by the following. The formula fails if the initial
180     // point is a pole. We can special case this with:
181     //
182     // IF (cos(lat1) < EPS)   // EPS a small number ~ machine precision
183     //   IF (lat1 > 0)
184     //     tc1= pi        //  starting from N pole
185     //   ELSE
186     //     tc1= 0         //  starting from S pole
187     //   ENDIF
188     // ENDIF
189     //
190     // For starting points other than the poles:
191     //
192     // IF sin(lon2-lon1)<0
193     //   tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
194     // ELSE
195     //   tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
196     // ENDIF
197
198     double tc1;
199
200     if ( cos(start.y()) < SG_EPSILON ) {
201         // EPS a small number ~ machine precision
202         if ( start.y() > 0 ) {
203             tc1 = SGD_PI;        // starting from N pole
204         } else {
205             tc1 = 0;            // starting from S pole
206         }
207     }
208
209     // For starting points other than the poles:
210
211     double tmp3 = sin(d)*cos(start.y());
212     double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
213     double tmp5 = acos(tmp4/tmp3);
214     if ( sin( dest.x() - start.x() ) < 0 ) {
215          tc1 = tmp5;
216     } else {
217          tc1 = SGD_2PI - tmp5;
218     }
219
220     *course = tc1;
221     *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
222 }
223 #endif // 0