]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navlist.cxx
Implement a persistent cache for navigation data.
[flightgear.git] / src / Navaids / navlist.cxx
1 // navlist.cxx -- navaids management class
2 //
3 // Written by Curtis Olson, started April 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <cassert>
29 #include <boost/foreach.hpp>
30 #include <algorithm>
31
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/sg_geodesy.hxx>
34 #include <simgear/sg_inlines.h>
35
36 #include "navlist.hxx"
37
38 #include <Airports/runways.hxx>
39 #include <Navaids/NavDataCache.hxx>
40
41 using std::string;
42
43 namespace { // anonymous
44
45 class NavRecordDistanceSortPredicate
46 {
47 public:
48   NavRecordDistanceSortPredicate( const SGGeod & position ) :
49   _position(SGVec3d::fromGeod(position)) {}
50   
51   bool operator()( const nav_rec_ptr & n1, const nav_rec_ptr & n2 )
52   {
53     if( n1 == NULL || n2 == NULL ) return false;
54     return distSqr(n1->cart(), _position) < distSqr(n2->cart(), _position);
55   }
56 private:
57   SGVec3d _position;
58   
59 };
60
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)
65 {
66   FGRunway* r(aNav->runway());
67   if (!r || !r->reciprocalRunway()) {
68     return true;
69   }
70   
71   // check if the runway frequency is paired
72   FGNavRecord* locA = r->ILS();
73   FGNavRecord* locB = r->reciprocalRunway()->ILS();
74   
75   if (!locA || !locB || (locA->get_freq() != locB->get_freq())) {
76     return true; // not paired, ok
77   }
78   
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);
87 }
88   
89 } // of anonymous namespace
90
91 // FGNavList ------------------------------------------------------------------
92
93
94 FGNavList::TypeFilter::TypeFilter(const FGPositioned::Type type)
95 {
96   if (type == FGPositioned::INVALID) {
97     _mintype = FGPositioned::NDB;
98     _maxtype = FGPositioned::GS;
99   } else {
100     _mintype = _maxtype = type;
101   }
102 }
103
104
105 FGNavList::TypeFilter::TypeFilter(const FGPositioned::Type minType,
106                                   const FGPositioned::Type maxType) :
107   _mintype(minType),
108   _maxtype(maxType)
109 {
110 }
111
112 /**
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.
116  */
117 class TacanFilter : public FGNavList::TypeFilter
118 {
119 public:
120   TacanFilter() :
121     TypeFilter(FGPositioned::DME, FGPositioned::TACAN)
122   {
123   }
124   
125   virtual bool pass(FGPositioned* pos) const
126   {
127     if (pos->type() == FGPositioned::TACAN) {
128       return true;
129     }
130     
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);
135   }
136 };
137
138 FGNavList::TypeFilter* FGNavList::locFilter()
139 {
140   static TypeFilter tf(FGPositioned::ILS, FGPositioned::LOC);
141   return &tf;
142 }
143
144 FGNavList::TypeFilter* FGNavList::ndbFilter()
145 {
146   static TypeFilter tf(FGPositioned::NDB);
147   return &tf;
148 }
149
150 FGNavList::TypeFilter* FGNavList::navFilter()
151 {
152   static TypeFilter tf(FGPositioned::VOR, FGPositioned::LOC);
153   return &tf;
154 }
155
156 FGNavList::TypeFilter* FGNavList::tacanFilter()
157 {
158   static TacanFilter tf;
159   return &tf;
160 }
161
162 FGNavList::TypeFilter* FGNavList::carrierFilter()
163 {
164   static TypeFilter tf(FGPositioned::MOBILE_TACAN);
165   return &tf;
166 }
167
168 FGNavRecord *FGNavList::findByFreq( double freq, const SGGeod& position,
169                                    TypeFilter* filter)
170 {
171   flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
172   int freqKhz = static_cast<int>(freq * 100);
173   PositionedIDVec stations(cache->findNavaidsByFreq(freqKhz, position, filter));
174   if (stations.empty()) {
175     return NULL;
176   }
177   
178 // now walk the (sorted) results list to find a usable, in-range navaid
179   SGVec3d acCart(SGVec3d::fromGeod(position));
180   double min_dist
181     = FG_NAV_MAX_RANGE*SG_NM_TO_METER*FG_NAV_MAX_RANGE*SG_NM_TO_METER;
182     
183   BOOST_FOREACH(PositionedID id, stations) {
184     FGNavRecord* station = (FGNavRecord*) cache->loadById(id);
185     double d2 = distSqr(station->cart(), acCart);
186     if (d2 > min_dist) {
187     // since results are sorted by proximity, as soon as we pass the
188     // distance cutoff we're done - fall out and return NULL
189       break;
190     }
191     
192     if (navidUsable(station, position)) {
193       return station;
194     }
195   }
196     
197 // fell out of the loop, no usable match
198   return NULL;
199 }
200
201 FGNavRecord *FGNavList::findByFreq( double freq, TypeFilter* filter)
202 {
203   flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
204   int freqKhz = static_cast<int>(freq * 1000);
205   PositionedIDVec stations(cache->findNavaidsByFreq(freqKhz, filter));
206   if (stations.empty()) {
207     return NULL;
208   }
209
210   return (FGNavRecord*) cache->loadById(stations.front());
211 }
212
213 nav_list_type FGNavList::findAllByFreq( double freq, const SGGeod& position,
214                                        TypeFilter* filter)
215 {
216   nav_list_type stations;
217   
218   flightgear::NavDataCache* cache = flightgear::NavDataCache::instance();
219   int freqKhz = static_cast<int>(freq * 1000);
220   PositionedIDVec ids(cache->findNavaidsByFreq(freqKhz, position, filter));
221   
222   BOOST_FOREACH(PositionedID id, ids) {
223     stations.push_back((FGNavRecord*) cache->loadById(id));
224   }
225   
226   return stations;
227 }
228
229 nav_list_type FGNavList::findByIdentAndFreq(const string& ident, const double freq,
230                                             TypeFilter* filter)
231 {
232   nav_list_type reply;
233   int f = (int)(freq*100.0 + 0.5);
234   
235   FGPositioned::List stations = FGPositioned::findAllWithIdent(ident, filter);
236   BOOST_FOREACH(FGPositionedRef ref, stations) {
237     FGNavRecord* nav = static_cast<FGNavRecord*>(ref.ptr());
238     if ( f <= 0.0 || nav->get_freq() == f) {
239       reply.push_back( nav );
240     }
241   }
242
243   return reply;
244 }
245
246 // Given an Ident and optional frequency and type ,
247 // return a list of matching stations sorted by distance to the given position
248 nav_list_type FGNavList::findByIdentAndFreq( const SGGeod & position,
249         const std::string& ident, const double freq,
250                                             TypeFilter* filter)
251 {
252     nav_list_type reply = findByIdentAndFreq( ident, freq, filter );
253     NavRecordDistanceSortPredicate sortPredicate( position );
254     std::sort( reply.begin(), reply.end(), sortPredicate );
255
256     return reply;
257 }
258
259 // FGTACANList ----------------------------------------------------------------
260
261 FGTACANList::FGTACANList( void )
262 {
263 }
264
265
266 FGTACANList::~FGTACANList( void )
267 {
268 }
269
270
271 bool FGTACANList::init()
272 {
273     return true;
274 }
275
276
277 // add an entry to the lists
278 bool FGTACANList::add( FGTACANRecord *c )
279 {
280     ident_channels[c->get_channel()].push_back(c);
281     return true;
282 }
283
284
285 // Given a TACAN Channel return the first matching frequency
286 FGTACANRecord *FGTACANList::findByChannel( const string& channel )
287 {
288     const tacan_list_type& stations = ident_channels[channel];
289     SG_LOG( SG_INSTR, SG_DEBUG, "findByChannel " << channel<< " size " << stations.size() );
290
291     if (!stations.empty()) {
292         return stations[0];
293     }
294     return NULL;
295 }
296
297