]> git.mxchange.org Git - simgear.git/blob - simgear/math/polar3d.cxx
Added simgear/magvar which impliments WMM 2000 world magnetic variance model.
[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  - curt@infoplane.com
6 //
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.
11 //
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <math.h>
25 #include <stdio.h>
26
27 #include <simgear/constants.h>
28
29 #include "polar3d.hxx"
30
31
32 // Find the Altitude above the Ellipsoid (WGS84) given the Earth
33 // Centered Cartesian coordinate vector Distances are specified in
34 // meters.
35 double fgGeodAltFromCart(const Point3D& cp)
36 {
37     double t_lat, x_alpha, mu_alpha;
38     double lat_geoc, radius;
39     double result;
40
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() );
43         
44     if( ( (FG_PI_2 - lat_geoc) < ONE_SECOND )        // near North pole
45         || ( (FG_PI_2 + lat_geoc) < ONE_SECOND ) )   // near South pole
46     {
47         result = radius - EQUATORIAL_RADIUS_M*E;
48     } else {
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);
52         if (lat_geoc < 0) {
53             mu_alpha = - mu_alpha;
54         }
55         result = (radius - x_alpha/cos(lat_geoc))*cos(mu_alpha - lat_geoc);
56     }
57
58     return(result);
59 }
60
61