]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navlist.cxx
new FSF address
[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 <simgear/debug/logstream.hxx>
29 #include <simgear/misc/sgstream.hxx>
30 #include <simgear/math/sg_geodesy.hxx>
31
32 #include "navlist.hxx"
33
34
35 // Return true if the nav record matches the type
36 static bool isTypeMatch(const FGNavRecord* n, fg_nav_types type) {
37     switch(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;
43     }
44 }
45
46
47 // Constructor
48 FGNavList::FGNavList( void ) {
49 }
50
51 FGTACANList::FGTACANList( void ){
52 }
53
54
55 // Destructor
56 FGNavList::~FGNavList( void ) {
57 }
58
59 FGTACANList::~FGTACANList( void ){
60 }
61
62 // load the navaids and build the map
63 bool FGNavList::init() {
64
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() );
69
70     return true;
71 }
72
73 bool FGTACANList::init() {
74     
75     return true;      
76 }
77
78 // real add a marker beacon
79 static void real_add( nav_map_type &navmap, const int master_index,
80                       FGNavRecord *n )
81 {
82     navmap[master_index].push_back( n );
83 }
84
85
86 // front end for add a marker beacon
87 static void tile_add( nav_map_type &navmap, FGNavRecord *n ) {
88     double diff;
89
90     double lon = n->get_lon();
91     double lat = n->get_lat();
92
93     int lonidx = (int)lon;
94     diff = lon - (double)lonidx;
95     if ( (lon < 0.0) && (fabs(diff) > SG_EPSILON) ) {
96         lonidx -= 1;
97     }
98     double lonfrac = lon - (double)lonidx;
99     lonidx += 180;
100
101     int latidx = (int)lat;
102     diff = lat - (double)latidx;
103     if ( (lat < 0.0) && (fabs(diff) > SG_EPSILON) ) {
104         latidx -= 1;
105     }
106     double latfrac = lat - (double)latidx;
107     latidx += 90;
108
109     int master_index = lonidx * 1000 + latidx;
110     // cout << "lonidx = " << lonidx << " latidx = " << latidx << "  ";
111     // cout << "Master index = " << master_index << endl;
112
113     // add to the actual bucket
114     real_add( navmap, master_index, n );
115
116     // if we are close to the edge, add to adjacent buckets so we only
117     // have to search one bucket at run time
118
119     // there are 8 cases since there are 8 adjacent tiles
120
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 );
127         }
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 );
134         }
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 );
139     }
140 }
141
142
143
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 );
149     return true;
150 }
151
152 // add an entry to the lists
153 bool FGTACANList::add( FGTACANRecord *c ) {
154     ident_channels[c->get_channel()].push_back(c);
155     return true;
156 }
157
158 FGNavRecord *FGNavList::findByFreq( double freq, double lon, double lat, double elev )
159 {
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()  );
163
164     return findNavFromList( aircraft, stations );
165 }
166
167
168 FGNavRecord *FGNavList::findByIdent( const char* ident,
169                                const double lon, const double lat )
170 {
171     nav_list_type stations = ident_navaids[ident];
172     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, 0.0) );
173
174     return findNavFromList( aircraft, stations );
175 }
176
177
178 nav_list_type FGNavList::findFirstByIdent( string ident, fg_nav_types type, bool exact)
179 {
180     nav_list_type n2;
181     n2.clear();
182     
183     int iType;
184     if(type == FG_NAV_VOR) iType = 3;
185     else if(type == FG_NAV_NDB) iType = 2;
186     else return(n2);
187     
188     nav_ident_map_iterator it;
189     if(exact) {
190         it = ident_navaids.find(ident);
191     } else {
192         bool typeMatch = false;
193         int safety_count = 0;
194         it = ident_navaids.lower_bound(ident);
195         while(!typeMatch) {
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) {
202                     typeMatch = true;
203                     ++it0;
204                 } else {
205                     it0 = n0.erase(it0);
206                 }
207             }
208             if(typeMatch) {
209                 return(n0);
210             }
211             if(it == ident_navaids.begin()) {
212                 // We didn't find a match before reaching the beginning of the map
213                 n0.clear();
214                 return(n0);             
215             }
216             safety_count++;
217             if(safety_count == 1000000) {
218                 SG_LOG(SG_INSTR, SG_ALERT,
219                        "safety_count triggered exit from while loop in findFirstByIdent!");
220                 break;
221             }
222             ++it;
223             if(it == ident_navaids.end()) {
224                 n0.clear();
225                 return(n0);
226             }
227         }
228     }
229     if(it == ident_navaids.end()) {
230         n2.clear();
231         return(n2);
232     } else {
233         nav_list_type n1 = it->second;
234         n2.clear();
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);
238         }
239         return(n2);
240     }
241 }
242
243
244 // Given an Ident and optional freqency, return the first matching
245 // station.
246 FGNavRecord *FGNavList::findByIdentAndFreq( const char* ident, const double freq )
247 {
248     nav_list_type stations = ident_navaids[ident];
249     SG_LOG( SG_INSTR, SG_DEBUG, "findByIdent " << ident<< " size " << stations.size()  );
250     if ( freq > 0.0 ) {
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() ) {
256                 return stations[i];
257             }
258         }
259     } else if (stations.size()) {
260         return stations[0];
261     }
262
263     return NULL;
264 }
265
266
267 // Given a point and a list of stations, return the closest one to the
268 // specified point.
269 FGNavRecord *FGNavList::findNavFromList( const Point3D &aircraft, 
270                                          const nav_list_type &stations )
271 {
272     FGNavRecord *nav = NULL;
273     Point3D station;
274     double d2;                  // in meters squared
275     double min_dist
276         = FG_NAV_MAX_RANGE*SG_NM_TO_METER*FG_NAV_MAX_RANGE*SG_NM_TO_METER;
277
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() );
284
285         d2 = aircraft.distance3Dsquared( station );
286
287         // cout << "  dist = " << sqrt(d)
288         //      << "  range = " << current->get_range() * SG_NM_TO_METER
289         //      << endl;
290
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) )
304         {
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!
315             }
316
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(),
325                                 &az1, &az2, &s);
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);
335             }
336         }
337
338         if ( d2 < min_dist ) {
339             min_dist = d2;
340             nav = stations[i];
341         }
342     }
343
344     return nav;
345 }
346
347
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)
351 {
352     FGNavRecord *result = NULL;
353     double diff;
354
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) ) {
360         lonidx -= 1;
361     }
362     lonidx += 180;
363
364     int latidx = (int)lat_deg;
365     diff = lat_deg - (double)latidx;
366     if ( (lat_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
367         latidx -= 1;
368     }
369     latidx += 90;
370
371     int master_index = lonidx * 1000 + latidx;
372
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;
376
377     nav_list_const_iterator current = navs.begin();
378     nav_list_const_iterator last = navs.end();
379
380     Point3D aircraft = sgGeodToCart( Point3D(lon_rad,
381                                              lat_rad,
382                                              elev_m) );
383
384     double min_dist = 999999999.0;
385
386     for ( ; current != last ; ++current ) {
387         if(isTypeMatch(*current, type)) {
388             // cout << "  testing " << (*current)->get_ident() << endl;
389             Point3D station = Point3D( (*current)->get_x(),
390                                        (*current)->get_y(),
391                                        (*current)->get_z() );
392             // cout << "    aircraft = " << aircraft << " station = " << station 
393             //      << endl;
394     
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
399             //      << ")" << endl;
400     
401             // cout << "  range = " << sqrt(d) << endl;
402     
403             if ( d < min_dist ) {
404                 min_dist = d;
405                 result = (*current);
406             }
407         }
408     }
409
410     // cout << "lon = " << lon << " lat = " << lat
411     //      << "  closest beacon = " << sqrt( min_dist ) << endl;
412
413     return result;
414 }
415
416 // Given a TACAN Channel return the first matching frequency
417 FGTACANRecord *FGTACANList::findByChannel( const string& channel )
418 {
419     tacan_list_type stations = ident_channels[channel];
420     SG_LOG( SG_INSTR, SG_DEBUG, "findByChannel " << channel<< " size " << stations.size()  );
421     
422     if (stations.size()) {
423         return stations[0];
424     }    
425     return NULL;
426 }
427
428 // Given a frequency, return the first matching station.
429 FGNavRecord *FGNavList::findStationByFreq( double freq )
430 {
431     nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
432    
433     SG_LOG( SG_INSTR, SG_DEBUG, "findStationByFreq " << freq << " size " << stations.size()  );
434     
435     if (stations.size()) {
436         return stations[0];
437     }    
438     return NULL;
439 }