]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCutils.cxx
Update VS2008 projects : use Boost 1.44.0 available in last 3rd Party archive
[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",
39                                 "five", "six", "seven", "eight", "niner"}; 
40
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"
47 };
48
49 // Convert any number to spoken digits
50 string ConvertNumToSpokenDigits(const string &n) {
51         //cout << "n = " << n << endl;
52         static const string pt = "decimal";
53         string str = "";
54         
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) == " ") {
58                         // do nothing
59                 } else if(n.substr(i,1) == ".") {
60                         str += pt;
61                 } else {
62                         str += nums[atoi((n.substr(i,1)).c_str())];
63                 }
64                 if(i != (n.length()-1)) {       // ie. don't add a space at the end.
65                         str += " ";
66                 }
67         }
68         return(str);
69 }
70
71 // Convert an integer to a decimal numeral string
72 string decimalNumeral(const int& n) {
73         std::ostringstream buf;
74         buf << n;
75         return buf.str();
76 }
77
78 // Convert an integer to spoken digits
79 string ConvertNumToSpokenDigits(const int& n) {
80         return ConvertNumToSpokenDigits(decimalNumeral(n));
81 }
82
83
84 // Assumes we get a string of digits optionally appended with L, R or C
85 // eg 1 7L 29R 36
86 // Anything else is not guaranteed to be handled correctly!
87 string ConvertRwyNumToSpokenString(const string &rwy) {
88   string rslt;
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";
96     else {
97       rslt += GetPhoneticLetter(ch[0]);
98       SG_LOG(SG_ATC, SG_WARN, "WARNING: Unknown suffix '" << ch 
99           << "' in runway " << rwy << " in ConvertRwyNumToSpokenString(...)");
100     }
101   }
102   return rslt;
103 }
104         
105
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]);
109 }
110
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'));
115 }
116
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) {
124                 return("North");
125         } else if(h < 67.5) {
126                 return("North-East");
127         } else if(h < 112.5) {
128                 return("East");
129         } else if(h < 157.5) {
130                 return("South-East");
131         } else if(h < 202.5) {
132                 return("South");
133         } else if(h < 247.5) {
134                 return("South-West");
135         } else if(h < 292.5) {
136                 return("West");
137         } else {
138                 return("North-West");
139         }
140 }
141
142 //================================================================================================================
143
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)
149         
150         double lat1 = pos1.getLatitudeRad();
151         double lon1 = pos1.getLongitudeRad();
152         double lat2 = pos2.getLatitudeRad();
153         double lon2 = pos2.getLongitudeRad();
154         
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));
157         z = sqrt(x*x + y*y);
158         
159         return(z);
160 }
161
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) {
166         double vecx = x2-x1;
167         double vecy = y2-y1;
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);
172         vecx = px - x0;
173         vecy = py - y0;
174         double d = sqrt(vecx*vecx + vecy*vecy);
175         if(d < 0) {
176                 d *= -1;
177         }
178         return(d);
179 }
180
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 ...
186
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';
194         
195         double horiz_dist = distance * cos(angle);
196         double vert_dist = distance * sin(angle);
197         
198         double north_dist = horiz_dist * cos(heading);
199         double east_dist = horiz_dist * sin(heading);
200         
201         //cout << distance << ' ' << horiz_dist << ' ' << vert_dist << ' ' << north_dist << ' ' << east_dist << '\n';
202         
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';
206         lat += delta_lat;
207         lon += delta_lon;
208         elev += vert_dist;
209         //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
210         
211         //cout << setprecision(15) << DCL_DEGREES_TO_RADIANS * DCL_RADIANS_TO_DEGREES << '\n';
212         
213         return SGGeod::fromRadM(lon, lat, elev);
214 }
215
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;
228 }
229
230 // Given a heading (in degrees), bound it from 0 -> 360
231 void dclBoundHeading(double &hdg) {
232         while(hdg < 0.0) {
233                 hdg += 360.0;
234         }
235         while(hdg > 360.0) {
236                 hdg -= 360.0;
237         }
238 }
239
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) {
243   
244   double a3 = a2 - a1;
245   while (a3 < 180.0) a3 += 360.0;
246   while (a3 > 180.0) a3 -= 360.0;
247
248   return a3;
249 }
250
251 // Runway stuff
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());
257         
258         SGVec3d xyc = ortho.ConvertToLocal(centre);
259         SGVec3d xyp = ortho.ConvertToLocal(pt);
260         
261         //cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
262         //cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
263         
264         if((fabs(xyp.y() - xyc.y()) < ((rwy->lengthFt()/2.0) + 5.0)) 
265                 && (fabs(xyp.x() - xyc.x()) < (rwy->widthFt()/2.0))) {
266                 return(true);
267         }
268         
269         return(false);
270 }
271