Debian Codegraph @

src/Navaids/procedure.cxx

raw symbols SCIP
1 // procedure.cxx - define route storing an approach, arrival or departure procedure
2 // Written by James Turner, started 2009.
3 //
4 // Copyright (C) 2009 Curtis L. Olson
5 //
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.
10 //
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.
15 //
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.
19
20 #include "procedure.hxx"
21
22 #include <cassert>
23 #include <algorithm> // for reverse_copy
24
25 #include <simgear/structure/exception.hxx>
26
27 #include <Airports/airport.hxx>
28 #include <Airports/airports_fwd.hxx>
29 #include <Airports/runways.hxx>
30 #include <Navaids/waypoint.hxx>
31
32 #include <iterator> // required for WIN
33 using std::string;
34
35 namespace flightgear
36 {
37
38 static void markWaypoints(WayptVec& wps, WayptFlag f)
39 {
40 for (unsigned int i=0; i<wps.size(); ++i) {
41 wps[i]->setFlag(f, true);
42 }
43 }
44
45 Procedure::Procedure(const string& aIdent) :
46 _ident(aIdent)
47 {
48 }
49
50 bool Procedure::isForRunway(FGRunwayRef rwy) const
51 {
52 // this implementation is a bit wasteful but we don't call
53 // this on any hot paths
54 const auto rwys = runways();
55 auto it = std::find_if(rwys.cbegin(), rwys.cend(), [rwy](const FGRunwayRef& r) {
56 return r == rwy;
57 });
58 return it != rwys.end();
59 }
60
61
62 Approach::Approach(const string& aIdent, ProcedureType ty) :
63 Procedure(aIdent),
64 _type(ty)
65 {
66
67 }
68
69 Approach::~Approach() = default;
70
71 Approach* Approach::createTempApproach(const std::string& aIdent, FGRunway* aRunway, const WayptVec& aPath)
72 {
73 Approach* app = new Approach(aIdent, PROCEDURE_APPROACH_RNAV);
74 app->setRunway(aRunway);
75 app->setPrimaryAndMissed(aPath, WayptVec());
76 return app;
77 }
78
79 void Approach::setRunway(FGRunwayRef aRwy)
80 {
81 _runway = aRwy;
82 }
83
84 FGAirport* Approach::airport() const
85 {
86 return _runway->airport();
87 }
88
89 RunwayVec Approach::runways() const
90 {
91 RunwayVec r;
92 r.push_back(_runway);
93 return r;
94 }
95
96 void Approach::setPrimaryAndMissed(const WayptVec& aPrimary, const WayptVec& aMissed)
97 {
98 _primary = aPrimary;
99 _primary[0]->setFlag(WPT_IAF, true);
100 _primary[_primary.size()-1]->setFlag(WPT_FAF, true);
101 markWaypoints(_primary, WPT_APPROACH);
102
103 _missed = aMissed;
104
105 if (!_missed.empty()) {
106 // mark the first point as the published missed-approach point
107 _missed[0]->setFlag(WPT_MAP, true);
108 markWaypoints(_missed, WPT_MISS);
109 markWaypoints(_missed, WPT_APPROACH);
110 }
111 }
112
113 void Approach::addTransition(Transition* aTrans)
114 {
115 WayptRef entry = aTrans->enroute();
116 _transitions[entry] = aTrans;
117 aTrans->mark(WPT_APPROACH);
118 }
119
120 bool Approach::routeWithTransition(FGRunwayRef runway, Transition* trans, WayptVec& aWps)
121 {
122 if (!trans)
123 return false;
124
125 trans->route(aWps);
126 bool ok = routeFromVectors(aWps);
127 return ok;
128 }
129
130 bool Approach::route(FGRunwayRef runway, WayptRef aIAF, WayptVec& aWps)
131 {
132 if (aIAF.valid()) {
133 bool haveTrans = false;
134 for (auto te : _transitions) {
135 auto t = te.second;
136 if (t->enroute()->matches(aIAF)) {
137 t->route(aWps);
138 haveTrans = true;
139 break;
140 }
141 } // of transitions iteration
142
143 if (!haveTrans) {
144 if (_primary.front()->matches(aIAF)) {
145 // direct IAF on the approach, no transition is needed
146 } else if (aIAF->type() == "vectors") {
147 // IAF is actually VECTORS, so assume ATC will vector us to the approach
148 } else {
149 // we couldn't find the IAF at the front of any obvious thing - either
150 // the primary waypoints or any transition we have defined.
151 // warn and just use the primary waypoints down below
152 SG_LOG(SG_NAVAID, SG_INFO, "approach " << ident() << " has no transition " << "for IAF: " << aIAF->ident());
153 }
154 }
155 }
156
157 bool ok = routeFromVectors(aWps);
158
159 if (ok && !aWps.empty() && aIAF.valid() && aWps.front()->matches(aIAF)) {
160 // don't duplicate the IAF into the route we return. This avoids a
161 // duplicated waypt between the end of a STAR and the approach
162 aWps.erase(aWps.begin());
163 }
164
165 return ok;
166 }
167
168 bool Approach::routeFromVectors(WayptVec& aWps)
169 {
170 aWps.insert(aWps.end(), _primary.begin(), _primary.end());
171 RunwayWaypt* rwy = new RunwayWaypt(_runway, NULL);
172 rwy->setFlag(WPT_APPROACH);
173 aWps.push_back(rwy);
174 aWps.insert(aWps.end(), _missed.begin(), _missed.end());
175 return true;
176 }
177
178 bool Approach::isApproach(ProcedureType ty)
179 {
180 return (ty >= PROCEDURE_APPROACH_ILS) && (ty <= PROCEDURE_APPROACH_RNAV);
181 }
182
183 string_list Approach::transitionIdents() const
184 {
185 string_list r;
186 r.reserve(_transitions.size());
187 for (const auto& t : _transitions) {
188 r.push_back(t.second->ident());
189 }
190 return r;
191 }
192
193 Transition* Approach::findTransitionByName(const string& aIdent) const
194 {
195 auto it = std::find_if(_transitions.begin(), _transitions.end(), [aIdent](const WptTransitionMap::value_type& t) {
196 return aIdent == t.second->ident();
197 });
198
199 if (it == _transitions.end())
200 return nullptr;
201
202 return it->second;
203 }
204
205 //////////////////////////////////////////////////////////////////////////////
206
207 ArrivalDeparture::ArrivalDeparture(const string& aIdent, FGAirport* apt) :
208 Procedure(aIdent),
209 _airport(apt)
210 {
211 }
212
213 void ArrivalDeparture::addRunway(FGRunwayRef aWay)
214 {
215 assert(aWay->airport() == _airport);
216 _runways[aWay] = NULL;
217 }
218
219 bool ArrivalDeparture::isForRunway(const FGRunway* aWay) const
220 {
221 // null runway always passes
222 if (!aWay) {
223 return true;
224 }
225
226 FGRunwayRef r(const_cast<FGRunway*>(aWay));
227 return (_runways.count(r) > 0);
228 }
229
230 RunwayVec ArrivalDeparture::runways() const
231 {
232 RunwayVec r;
233 RunwayTransitionMap::const_iterator it = _runways.begin();
234 for (; it != _runways.end(); ++it) {
235 r.push_back(it->first);
236 }
237
238 return r;
239 }
240
241 void ArrivalDeparture::addTransition(Transition* aTrans)
242 {
243 WayptRef entry = aTrans->enroute();
244 aTrans->mark(flagType());
245 _enrouteTransitions[entry] = aTrans;
246 }
247
248 string_list ArrivalDeparture::transitionIdents() const
249 {
250 string_list r;
251 WptTransitionMap::const_iterator eit;
252 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
253 r.push_back(eit->second->ident());
254 }
255 return r;
256 }
257
258 void ArrivalDeparture::addRunwayTransition(FGRunwayRef aWay, Transition* aTrans)
259 {
260 assert(aWay->ident() == aTrans->ident());
261 if (!isForRunway(aWay)) {
262 throw sg_io_exception("adding transition for unspecified runway:" + aWay->ident(), ident());
263 }
264
265 aTrans->mark(flagType());
266 _runways[aWay] = aTrans;
267 }
268
269 void ArrivalDeparture::setCommon(const WayptVec& aWps)
270 {
271 _common = aWps;
272 markWaypoints(_common, flagType());
273 }
274
275 bool ArrivalDeparture::commonRoute(Transition* t, WayptVec& aPath, FGRunwayRef aRwy)
276 {
277 // assume we're routing from enroute, to the runway.
278 // for departures, we'll flip the result points
279
280 WayptVec::iterator firstCommon = _common.begin();
281 if (t) {
282 t->route(aPath);
283
284 Waypt* transEnd = t->procedureEnd();
285 for (; firstCommon != _common.end(); ++firstCommon) {
286 if ((*firstCommon)->matches(transEnd)) {
287 // found transition->common point, stop search
288 break;
289 }
290 } // of common points
291
292 // if we hit this point, the transition doesn't end (start, for a SID) on
293 // a common point. We assume this means we should just append the entire
294 // common section after the transition.
295 firstCommon = _common.begin();
296 } else {
297 // no tranasition
298 } // of not using a transition
299
300 // append (some) common points
301 aPath.insert(aPath.end(), firstCommon, _common.end());
302
303 if (!aRwy) {
304 // no runway specified, we're done
305 return true;
306 }
307
308 RunwayTransitionMap::iterator r = _runways.find(aRwy);
309 if (r == _runways.end()) {
310 // runway doesn't match STAR/SID; this may be intentional (cf. EDDF
311 // transitions), so we have to cater for it: we will just return at this
312 // point, and trust the calling code to insert VECTORS or select an
313 // appropriate approach transition.
314 return true;
315 }
316
317 if (!r->second) {
318 // no transitions specified. Not great, but not
319 // much we can do about it. Calling code will insert VECTORS to the approach
320 // if required, or maybe there's an approach transition defined.
321 return true;
322 }
323
324 SG_LOG(SG_NAVAID, SG_INFO, ident() << " using runway transition for " << r->first->ident());
325 r->second->route(aPath);
326 return true;
327 }
328
329 Transition* ArrivalDeparture::findTransitionByEnroute(Waypt* aEnroute) const
330 {
331 if (!aEnroute) {
332 return NULL;
333 }
334
335 WptTransitionMap::const_iterator eit;
336 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
337 if (eit->second->enroute()->matches(aEnroute)) {
338 return eit->second;
339 }
340 } // of enroute transition iteration
341
342 return NULL;
343 }
344
345 Transition* ArrivalDeparture::findTransitionByEnroute(FGPositioned* aEnroute) const
346 {
347 if (!aEnroute) {
348 return NULL;
349 }
350
351 WptTransitionMap::const_iterator eit;
352 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
353 if (eit->second->enroute()->matches(aEnroute)) {
354 return eit->second;
355 }
356 } // of enroute transition iteration
357
358 return NULL;
359 }
360
361 WayptRef ArrivalDeparture::findBestTransition(const SGGeod& aPos) const
362 {
363 // no transitions, that's easy
364 if (_enrouteTransitions.empty()) {
365 SG_LOG(SG_NAVAID, SG_INFO, "no enroute transitions for " << ident());
366 return _common.front();
367 }
368
369 double d = 1e9;
370 WayptRef w;
371 WptTransitionMap::const_iterator eit;
372 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
373 WayptRef c = eit->second->enroute();
374 SG_LOG(SG_NAVAID, SG_INFO, "findBestTransition for " << ident() << ", looking at " << c->ident());
375 // assert(c->hasFixedPosition());
376 double cd = SGGeodesy::distanceM(aPos, c->position());
377
378 if (cd < d) { // distance to 'c' is less, new best match
379 d = cd;
380 w = c;
381 }
382 } // of transitions iteration
383
384 assert(w);
385 return w;
386 }
387
388 Transition* ArrivalDeparture::findTransitionByName(const string& aIdent) const
389 {
390 WptTransitionMap::const_iterator eit;
391 for (eit = _enrouteTransitions.begin(); eit != _enrouteTransitions.end(); ++eit) {
392 if (eit->second->ident() == aIdent) {
393 return eit->second;
394 }
395 }
396
397 return NULL;
398 }
399
400 ////////////////////////////////////////////////////////////////////////////
401
402 SID::SID(const string& aIdent, FGAirport* apt) :
403 ArrivalDeparture(aIdent, apt)
404 {
405 }
406
407 bool SID::route(FGRunwayRef aWay, Transition* trans, WayptVec& aPath)
408 {
409 if (!isForRunway(aWay)) {
410 SG_LOG(SG_NAVAID, SG_WARN, "SID " << ident() << " not for runway " << aWay->ident());
411 return false;
412 }
413
414 WayptVec path;
415 if (!commonRoute(trans, path, aWay)) {
416 return false;
417 }
418
419 // SID waypoints (including transitions) are stored reversed, so we can
420 // re-use the routing code. This is where we fix the ordering for client code
421 std::back_insert_iterator<WayptVec> bi(aPath);
422 std::reverse_copy(path.begin(), path.end(), bi);
423
424 return true;
425 }
426
427 SID* SID::createTempSID(const std::string& aIdent, FGRunway* aRunway, const WayptVec& aPath)
428 {
429 // flip waypoints since SID stores them reversed
430 WayptVec path;
431 std::back_insert_iterator<WayptVec> bi(path);
432 std::reverse_copy(aPath.begin(), aPath.end(), bi);
433
434 SID* sid = new SID(aIdent, aRunway->airport());
435 sid->setCommon(path);
436 sid->addRunway(aRunway);
437 return sid;
438 }
439
440 ////////////////////////////////////////////////////////////////////////////
441
442 STAR::STAR(const string& aIdent, FGAirport* apt) :
443 ArrivalDeparture(aIdent, apt)
444 {
445 }
446
447 bool STAR::route(FGRunwayRef aWay, Transition* trans, WayptVec& aPath)
448 {
449 if (aWay && !isForRunway(aWay)) {
450 return false;
451 }
452
453 return commonRoute(trans, aPath, aWay);
454 }
455
456 /////////////////////////////////////////////////////////////////////////////
457
458 Transition::Transition(const std::string& aIdent, ProcedureType ty, Procedure* aPr) :
459 Procedure(aIdent),
460 _type(ty),
461 _parent(aPr)
462 {
463 assert(aPr);
464 }
465
466 void Transition::setPrimary(const WayptVec& aWps)
467 {
468 _primary = aWps;
469 assert(!_primary.empty());
470 _primary[0]->setFlag(WPT_TRANSITION, true);
471 }
472
473 WayptRef Transition::enroute() const
474 {
475 assert(!_primary.empty());
476 return _primary[0];
477 }
478
479 WayptRef Transition::procedureEnd() const
480 {
481 assert(!_primary.empty());
482 return _primary[_primary.size() - 1];
483 }
484
485 bool Transition::route(WayptVec& aPath)
486 {
487 aPath.insert(aPath.end(), _primary.begin(), _primary.end());
488 return true;
489 }
490
491 FGAirport* Transition::airport() const
492 {
493 return _parent->airport();
494 }
495
496 void Transition::mark(WayptFlag f)
497 {
498 markWaypoints(_primary, f);
499 }
500
501 } // of namespace