]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navlist.cxx
Working on straightening out VOR navigation.
[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/fgstream.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( FGPath path ) {
46     FGNav n;
47
48     SGTime time_params;
49     time_params.update( 0.0, 0.0, 0 );
50
51     navaids.erase( navaids.begin(), navaids.end() );
52
53     fg_gzifstream in( path.str() );
54     if ( !in.is_open() ) {
55         FG_LOG( FG_GENERAL, FG_ALERT, "Cannot open file: " << path.str() );
56         exit(-1);
57     }
58
59     // read in each line of the file
60
61     in >> skipeol;
62     in >> skipcomment;
63
64 #ifdef __MWERKS__
65
66     char c = 0;
67     while ( in.get(c) && c != '\0' && n.get_type() != '[' ) {
68         in.putback(c);
69         in >> n;
70         if ( n.get_type() != '[' ) {
71             navaids[n.get_freq()].push_back(n);
72         }
73         in >> skipcomment;
74     }
75
76 #else
77
78     double min = 100000;
79     double max = 0;
80
81     while ( ! in.eof() && n.get_type() != '[' ) {
82         in >> n;
83         /* cout << "id = " << n.get_ident() << endl;
84         cout << " type = " << n.get_type() << endl;
85         cout << " lon = " << n.get_lon() << endl;
86         cout << " lat = " << n.get_lat() << endl;
87         cout << " elev = " << n.get_elev() << endl;
88         cout << " freq = " << n.get_freq() << endl;
89         cout << " range = " << n.get_range() << endl; */
90         if ( n.get_type() != '[' ) {
91             navaids[n.get_freq()].push_back(n);
92         }
93         in >> skipcomment;
94
95         if ( n.get_type() != 'N' ) {
96             if ( n.get_freq() < min ) {
97                 min = n.get_freq();
98             }
99             if ( n.get_freq() > max ) {
100                 max = n.get_freq();
101             }
102         }
103     }
104
105     // cout << "min freq = " << min << endl;
106     // cout << "max freq = " << max << endl;
107
108 #endif
109
110     return true;
111 }
112
113
114 // query the database for the specified frequency, lon and lat are in
115 // degrees, elev is in meters
116 bool FGNavList::query( double lon, double lat, double elev, double freq,
117                        FGNav *n )
118 {
119     nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
120
121     nav_list_iterator current = stations.begin();
122     nav_list_iterator last = stations.end();
123
124     // double az1, az2, s;
125     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
126     Point3D station;
127     double d;
128     for ( ; current != last ; ++current ) {
129         // cout << "testing " << current->get_ident() << endl;
130         station = Point3D(current->get_x(), current->get_y(), current->get_z());
131
132         d = aircraft.distance3Dsquared( station );
133
134         // cout << "  dist = " << sqrt(d)
135         //      << "  range = " << current->get_range() * NM_TO_METER << endl;
136         if ( d < (current->get_range() * NM_TO_METER 
137                   * current->get_range() * NM_TO_METER * 5.0) ) {
138             // cout << "matched = " << current->get_ident() << endl;
139             *n = *current;
140             return true;
141         }
142     }
143
144     return false;
145 }