3 * Routines to deal with polar math and transformations.
6 // Written by Curtis Olson, started June 1997.
8 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Library General Public License for more details.
20 // You should have received a copy of the GNU Library General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA 02111-1307, USA.
33 # error This library requires C++
39 #include <simgear/constants.h>
40 #include <simgear/math/point3d.hxx>
44 * Find the Altitude above the Ellipsoid (WGS84) given the Earth
45 * Centered Cartesian coordinate vector Distances are specified in
47 * @param cp point specified in cartesian coordinates
48 * @return altitude above the (wgs84) earth in meters
50 double sgGeodAltFromCart(const Point3D& cp);
54 * Convert a polar coordinate to a cartesian coordinate. Lon and Lat
55 * must be specified in radians. The SG convention is for distances
56 * to be specified in meters
57 * @param p point specified in polar coordinates
58 * @return the same point in cartesian coordinates
60 inline Point3D sgPolarToCart3d(const Point3D& p) {
61 double tmp = cos( p.lat() ) * p.radius();
63 return Point3D( cos( p.lon() ) * tmp,
65 sin( p.lat() ) * p.radius() );
70 * Convert a cartesian coordinate to polar coordinates (lon/lat
71 * specified in radians. Distances are specified in meters.
72 * @param cp point specified in cartesian coordinates
73 * @return the same point in polar coordinates
75 inline Point3D sgCartToPolar3d(const Point3D& cp) {
76 return Point3D( atan2( cp.y(), cp.x() ),
78 atan2( sqrt(cp.x()*cp.x() + cp.y()*cp.y()), cp.z() ),
79 sqrt(cp.x()*cp.x() + cp.y()*cp.y() + cp.z()*cp.z()) );
84 * Calculate new lon/lat given starting lon/lat, and offset radial, and
85 * distance. NOTE: starting point is specifed in radians, distance is
86 * specified in meters (and converted internally to radians)
87 * ... assumes a spherical world.
88 * @param orig specified in polar coordinates
89 * @param course offset radial
90 * @param dist offset distance
91 * @return destination point in polar coordinates
93 inline Point3D calc_gc_lon_lat( const Point3D& orig, double course,
97 // lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
99 // lon=lon1 // endpoint a pole
101 // lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
104 // printf("calc_lon_lat() offset.theta = %.2f offset.dist = %.2f\n",
105 // offset.theta, offset.dist);
107 dist *= SG_METER_TO_NM * SG_NM_TO_RAD;
109 result.sety( asin( sin(orig.y()) * cos(dist) +
110 cos(orig.y()) * sin(dist) * cos(course) ) );
112 if ( cos(result.y()) < SG_EPSILON ) {
113 result.setx( orig.x() ); // endpoint a pole
116 fmod(orig.x() - asin( sin(course) * sin(dist) /
118 + SGD_PI, SGD_2PI) - SGD_PI );
126 * Calculate course/dist given two spherical points.
127 * @param start starting point
128 * @param dest ending point
129 * @param course resulting course
130 * @param dist resulting distance
132 inline void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
133 double *course, double *dist ) {
134 // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
135 // cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
136 double tmp1 = sin( (start.y() - dest.y()) / 2 );
137 double tmp2 = sin( (start.x() - dest.x()) / 2 );
138 double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
139 cos(start.y()) * cos(dest.y()) * tmp2 * tmp2));
141 // We obtain the initial course, tc1, (at point 1) from point 1 to
142 // point 2 by the following. The formula fails if the initial
143 // point is a pole. We can special case this with:
145 // IF (cos(lat1) < EPS) // EPS a small number ~ machine precision
147 // tc1= pi // starting from N pole
149 // tc1= 0 // starting from S pole
153 // For starting points other than the poles:
155 // IF sin(lon2-lon1)<0
156 // tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
158 // tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
163 if ( cos(start.y()) < SG_EPSILON ) {
164 // EPS a small number ~ machine precision
165 if ( start.y() > 0 ) {
166 tc1 = SGD_PI; // starting from N pole
168 tc1 = 0; // starting from S pole
172 // For starting points other than the poles:
174 double tmp3 = sin(d)*cos(start.y());
175 double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
176 double tmp5 = acos(tmp4/tmp3);
177 if ( sin( dest.x() - start.x() ) < 0 ) {
180 tc1 = 2 * SGD_PI - tmp5;
184 *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
187 #endif // _POLAR3D_HXX