]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navlist.cxx
Updated FGILSList api to better match the FGNavList api.
[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 FGNavList *current_navlist;
36
37
38 // Constructor
39 FGNavList::FGNavList( void ) {
40 }
41
42
43 // Destructor
44 FGNavList::~FGNavList( void ) {
45 }
46
47
48 // load the navaids and build the map
49 bool FGNavList::init( SGPath path ) {
50
51     navaids.erase( navaids.begin(), navaids.end() );
52
53     sg_gzifstream in( path.str() );
54     if ( !in.is_open() ) {
55         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
56         exit(-1);
57     }
58
59     // read in each line of the file
60
61     in >> skipeol;
62     in >> skipcomment;
63
64     // double min = 100000;
65     // double max = 0;
66
67 #ifdef __MWERKS__
68     char c = 0;
69     while ( in.get(c) && c != '\0'  ) {
70         in.putback(c);
71 #else
72     while ( ! in.eof() ) {
73 #endif
74
75         FGNav *n = new FGNav;
76         in >> (*n);
77         if ( n->get_type() == '[' ) {
78             break;
79         }
80
81         /* cout << "id = " << n.get_ident() << endl;
82         cout << " type = " << n.get_type() << endl;
83         cout << " lon = " << n.get_lon() << endl;
84         cout << " lat = " << n.get_lat() << endl;
85         cout << " elev = " << n.get_elev() << endl;
86         cout << " freq = " << n.get_freq() << endl;
87         cout << " range = " << n.get_range() << endl << endl; */
88
89         navaids      [n->get_freq() ].push_back(n);
90         ident_navaids[n->get_ident()].push_back(n);
91                 
92         in >> skipcomment;
93
94         /* if ( n.get_type() != 'N' ) {
95             if ( n.get_freq() < min ) {
96                 min = n.get_freq();
97             }
98             if ( n.get_freq() > max ) {
99                 max = n.get_freq();
100             }
101         } */
102     }
103
104     // cout << "min freq = " << min << endl;
105     // cout << "max freq = " << max << endl;
106
107     return true;
108 }
109
110
111 // Query the database for the specified frequency.  It is assumed that
112 // there will be multiple stations with matching frequencies so a
113 // position must be specified.  Lon and lat are in degrees, elev is in
114 // meters.
115 FGNav *FGNavList::findByFreq( double freq, double lon, double lat, double elev )
116 {
117     nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
118     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
119
120     return findNavFromList( aircraft, stations );
121 }
122
123
124 FGNav *FGNavList::findByIdent( const char* ident,
125                                const double lon, const double lat )
126 {
127     nav_list_type stations = ident_navaids[ident];
128     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, 0.0) );
129
130     return findNavFromList( aircraft, stations );
131 }
132
133
134 // Given an Ident and optional freqency, return the first matching
135 // station.
136 FGNav *FGNavList::findByIdentAndFreq( const char* ident, const double freq )
137 {
138     nav_list_type stations = ident_navaids[ident];
139
140     if ( freq > 0.0 ) {
141         // sometimes there can be duplicated idents.  If a freq is
142         // specified, use it to refine the search.
143         int f = (int)(freq*100.0 + 0.5);
144         for ( unsigned int i = 0; i < stations.size(); ++i ) {
145             if ( f == stations[i]->get_freq() ) {
146                 return stations[i];
147             }
148         }
149     } else {
150         return stations[0];
151     }
152
153     return NULL;
154 }
155
156
157 // Given a point and a list of stations, return the closest one to the
158 // specified point.
159 FGNav *FGNavList::findNavFromList( const Point3D &aircraft, 
160                                    const nav_list_type &stations )
161 {
162     FGNav *nav = NULL;
163     Point3D station;
164     double d2;
165     double min_dist = 999999999.0;
166
167     // prime the pump with info from stations[0]
168     if ( stations.size() > 0 ) {
169         nav = stations[0];
170         station = Point3D( nav->get_x(), nav->get_y(), nav->get_z());
171         min_dist = aircraft.distance3Dsquared( station );
172     }
173
174     // check if any of the remaining stations are closer
175     for ( unsigned int i = 1; i < stations.size(); ++i ) {
176         // cout << "testing " << current->get_ident() << endl;
177         station = Point3D( stations[i]->get_x(),
178                            stations[i]->get_y(),
179                            stations[i]->get_z() );
180
181         d2 = aircraft.distance3Dsquared( station );
182
183         // cout << "  dist = " << sqrt(d)
184         //      << "  range = " << current->get_range() * SG_NM_TO_METER
185         //      << endl;
186
187         if ( d2 < min_dist ) {
188             min_dist = d2;
189             nav = stations[i];
190         }
191     }
192
193     return nav;
194 }