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"
36 FGNavList::FGNavList( void ) {
39 FGTACANList::FGTACANList( void ){
44 FGNavList::~FGNavList( void ) {
47 FGTACANList::~FGTACANList( void ){
50 // load the navaids and build the map
51 bool FGNavList::init() {
53 // FIXME: leaves all the individual navaid entries leaked
54 navaids.erase( navaids.begin(), navaids.end() );
55 navaids_by_tile.erase( navaids_by_tile.begin(), navaids_by_tile.end() );
56 ident_navaids.erase( ident_navaids.begin(), ident_navaids.end() );
61 bool FGTACANList::init() {
66 // real add a marker beacon
67 static void real_add( nav_map_type &navmap, const int master_index,
70 navmap[master_index].push_back( n );
74 // front end for add a marker beacon
75 static void tile_add( nav_map_type &navmap, FGNavRecord *n ) {
78 double lon = n->get_lon();
79 double lat = n->get_lat();
81 int lonidx = (int)lon;
82 diff = lon - (double)lonidx;
83 if ( (lon < 0.0) && (fabs(diff) > SG_EPSILON) ) {
86 double lonfrac = lon - (double)lonidx;
89 int latidx = (int)lat;
90 diff = lat - (double)latidx;
91 if ( (lat < 0.0) && (fabs(diff) > SG_EPSILON) ) {
94 double latfrac = lat - (double)latidx;
97 int master_index = lonidx * 1000 + latidx;
98 // cout << "lonidx = " << lonidx << " latidx = " << latidx << " ";
99 // cout << "Master index = " << master_index << endl;
101 // add to the actual bucket
102 real_add( navmap, master_index, n );
104 // if we are close to the edge, add to adjacent buckets so we only
105 // have to search one bucket at run time
107 // there are 8 cases since there are 8 adjacent tiles
109 if ( lonfrac < 0.2 ) {
110 real_add( navmap, master_index - 1000, n );
111 if ( latfrac < 0.2 ) {
112 real_add( navmap, master_index - 1000 - 1, n );
113 } else if ( latfrac > 0.8 ) {
114 real_add( navmap, master_index - 1000 + 1, n );
116 } else if ( lonfrac > 0.8 ) {
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 );
123 } else if ( latfrac < 0.2 ) {
124 real_add( navmap, master_index - 1, n );
125 } else if ( latfrac > 0.8 ) {
126 real_add( navmap, master_index + 1, n );
132 // add an entry to the lists
133 bool FGNavList::add( FGNavRecord *n ) {
134 navaids[n->get_freq()].push_back(n);
135 ident_navaids[n->get_ident()].push_back(n);
136 tile_add( navaids_by_tile, n );
140 // add an entry to the lists
141 bool FGTACANList::add( FGTACANRecord *c ) {
142 ident_channels[c->get_channel()].push_back(c);
146 FGNavRecord *FGNavList::findByFreq( double freq, double lon, double lat, double elev )
148 nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
149 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
150 SG_LOG( SG_INSTR, SG_DEBUG, "findbyFreq " << freq << " size " << stations.size() );
152 return findNavFromList( aircraft, stations );
156 FGNavRecord *FGNavList::findByIdent( const char* ident,
157 const double lon, const double lat )
159 nav_list_type stations = ident_navaids[ident];
160 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, 0.0) );
162 return findNavFromList( aircraft, stations );
166 // Given an Ident and optional freqency, return the first matching
168 FGNavRecord *FGNavList::findByIdentAndFreq( const char* ident, const double freq )
170 nav_list_type stations = ident_navaids[ident];
171 SG_LOG( SG_INSTR, SG_DEBUG, "findByIdent " << ident<< " size " << stations.size() );
173 // sometimes there can be duplicated idents. If a freq is
174 // specified, use it to refine the search.
175 int f = (int)(freq*100.0 + 0.5);
176 for ( unsigned int i = 0; i < stations.size(); ++i ) {
177 if ( f == stations[i]->get_freq() ) {
181 } else if (stations.size()) {
189 // Given a point and a list of stations, return the closest one to the
191 FGNavRecord *FGNavList::findNavFromList( const Point3D &aircraft,
192 const nav_list_type &stations )
194 FGNavRecord *nav = NULL;
196 double d2; // in meters squared
198 = FG_NAV_MAX_RANGE*SG_NM_TO_METER*FG_NAV_MAX_RANGE*SG_NM_TO_METER;
200 // find the closest station within a sensible range (FG_NAV_MAX_RANGE)
201 for ( unsigned int i = 0; i < stations.size(); ++i ) {
202 // cout << "testing " << current->get_ident() << endl;
203 station = Point3D( stations[i]->get_x(),
204 stations[i]->get_y(),
205 stations[i]->get_z() );
207 d2 = aircraft.distance3Dsquared( station );
209 // cout << " dist = " << sqrt(d)
210 // << " range = " << current->get_range() * SG_NM_TO_METER
213 // LOC, ILS, GS, and DME antenna's could potentially be
214 // installed at the opposite end of the runway. So it's not
215 // enough to simply find the closest antenna with the right
216 // frequency. We need the closest antenna with the right
217 // frequency that is most oriented towards us. (We penalize
218 // stations that are facing away from us by adding 5000 meters
219 // which is further than matching stations would ever be
220 // placed from each other. (Do the expensive check only for
221 // directional atennas and only when there is a chance it is
222 // the closest station.)
223 if ( d2 < min_dist &&
224 (stations[i]->get_type() == 4 || stations[i]->get_type() == 5 ||
225 stations[i]->get_type() == 6 || stations[i]->get_type() == 12) )
227 double hdg_deg = 0.0;
228 if ( stations[i]->get_type() == 4 || stations[i]->get_type() == 5 ){
229 hdg_deg = stations[i]->get_multiuse();
230 } else if ( stations[i]->get_type() == 6 ) {
231 int tmp = (int)(stations[i]->get_multiuse() / 1000.0);
232 hdg_deg = stations[i]->get_multiuse() - (tmp * 1000);
233 } else if ( stations[i]->get_type() == 12 ) {
234 // oops, Robin's data format doesn't give us the
235 // needed information to compute a heading for a DME
236 // transmitter. FIXME Robin!
239 double az1 = 0.0, az2 = 0.0, s = 0.0;
240 double elev_m = 0.0, lat_rad = 0.0, lon_rad = 0.0;
241 double xyz[3] = { aircraft.x(), aircraft.y(), aircraft.z() };
242 sgCartToGeod( xyz, &lat_rad, &lon_rad, &elev_m );
243 geo_inverse_wgs_84( elev_m,
244 lat_rad * SG_RADIANS_TO_DEGREES,
245 lon_rad * SG_RADIANS_TO_DEGREES,
246 stations[i]->get_lat(), stations[i]->get_lon(),
248 az1 = az1 - stations[i]->get_multiuse();
249 if ( az1 > 180.0) az1 -= 360.0;
250 if ( az1 < -180.0) az1 += 360.0;
251 // penalize opposite facing stations by adding 5000 meters
252 // (squared) which is further than matching stations would
253 // ever be placed from each other.
254 if ( fabs(az1) > 90.0 ) {
255 double dist = sqrt(d2);
256 d2 = (dist + 5000) * (dist + 5000);
260 if ( d2 < min_dist ) {
270 // returns the closest entry to the give lon/lat/elev
271 FGNavRecord *FGNavList::findClosest( double lon_rad, double lat_rad,
274 FGNavRecord *result = NULL;
277 double lon_deg = lon_rad * SG_RADIANS_TO_DEGREES;
278 double lat_deg = lat_rad * SG_RADIANS_TO_DEGREES;
279 int lonidx = (int)lon_deg;
280 diff = lon_deg - (double)lonidx;
281 if ( (lon_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
286 int latidx = (int)lat_deg;
287 diff = lat_deg - (double)latidx;
288 if ( (lat_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
293 int master_index = lonidx * 1000 + latidx;
295 nav_list_type navs = navaids_by_tile[ master_index ];
296 // cout << "Master index = " << master_index << endl;
297 // cout << "beacon search length = " << beacons.size() << endl;
299 nav_list_iterator current = navs.begin();
300 nav_list_iterator last = navs.end();
302 Point3D aircraft = sgGeodToCart( Point3D(lon_rad,
306 double min_dist = 999999999.0;
308 for ( ; current != last ; ++current ) {
309 // cout << " testing " << (*current)->get_ident() << endl;
310 Point3D station = Point3D( (*current)->get_x(),
312 (*current)->get_z() );
313 // cout << " aircraft = " << aircraft << " station = " << station
316 double d = aircraft.distance3Dsquared( station ); // meters^2
317 // cout << " distance = " << d << " ("
318 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
319 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
322 // cout << " range = " << sqrt(d) << endl;
324 if ( d < min_dist ) {
330 // cout << "lon = " << lon << " lat = " << lat
331 // << " closest beacon = " << sqrt( min_dist ) << endl;
336 // Given a TACAN Channel return the first matching frequency
337 FGTACANRecord *FGTACANList::findByChannel( const string& channel )
339 tacan_list_type stations = ident_channels[channel];
340 SG_LOG( SG_INSTR, SG_DEBUG, "findByChannel " << channel<< " size " << stations.size() );
342 if (stations.size()) {
348 // Given a frequency, return the first matching station.
349 FGNavRecord *FGNavList::findStationByFreq( double freq )
351 nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
353 SG_LOG( SG_INSTR, SG_DEBUG, "findStationByFreq " << freq << " size " << stations.size() );
355 if (stations.size()) {