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/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",
39 "five", "six", "seven", "eight", "niner"};
41 static const string letters[LTRS] = {
42 "alpha", "bravo", "charlie", "delta", "echo",
43 "foxtrot", "golf", "hotel", "india", "juliet",
44 "kilo", "lima", "mike", "november", "oscar",
45 "papa", "quebec", "romeo", "sierra", "tango",
46 "uniform", "victor", "whiskey", "xray", "yankee", "zulu"
49 // Convert any number to spoken digits
50 string ConvertNumToSpokenDigits(const string &n) {
51 //cout << "n = " << n << endl;
52 static const string pt = "decimal";
55 for(unsigned int i=0; i<n.length(); ++i) {
56 //cout << "n.substr(" << i << ",1 = " << n.substr(i,1) << endl;
57 if(n.substr(i,1) == " ") {
59 } else if(n.substr(i,1) == ".") {
62 str += nums[atoi((n.substr(i,1)).c_str())];
64 if(i != (n.length()-1)) { // ie. don't add a space at the end.
71 // Convert an integer to a decimal numeral string
72 string decimalNumeral(const int& n) {
73 std::ostringstream buf;
78 // Convert an integer to spoken digits
79 string ConvertNumToSpokenDigits(const int& n) {
80 return ConvertNumToSpokenDigits(decimalNumeral(n));
84 // Assumes we get a string of digits optionally appended with L, R or C
86 // Anything else is not guaranteed to be handled correctly!
87 string ConvertRwyNumToSpokenString(const string &rwy) {
89 for (size_t ii = 0; ii < rwy.length(); ii++){
90 if (rslt.length()) rslt += " ";
91 string ch = rwy.substr(ii,1);
92 if (isdigit(ch[0])) rslt += ConvertNumToSpokenDigits(atoi(ch.c_str()));
93 else if (ch == "R") rslt += "right";
94 else if (ch == "C") rslt += "center";
95 else if (ch == "L") rslt += "left";
97 rslt += GetPhoneticLetter(ch[0]);
98 SG_LOG(SG_ATC, SG_WARN, "WARNING: Unknown suffix '" << ch
99 << "' in runway " << rwy << " in ConvertRwyNumToSpokenString(...)");
106 // Return the phonetic letter of a letter represented as an integer 1->26
107 string GetPhoneticLetter(const int i) {
108 return(letters[i % LTRS]);
111 // Return the phonetic letter of a character in the range a-z or A-Z.
112 // Currently always returns prefixed by lowercase.
113 string GetPhoneticLetter(const char c) {
114 return GetPhoneticLetter(int(tolower(c) - 'a'));
117 // Get the compass direction associated with a heading in degrees
118 // Currently returns 8 direction resolution (N, NE, E etc...)
119 // Might be modified in future to return 4, 8 or 16 resolution but defaulting to 8.
120 string GetCompassDirection(double h) {
121 while(h < 0.0) h += 360.0;
122 while(h > 360.0) h -= 360.0;
123 if(h < 22.5 || h > 337.5) {
125 } else if(h < 67.5) {
126 return("North-East");
127 } else if(h < 112.5) {
129 } else if(h < 157.5) {
130 return("South-East");
131 } else if(h < 202.5) {
133 } else if(h < 247.5) {
134 return("South-West");
135 } else if(h < 292.5) {
138 return("North-West");
142 //================================================================================================================
144 // Given two positions (lat & lon in degrees), get the HORIZONTAL separation (in meters)
145 double dclGetHorizontalSeparation(const SGGeod& pos1, const SGGeod& pos2) {
146 double x; //East-West separation
147 double y; //North-South separation
148 double z; //Horizontal separation - z = sqrt(x^2 + y^2)
150 double lat1 = pos1.getLatitudeRad();
151 double lon1 = pos1.getLongitudeRad();
152 double lat2 = pos2.getLatitudeRad();
153 double lon2 = pos2.getLongitudeRad();
155 y = sin(fabs(lat1 - lat2)) * SG_EQUATORIAL_RADIUS_M;
156 x = sin(fabs(lon1 - lon2)) * SG_EQUATORIAL_RADIUS_M * (cos((lat1 + lat2) / 2.0));
162 // Given a point and a line, get the HORIZONTAL shortest distance from the point to a point on the line.
163 // Expects to be fed orthogonal co-ordinates, NOT lat & lon !
164 // The units of the separation will be those of the input.
165 double dclGetLinePointSeparation(double px, double py, double x1, double y1, double x2, double y2) {
168 double magline = sqrt(vecx*vecx + vecy*vecy);
169 double u = ((px-x1)*(x2-x1) + (py-y1)*(y2-y1)) / (magline * magline);
170 double x0 = x1 + u*(x2-x1);
171 double y0 = y1 + u*(y2-y1);
174 double d = sqrt(vecx*vecx + vecy*vecy);
181 // Given a position (lat/lon/elev), heading and vertical angle (degrees), and distance (meters), calculate the new position.
182 // This function assumes the world is spherical. If geodetic accuracy is required use the functions is sg_geodesy instead!
183 // Assumes that the ground is not hit!!! Expects heading and angle in degrees, distance in meters.
184 SGGeod dclUpdatePosition(const SGGeod& pos, double heading, double angle, double distance) {
185 // FIXME: use SGGeodesy instead ...
187 //cout << setprecision(10) << pos.lon() << ' ' << pos.lat() << '\n';
188 heading *= DCL_DEGREES_TO_RADIANS;
189 angle *= DCL_DEGREES_TO_RADIANS;
190 double lat = pos.getLatitudeRad();
191 double lon = pos.getLongitudeRad();
192 double elev = pos.getElevationM();
193 //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
195 double horiz_dist = distance * cos(angle);
196 double vert_dist = distance * sin(angle);
198 double north_dist = horiz_dist * cos(heading);
199 double east_dist = horiz_dist * sin(heading);
201 //cout << distance << ' ' << horiz_dist << ' ' << vert_dist << ' ' << north_dist << ' ' << east_dist << '\n';
203 double delta_lat = asin(north_dist / (double)SG_EQUATORIAL_RADIUS_M);
204 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.
205 //cout << delta_lon*DCL_RADIANS_TO_DEGREES << ' ' << delta_lat*DCL_RADIANS_TO_DEGREES << '\n';
209 //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
211 //cout << setprecision(15) << DCL_DEGREES_TO_RADIANS * DCL_RADIANS_TO_DEGREES << '\n';
213 return SGGeod::fromRadM(lon, lat, elev);
216 // Get a heading in degrees from one lat/lon to another.
217 // This function assumes the world is spherical. If geodetic accuracy is required use the functions is sg_geodesy instead!
218 // Warning - at the moment we are not checking for identical points - currently it returns 0 in this instance.
219 double GetHeadingFromTo(const SGGeod& A, const SGGeod& B) {
220 double latA = A.getLatitudeRad();
221 double lonA = A.getLongitudeRad();
222 double latB = B.getLatitudeRad();
223 double lonB = B.getLongitudeRad();
224 double xdist = sin(lonB - lonA) * (double)SG_EQUATORIAL_RADIUS_M * cos((latA+latB)/2.0);
225 double ydist = sin(latB - latA) * (double)SG_EQUATORIAL_RADIUS_M;
226 double heading = atan2(xdist, ydist) * DCL_RADIANS_TO_DEGREES;
227 return heading < 0.0 ? heading + 360 : heading;
230 // Given a heading (in degrees), bound it from 0 -> 360
231 void dclBoundHeading(double &hdg) {
240 // smallest difference between two angles in degrees
241 // difference is negative if a1 > a2 and positive if a2 > a1
242 double GetAngleDiff_deg( const double &a1, const double &a2) {
245 while (a3 < 180.0) a3 += 360.0;
246 while (a3 > 180.0) a3 -= 360.0;
252 // Given (lon/lat/elev) and an FGRunway struct, determine if the point lies on the runway
253 bool OnRunway(const SGGeod& pt, const FGRunwayBase* rwy) {
254 FGATCAlignedProjection ortho;
255 SGGeod centre = SGGeod::fromDegM(rwy->longitude(), rwy->latitude(), 0); // We don't need the elev
256 ortho.Init(centre, rwy->headingDeg());
258 SGVec3d xyc = ortho.ConvertToLocal(centre);
259 SGVec3d xyp = ortho.ConvertToLocal(pt);
261 //cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
262 //cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
264 if((fabs(xyp.y() - xyc.y()) < ((rwy->lengthFt()/2.0) + 5.0))
265 && (fabs(xyp.x() - xyc.x()) < (rwy->widthFt()/2.0))) {