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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <simgear/math/point3d.hxx>
23 #include <simgear/constants.h>
24 #include <simgear/misc/sg_path.hxx>
25 #include <simgear/debug/logstream.hxx>
27 //#include <iomanip.h>
29 #include <Airports/runways.hxx>
30 #include <Main/globals.hxx>
32 #include "ATCutils.hxx"
33 #include "ATCProjection.hxx"
35 // Convert any number to spoken digits
36 string ConvertNumToSpokenDigits(string n) {
37 //cout << "n = " << n << endl;
38 string nums[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
39 string pt = "decimal";
42 for(unsigned int i=0; i<n.length(); ++i) {
43 //cout << "n.substr(" << i << ",1 = " << n.substr(i,1) << endl;
44 if(n.substr(i,1) == " ") {
46 } else if(n.substr(i,1) == ".") {
49 str += nums[atoi((n.substr(i,1)).c_str())];
51 if(i != (n.length()-1)) { // ie. don't add a space at the end.
59 // Convert a 2 digit rwy number to a spoken-style string
60 string ConvertRwyNumToSpokenString(int n) {
61 string nums[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
62 // Basic error/sanity checking
70 n = 36; // Is this right?
78 str += " "; //Changed this for the benefit of the voice token parser - prefer the "-" in the visual output though.
83 // Assumes we get a two-digit string optionally appended with L, R or C
85 // Anything else is not guaranteed to be handled correctly!
86 string ConvertRwyNumToSpokenString(string s) {
88 return(ConvertRwyNumToSpokenString(atoi(s.c_str())));
90 string r = ConvertRwyNumToSpokenString(atoi(s.substr(0,2).c_str()));
91 if(s.substr(2,1) == "L") {
93 } else if(s.substr(2,1) == "R") {
95 } else if(s.substr(2,1) == "C") {
98 SG_LOG(SG_ATC, SG_WARN, "WARNING: Unknown suffix " << s.substr(2,1) << " from runway ID " << s << " in ConvertRwyNumToSpokenString(...)");
105 // Return the phonetic letter of a letter represented as an integer 1->26
106 string GetPhoneticIdent(int i) {
107 // TODO - Check i is between 1 and 26 and wrap if necessary
109 case 1 : return("alpha");
110 case 2 : return("bravo");
111 case 3 : return("charlie");
112 case 4 : return("delta");
113 case 5 : return("echo");
114 case 6 : return("foxtrot");
115 case 7 : return("golf");
116 case 8 : return("hotel");
117 case 9 : return("india");
118 case 10 : return("juliet");
119 case 11 : return("kilo");
120 case 12 : return("lima");
121 case 13 : return("mike");
122 case 14 : return("november");
123 case 15 : return("oscar");
124 case 16 : return("papa");
125 case 17 : return("quebec");
126 case 18 : return("romeo");
127 case 19 : return("sierra");
128 case 20 : return("tango");
129 case 21 : return("uniform");
130 case 22 : return("victor");
131 case 23 : return("whiskey");
132 case 24 : return("x-ray");
133 case 25 : return("yankee");
134 case 26 : return("zulu");
136 // We shouldn't get here
140 //================================================================================================================
142 // Given two positions (lat & lon in degrees), get the HORIZONTAL separation (in meters)
143 double dclGetHorizontalSeparation(Point3D pos1, Point3D pos2) {
144 double x; //East-West separation
145 double y; //North-South separation
146 double z; //Horizontal separation - z = sqrt(x^2 + y^2)
148 double lat1 = pos1.lat() * SG_DEGREES_TO_RADIANS;
149 double lon1 = pos1.lon() * SG_DEGREES_TO_RADIANS;
150 double lat2 = pos2.lat() * SG_DEGREES_TO_RADIANS;
151 double lon2 = pos2.lon() * SG_DEGREES_TO_RADIANS;
153 y = sin(fabs(lat1 - lat2)) * SG_EQUATORIAL_RADIUS_M;
154 x = sin(fabs(lon1 - lon2)) * SG_EQUATORIAL_RADIUS_M * (cos((lat1 + lat2) / 2.0));
160 // Given a point and a line, get the HORIZONTAL shortest distance from the point to a point on the line.
161 // Expects to be fed orthogonal co-ordinates, NOT lat & lon !
162 // The units of the separation will be those of the input.
163 double dclGetLinePointSeparation(double px, double py, double x1, double y1, double x2, double y2) {
166 double magline = sqrt(vecx*vecx + vecy*vecy);
167 double u = ((px-x1)*(x2-x1) + (py-y1)*(y2-y1)) / (magline * magline);
168 double x0 = x1 + u*(x2-x1);
169 double y0 = y1 + u*(y2-y1);
172 double d = sqrt(vecx*vecx + vecy*vecy);
179 // Given a position (lat/lon/elev), heading and vertical angle (degrees), and distance (meters), calculate the new position.
180 // This function assumes the world is spherical. If geodetic accuracy is required use the functions is sg_geodesy instead!
181 // Assumes that the ground is not hit!!! Expects heading and angle in degrees, distance in meters.
182 Point3D dclUpdatePosition(Point3D pos, double heading, double angle, double distance) {
183 //cout << setprecision(10) << pos.lon() << ' ' << pos.lat() << '\n';
184 heading *= DCL_DEGREES_TO_RADIANS;
185 angle *= DCL_DEGREES_TO_RADIANS;
186 double lat = pos.lat() * DCL_DEGREES_TO_RADIANS;
187 double lon = pos.lon() * DCL_DEGREES_TO_RADIANS;
188 double elev = pos.elev();
189 //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
191 double horiz_dist = distance * cos(angle);
192 double vert_dist = distance * sin(angle);
194 double north_dist = horiz_dist * cos(heading);
195 double east_dist = horiz_dist * sin(heading);
197 //cout << distance << ' ' << horiz_dist << ' ' << vert_dist << ' ' << north_dist << ' ' << east_dist << '\n';
199 double delta_lat = asin(north_dist / (double)SG_EQUATORIAL_RADIUS_M);
200 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.
201 //cout << delta_lon*DCL_RADIANS_TO_DEGREES << ' ' << delta_lat*DCL_RADIANS_TO_DEGREES << '\n';
205 //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
207 //cout << setprecision(15) << DCL_DEGREES_TO_RADIANS * DCL_RADIANS_TO_DEGREES << '\n';
209 return(Point3D(lon*DCL_RADIANS_TO_DEGREES, lat*DCL_RADIANS_TO_DEGREES, elev));
212 // Get a heading in degrees from one lat/lon to another.
213 // This function assumes the world is spherical. If geodetic accuracy is required use the functions is sg_geodesy instead!
214 // Warning - at the moment we are not checking for identical points - currently it returns 90 in this instance.
215 double GetHeadingFromTo(Point3D A, Point3D B) {
216 double latA = A.lat() * DCL_DEGREES_TO_RADIANS;
217 double lonA = A.lon() * DCL_DEGREES_TO_RADIANS;
218 double latB = B.lat() * DCL_DEGREES_TO_RADIANS;
219 double lonB = B.lon() * DCL_DEGREES_TO_RADIANS;
220 double xdist = sin(lonB - lonA) * (double)SG_EQUATORIAL_RADIUS_M * cos((latA+latB)/2.0);
221 double ydist = sin(latB - latA) * (double)SG_EQUATORIAL_RADIUS_M;
225 return(atan(xdist/ydist) * DCL_RADIANS_TO_DEGREES);
226 } else if (ydist == 0) {
229 return(180.0 - atan(xdist/fabs(ydist)) * DCL_RADIANS_TO_DEGREES);
233 return(360.0 - atan(fabs(xdist)/ydist) * DCL_RADIANS_TO_DEGREES);
234 } else if (ydist == 0) {
237 return(180.0 + atan(xdist/ydist) * DCL_RADIANS_TO_DEGREES);
242 // Given a heading (in degrees), bound it from 0 -> 360
243 void dclBoundHeading(double &hdg) {
252 // smallest difference between two angles in degrees
253 // difference is negative if a1 > a2 and positive if a2 > a1
254 double GetAngleDiff_deg( const double &a1, const double &a2) {
257 while (a3 < 180.0) a3 += 360.0;
258 while (a3 > 180.0) a3 -= 360.0;
263 //================================================================================================================
265 // Airport stuff. The next two functions are straight copies of their fg.... equivalents
266 // in fg_init.cxx, and are just here temporarily until some rationalisation occurs.
267 // find basic airport location info from airport database
268 bool dclFindAirportID( const string& id, FGAirport *a ) {
270 SGPath path( globals->get_fg_root() );
271 path.append( "Airports" );
272 path.append( "simple.mk4" );
273 FGAirports airports( path.c_str() );
275 SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
277 if ( ! airports.search( id, a ) ) {
278 SG_LOG( SG_GENERAL, SG_ALERT,
279 "Failed to find " << id << " in " << path.str() );
286 SG_LOG( SG_GENERAL, SG_INFO,
287 "Position for " << id << " is ("
288 << a->longitude << ", "
289 << a->latitude << ")" );
294 // get airport elevation
295 double dclGetAirportElev( const string& id ) {
299 SG_LOG( SG_GENERAL, SG_INFO,
300 "Finding elevation for airport: " << id );
302 if ( dclFindAirportID( id, &a ) ) {
310 // Given a Point3D (lon/lat/elev) and an FGRunway struct, determine if the point lies on the runway
311 bool OnRunway(Point3D pt, const FGRunway& rwy) {
312 FGATCAlignedProjection ortho;
313 Point3D centre(rwy.lon, rwy.lat, 0.0); // We don't need the elev
314 ortho.Init(centre, rwy.heading);
316 Point3D xyc = ortho.ConvertToLocal(centre);
317 Point3D xyp = ortho.ConvertToLocal(pt);
319 //cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
320 //cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
322 if((fabs(xyp.y() - xyc.y()) < ((rwy.length/2.0) + 5.0))
323 && (fabs(xyp.x() - xyc.x()) < (rwy.width/2.0))) {