1 // navlist.cxx -- navaids management class
3 // Written by Curtis Olson, started April 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/math/sg_geodesy.hxx>
30 #include <simgear/sg_inlines.h>
32 #include "navlist.hxx"
34 #include <Airports/runways.hxx>
38 // FGNavList ------------------------------------------------------------------
40 FGNavList::FGNavList( void )
45 FGNavList::~FGNavList( void )
47 nav_list_type navlist = navaids.begin()->second;
48 navaids.erase( navaids.begin(), navaids.end() );
52 // load the navaids and build the map
53 bool FGNavList::init()
55 // No need to delete the original navaid structures
56 // since we're using an SGSharedPointer
57 nav_list_type navlist = navaids.begin()->second;
58 navaids.erase( navaids.begin(), navaids.end() );
62 // add an entry to the lists
63 bool FGNavList::add( FGNavRecord *n )
65 navaids[n->get_freq()].push_back(n);
69 FGNavRecord *FGNavList::findByFreq( double freq, const SGGeod& position)
71 const nav_list_type& stations = navaids[(int)(freq*100.0 + 0.5)];
72 SG_LOG( SG_INSTR, SG_DEBUG, "findbyFreq " << freq << " size " << stations.size() );
73 return findNavFromList( position, stations );
76 class VORNDBFilter : public FGPositioned::Filter
79 virtual FGPositioned::Type minType() const {
80 return FGPositioned::VOR;
83 virtual FGPositioned::Type maxType() const {
84 return FGPositioned::NDB;
88 // Given an Ident and optional freqency, return the first matching
90 FGNavRecord *FGNavList::findByIdentAndFreq(const string& ident, const double freq )
94 cur = FGPositioned::findNextWithPartialId(cur, ident, &filter);
97 return static_cast<FGNavRecord*>(cur.ptr()); // might be null
100 int f = (int)(freq*100.0 + 0.5);
102 FGNavRecord* nav = static_cast<FGNavRecord*>(cur.ptr());
103 if (nav->get_freq() == f) {
107 cur = FGPositioned::findNextWithPartialId(cur, ident, &filter);
113 // discount navids if they conflict with another on the same frequency
114 // this only applies to navids associated with opposite ends of a runway,
115 // with matching frequencies.
116 static bool navidUsable(FGNavRecord* aNav, const SGGeod &aircraft)
118 FGRunway* r(aNav->runway());
119 if (!r || !r->reciprocalRunway()) {
123 // check if the runway frequency is paired
124 FGNavRecord* locA = r->ILS();
125 FGNavRecord* locB = r->reciprocalRunway()->ILS();
127 if (!locA || !locB || (locA->get_freq() != locB->get_freq())) {
128 return true; // not paired, ok
131 // okay, both ends have an ILS, and they're paired. We need to select based on
132 // aircraft position. What we're going to use is *runway* (not navid) position,
133 // ie whichever runway end we are closer too. This makes back-course / missed
134 // approach behaviour incorrect, but that's the price we accept.
135 double crs = SGGeodesy::courseDeg(aircraft, r->geod());
136 double hdgDiff = crs - r->headingDeg();
137 SG_NORMALIZE_RANGE(hdgDiff, -180.0, 180.0);
138 return (fabs(hdgDiff) < 90.0);
141 // Given a point and a list of stations, return the closest one to
142 // the specified point.
143 FGNavRecord* FGNavList::findNavFromList( const SGGeod &aircraft,
144 const nav_list_type &stations )
146 FGNavRecord *nav = NULL;
147 double d2; // in meters squared
149 = FG_NAV_MAX_RANGE*SG_NM_TO_METER*FG_NAV_MAX_RANGE*SG_NM_TO_METER;
150 SGVec3d aircraftCart = SGVec3d::fromGeod(aircraft);
152 nav_list_const_iterator it;
153 nav_list_const_iterator end = stations.end();
154 // find the closest station within a sensible range (FG_NAV_MAX_RANGE)
155 for ( it = stations.begin(); it != end; ++it ) {
156 FGNavRecord *station = *it;
157 d2 = distSqr(station->cart(), aircraftCart);
158 if ( d2 > min_dist || !navidUsable(station, aircraft)) {
169 // Given a frequency, return the first matching station.
170 FGNavRecord *FGNavList::findStationByFreq( double freq )
172 const nav_list_type& stations = navaids[(int)(freq*100.0 + 0.5)];
174 SG_LOG( SG_INSTR, SG_DEBUG, "findStationByFreq " << freq << " size " << stations.size() );
176 if (!stations.empty()) {
184 // FGTACANList ----------------------------------------------------------------
186 FGTACANList::FGTACANList( void )
191 FGTACANList::~FGTACANList( void )
196 bool FGTACANList::init()
202 // add an entry to the lists
203 bool FGTACANList::add( FGTACANRecord *c )
205 ident_channels[c->get_channel()].push_back(c);
210 // Given a TACAN Channel return the first matching frequency
211 FGTACANRecord *FGTACANList::findByChannel( const string& channel )
213 const tacan_list_type& stations = ident_channels[channel];
214 SG_LOG( SG_INSTR, SG_DEBUG, "findByChannel " << channel<< " size " << stations.size() );
216 if (!stations.empty()) {