]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navlist.cxx
Ed Sirett submitted a patch to consider antenna orientation when searching
[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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, 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 // Constructor
36 FGNavList::FGNavList( void ) {
37 }
38
39
40 // Destructor
41 FGNavList::~FGNavList( void ) {
42 }
43
44
45 // load the navaids and build the map
46 bool FGNavList::init() {
47
48     // FIXME: leaves all the individual navaid entries leaked
49     navaids.erase( navaids.begin(), navaids.end() );
50     navaids_by_tile.erase( navaids_by_tile.begin(), navaids_by_tile.end() );
51     ident_navaids.erase( ident_navaids.begin(), ident_navaids.end() );
52
53     return true;
54 }
55
56
57 // real add a marker beacon
58 static void real_add( nav_map_type &navmap, const int master_index,
59                       FGNavRecord *n )
60 {
61     navmap[master_index].push_back( n );
62 }
63
64
65 // front end for add a marker beacon
66 static void tile_add( nav_map_type &navmap, FGNavRecord *n ) {
67     double diff;
68
69     double lon = n->get_lon();
70     double lat = n->get_lat();
71
72     int lonidx = (int)lon;
73     diff = lon - (double)lonidx;
74     if ( (lon < 0.0) && (fabs(diff) > SG_EPSILON) ) {
75         lonidx -= 1;
76     }
77     double lonfrac = lon - (double)lonidx;
78     lonidx += 180;
79
80     int latidx = (int)lat;
81     diff = lat - (double)latidx;
82     if ( (lat < 0.0) && (fabs(diff) > SG_EPSILON) ) {
83         latidx -= 1;
84     }
85     double latfrac = lat - (double)latidx;
86     latidx += 90;
87
88     int master_index = lonidx * 1000 + latidx;
89     // cout << "lonidx = " << lonidx << " latidx = " << latidx << "  ";
90     // cout << "Master index = " << master_index << endl;
91
92     // add to the actual bucket
93     real_add( navmap, master_index, n );
94
95     // if we are close to the edge, add to adjacent buckets so we only
96     // have to search one bucket at run time
97
98     // there are 8 cases since there are 8 adjacent tiles
99
100     if ( lonfrac < 0.2 ) {
101         real_add( navmap, master_index - 1000, n );
102         if ( latfrac < 0.2 ) {
103             real_add( navmap, master_index - 1000 - 1, n );
104         } else if ( latfrac > 0.8 ) {
105             real_add( navmap, master_index - 1000 + 1, n );
106         }
107     } else if ( lonfrac > 0.8 ) {
108         real_add( navmap, master_index + 1000, n );
109         if ( latfrac < 0.2 ) {
110             real_add( navmap, master_index + 1000 - 1, n );
111         } else if ( latfrac > 0.8 ) {
112             real_add( navmap, master_index + 1000 + 1, n );
113         }
114     } else if ( latfrac < 0.2 ) {
115         real_add( navmap, master_index - 1, n );
116     } else if ( latfrac > 0.8 ) {
117         real_add( navmap, master_index + 1, n );
118     }
119 }
120
121
122
123 // add an entry to the lists
124 bool FGNavList::add( FGNavRecord *n ) {
125     navaids[n->get_freq()].push_back(n);
126     ident_navaids[n->get_ident()].push_back(n);
127     tile_add( navaids_by_tile, n );
128     return true;
129 }
130
131
132 // Query the database for the specified frequency.  It is assumed that
133 // there will be multiple stations with matching frequencies so a
134 // position must be specified.  Lon and lat are in degrees, elev is in
135 // meters.
136 FGNavRecord *FGNavList::findByFreq( double freq, double lon, double lat, double elev )
137 {
138     nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
139     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
140
141     return findNavFromList( aircraft, stations );
142 }
143
144
145 FGNavRecord *FGNavList::findByIdent( const char* ident,
146                                const double lon, const double lat )
147 {
148     nav_list_type stations = ident_navaids[ident];
149     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, 0.0) );
150
151     return findNavFromList( aircraft, stations );
152 }
153
154
155 // Given an Ident and optional freqency, return the first matching
156 // station.
157 FGNavRecord *FGNavList::findByIdentAndFreq( const char* ident, const double freq )
158 {
159     nav_list_type stations = ident_navaids[ident];
160
161     if ( freq > 0.0 ) {
162         // sometimes there can be duplicated idents.  If a freq is
163         // specified, use it to refine the search.
164         int f = (int)(freq*100.0 + 0.5);
165         for ( unsigned int i = 0; i < stations.size(); ++i ) {
166             if ( f == stations[i]->get_freq() ) {
167                 return stations[i];
168             }
169         }
170     } else {
171         return stations[0];
172     }
173
174     return NULL;
175 }
176
177
178 // Given a point and a list of stations, return the closest one to the
179 // specified point.
180 FGNavRecord *FGNavList::findNavFromList( const Point3D &aircraft, 
181                                          const nav_list_type &stations )
182 {
183     FGNavRecord *nav = NULL;
184     Point3D station;
185     double d2;                  // in meters squared
186     double min_dist
187         = FG_NAV_MAX_RANGE*SG_NM_TO_METER*FG_NAV_MAX_RANGE*SG_NM_TO_METER;
188
189     // find the closest station within a sensible range (FG_NAV_MAX_RANGE)
190     for ( unsigned int i = 0; i < stations.size(); ++i ) {
191         // cout << "testing " << current->get_ident() << endl;
192         station = Point3D( stations[i]->get_x(),
193                            stations[i]->get_y(),
194                            stations[i]->get_z() );
195
196         d2 = aircraft.distance3Dsquared( station );
197
198         // cout << "  dist = " << sqrt(d)
199         //      << "  range = " << current->get_range() * SG_NM_TO_METER
200         //      << endl;
201
202         // LOC, ILS, GS, and DME antenna's could potentially be
203         // installed at the opposite end of the runway.  So it's not
204         // enough to simply find the closest antenna with the right
205         // frequency.  We need the closest antenna with the right
206         // frequency that is most oriented towards us.  (We penalize
207         // stations that are facing away from us by adding 5000 meters
208         // which is further than matching stations would ever be
209         // placed from each other.  (Do the expensive check only for
210         // directional atennas and only when there is a chance it is
211         // the closest station.)
212         if ( d2 < min_dist &&
213              (stations[i]->get_type() == 4 || stations[i]->get_type() == 5 ||
214               stations[i]->get_type() == 6 || stations[i]->get_type() == 12) )
215         {
216             double hdg_deg = 0.0;
217             if ( stations[i]->get_type() == 4 || stations[i]->get_type() == 5 ){
218                 hdg_deg = stations[i]->get_multiuse();
219             } else if ( stations[i]->get_type() == 6 ) {
220                 int tmp = (int)(stations[i]->get_multiuse() / 1000.0);
221                 hdg_deg = stations[i]->get_multiuse() - (tmp * 1000);
222             } else if ( stations[i]->get_type() == 12 ) {
223                 // oops, Robin's data format doesn't give us the
224                 // needed information to compute a heading for a DME
225                 // transmitter.  FIXME Robin!
226             }
227
228             double az1 = 0.0, az2 = 0.0, s = 0.0;
229             double elev_m = 0.0, lat_rad = 0.0, lon_rad = 0.0;
230             double xyz[3] = { aircraft.x(), aircraft.y(), aircraft.z() };
231             sgCartToGeod( xyz, &lat_rad, &lon_rad, &elev_m );
232             geo_inverse_wgs_84( elev_m,
233                                 lat_rad * SG_RADIANS_TO_DEGREES,
234                                 lon_rad * SG_RADIANS_TO_DEGREES,
235                                 stations[i]->get_lat(), stations[i]->get_lon(),
236                                 &az1, &az2, &s);
237             az1 = az1 - stations[i]->get_multiuse();
238             if ( az1 >  180.0) az1 -= 360.0;
239             if ( az1 < -180.0) az1 += 360.0;
240             // penalize opposite facing stations by adding 5000 meters
241             // (squared) which is further than matching stations would
242             // ever be placed from each other.
243             if ( fabs(az1) > 90.0 ) {
244                 d2 += 5000*5000;
245             }
246         }
247
248         if ( d2 < min_dist ) {
249             min_dist = d2;
250             nav = stations[i];
251         }
252     }
253
254     return nav;
255 }
256
257
258 // returns the closest entry to the give lon/lat/elev
259 FGNavRecord *FGNavList::findClosest( double lon_rad, double lat_rad,
260                                      double elev_m )
261 {
262     FGNavRecord *result = NULL;
263     double diff;
264
265     double lon_deg = lon_rad * SG_RADIANS_TO_DEGREES;
266     double lat_deg = lat_rad * SG_RADIANS_TO_DEGREES;
267     int lonidx = (int)lon_deg;
268     diff = lon_deg - (double)lonidx;
269     if ( (lon_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
270         lonidx -= 1;
271     }
272     lonidx += 180;
273
274     int latidx = (int)lat_deg;
275     diff = lat_deg - (double)latidx;
276     if ( (lat_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
277         latidx -= 1;
278     }
279     latidx += 90;
280
281     int master_index = lonidx * 1000 + latidx;
282     
283     nav_list_type navs = navaids_by_tile[ master_index ];
284     // cout << "Master index = " << master_index << endl;
285     // cout << "beacon search length = " << beacons.size() << endl;
286
287     nav_list_iterator current = navs.begin();
288     nav_list_iterator last = navs.end();
289
290     Point3D aircraft = sgGeodToCart( Point3D(lon_rad,
291                                              lat_rad,
292                                              elev_m) );
293
294     double min_dist = 999999999.0;
295
296     for ( ; current != last ; ++current ) {
297         // cout << "  testing " << (*current)->get_ident() << endl;
298         Point3D station = Point3D( (*current)->get_x(),
299                                    (*current)->get_y(),
300                                    (*current)->get_z() );
301         // cout << "    aircraft = " << aircraft << " station = " << station 
302         //      << endl;
303
304         double d = aircraft.distance3Dsquared( station ); // meters^2
305         // cout << "  distance = " << d << " (" 
306         //      << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER 
307         //         * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
308         //      << ")" << endl;
309
310         // cout << "  range = " << sqrt(d) << endl;
311
312         if ( d < min_dist ) {
313             min_dist = d;
314             result = (*current);
315         }
316     }
317
318     // cout << "lon = " << lon << " lat = " << lat
319     //      << "  closest beacon = " << sqrt( min_dist ) << endl;
320
321     return result;
322 }