]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCutils.cxx
warning fixes
[flightgear.git] / src / ATCDCL / ATCutils.cxx
1 // ATCutils.cxx - Utility functions for the ATC / AI system
2 //
3 // Written by David Luff, started March 2002.
4 //
5 // Copyright (C) 2002  David C Luff - david.luff@nottingham.ac.uk
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <sstream>
26
27 #include <simgear/math/SGMath.hxx>
28 #include <simgear/constants.h>
29 #include <simgear/misc/sg_path.hxx>
30 #include <simgear/debug/logstream.hxx>
31
32 #include <Airports/runways.hxx>
33 #include <Main/globals.hxx>
34
35 #include "ATCutils.hxx"
36 #include "ATCProjection.hxx"
37
38 static const string nums[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "niner"};
39
40 // Convert any number to spoken digits
41 string ConvertNumToSpokenDigits(const string &n) {
42         //cout << "n = " << n << endl;
43         static const string pt = "decimal";
44         string str = "";
45         
46         for(unsigned int i=0; i<n.length(); ++i) {
47                 //cout << "n.substr(" << i << ",1 = " << n.substr(i,1) << endl;
48                 if(n.substr(i,1) == " ") {
49                         // do nothing
50                 } else if(n.substr(i,1) == ".") {
51                         str += pt;
52                 } else {
53                         str += nums[atoi((n.substr(i,1)).c_str())];
54                 }
55                 if(i != (n.length()-1)) {       // ie. don't add a space at the end.
56                         str += " ";
57                 }
58         }
59         return(str);
60 }
61
62
63 // Convert an integer to spoken digits
64 string ConvertNumToSpokenDigits(int n) {
65         std::ostringstream buf;
66         buf << n;
67         return(ConvertNumToSpokenDigits(buf.str()));
68 }
69
70
71 // Convert a 2 digit rwy number to a spoken-style string
72 string ConvertRwyNumToSpokenString(int n) {
73         // Basic error/sanity checking
74         while(n < 0) {
75                 n += 36;
76         }
77         while(n > 36) {
78                 n -= 36;
79         }
80         if(n == 0) {
81                 n = 36; // Is this right?
82         }
83         
84         string str = "";
85         int index = n/10;
86         str += nums[index];
87         n -= (index * 10);
88         //str += "-";
89         str += " ";             //Changed this for the benefit of the voice token parser - prefer the "-" in the visual output though.
90         str += nums[n];
91         return(str);
92 }
93
94 // Assumes we get a two-digit string optionally appended with L, R or C
95 // eg 01 07L 29R 36
96 // Anything else is not guaranteed to be handled correctly!
97 string ConvertRwyNumToSpokenString(const string &s) {
98         if(s.size() < 3) {
99                 return(ConvertRwyNumToSpokenString(atoi(s.c_str())));
100         } else {
101                 string r = ConvertRwyNumToSpokenString(atoi(s.substr(0,2).c_str()));
102                 if(s.substr(2,1) == "L") {
103                         r += " left";
104                 } else if(s.substr(2,1) == "R") {
105                         r += " right";
106                 } else if(s.substr(2,1) == "C") {
107                         r += " center";
108                 } else {
109                         SG_LOG(SG_ATC, SG_WARN, "WARNING: Unknown suffix " << s.substr(2,1) << " from runway ID " << s << " in ConvertRwyNumToSpokenString(...)");
110                 }
111                 return(r);
112         }
113 }
114         
115
116 // Return the phonetic letter of a letter represented as an integer 1->26
117 string GetPhoneticIdent(int i) {
118         // TODO - Check i is between 1 and 26 and wrap if necessary
119         return(GetPhoneticIdent(char('a' + (i-1))));
120 }
121
122 // Return the phonetic letter of a character in the range a-z or A-Z.
123 // Currently always returns prefixed by lowercase.
124 string GetPhoneticIdent(char c) {
125         c = tolower(c);
126         // TODO - Check c is between a and z and wrap if necessary
127         switch(c) {
128         case 'a' : return("alpha");
129         case 'b' : return("bravo");
130         case 'c' : return("charlie");
131         case 'd' : return("delta");
132         case 'e' : return("echo");
133         case 'f' : return("foxtrot");
134         case 'g' : return("golf");
135         case 'h' : return("hotel");
136         case 'i' : return("india");
137         case 'j' : return("juliet");
138         case 'k' : return("kilo");
139         case 'l' : return("lima");
140         case 'm' : return("mike");
141         case 'n' : return("november");
142         case 'o' : return("oscar");
143         case 'p' : return("papa");
144         case 'q' : return("quebec");
145         case 'r' : return("romeo");
146         case 's' : return("sierra");
147         case 't' : return("tango");
148         case 'u' : return("uniform");
149         case 'v' : return("victor");
150         case 'w' : return("whiskey");
151         case 'x' : return("x-ray");
152         case 'y' : return("yankee");
153         case 'z' : return("zulu");
154         }
155         // We shouldn't get here
156         return("Error");
157 }
158
159 // Get the compass direction associated with a heading in degrees
160 // Currently returns 8 direction resolution (N, NE, E etc...)
161 // Might be modified in future to return 4, 8 or 16 resolution but defaulting to 8. 
162 string GetCompassDirection(double h) {
163         while(h < 0.0) h += 360.0;
164         while(h > 360.0) h -= 360.0;
165         if(h < 22.5 || h > 337.5) {
166                 return("North");
167         } else if(h < 67.5) {
168                 return("North-East");
169         } else if(h < 112.5) {
170                 return("East");
171         } else if(h < 157.5) {
172                 return("South-East");
173         } else if(h < 202.5) {
174                 return("South");
175         } else if(h < 247.5) {
176                 return("South-West");
177         } else if(h < 292.5) {
178                 return("West");
179         } else {
180                 return("North-West");
181         }
182 }
183
184 //================================================================================================================
185
186 // Given two positions (lat & lon in degrees), get the HORIZONTAL separation (in meters)
187 double dclGetHorizontalSeparation(const SGGeod& pos1, const SGGeod& pos2) {
188         double x;       //East-West separation
189         double y;       //North-South separation
190         double z;       //Horizontal separation - z = sqrt(x^2 + y^2)
191         
192         double lat1 = pos1.getLatitudeRad();
193         double lon1 = pos1.getLongitudeRad();
194         double lat2 = pos2.getLatitudeRad();
195         double lon2 = pos2.getLongitudeRad();
196         
197         y = sin(fabs(lat1 - lat2)) * SG_EQUATORIAL_RADIUS_M;
198         x = sin(fabs(lon1 - lon2)) * SG_EQUATORIAL_RADIUS_M * (cos((lat1 + lat2) / 2.0));
199         z = sqrt(x*x + y*y);
200         
201         return(z);
202 }
203
204 // Given a point and a line, get the HORIZONTAL shortest distance from the point to a point on the line.
205 // Expects to be fed orthogonal co-ordinates, NOT lat & lon !
206 // The units of the separation will be those of the input.
207 double dclGetLinePointSeparation(double px, double py, double x1, double y1, double x2, double y2) {
208         double vecx = x2-x1;
209         double vecy = y2-y1;
210         double magline = sqrt(vecx*vecx + vecy*vecy);
211         double u = ((px-x1)*(x2-x1) + (py-y1)*(y2-y1)) / (magline * magline);
212         double x0 = x1 + u*(x2-x1);
213         double y0 = y1 + u*(y2-y1);
214         vecx = px - x0;
215         vecy = py - y0;
216         double d = sqrt(vecx*vecx + vecy*vecy);
217         if(d < 0) {
218                 d *= -1;
219         }
220         return(d);
221 }
222
223 // Given a position (lat/lon/elev), heading and vertical angle (degrees), and distance (meters), calculate the new position.
224 // This function assumes the world is spherical.  If geodetic accuracy is required use the functions is sg_geodesy instead!
225 // Assumes that the ground is not hit!!!  Expects heading and angle in degrees, distance in meters. 
226 SGGeod dclUpdatePosition(const SGGeod& pos, double heading, double angle, double distance) {
227     // FIXME: use SGGeodesy instead ...
228
229         //cout << setprecision(10) << pos.lon() << ' ' << pos.lat() << '\n';
230         heading *= DCL_DEGREES_TO_RADIANS;
231         angle *= DCL_DEGREES_TO_RADIANS;
232         double lat = pos.getLatitudeRad();
233         double lon = pos.getLongitudeRad();
234         double elev = pos.getElevationM();
235         //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
236         
237         double horiz_dist = distance * cos(angle);
238         double vert_dist = distance * sin(angle);
239         
240         double north_dist = horiz_dist * cos(heading);
241         double east_dist = horiz_dist * sin(heading);
242         
243         //cout << distance << ' ' << horiz_dist << ' ' << vert_dist << ' ' << north_dist << ' ' << east_dist << '\n';
244         
245         double delta_lat = asin(north_dist / (double)SG_EQUATORIAL_RADIUS_M);
246         double delta_lon = asin(east_dist / (double)SG_EQUATORIAL_RADIUS_M) * (1.0 / cos(lat));  // I suppose really we should use the average of the original and new lat but we'll assume that this will be good enough.
247         //cout << delta_lon*DCL_RADIANS_TO_DEGREES << ' ' << delta_lat*DCL_RADIANS_TO_DEGREES << '\n';
248         lat += delta_lat;
249         lon += delta_lon;
250         elev += vert_dist;
251         //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
252         
253         //cout << setprecision(15) << DCL_DEGREES_TO_RADIANS * DCL_RADIANS_TO_DEGREES << '\n';
254         
255         return SGGeod::fromRadM(lon, lat, elev);
256 }
257
258 // Get a heading in degrees from one lat/lon to another.
259 // This function assumes the world is spherical.  If geodetic accuracy is required use the functions is sg_geodesy instead!
260 // Warning - at the moment we are not checking for identical points - currently it returns 0 in this instance.
261 double GetHeadingFromTo(const SGGeod& A, const SGGeod& B) {
262         double latA = A.getLatitudeRad();
263         double lonA = A.getLongitudeRad();
264         double latB = B.getLatitudeRad();
265         double lonB = B.getLongitudeRad();
266         double xdist = sin(lonB - lonA) * (double)SG_EQUATORIAL_RADIUS_M * cos((latA+latB)/2.0);
267         double ydist = sin(latB - latA) * (double)SG_EQUATORIAL_RADIUS_M;
268         double heading = atan2(xdist, ydist) * DCL_RADIANS_TO_DEGREES;
269         return heading < 0.0 ? heading + 360 : heading;
270 }
271
272 // Given a heading (in degrees), bound it from 0 -> 360
273 void dclBoundHeading(double &hdg) {
274         while(hdg < 0.0) {
275                 hdg += 360.0;
276         }
277         while(hdg > 360.0) {
278                 hdg -= 360.0;
279         }
280 }
281
282 // smallest difference between two angles in degrees
283 // difference is negative if a1 > a2 and positive if a2 > a1
284 double GetAngleDiff_deg( const double &a1, const double &a2) {
285   
286   double a3 = a2 - a1;
287   while (a3 < 180.0) a3 += 360.0;
288   while (a3 > 180.0) a3 -= 360.0;
289
290   return a3;
291 }
292
293 // Runway stuff
294 // Given (lon/lat/elev) and an FGRunway struct, determine if the point lies on the runway
295 bool OnRunway(const SGGeod& pt, const FGRunwayBase* rwy) {
296         FGATCAlignedProjection ortho;
297         SGGeod centre = SGGeod::fromDegM(rwy->longitude(), rwy->latitude(), 0); // We don't need the elev
298         ortho.Init(centre, rwy->headingDeg());
299         
300         SGVec3d xyc = ortho.ConvertToLocal(centre);
301         SGVec3d xyp = ortho.ConvertToLocal(pt);
302         
303         //cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
304         //cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
305         
306         if((fabs(xyp.y() - xyc.y()) < ((rwy->lengthFt()/2.0) + 5.0)) 
307                 && (fabs(xyp.x() - xyc.x()) < (rwy->widthFt()/2.0))) {
308                 return(true);
309         }
310         
311         return(false);
312 }
313