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.
28 #include <simgear/math/SGMath.hxx>
29 #include <simgear/constants.h>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/debug/logstream.hxx>
33 #include <Airports/runways.hxx>
34 #include <Main/globals.hxx>
36 #include "ATCutils.hxx"
37 #include "ATCProjection.hxx"
39 static const string nums[10] = {"zero", "one", "two", "three", "four",
40 "five", "six", "seven", "eight", "niner"};
42 static const string letters[LTRS] = {
43 "alpha", "bravo", "charlie", "delta", "echo",
44 "foxtrot", "golf", "hotel", "india", "juliet",
45 "kilo", "lima", "mike", "november", "oscar",
46 "papa", "quebec", "romeo", "sierra", "tango",
47 "uniform", "victor", "whiskey", "xray", "yankee", "zulu"
50 // Convert any number to spoken digits
51 string ConvertNumToSpokenDigits(const string &n) {
52 //cout << "n = " << n << endl;
53 static const string pt = "decimal";
56 for(unsigned int i=0; i<n.length(); ++i) {
57 //cout << "n.substr(" << i << ",1 = " << n.substr(i,1) << endl;
58 if(n.substr(i,1) == " ") {
60 } else if(n.substr(i,1) == ".") {
63 str += nums[atoi((n.substr(i,1)).c_str())];
65 if(i != (n.length()-1)) { // ie. don't add a space at the end.
72 // Convert an integer to a decimal numeral string
73 string decimalNumeral(const int& n) {
74 std::ostringstream buf;
79 // Convert an integer to spoken digits
80 string ConvertNumToSpokenDigits(const int& n) {
81 return ConvertNumToSpokenDigits(decimalNumeral(n));
85 // Assumes we get a string of digits optionally appended with L, R or C
87 // Anything else is not guaranteed to be handled correctly!
88 string ConvertRwyNumToSpokenString(const string &rwy) {
90 for (size_t ii = 0; ii < rwy.length(); ii++){
91 if (rslt.length()) rslt += " ";
92 string ch = rwy.substr(ii,1);
93 if (isdigit(ch[0])) rslt += ConvertNumToSpokenDigits(atoi(ch.c_str()));
94 else if (ch == "R") rslt += "right";
95 else if (ch == "C") rslt += "center";
96 else if (ch == "L") rslt += "left";
98 rslt += GetPhoneticLetter(ch[0]);
99 SG_LOG(SG_ATC, SG_WARN, "WARNING: Unknown suffix '" << ch
100 << "' in runway " << rwy << " in ConvertRwyNumToSpokenString(...)");
107 // Return the phonetic letter of a letter represented as an integer 1->26
108 string GetPhoneticLetter(const int i) {
109 return(letters[i % LTRS]);
112 // Return the phonetic letter of a character in the range a-z or A-Z.
113 // Currently always returns prefixed by lowercase.
114 string GetPhoneticLetter(const char c) {
115 return GetPhoneticLetter(int(tolower(c) - 'a'));
118 // Get the compass direction associated with a heading in degrees
119 // Currently returns 8 direction resolution (N, NE, E etc...)
120 // Might be modified in future to return 4, 8 or 16 resolution but defaulting to 8.
121 string GetCompassDirection(double h) {
122 while(h < 0.0) h += 360.0;
123 while(h > 360.0) h -= 360.0;
124 if(h < 22.5 || h > 337.5) {
126 } else if(h < 67.5) {
127 return("North-East");
128 } else if(h < 112.5) {
130 } else if(h < 157.5) {
131 return("South-East");
132 } else if(h < 202.5) {
134 } else if(h < 247.5) {
135 return("South-West");
136 } else if(h < 292.5) {
139 return("North-West");
143 //================================================================================================================
145 // Given two positions (lat & lon in degrees), get the HORIZONTAL separation (in meters)
146 double dclGetHorizontalSeparation(const SGGeod& pos1, const SGGeod& pos2) {
147 double x; //East-West separation
148 double y; //North-South separation
149 double z; //Horizontal separation - z = sqrt(x^2 + y^2)
151 double lat1 = pos1.getLatitudeRad();
152 double lon1 = pos1.getLongitudeRad();
153 double lat2 = pos2.getLatitudeRad();
154 double lon2 = pos2.getLongitudeRad();
156 y = sin(fabs(lat1 - lat2)) * SG_EQUATORIAL_RADIUS_M;
157 x = sin(fabs(lon1 - lon2)) * SG_EQUATORIAL_RADIUS_M * (cos((lat1 + lat2) / 2.0));
163 // Given a point and a line, get the HORIZONTAL shortest distance from the point to a point on the line.
164 // Expects to be fed orthogonal co-ordinates, NOT lat & lon !
165 // The units of the separation will be those of the input.
166 double dclGetLinePointSeparation(double px, double py, double x1, double y1, double x2, double y2) {
169 double magline = sqrt(vecx*vecx + vecy*vecy);
170 double u = ((px-x1)*(x2-x1) + (py-y1)*(y2-y1)) / (magline * magline);
171 double x0 = x1 + u*(x2-x1);
172 double y0 = y1 + u*(y2-y1);
175 double d = sqrt(vecx*vecx + vecy*vecy);
182 // Given a position (lat/lon/elev), heading and vertical angle (degrees), and distance (meters), calculate the new position.
183 // This function assumes the world is spherical. If geodetic accuracy is required use the functions is sg_geodesy instead!
184 // Assumes that the ground is not hit!!! Expects heading and angle in degrees, distance in meters.
185 SGGeod dclUpdatePosition(const SGGeod& pos, double heading, double angle, double distance) {
186 // FIXME: use SGGeodesy instead ...
188 //cout << setprecision(10) << pos.lon() << ' ' << pos.lat() << '\n';
189 heading *= DCL_DEGREES_TO_RADIANS;
190 angle *= DCL_DEGREES_TO_RADIANS;
191 double lat = pos.getLatitudeRad();
192 double lon = pos.getLongitudeRad();
193 double elev = pos.getElevationM();
194 //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
196 double horiz_dist = distance * cos(angle);
197 double vert_dist = distance * sin(angle);
199 double north_dist = horiz_dist * cos(heading);
200 double east_dist = horiz_dist * sin(heading);
202 //cout << distance << ' ' << horiz_dist << ' ' << vert_dist << ' ' << north_dist << ' ' << east_dist << '\n';
204 double delta_lat = asin(north_dist / (double)SG_EQUATORIAL_RADIUS_M);
205 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.
206 //cout << delta_lon*DCL_RADIANS_TO_DEGREES << ' ' << delta_lat*DCL_RADIANS_TO_DEGREES << '\n';
210 //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
212 //cout << setprecision(15) << DCL_DEGREES_TO_RADIANS * DCL_RADIANS_TO_DEGREES << '\n';
214 return SGGeod::fromRadM(lon, lat, elev);
217 // Get a heading in degrees from one lat/lon to another.
218 // This function assumes the world is spherical. If geodetic accuracy is required use the functions is sg_geodesy instead!
219 // Warning - at the moment we are not checking for identical points - currently it returns 0 in this instance.
220 double GetHeadingFromTo(const SGGeod& A, const SGGeod& B) {
221 double latA = A.getLatitudeRad();
222 double lonA = A.getLongitudeRad();
223 double latB = B.getLatitudeRad();
224 double lonB = B.getLongitudeRad();
225 double xdist = sin(lonB - lonA) * (double)SG_EQUATORIAL_RADIUS_M * cos((latA+latB)/2.0);
226 double ydist = sin(latB - latA) * (double)SG_EQUATORIAL_RADIUS_M;
227 double heading = atan2(xdist, ydist) * DCL_RADIANS_TO_DEGREES;
228 return heading < 0.0 ? heading + 360 : heading;
231 // Given a heading (in degrees), bound it from 0 -> 360
232 void dclBoundHeading(double &hdg) {
241 // smallest difference between two angles in degrees
242 // difference is negative if a1 > a2 and positive if a2 > a1
243 double GetAngleDiff_deg( const double &a1, const double &a2) {
246 while (a3 < 180.0) a3 += 360.0;
247 while (a3 > 180.0) a3 -= 360.0;
253 // Given (lon/lat/elev) and an FGRunway struct, determine if the point lies on the runway
254 bool OnRunway(const SGGeod& pt, const FGRunwayBase* rwy) {
255 FGATCAlignedProjection ortho;
256 SGGeod centre = SGGeod::fromDegM(rwy->longitude(), rwy->latitude(), 0); // We don't need the elev
257 ortho.Init(centre, rwy->headingDeg());
259 SGVec3d xyc = ortho.ConvertToLocal(centre);
260 SGVec3d xyp = ortho.ConvertToLocal(pt);
262 //cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
263 //cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
265 if((fabs(xyp.y() - xyc.y()) < ((rwy->lengthFt()/2.0) + 5.0))
266 && (fabs(xyp.x() - xyc.x()) < (rwy->widthFt()/2.0))) {