]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCutils.cxx
Moved random ground cover object management code (userdata.[ch]xx) over
[flightgear.git] / src / ATC / 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., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 #include <math.h>
22 #include <simgear/math/point3d.hxx>
23 #include <simgear/constants.h>
24 #include <simgear/misc/sg_path.hxx>
25 #include <simgear/debug/logstream.hxx>
26 #include <plib/sg.h>
27 //#include <iomanip.h>
28
29 #include <Airports/runways.hxx>
30 #include <Main/globals.hxx>
31
32 #include "ATCutils.hxx"
33 #include "ATCProjection.hxx"
34
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";
40         string str = "";
41         
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) == " ") {
45                         // do nothing
46                 } else if(n.substr(i,1) == ".") {
47                         str += pt;
48                 } else {
49                         str += nums[atoi((n.substr(i,1)).c_str())];
50                 }
51                 if(i != (n.length()-1)) {       // ie. don't add a space at the end.
52                         str += " ";
53                 }
54         }
55
56         return(str);
57 }
58
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
63         while(n < 0) {
64                 n += 36;
65         }
66         while(n > 36) {
67                 n -= 36;
68         }
69         if(n == 0) {
70                 n = 36; // Is this right?
71         }
72         
73         string str = "";
74         int index = n/10;
75         str += nums[index];
76         n -= (index * 10);
77         //str += "-";
78         str += " ";             //Changed this for the benefit of the voice token parser - prefer the "-" in the visual output though.
79         str += nums[n];
80         return(str);
81 }
82
83 // Assumes we get a two-digit string optionally appended with L, R or C
84 // eg 01 07L 29R 36
85 // Anything else is not guaranteed to be handled correctly!
86 string ConvertRwyNumToSpokenString(string s) {
87         if(s.size() < 3) {
88                 return(ConvertRwyNumToSpokenString(atoi(s.c_str())));
89         } else {
90                 string r = ConvertRwyNumToSpokenString(atoi(s.substr(0,2).c_str()));
91                 if(s.substr(2,1) == "L") {
92                         r += " left";
93                 } else if(s.substr(2,1) == "R") {
94                         r += " right";
95                 } else if(s.substr(2,1) == "C") {
96                         r += " center";
97                 } else {
98                         SG_LOG(SG_ATC, SG_WARN, "WARNING: Unknown suffix " << s.substr(2,1) << " from runway ID " << s << " in ConvertRwyNumToSpokenString(...)");
99                 }
100                 return(r);
101         }
102 }
103         
104
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
108         switch(i) {
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");
135         }
136         // We shouldn't get here
137         return("Error");
138 }
139
140 //================================================================================================================
141
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)
147         
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;
152         
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));
155         z = sqrt(x*x + y*y);
156         
157         return(z);
158 }
159
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) {
164         double vecx = x2-x1;
165         double vecy = y2-y1;
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);
170         vecx = px - x0;
171         vecy = py - y0;
172         double d = sqrt(vecx*vecx + vecy*vecy);
173         if(d < 0) {
174                 d *= -1;
175         }
176         return(d);
177 }
178
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';
190         
191         double horiz_dist = distance * cos(angle);
192         double vert_dist = distance * sin(angle);
193         
194         double north_dist = horiz_dist * cos(heading);
195         double east_dist = horiz_dist * sin(heading);
196         
197         //cout << distance << ' ' << horiz_dist << ' ' << vert_dist << ' ' << north_dist << ' ' << east_dist << '\n';
198         
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';
202         lat += delta_lat;
203         lon += delta_lon;
204         elev += vert_dist;
205         //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
206         
207         //cout << setprecision(15) << DCL_DEGREES_TO_RADIANS * DCL_RADIANS_TO_DEGREES << '\n';
208         
209         return(Point3D(lon*DCL_RADIANS_TO_DEGREES, lat*DCL_RADIANS_TO_DEGREES, elev));
210 }
211
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;
222         
223         if(xdist >= 0) {
224                 if(ydist > 0) {
225                         return(atan(xdist/ydist) * DCL_RADIANS_TO_DEGREES);
226                 } else if (ydist == 0) {
227                         return(90.0);
228                 } else {
229                         return(180.0 - atan(xdist/fabs(ydist)) * DCL_RADIANS_TO_DEGREES);
230                 }
231         } else {
232                 if(ydist > 0) {
233                         return(360.0 - atan(fabs(xdist)/ydist) * DCL_RADIANS_TO_DEGREES);
234                 } else if (ydist == 0) {
235                         return(270.0);
236                 } else {
237                         return(180.0 + atan(xdist/ydist) * DCL_RADIANS_TO_DEGREES);
238                 }
239         }
240 }
241
242 // Given a heading (in degrees), bound it from 0 -> 360
243 void dclBoundHeading(double &hdg) {
244         while(hdg < 0.0) {
245                 hdg += 360.0;
246         }
247         while(hdg > 360.0) {
248                 hdg -= 360.0;
249         }
250 }
251
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) {
255   
256   double a3 = a2 - a1;
257   while (a3 < 180.0) a3 += 360.0;
258   while (a3 > 180.0) a3 -= 360.0;
259
260   return a3;
261 }
262
263 //================================================================================================================
264
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 ) {
269     if ( id.length() ) {
270         SGPath path( globals->get_fg_root() );
271         path.append( "Airports" );
272         path.append( "simple.mk4" );
273         FGAirports airports( path.c_str() );
274
275         SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
276
277         if ( ! airports.search( id, a ) ) {
278             SG_LOG( SG_GENERAL, SG_ALERT,
279                     "Failed to find " << id << " in " << path.str() );
280             return false;
281         }
282     } else {
283         return false;
284     }
285
286     SG_LOG( SG_GENERAL, SG_INFO,
287             "Position for " << id << " is ("
288             << a->longitude << ", "
289             << a->latitude << ")" );
290
291     return true;
292 }
293
294 // get airport elevation
295 double dclGetAirportElev( const string& id ) {
296     FGAirport a;
297     // double lon, lat;
298
299     SG_LOG( SG_GENERAL, SG_INFO,
300             "Finding elevation for airport: " << id );
301
302     if ( dclFindAirportID( id, &a ) ) {
303         return a.elevation;
304     } else {
305         return -9999.0;
306     }
307 }
308
309 // Runway stuff
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);
315         
316         Point3D xyc = ortho.ConvertToLocal(centre);
317         Point3D xyp = ortho.ConvertToLocal(pt);
318         
319         //cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
320         //cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
321         
322         if((fabs(xyp.y() - xyc.y()) < ((rwy.length/2.0) + 5.0)) 
323                 && (fabs(xyp.x() - xyc.x()) < (rwy.width/2.0))) {
324                 return(true);
325         }
326         
327         return(false);
328 }
329