]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/ilslist.cxx
FG_ to SG_ namespace changes.
[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 #include <simgear/debug/logstream.hxx>
25 #include <simgear/misc/fgstream.hxx>
26 #include <simgear/math/sg_geodesy.hxx>
27
28 #include "mkrbeacons.hxx"
29 #include "ilslist.hxx"
30
31
32 FGILSList *current_ilslist;
33
34
35 // Constructor
36 FGILSList::FGILSList( void ) {
37 }
38
39
40 // Destructor
41 FGILSList::~FGILSList( void ) {
42 }
43
44
45 // load the navaids and build the map
46 bool FGILSList::init( FGPath path ) {
47     FGILS ils;
48
49     ilslist.erase( ilslist.begin(), ilslist.end() );
50
51     fg_gzifstream in( path.str() );
52     if ( !in.is_open() ) {
53         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << path.str() );
54         exit(-1);
55     }
56
57     // read in each line of the file
58
59     in >> skipeol;
60     in >> skipcomment;
61
62 #ifdef __MWERKS__
63
64     char c = 0;
65     while ( in.get(c) && c != '\0' && ils.get_ilstype() != '[' ) {
66         in.putback(c);
67         in >> ils;
68         if ( ils.get_ilstype() != '[' ) {
69             ilslist[ils.get_locfreq()].push_back(ils);
70         }
71         in >> skipcomment;
72     }
73
74 #else
75
76     double min = 1000000.0;
77     double max = 0.0;
78
79     while ( ! in.eof() && ils.get_ilstype() != '[' ) {
80         in >> ils;
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; */
88         if ( ils.get_ilstype() != '[' ) {
89             ilslist[ils.get_locfreq()].push_back(ils);
90         }
91         in >> skipcomment;
92
93         if ( ils.get_locfreq() < min ) {
94             min = ils.get_locfreq();
95         }
96         if ( ils.get_locfreq() > max ) {
97             max = ils.get_locfreq();
98         }
99
100         // update the marker beacon list
101         if ( fabs(ils.get_omlon()) > SG_EPSILON ||
102              fabs(ils.get_omlat()) > SG_EPSILON ) {
103             current_beacons->add( ils.get_omlon(), ils.get_omlat(),
104                                   ils.get_gselev(), FGBeacon::OUTER );
105         }
106         if ( fabs(ils.get_mmlon()) > SG_EPSILON ||
107              fabs(ils.get_mmlat()) > SG_EPSILON ) {
108             current_beacons->add( ils.get_mmlon(), ils.get_mmlat(),
109                                   ils.get_gselev(), FGBeacon::MIDDLE );
110         }
111         if ( fabs(ils.get_imlon()) > SG_EPSILON ||
112              fabs(ils.get_imlat()) > SG_EPSILON ) {
113             current_beacons->add( ils.get_imlon(), ils.get_imlat(),
114                                   ils.get_gselev(), FGBeacon::INNER );
115         }
116     }
117
118     // cout << "min freq = " << min << endl;
119     // cout << "max freq = " << max << endl;
120
121 #endif
122
123     return true;
124 }
125
126
127 // query the database for the specified frequency, lon and lat are in
128 // degrees, elev is in meters
129 bool FGILSList::query( double lon, double lat, double elev, double freq,
130                        FGILS *ils )
131 {
132     ils_list_type stations = ilslist[(int)(freq*100.0 + 0.5)];
133
134     ils_list_iterator current = stations.begin();
135     ils_list_iterator last = stations.end();
136
137     // double az1, az2, s;
138     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
139     Point3D station;
140     double d;
141     for ( ; current != last ; ++current ) {
142         // cout << "  testing " << current->get_locident() << endl;
143         station = Point3D(current->get_x(), 
144                           current->get_y(),
145                           current->get_z());
146         // cout << "    aircraft = " << aircraft << " station = " << station 
147         //      << endl;
148
149         d = aircraft.distance3Dsquared( station );
150         // cout << "  distance = " << d << " (" 
151         //      << FG_ILS_DEFAULT_RANGE * NM_TO_METER 
152         //         * FG_ILS_DEFAULT_RANGE * NM_TO_METER
153         //      << ")" << endl;
154
155         // cout << "  dist = " << s << endl;
156
157         // match up to twice the published range so we can model
158         // reduced signal strength
159         if ( d < (2* FG_ILS_DEFAULT_RANGE * NM_TO_METER 
160                   * 2 * FG_ILS_DEFAULT_RANGE * NM_TO_METER) ) {
161             *ils = *current;
162             return true;
163         }
164     }
165
166     return false;
167 }