]> git.mxchange.org Git - flightgear.git/blobdiff - src/ATC/ATCutils.cxx
Migrate FlightGear code to use "#include SG_GL*" defined in
[flightgear.git] / src / ATC / ATCutils.cxx
index 0c98fa73b00e81f981cab3bee7805035e7452e99..fa00be1e4993939eda3677140af1354af98a09a6 100644 (file)
 #include <math.h>
 #include <simgear/math/point3d.hxx>
 #include <simgear/constants.h>
+#include <simgear/misc/sg_path.hxx>
+#include <simgear/debug/logstream.hxx>
 #include <plib/sg.h>
 //#include <iomanip.h>
 
+#include <Airports/runways.hxx>
+#include <Main/globals.hxx>
+
 #include "ATCutils.hxx"
+#include "ATCProjection.hxx"
 
 // Convert any number to spoken digits
 string ConvertNumToSpokenDigits(string n) {
@@ -46,10 +52,19 @@ string ConvertNumToSpokenDigits(string n) {
                        str += " ";
                }
        }
-
        return(str);
 }
 
+
+// Convert an integer to spoken digits
+string ConvertNumToSpokenDigits(int n) {
+       char buf[12];   // should be big enough!!
+       sprintf(buf, "%i", n);
+       string tempstr1 = buf;
+       return(ConvertNumToSpokenDigits(tempstr1));
+}
+
+
 // 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"};
@@ -74,41 +89,98 @@ string ConvertRwyNumToSpokenString(int n) {
        return(str);
 }
 
+// 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) {
+       if(s.size() < 3) {
+               return(ConvertRwyNumToSpokenString(atoi(s.c_str())));
+       } else {
+               string r = ConvertRwyNumToSpokenString(atoi(s.substr(0,2).c_str()));
+               if(s.substr(2,1) == "L") {
+                       r += " left";
+               } else if(s.substr(2,1) == "R") {
+                       r += " right";
+               } else if(s.substr(2,1) == "C") {
+                       r += " center";
+               } else {
+                       SG_LOG(SG_ATC, SG_WARN, "WARNING: Unknown suffix " << s.substr(2,1) << " from runway ID " << s << " in ConvertRwyNumToSpokenString(...)");
+               }
+               return(r);
+       }
+}
+       
+
 // 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 x;       //East-West separation
@@ -218,3 +290,97 @@ void dclBoundHeading(double &hdg) {
                hdg -= 360.0;
        }
 }
+
+// smallest difference between two angles in degrees
+// difference is negative if a1 > a2 and positive if a2 > a1
+double GetAngleDiff_deg( const double &a1, const double &a2) {
+  
+  double a3 = a2 - a1;
+  while (a3 < 180.0) a3 += 360.0;
+  while (a3 > 180.0) a3 -= 360.0;
+
+  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 ) {
+    FGAirport result;
+
+    if ( id.length() ) {
+        SG_LOG( SG_GENERAL, SG_INFO, "Searching for airport code = " << id );
+
+        result = globals->get_airports()->search( id );
+        if ( result.id.empty() ) {
+            SG_LOG( SG_GENERAL, SG_WARN,
+                    "Failed to find " << id << " in basic.dat.gz" );
+            return false;
+        }
+    } else {
+        return false;
+    }
+
+    *a = result;
+
+    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_ATC, SG_INFO,
+            "Finding elevation for airport: " << id );
+
+    if ( dclFindAirportID( id, &a ) ) {
+        return a.elevation * SG_FEET_TO_METER;
+    } else {
+        return -9999.0;
+    }
+}
+
+// get airport position
+Point3D dclGetAirportPos( const string& id ) {
+    FGAirport a;
+    // double lon, lat;
+
+    SG_LOG( SG_ATC, SG_INFO,
+            "Finding position for airport: " << id );
+
+    if ( dclFindAirportID( id, &a ) ) {
+        return Point3D(a.longitude, a.latitude, a.elevation);
+    } else {
+        return Point3D(0.0, 0.0, -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) {
+       FGATCAlignedProjection ortho;
+       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);
+       
+       //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))) {
+               return(true);
+       }
+       
+       return(false);
+}
+