]> git.mxchange.org Git - flightgear.git/blobdiff - src/Airports/runways.cxx
Moved some of the low level scene graph construction code over to simgear.
[flightgear.git] / src / Airports / runways.cxx
index 23c7d68dddecfd6fc9562d982f6c5c5ea0fa881d..ff45f4c7e7e00fe17c31a514d343e0fdb91de38b 100644 (file)
@@ -1,4 +1,4 @@
-// runways.hxx -- a simple class to manage airport runway info
+// runways.cxx -- a simple class to manage airport runway info
 //
 // Written by Curtis Olson, started August 2000.
 //
@@ -54,17 +54,13 @@ SG_USING_NAMESPACE(std);
 
 #ifdef SG_HAVE_STD_INCLUDES
 #  include <istream>
-#elif defined( SG_HAVE_NATIVE_SGI_COMPILERS )
-#  include <iostream.h>
-#elif defined( __BORLANDC__ )
+#elif defined( __BORLANDC__ ) || defined (__APPLE__)
 #  include <iostream>
 #else
 #  include <istream.h>
 #endif
 
-#if ! defined( SG_HAVE_NATIVE_SGI_COMPILERS )
 SG_USING_STD(istream);
-#endif
 
 inline istream&
 operator >> ( istream& in, FGRunway& a )
@@ -94,6 +90,47 @@ FGRunways::FGRunways( const string& file ) {
 }
 
 
+// Return reverse rwy number
+// eg 01 -> 19
+// 03L -> 21R
+static string GetReverseRunwayNo(string rwyno) {       
+    // cout << "Original rwyno = " << rwyNo << '\n';
+    
+    // standardize input number
+    string tmp = rwyno.substr(1, 1);
+    if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (rwyno.size() == 1)) {
+       tmp = rwyno;
+       rwyno = "0" + tmp;
+        SG_LOG( SG_GENERAL, SG_INFO, "Standardising rwy number from " << tmp
+                                     << " to " << rwyno );
+    }
+    
+    char buf[4];
+    int rn = atoi(rwyno.substr(0,2).c_str());
+    rn += 18;
+    while(rn > 36) {
+       rn -= 36;
+    }
+    sprintf(buf, "%02i", rn);
+    if(rwyno.size() == 3) {
+       if(rwyno.substr(2,1) == "L") {
+           buf[2] = 'R';
+           buf[3] = '\0';
+       } else if (rwyno.substr(2,1) == "R") {
+           buf[2] = 'L';
+           buf[3] = '\0';
+       } else if (rwyno.substr(2,1) == "C") {
+           buf[2] = 'C';
+           buf[3] = '\0';
+       } else {
+           SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
+           << rwyno << " passed to GetReverseRunwayNo(...)");
+       }
+    }
+    return((string)buf);
+}
+
+
 // search for the specified apt id
 bool FGRunways::search( const string& aptid, FGRunway* r ) {
     c4_StringProp pID ("ID");
@@ -136,6 +173,7 @@ bool FGRunways::search( const string& aptid, FGRunway* r ) {
 // search for the specified apt id and runway no
 bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
 {
+    string runwayno = rwyno;
     c4_StringProp pID ("ID");
     c4_StringProp pRwy ("Rwy");
     c4_FloatProp pLon ("Longitude");
@@ -153,6 +191,15 @@ bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
     if ( index == -1 ) {
        return false;
     }
+    
+    // standardize input number
+    string tmp = runwayno.substr(1, 1);
+    if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (runwayno.size() == 1)) {
+       tmp = runwayno;
+       runwayno = "0" + tmp;
+        SG_LOG(SG_GENERAL, SG_INFO, "Standardising rwy number from " << tmp
+                                     << " to " << runwayno );
+    }
 
     c4_RowRef row = vRunway->GetAt(index);
     string rowid = (const char *) pID(row);
@@ -160,7 +207,7 @@ bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
     while ( rowid == aptid ) {
         next_index = index + 1;
 
-        if ( rowrwyno == rwyno ) {
+        if ( rowrwyno == runwayno ) {
             r->id =      (const char *) pID(row);
             r->rwy_no =  (const char *) pRwy(row);
             r->lon =     (double) pLon(row);
@@ -174,11 +221,31 @@ bool FGRunways::search( const string& aptid, const string& rwyno, FGRunway* r )
 
             return true;
         }
+       
+       // Search again with the other-end runway number
+       // Remember we have to munge the heading and rwy_no results if this one matches
+       rowrwyno = GetReverseRunwayNo(rowrwyno);
+       // cout << "New rowrwyno = " << rowrwyno << '\n';
+       if ( rowrwyno == runwayno ) {
+           r->id =      (const char *) pID(row);
+           r->rwy_no =  rowrwyno;
+           r->lon =     (double) pLon(row);
+           r->lat =     (double) pLat(row);
+           r->heading = (double) pHdg(row) + 180.0;
+           r->length =  (double) pLen(row);
+           r->width =   (double) pWid(row);
+           r->surface_flags = (const char *) pSurf(row);
+           r->end1_flags =    (const char *) pEnd2(row);
+           r->end2_flags =    (const char *) pEnd1(row);
+           // I've swapped the end flags as well 
+           
+           return true;
+       }
 
         index++;
-        c4_RowRef row = vRunway->GetAt(index);
-        string rowid = (const char *) pID(row);
-        string rowrwyno = (const char *) pRwy(row);
+        row = vRunway->GetAt(index);
+        rowid = (const char *) pID(row);
+        rowrwyno = (const char *) pRwy(row);
     }
 
     return false;
@@ -238,69 +305,19 @@ bool FGRunways::next( FGRunway* r ) {
 bool FGRunways::search( const string& aptid, const int tgt_hdg,
                         FGRunway* runway )
 {
-    FGRunway r;
-    double found_dir = 0.0;  
-    if ( !search( aptid, &r ) ) {
-       SG_LOG( SG_GENERAL, SG_ALERT,
-                "Failed to find " << aptid << " in database." );
-       return false;
-    }
-    
-    double diff;
-    double min_diff = 360.0;
-    
-    while ( r.id == aptid ) {
-       // forward direction
-       diff = tgt_hdg - r.heading;
-       while ( diff < -180.0 ) { diff += 360.0; }
-       while ( diff >  180.0 ) { diff -= 360.0; }
-       diff = fabs(diff);
-        // SG_LOG( SG_GENERAL, SG_INFO,
-        //        "Runway " << r.rwy_no << " heading = " << r.heading <<
-        //        " diff = " << diff );
-       if ( diff < min_diff ) {
-           min_diff = diff;
-           runway = &r;
-           found_dir = 0;
-       }
-       
-       // reverse direction
-       diff = tgt_hdg - r.heading - 180.0;
-       while ( diff < -180.0 ) { diff += 360.0; }
-       while ( diff >  180.0 ) { diff -= 360.0; }
-       diff = fabs(diff);
-        // SG_LOG( SG_GENERAL, SG_INFO,
-        //        "Runway -" << r.rwy_no << " heading = " <<
-        //        r.heading + 180.0 <<
-        //        " diff = " << diff );
-       if ( diff < min_diff ) {
-           min_diff = diff;
-           runway = &r;
-           found_dir = 180.0;
-       }
-       
-       next( &r );
-    }
-    
-    // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << runway->rwy_no
-    //        << " + " << found_dir );
-    
-    double heading = runway->heading + found_dir;
-    while ( heading >= 360.0 ) { heading -= 360.0; }
-    runway->heading = heading;
-
-    return true;
+    string rwyNo = search(aptid, tgt_hdg);
+    return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
 }
 
 
 // Return the runway number of the runway closest to a given heading
 string FGRunways::search( const string& aptid, const int tgt_hdg ) {
     FGRunway r;
+    FGRunway tmp_r;    
     string rn;
     double found_dir = 0.0;  
  
-    if ( !search( aptid, &r ) ) {
+    if ( !search( aptid, &tmp_r ) ) {
        SG_LOG( SG_GENERAL, SG_ALERT,
                 "Failed to find " << aptid << " in database." );
        return "NN";
@@ -309,7 +326,9 @@ string FGRunways::search( const string& aptid, const int tgt_hdg ) {
     double diff;
     double min_diff = 360.0;
     
-    while ( r.id == aptid ) {
+    while ( tmp_r.id == aptid ) {
+       r = tmp_r;
+       
        // forward direction
        diff = tgt_hdg - r.heading;
        while ( diff < -180.0 ) { diff += 360.0; }
@@ -339,22 +358,16 @@ string FGRunways::search( const string& aptid, const int tgt_hdg ) {
            found_dir = 180.0;
        }
        
-       next( &r );
+       next( &tmp_r );
     }
     
     // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
     //        << " + " << found_dir );
-    // rn = r.rwy_no;
+    rn = r.rwy_no;
     // cout << "In search, rn = " << rn << endl;
     if ( found_dir == 180 ) {
-       int irn = atoi(rn.c_str());
-       irn += 18;
-       if ( irn > 36 ) {
-           irn -= 36;
-       }
-       char buf[4];            // 2 chars + string terminator + 1 for safety
-       sprintf(buf, "%i", irn);
-       rn = buf;
+       rn = GetReverseRunwayNo(rn);
+       //cout << "New rn = " << rn << '\n';
     }  
     
     return rn;