1 // procedure.cxx - define route storing an approach, arrival or departure procedure
2 // Written by James Turner, started 2009.
4 // Copyright (C) 2009 Curtis L. Olson
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "procedure.hxx"
23 #include <algorithm> // for reverse_copy
25 #include <simgear/structure/exception.hxx>
27 #include <Navaids/waypoint.hxx>
34 static void markWaypoints(WayptVec& wps, WayptFlag f)
36 for (unsigned int i=0; i<wps.size(); ++i) {
37 wps[i]->setFlag(f, true);
41 Procedure::Procedure(const string& aIdent) :
46 Approach::Approach(const string& aIdent, ProcedureType ty) :
53 void Approach::setRunway(FGRunwayRef aRwy)
58 FGAirport* Approach::airport() const
60 return _runway->airport();
63 RunwayVec Approach::runways() const
70 void Approach::setPrimaryAndMissed(const WayptVec& aPrimary, const WayptVec& aMissed)
73 _primary[0]->setFlag(WPT_IAF, true);
74 _primary[_primary.size()-1]->setFlag(WPT_FAF, true);
75 markWaypoints(_primary, WPT_APPROACH);
79 if (!_missed.empty()) {
80 // mark the first point as the published missed-approach point
81 _missed[0]->setFlag(WPT_MAP, true);
82 markWaypoints(_missed, WPT_MISS);
83 markWaypoints(_missed, WPT_APPROACH);
87 void Approach::addTransition(Transition* aTrans)
89 WayptRef entry = aTrans->enroute();
90 _transitions[entry] = aTrans;
91 aTrans->mark(WPT_APPROACH);
94 bool Approach::route(WayptRef aIAF, WayptVec& aWps)
96 WptTransitionMap::iterator it;
97 bool haveTrans = false;
98 for (it = _transitions.begin(); it != _transitions.end(); ++it) {
99 Transition* t= it->second;
100 if (t->enroute()->matches(aIAF)) {
105 } // of transitions iteration
108 SG_LOG(SG_GENERAL, SG_INFO, "approach " << ident() << " has no transition " <<
109 "for IAF: " << aIAF->ident());
113 return routeFromVectors(aWps);
116 bool Approach::routeFromVectors(WayptVec& aWps)
118 aWps.insert(aWps.end(), _primary.begin(), _primary.end());
119 aWps.push_back(new RunwayWaypt(_runway, NULL));
120 aWps.insert(aWps.end(), _missed.begin(), _missed.end());
124 bool Approach::isApproach(ProcedureType ty)
126 return (ty >= PROCEDURE_APPROACH_ILS) && (ty <= PROCEDURE_APPROACH_RNAV);
129 //////////////////////////////////////////////////////////////////////////////
131 ArrivalDeparture::ArrivalDeparture(const string& aIdent, FGAirport* apt) :
137 void ArrivalDeparture::addRunway(FGRunwayRef aWay)
139 assert(aWay->airport() == _airport);
140 _runways[aWay] = NULL;
143 bool ArrivalDeparture::isForRunway(const FGRunway* aWay) const
145 // null runway always passes
150 FGRunwayRef r(const_cast<FGRunway*>(aWay));
151 return (_runways.count(r));
154 RunwayVec ArrivalDeparture::runways() const
157 RunwayTransitionMap::const_iterator it = _runways.begin();
158 for (; it != _runways.end(); ++it) {
159 r.push_back(it->first);
165 void ArrivalDeparture::addTransition(Transition* aTrans)
167 WayptRef entry = aTrans->enroute();
168 aTrans->mark(flagType());
169 _enrouteTransitions[entry] = aTrans;
172 string_list ArrivalDeparture::transitionIdents() const
175 WptTransitionMap::const_iterator eit;
176 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
177 r.push_back(eit->second->ident());
182 void ArrivalDeparture::addRunwayTransition(FGRunwayRef aWay, Transition* aTrans)
184 assert(aWay->ident() == aTrans->ident());
185 if (!isForRunway(aWay)) {
186 throw sg_io_exception("adding transition for unspecified runway:" + aWay->ident(), ident());
189 aTrans->mark(flagType());
190 _runways[aWay] = aTrans;
193 void ArrivalDeparture::setCommon(const WayptVec& aWps)
196 markWaypoints(_common, flagType());
199 bool ArrivalDeparture::commonRoute(Transition* t, WayptVec& aPath, FGRunwayRef aRwy)
201 // assume we're routing from enroute, to the runway.
202 // for departures, we'll flip the result points
204 WayptVec::iterator firstCommon = _common.begin();
208 Waypt* transEnd = t->procedureEnd();
209 for (; firstCommon != _common.end(); ++firstCommon) {
210 if ((*firstCommon)->matches(transEnd)) {
211 // found transition->common point, stop search
214 } // of common points
216 // if we hit this point, the transition doesn't end (start, for a SID) on
217 // a common point. We assume this means we should just append the entire
218 // common section after the transition.
219 firstCommon = _common.begin();
222 } // of not using a transition
224 // append (some) common points
225 aPath.insert(aPath.end(), firstCommon, _common.end());
228 // no runway specified, we're done
232 RunwayTransitionMap::iterator r = _runways.find(aRwy);
233 assert(r != _runways.end());
235 // no transitions specified. Not great, but not
236 // much we can do about it. Calling code will insert VECTORS to the approach
237 // if required, or maybe there's an approach transition defined.
241 SG_LOG(SG_GENERAL, SG_INFO, ident() << " using runway transition for " << r->first->ident());
242 r->second->route(aPath);
246 Transition* ArrivalDeparture::findTransitionByEnroute(Waypt* aEnroute) const
252 WptTransitionMap::const_iterator eit;
253 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
254 if (eit->second->enroute()->matches(aEnroute)) {
255 SG_LOG(SG_GENERAL, SG_INFO, ident() << " using enroute transition " << eit->second->ident());
258 } // of enroute transition iteration
263 WayptRef ArrivalDeparture::findBestTransition(const SGGeod& aPos) const
265 // no transitions, that's easy
266 if (_enrouteTransitions.empty()) {
267 SG_LOG(SG_GENERAL, SG_INFO, "no enroute transitions for " << ident());
268 return _common.front();
273 WptTransitionMap::const_iterator eit;
274 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
275 WayptRef c = eit->second->enroute();
276 SG_LOG(SG_GENERAL, SG_INFO, "findBestTransition for " << ident() << ", looking at " << c->ident());
277 // assert(c->hasFixedPosition());
278 double cd = SGGeodesy::distanceM(aPos, c->position());
280 if (cd < d) { // distance to 'c' is less, new best match
284 } // of transitions iteration
290 Transition* ArrivalDeparture::findTransitionByName(const string& aIdent) const
292 WptTransitionMap::const_iterator eit;
293 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
294 if (eit->second->ident() == aIdent) {
302 ////////////////////////////////////////////////////////////////////////////
304 SID::SID(const string& aIdent, FGAirport* apt) :
305 ArrivalDeparture(aIdent, apt)
309 bool SID::route(FGRunwayRef aWay, Transition* trans, WayptVec& aPath)
311 if (!isForRunway(aWay)) {
312 SG_LOG(SG_GENERAL, SG_WARN, "SID " << ident() << " not for runway " << aWay->ident());
317 if (!commonRoute(trans, path, aWay)) {
321 // SID waypoints (including transitions) are stored reversed, so we can
322 // re-use the routing code. This is where we fix the ordering for client code
323 std::back_insert_iterator<WayptVec> bi(aPath);
324 std::reverse_copy(path.begin(), path.end(), bi);
329 ////////////////////////////////////////////////////////////////////////////
331 STAR::STAR(const string& aIdent, FGAirport* apt) :
332 ArrivalDeparture(aIdent, apt)
336 bool STAR::route(FGRunwayRef aWay, Transition* trans, WayptVec& aPath)
338 if (aWay && !isForRunway(aWay)) {
342 return commonRoute(trans, aPath, aWay);
345 /////////////////////////////////////////////////////////////////////////////
347 Transition::Transition(const std::string& aIdent, ProcedureType ty, Procedure* aPr) :
355 void Transition::setPrimary(const WayptVec& aWps)
358 assert(!_primary.empty());
359 _primary[0]->setFlag(WPT_TRANSITION, true);
362 WayptRef Transition::enroute() const
364 assert(!_primary.empty());
368 WayptRef Transition::procedureEnd() const
370 assert(!_primary.empty());
371 return _primary[_primary.size() - 1];
374 bool Transition::route(WayptVec& aPath)
376 aPath.insert(aPath.end(), _primary.begin(), _primary.end());
380 FGAirport* Transition::airport() const
382 return _parent->airport();
385 void Transition::mark(WayptFlag f)
387 markWaypoints(_primary, f);