1 // navlist.cxx -- navaids management class
3 // Written by Curtis Olson, started April 2000.
5 // Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org
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 ) {
41 FGNavList::~FGNavList( void ) {
45 // load the navaids and build the map
46 bool FGNavList::init() {
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() );
57 // real add a marker beacon
58 static void real_add( nav_map_type &navmap, const int master_index,
61 navmap[master_index].push_back( n );
65 // front end for add a marker beacon
66 static void tile_add( nav_map_type &navmap, FGNavRecord *n ) {
69 double lon = n->get_lon();
70 double lat = n->get_lat();
72 int lonidx = (int)lon;
73 diff = lon - (double)lonidx;
74 if ( (lon < 0.0) && (fabs(diff) > SG_EPSILON) ) {
77 double lonfrac = lon - (double)lonidx;
80 int latidx = (int)lat;
81 diff = lat - (double)latidx;
82 if ( (lat < 0.0) && (fabs(diff) > SG_EPSILON) ) {
85 double latfrac = lat - (double)latidx;
88 int master_index = lonidx * 1000 + latidx;
89 // cout << "lonidx = " << lonidx << " latidx = " << latidx << " ";
90 // cout << "Master index = " << master_index << endl;
92 // add to the actual bucket
93 real_add( navmap, master_index, n );
95 // if we are close to the edge, add to adjacent buckets so we only
96 // have to search one bucket at run time
98 // there are 8 cases since there are 8 adjacent tiles
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 );
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 );
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 );
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 );
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
136 FGNavRecord *FGNavList::findByFreq( double freq, double lon, double lat, double elev )
138 nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
139 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
141 return findNavFromList( aircraft, stations );
145 FGNavRecord *FGNavList::findByIdent( const char* ident,
146 const double lon, const double lat )
148 nav_list_type stations = ident_navaids[ident];
149 Point3D aircraft = sgGeodToCart( Point3D(lon, lat, 0.0) );
151 return findNavFromList( aircraft, stations );
155 // Given an Ident and optional freqency, return the first matching
157 FGNavRecord *FGNavList::findByIdentAndFreq( const char* ident, const double freq )
159 nav_list_type stations = ident_navaids[ident];
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() ) {
178 // Given a point and a list of stations, return the closest one to the
180 FGNavRecord *FGNavList::findNavFromList( const Point3D &aircraft,
181 const nav_list_type &stations )
183 FGNavRecord *nav = NULL;
186 double min_dist = 999999999.0;
188 // prime the pump with info from stations[0]
189 if ( stations.size() > 0 ) {
191 station = Point3D( nav->get_x(), nav->get_y(), nav->get_z());
192 min_dist = aircraft.distance3Dsquared( station );
195 // check if any of the remaining stations are closer
196 for ( unsigned int i = 1; i < stations.size(); ++i ) {
197 // cout << "testing " << current->get_ident() << endl;
198 station = Point3D( stations[i]->get_x(),
199 stations[i]->get_y(),
200 stations[i]->get_z() );
202 d2 = aircraft.distance3Dsquared( station );
204 // cout << " dist = " << sqrt(d)
205 // << " range = " << current->get_range() * SG_NM_TO_METER
208 if ( d2 < min_dist ) {
218 // returns the closest entry to the give lon/lat/elev
219 FGNavRecord *FGNavList::findClosest( double lon_rad, double lat_rad,
222 FGNavRecord *result = NULL;
225 double lon_deg = lon_rad * SG_RADIANS_TO_DEGREES;
226 double lat_deg = lat_rad * SG_RADIANS_TO_DEGREES;
227 int lonidx = (int)lon_deg;
228 diff = lon_deg - (double)lonidx;
229 if ( (lon_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
234 int latidx = (int)lat_deg;
235 diff = lat_deg - (double)latidx;
236 if ( (lat_deg < 0.0) && (fabs(diff) > SG_EPSILON) ) {
241 int master_index = lonidx * 1000 + latidx;
243 nav_list_type navs = navaids_by_tile[ master_index ];
244 // cout << "Master index = " << master_index << endl;
245 // cout << "beacon search length = " << beacons.size() << endl;
247 nav_list_iterator current = navs.begin();
248 nav_list_iterator last = navs.end();
250 Point3D aircraft = sgGeodToCart( Point3D(lon_rad,
254 double min_dist = 999999999.0;
256 for ( ; current != last ; ++current ) {
257 // cout << " testing " << (*current)->get_ident() << endl;
258 Point3D station = Point3D( (*current)->get_x(),
260 (*current)->get_z() );
261 // cout << " aircraft = " << aircraft << " station = " << station
264 double d = aircraft.distance3Dsquared( station ); // meters^2
265 // cout << " distance = " << d << " ("
266 // << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
267 // * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
270 // cout << " range = " << sqrt(d) << endl;
272 if ( d < min_dist ) {
278 // cout << "lon = " << lon << " lat = " << lat
279 // << " closest beacon = " << sqrt( min_dist ) << endl;