]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navlist.cxx
3892dcb8da303d360b31f56c31331adf459aa62a
[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 #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 "navlist.hxx"
33
34
35 FGNavList *current_navlist;
36
37
38 // Constructor
39 FGNavList::FGNavList( void ) {
40 }
41
42
43 // Destructor
44 FGNavList::~FGNavList( void ) {
45 }
46
47
48 // load the navaids and build the map
49 bool FGNavList::init( SGPath path ) {
50     FGNav n;
51
52     navaids.erase( navaids.begin(), navaids.end() );
53
54     sg_gzifstream in( path.str() );
55     if ( !in.is_open() ) {
56         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
57         exit(-1);
58     }
59
60     // read in each line of the file
61
62     in >> skipeol;
63     in >> skipcomment;
64
65 #ifdef __MWERKS__
66
67     char c = 0;
68     while ( in.get(c) && c != '\0' && n.get_type() != '[' ) {
69         in.putback(c);
70         in >> n;
71         if ( n.get_type() != '[' ) {
72             navaids[n.get_freq()].push_back(n);
73         }
74         in >> skipcomment;
75     }
76
77 #else
78
79     double min = 100000;
80     double max = 0;
81
82     while ( ! in.eof() ) {
83         in >> n;
84
85         if ( n.get_type() == '[' ) {
86             break;
87         }
88
89         /* cout << "id = " << n.get_ident() << endl;
90         cout << " type = " << n.get_type() << endl;
91         cout << " lon = " << n.get_lon() << endl;
92         cout << " lat = " << n.get_lat() << endl;
93         cout << " elev = " << n.get_elev() << endl;
94         cout << " freq = " << n.get_freq() << endl;
95         cout << " range = " << n.get_range() << endl; */
96
97         navaids[n.get_freq()].push_back(n);
98         in >> skipcomment;
99
100         if ( n.get_type() != 'N' ) {
101             if ( n.get_freq() < min ) {
102                 min = n.get_freq();
103             }
104             if ( n.get_freq() > max ) {
105                 max = n.get_freq();
106             }
107         }
108     }
109
110     // cout << "min freq = " << min << endl;
111     // cout << "max freq = " << max << endl;
112
113 #endif
114
115     return true;
116 }
117
118
119 // query the database for the specified frequency, lon and lat are in
120 // degrees, elev is in meters
121 bool FGNavList::query( double lon, double lat, double elev, double freq,
122                        FGNav *n )
123 {
124     nav_list_type stations = navaids[(int)(freq*100.0 + 0.5)];
125
126     nav_list_iterator current = stations.begin();
127     nav_list_iterator last = stations.end();
128
129     // double az1, az2, s;
130     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
131     Point3D station;
132     double d;
133     for ( ; current != last ; ++current ) {
134         // cout << "testing " << current->get_ident() << endl;
135         station = Point3D(current->get_x(), current->get_y(), current->get_z());
136
137         d = aircraft.distance3Dsquared( station );
138
139         // cout << "  dist = " << sqrt(d)
140         //      << "  range = " << current->get_range() * SG_NM_TO_METER << endl;
141
142         // match up to twice the published range so we can model
143         // reduced signal strength
144         if ( d < (2 * current->get_range() * SG_NM_TO_METER 
145                   * 2 * current->get_range() * SG_NM_TO_METER ) ) {
146             // cout << "matched = " << current->get_ident() << endl;
147             *n = *current;
148             return true;
149         }
150     }
151
152     return false;
153 }