]> git.mxchange.org Git - flightgear.git/blob - src/Navaids/navlist.cxx
Added static port system and a new altimeter model connected to it.
[flightgear.git] / src / Navaids / navlist.cxx
1 // navlist.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
51     navaids.erase( navaids.begin(), navaids.end() );
52
53     sg_gzifstream in( path.str() );
54     if ( !in.is_open() ) {
55         SG_LOG( SG_GENERAL, SG_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     // double min = 100000;
65     // double max = 0;
66
67 #ifdef __MWERKS__
68     char c = 0;
69     while ( in.get(c) && c != '\0'  ) {
70         in.putback(c);
71 #else
72     while ( ! in.eof() ) {
73 #endif
74
75         FGNav *n = new FGNav;
76         in >> (*n);
77         if ( n->get_type() == '[' ) {
78             break;
79         }
80
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 << endl; */
88
89         navaids      [n->get_freq() ].push_back(n);
90         ident_navaids[n->get_ident()].push_back(n);
91                 
92         in >> skipcomment;
93
94         /* if ( n.get_type() != 'N' ) {
95             if ( n.get_freq() < min ) {
96                 min = n.get_freq();
97             }
98             if ( n.get_freq() > max ) {
99                 max = n.get_freq();
100             }
101         } */
102     }
103
104     // cout << "min freq = " << min << endl;
105     // cout << "max freq = " << max << endl;
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     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, elev) );
122     return findNavFromList(aircraft, current, last, n);
123 }
124
125
126 bool FGNavList::findByIdent(const char* ident, double lon, double lat,
127                             FGNav *nav)
128 {
129     nav_list_type stations = ident_navaids[ident];
130
131     nav_list_iterator current = stations.begin();
132     nav_list_iterator last = stations.end();
133         
134     Point3D aircraft = sgGeodToCart( Point3D(lon, lat, 0.0) );
135     return findNavFromList(aircraft, current, last, nav);
136 }
137
138
139 bool FGNavList::findNavFromList(const Point3D &aircraft, 
140                                 nav_list_iterator current,
141                                 nav_list_iterator end, FGNav *n)
142 {
143     // double az1, az2, s;
144     
145     Point3D station;
146     double d2;
147     double min_dist = 99999999999999.9;
148     bool found_one = false;
149     for ( ; current != end ; ++current ) {
150         // cout << "testing " << current->get_ident() << endl;
151         station = Point3D((*current)->get_x(), (*current)->get_y(),
152                           (*current)->get_z());
153
154         d2 = aircraft.distance3Dsquared( station );
155
156         // cout << "  dist = " << sqrt(d)
157         //      << "  range = " << current->get_range() * SG_NM_TO_METER
158         //      << endl;
159
160         // match d^2 < 2 * range^2 the published range so we can model
161         // reduced signal strength
162         double twiceRange = 2 * (*current)->get_range() * SG_NM_TO_METER;
163         if ( d2 < (twiceRange * twiceRange)) {
164             // cout << "d2 = " << d2 << " min_dist = " << min_dist << endl;
165             if ( d2 < min_dist ) {
166                 min_dist = d2;
167                 found_one = true;
168                 *n = (**current);
169                 // cout << "matched = " << (*current)->get_ident() << endl;
170             } else {
171                 // cout << "matched, but too far away = "
172                 //      << (*current)->get_ident() << endl;
173             }
174         }
175     }
176
177     return found_one;
178 }