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 *******************************************************************************/
46 # include <Include/compiler.h>
47 # ifdef FG_HAVE_STD_INCLUDES
56 #include "FGUtility.h"
58 #include "FGFDMExec.h"
61 /* get a definition for pi */
62 #include <Include/fg_constants.h>
63 #define M_PI_2 FG_PI_2
66 /*******************************************************************************
67 ************************************ CODE **************************************
68 *******************************************************************************/
70 FGUtility::FGUtility()
72 // Move constant stuff to here (if any) so it won't take CPU time
73 // in the methods below.
74 SeaLevelR = EARTHRAD * ECCENT;
78 FGUtility::~FGUtility()
83 float FGUtility::ToGeodetic()
85 float Latitude, Radius, Altitude;
86 float tanLat, xAlpha, muAlpha, sinmuAlpha, denom, rhoAlpha, dMu;
87 float lPoint, lambdaSL, sinlambdaSL, dLambda, rAlpha;
89 Latitude = State->Getlatitude();
90 Radius = State->Geth() + EARTHRAD;
92 if (( M_PI_2 - Latitude < ONESECOND) ||
93 ( M_PI_2 + Latitude < ONESECOND)) { // Near a pole
95 tanLat = tan(Latitude);
96 xAlpha = ECCENT*EARTHRAD /
97 sqrt(tanLat*tanLat + ECCENTSQRD);
98 muAlpha = atan2(sqrt(EARTHRADSQRD - xAlpha*xAlpha), ECCENT*xAlpha);
100 if (Latitude < 0.0) muAlpha = -muAlpha;
102 sinmuAlpha = sin(muAlpha);
103 dLambda = muAlpha - Latitude;
104 rAlpha = xAlpha / cos(Latitude);
105 lPoint = Radius - rAlpha;
106 Altitude = lPoint*cos(dLambda);
107 denom = sqrt(1-EPS*EPS*sinmuAlpha*sinmuAlpha);
108 rhoAlpha = EARTHRAD*(1.0 - EPS) / (denom*denom*denom);
109 dMu = atan2(lPoint*sin(dLambda),rhoAlpha + Altitude);
110 State->SetGeodeticLat(muAlpha - dMu);
111 lambdaSL = atan(ECCENTSQRD*tan(muAlpha - dMu));
112 sinlambdaSL = sin(lambdaSL);
113 SeaLevelR = sqrt(EARTHRADSQRD / (1 + INVECCENTSQRDM1* sinlambdaSL*sinlambdaSL));
119 float FGUtility:: FromGeodetic()
121 float lambdaSL, sinlambdaSL, coslambdaSL, sinMu, cosMu, py, px;
122 float Altitude, SeaLevelR, Radius;
124 Radius = State->Geth() + EARTHRAD;
125 lambdaSL = atan(ECCENTSQRD*tan(State->GetGeodeticLat()));
126 sinlambdaSL = sin(lambdaSL);
127 coslambdaSL = cos(lambdaSL);
128 sinMu = sin(State->GetGeodeticLat());
129 cosMu = cos(State->GetGeodeticLat());
130 SeaLevelR = sqrt(EARTHRADSQRD /
131 (1 + INVECCENTSQRDM1*sinlambdaSL*sinlambdaSL));
132 Altitude = Radius - SeaLevelR;
133 px = SeaLevelR*coslambdaSL + Altitude*cosMu;
134 py = SeaLevelR*sinlambdaSL + Altitude*sinMu;
135 State->Setlatitude(atan2(py,px));