]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/ilslist.cxx
MacOS changes by Darrell Walisser.
[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/fg_geodesy.hxx>
27
28 #include "ilslist.hxx"
29
30
31 FGILSList *current_ilslist;
32
33
34 // Constructor
35 FGILSList::FGILSList( void ) {
36 }
37
38
39 // Destructor
40 FGILSList::~FGILSList( void ) {
41 }
42
43
44 // load the navaids and build the map
45 bool FGILSList::init( FGPath path ) {
46     FGILS ils;
47
48     ilslist.erase( ilslist.begin(), ilslist.end() );
49
50     fg_gzifstream in( path.str() );
51     if ( !in.is_open() ) {
52         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << path.str() );
53         exit(-1);
54     }
55
56     // read in each line of the file
57
58     in >> skipeol;
59     in >> skipcomment;
60
61 #ifdef __MWERKS__
62
63     char c = 0;
64     while ( in.get(c) && c != '\0' && ils.get_ilstype() != '[' ) {
65         in.putback(c);
66         in >> ils;
67         if ( ils.get_ilstype() != '[' ) {
68             ilslist[ils.get_locfreq()].push_back(ils);
69         }
70         in >> skipcomment;
71     }
72
73 #else
74
75     double min = 1000000.0;
76     double max = 0.0;
77
78     while ( ! in.eof() && ils.get_ilstype() != '[' ) {
79         in >> ils;
80         /* cout << "id = " << n.get_ident() << endl;
81         cout << " type = " << n.get_type() << endl;
82         cout << " lon = " << n.get_lon() << endl;
83         cout << " lat = " << n.get_lat() << endl;
84         cout << " elev = " << n.get_elev() << endl;
85         cout << " freq = " << n.get_freq() << endl;
86         cout << " range = " << n.get_range() << endl; */
87         if ( ils.get_ilstype() != '[' ) {
88             ilslist[ils.get_locfreq()].push_back(ils);
89         }
90         in >> skipcomment;
91
92         if ( ils.get_locfreq() < min ) {
93             min = ils.get_locfreq();
94         }
95         if ( ils.get_locfreq() > max ) {
96             max = ils.get_locfreq();
97         }
98     }
99
100     // cout << "min freq = " << min << endl;
101     // cout << "max freq = " << max << endl;
102
103 #endif
104
105     return true;
106 }
107
108
109 // query the database for the specified frequency, lon and lat are in
110 // degrees, elev is in meters
111 bool FGILSList::query( double lon, double lat, double elev, double freq,
112                        FGILS *ils )
113 {
114     ils_list_type stations = ilslist[(int)(freq*100.0 + 0.5)];
115
116     ils_list_iterator current = stations.begin();
117     ils_list_iterator last = stations.end();
118
119     // double az1, az2, s;
120     Point3D aircraft = fgGeodToCart( Point3D(lon, lat, elev) );
121     Point3D station;
122     double d;
123     for ( ; current != last ; ++current ) {
124         // cout << "  testing " << current->get_locident() << endl;
125         station = Point3D(current->get_x(), 
126                           current->get_y(),
127                           current->get_z());
128         // cout << "    aircraft = " << aircraft << " station = " << station 
129         //      << endl;
130
131         d = aircraft.distance3Dsquared( station );
132         // cout << "  distance = " << d << " (" 
133         //      << FG_ILS_DEFAULT_RANGE * NM_TO_METER 
134         //         * FG_ILS_DEFAULT_RANGE * NM_TO_METER
135         //      << ")" << endl;
136
137         // cout << "  dist = " << s << endl;
138         if ( d < (FG_ILS_DEFAULT_RANGE * NM_TO_METER 
139                   * FG_ILS_DEFAULT_RANGE * NM_TO_METER) ) {
140             *ils = *current;
141             return true;
142         }
143     }
144
145     return false;
146 }