]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navlist.cxx
a145bac0d9d5d7090e4c237597062da2234d6366
[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
48 // FGNavList ------------------------------------------------------------------
49
50 FGNavList::FGNavList( void )
51 {
52 }
53
54
55 FGNavList::~FGNavList( void )
56 {
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() );
60 }
61
62
63 // load the navaids and build the map
64 bool FGNavList::init()
65 {
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() );
72
73     return true;
74 }
75
76
77 // real add a marker beacon
78 static void real_add( nav_map_type &navmap, const int master_index,
79                       FGNavRecord *n )
80 {
81     navmap[master_index].push_back( n );
82 }
83
84
85 // front end for add a marker beacon
86 static void tile_add( nav_map_type &navmap, FGNavRecord *n )
87 {
88     double diff = 0;
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 // add an entry to the lists
144 bool FGNavList::add( FGNavRecord *n )
145 {
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
153 FGNavRecord *FGNavList::findByFreq( double freq, double lon, double lat, double elev )
154 {
155     const nav_list_type& stations = navaids[(int)(freq*100.0 + 0.5)];
156
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()  );
160
161     return findNavFromList( aircraft, stations );
162 }
163
164
165 FGNavRecord *FGNavList::findByIdent( const char* ident,
166                                const double lon, const double lat )
167 {
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 );
172 }
173
174
175 nav_list_type FGNavList::findFirstByIdent( const string& ident, fg_nav_types type, bool exact)
176 {
177     nav_list_type n2;
178     n2.clear();
179
180     int iType;
181     if(type == FG_NAV_VOR) iType = 3;
182     else if(type == FG_NAV_NDB) iType = 2;
183     else return(n2);
184
185     nav_ident_map_iterator it;
186     if(exact) {
187         it = ident_navaids.find(ident);
188     } else {
189         bool typeMatch = false;
190         int safety_count = 0;
191         it = ident_navaids.lower_bound(ident);
192         while(!typeMatch) {
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) {
199                     typeMatch = true;
200                     ++it0;
201                 } else {
202                     it0 = n0.erase(it0);
203                 }
204             }
205             if(typeMatch) {
206                 return(n0);
207             }
208             if(it == ident_navaids.begin()) {
209                 // We didn't find a match before reaching the beginning of the map
210                 n0.clear();
211                 return(n0);
212             }
213             safety_count++;
214             if(safety_count == 1000000) {
215                 SG_LOG(SG_INSTR, SG_ALERT,
216                        "safety_count triggered exit from while loop in findFirstByIdent!");
217                 break;
218             }
219             ++it;
220             if(it == ident_navaids.end()) {
221                 n0.clear();
222                 return(n0);
223             }
224         }
225     }
226     if(it == ident_navaids.end()) {
227         n2.clear();
228         return(n2);
229     } else {
230         nav_list_type n1 = it->second;
231         n2.clear();
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);
235         }
236         return(n2);
237     }
238 }
239
240
241 // Given an Ident and optional freqency, return the first matching
242 // station.
243 FGNavRecord *FGNavList::findByIdentAndFreq( const char* ident, const double freq )
244 {
245     nav_list_type stations = ident_navaids[ident];
246     SG_LOG( SG_INSTR, SG_DEBUG, "findByIdent " << ident<< " size " << stations.size()  );
247     if ( freq > 0.0 ) {
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() ) {
254                 return (*it);
255             }
256         }
257     } else if (!stations.empty()) {
258         return stations[0];
259     }
260
261     return NULL;
262 }
263
264
265 // Given a point and a list of stations, return the closest one to the
266 // specified point.
267 FGNavRecord *FGNavList::findNavFromList( const SGVec3d &aircraft,
268                                          const nav_list_type &stations )
269 {
270     FGNavRecord *nav = NULL;
271     double d2;                  // in meters squared
272     double min_dist
273         = FG_NAV_MAX_RANGE*SG_NM_TO_METER*FG_NAV_MAX_RANGE*SG_NM_TO_METER;
274
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);
282
283         // cout << "  dist = " << sqrt(d)
284         //      << "  range = " << current->get_range() * SG_NM_TO_METER
285         //      << endl;
286
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) )
300         {
301             double hdg_deg = 0.0;
302             if ( type == 4 || type == 5 ){
303                 hdg_deg = station->get_multiuse();
304
305             } else if ( type == 6 ) {
306                 int tmp = (int)(station->get_multiuse() / 1000.0);
307                 hdg_deg = station->get_multiuse() - (tmp * 1000);
308
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!
313             }
314
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);
327             }
328         }
329
330         if ( d2 < min_dist ) {
331             min_dist = d2;
332             nav = station;
333         }
334     }
335
336     return nav;
337 }
338
339
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)
343 {
344     FGNavRecord *result = NULL;
345     double diff;
346
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) ) {
352         lonidx -= 1;
353     }
354     lonidx += 180;
355
356     int latidx = (int)lat_deg;
357     diff = lat_deg - (double)latidx;
358     if ( (lat_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
359         latidx -= 1;
360     }
361     latidx += 90;
362
363     int master_index = lonidx * 1000 + latidx;
364
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;
368
369     nav_list_const_iterator current = navs.begin();
370     nav_list_const_iterator last = navs.end();
371
372     SGGeod geod = SGGeod::fromRadM(lon_rad, lat_rad, elev_m);
373     SGVec3d aircraft = SGVec3d::fromGeod(geod);
374
375     double min_dist = 999999999.0;
376
377     for ( ; current != last ; ++current ) {
378         if(isTypeMatch(*current, type)) {
379             // cout << "  testing " << (*current)->get_ident() << endl;
380
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
385             //      << ")" << endl;
386
387             // cout << "  range = " << sqrt(d) << endl;
388
389             if ( d < min_dist ) {
390                 min_dist = d;
391                 result = (*current);
392             }
393         }
394     }
395
396     // cout << "lon = " << lon << " lat = " << lat
397     //      << "  closest beacon = " << sqrt( min_dist ) << endl;
398
399     return result;
400 }
401
402 // Given a frequency, return the first matching station.
403 FGNavRecord *FGNavList::findStationByFreq( double freq )
404 {
405     const nav_list_type& stations = navaids[(int)(freq*100.0 + 0.5)];
406
407     SG_LOG( SG_INSTR, SG_DEBUG, "findStationByFreq " << freq << " size " << stations.size()  );
408
409     if (!stations.empty()) {
410         return stations[0];
411     }
412     return NULL;
413 }
414
415
416
417 // FGTACANList ----------------------------------------------------------------
418
419 FGTACANList::FGTACANList( void )
420 {
421 }
422
423
424 FGTACANList::~FGTACANList( void )
425 {
426 }
427
428
429 bool FGTACANList::init()
430 {
431     return true;
432 }
433
434
435 // add an entry to the lists
436 bool FGTACANList::add( FGTACANRecord *c )
437 {
438     ident_channels[c->get_channel()].push_back(c);
439     return true;
440 }
441
442
443 // Given a TACAN Channel return the first matching frequency
444 FGTACANRecord *FGTACANList::findByChannel( const string& channel )
445 {
446     const tacan_list_type& stations = ident_channels[channel];
447     SG_LOG( SG_INSTR, SG_DEBUG, "findByChannel " << channel<< " size " << stations.size() );
448
449     if (!stations.empty()) {
450         return stations[0];
451     }
452     return NULL;
453 }
454
455