]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/ilslist.cxx
Patch from Melchior Franz:
[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() ) {
84         in >> ils;
85
86         if ( ils.get_ilstype() == '[' ) {
87             break;
88         }
89
90         /* cout << "id = " << n.get_ident() << endl;
91         cout << " type = " << n.get_type() << endl;
92         cout << " lon = " << n.get_lon() << endl;
93         cout << " lat = " << n.get_lat() << endl;
94         cout << " elev = " << n.get_elev() << endl;
95         cout << " freq = " << n.get_freq() << endl;
96         cout << " range = " << n.get_range() << endl; */
97
98         ilslist[ils.get_locfreq()].push_back(ils);
99         in >> skipcomment;
100
101         if ( ils.get_locfreq() < min ) {
102             min = ils.get_locfreq();
103         }
104         if ( ils.get_locfreq() > max ) {
105             max = ils.get_locfreq();
106         }
107
108         // update the marker beacon list
109         if ( fabs(ils.get_omlon()) > SG_EPSILON ||
110              fabs(ils.get_omlat()) > SG_EPSILON ) {
111             current_beacons->add( ils.get_omlon(), ils.get_omlat(),
112                                   ils.get_gselev(), FGMkrBeacon::OUTER );
113         }
114         if ( fabs(ils.get_mmlon()) > SG_EPSILON ||
115              fabs(ils.get_mmlat()) > SG_EPSILON ) {
116             current_beacons->add( ils.get_mmlon(), ils.get_mmlat(),
117                                   ils.get_gselev(), FGMkrBeacon::MIDDLE );
118         }
119         if ( fabs(ils.get_imlon()) > SG_EPSILON ||
120              fabs(ils.get_imlat()) > SG_EPSILON ) {
121             current_beacons->add( ils.get_imlon(), ils.get_imlat(),
122                                   ils.get_gselev(), FGMkrBeacon::INNER );
123         }
124     }
125
126     // cout << "min freq = " << min << endl;
127     // cout << "max freq = " << max << endl;
128
129 #endif
130
131     return true;
132 }
133
134
135 // query the database for the specified frequency, lon and lat are in
136 // degrees, elev is in meters
137 bool FGILSList::query( double lon, double lat, double elev, double freq,
138                        FGILS *ils )
139 {
140     ils_list_type stations = ilslist[(int)(freq*100.0 + 0.5)];
141
142     ils_list_iterator current = stations.begin();
143     ils_list_iterator last = stations.end();
144
145     // double az1, az2, s;
146     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
147     Point3D station;
148     double d;
149     for ( ; current != last ; ++current ) {
150         // cout << "  testing " << current->get_locident() << endl;
151         station = Point3D(current->get_x(), 
152                           current->get_y(),
153                           current->get_z());
154         // cout << "    aircraft = " << aircraft << " station = " << station 
155         //      << endl;
156
157         d = aircraft.distance3Dsquared( station );
158         // cout << "  distance = " << d << " (" 
159         //      << FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER 
160         //         * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER
161         //      << ")" << endl;
162
163         // cout << "  dist = " << s << endl;
164
165         // match up to twice the published range so we can model
166         // reduced signal strength
167         if ( d < (2* FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER 
168                   * 2 * FG_ILS_DEFAULT_RANGE * SG_NM_TO_METER) ) {
169
170             // Get our bearing from this station.
171             double reciprocal_bearing, dummy;
172             double a_lat_deg = lat * SGD_RADIANS_TO_DEGREES;
173             double a_lon_deg = lon * SGD_RADIANS_TO_DEGREES;
174             // Locator beam direction
175             double s_ils_deg = current->get_locheading() - 180.0;
176             if ( s_ils_deg < 0.0 ) { s_ils_deg += 360.0; }
177             double angle_to_beam_deg;
178
179             // printf("**ALI geting geo_inverse_wgs_84 with elev = %.2f, a.lat = %.2f, a.lon = %.2f,
180             // s.lat = %.2f, s.lon = %.2f\n", elev,a_lat_deg,a_lon_deg,current->get_loclat(),current->get_loclon());
181
182             geo_inverse_wgs_84( elev, current->get_loclat(),
183                                 current->get_loclon(), a_lat_deg, a_lon_deg,
184                                 &reciprocal_bearing, &dummy, &dummy );
185             angle_to_beam_deg = fabs(reciprocal_bearing - s_ils_deg);
186             if ( angle_to_beam_deg < 90.0 ) {
187                 *ils = *current;
188                 return true;
189             }
190         }
191     }
192
193     return false;
194 }