]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navlist.cxx
35ac0d5c22253a539cd6ece60f4975381ef1aed3
[flightgear.git] / src / Navaids / navlist.cxx
1 // navaids.cxx -- navaids 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/sgstream.hxx>
26 #include <simgear/math/sg_geodesy.hxx>
27
28 #include "navlist.hxx"
29
30
31 FGNavList *current_navlist;
32
33
34 // Constructor
35 FGNavList::FGNavList( void ) {
36 }
37
38
39 // Destructor
40 FGNavList::~FGNavList( void ) {
41 }
42
43
44 // load the navaids and build the map
45 bool FGNavList::init( SGPath path ) {
46     FGNav n;
47
48     navaids.erase( navaids.begin(), navaids.end() );
49
50     sg_gzifstream in( path.str() );
51     if ( !in.is_open() ) {
52         SG_LOG( SG_GENERAL, SG_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' && n.get_type() != '[' ) {
65         in.putback(c);
66         in >> n;
67         if ( n.get_type() != '[' ) {
68             navaids[n.get_freq()].push_back(n);
69         }
70         in >> skipcomment;
71     }
72
73 #else
74
75     double min = 100000;
76     double max = 0;
77
78     while ( ! in.eof() && n.get_type() != '[' ) {
79         in >> n;
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 ( n.get_type() != '[' ) {
88             navaids[n.get_freq()].push_back(n);
89         }
90         in >> skipcomment;
91
92         if ( n.get_type() != 'N' ) {
93             if ( n.get_freq() < min ) {
94                 min = n.get_freq();
95             }
96             if ( n.get_freq() > max ) {
97                 max = n.get_freq();
98             }
99         }
100     }
101
102     // cout << "min freq = " << min << endl;
103     // cout << "max freq = " << max << endl;
104
105 #endif
106
107     return true;
108 }
109
110
111 // query the database for the specified frequency, lon and lat are in
112 // degrees, elev is in meters
113 bool FGNavList::query( double lon, double lat, double elev, double freq,
114                        FGNav *n )
115 {
116     nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
117
118     nav_list_iterator current = stations.begin();
119     nav_list_iterator last = stations.end();
120
121     // double az1, az2, s;
122     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
123     Point3D station;
124     double d;
125     for ( ; current != last ; ++current ) {
126         // cout << "testing " << current->get_ident() << endl;
127         station = Point3D(current->get_x(), current->get_y(), current->get_z());
128
129         d = aircraft.distance3Dsquared( station );
130
131         // cout << "  dist = " << sqrt(d)
132         //      << "  range = " << current->get_range() * SG_NM_TO_METER << endl;
133
134         // match up to twice the published range so we can model
135         // reduced signal strength
136         if ( d < (2 * current->get_range() * SG_NM_TO_METER 
137                   * 2 * current->get_range() * SG_NM_TO_METER ) ) {
138             // cout << "matched = " << current->get_ident() << endl;
139             *n = *current;
140             return true;
141         }
142     }
143
144     return false;
145 }