]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/ilslist.cxx
Updated to match changes in radiostack.[ch]xx
[flightgear.git] / src / Navaids / ilslist.cxx
1 // ilslist.cxx -- ils 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 "mkrbeacons.hxx"
33 #include "ilslist.hxx"
34
35
36 FGILSList *current_ilslist;
37
38
39 // Constructor
40 FGILSList::FGILSList( void ) {
41 }
42
43
44 // Destructor
45 FGILSList::~FGILSList( void ) {
46 }
47
48
49 // load the navaids and build the map
50 bool FGILSList::init( SGPath path ) {
51
52     ilslist.erase( ilslist.begin(), ilslist.end() );
53
54     sg_gzifstream in( path.str() );
55     if ( !in.is_open() ) {
56         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
57         exit(-1);
58     }
59
60     // read in each line of the file
61
62     in >> skipeol;
63     in >> skipcomment;
64
65     // double min = 1000000.0;
66     // double max = 0.0;
67
68 #ifdef __MWERKS__
69     char c = 0;
70     while ( in.get(c) && c != '\0' ) {
71         in.putback(c);
72 #else
73     while ( ! in.eof() ) {
74 #endif
75
76         FGILS ils;
77         in >> ils;
78         if ( ils.get_ilstype() == '[' ) {
79             break;
80         }
81
82         /* cout << "typename = " << ils.get_ilstypename() << endl;
83         cout << " aptcode = " << ils.get_aptcode() << endl;
84         cout << " twyno = " << ils.get_rwyno() << endl;
85         cout << " locfreq = " << ils.get_locfreq() << endl;
86         cout << " locident = " << ils.get_locident() << endl << endl; */
87
88         ilslist[ils.get_locfreq()].push_back(ils);
89         in >> skipcomment;
90
91         /* if ( ils.get_locfreq() < min ) {
92             min = ils.get_locfreq();
93         }
94         if ( ils.get_locfreq() > max ) {
95             max = ils.get_locfreq();
96         } */
97
98         // update the marker beacon list
99         if ( fabs(ils.get_omlon()) > SG_EPSILON ||
100              fabs(ils.get_omlat()) > SG_EPSILON ) {
101             current_beacons->add( ils.get_omlon(), ils.get_omlat(),
102                                   ils.get_gselev(), FGMkrBeacon::OUTER );
103         }
104         if ( fabs(ils.get_mmlon()) > SG_EPSILON ||
105              fabs(ils.get_mmlat()) > SG_EPSILON ) {
106             current_beacons->add( ils.get_mmlon(), ils.get_mmlat(),
107                                   ils.get_gselev(), FGMkrBeacon::MIDDLE );
108         }
109         if ( fabs(ils.get_imlon()) > SG_EPSILON ||
110              fabs(ils.get_imlat()) > SG_EPSILON ) {
111             current_beacons->add( ils.get_imlon(), ils.get_imlat(),
112                                   ils.get_gselev(), FGMkrBeacon::INNER );
113         }
114     }
115
116     // cout << "min freq = " << min << endl;
117     // cout << "max freq = " << max << endl;
118
119     return true;
120 }
121
122
123 // query the database for the specified frequency, lon and lat are in
124 // degrees, elev is in meters
125 bool FGILSList::query( double lon, double lat, double elev, double freq,
126                        FGILS *ils )
127 {
128     ils_list_type stations = ilslist[(int)(freq*100.0 + 0.5)];
129
130     ils_list_iterator current = stations.begin();
131     ils_list_iterator last = stations.end();
132
133     double best_angle = 362.0;
134     bool found_one = false;
135
136     // double az1, az2, s;
137     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
138     Point3D station;
139     double d;
140     for ( ; current != last ; ++current ) {
141         // cout << "  testing " << current->get_locident() << endl;
142         station = Point3D(current->get_x(), 
143                           current->get_y(),
144                           current->get_z());
145         // cout << "    aircraft = " << aircraft << " station = " << station 
146         //      << endl;
147
148         d = aircraft.distance3Dsquared( station );
149         // cout << "  distance = " << d << " (" 
150         //      << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER 
151         //         * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
152         //      << ")" << endl;
153
154         // cout << "  dist = " << s << endl;
155
156         // match up to twice the published range so we can model
157         // reduced signal strength
158         if ( d < (2* FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER 
159                   * 2 * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER) ) {
160
161             found_one = true;
162
163             // Get our bearing from this station.
164             double reciprocal_bearing, dummy;
165             double a_lat_deg = lat * SGD_RADIANS_TO_DEGREES;
166             double a_lon_deg = lon * SGD_RADIANS_TO_DEGREES;
167             // Locator beam direction
168             double s_ils_deg = current->get_locheading() - 180.0;
169             if ( s_ils_deg < 0.0 ) { s_ils_deg += 360.0; }
170             double angle_to_beam_deg;
171
172             // printf("**ALI geting geo_inverse_wgs_84 with elev = %.2f, a.lat = %.2f, a.lon = %.2f,
173             // s.lat = %.2f, s.lon = %.2f\n", elev,a_lat_deg,a_lon_deg,current->get_loclat(),current->get_loclon());
174
175             geo_inverse_wgs_84( elev, current->get_loclat(),
176                                 current->get_loclon(), a_lat_deg, a_lon_deg,
177                                 &reciprocal_bearing, &dummy, &dummy );
178             angle_to_beam_deg = fabs(reciprocal_bearing - s_ils_deg);
179             if ( angle_to_beam_deg <= best_angle ) {
180                 *ils = *current;
181                 best_angle = angle_to_beam_deg;
182             }
183         }
184     }
185
186     return found_one;
187 }