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., 675 Mass Ave, Cambridge, MA 02139, 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::FGNavList( void ) {
51 FGTACANList::FGTACANList( void ){
56 FGNavList::~FGNavList( void ) {
59 FGTACANList::~FGTACANList( void ){
62 // load the navaids and build the map
63 bool FGNavList::init() {
65 // FIXME: leaves all the individual navaid entries leaked
66 navaids.erase( navaids.begin(), navaids.end() );
67 navaids_by_tile.erase( navaids_by_tile.begin(), navaids_by_tile.end() );
68 ident_navaids.erase( ident_navaids.begin(), ident_navaids.end() );
73 bool FGTACANList::init() {
78 // real add a marker beacon
79 static void real_add( nav_map_type &navmap, const int master_index,
82 navmap[master_index].push_back( n );
86 // front end for add a marker beacon
87 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 );
144 // add an entry to the lists
145 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 );
152 // add an entry to the lists
153 bool FGTACANList::add( FGTACANRecord *c ) {
154 ident_channels[c->get_channel()].push_back(c);
158 FGNavRecord *FGNavList::findByFreq( double freq, double lon, double lat, double elev )
160 nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
161 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
162 SG_LOG( SG_INSTR, SG_DEBUG, "findbyFreq " << freq << " size " << stations.size() );
164 return findNavFromList( aircraft, stations );
168 FGNavRecord *FGNavList::findByIdent( const char* ident,
169 const double lon, const double lat )
171 nav_list_type stations = ident_navaids[ident];
172 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, 0.0) );
174 return findNavFromList( aircraft, stations );
178 nav_list_type FGNavList::findFirstByIdent( string ident, fg_nav_types type, bool exact)
184 if(type == FG_NAV_VOR) iType = 3;
185 else if(type == FG_NAV_NDB) iType = 2;
188 nav_ident_map_iterator it;
190 it = ident_navaids.find(ident);
192 bool typeMatch = false;
193 int safety_count = 0;
194 it = ident_navaids.lower_bound(ident);
196 nav_list_type n0 = it->second;
197 // local copy, so we should be able to do anything with n0.
198 // Remove the types that don't match request.
199 for(nav_list_iterator it0 = n0.begin(); it0 != n0.end();) {
200 FGNavRecord* nv = *it0;
201 if(nv->get_type() == iType) {
211 if(it == ident_navaids.begin()) {
212 // We didn't find a match before reaching the beginning of the map
217 if(safety_count == 1000000) {
218 SG_LOG(SG_INSTR, SG_ALERT,
219 "safety_count triggered exit from while loop in findFirstByIdent!");
223 if(it == ident_navaids.end()) {
229 if(it == ident_navaids.end()) {
233 nav_list_type n1 = it->second;
235 for(nav_list_iterator it2 = n1.begin(); it2 != n1.end(); ++it2) {
236 FGNavRecord* nv = *it2;
237 if(nv->get_type() == iType) n2.push_back(nv);
244 // Given an Ident and optional freqency, return the first matching
246 FGNavRecord *FGNavList::findByIdentAndFreq( const char* ident, const double freq )
248 nav_list_type stations = ident_navaids[ident];
249 SG_LOG( SG_INSTR, SG_DEBUG, "findByIdent " << ident<< " size " << stations.size() );
251 // sometimes there can be duplicated idents. If a freq is
252 // specified, use it to refine the search.
253 int f = (int)(freq*100.0 + 0.5);
254 for ( unsigned int i = 0; i < stations.size(); ++i ) {
255 if ( f == stations[i]->get_freq() ) {
259 } else if (stations.size()) {
267 // Given a point and a list of stations, return the closest one to the
269 FGNavRecord *FGNavList::findNavFromList( const Point3D &aircraft,
270 const nav_list_type &stations )
272 FGNavRecord *nav = NULL;
274 double d2; // in meters squared
276 = FG_NAV_MAX_RANGE*SG_NM_TO_METER*FG_NAV_MAX_RANGE*SG_NM_TO_METER;
278 // find the closest station within a sensible range (FG_NAV_MAX_RANGE)
279 for ( unsigned int i = 0; i < stations.size(); ++i ) {
280 // cout << "testing " << current->get_ident() << endl;
281 station = Point3D( stations[i]->get_x(),
282 stations[i]->get_y(),
283 stations[i]->get_z() );
285 d2 = aircraft.distance3Dsquared( station );
287 // cout << " dist = " << sqrt(d)
288 // << " range = " << current->get_range() * SG_NM_TO_METER
291 // LOC, ILS, GS, and DME antenna's could potentially be
292 // installed at the opposite end of the runway. So it's not
293 // enough to simply find the closest antenna with the right
294 // frequency. We need the closest antenna with the right
295 // frequency that is most oriented towards us. (We penalize
296 // stations that are facing away from us by adding 5000 meters
297 // which is further than matching stations would ever be
298 // placed from each other. (Do the expensive check only for
299 // directional atennas and only when there is a chance it is
300 // the closest station.)
301 if ( d2 < min_dist &&
302 (stations[i]->get_type() == 4 || stations[i]->get_type() == 5 ||
303 stations[i]->get_type() == 6 || stations[i]->get_type() == 12) )
305 double hdg_deg = 0.0;
306 if ( stations[i]->get_type() == 4 || stations[i]->get_type() == 5 ){
307 hdg_deg = stations[i]->get_multiuse();
308 } else if ( stations[i]->get_type() == 6 ) {
309 int tmp = (int)(stations[i]->get_multiuse() / 1000.0);
310 hdg_deg = stations[i]->get_multiuse() - (tmp * 1000);
311 } else if ( stations[i]->get_type() == 12 ) {
312 // oops, Robin's data format doesn't give us the
313 // needed information to compute a heading for a DME
314 // transmitter. FIXME Robin!
317 double az1 = 0.0, az2 = 0.0, s = 0.0;
318 double elev_m = 0.0, lat_rad = 0.0, lon_rad = 0.0;
319 double xyz[3] = { aircraft.x(), aircraft.y(), aircraft.z() };
320 sgCartToGeod( xyz, &lat_rad, &lon_rad, &elev_m );
321 geo_inverse_wgs_84( elev_m,
322 lat_rad * SG_RADIANS_TO_DEGREES,
323 lon_rad * SG_RADIANS_TO_DEGREES,
324 stations[i]->get_lat(), stations[i]->get_lon(),
326 az1 = az1 - stations[i]->get_multiuse();
327 if ( az1 > 180.0) az1 -= 360.0;
328 if ( az1 < -180.0) az1 += 360.0;
329 // penalize opposite facing stations by adding 5000 meters
330 // (squared) which is further than matching stations would
331 // ever be placed from each other.
332 if ( fabs(az1) > 90.0 ) {
333 double dist = sqrt(d2);
334 d2 = (dist + 5000) * (dist + 5000);
338 if ( d2 < min_dist ) {
348 // returns the closest entry to the give lon/lat/elev
349 FGNavRecord *FGNavList::findClosest( double lon_rad, double lat_rad,
350 double elev_m, fg_nav_types type)
352 FGNavRecord *result = NULL;
355 double lon_deg = lon_rad * SG_RADIANS_TO_DEGREES;
356 double lat_deg = lat_rad * SG_RADIANS_TO_DEGREES;
357 int lonidx = (int)lon_deg;
358 diff = lon_deg - (double)lonidx;
359 if ( (lon_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
364 int latidx = (int)lat_deg;
365 diff = lat_deg - (double)latidx;
366 if ( (lat_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
371 int master_index = lonidx * 1000 + latidx;
373 nav_list_type navs = navaids_by_tile[ master_index ];
374 // cout << "Master index = " << master_index << endl;
375 // cout << "beacon search length = " << beacons.size() << endl;
377 nav_list_const_iterator current = navs.begin();
378 nav_list_const_iterator last = navs.end();
380 Point3D aircraft = sgGeodToCart( Point3D(lon_rad,
384 double min_dist = 999999999.0;
386 for ( ; current != last ; ++current ) {
387 if(isTypeMatch(*current, type)) {
388 // cout << " testing " << (*current)->get_ident() << endl;
389 Point3D station = Point3D( (*current)->get_x(),
391 (*current)->get_z() );
392 // cout << " aircraft = " << aircraft << " station = " << station
395 double d = aircraft.distance3Dsquared( station ); // meters^2
396 // cout << " distance = " << d << " ("
397 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
398 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
401 // cout << " range = " << sqrt(d) << endl;
403 if ( d < min_dist ) {
410 // cout << "lon = " << lon << " lat = " << lat
411 // << " closest beacon = " << sqrt( min_dist ) << endl;
416 // Given a TACAN Channel return the first matching frequency
417 FGTACANRecord *FGTACANList::findByChannel( const string& channel )
419 tacan_list_type stations = ident_channels[channel];
420 SG_LOG( SG_INSTR, SG_DEBUG, "findByChannel " << channel<< " size " << stations.size() );
422 if (stations.size()) {
428 // Given a frequency, return the first matching station.
429 FGNavRecord *FGNavList::findStationByFreq( double freq )
431 nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
433 SG_LOG( SG_INSTR, SG_DEBUG, "findStationByFreq " << freq << " size " << stations.size() );
435 if (stations.size()) {