1 // navlist.cxx -- navaids management class
3 // Written by Curtis Olson, started April 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <boost/foreach.hpp>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/sg_geodesy.hxx>
34 #include <simgear/sg_inlines.h>
36 #include "navlist.hxx"
38 #include <Airports/runways.hxx>
39 #include <Navaids/NavDataCache.hxx>
43 namespace { // anonymous
45 class NavRecordDistanceSortPredicate
48 NavRecordDistanceSortPredicate( const SGGeod & position ) :
49 _position(SGVec3d::fromGeod(position)) {}
51 bool operator()( const nav_rec_ptr & n1, const nav_rec_ptr & n2 )
53 if( n1 == NULL || n2 == NULL ) return false;
54 return distSqr(n1->cart(), _position) < distSqr(n2->cart(), _position);
61 // discount navids if they conflict with another on the same frequency
62 // this only applies to navids associated with opposite ends of a runway,
63 // with matching frequencies.
64 bool navidUsable(FGNavRecord* aNav, const SGGeod &aircraft)
66 FGRunway* r(aNav->runway());
67 if (!r || !r->reciprocalRunway()) {
71 // check if the runway frequency is paired
72 FGNavRecord* locA = r->ILS();
73 FGNavRecord* locB = r->reciprocalRunway()->ILS();
75 if (!locA || !locB || (locA->get_freq() != locB->get_freq())) {
76 return true; // not paired, ok
79 // okay, both ends have an ILS, and they're paired. We need to select based on
80 // aircraft position. What we're going to use is *runway* (not navid) position,
81 // ie whichever runway end we are closer too. This makes back-course / missed
82 // approach behaviour incorrect, but that's the price we accept.
83 double crs = SGGeodesy::courseDeg(aircraft, r->geod());
84 double hdgDiff = crs - r->headingDeg();
85 SG_NORMALIZE_RANGE(hdgDiff, -180.0, 180.0);
86 return (fabs(hdgDiff) < 90.0);
89 } // of anonymous namespace
91 // FGNavList ------------------------------------------------------------------
94 FGNavList::TypeFilter::TypeFilter(const FGPositioned::Type type)
96 if (type == FGPositioned::INVALID) {
97 _mintype = FGPositioned::NDB;
98 _maxtype = FGPositioned::GS;
100 _mintype = _maxtype = type;
105 FGNavList::TypeFilter::TypeFilter(const FGPositioned::Type minType,
106 const FGPositioned::Type maxType) :
113 * Filter returning Tacan stations. Checks for both pure TACAN stations
114 * but also co-located VORTACs. This is done by searching for DMEs whose
115 * name indicates they are a TACAN or VORTAC; not a great solution.
117 class TacanFilter : public FGNavList::TypeFilter
121 TypeFilter(FGPositioned::DME, FGPositioned::TACAN)
125 virtual bool pass(FGPositioned* pos) const
127 if (pos->type() == FGPositioned::TACAN) {
131 assert(pos->type() == FGPositioned::DME);
132 string::size_type loc1 = pos->name().find( "TACAN" );
133 string::size_type loc2 = pos->name().find( "VORTAC" );
134 return (loc1 != string::npos) || (loc2 != string::npos);
138 FGNavList::TypeFilter* FGNavList::locFilter()
140 static TypeFilter tf(FGPositioned::ILS, FGPositioned::LOC);
144 FGNavList::TypeFilter* FGNavList::ndbFilter()
146 static TypeFilter tf(FGPositioned::NDB);
150 FGNavList::TypeFilter* FGNavList::navFilter()
152 static TypeFilter tf(FGPositioned::VOR, FGPositioned::LOC);
156 FGNavList::TypeFilter* FGNavList::tacanFilter()
158 static TacanFilter tf;
162 FGNavList::TypeFilter* FGNavList::carrierFilter()
164 static TypeFilter tf(FGPositioned::MOBILE_TACAN);
168 FGNavRecord *FGNavList::findByFreq( double freq, const SGGeod& position,
171 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
172 int freqKhz = static_cast<int>(freq * 100 + 0.5);
173 PositionedIDVec stations(cache->findNavaidsByFreq(freqKhz, position, filter));
174 if (stations.empty()) {
178 // now walk the (sorted) results list to find a usable, in-range navaid
179 SGVec3d acCart(SGVec3d::fromGeod(position));
181 = FG_NAV_MAX_RANGE*SG_NM_TO_METER*FG_NAV_MAX_RANGE*SG_NM_TO_METER;
183 BOOST_FOREACH(PositionedID id, stations) {
184 FGNavRecord* station = (FGNavRecord*) cache->loadById(id);
185 if (!filter->pass(station)) {
189 double d2 = distSqr(station->cart(), acCart);
191 // since results are sorted by proximity, as soon as we pass the
192 // distance cutoff we're done - fall out and return NULL
196 if (navidUsable(station, position)) {
201 // fell out of the loop, no usable match
205 FGNavRecord *FGNavList::findByFreq( double freq, TypeFilter* filter)
207 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
208 int freqKhz = static_cast<int>(freq * 100 + 0.5);
209 PositionedIDVec stations(cache->findNavaidsByFreq(freqKhz, filter));
210 if (stations.empty()) {
214 BOOST_FOREACH(PositionedID id, stations) {
215 FGNavRecord* station = (FGNavRecord*) cache->loadById(id);
216 if (filter->pass(station)) {
224 nav_list_type FGNavList::findAllByFreq( double freq, const SGGeod& position,
227 nav_list_type stations;
229 flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
230 int freqKhz = static_cast<int>(freq * 1000 + 0.5);
231 PositionedIDVec ids(cache->findNavaidsByFreq(freqKhz, position, filter));
233 BOOST_FOREACH(PositionedID id, ids) {
234 FGNavRecord* station = (FGNavRecord*) cache->loadById(id);
235 if (!filter->pass(station)) {
239 stations.push_back(station);
245 nav_list_type FGNavList::findByIdentAndFreq(const string& ident, const double freq,
249 int f = (int)(freq*100.0 + 0.5);
251 FGPositioned::List stations = FGPositioned::findAllWithIdent(ident, filter);
252 BOOST_FOREACH(FGPositionedRef ref, stations) {
253 FGNavRecord* nav = static_cast<FGNavRecord*>(ref.ptr());
254 if ( f <= 0.0 || nav->get_freq() == f) {
255 reply.push_back( nav );
262 // Given an Ident and optional frequency and type ,
263 // return a list of matching stations sorted by distance to the given position
264 nav_list_type FGNavList::findByIdentAndFreq( const SGGeod & position,
265 const std::string& ident, const double freq,
268 nav_list_type reply = findByIdentAndFreq( ident, freq, filter );
269 NavRecordDistanceSortPredicate sortPredicate( position );
270 std::sort( reply.begin(), reply.end(), sortPredicate );
275 // FGTACANList ----------------------------------------------------------------
277 FGTACANList::FGTACANList( void )
282 FGTACANList::~FGTACANList( void )
287 bool FGTACANList::init()
293 // add an entry to the lists
294 bool FGTACANList::add( FGTACANRecord *c )
296 ident_channels[c->get_channel()].push_back(c);
301 // Given a TACAN Channel return the first matching frequency
302 FGTACANRecord *FGTACANList::findByChannel( const string& channel )
304 const tacan_list_type& stations = ident_channels[channel];
305 SG_LOG( SG_NAVAID, SG_DEBUG, "findByChannel " << channel<< " size " << stations.size() );
307 if (!stations.empty()) {