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