]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCutils.cxx
Merge branch 'tbm/graphics-bug' into next
[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 <math.h>
28 #include <simgear/math/point3d.hxx>
29 #include <simgear/constants.h>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/debug/logstream.hxx>
32 #include <plib/sg.h>
33 //#include <iomanip.h>
34
35 #include <Airports/runways.hxx>
36 #include <Main/globals.hxx>
37
38 #include "ATCutils.hxx"
39 #include "ATCProjection.hxx"
40
41 static const string nums[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "niner"};
42
43 // Convert any number to spoken digits
44 string ConvertNumToSpokenDigits(const string &n) {
45         //cout << "n = " << n << endl;
46         static const string pt = "decimal";
47         string str = "";
48         
49         for(unsigned int i=0; i<n.length(); ++i) {
50                 //cout << "n.substr(" << i << ",1 = " << n.substr(i,1) << endl;
51                 if(n.substr(i,1) == " ") {
52                         // do nothing
53                 } else if(n.substr(i,1) == ".") {
54                         str += pt;
55                 } else {
56                         str += nums[atoi((n.substr(i,1)).c_str())];
57                 }
58                 if(i != (n.length()-1)) {       // ie. don't add a space at the end.
59                         str += " ";
60                 }
61         }
62         return(str);
63 }
64
65
66 // Convert an integer to spoken digits
67 string ConvertNumToSpokenDigits(int n) {
68         std::ostringstream buf;
69         buf << n;
70         return(ConvertNumToSpokenDigits(buf.str()));
71 }
72
73
74 // Convert a 2 digit rwy number to a spoken-style string
75 string ConvertRwyNumToSpokenString(int n) {
76         // Basic error/sanity checking
77         while(n < 0) {
78                 n += 36;
79         }
80         while(n > 36) {
81                 n -= 36;
82         }
83         if(n == 0) {
84                 n = 36; // Is this right?
85         }
86         
87         string str = "";
88         int index = n/10;
89         str += nums[index];
90         n -= (index * 10);
91         //str += "-";
92         str += " ";             //Changed this for the benefit of the voice token parser - prefer the "-" in the visual output though.
93         str += nums[n];
94         return(str);
95 }
96
97 // Assumes we get a two-digit string optionally appended with L, R or C
98 // eg 01 07L 29R 36
99 // Anything else is not guaranteed to be handled correctly!
100 string ConvertRwyNumToSpokenString(const string &s) {
101         if(s.size() < 3) {
102                 return(ConvertRwyNumToSpokenString(atoi(s.c_str())));
103         } else {
104                 string r = ConvertRwyNumToSpokenString(atoi(s.substr(0,2).c_str()));
105                 if(s.substr(2,1) == "L") {
106                         r += " left";
107                 } else if(s.substr(2,1) == "R") {
108                         r += " right";
109                 } else if(s.substr(2,1) == "C") {
110                         r += " center";
111                 } else {
112                         SG_LOG(SG_ATC, SG_WARN, "WARNING: Unknown suffix " << s.substr(2,1) << " from runway ID " << s << " in ConvertRwyNumToSpokenString(...)");
113                 }
114                 return(r);
115         }
116 }
117         
118
119 // Return the phonetic letter of a letter represented as an integer 1->26
120 string GetPhoneticIdent(int i) {
121         // TODO - Check i is between 1 and 26 and wrap if necessary
122         return(GetPhoneticIdent(char('a' + (i-1))));
123 }
124
125 // Return the phonetic letter of a character in the range a-z or A-Z.
126 // Currently always returns prefixed by lowercase.
127 string GetPhoneticIdent(char c) {
128         c = tolower(c);
129         // TODO - Check c is between a and z and wrap if necessary
130         switch(c) {
131         case 'a' : return("alpha");
132         case 'b' : return("bravo");
133         case 'c' : return("charlie");
134         case 'd' : return("delta");
135         case 'e' : return("echo");
136         case 'f' : return("foxtrot");
137         case 'g' : return("golf");
138         case 'h' : return("hotel");
139         case 'i' : return("india");
140         case 'j' : return("juliet");
141         case 'k' : return("kilo");
142         case 'l' : return("lima");
143         case 'm' : return("mike");
144         case 'n' : return("november");
145         case 'o' : return("oscar");
146         case 'p' : return("papa");
147         case 'q' : return("quebec");
148         case 'r' : return("romeo");
149         case 's' : return("sierra");
150         case 't' : return("tango");
151         case 'u' : return("uniform");
152         case 'v' : return("victor");
153         case 'w' : return("whiskey");
154         case 'x' : return("x-ray");
155         case 'y' : return("yankee");
156         case 'z' : return("zulu");
157         }
158         // We shouldn't get here
159         return("Error");
160 }
161
162 // Get the compass direction associated with a heading in degrees
163 // Currently returns 8 direction resolution (N, NE, E etc...)
164 // Might be modified in future to return 4, 8 or 16 resolution but defaulting to 8. 
165 string GetCompassDirection(double h) {
166         while(h < 0.0) h += 360.0;
167         while(h > 360.0) h -= 360.0;
168         if(h < 22.5 || h > 337.5) {
169                 return("North");
170         } else if(h < 67.5) {
171                 return("North-East");
172         } else if(h < 112.5) {
173                 return("East");
174         } else if(h < 157.5) {
175                 return("South-East");
176         } else if(h < 202.5) {
177                 return("South");
178         } else if(h < 247.5) {
179                 return("South-West");
180         } else if(h < 292.5) {
181                 return("West");
182         } else {
183                 return("North-West");
184         }
185 }
186
187 //================================================================================================================
188
189 // Given two positions (lat & lon in degrees), get the HORIZONTAL separation (in meters)
190 double dclGetHorizontalSeparation(const Point3D& pos1, const Point3D& pos2) {
191         double x;       //East-West separation
192         double y;       //North-South separation
193         double z;       //Horizontal separation - z = sqrt(x^2 + y^2)
194         
195         double lat1 = pos1.lat() * SG_DEGREES_TO_RADIANS;
196         double lon1 = pos1.lon() * SG_DEGREES_TO_RADIANS;
197         double lat2 = pos2.lat() * SG_DEGREES_TO_RADIANS;
198         double lon2 = pos2.lon() * SG_DEGREES_TO_RADIANS;
199         
200         y = sin(fabs(lat1 - lat2)) * SG_EQUATORIAL_RADIUS_M;
201         x = sin(fabs(lon1 - lon2)) * SG_EQUATORIAL_RADIUS_M * (cos((lat1 + lat2) / 2.0));
202         z = sqrt(x*x + y*y);
203         
204         return(z);
205 }
206
207 // Given a point and a line, get the HORIZONTAL shortest distance from the point to a point on the line.
208 // Expects to be fed orthogonal co-ordinates, NOT lat & lon !
209 // The units of the separation will be those of the input.
210 double dclGetLinePointSeparation(double px, double py, double x1, double y1, double x2, double y2) {
211         double vecx = x2-x1;
212         double vecy = y2-y1;
213         double magline = sqrt(vecx*vecx + vecy*vecy);
214         double u = ((px-x1)*(x2-x1) + (py-y1)*(y2-y1)) / (magline * magline);
215         double x0 = x1 + u*(x2-x1);
216         double y0 = y1 + u*(y2-y1);
217         vecx = px - x0;
218         vecy = py - y0;
219         double d = sqrt(vecx*vecx + vecy*vecy);
220         if(d < 0) {
221                 d *= -1;
222         }
223         return(d);
224 }
225
226 // Given a position (lat/lon/elev), heading and vertical angle (degrees), and distance (meters), calculate the new position.
227 // This function assumes the world is spherical.  If geodetic accuracy is required use the functions is sg_geodesy instead!
228 // Assumes that the ground is not hit!!!  Expects heading and angle in degrees, distance in meters. 
229 Point3D dclUpdatePosition(const Point3D& pos, double heading, double angle, double distance) {
230         //cout << setprecision(10) << pos.lon() << ' ' << pos.lat() << '\n';
231         heading *= DCL_DEGREES_TO_RADIANS;
232         angle *= DCL_DEGREES_TO_RADIANS;
233         double lat = pos.lat() * DCL_DEGREES_TO_RADIANS;
234         double lon = pos.lon() * DCL_DEGREES_TO_RADIANS;
235         double elev = pos.elev();
236         //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
237         
238         double horiz_dist = distance * cos(angle);
239         double vert_dist = distance * sin(angle);
240         
241         double north_dist = horiz_dist * cos(heading);
242         double east_dist = horiz_dist * sin(heading);
243         
244         //cout << distance << ' ' << horiz_dist << ' ' << vert_dist << ' ' << north_dist << ' ' << east_dist << '\n';
245         
246         double delta_lat = asin(north_dist / (double)SG_EQUATORIAL_RADIUS_M);
247         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.
248         //cout << delta_lon*DCL_RADIANS_TO_DEGREES << ' ' << delta_lat*DCL_RADIANS_TO_DEGREES << '\n';
249         lat += delta_lat;
250         lon += delta_lon;
251         elev += vert_dist;
252         //cout << setprecision(10) << lon*DCL_RADIANS_TO_DEGREES << ' ' << lat*DCL_RADIANS_TO_DEGREES << '\n';
253         
254         //cout << setprecision(15) << DCL_DEGREES_TO_RADIANS * DCL_RADIANS_TO_DEGREES << '\n';
255         
256         return(Point3D(lon*DCL_RADIANS_TO_DEGREES, lat*DCL_RADIANS_TO_DEGREES, elev));
257 }
258
259 // Get a heading in degrees from one lat/lon to another.
260 // This function assumes the world is spherical.  If geodetic accuracy is required use the functions is sg_geodesy instead!
261 // Warning - at the moment we are not checking for identical points - currently it returns 0 in this instance.
262 double GetHeadingFromTo(const Point3D& A, const Point3D& B) {
263         double latA = A.lat() * DCL_DEGREES_TO_RADIANS;
264         double lonA = A.lon() * DCL_DEGREES_TO_RADIANS;
265         double latB = B.lat() * DCL_DEGREES_TO_RADIANS;
266         double lonB = B.lon() * DCL_DEGREES_TO_RADIANS;
267         double xdist = sin(lonB - lonA) * (double)SG_EQUATORIAL_RADIUS_M * cos((latA+latB)/2.0);
268         double ydist = sin(latB - latA) * (double)SG_EQUATORIAL_RADIUS_M;
269         double heading = atan2(xdist, ydist) * DCL_RADIANS_TO_DEGREES;
270         return heading < 0.0 ? heading + 360 : heading;
271 }
272
273 // Given a heading (in degrees), bound it from 0 -> 360
274 void dclBoundHeading(double &hdg) {
275         while(hdg < 0.0) {
276                 hdg += 360.0;
277         }
278         while(hdg > 360.0) {
279                 hdg -= 360.0;
280         }
281 }
282
283 // smallest difference between two angles in degrees
284 // difference is negative if a1 > a2 and positive if a2 > a1
285 double GetAngleDiff_deg( const double &a1, const double &a2) {
286   
287   double a3 = a2 - a1;
288   while (a3 < 180.0) a3 += 360.0;
289   while (a3 > 180.0) a3 -= 360.0;
290
291   return a3;
292 }
293
294 // Runway stuff
295 // Given a Point3D (lon/lat/elev) and an FGRunway struct, determine if the point lies on the runway
296 bool OnRunway(const Point3D& pt, const FGRunway* rwy) {
297         FGATCAlignedProjection ortho;
298         Point3D centre(rwy->longitude(), rwy->latitude(), 0.0); // We don't need the elev
299         ortho.Init(centre, rwy->headingDeg());
300         
301         Point3D xyc = ortho.ConvertToLocal(centre);
302         Point3D xyp = ortho.ConvertToLocal(pt);
303         
304         //cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
305         //cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
306         
307         if((fabs(xyp.y() - xyc.y()) < ((rwy->lengthFt()/2.0) + 5.0)) 
308                 && (fabs(xyp.x() - xyc.x()) < (rwy->widthFt()/2.0))) {
309                 return(true);
310         }
311         
312         return(false);
313 }
314