]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navlist.cxx
- untangle classes (methods of two classes were interwoven)
[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 }
58
59
60 // load the navaids and build the map
61 bool FGNavList::init()
62 {
63     // FIXME: leaves all the individual navaid entries leaked
64     navaids.erase( navaids.begin(), navaids.end() );
65     navaids_by_tile.erase( navaids_by_tile.begin(), navaids_by_tile.end() );
66     ident_navaids.erase( ident_navaids.begin(), ident_navaids.end() );
67
68     return true;
69 }
70
71
72 // real add a marker beacon
73 static void real_add( nav_map_type &navmap, const int master_index,
74                       FGNavRecord *n )
75 {
76     navmap[master_index].push_back( n );
77 }
78
79
80 // front end for add a marker beacon
81 static void tile_add( nav_map_type &navmap, FGNavRecord *n )
82 {
83     double diff;
84
85     double lon = n->get_lon();
86     double lat = n->get_lat();
87
88     int lonidx = (int)lon;
89     diff = lon - (double)lonidx;
90     if ( (lon < 0.0) && (fabs(diff) > SG_EPSILON) ) {
91         lonidx -= 1;
92     }
93     double lonfrac = lon - (double)lonidx;
94     lonidx += 180;
95
96     int latidx = (int)lat;
97     diff = lat - (double)latidx;
98     if ( (lat < 0.0) && (fabs(diff) > SG_EPSILON) ) {
99         latidx -= 1;
100     }
101     double latfrac = lat - (double)latidx;
102     latidx += 90;
103
104     int master_index = lonidx * 1000 + latidx;
105     // cout << "lonidx = " << lonidx << " latidx = " << latidx << "  ";
106     // cout << "Master index = " << master_index << endl;
107
108     // add to the actual bucket
109     real_add( navmap, master_index, n );
110
111     // if we are close to the edge, add to adjacent buckets so we only
112     // have to search one bucket at run time
113
114     // there are 8 cases since there are 8 adjacent tiles
115
116     if ( lonfrac < 0.2 ) {
117         real_add( navmap, master_index - 1000, n );
118         if ( latfrac < 0.2 ) {
119             real_add( navmap, master_index - 1000 - 1, n );
120         } else if ( latfrac > 0.8 ) {
121             real_add( navmap, master_index - 1000 + 1, n );
122         }
123     } else if ( lonfrac > 0.8 ) {
124         real_add( navmap, master_index + 1000, n );
125         if ( latfrac < 0.2 ) {
126             real_add( navmap, master_index + 1000 - 1, n );
127         } else if ( latfrac > 0.8 ) {
128             real_add( navmap, master_index + 1000 + 1, n );
129         }
130     } else if ( latfrac < 0.2 ) {
131         real_add( navmap, master_index - 1, n );
132     } else if ( latfrac > 0.8 ) {
133         real_add( navmap, master_index + 1, n );
134     }
135 }
136
137
138 // add an entry to the lists
139 bool FGNavList::add( FGNavRecord *n )
140 {
141     navaids[n->get_freq()].push_back(n);
142     ident_navaids[n->get_ident()].push_back(n);
143     tile_add( navaids_by_tile, n );
144     return true;
145 }
146
147
148 FGNavRecord *FGNavList::findByFreq( double freq, double lon, double lat, double elev )
149 {
150     const nav_list_type& stations = navaids[(int)(freq*100.0 + 0.5)];
151
152     SGGeod geod = SGGeod::fromRadM(lon, lat, elev);
153     SGVec3d aircraft = SGVec3d::fromGeod(geod);
154     SG_LOG( SG_INSTR, SG_DEBUG, "findbyFreq " << freq << " size " << stations.size()  );
155
156     return findNavFromList( aircraft, stations );
157 }
158
159
160 FGNavRecord *FGNavList::findByIdent( const char* ident,
161                                const double lon, const double lat )
162 {
163     const nav_list_type& stations = ident_navaids[ident];
164     SGGeod geod = SGGeod::fromRad(lon, lat);
165     SGVec3d aircraft = SGVec3d::fromGeod(geod);
166     return findNavFromList( aircraft, stations );
167 }
168
169
170 nav_list_type FGNavList::findFirstByIdent( const string& ident, fg_nav_types type, bool exact)
171 {
172     nav_list_type n2;
173     n2.clear();
174
175     int iType;
176     if(type == FG_NAV_VOR) iType = 3;
177     else if(type == FG_NAV_NDB) iType = 2;
178     else return(n2);
179
180     nav_ident_map_iterator it;
181     if(exact) {
182         it = ident_navaids.find(ident);
183     } else {
184         bool typeMatch = false;
185         int safety_count = 0;
186         it = ident_navaids.lower_bound(ident);
187         while(!typeMatch) {
188             nav_list_type n0 = it->second;
189             // local copy, so we should be able to do anything with n0.
190             // Remove the types that don't match request.
191             for(nav_list_iterator it0 = n0.begin(); it0 != n0.end();) {
192                 FGNavRecord* nv = *it0;
193                 if(nv->get_type() == iType) {
194                     typeMatch = true;
195                     ++it0;
196                 } else {
197                     it0 = n0.erase(it0);
198                 }
199             }
200             if(typeMatch) {
201                 return(n0);
202             }
203             if(it == ident_navaids.begin()) {
204                 // We didn't find a match before reaching the beginning of the map
205                 n0.clear();
206                 return(n0);
207             }
208             safety_count++;
209             if(safety_count == 1000000) {
210                 SG_LOG(SG_INSTR, SG_ALERT,
211                        "safety_count triggered exit from while loop in findFirstByIdent!");
212                 break;
213             }
214             ++it;
215             if(it == ident_navaids.end()) {
216                 n0.clear();
217                 return(n0);
218             }
219         }
220     }
221     if(it == ident_navaids.end()) {
222         n2.clear();
223         return(n2);
224     } else {
225         nav_list_type n1 = it->second;
226         n2.clear();
227         for(nav_list_iterator it2 = n1.begin(); it2 != n1.end(); ++it2) {
228             FGNavRecord* nv = *it2;
229             if(nv->get_type() == iType) n2.push_back(nv);
230         }
231         return(n2);
232     }
233 }
234
235
236 // Given an Ident and optional freqency, return the first matching
237 // station.
238 FGNavRecord *FGNavList::findByIdentAndFreq( const char* ident, const double freq )
239 {
240     nav_list_type stations = ident_navaids[ident];
241     SG_LOG( SG_INSTR, SG_DEBUG, "findByIdent " << ident<< " size " << stations.size()  );
242     if ( freq > 0.0 ) {
243         // sometimes there can be duplicated idents.  If a freq is
244         // specified, use it to refine the search.
245         int f = (int)(freq*100.0 + 0.5);
246         for ( unsigned int i = 0; i < stations.size(); ++i ) {
247             if ( f == stations[i]->get_freq() ) {
248                 return stations[i];
249             }
250         }
251     } else if (stations.size()) {
252         return stations[0];
253     }
254
255     return NULL;
256 }
257
258
259 // Given a point and a list of stations, return the closest one to the
260 // specified point.
261 FGNavRecord *FGNavList::findNavFromList( const SGVec3d &aircraft,
262                                          const nav_list_type &stations )
263 {
264     FGNavRecord *nav = NULL;
265     double d2;                  // in meters squared
266     double min_dist
267         = FG_NAV_MAX_RANGE*SG_NM_TO_METER*FG_NAV_MAX_RANGE*SG_NM_TO_METER;
268
269     // find the closest station within a sensible range (FG_NAV_MAX_RANGE)
270     for ( unsigned int i = 0; i < stations.size(); ++i ) {
271         // cout << "testing " << current->get_ident() << endl;
272         d2 = distSqr(stations[i]->get_cart(), aircraft);
273
274         // cout << "  dist = " << sqrt(d)
275         //      << "  range = " << current->get_range() * SG_NM_TO_METER
276         //      << endl;
277
278         // LOC, ILS, GS, and DME antenna's could potentially be
279         // installed at the opposite end of the runway.  So it's not
280         // enough to simply find the closest antenna with the right
281         // frequency.  We need the closest antenna with the right
282         // frequency that is most oriented towards us.  (We penalize
283         // stations that are facing away from us by adding 5000 meters
284         // which is further than matching stations would ever be
285         // placed from each other.  (Do the expensive check only for
286         // directional atennas and only when there is a chance it is
287         // the closest station.)
288         if ( d2 < min_dist &&
289              (stations[i]->get_type() == 4 || stations[i]->get_type() == 5 ||
290               stations[i]->get_type() == 6 || stations[i]->get_type() == 12 ||
291               stations[i]->get_type() == 13) )
292         {
293             double hdg_deg = 0.0;
294             if ( stations[i]->get_type() == 4 || stations[i]->get_type() == 5 ){
295                 hdg_deg = stations[i]->get_multiuse();
296             } else if ( stations[i]->get_type() == 6 ) {
297                 int tmp = (int)(stations[i]->get_multiuse() / 1000.0);
298                 hdg_deg = stations[i]->get_multiuse() - (tmp * 1000);
299             } else if ( stations[i]->get_type() == 12 ||
300                         stations[i]->get_type() == 13 ) {
301                 // oops, Robin's data format doesn't give us the
302                 // needed information to compute a heading for a DME
303                 // transmitter.  FIXME Robin!
304             }
305
306             double az1 = 0.0, az2 = 0.0, s = 0.0;
307             SGGeod geod = SGGeod::fromCart(aircraft);
308             geo_inverse_wgs_84( geod, stations[i]->get_pos(),
309                                 &az1, &az2, &s);
310             az1 = az1 - stations[i]->get_multiuse();
311             if ( az1 >  180.0) az1 -= 360.0;
312             if ( az1 < -180.0) az1 += 360.0;
313             // penalize opposite facing stations by adding 5000 meters
314             // (squared) which is further than matching stations would
315             // ever be placed from each other.
316             if ( fabs(az1) > 90.0 ) {
317                 double dist = sqrt(d2);
318                 d2 = (dist + 5000) * (dist + 5000);
319             }
320         }
321
322         if ( d2 < min_dist ) {
323             min_dist = d2;
324             nav = stations[i];
325         }
326     }
327
328     return nav;
329 }
330
331
332 // returns the closest entry to the give lon/lat/elev
333 FGNavRecord *FGNavList::findClosest( double lon_rad, double lat_rad,
334                                      double elev_m, fg_nav_types type)
335 {
336     FGNavRecord *result = NULL;
337     double diff;
338
339     double lon_deg = lon_rad * SG_RADIANS_TO_DEGREES;
340     double lat_deg = lat_rad * SG_RADIANS_TO_DEGREES;
341     int lonidx = (int)lon_deg;
342     diff = lon_deg - (double)lonidx;
343     if ( (lon_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
344         lonidx -= 1;
345     }
346     lonidx += 180;
347
348     int latidx = (int)lat_deg;
349     diff = lat_deg - (double)latidx;
350     if ( (lat_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
351         latidx -= 1;
352     }
353     latidx += 90;
354
355     int master_index = lonidx * 1000 + latidx;
356
357     const nav_list_type& navs = navaids_by_tile[ master_index ];
358     // cout << "Master index = " << master_index << endl;
359     // cout << "beacon search length = " << beacons.size() << endl;
360
361     nav_list_const_iterator current = navs.begin();
362     nav_list_const_iterator last = navs.end();
363
364     SGGeod geod = SGGeod::fromRadM(lon_rad, lat_rad, elev_m);
365     SGVec3d aircraft = SGVec3d::fromGeod(geod);
366
367     double min_dist = 999999999.0;
368
369     for ( ; current != last ; ++current ) {
370         if(isTypeMatch(*current, type)) {
371             // cout << "  testing " << (*current)->get_ident() << endl;
372
373             double d = distSqr((*current)->get_cart(), aircraft);
374             // cout << "  distance = " << d << " ("
375             //      << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
376             //         * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
377             //      << ")" << endl;
378
379             // cout << "  range = " << sqrt(d) << endl;
380
381             if ( d < min_dist ) {
382                 min_dist = d;
383                 result = (*current);
384             }
385         }
386     }
387
388     // cout << "lon = " << lon << " lat = " << lat
389     //      << "  closest beacon = " << sqrt( min_dist ) << endl;
390
391     return result;
392 }
393
394 // Given a frequency, return the first matching station.
395 FGNavRecord *FGNavList::findStationByFreq( double freq )
396 {
397     const nav_list_type& stations = navaids[(int)(freq*100.0 + 0.5)];
398
399     SG_LOG( SG_INSTR, SG_DEBUG, "findStationByFreq " << freq << " size " << stations.size()  );
400
401     if (stations.size()) {
402         return stations[0];
403     }
404     return NULL;
405 }
406
407
408
409 // FGTACANList ----------------------------------------------------------------
410
411 FGTACANList::FGTACANList( void )
412 {
413 }
414
415
416 FGTACANList::~FGTACANList( void )
417 {
418 }
419
420
421 bool FGTACANList::init()
422 {
423     return true;
424 }
425
426
427 // add an entry to the lists
428 bool FGTACANList::add( FGTACANRecord *c )
429 {
430     ident_channels[c->get_channel()].push_back(c);
431     return true;
432 }
433
434
435 // Given a TACAN Channel return the first matching frequency
436 FGTACANRecord *FGTACANList::findByChannel( const string& channel )
437 {
438     const tacan_list_type& stations = ident_channels[channel];
439     SG_LOG( SG_INSTR, SG_DEBUG, "findByChannel " << channel<< " size " << stations.size() );
440
441     if (stations.size()) {
442         return stations[0];
443     }
444     return NULL;
445 }
446
447