1 /*******************************************************************************
6 Purpose: Contains utility classes for the FG FDM
7 Called by: FGPosition, et. al.
9 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place - Suite 330, Boston, MA 02111-1307, USA.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class is a container for all utility classes used by the flight dynamics
34 --------------------------------------------------------------------------------
37 ********************************************************************************
39 *******************************************************************************/
41 /*******************************************************************************
43 *******************************************************************************/
45 #include "FGUtility.h"
47 #include "FGFDMExec.h"
50 /*******************************************************************************
51 ************************************ CODE **************************************
52 *******************************************************************************/
54 FGUtility::FGUtility()
56 // Move constant stuff to here (if any) so it won't take CPU time
57 // in the methods below.
58 SeaLevelR = EARTHRAD * ECCENT;
62 FGUtility::~FGUtility()
67 float FGUtility::ToGeodetic()
69 float GeodeticLat, Latitude, Radius, Altitude;
70 float tanLat, xAlpha, muAlpha, sinmuAlpha, denom, rhoAlpha, dMu;
71 float lPoint, lambdaSL, sinlambdaSL, dLambda, rAlpha;
73 Latitude = State->Getlatitude();
74 Radius = State->Geth() + EARTHRAD;
76 if (( M_PI_2 - Latitude < ONESECOND) ||
77 ( M_PI_2 + Latitude < ONESECOND)) { // Near a pole
78 GeodeticLat = Latitude;
79 Altitude = Radius - SeaLevelR;
81 tanLat = tan(Latitude);
82 xAlpha = ECCENT*EARTHRAD /
83 sqrt(tanLat*tanLat + ECCENTSQRD);
84 muAlpha = atan2(sqrt(EARTHRADSQRD - xAlpha*xAlpha), ECCENT*xAlpha);
86 if (Latitude < 0.0) muAlpha = -muAlpha;
88 sinmuAlpha = sin(muAlpha);
89 dLambda = muAlpha - Latitude;
90 rAlpha = xAlpha / cos(Latitude);
91 lPoint = Radius - rAlpha;
92 Altitude = lPoint*cos(dLambda);
93 denom = sqrt(1-EPS*EPS*sinmuAlpha*sinmuAlpha);
94 rhoAlpha = EARTHRAD*(1.0 - EPS) / (denom*denom*denom);
95 dMu = atan2(lPoint*sin(dLambda),rhoAlpha + Altitude);
96 State->SetGeodeticLat(muAlpha - dMu);
97 lambdaSL = atan(ECCENTSQRD*tan(GeodeticLat));
98 sinlambdaSL = sin(lambdaSL);
99 SeaLevelR = sqrt(EARTHRADSQRD / (1 + INVECCENTSQRDM1* sinlambdaSL*sinlambdaSL));
105 float FGUtility:: FromGeodetic()
107 float lambdaSL, sinlambdaSL, coslambdaSL, sinMu, cosMu, py, px;
108 float Altitude, SeaLevelR;
110 lambdaSL = atan(ECCENTSQRD*tan(State->GetGeodeticLat()));
111 sinlambdaSL = sin(lambdaSL);
112 coslambdaSL = cos(lambdaSL);
113 sinMu = sin(State->GetGeodeticLat());
114 cosMu = cos(State->GetGeodeticLat());
115 SeaLevelR = sqrt(EARTHRADSQRD /
116 (1 + INVECCENTSQRDM1*sinlambdaSL*sinlambdaSL));
117 px = SeaLevelR*coslambdaSL + Altitude*cosMu;
118 py = SeaLevelR*sinlambdaSL + Altitude*sinMu;
119 State->Setlatitude(atan2(py,px));