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.
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/misc/sgstream.hxx>
30 #include <simgear/math/sg_geodesy.hxx>
32 #include "navlist.hxx"
35 // Return true if the nav record matches the type
36 static bool isTypeMatch(const FGNavRecord* n, fg_nav_types type) {
38 case FG_NAV_ANY: return(true);
39 case FG_NAV_VOR: return(n->get_type() == 3);
40 case FG_NAV_NDB: return(n->get_type() == 2);
41 case FG_NAV_ILS: return(n->get_type() == 4); // Note - very simplified, only matches loc as part of full ILS.
42 default: return false;
48 // FGNavList ------------------------------------------------------------------
50 FGNavList::FGNavList( void )
55 FGNavList::~FGNavList( void )
57 navaids_by_tile.erase( navaids_by_tile.begin(), navaids_by_tile.end() );
58 nav_list_type navlist = navaids.begin()->second;
59 navaids.erase( navaids.begin(), navaids.end() );
63 // load the navaids and build the map
64 bool FGNavList::init()
66 // No need to delete the original navaid structures
67 // since we're using an SGSharedPointer
68 nav_list_type navlist = navaids.begin()->second;
69 navaids.erase( navaids.begin(), navaids.end() );
70 navaids_by_tile.erase( navaids_by_tile.begin(), navaids_by_tile.end() );
71 ident_navaids.erase( ident_navaids.begin(), ident_navaids.end() );
77 // real add a marker beacon
78 static void real_add( nav_map_type &navmap, const int master_index,
81 navmap[master_index].push_back( n );
85 // front end for add a marker beacon
86 static void tile_add( nav_map_type &navmap, FGNavRecord *n )
90 double lon = n->get_lon();
91 double lat = n->get_lat();
93 int lonidx = (int)lon;
94 diff = lon - (double)lonidx;
95 if ( (lon < 0.0) && (fabs(diff) > SG_EPSILON) ) {
98 double lonfrac = lon - (double)lonidx;
101 int latidx = (int)lat;
102 diff = lat - (double)latidx;
103 if ( (lat < 0.0) && (fabs(diff) > SG_EPSILON) ) {
106 double latfrac = lat - (double)latidx;
109 int master_index = lonidx * 1000 + latidx;
110 // cout << "lonidx = " << lonidx << " latidx = " << latidx << " ";
111 // cout << "Master index = " << master_index << endl;
113 // add to the actual bucket
114 real_add( navmap, master_index, n );
116 // if we are close to the edge, add to adjacent buckets so we only
117 // have to search one bucket at run time
119 // there are 8 cases since there are 8 adjacent tiles
121 if ( lonfrac < 0.2 ) {
122 real_add( navmap, master_index - 1000, n );
123 if ( latfrac < 0.2 ) {
124 real_add( navmap, master_index - 1000 - 1, n );
125 } else if ( latfrac > 0.8 ) {
126 real_add( navmap, master_index - 1000 + 1, n );
128 } else if ( lonfrac > 0.8 ) {
129 real_add( navmap, master_index + 1000, n );
130 if ( latfrac < 0.2 ) {
131 real_add( navmap, master_index + 1000 - 1, n );
132 } else if ( latfrac > 0.8 ) {
133 real_add( navmap, master_index + 1000 + 1, n );
135 } else if ( latfrac < 0.2 ) {
136 real_add( navmap, master_index - 1, n );
137 } else if ( latfrac > 0.8 ) {
138 real_add( navmap, master_index + 1, n );
143 // add an entry to the lists
144 bool FGNavList::add( FGNavRecord *n )
146 navaids[n->get_freq()].push_back(n);
147 ident_navaids[n->get_ident()].push_back(n);
148 tile_add( navaids_by_tile, n );
153 FGNavRecord *FGNavList::findByFreq( double freq, double lon, double lat, double elev )
155 const nav_list_type& stations = navaids[(int)(freq*100.0 + 0.5)];
157 SGGeod geod = SGGeod::fromRadM(lon, lat, elev);
158 SGVec3d aircraft = SGVec3d::fromGeod(geod);
159 SG_LOG( SG_INSTR, SG_DEBUG, "findbyFreq " << freq << " size " << stations.size() );
161 return findNavFromList( aircraft, stations );
165 FGNavRecord *FGNavList::findByIdent( const char* ident,
166 const double lon, const double lat )
168 const nav_list_type& stations = ident_navaids[ident];
169 SGGeod geod = SGGeod::fromRad(lon, lat);
170 SGVec3d aircraft = SGVec3d::fromGeod(geod);
171 return findNavFromList( aircraft, stations );
175 nav_list_type FGNavList::findFirstByIdent( const string& ident, fg_nav_types type, bool exact)
181 if(type == FG_NAV_VOR) iType = 3;
182 else if(type == FG_NAV_NDB) iType = 2;
185 nav_ident_map_iterator it;
187 it = ident_navaids.find(ident);
189 bool typeMatch = false;
190 int safety_count = 0;
191 it = ident_navaids.lower_bound(ident);
193 nav_list_type n0 = it->second;
194 // local copy, so we should be able to do anything with n0.
195 // Remove the types that don't match request.
196 for(nav_list_iterator it0 = n0.begin(); it0 != n0.end();) {
197 FGNavRecord* nv = *it0;
198 if(nv->get_type() == iType) {
208 if(it == ident_navaids.begin()) {
209 // We didn't find a match before reaching the beginning of the map
214 if(safety_count == 1000000) {
215 SG_LOG(SG_INSTR, SG_ALERT,
216 "safety_count triggered exit from while loop in findFirstByIdent!");
220 if(it == ident_navaids.end()) {
226 if(it == ident_navaids.end()) {
230 nav_list_type n1 = it->second;
232 for(nav_list_iterator it2 = n1.begin(); it2 != n1.end(); ++it2) {
233 FGNavRecord* nv = *it2;
234 if(nv->get_type() == iType) n2.push_back(nv);
241 // Given an Ident and optional freqency, return the first matching
243 FGNavRecord *FGNavList::findByIdentAndFreq( const char* ident, const double freq )
245 nav_list_type stations = ident_navaids[ident];
246 SG_LOG( SG_INSTR, SG_DEBUG, "findByIdent " << ident<< " size " << stations.size() );
248 // sometimes there can be duplicated idents. If a freq is
249 // specified, use it to refine the search.
250 int f = (int)(freq*100.0 + 0.5);
251 nav_list_const_iterator it, end = stations.end();
252 for ( it = stations.begin(); it != end; ++it ) {
253 if ( f == (*it)->get_freq() ) {
257 } else if (!stations.empty()) {
265 // Given a point and a list of stations, return the closest one to the
267 FGNavRecord *FGNavList::findNavFromList( const SGVec3d &aircraft,
268 const nav_list_type &stations )
270 FGNavRecord *nav = NULL;
271 double d2; // in meters squared
273 = FG_NAV_MAX_RANGE*SG_NM_TO_METER*FG_NAV_MAX_RANGE*SG_NM_TO_METER;
275 nav_list_const_iterator it;
276 nav_list_const_iterator end = stations.end();
277 // find the closest station within a sensible range (FG_NAV_MAX_RANGE)
278 for ( it = stations.begin(); it != end; ++it ) {
279 FGNavRecord *station = *it;
280 // cout << "testing " << current->get_ident() << endl;
281 d2 = distSqr(station->get_cart(), aircraft);
283 // cout << " dist = " << sqrt(d)
284 // << " range = " << current->get_range() * SG_NM_TO_METER
287 // LOC, ILS, GS, and DME antenna's could potentially be
288 // installed at the opposite end of the runway. So it's not
289 // enough to simply find the closest antenna with the right
290 // frequency. We need the closest antenna with the right
291 // frequency that is most oriented towards us. (We penalize
292 // stations that are facing away from us by adding 5000 meters
293 // which is further than matching stations would ever be
294 // placed from each other. (Do the expensive check only for
295 // directional atennas and only when there is a chance it is
296 // the closest station.)
297 int type = station->get_type();
298 if ( d2 < min_dist &&
299 (type == 4 || type == 5 || type == 6 || type == 12 || type == 13) )
301 double hdg_deg = 0.0;
302 if ( type == 4 || type == 5 ){
303 hdg_deg = station->get_multiuse();
305 } else if ( type == 6 ) {
306 int tmp = (int)(station->get_multiuse() / 1000.0);
307 hdg_deg = station->get_multiuse() - (tmp * 1000);
309 } else if ( type == 12 || type == 13 ) {
310 // oops, Robin's data format doesn't give us the
311 // needed information to compute a heading for a DME
312 // transmitter. FIXME Robin!
315 double az1 = 0.0, az2 = 0.0, s = 0.0;
316 SGGeod geod = SGGeod::fromCart(aircraft);
317 geo_inverse_wgs_84( geod, station->get_pos(), &az1, &az2, &s);
318 az1 = az1 - station->get_multiuse();
319 if ( az1 > 180.0) az1 -= 360.0;
320 if ( az1 < -180.0) az1 += 360.0;
321 // penalize opposite facing stations by adding 5000 meters
322 // (squared) which is further than matching stations would
323 // ever be placed from each other.
324 if ( fabs(az1) > 90.0 ) {
325 double dist = sqrt(d2);
326 d2 = (dist + 5000) * (dist + 5000);
330 if ( d2 < min_dist ) {
340 // returns the closest entry to the give lon/lat/elev
341 FGNavRecord *FGNavList::findClosest( double lon_rad, double lat_rad,
342 double elev_m, fg_nav_types type)
344 FGNavRecord *result = NULL;
347 double lon_deg = lon_rad * SG_RADIANS_TO_DEGREES;
348 double lat_deg = lat_rad * SG_RADIANS_TO_DEGREES;
349 int lonidx = (int)lon_deg;
350 diff = lon_deg - (double)lonidx;
351 if ( (lon_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
356 int latidx = (int)lat_deg;
357 diff = lat_deg - (double)latidx;
358 if ( (lat_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
363 int master_index = lonidx * 1000 + latidx;
365 const nav_list_type& navs = navaids_by_tile[ master_index ];
366 // cout << "Master index = " << master_index << endl;
367 // cout << "beacon search length = " << beacons.size() << endl;
369 nav_list_const_iterator current = navs.begin();
370 nav_list_const_iterator last = navs.end();
372 SGGeod geod = SGGeod::fromRadM(lon_rad, lat_rad, elev_m);
373 SGVec3d aircraft = SGVec3d::fromGeod(geod);
375 double min_dist = 999999999.0;
377 for ( ; current != last ; ++current ) {
378 if(isTypeMatch(*current, type)) {
379 // cout << " testing " << (*current)->get_ident() << endl;
381 double d = distSqr((*current)->get_cart(), aircraft);
382 // cout << " distance = " << d << " ("
383 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
384 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
387 // cout << " range = " << sqrt(d) << endl;
389 if ( d < min_dist ) {
396 // cout << "lon = " << lon << " lat = " << lat
397 // << " closest beacon = " << sqrt( min_dist ) << endl;
402 // Given a frequency, return the first matching station.
403 FGNavRecord *FGNavList::findStationByFreq( double freq )
405 const nav_list_type& stations = navaids[(int)(freq*100.0 + 0.5)];
407 SG_LOG( SG_INSTR, SG_DEBUG, "findStationByFreq " << freq << " size " << stations.size() );
409 if (!stations.empty()) {
417 // FGTACANList ----------------------------------------------------------------
419 FGTACANList::FGTACANList( void )
424 FGTACANList::~FGTACANList( void )
429 bool FGTACANList::init()
435 // add an entry to the lists
436 bool FGTACANList::add( FGTACANRecord *c )
438 ident_channels[c->get_channel()].push_back(c);
443 // Given a TACAN Channel return the first matching frequency
444 FGTACANRecord *FGTACANList::findByChannel( const string& channel )
446 const tacan_list_type& stations = ident_channels[channel];
447 SG_LOG( SG_INSTR, SG_DEBUG, "findByChannel " << channel<< " size " << stations.size() );
449 if (!stations.empty()) {