]> git.mxchange.org Git - simgear.git/blob - simgear/math/polar3d.cxx
Modified Files:
[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 General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #include <math.h>
28
29 #include <simgear/constants.h>
30
31 #include "polar3d.hxx"
32
33 /**
34  * Calculate new lon/lat given starting lon/lat, and offset radial, and
35  * distance.  NOTE: starting point is specifed in radians, distance is
36  * specified in meters (and converted internally to radians)
37  * ... assumes a spherical world.
38  * @param orig specified in polar coordinates
39  * @param course offset radial
40  * @param dist offset distance
41  * @return destination point in polar coordinates
42  */
43 Point3D calc_gc_lon_lat( const Point3D& orig, double course,
44                                 double dist ) {
45     Point3D result;
46
47     // lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
48     // IF (cos(lat)=0)
49     //   lon=lon1      // endpoint a pole
50     // ELSE
51     //   lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
52     // ENDIF
53
54     // printf("calc_lon_lat()  offset.theta = %.2f offset.dist = %.2f\n",
55     //        offset.theta, offset.dist);
56
57     dist *= SG_METER_TO_NM * SG_NM_TO_RAD;
58
59     result.sety( asin( sin(orig.y()) * cos(dist) +
60                        cos(orig.y()) * sin(dist) * cos(course) ) );
61
62     if ( cos(result.y()) < SG_EPSILON ) {
63         result.setx( orig.x() );      // endpoint a pole
64     } else {
65         result.setx(
66             fmod(orig.x() - asin( sin(course) * sin(dist) /
67                                   cos(result.y()) )
68                  + SGD_PI, SGD_2PI) - SGD_PI );
69     }
70
71     return result;
72 }
73
74 /**
75  * Calculate course/dist given two spherical points.
76  * @param start starting point
77  * @param dest ending point
78  * @param course resulting course
79  * @param dist resulting distance
80  */
81 void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
82                                  double *course, double *dist )
83 {
84     if ( start == dest) {
85             *dist=0;
86             *course=0;
87             return;
88     }
89     // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
90     //            cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
91     double cos_start_y = cos( start.y() );
92     double tmp1 = sin( (start.y() - dest.y()) * 0.5 );
93     double tmp2 = sin( (start.x() - dest.x()) * 0.5 );
94     double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
95                                  cos_start_y * cos(dest.y()) * tmp2 * tmp2));
96
97     *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
98
99 #if 1
100     double c1 = atan2(
101                 cos(dest.y())*sin(dest.x()-start.x()),
102                 cos(start.y())*sin(dest.y())-
103                 sin(start.y())*cos(dest.y())*cos(dest.x()-start.x()));
104     if (c1 >= 0)
105       *course = SGD_2PI-c1;
106     else
107       *course = -c1;
108 #else
109     // We obtain the initial course, tc1, (at point 1) from point 1 to
110     // point 2 by the following. The formula fails if the initial
111     // point is a pole. We can special case this with:
112     //
113     // IF (cos(lat1) < EPS)   // EPS a small number ~ machine precision
114     //   IF (lat1 > 0)
115     //     tc1= pi        //  starting from N pole
116     //   ELSE
117     //     tc1= 0         //  starting from S pole
118     //   ENDIF
119     // ENDIF
120     //
121     // For starting points other than the poles:
122     //
123     // IF sin(lon2-lon1)<0
124     //   tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
125     // ELSE
126     //   tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
127     // ENDIF
128
129     // if ( cos(start.y()) < SG_EPSILON ) {
130     // doing it this way saves a transcendental call
131     double sin_start_y = sin( start.y() );
132     if ( fabs(1.0-sin_start_y) < SG_EPSILON ) {
133         // EPS a small number ~ machine precision
134         if ( start.y() > 0 ) {
135             *course = SGD_PI;   // starting from N pole
136         } else {
137             *course = 0;        // starting from S pole
138         }
139     } else {
140         // For starting points other than the poles:
141         // double tmp3 = sin(d)*cos_start_y);
142         // double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
143         // double tmp5 = acos(tmp4/tmp3);
144
145         // Doing this way gaurentees that the temps are
146         // not stored into memory
147         double tmp5 = acos( (sin(dest.y()) - sin_start_y * cos(d)) /
148                             (sin(d) * cos_start_y) );
149
150         // if ( sin( dest.x() - start.x() ) < 0 ) {
151         // the sin of the negative angle is just the opposite sign
152         // of the sin of the angle  so tmp2 will have the opposite
153         // sign of sin( dest.x() - start.x() )
154         if ( tmp2 >= 0 ) {
155             *course = tmp5;
156         } else {
157             *course = SGD_2PI - tmp5;
158         }
159     }
160 #endif
161 }
162
163
164 #if 0
165 /**
166  * Calculate course/dist given two spherical points.
167  * @param start starting point
168  * @param dest ending point
169  * @param course resulting course
170  * @param dist resulting distance
171  */
172 void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
173                                  double *course, double *dist ) {
174     // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
175     //            cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
176     double tmp1 = sin( (start.y() - dest.y()) / 2 );
177     double tmp2 = sin( (start.x() - dest.x()) / 2 );
178     double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
179                                  cos(start.y()) * cos(dest.y()) * tmp2 * tmp2));
180     // We obtain the initial course, tc1, (at point 1) from point 1 to
181     // point 2 by the following. The formula fails if the initial
182     // point is a pole. We can special case this with:
183     //
184     // IF (cos(lat1) < EPS)   // EPS a small number ~ machine precision
185     //   IF (lat1 > 0)
186     //     tc1= pi        //  starting from N pole
187     //   ELSE
188     //     tc1= 0         //  starting from S pole
189     //   ENDIF
190     // ENDIF
191     //
192     // For starting points other than the poles:
193     //
194     // IF sin(lon2-lon1)<0
195     //   tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
196     // ELSE
197     //   tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
198     // ENDIF
199
200     double tc1;
201
202     if ( cos(start.y()) < SG_EPSILON ) {
203         // EPS a small number ~ machine precision
204         if ( start.y() > 0 ) {
205             tc1 = SGD_PI;        // starting from N pole
206         } else {
207             tc1 = 0;            // starting from S pole
208         }
209     }
210
211     // For starting points other than the poles:
212
213     double tmp3 = sin(d)*cos(start.y());
214     double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
215     double tmp5 = acos(tmp4/tmp3);
216     if ( sin( dest.x() - start.x() ) < 0 ) {
217          tc1 = tmp5;
218     } else {
219          tc1 = SGD_2PI - tmp5;
220     }
221
222     *course = tc1;
223     *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
224 }
225 #endif // 0