# include <config.h>
#endif
-// #include <sys/types.h> // for gdbm open flags
-// #include <sys/stat.h> // for gdbm open flags
+#include <math.h> // fabs()
+#include <stdio.h> // sprintf()
#include <simgear/compiler.h>
SG_USING_NAMESPACE(std);
+#ifndef _MSC_VER
+#define NDEBUG // MSVC needs this
+#endif // !_MSC_VER
-FGRunway::FGRunway() {
-}
+#include <mk4.h>
+#include <mk4str.h>
+
+#ifndef _MSC_VER
+#undef NDEBUG
+#endif // !_MSC_VER
+
+#ifdef SG_HAVE_STD_INCLUDES
+# include <istream>
+#elif defined( SG_HAVE_NATIVE_SGI_COMPILERS )
+# include <iostream.h>
+#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 )
+{
+ int tmp;
-FGRunway::~FGRunway() {
+ return in >> 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;
}
vRunway = new c4_View;
*vRunway =
- storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
+ storage->GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
next_index = 0;
}
c4_StringProp pEnd2 ("End2Flags");
int index = vRunway->Find(pID[aptid.c_str()]);
- cout << "index = " << index << endl;
+ // cout << "index = " << index << endl;
if ( index == -1 ) {
return false;
c4_StringProp pEnd2 ("End2Flags");
int index = vRunway->Find(pID[aptid.c_str()]);
- cout << "index = " << index << endl;
+ // cout << "index = " << index << endl;
if ( index == -1 ) {
return false;
}
+// Return the runway closest to a given heading
+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;
+}
+
+
+// Return the runway number of the runway closest to a given heading
+string FGRunways::search( const string& aptid, const int tgt_hdg ) {
+ FGRunway r;
+ string rn;
+ double found_dir = 0.0;
+
+ if ( !search( aptid, &r ) ) {
+ SG_LOG( SG_GENERAL, SG_ALERT,
+ "Failed to find " << aptid << " in database." );
+ return "NN";
+ }
+
+ 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;
+ 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( &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 ) {
+ 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;
+ }
+
+ return rn;
+}
+
+
// Destructor
FGRunways::~FGRunways( void ) {
delete storage;
// save the data in gdbm format
bool FGRunwaysUtil::dump_mk4( const string& file ) {
-
// open database for writing
c4_Storage storage( file.c_str(), true );
// Start with an empty view of the proper structure.
c4_View vRunway =
- storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:F,End2Flags:F]");
+ storage.GetAs("runway[ID:S,Rwy:S,Longitude:F,Latitude:F,Heading:F,Length:F,Width:F,SurfaceFlags:S,End1Flags:S,End2Flags:S]");
c4_Row row;