1
0
Fork 0

James Turner:

By way of example, here's a patch to make the position init code (in fg_init.cxx) cleaner, partly thanks to the FGPositioned changes. It reduces the file size by 200 lines - virtually all of which was copy-and-paste. Once the remaining class (FGAirport) is converted to inherit FGPositioned, all the future patches should be like this - touching one or a few files at most.

This factors the start-offset logic out into a helper, which also does the final property setting (which has to happen on both the /preset and 'real' values). Using the accessors in FGPositioned, and the offset helper, a couple of cases become trivial (fix and nav) and others become much simpler.
This commit is contained in:
ehofman 2008-09-13 08:06:15 +00:00
parent bb2b03c7e3
commit fe020badf0

View file

@ -686,45 +686,6 @@ bool fgInitConfig ( int argc, char **argv ) {
return true; return true;
} }
#if 0
//
// This function is never used, but maybe useful in the future ???
//
// Preset lon/lat given an airport id
static bool fgSetPosFromAirportID( const string& id ) {
FGAirport *a;
// double lon, lat;
SG_LOG( SG_GENERAL, SG_INFO,
"Attempting to set starting position from airport code " << id );
if ( fgFindAirportID( id, &a ) ) {
// presets
fgSetDouble("/sim/presets/longitude-deg", a->longitude );
fgSetDouble("/sim/presets/latitude-deg", a->latitude );
// other code depends on the actual postition being set so set
// that as well
fgSetDouble("/position/longitude-deg", a->longitude );
fgSetDouble("/position/latitude-deg", a->latitude );
SG_LOG( SG_GENERAL, SG_INFO,
"Position for " << id << " is (" << a->longitude
<< ", " << a->latitude << ")" );
return true;
} else {
return false;
}
}
#endif
// Set current tower position lon/lat given an airport id // Set current tower position lon/lat given an airport id
static bool fgSetTowerPosFromAirportID( const string& id) { static bool fgSetTowerPosFromAirportID( const string& id) {
const FGAirport *a = fgFindAirportID( id); const FGAirport *a = fgFindAirportID( id);
@ -752,6 +713,39 @@ void fgInitTowerLocationListener() {
->addChangeListener( new FGTowerLocationListener(), true ); ->addChangeListener( new FGTowerLocationListener(), true );
} }
static void fgApplyStartOffset(const SGGeod& aStartPos, double aHeading, double aTargetHeading = HUGE_VAL)
{
SGGeod startPos(aStartPos);
if (aTargetHeading == HUGE_VAL) {
aTargetHeading = aHeading;
}
if ( fabs( fgGetDouble("/sim/presets/offset-distance-nm") ) > SG_EPSILON ) {
double offsetDistance = fgGetDouble("/sim/presets/offset-distance-nm");
offsetDistance *= SG_NM_TO_METER;
double offsetAzimuth = aHeading;
if ( fabs(fgGetDouble("/sim/presets/offset-azimuth-deg")) > SG_EPSILON ) {
offsetAzimuth = fgGetDouble("/sim/presets/offset-azimuth-deg");
aHeading = aTargetHeading;
}
SGGeod offset;
double az2; // dummy
SGGeodesy::direct(startPos, offsetAzimuth + 180, offsetDistance, offset, az2);
startPos = offset;
}
// presets
fgSetDouble("/sim/presets/longitude-deg", startPos.getLongitudeDeg() );
fgSetDouble("/sim/presets/latitude-deg", startPos.getLatitudeDeg() );
fgSetDouble("/sim/presets/heading-deg", aHeading );
// other code depends on the actual values being set ...
fgSetDouble("/position/longitude-deg", startPos.getLongitudeDeg() );
fgSetDouble("/position/latitude-deg", startPos.getLatitudeDeg() );
fgSetDouble("/orientation/heading-deg", aHeading );
}
// Set current_options lon/lat given an airport id and heading (degrees) // Set current_options lon/lat given an airport id and heading (degrees)
static bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) { static bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
if ( id.empty() ) if ( id.empty() )
@ -767,46 +761,8 @@ static bool fgSetPosFromAirportIDandHdg( const string& id, double tgt_hdg ) {
FGRunway* r = apt->findBestRunwayForHeading(tgt_hdg); FGRunway* r = apt->findBestRunwayForHeading(tgt_hdg);
fgSetString("/sim/atc/runway", r->ident().c_str()); fgSetString("/sim/atc/runway", r->ident().c_str());
double lat2, lon2, az2; SGGeod startPos = r->pointOnCenterline(fgGetDouble("/sim/airport/runways/start-offset-m", 5.0));
double heading = r->headingDeg(); fgApplyStartOffset(startPos, r->headingDeg(), tgt_hdg);
double azimuth = heading + 180.0;
while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
geo_direct_wgs_84 ( 0, r->latitude(), r->longitude(), azimuth, r->lengthM() * 0.5
- fgGetDouble("/sim/airport/runways/start-offset-m", 5.0),
&lat2, &lon2, &az2 );
if ( fabs( fgGetDouble("/sim/presets/offset-distance-nm") ) > SG_EPSILON ) {
double olat, olon;
double odist = fgGetDouble("/sim/presets/offset-distance-nm");
odist *= SG_NM_TO_METER;
double oaz = azimuth;
if ( fabs(fgGetDouble("/sim/presets/offset-azimuth-deg")) > SG_EPSILON ) {
oaz = fgGetDouble("/sim/presets/offset-azimuth-deg") + 180;
heading = tgt_hdg;
}
while ( oaz >= 360.0 ) { oaz -= 360.0; }
geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
lat2=olat;
lon2=olon;
}
// presets
fgSetDouble("/sim/presets/longitude-deg", lon2 );
fgSetDouble("/sim/presets/latitude-deg", lat2 );
fgSetDouble("/sim/presets/heading-deg", heading );
// other code depends on the actual values being set ...
fgSetDouble("/position/longitude-deg", lon2 );
fgSetDouble("/position/latitude-deg", lat2 );
fgSetDouble("/orientation/heading-deg", heading );
SG_LOG( SG_GENERAL, SG_INFO,
"Position for " << id << " is ("
<< lon2 << ", "
<< lat2 << ") new heading is "
<< heading );
return true; return true;
} }
@ -818,8 +774,7 @@ static bool fgSetPosFromAirportIDandParkpos( const string& id, const string& par
// can't see an easy way around this const_cast at the moment // can't see an easy way around this const_cast at the moment
FGAirport* apt = const_cast<FGAirport*>(fgFindAirportID(id)); FGAirport* apt = const_cast<FGAirport*>(fgFindAirportID(id));
if (!apt) { if (!apt) {
SG_LOG( SG_GENERAL, SG_ALERT, SG_LOG( SG_GENERAL, SG_ALERT, "Failed to find airport " << id );
"Failed to find airport " << id );
return false; return false;
} }
FGAirportDynamics* dcs = apt->getDynamics(); FGAirportDynamics* dcs = apt->getDynamics();
@ -839,27 +794,9 @@ static bool fgSetPosFromAirportIDandParkpos( const string& id, const string& par
return false; return false;
} }
FGParking* parking = dcs->getParking(park_index); FGParking* parking = dcs->getParking(park_index);
fgApplyStartOffset(
double lat = parking->getLatitude(); SGGeod::fromDeg(parking->getLongitude(), parking->getLatitude()),
double lon = parking->getLongitude(); parking->getHeading());
double hdg = parking->getHeading();
// presets
fgSetDouble("/sim/presets/longitude-deg", lon );
fgSetDouble("/sim/presets/latitude-deg", lat );
fgSetDouble("/sim/presets/heading-deg", hdg );
// other code depends on the actual values being set ...
fgSetDouble("/position/longitude-deg", lon );
fgSetDouble("/position/latitude-deg", lat );
fgSetDouble("/orientation/heading-deg", hdg );
SG_LOG( SG_GENERAL, SG_INFO,
"Position for " << id << " is ("
<< lon << ", "
<< lat << ") new heading is "
<< hdg );
return true; return true;
} }
@ -889,49 +826,8 @@ static bool fgSetPosFromAirportIDandRwy( const string& id, const string& rwy, bo
FGRunway* r(apt->getRunwayByIdent(rwy)); FGRunway* r(apt->getRunwayByIdent(rwy));
fgSetString("/sim/atc/runway", r->ident().c_str()); fgSetString("/sim/atc/runway", r->ident().c_str());
SGGeod startPos = r->pointOnCenterline( fgGetDouble("/sim/airport/runways/start-offset-m", 5.0));
double lat2, lon2, az2; fgApplyStartOffset(startPos, r->headingDeg());
double heading = r->headingDeg();
double azimuth = heading + 180.0;
while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
geo_direct_wgs_84 ( 0, r->latitude(), r->longitude(), azimuth, r->lengthM() * 0.5
- fgGetDouble("/sim/airport/runways/start-offset-m", 5.0),
&lat2, &lon2, &az2 );
if ( fabs( fgGetDouble("/sim/presets/offset-distance-nm") ) > SG_EPSILON )
{
double olat, olon;
double odist = fgGetDouble("/sim/presets/offset-distance-nm");
odist *= SG_NM_TO_METER;
double oaz = azimuth;
if ( fabs(fgGetDouble("/sim/presets/offset-azimuth-deg")) > SG_EPSILON )
{
oaz = fgGetDouble("/sim/presets/offset-azimuth-deg") + 180;
heading = fgGetDouble("/sim/presets/heading-deg");
}
while ( oaz >= 360.0 ) { oaz -= 360.0; }
geo_direct_wgs_84 ( 0, lat2, lon2, oaz, odist, &olat, &olon, &az2 );
lat2=olat;
lon2=olon;
}
// presets
fgSetDouble("/sim/presets/longitude-deg", lon2 );
fgSetDouble("/sim/presets/latitude-deg", lat2 );
fgSetDouble("/sim/presets/heading-deg", heading );
// other code depends on the actual values being set ...
fgSetDouble("/position/longitude-deg", lon2 );
fgSetDouble("/position/latitude-deg", lat2 );
fgSetDouble("/orientation/heading-deg", heading );
SG_LOG( SG_GENERAL, SG_INFO,
"Position for " << id << " is ("
<< lon2 << ", "
<< lat2 << ") new heading is "
<< heading );
return true; return true;
} }
@ -985,48 +881,14 @@ static bool fgSetPosFromNAV( const string& id, const double& freq ) {
FGNavRecord *nav FGNavRecord *nav
= globals->get_navlist()->findByIdentAndFreq( id.c_str(), freq ); = globals->get_navlist()->findByIdentAndFreq( id.c_str(), freq );
// set initial position from runway and heading if (!nav) {
if ( nav != NULL ) { SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for "
<< id << ":" << freq ); << id << ":" << freq );
return false;
double lon = nav->get_lon(); }
double lat = nav->get_lat();
fgApplyStartOffset(nav->geod(), fgGetDouble("/sim/presets/heading-deg"));
if ( fabs( fgGetDouble("/sim/presets/offset-distance-nm") ) > SG_EPSILON ) return true;
{
double odist = fgGetDouble("/sim/presets/offset-distance-nm")
* SG_NM_TO_METER;
double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth-deg"))
+ 180.0;
while ( oaz >= 360.0 ) { oaz -= 360.0; }
double olat, olon, az2;
geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
lat = olat;
lon = olon;
}
// presets
fgSetDouble("/sim/presets/longitude-deg", lon );
fgSetDouble("/sim/presets/latitude-deg", lat );
// other code depends on the actual values being set ...
fgSetDouble("/position/longitude-deg", lon );
fgSetDouble("/position/latitude-deg", lat );
fgSetDouble("/orientation/heading-deg",
fgGetDouble("/sim/presets/heading-deg") );
SG_LOG( SG_GENERAL, SG_INFO,
"Position for " << id << ":" << freq << " is ("
<< lon << ", "<< lat << ")" );
return true;
} else {
SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
<< id << ":" << freq );
return false;
}
} }
// Set current_options lon/lat given an aircraft carrier id // Set current_options lon/lat given an aircraft carrier id
@ -1073,51 +935,18 @@ static bool fgSetPosFromCarrier( const string& carrier, const string& posid ) {
} }
// Set current_options lon/lat given an airport id and heading (degrees) // Set current_options lon/lat given an airport id and heading (degrees)
static bool fgSetPosFromFix( const string& id ) { static bool fgSetPosFromFix( const string& id )
FGFix* fix; {
FGFix* fix;
// set initial position from runway and heading
if ( globals->get_fixlist()->query( id.c_str(), fix ) ) { // set initial position from runway and heading
SG_LOG( SG_GENERAL, SG_INFO, "Attempting to set starting position for " if ( !globals->get_fixlist()->query( id.c_str(), fix ) ) {
<< id ); SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = " << id );
return false;
double lon = fix->get_lon(); }
double lat = fix->get_lat();
fgApplyStartOffset(fix->geod(), fgGetDouble("/sim/presets/heading-deg"));
if ( fabs( fgGetDouble("/sim/presets/offset-distance-nm") ) > SG_EPSILON ) return true;
{
double odist = fgGetDouble("/sim/presets/offset-distance-nm")
* SG_NM_TO_METER;
double oaz = fabs(fgGetDouble("/sim/presets/offset-azimuth-deg"))
+ 180.0;
while ( oaz >= 360.0 ) { oaz -= 360.0; }
double olat, olon, az2;
geo_direct_wgs_84 ( 0, lat, lon, oaz, odist, &olat, &olon, &az2 );
lat = olat;
lon = olon;
}
// presets
fgSetDouble("/sim/presets/longitude-deg", lon );
fgSetDouble("/sim/presets/latitude-deg", lat );
// other code depends on the actual values being set ...
fgSetDouble("/position/longitude-deg", lon );
fgSetDouble("/position/latitude-deg", lat );
fgSetDouble("/orientation/heading-deg",
fgGetDouble("/sim/presets/heading-deg") );
SG_LOG( SG_GENERAL, SG_INFO,
"Position for " << id << " is ("
<< lon << ", "<< lat << ")" );
return true;
} else {
SG_LOG( SG_GENERAL, SG_ALERT, "Failed to locate NAV = "
<< id );
return false;
}
} }
@ -1163,7 +992,7 @@ fgInitNav ()
SG_LOG( SG_GENERAL, SG_ALERT, SG_LOG( SG_GENERAL, SG_ALERT,
"Problems loading one or more navigational database" ); "Problems loading one or more navigational database" );
} }
SG_LOG(SG_GENERAL, SG_INFO, " Fixes"); SG_LOG(SG_GENERAL, SG_INFO, " Fixes");
SGPath p_fix( globals->get_fg_root() ); SGPath p_fix( globals->get_fg_root() );
p_fix.append( "Navaids/fix.dat" ); p_fix.append( "Navaids/fix.dat" );