1 // ATCutils.cxx - Utility functions for the ATC / AI system
3 // Written by David Luff, started March 2002.
5 // Copyright (C) 2002 David C Luff - david.luff@nottingham.ac.uk
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <simgear/math/SGMath.hxx>
28 #include <simgear/constants.h>
29 #include <simgear/misc/sg_path.hxx>
30 #include <simgear/debug/logstream.hxx>
32 #include <Airports/runways.hxx>
33 #include <Main/globals.hxx>
35 #include "ATCutils.hxx"
36 #include "ATCProjection.hxx"
38 static const string nums[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "niner"};
40 // Convert any number to spoken digits
41 string ConvertNumToSpokenDigits(const string &n) {
42 //cout << "n = " << n << endl;
43 static const string pt = "decimal";
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) == " ") {
50 } else if(n.substr(i,1) == ".") {
53 str += nums[atoi((n.substr(i,1)).c_str())];
55 if(i != (n.length()-1)) { // ie. don't add a space at the end.
63 // Convert an integer to spoken digits
64 string ConvertNumToSpokenDigits(int n) {
65 std::ostringstream buf;
67 return(ConvertNumToSpokenDigits(buf.str()));
71 // Convert a 2 digit rwy number to a spoken-style string
72 string ConvertRwyNumToSpokenString(int n) {
73 // Basic error/sanity checking
81 n = 36; // Is this right?
89 str += " "; //Changed this for the benefit of the voice token parser - prefer the "-" in the visual output though.
94 // Assumes we get a two-digit string optionally appended with L, R or C
96 // Anything else is not guaranteed to be handled correctly!
97 string ConvertRwyNumToSpokenString(const string &s) {
99 return(ConvertRwyNumToSpokenString(atoi(s.c_str())));
101 string r = ConvertRwyNumToSpokenString(atoi(s.substr(0,2).c_str()));
102 if(s.substr(2,1) == "L") {
104 } else if(s.substr(2,1) == "R") {
106 } else if(s.substr(2,1) == "C") {
109 SG_LOG(SG_ATC, SG_WARN, "WARNING: Unknown suffix " << s.substr(2,1) << " from runway ID " << s << " in ConvertRwyNumToSpokenString(...)");
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))));
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) {
126 // TODO - Check c is between a and z and wrap if necessary
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");
155 // We shouldn't get here
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) {
167 } else if(h < 67.5) {
168 return("North-East");
169 } else if(h < 112.5) {
171 } else if(h < 157.5) {
172 return("South-East");
173 } else if(h < 202.5) {
175 } else if(h < 247.5) {
176 return("South-West");
177 } else if(h < 292.5) {
180 return("North-West");
184 //================================================================================================================
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)
192 double lat1 = pos1.getLatitudeRad();
193 double lon1 = pos1.getLongitudeRad();
194 double lat2 = pos2.getLatitudeRad();
195 double lon2 = pos2.getLongitudeRad();
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));
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) {
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);
216 double d = sqrt(vecx*vecx + vecy*vecy);
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 ...
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';
237 double horiz_dist = distance * cos(angle);
238 double vert_dist = distance * sin(angle);
240 double north_dist = horiz_dist * cos(heading);
241 double east_dist = horiz_dist * sin(heading);
243 //cout << distance << ' ' << horiz_dist << ' ' << vert_dist << ' ' << north_dist << ' ' << east_dist << '\n';
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';
251 //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
253 //cout << setprecision(15) << DCL_DEGREES_TO_RADIANS * DCL_RADIANS_TO_DEGREES << '\n';
255 return SGGeod::fromRadM(lon, lat, elev);
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;
272 // Given a heading (in degrees), bound it from 0 -> 360
273 void dclBoundHeading(double &hdg) {
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) {
287 while (a3 < 180.0) a3 += 360.0;
288 while (a3 > 180.0) a3 -= 360.0;
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());
300 SGVec3d xyc = ortho.ConvertToLocal(centre);
301 SGVec3d xyp = ortho.ConvertToLocal(pt);
303 //cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
304 //cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
306 if((fabs(xyp.y() - xyc.y()) < ((rwy->lengthFt()/2.0) + 5.0))
307 && (fabs(xyp.x() - xyc.x()) < (rwy->widthFt()/2.0))) {