2009-10-11 11:37:13 +00:00
|
|
|
// procedure.cxx - define route storing an approach, arrival or departure procedure
|
|
|
|
// Written by James Turner, started 2009.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2009 Curtis L. Olson
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
#include "procedure.hxx"
|
|
|
|
|
2010-10-20 10:48:06 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <algorithm> // for reverse_copy
|
|
|
|
|
2009-10-11 11:37:13 +00:00
|
|
|
#include <simgear/structure/exception.hxx>
|
|
|
|
|
2013-03-04 15:31:37 +00:00
|
|
|
#include <Airports/runways.hxx>
|
2009-10-11 11:37:13 +00:00
|
|
|
#include <Navaids/waypoint.hxx>
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
namespace flightgear
|
|
|
|
{
|
2012-04-24 21:55:30 +00:00
|
|
|
|
|
|
|
static void markWaypoints(WayptVec& wps, WayptFlag f)
|
|
|
|
{
|
|
|
|
for (unsigned int i=0; i<wps.size(); ++i) {
|
|
|
|
wps[i]->setFlag(f, true);
|
|
|
|
}
|
|
|
|
}
|
2009-10-11 11:37:13 +00:00
|
|
|
|
|
|
|
Procedure::Procedure(const string& aIdent) :
|
|
|
|
_ident(aIdent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
Approach::Approach(const string& aIdent, ProcedureType ty) :
|
|
|
|
Procedure(aIdent),
|
|
|
|
_type(ty)
|
2009-10-11 11:37:13 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2012-11-12 21:30:28 +00:00
|
|
|
|
|
|
|
Approach* Approach::createTempApproach(const std::string& aIdent, FGRunway* aRunway, const WayptVec& aPath)
|
|
|
|
{
|
|
|
|
Approach* app = new Approach(aIdent, PROCEDURE_APPROACH_RNAV);
|
|
|
|
app->setRunway(aRunway);
|
|
|
|
app->setPrimaryAndMissed(aPath, WayptVec());
|
|
|
|
return app;
|
|
|
|
}
|
2009-10-11 11:37:13 +00:00
|
|
|
|
|
|
|
void Approach::setRunway(FGRunwayRef aRwy)
|
|
|
|
{
|
|
|
|
_runway = aRwy;
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
FGAirport* Approach::airport() const
|
|
|
|
{
|
|
|
|
return _runway->airport();
|
|
|
|
}
|
|
|
|
|
|
|
|
RunwayVec Approach::runways() const
|
|
|
|
{
|
|
|
|
RunwayVec r;
|
|
|
|
r.push_back(_runway);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-10-11 11:37:13 +00:00
|
|
|
void Approach::setPrimaryAndMissed(const WayptVec& aPrimary, const WayptVec& aMissed)
|
|
|
|
{
|
|
|
|
_primary = aPrimary;
|
|
|
|
_primary[0]->setFlag(WPT_IAF, true);
|
|
|
|
_primary[_primary.size()-1]->setFlag(WPT_FAF, true);
|
2012-04-24 21:55:30 +00:00
|
|
|
markWaypoints(_primary, WPT_APPROACH);
|
2009-10-11 11:37:13 +00:00
|
|
|
|
|
|
|
_missed = aMissed;
|
|
|
|
|
|
|
|
if (!_missed.empty()) {
|
|
|
|
// mark the first point as the published missed-approach point
|
|
|
|
_missed[0]->setFlag(WPT_MAP, true);
|
2012-04-24 21:55:30 +00:00
|
|
|
markWaypoints(_missed, WPT_MISS);
|
|
|
|
markWaypoints(_missed, WPT_APPROACH);
|
2009-10-11 11:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Approach::addTransition(Transition* aTrans)
|
|
|
|
{
|
|
|
|
WayptRef entry = aTrans->enroute();
|
|
|
|
_transitions[entry] = aTrans;
|
2012-04-24 21:55:30 +00:00
|
|
|
aTrans->mark(WPT_APPROACH);
|
2009-10-11 11:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Approach::route(WayptRef aIAF, WayptVec& aWps)
|
|
|
|
{
|
2012-05-11 16:07:56 +00:00
|
|
|
if (aIAF.valid()) {
|
|
|
|
WptTransitionMap::iterator it;
|
|
|
|
bool haveTrans = false;
|
|
|
|
for (it = _transitions.begin(); it != _transitions.end(); ++it) {
|
|
|
|
Transition* t= it->second;
|
|
|
|
if (t->enroute()->matches(aIAF)) {
|
|
|
|
t->route(aWps);
|
|
|
|
haveTrans = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // of transitions iteration
|
|
|
|
|
|
|
|
if (!haveTrans) {
|
2012-11-23 20:14:40 +00:00
|
|
|
SG_LOG(SG_NAVAID, SG_INFO, "approach " << ident() << " has no transition " <<
|
2012-05-11 16:07:56 +00:00
|
|
|
"for IAF: " << aIAF->ident());
|
|
|
|
return false;
|
2009-10-11 11:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
return routeFromVectors(aWps);
|
2009-10-11 11:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Approach::routeFromVectors(WayptVec& aWps)
|
|
|
|
{
|
|
|
|
aWps.insert(aWps.end(), _primary.begin(), _primary.end());
|
2012-05-15 16:53:30 +00:00
|
|
|
RunwayWaypt* rwy = new RunwayWaypt(_runway, NULL);
|
|
|
|
rwy->setFlag(WPT_APPROACH);
|
|
|
|
aWps.push_back(rwy);
|
2009-10-11 11:37:13 +00:00
|
|
|
aWps.insert(aWps.end(), _missed.begin(), _missed.end());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
bool Approach::isApproach(ProcedureType ty)
|
|
|
|
{
|
|
|
|
return (ty >= PROCEDURE_APPROACH_ILS) && (ty <= PROCEDURE_APPROACH_RNAV);
|
|
|
|
}
|
|
|
|
|
2009-10-11 11:37:13 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
ArrivalDeparture::ArrivalDeparture(const string& aIdent, FGAirport* apt) :
|
|
|
|
Procedure(aIdent),
|
|
|
|
_airport(apt)
|
2009-10-11 11:37:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArrivalDeparture::addRunway(FGRunwayRef aWay)
|
|
|
|
{
|
2012-04-24 21:55:30 +00:00
|
|
|
assert(aWay->airport() == _airport);
|
2009-10-11 11:37:13 +00:00
|
|
|
_runways[aWay] = NULL;
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
bool ArrivalDeparture::isForRunway(const FGRunway* aWay) const
|
2009-10-11 11:37:13 +00:00
|
|
|
{
|
|
|
|
// null runway always passes
|
|
|
|
if (!aWay) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
FGRunwayRef r(const_cast<FGRunway*>(aWay));
|
2012-10-13 15:59:47 +00:00
|
|
|
return (_runways.count(r) > 0);
|
2009-10-11 11:37:13 +00:00
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
RunwayVec ArrivalDeparture::runways() const
|
|
|
|
{
|
|
|
|
RunwayVec r;
|
|
|
|
RunwayTransitionMap::const_iterator it = _runways.begin();
|
|
|
|
for (; it != _runways.end(); ++it) {
|
|
|
|
r.push_back(it->first);
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-10-11 11:37:13 +00:00
|
|
|
void ArrivalDeparture::addTransition(Transition* aTrans)
|
|
|
|
{
|
|
|
|
WayptRef entry = aTrans->enroute();
|
2012-04-24 21:55:30 +00:00
|
|
|
aTrans->mark(flagType());
|
2009-10-11 11:37:13 +00:00
|
|
|
_enrouteTransitions[entry] = aTrans;
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
string_list ArrivalDeparture::transitionIdents() const
|
|
|
|
{
|
|
|
|
string_list r;
|
|
|
|
WptTransitionMap::const_iterator eit;
|
|
|
|
for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
|
|
|
|
r.push_back(eit->second->ident());
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2009-10-11 11:37:13 +00:00
|
|
|
void ArrivalDeparture::addRunwayTransition(FGRunwayRef aWay, Transition* aTrans)
|
|
|
|
{
|
|
|
|
assert(aWay->ident() == aTrans->ident());
|
|
|
|
if (!isForRunway(aWay)) {
|
|
|
|
throw sg_io_exception("adding transition for unspecified runway:" + aWay->ident(), ident());
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
aTrans->mark(flagType());
|
2009-10-11 11:37:13 +00:00
|
|
|
_runways[aWay] = aTrans;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArrivalDeparture::setCommon(const WayptVec& aWps)
|
|
|
|
{
|
|
|
|
_common = aWps;
|
2012-04-24 21:55:30 +00:00
|
|
|
markWaypoints(_common, flagType());
|
2009-10-11 11:37:13 +00:00
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
bool ArrivalDeparture::commonRoute(Transition* t, WayptVec& aPath, FGRunwayRef aRwy)
|
2009-10-11 11:37:13 +00:00
|
|
|
{
|
|
|
|
// assume we're routing from enroute, to the runway.
|
|
|
|
// for departures, we'll flip the result points
|
|
|
|
|
|
|
|
WayptVec::iterator firstCommon = _common.begin();
|
|
|
|
if (t) {
|
2012-04-24 21:55:30 +00:00
|
|
|
t->route(aPath);
|
2009-10-11 11:37:13 +00:00
|
|
|
|
|
|
|
Waypt* transEnd = t->procedureEnd();
|
|
|
|
for (; firstCommon != _common.end(); ++firstCommon) {
|
|
|
|
if ((*firstCommon)->matches(transEnd)) {
|
|
|
|
// found transition->common point, stop search
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // of common points
|
|
|
|
|
|
|
|
// if we hit this point, the transition doesn't end (start, for a SID) on
|
|
|
|
// a common point. We assume this means we should just append the entire
|
|
|
|
// common section after the transition.
|
|
|
|
firstCommon = _common.begin();
|
|
|
|
} else {
|
2012-04-24 21:55:30 +00:00
|
|
|
// no tranasition
|
2009-10-11 11:37:13 +00:00
|
|
|
} // of not using a transition
|
|
|
|
|
|
|
|
// append (some) common points
|
|
|
|
aPath.insert(aPath.end(), firstCommon, _common.end());
|
|
|
|
|
|
|
|
if (!aRwy) {
|
|
|
|
// no runway specified, we're done
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
RunwayTransitionMap::iterator r = _runways.find(aRwy);
|
|
|
|
assert(r != _runways.end());
|
|
|
|
if (!r->second) {
|
|
|
|
// no transitions specified. Not great, but not
|
|
|
|
// much we can do about it. Calling code will insert VECTORS to the approach
|
|
|
|
// if required, or maybe there's an approach transition defined.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-23 20:14:40 +00:00
|
|
|
SG_LOG(SG_NAVAID, SG_INFO, ident() << " using runway transition for " << r->first->ident());
|
2012-04-24 21:55:30 +00:00
|
|
|
r->second->route(aPath);
|
2009-10-11 11:37:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Transition* ArrivalDeparture::findTransitionByEnroute(Waypt* aEnroute) const
|
|
|
|
{
|
|
|
|
if (!aEnroute) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
WptTransitionMap::const_iterator eit;
|
|
|
|
for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
|
|
|
|
if (eit->second->enroute()->matches(aEnroute)) {
|
2012-11-23 20:14:40 +00:00
|
|
|
SG_LOG(SG_NAVAID, SG_INFO, ident() << " using enroute transition " << eit->second->ident());
|
2009-10-11 11:37:13 +00:00
|
|
|
return eit->second;
|
|
|
|
}
|
|
|
|
} // of enroute transition iteration
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
WayptRef ArrivalDeparture::findBestTransition(const SGGeod& aPos) const
|
|
|
|
{
|
|
|
|
// no transitions, that's easy
|
|
|
|
if (_enrouteTransitions.empty()) {
|
2012-11-23 20:14:40 +00:00
|
|
|
SG_LOG(SG_NAVAID, SG_INFO, "no enroute transitions for " << ident());
|
2009-10-11 11:37:13 +00:00
|
|
|
return _common.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
double d = 1e9;
|
|
|
|
WayptRef w;
|
|
|
|
WptTransitionMap::const_iterator eit;
|
|
|
|
for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
|
|
|
|
WayptRef c = eit->second->enroute();
|
2012-11-23 20:14:40 +00:00
|
|
|
SG_LOG(SG_NAVAID, SG_INFO, "findBestTransition for " << ident() << ", looking at " << c->ident());
|
2009-10-11 11:37:13 +00:00
|
|
|
// assert(c->hasFixedPosition());
|
|
|
|
double cd = SGGeodesy::distanceM(aPos, c->position());
|
|
|
|
|
|
|
|
if (cd < d) { // distance to 'c' is less, new best match
|
|
|
|
d = cd;
|
|
|
|
w = c;
|
|
|
|
}
|
|
|
|
} // of transitions iteration
|
|
|
|
|
|
|
|
assert(w);
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
Transition* ArrivalDeparture::findTransitionByName(const string& aIdent) const
|
2009-10-11 11:37:13 +00:00
|
|
|
{
|
|
|
|
WptTransitionMap::const_iterator eit;
|
|
|
|
for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
|
2012-04-24 21:55:30 +00:00
|
|
|
if (eit->second->ident() == aIdent) {
|
|
|
|
return eit->second;
|
2009-10-11 11:37:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
SID::SID(const string& aIdent, FGAirport* apt) :
|
|
|
|
ArrivalDeparture(aIdent, apt)
|
2009-10-11 11:37:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
bool SID::route(FGRunwayRef aWay, Transition* trans, WayptVec& aPath)
|
2009-10-11 11:37:13 +00:00
|
|
|
{
|
|
|
|
if (!isForRunway(aWay)) {
|
2012-11-23 20:14:40 +00:00
|
|
|
SG_LOG(SG_NAVAID, SG_WARN, "SID " << ident() << " not for runway " << aWay->ident());
|
2009-10-11 11:37:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
WayptVec path;
|
2012-04-24 21:55:30 +00:00
|
|
|
if (!commonRoute(trans, path, aWay)) {
|
2009-10-11 11:37:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SID waypoints (including transitions) are stored reversed, so we can
|
|
|
|
// re-use the routing code. This is where we fix the ordering for client code
|
|
|
|
std::back_insert_iterator<WayptVec> bi(aPath);
|
|
|
|
std::reverse_copy(path.begin(), path.end(), bi);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-11-12 21:30:28 +00:00
|
|
|
|
|
|
|
SID* SID::createTempSID(const std::string& aIdent, FGRunway* aRunway, const WayptVec& aPath)
|
|
|
|
{
|
|
|
|
// flip waypoints since SID stores them reversed
|
|
|
|
WayptVec path;
|
|
|
|
std::back_insert_iterator<WayptVec> bi(path);
|
|
|
|
std::reverse_copy(aPath.begin(), aPath.end(), bi);
|
|
|
|
|
|
|
|
SID* sid = new SID(aIdent, aRunway->airport());
|
|
|
|
sid->setCommon(path);
|
|
|
|
sid->addRunway(aRunway);
|
|
|
|
return sid;
|
|
|
|
}
|
2009-10-11 11:37:13 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
STAR::STAR(const string& aIdent, FGAirport* apt) :
|
|
|
|
ArrivalDeparture(aIdent, apt)
|
2009-10-11 11:37:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
bool STAR::route(FGRunwayRef aWay, Transition* trans, WayptVec& aPath)
|
2009-10-11 11:37:13 +00:00
|
|
|
{
|
|
|
|
if (aWay && !isForRunway(aWay)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
return commonRoute(trans, aPath, aWay);
|
2009-10-11 11:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
Transition::Transition(const std::string& aIdent, ProcedureType ty, Procedure* aPr) :
|
|
|
|
Procedure(aIdent),
|
|
|
|
_type(ty),
|
2012-05-02 21:09:41 +00:00
|
|
|
_parent(aPr)
|
2009-10-11 11:37:13 +00:00
|
|
|
{
|
|
|
|
assert(aPr);
|
2012-05-02 21:09:41 +00:00
|
|
|
}
|
2009-10-11 11:37:13 +00:00
|
|
|
|
2012-05-02 21:09:41 +00:00
|
|
|
void Transition::setPrimary(const WayptVec& aWps)
|
|
|
|
{
|
|
|
|
_primary = aWps;
|
|
|
|
assert(!_primary.empty());
|
2009-10-11 11:37:13 +00:00
|
|
|
_primary[0]->setFlag(WPT_TRANSITION, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
WayptRef Transition::enroute() const
|
|
|
|
{
|
|
|
|
assert(!_primary.empty());
|
|
|
|
return _primary[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
WayptRef Transition::procedureEnd() const
|
|
|
|
{
|
|
|
|
assert(!_primary.empty());
|
|
|
|
return _primary[_primary.size() - 1];
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
bool Transition::route(WayptVec& aPath)
|
2009-10-11 11:37:13 +00:00
|
|
|
{
|
|
|
|
aPath.insert(aPath.end(), _primary.begin(), _primary.end());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-24 21:55:30 +00:00
|
|
|
FGAirport* Transition::airport() const
|
|
|
|
{
|
|
|
|
return _parent->airport();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Transition::mark(WayptFlag f)
|
|
|
|
{
|
|
|
|
markWaypoints(_primary, f);
|
|
|
|
}
|
|
|
|
|
2009-10-11 11:37:13 +00:00
|
|
|
} // of namespace
|