1 // polar.cxx -- routines to deal with polar math and transformations
3 // Written by Curtis Olson, started June 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <simgear/constants.h>
29 #include "polar3d.hxx"
32 // Find the Altitude above the Ellipsoid (WGS84) given the Earth
33 // Centered Cartesian coordinate vector Distances are specified in
35 double fgGeodAltFromCart(const Point3D& cp)
37 double t_lat, x_alpha, mu_alpha;
38 double lat_geoc, radius;
41 lat_geoc = FG_PI_2 - atan2( sqrt(cp.x()*cp.x() + cp.y()*cp.y()), cp.z() );
42 radius = sqrt( cp.x()*cp.x() + cp.y()*cp.y() + cp.z()*cp.z() );
44 if( ( (FG_PI_2 - lat_geoc) < ONE_SECOND ) // near North pole
45 || ( (FG_PI_2 + lat_geoc) < ONE_SECOND ) ) // near South pole
47 result = radius - EQUATORIAL_RADIUS_M*E;
49 t_lat = tan(lat_geoc);
50 x_alpha = E*EQUATORIAL_RADIUS_M/sqrt(t_lat*t_lat + E*E);
51 mu_alpha = atan2(sqrt(RESQ_M - x_alpha*x_alpha),E*x_alpha);
53 mu_alpha = - mu_alpha;
55 result = (radius - x_alpha/cos(lat_geoc))*cos(mu_alpha - lat_geoc);