]> git.mxchange.org Git - flightgear.git/blobdiff - src/Airports/runways.cxx
Merge branch 'torsten/metar'
[flightgear.git] / src / Airports / runways.cxx
index 6450bcd78019642b1611be6ca360e169b87a2291..3eb853823e4d39f0927fcba599c71f3d08750217 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 <cstdio>              // sprintf()
+#include <cstdlib>             // atoi()
 
 #include <simgear/compiler.h>
 
-#include <simgear/debug/logstream.hxx>
-#include <simgear/misc/sgstream.hxx>
+#include <simgear/props/props.hxx>
 
-#include STL_STRING
-#include STL_IOSTREAM
-#include <map>
+#include <string>
 
 #include "runways.hxx"
 
-SG_USING_NAMESPACE(std);
-SG_USING_STD(istream);
-SG_USING_STD(multimap);
+using std::string;
 
-inline istream&
-operator >> ( istream& in, FGRunway& a )
+static std::string cleanRunwayNo(const std::string& aRwyNo)
 {
-    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;
+  if (aRwyNo[0] == 'x') {
+    return std::string(); // no ident for taxiways
+  }
+  
+  string result(aRwyNo);
+  // canonicalise runway ident
+  if ((aRwyNo.size() == 1) || !isdigit(aRwyNo[1])) {
+         result = "0" + aRwyNo;
+  }
+
+  // trim off trailing garbage
+  if (result.size() > 2) {
+    char suffix = toupper(result[2]);
+    if (suffix == 'X') {
+       result = result.substr(0, 2);
     }
-
-    return in;
+  }
+  
+  return result;
 }
 
-
-FGRunwayList::FGRunwayList( const string& file ) {
-    SG_LOG( SG_GENERAL, SG_DEBUG, "Reading runway list: " << file );
-
-    // 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);
-    }
-
-    // skip header line
-    in >> skipeol;
-
-    FGRunway rwy;
-    while ( in ) {
-        in >> rwy;
-        runways.insert(pair<const string, FGRunway>(rwy.id, rwy));
-    }
+FGRunway::FGRunway(FGAirport* aAirport, const string& aIdent,
+                        const SGGeod& aGeod,
+                        const double heading, const double length,
+                        const double width,
+                        const double displ_thresh,
+                        const double stopway,
+                        const int surface_code,
+                        bool reciprocal) :
+  FGRunwayBase(RUNWAY, cleanRunwayNo(aIdent), aGeod, heading, length, width, surface_code, true),
+  _airport(aAirport),
+  _isReciprocal(reciprocal),
+  _reciprocal(NULL),
+  _displ_thresh(displ_thresh),
+  _stopway(stopway),
+  _ils(NULL)
+{
 }
 
+string FGRunway::reverseIdent(const string& aRunwayIdent)
+{
+  // Helipads don't have a seperate number per end
+  if (aRunwayIdent.size() && (aRunwayIdent[0] == 'H' || aRunwayIdent[0] == 'h' || aRunwayIdent[0] == 'x')) {
+    return aRunwayIdent;
+  }
 
-// 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 );
-    }
+  std::string ident(aRunwayIdent);
     
-    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 (wierd!)
-bool FGRunwayList::search( const string& aptid, FGRunway* r ) {
-    runway_map_iterator pos;
-
-    pos = runways.lower_bound(aptid);
-    if ( pos != runways.end() ) {
-        current = pos;
-        *r = pos->second;
-        return true;
+  char buf[4];
+  int rn = atoi(ident.substr(0,2).c_str());
+  rn += 18;
+  while(rn > 36) {
+         rn -= 36;
+  }
+  
+  sprintf(buf, "%02i", rn);
+  if(ident.size() == 3) {
+    char suffix = toupper(ident[2]);
+    if(suffix == 'L') {
+      buf[2] = 'R';
+    } else if (suffix == 'R') {
+      buf[2] = 'L';
     } else {
-        return false;
+      // something else, just copy
+      buf[2] = suffix;
     }
+    
+    buf[3] = 0;
+  }
+  
+  return buf;
 }
 
-
-// search for the specified apt id and runway no
-bool FGRunwayList::search( const string& aptid, const string& rwyno,
-                           FGRunway *r )
+double FGRunway::score(double aLengthWt, double aWidthWt, double aSurfaceWt) const
 {
-    // standardize input number
-    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 );
-    }
-    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 ) {
-            current = pos;
-            *r = pos->second;
-            return true;
-        } 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;
-            return true;
-        }
-    }
-
-    return false;
-}
-
-
-// (wierd!)
-FGRunway FGRunwayList::search( const string& aptid ) {
-    FGRunway a;
-    search( aptid, &a );
-    return a;
+  int surface = 1;
+  if (_surface_code == 12 || _surface_code == 5) // dry lakebed & gravel
+    surface = 2;
+  else if (_surface_code == 1 || _surface_code == 2) // asphalt & concrete
+    surface = 3;
+    
+  return _length * aLengthWt + _width * aWidthWt + surface * aSurfaceWt + 1e-20;
 }
 
-
-// Return the runway closest to a given heading
-bool FGRunwayList::search( const string& aptid, const int tgt_hdg,
-                           FGRunway *runway )
+SGGeod FGRunway::begin() const
 {
-    string rwyNo = search(aptid, tgt_hdg);
-    return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
+  return pointOnCenterline(0.0);
 }
 
-
-// Return the runway number of the runway closest to a given heading
-string FGRunwayList::search( const string& aptid, const int tgt_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 );
-    }
-    
-    // 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;
+SGGeod FGRunway::end() const
+{
+  return pointOnCenterline(lengthM());
 }
 
-
-bool FGRunwayList::next( FGRunway* runway ) {
-    ++current;
-    if ( current != runways.end() ) {
-        *runway = current->second;
-        return true;
-    } else {
-        return false;
-    }
+SGGeod FGRunway::threshold() const
+{
+  return pointOnCenterline(_displ_thresh * SG_FEET_TO_METER);
 }
 
-
-FGRunway FGRunwayList::next() {
-    FGRunway result;
-
-    ++current;
-    if ( current != runways.end() ) {
-        result = current->second;
-    }
-
-    return result;
+void FGRunway::processThreshold(SGPropertyNode* aThreshold)
+{
+  assert(ident() == aThreshold->getStringValue("rwy"));
+  
+  double lon = aThreshold->getDoubleValue("lon"),
+    lat = aThreshold->getDoubleValue("lat");
+  SGGeod newThreshold(SGGeod::fromDegM(lon, lat, mPosition.getElevationM()));
+  
+  _heading = aThreshold->getDoubleValue("hdg-deg");
+  _displ_thresh = aThreshold->getDoubleValue("displ-m") * SG_METER_TO_FEET;
+  _stopway = aThreshold->getDoubleValue("stopw-m") * SG_METER_TO_FEET;
+  
+  // compute the new runway center, based on the threshold lat/lon and length,
+  double offsetFt = (0.5 * _length);
+  SGGeod newCenter;
+  double dummy;
+  SGGeodesy::direct(newThreshold, _heading, offsetFt * SG_FEET_TO_METER, newCenter, dummy);
+  mPosition = newCenter;
+} 
+
+void FGRunway::setReciprocalRunway(FGRunway* other)
+{
+  assert(_reciprocal==NULL);
+  assert((other->_reciprocal == NULL) || (other->_reciprocal == this));
+  
+  _reciprocal = other;
 }
 
-
-// Destructor
-FGRunwayList::~FGRunwayList( void ) {
-}