]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/ATCutils.cxx
Modified Files:
[flightgear.git] / src / ATC / ATCutils.cxx
index 20a7819023af53bd2407db50e6e7d87959352728..c5a229fee1cf04b97c631a4d111b86e723eb4993 100644 (file)
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
+#include <sstream>
 
 #include <math.h>
 #include <simgear/math/point3d.hxx>
 #include "ATCutils.hxx"
 #include "ATCProjection.hxx"
 
+static const string nums[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "niner"};
+
 // Convert any number to spoken digits
-string ConvertNumToSpokenDigits(string n) {
+string ConvertNumToSpokenDigits(const string &n) {
        //cout << "n = " << n << endl;
-       string nums[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
-       string pt = "decimal";
+       static const string pt = "decimal";
        string str = "";
        
        for(unsigned int i=0; i<n.length(); ++i) {
@@ -52,13 +59,20 @@ string ConvertNumToSpokenDigits(string n) {
                        str += " ";
                }
        }
-
        return(str);
 }
 
+
+// Convert an integer to spoken digits
+string ConvertNumToSpokenDigits(int n) {
+       std::ostringstream buf;
+       buf << n;
+       return(ConvertNumToSpokenDigits(buf.str()));
+}
+
+
 // Convert a 2 digit rwy number to a spoken-style string
 string ConvertRwyNumToSpokenString(int n) {
-       string nums[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
        // Basic error/sanity checking
        while(n < 0) {
                n += 36;
@@ -83,7 +97,7 @@ string ConvertRwyNumToSpokenString(int n) {
 // Assumes we get a two-digit string optionally appended with L, R or C
 // eg 01 07L 29R 36
 // Anything else is not guaranteed to be handled correctly!
-string ConvertRwyNumToSpokenString(string s) {
+string ConvertRwyNumToSpokenString(const string &s) {
        if(s.size() < 3) {
                return(ConvertRwyNumToSpokenString(atoi(s.c_str())));
        } else {
@@ -105,42 +119,75 @@ string ConvertRwyNumToSpokenString(string s) {
 // Return the phonetic letter of a letter represented as an integer 1->26
 string GetPhoneticIdent(int i) {
        // TODO - Check i is between 1 and 26 and wrap if necessary
-       switch(i) {
-       case 1 : return("alpha");
-       case 2 : return("bravo");
-       case 3 : return("charlie");
-       case 4 : return("delta");
-       case 5 : return("echo");
-       case 6 : return("foxtrot");
-       case 7 : return("golf");
-       case 8 : return("hotel");
-       case 9 : return("india");
-       case 10 : return("juliet");
-       case 11 : return("kilo");
-       case 12 : return("lima");
-       case 13 : return("mike");
-       case 14 : return("november");
-       case 15 : return("oscar");
-       case 16 : return("papa");
-       case 17 : return("quebec");
-       case 18 : return("romeo");
-       case 19 : return("sierra");
-       case 20 : return("tango");
-       case 21 : return("uniform");
-       case 22 : return("victor");
-       case 23 : return("whiskey");
-       case 24 : return("x-ray");
-       case 25 : return("yankee");
-       case 26 : return("zulu");
+       return(GetPhoneticIdent(char('a' + (i-1))));
+}
+
+// Return the phonetic letter of a character in the range a-z or A-Z.
+// Currently always returns prefixed by lowercase.
+string GetPhoneticIdent(char c) {
+       c = tolower(c);
+       // TODO - Check c is between a and z and wrap if necessary
+       switch(c) {
+       case 'a' : return("alpha");
+       case 'b' : return("bravo");
+       case 'c' : return("charlie");
+       case 'd' : return("delta");
+       case 'e' : return("echo");
+       case 'f' : return("foxtrot");
+       case 'g' : return("golf");
+       case 'h' : return("hotel");
+       case 'i' : return("india");
+       case 'j' : return("juliet");
+       case 'k' : return("kilo");
+       case 'l' : return("lima");
+       case 'm' : return("mike");
+       case 'n' : return("november");
+       case 'o' : return("oscar");
+       case 'p' : return("papa");
+       case 'q' : return("quebec");
+       case 'r' : return("romeo");
+       case 's' : return("sierra");
+       case 't' : return("tango");
+       case 'u' : return("uniform");
+       case 'v' : return("victor");
+       case 'w' : return("whiskey");
+       case 'x' : return("x-ray");
+       case 'y' : return("yankee");
+       case 'z' : return("zulu");
        }
        // We shouldn't get here
        return("Error");
 }
 
+// Get the compass direction associated with a heading in degrees
+// Currently returns 8 direction resolution (N, NE, E etc...)
+// Might be modified in future to return 4, 8 or 16 resolution but defaulting to 8. 
+string GetCompassDirection(double h) {
+       while(h < 0.0) h += 360.0;
+       while(h > 360.0) h -= 360.0;
+       if(h < 22.5 || h > 337.5) {
+               return("North");
+       } else if(h < 67.5) {
+               return("North-East");
+       } else if(h < 112.5) {
+               return("East");
+       } else if(h < 157.5) {
+               return("South-East");
+       } else if(h < 202.5) {
+               return("South");
+       } else if(h < 247.5) {
+               return("South-West");
+       } else if(h < 292.5) {
+               return("West");
+       } else {
+               return("North-West");
+       }
+}
+
 //================================================================================================================
 
 // Given two positions (lat & lon in degrees), get the HORIZONTAL separation (in meters)
-double dclGetHorizontalSeparation(Point3D pos1, Point3D pos2) {
+double dclGetHorizontalSeparation(const Point3D& pos1, const Point3D& pos2) {
        double x;       //East-West separation
        double y;       //North-South separation
        double z;       //Horizontal separation - z = sqrt(x^2 + y^2)
@@ -179,7 +226,7 @@ double dclGetLinePointSeparation(double px, double py, double x1, double y1, dou
 // Given a position (lat/lon/elev), heading and vertical angle (degrees), and distance (meters), calculate the new position.
 // This function assumes the world is spherical.  If geodetic accuracy is required use the functions is sg_geodesy instead!
 // Assumes that the ground is not hit!!!  Expects heading and angle in degrees, distance in meters. 
-Point3D dclUpdatePosition(Point3D pos, double heading, double angle, double distance) {
+Point3D dclUpdatePosition(const Point3D& pos, double heading, double angle, double distance) {
        //cout << setprecision(10) << pos.lon() << ' ' << pos.lat() << '\n';
        heading *= DCL_DEGREES_TO_RADIANS;
        angle *= DCL_DEGREES_TO_RADIANS;
@@ -211,32 +258,16 @@ Point3D dclUpdatePosition(Point3D pos, double heading, double angle, double dist
 
 // Get a heading in degrees from one lat/lon to another.
 // This function assumes the world is spherical.  If geodetic accuracy is required use the functions is sg_geodesy instead!
-// Warning - at the moment we are not checking for identical points - currently it returns 90 in this instance.
-double GetHeadingFromTo(Point3D A, Point3D B) {
+// Warning - at the moment we are not checking for identical points - currently it returns 0 in this instance.
+double GetHeadingFromTo(const Point3D& A, const Point3D& B) {
        double latA = A.lat() * DCL_DEGREES_TO_RADIANS;
        double lonA = A.lon() * DCL_DEGREES_TO_RADIANS;
        double latB = B.lat() * DCL_DEGREES_TO_RADIANS;
        double lonB = B.lon() * DCL_DEGREES_TO_RADIANS;
        double xdist = sin(lonB - lonA) * (double)SG_EQUATORIAL_RADIUS_M * cos((latA+latB)/2.0);
        double ydist = sin(latB - latA) * (double)SG_EQUATORIAL_RADIUS_M;
-       
-       if(xdist >= 0) {
-               if(ydist > 0) {
-                       return(atan(xdist/ydist) * DCL_RADIANS_TO_DEGREES);
-               } else if (ydist == 0) {
-                       return(90.0);
-               } else {
-                       return(180.0 - atan(xdist/fabs(ydist)) * DCL_RADIANS_TO_DEGREES);
-               }
-       } else {
-               if(ydist > 0) {
-                       return(360.0 - atan(fabs(xdist)/ydist) * DCL_RADIANS_TO_DEGREES);
-               } else if (ydist == 0) {
-                       return(270.0);
-               } else {
-                       return(180.0 + atan(xdist/ydist) * DCL_RADIANS_TO_DEGREES);
-               }
-       }
+       double heading = atan2(xdist, ydist) * DCL_RADIANS_TO_DEGREES;
+       return heading < 0.0 ? heading + 360 : heading;
 }
 
 // Given a heading (in degrees), bound it from 0 -> 360
@@ -260,58 +291,12 @@ double GetAngleDiff_deg( const double &a1, const double &a2) {
   return a3;
 }
 
-//================================================================================================================
-
-// Airport stuff.  The next two functions are straight copies of their fg.... equivalents
-// in fg_init.cxx, and are just here temporarily until some rationalisation occurs.
-// find basic airport location info from airport database
-bool dclFindAirportID( const string& id, FGAirport *a ) {
-    if ( id.length() ) {
-        SGPath path( globals->get_fg_root() );
-        path.append( "Airports" );
-        path.append( "simple.mk4" );
-        FGAirports airports( path.c_str() );
-
-        SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
-
-        if ( ! airports.search( id, a ) ) {
-            SG_LOG( SG_GENERAL, SG_ALERT,
-                    "Failed to find " << id << " in " << path.str() );
-            return false;
-        }
-    } else {
-        return false;
-    }
-
-    SG_LOG( SG_GENERAL, SG_INFO,
-            "Position for " << id << " is ("
-            << a->longitude << ", "
-            << a->latitude << ")" );
-
-    return true;
-}
-
-// get airport elevation
-double dclGetAirportElev( const string& id ) {
-    FGAirport a;
-    // double lon, lat;
-
-    SG_LOG( SG_GENERAL, SG_INFO,
-            "Finding elevation for airport: " << id );
-
-    if ( dclFindAirportID( id, &a ) ) {
-        return a.elevation;
-    } else {
-        return -9999.0;
-    }
-}
-
 // Runway stuff
 // Given a Point3D (lon/lat/elev) and an FGRunway struct, determine if the point lies on the runway
-bool OnRunway(Point3D pt, const FGRunway& rwy) {
+bool OnRunway(const Point3D& pt, const FGRunway& rwy) {
        FGATCAlignedProjection ortho;
-       Point3D centre(rwy.lon, rwy.lat, 0.0);  // We don't need the elev
-       ortho.Init(centre, rwy.heading);
+       Point3D centre(rwy._lon, rwy._lat, 0.0);        // We don't need the elev
+       ortho.Init(centre, rwy._heading);
        
        Point3D xyc = ortho.ConvertToLocal(centre);
        Point3D xyp = ortho.ConvertToLocal(pt);
@@ -319,8 +304,8 @@ bool OnRunway(Point3D pt, const FGRunway& rwy) {
        //cout << "Length offset = " << fabs(xyp.y() - xyc.y()) << '\n';
        //cout << "Width offset = " << fabs(xyp.x() - xyc.x()) << '\n';
        
-       if((fabs(xyp.y() - xyc.y()) < ((rwy.length/2.0) + 5.0)) 
-               && (fabs(xyp.x() - xyc.x()) < (rwy.width/2.0))) {
+       if((fabs(xyp.y() - xyc.y()) < ((rwy._length/2.0) + 5.0)) 
+               && (fabs(xyp.x() - xyc.x()) < (rwy._width/2.0))) {
                return(true);
        }