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)
97 WptTransitionMap::iterator it;
98 bool haveTrans = false;
99 for (it = _transitions.begin(); it != _transitions.end(); ++it) {
100 Transition* t= it->second;
101 if (t->enroute()->matches(aIAF)) {
106 } // of transitions iteration
109 SG_LOG(SG_GENERAL, SG_INFO, "approach " << ident() << " has no transition " <<
110 "for IAF: " << aIAF->ident());
115 return routeFromVectors(aWps);
118 bool Approach::routeFromVectors(WayptVec& aWps)
120 aWps.insert(aWps.end(), _primary.begin(), _primary.end());
121 RunwayWaypt* rwy = new RunwayWaypt(_runway, NULL);
122 rwy->setFlag(WPT_APPROACH);
124 aWps.insert(aWps.end(), _missed.begin(), _missed.end());
128 bool Approach::isApproach(ProcedureType ty)
130 return (ty >= PROCEDURE_APPROACH_ILS) && (ty <= PROCEDURE_APPROACH_RNAV);
133 //////////////////////////////////////////////////////////////////////////////
135 ArrivalDeparture::ArrivalDeparture(const string& aIdent, FGAirport* apt) :
141 void ArrivalDeparture::addRunway(FGRunwayRef aWay)
143 assert(aWay->airport() == _airport);
144 _runways[aWay] = NULL;
147 bool ArrivalDeparture::isForRunway(const FGRunway* aWay) const
149 // null runway always passes
154 FGRunwayRef r(const_cast<FGRunway*>(aWay));
155 return (_runways.count(r));
158 RunwayVec ArrivalDeparture::runways() const
161 RunwayTransitionMap::const_iterator it = _runways.begin();
162 for (; it != _runways.end(); ++it) {
163 r.push_back(it->first);
169 void ArrivalDeparture::addTransition(Transition* aTrans)
171 WayptRef entry = aTrans->enroute();
172 aTrans->mark(flagType());
173 _enrouteTransitions[entry] = aTrans;
176 string_list ArrivalDeparture::transitionIdents() const
179 WptTransitionMap::const_iterator eit;
180 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
181 r.push_back(eit->second->ident());
186 void ArrivalDeparture::addRunwayTransition(FGRunwayRef aWay, Transition* aTrans)
188 assert(aWay->ident() == aTrans->ident());
189 if (!isForRunway(aWay)) {
190 throw sg_io_exception("adding transition for unspecified runway:" + aWay->ident(), ident());
193 aTrans->mark(flagType());
194 _runways[aWay] = aTrans;
197 void ArrivalDeparture::setCommon(const WayptVec& aWps)
200 markWaypoints(_common, flagType());
203 bool ArrivalDeparture::commonRoute(Transition* t, WayptVec& aPath, FGRunwayRef aRwy)
205 // assume we're routing from enroute, to the runway.
206 // for departures, we'll flip the result points
208 WayptVec::iterator firstCommon = _common.begin();
212 Waypt* transEnd = t->procedureEnd();
213 for (; firstCommon != _common.end(); ++firstCommon) {
214 if ((*firstCommon)->matches(transEnd)) {
215 // found transition->common point, stop search
218 } // of common points
220 // if we hit this point, the transition doesn't end (start, for a SID) on
221 // a common point. We assume this means we should just append the entire
222 // common section after the transition.
223 firstCommon = _common.begin();
226 } // of not using a transition
228 // append (some) common points
229 aPath.insert(aPath.end(), firstCommon, _common.end());
232 // no runway specified, we're done
236 RunwayTransitionMap::iterator r = _runways.find(aRwy);
237 assert(r != _runways.end());
239 // no transitions specified. Not great, but not
240 // much we can do about it. Calling code will insert VECTORS to the approach
241 // if required, or maybe there's an approach transition defined.
245 SG_LOG(SG_GENERAL, SG_INFO, ident() << " using runway transition for " << r->first->ident());
246 r->second->route(aPath);
250 Transition* ArrivalDeparture::findTransitionByEnroute(Waypt* aEnroute) const
256 WptTransitionMap::const_iterator eit;
257 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
258 if (eit->second->enroute()->matches(aEnroute)) {
259 SG_LOG(SG_GENERAL, SG_INFO, ident() << " using enroute transition " << eit->second->ident());
262 } // of enroute transition iteration
267 WayptRef ArrivalDeparture::findBestTransition(const SGGeod& aPos) const
269 // no transitions, that's easy
270 if (_enrouteTransitions.empty()) {
271 SG_LOG(SG_GENERAL, SG_INFO, "no enroute transitions for " << ident());
272 return _common.front();
277 WptTransitionMap::const_iterator eit;
278 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
279 WayptRef c = eit->second->enroute();
280 SG_LOG(SG_GENERAL, SG_INFO, "findBestTransition for " << ident() << ", looking at " << c->ident());
281 // assert(c->hasFixedPosition());
282 double cd = SGGeodesy::distanceM(aPos, c->position());
284 if (cd < d) { // distance to 'c' is less, new best match
288 } // of transitions iteration
294 Transition* ArrivalDeparture::findTransitionByName(const string& aIdent) const
296 WptTransitionMap::const_iterator eit;
297 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
298 if (eit->second->ident() == aIdent) {
306 ////////////////////////////////////////////////////////////////////////////
308 SID::SID(const string& aIdent, FGAirport* apt) :
309 ArrivalDeparture(aIdent, apt)
313 bool SID::route(FGRunwayRef aWay, Transition* trans, WayptVec& aPath)
315 if (!isForRunway(aWay)) {
316 SG_LOG(SG_GENERAL, SG_WARN, "SID " << ident() << " not for runway " << aWay->ident());
321 if (!commonRoute(trans, path, aWay)) {
325 // SID waypoints (including transitions) are stored reversed, so we can
326 // re-use the routing code. This is where we fix the ordering for client code
327 std::back_insert_iterator<WayptVec> bi(aPath);
328 std::reverse_copy(path.begin(), path.end(), bi);
333 ////////////////////////////////////////////////////////////////////////////
335 STAR::STAR(const string& aIdent, FGAirport* apt) :
336 ArrivalDeparture(aIdent, apt)
340 bool STAR::route(FGRunwayRef aWay, Transition* trans, WayptVec& aPath)
342 if (aWay && !isForRunway(aWay)) {
346 return commonRoute(trans, aPath, aWay);
349 /////////////////////////////////////////////////////////////////////////////
351 Transition::Transition(const std::string& aIdent, ProcedureType ty, Procedure* aPr) :
359 void Transition::setPrimary(const WayptVec& aWps)
362 assert(!_primary.empty());
363 _primary[0]->setFlag(WPT_TRANSITION, true);
366 WayptRef Transition::enroute() const
368 assert(!_primary.empty());
372 WayptRef Transition::procedureEnd() const
374 assert(!_primary.empty());
375 return _primary[_primary.size() - 1];
378 bool Transition::route(WayptVec& aPath)
380 aPath.insert(aPath.end(), _primary.begin(), _primary.end());
384 FGAirport* Transition::airport() const
386 return _parent->airport();
389 void Transition::mark(WayptFlag f)
391 markWaypoints(_primary, f);