]> git.mxchange.org Git - flightgear.git/blobdiff - src/Airports/runways.cxx
- export font properties to the property tree again
[flightgear.git] / src / Airports / runways.cxx
index 6450bcd78019642b1611be6ca360e169b87a2291..c1b22452259df4cf70eeb705d176bbb1ea90f505 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Written by Curtis Olson, started August 2000.
 //
-// Copyright (C) 2000  Curtis L. Olson  - curt@flightgear.org
+// Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
 //
 // This program is free software; you can redistribute it and/or
 // modify it under the terms of the GNU General Public License as
@@ -16,7 +16,7 @@
 //
 // 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.
 //
 // $Id$
 
 #  include <config.h>
 #endif
 
-#include <math.h>               // fabs()
-#include <stdio.h>              // sprintf()
+#include <cmath>               // fabs()
+#include <cstdio>              // sprintf()
+#include <cstdlib>             // atoi()
 
 #include <simgear/compiler.h>
-
 #include <simgear/debug/logstream.hxx>
-#include <simgear/misc/sgstream.hxx>
+#include <Main/fg_props.hxx>
 
 #include STL_STRING
-#include STL_IOSTREAM
 #include <map>
 
 #include "runways.hxx"
@@ -43,63 +42,72 @@ SG_USING_NAMESPACE(std);
 SG_USING_STD(istream);
 SG_USING_STD(multimap);
 
-inline istream&
-operator >> ( istream& in, FGRunway& a )
-{
-    string type;
-    int tmp;
-
-    in >> a.type;
-    if ( a.type == "R" ) {
-        in >> a.id >> a.rwy_no >> a.lat >> a.lon >> a.heading
-           >> a.length >> a.width >> a.surface_flags >> a.end1_flags
-           >> tmp >> tmp >> a.end2_flags >> tmp >> tmp;
-    } else if ( a.type == "T" ) {
-        // in >> a.id >> a.rwy_no >> a.lat >> a.lon >> a.heading
-        //    >> a.length >> a.width >> a.surface_flags;
-        in >> skipeol;
-    } else {
-        in >> skipeol;
-    }
 
-    return in;
-}
-
-
-FGRunwayList::FGRunwayList( const string& file ) {
-    SG_LOG( SG_GENERAL, SG_DEBUG, "Reading runway list: " << file );
+// add an entry to the list
+void FGRunwayList::add( const string& id, const string& rwy_no,
+                        const double longitude, const double latitude,
+                        const double heading, const double length,
+                        const double width,
+                        const double displ_thresh1, const double displ_thresh2,
+                        const double stopway1, const double stopway2,
+                        const string& lighting_flags, const int surface_code,
+                        const string& shoulder_code, const int marking_code,
+                        const double smoothness, const bool dist_remaining )
+{
+    FGRunway rwy;
 
-    // open the specified file for reading
-    sg_gzifstream in( file );
-    if ( !in.is_open() ) {
-        SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << file );
-       exit(-1);
+    rwy._id = id;
+    rwy._rwy_no = rwy_no;
+    // strip trailing "x" if it exists in runway number
+    string tmp = rwy._rwy_no.substr(2, 1);
+    if ( tmp == "x" ) {
+        rwy._rwy_no = rwy._rwy_no.substr(0, 2);
     }
 
-    // skip header line
-    in >> skipeol;
-
-    FGRunway rwy;
-    while ( in ) {
-        in >> rwy;
-        runways.insert(pair<const string, FGRunway>(rwy.id, rwy));
+    rwy._lon = longitude;
+    rwy._lat = latitude;
+    rwy._heading = heading;
+    rwy._length = length;
+    rwy._width = width;
+    rwy._displ_thresh1 = displ_thresh1;
+    rwy._displ_thresh2 = displ_thresh2;
+    rwy._stopway1 = stopway1;
+    rwy._stopway2 = stopway2;
+
+    rwy._lighting_flags = lighting_flags;
+    rwy._surface_code = surface_code;
+    rwy._shoulder_code = shoulder_code;
+    rwy._marking_code = marking_code;
+    rwy._smoothness = smoothness;
+    rwy._dist_remaining = dist_remaining;
+
+    if ( rwy_no[0] == 'x' ) {
+        rwy._type = "taxiway";
+    } else {
+        rwy._type = "runway";
     }
+    runways.insert(pair<const string, FGRunway>(rwy._id, rwy));
 }
 
 
 // Return reverse rwy number
 // eg 01 -> 19
 // 03L -> 21R
-static string GetReverseRunwayNo(string rwyno) {       
+static string GetReverseRunwayNo(string& rwyno) {      
     // cout << "Original rwyno = " << rwyNo << '\n';
     
+    // Helipads don't have a seperate number per end
+    if(rwyno.size() && (rwyno[0] == 'H' || rwyno[0] == 'h' || rwyno[0] == 'x')) {
+       return rwyno;
+    }
+    
     // 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 );
+        SG_LOG( SG_GENERAL, SG_INFO,
+                "Standardising rwy number from " << tmp << " to " << rwyno );
     }
     
     char buf[4];
@@ -119,12 +127,15 @@ static string GetReverseRunwayNo(string rwyno) {
        } else if (rwyno.substr(2,1) == "C") {
            buf[2] = 'C';
            buf[3] = '\0';
+       } else if (rwyno.substr(2,1) == "T") {
+           buf[2] = 'T';
+           buf[3] = '\0';
        } else {
            SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
            << rwyno << " passed to GetReverseRunwayNo(...)");
        }
     }
-    return((string)buf);
+    return(buf);
 }
 
 
@@ -147,36 +158,39 @@ bool FGRunwayList::search( const string& aptid, FGRunway* r ) {
 bool FGRunwayList::search( const string& aptid, const string& rwyno,
                            FGRunway *r )
 {
-    // standardize input number
+    string revrwyno = "";
     string runwayno = rwyno;
-    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 );
+    if ( runwayno.length() ) {
+        // 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 );
+        }
+        revrwyno = GetReverseRunwayNo(runwayno);
     }
-    string revrwyno = GetReverseRunwayNo(runwayno);
-
     runway_map_iterator pos;
     for ( pos = runways.lower_bound( aptid );
           pos != runways.upper_bound( aptid ); ++pos)
     {
-        if ( pos->second.rwy_no == runwayno ) {
+        if ( pos->second._rwy_no == runwayno ) {
             current = pos;
             *r = pos->second;
             return true;
-        } else if ( pos->second.rwy_no == revrwyno ) {
+        } else if ( pos->second._rwy_no == revrwyno ) {
             // Search again with the other-end runway number.
             // Remember we have to munge the heading and rwy_no
             // results if this one matches
             current = pos;
             *r = pos->second;
-           r->rwy_no = revrwyno;
-            r->heading += 180.0;
-            string tmp = r->end1_flags;
-            r->end1_flags = r->end2_flags;
-            r->end2_flags = tmp;
+            // NOTE - matching revrwyno implies that runwayno was
+            // actually correct.
+            r->_rwy_no = runwayno;
+            r->_heading += 180.0;
             return true;
         }
     }
@@ -203,66 +217,78 @@ bool FGRunwayList::search( const string& aptid, const int tgt_hdg,
 
 
 // Return the runway number of the runway closest to a given heading
-string FGRunwayList::search( const string& aptid, const int tgt_hdg ) {
+string FGRunwayList::search( const string& aptid, const int hdg ) {
+    //SG_LOG(SG_GENERAL, SG_ALERT, "searching runway for " << aptid
+    //        << " with target heading " << hdg);
+
     FGRunway r;
-    FGRunway tmp_r;    
-    string rn;
-    double found_dir = 0.0;  
-    if ( !search( aptid, &tmp_r ) ) {
-       SG_LOG( SG_GENERAL, SG_ALERT,
-                "Failed to find " << aptid << " in database." );
-       return "NN";
-    }
-    
-    double diff;
-    double min_diff = 360.0;
-    
-    while ( tmp_r.id == aptid ) {
-       r = tmp_r;
-       
-       // 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;
-           rn = r.rwy_no;
-           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;
-           rn = r.rwy_no;
-           found_dir = 180.0;
-       }
-       
-       next( &tmp_r );
+    if (!search(aptid, &r)) {
+        SG_LOG(SG_GENERAL, SG_ALERT, "Failed to find "
+                << aptid << " in database.");
+        return "NN";
     }
-    
-    // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r.rwy_no
-    //        << " + " << found_dir );
-    rn = r.rwy_no;
-    // cout << "In search, rn = " << rn << endl;
-    if ( found_dir == 180 ) {
-       rn = GetReverseRunwayNo(rn);
-       //cout << "New rn = " << rn << '\n';
-    }  
-    
-    return rn;
+
+    SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
+    double lenwgt = param->getDoubleValue("length-weight", 0.01);
+    double widwgt = param->getDoubleValue("width-weight", 0.01);
+    double surfwgt = param->getDoubleValue("surface-weight", 10);
+    double devwgt = param->getDoubleValue("deviation-weight", 1);
+
+    FGRunway best;
+    double max = 0.0;
+    bool reversed = false;
+
+    do {
+        if (r._id != aptid)
+            break;
+        if (r._type != "runway")
+            continue;
+
+        int surface = 1;
+        if (r._surface_code == 12 || r._surface_code == 5) // dry lakebed & gravel
+            surface = 2;
+        else if (r._surface_code == 1 || r._surface_code == 2) // asphalt & concrete
+            surface = 3;
+
+        double quality, bad, diff;
+        double good = lenwgt * r._length + widwgt * r._width + surfwgt * surface + 1e-20;
+
+        // this side
+        diff = hdg - r._heading;
+        while (diff < -180)
+            diff += 360;
+        while (diff >= 180)
+            diff -= 360;
+        bad = fabs(devwgt * diff) + 1e-20;
+
+        quality = good / bad;
+        //SG_LOG(SG_GENERAL, SG_ALERT, "  runway " << r._rwy_no <<  " -> " << quality);
+        if (quality > max) {
+            max = quality;
+            best = r;
+            reversed = false;
+        }
+
+        // other side
+        diff = hdg - r._heading - 180;
+        while (diff < -180)
+            diff += 360;
+        while (diff >= 180)
+            diff -= 360;
+        bad = fabs(devwgt * diff) + 1e-20;
+
+        quality = good / bad;
+        //SG_LOG(SG_GENERAL, SG_ALERT, "  runway " << GetReverseRunwayNo(r._rwy_no)
+        //        <<  " -> " << quality);
+        if (quality > max) {
+            max = quality;
+            best = r;
+            reversed = true;
+        }
+
+    } while (next(&r));
+
+    return reversed ? GetReverseRunwayNo(best._rwy_no) : best._rwy_no;
 }