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 Approach* Approach::createTempApproach(const std::string& aIdent, FGRunway* aRunway, const WayptVec& aPath)
55 Approach* app = new Approach(aIdent, PROCEDURE_APPROACH_RNAV);
56 app->setRunway(aRunway);
57 app->setPrimaryAndMissed(aPath, WayptVec());
61 void Approach::setRunway(FGRunwayRef aRwy)
66 FGAirport* Approach::airport() const
68 return _runway->airport();
71 RunwayVec Approach::runways() const
78 void Approach::setPrimaryAndMissed(const WayptVec& aPrimary, const WayptVec& aMissed)
81 _primary[0]->setFlag(WPT_IAF, true);
82 _primary[_primary.size()-1]->setFlag(WPT_FAF, true);
83 markWaypoints(_primary, WPT_APPROACH);
87 if (!_missed.empty()) {
88 // mark the first point as the published missed-approach point
89 _missed[0]->setFlag(WPT_MAP, true);
90 markWaypoints(_missed, WPT_MISS);
91 markWaypoints(_missed, WPT_APPROACH);
95 void Approach::addTransition(Transition* aTrans)
97 WayptRef entry = aTrans->enroute();
98 _transitions[entry] = aTrans;
99 aTrans->mark(WPT_APPROACH);
102 bool Approach::route(WayptRef aIAF, WayptVec& aWps)
105 WptTransitionMap::iterator it;
106 bool haveTrans = false;
107 for (it = _transitions.begin(); it != _transitions.end(); ++it) {
108 Transition* t= it->second;
109 if (t->enroute()->matches(aIAF)) {
114 } // of transitions iteration
117 SG_LOG(SG_NAVAID, SG_INFO, "approach " << ident() << " has no transition " <<
118 "for IAF: " << aIAF->ident());
123 return routeFromVectors(aWps);
126 bool Approach::routeFromVectors(WayptVec& aWps)
128 aWps.insert(aWps.end(), _primary.begin(), _primary.end());
129 RunwayWaypt* rwy = new RunwayWaypt(_runway, NULL);
130 rwy->setFlag(WPT_APPROACH);
132 aWps.insert(aWps.end(), _missed.begin(), _missed.end());
136 bool Approach::isApproach(ProcedureType ty)
138 return (ty >= PROCEDURE_APPROACH_ILS) && (ty <= PROCEDURE_APPROACH_RNAV);
141 //////////////////////////////////////////////////////////////////////////////
143 ArrivalDeparture::ArrivalDeparture(const string& aIdent, FGAirport* apt) :
149 void ArrivalDeparture::addRunway(FGRunwayRef aWay)
151 assert(aWay->airport() == _airport);
152 _runways[aWay] = NULL;
155 bool ArrivalDeparture::isForRunway(const FGRunway* aWay) const
157 // null runway always passes
162 FGRunwayRef r(const_cast<FGRunway*>(aWay));
163 return (_runways.count(r) > 0);
166 RunwayVec ArrivalDeparture::runways() const
169 RunwayTransitionMap::const_iterator it = _runways.begin();
170 for (; it != _runways.end(); ++it) {
171 r.push_back(it->first);
177 void ArrivalDeparture::addTransition(Transition* aTrans)
179 WayptRef entry = aTrans->enroute();
180 aTrans->mark(flagType());
181 _enrouteTransitions[entry] = aTrans;
184 string_list ArrivalDeparture::transitionIdents() const
187 WptTransitionMap::const_iterator eit;
188 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
189 r.push_back(eit->second->ident());
194 void ArrivalDeparture::addRunwayTransition(FGRunwayRef aWay, Transition* aTrans)
196 assert(aWay->ident() == aTrans->ident());
197 if (!isForRunway(aWay)) {
198 throw sg_io_exception("adding transition for unspecified runway:" + aWay->ident(), ident());
201 aTrans->mark(flagType());
202 _runways[aWay] = aTrans;
205 void ArrivalDeparture::setCommon(const WayptVec& aWps)
208 markWaypoints(_common, flagType());
211 bool ArrivalDeparture::commonRoute(Transition* t, WayptVec& aPath, FGRunwayRef aRwy)
213 // assume we're routing from enroute, to the runway.
214 // for departures, we'll flip the result points
216 WayptVec::iterator firstCommon = _common.begin();
220 Waypt* transEnd = t->procedureEnd();
221 for (; firstCommon != _common.end(); ++firstCommon) {
222 if ((*firstCommon)->matches(transEnd)) {
223 // found transition->common point, stop search
226 } // of common points
228 // if we hit this point, the transition doesn't end (start, for a SID) on
229 // a common point. We assume this means we should just append the entire
230 // common section after the transition.
231 firstCommon = _common.begin();
234 } // of not using a transition
236 // append (some) common points
237 aPath.insert(aPath.end(), firstCommon, _common.end());
240 // no runway specified, we're done
244 RunwayTransitionMap::iterator r = _runways.find(aRwy);
245 assert(r != _runways.end());
247 // no transitions specified. Not great, but not
248 // much we can do about it. Calling code will insert VECTORS to the approach
249 // if required, or maybe there's an approach transition defined.
253 SG_LOG(SG_NAVAID, SG_INFO, ident() << " using runway transition for " << r->first->ident());
254 r->second->route(aPath);
258 Transition* ArrivalDeparture::findTransitionByEnroute(Waypt* aEnroute) const
264 WptTransitionMap::const_iterator eit;
265 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
266 if (eit->second->enroute()->matches(aEnroute)) {
267 SG_LOG(SG_NAVAID, SG_INFO, ident() << " using enroute transition " << eit->second->ident());
270 } // of enroute transition iteration
275 WayptRef ArrivalDeparture::findBestTransition(const SGGeod& aPos) const
277 // no transitions, that's easy
278 if (_enrouteTransitions.empty()) {
279 SG_LOG(SG_NAVAID, SG_INFO, "no enroute transitions for " << ident());
280 return _common.front();
285 WptTransitionMap::const_iterator eit;
286 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
287 WayptRef c = eit->second->enroute();
288 SG_LOG(SG_NAVAID, SG_INFO, "findBestTransition for " << ident() << ", looking at " << c->ident());
289 // assert(c->hasFixedPosition());
290 double cd = SGGeodesy::distanceM(aPos, c->position());
292 if (cd < d) { // distance to 'c' is less, new best match
296 } // of transitions iteration
302 Transition* ArrivalDeparture::findTransitionByName(const string& aIdent) const
304 WptTransitionMap::const_iterator eit;
305 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
306 if (eit->second->ident() == aIdent) {
314 ////////////////////////////////////////////////////////////////////////////
316 SID::SID(const string& aIdent, FGAirport* apt) :
317 ArrivalDeparture(aIdent, apt)
321 bool SID::route(FGRunwayRef aWay, Transition* trans, WayptVec& aPath)
323 if (!isForRunway(aWay)) {
324 SG_LOG(SG_NAVAID, SG_WARN, "SID " << ident() << " not for runway " << aWay->ident());
329 if (!commonRoute(trans, path, aWay)) {
333 // SID waypoints (including transitions) are stored reversed, so we can
334 // re-use the routing code. This is where we fix the ordering for client code
335 std::back_insert_iterator<WayptVec> bi(aPath);
336 std::reverse_copy(path.begin(), path.end(), bi);
341 SID* SID::createTempSID(const std::string& aIdent, FGRunway* aRunway, const WayptVec& aPath)
343 // flip waypoints since SID stores them reversed
345 std::back_insert_iterator<WayptVec> bi(path);
346 std::reverse_copy(aPath.begin(), aPath.end(), bi);
348 SID* sid = new SID(aIdent, aRunway->airport());
349 sid->setCommon(path);
350 sid->addRunway(aRunway);
354 ////////////////////////////////////////////////////////////////////////////
356 STAR::STAR(const string& aIdent, FGAirport* apt) :
357 ArrivalDeparture(aIdent, apt)
361 bool STAR::route(FGRunwayRef aWay, Transition* trans, WayptVec& aPath)
363 if (aWay && !isForRunway(aWay)) {
367 return commonRoute(trans, aPath, aWay);
370 /////////////////////////////////////////////////////////////////////////////
372 Transition::Transition(const std::string& aIdent, ProcedureType ty, Procedure* aPr) :
380 void Transition::setPrimary(const WayptVec& aWps)
383 assert(!_primary.empty());
384 _primary[0]->setFlag(WPT_TRANSITION, true);
387 WayptRef Transition::enroute() const
389 assert(!_primary.empty());
393 WayptRef Transition::procedureEnd() const
395 assert(!_primary.empty());
396 return _primary[_primary.size() - 1];
399 bool Transition::route(WayptVec& aPath)
401 aPath.insert(aPath.end(), _primary.begin(), _primary.end());
405 FGAirport* Transition::airport() const
407 return _parent->airport();
410 void Transition::mark(WayptFlag f)
412 markWaypoints(_primary, f);