1 // commlist.cxx -- comm frequency lookup class
3 // Written by David Luff and Alexander Kappes, started Jan 2003.
4 // Based on navlist.cxx by Curtis Olson, started April 2000.
6 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 #include <simgear/debug/logstream.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/misc/sgstream.hxx>
32 #include <simgear/math/sg_geodesy.hxx>
33 #include <simgear/math/sg_random.h>
34 #include <simgear/bucket/newbucket.hxx>
35 #include <Airports/simple.hxx>
37 #include "atcutils.hxx"
42 FGCommList *current_commlist;
46 FGCommList::FGCommList( void ) {
52 FGCommList::~FGCommList( void ) {
56 // load the navaids and build the map
57 bool FGCommList::init( const SGPath& path ) {
60 commlist_freq.erase(commlist_freq.begin(), commlist_freq.end());
61 commlist_bck.erase(commlist_bck.begin(), commlist_bck.end());
62 temp.append( "ATC/default.atis" );
65 temp.append( "ATC/default.tower" );
68 temp.append( "ATC/default.ground" );
71 temp.append( "ATC/default.approach" );
77 bool FGCommList::LoadComms(const SGPath& path) {
79 sg_gzifstream fin( path.str() );
80 if ( !fin.is_open() ) {
81 SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
85 // read in each line of the file
88 while ( !fin.eof() ) {
91 if(a.type == INVALID) {
92 SG_LOG(SG_GENERAL, SG_DEBUG, "WARNING - INVALID type found in " << path.str() << '\n');
94 // Push all stations onto frequency map
95 commlist_freq[a.freq].push_back(a);
97 // Push non-atis stations onto bucket map as well
98 // In fact, push all stations onto bucket map for now so FGATCMgr::GetFrequency() works.
99 //if(a.type != ATIS and/or AWOS?) {
101 SGBucket bucket(a.geod);
102 int bucknum = bucket.gen_index();
103 commlist_bck[bucknum].push_back(a);
115 // query the database for the specified frequency, lon and lat are in
116 // degrees, elev is in meters
117 // If no atc_type is specified, it returns true if any non-invalid type is found
118 // If atc_type is specifed, returns true only if the specified type is found
119 bool FGCommList::FindByFreq(const SGGeod& aPos, double freq,
120 ATCData* ad, atc_type tp )
123 comm_list_type stations;
124 stations = commlist_freq[kHz10(freq)];
125 comm_list_iterator current = stations.begin();
126 comm_list_iterator last = stations.end();
128 // double az1, az2, s;
129 SGVec3d aircraft = SGVec3d::fromGeod(aPos);
130 const double orig_max_d = 1e100;
131 double max_d = orig_max_d;
133 // TODO - at the moment this loop returns the first match found in range
134 // We want to return the closest match in the event of a frequency conflict
135 for ( ; current != last ; ++current ) {
136 d = distSqr(aircraft, current->cart);
138 //cout << " dist = " << sqrt(d)
139 // << " range = " << current->range * SG_NM_TO_METER << endl;
141 // TODO - match up to twice the published range so we can model
142 // reduced signal strength
143 // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
144 if ( d < (current->range * SG_NM_TO_METER
145 * current->range * SG_NM_TO_METER ) ) {
146 //cout << "matched = " << current->ident << endl;
147 if((tp == INVALID) || (tp == (*current).type)) {
156 if(max_d < orig_max_d) {
165 int FGCommList::FindByPos(const SGGeod& aPos, double range, comm_list_type* stations, atc_type tp)
167 // number of relevant stations found within range
170 stations->erase(stations->begin(), stations->end());
172 // get bucket number for plane position
175 // get neigboring buckets
176 int bx = (int)( range*SG_NM_TO_METER / buck.get_width_m() / 2) + 1;
177 int by = (int)( range*SG_NM_TO_METER / buck.get_height_m() / 2 ) + 1;
179 // loop over bucket range
180 for ( int i=-bx; i<=bx; i++) {
181 for ( int j=-by; j<=by; j++) {
182 buck = sgBucketOffset(aPos.getLongitudeDeg(), aPos.getLatitudeDeg(), i, j);
183 long int bucket = buck.gen_index();
184 comm_map_const_iterator Fstations = commlist_bck.find(bucket);
185 if (Fstations == commlist_bck.end()) continue;
186 comm_list_const_iterator current = Fstations->second.begin();
187 comm_list_const_iterator last = Fstations->second.end();
190 // double az1, az2, s;
191 SGVec3d aircraft = SGVec3d::fromGeod(aPos);
193 for(; current != last; ++current) {
194 if((current->type == tp) || (tp == INVALID)) {
195 d = distSqr(aircraft, current->cart);
196 // NOTE The below is squared since we match to distance3Dsquared (above) to avoid a sqrt.
197 if ( d < (current->range * SG_NM_TO_METER
198 * current->range * SG_NM_TO_METER ) ) {
199 stations->push_back(*current);
211 // Returns the distance in meters to the closest station of a given type,
212 // with the details written into ATCData& ad. If no type is specifed simply
213 // returns the distance to the closest station of any type.
214 // Returns -9999 if no stations found within max_range in nautical miles (default 100 miles).
215 // Note that the search algorithm starts at 10 miles and multiplies by 10 thereafter, so if
216 // say 300 miles is specifed 10, then 100, then 1000 will be searched, breaking at first result
217 // and giving up after 1000.
218 double FGCommList::FindClosest(const SGGeod& aPos, ATCData& ad, atc_type tp, double max_range) {
220 int num_stations = 0;
222 comm_list_type stations;
223 comm_list_iterator itr;
224 double distance = -9999.0;
226 while(num_stations == 0) {
227 num_stations = FindByPos(aPos, range, &stations, tp);
229 double closest = max_range * SG_NM_TO_METER;
231 for(itr = stations.begin(); itr != stations.end(); ++itr) {
233 const FGAirport *a = fgFindAirportID(ad2.ident);
235 tmp = dclGetHorizontalSeparation(ad2.geod, aPos);
243 //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
246 if(range > max_range) {
256 // Find by Airport code.
257 // This is basically a wrapper for a call to the airport database to get the airport
258 // position followed by a call to FindByPos(...)
259 bool FGCommList::FindByCode( const string& ICAO, ATCData& ad, atc_type tp ) {
261 const FGAirport *a = fgFindAirportID( ICAO);
263 comm_list_type stations;
264 int found = FindByPos(a->geod(), 10.0, &stations, tp);
266 comm_list_iterator itr = stations.begin();
267 while(itr != stations.end()) {
268 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
270 //cout << "FindByCode returns " << ICAO
271 // << " type: " << tp
272 // << " freq: " << itr->freq
284 // TODO - this function should move somewhere else eventually!
285 // Return an appropriate sequence number for an ATIS transmission.
286 // Return sequence number + 2600 if sequence is unchanged since
288 int FGCommList::GetAtisSequence( const string& apt_id,
289 const double tstamp, const int interval, const int special)
292 atis_transmission_type tran;
294 if(atislog.find(apt_id) == atislog.end()) { // New station
295 tran.tstamp = tstamp - interval;
296 // Random number between 0 and 25 inclusive, i.e. 26 equiprobable outcomes:
297 tran.sequence = int(sg_random() * LTRS);
298 atislog[apt_id] = tran;
299 //cout << "New ATIS station: " << apt_id << " seq-1: "
300 // << tran.sequence << endl;
303 // calculate the appropriate identifier and update the log
304 tran = atislog[apt_id];
306 int delta = int((tstamp - tran.tstamp) / interval);
307 tran.tstamp += delta * interval;
308 if (special && !delta) delta++; // a "special" ATIS update is required
309 tran.sequence = (tran.sequence + delta) % LTRS;
310 atislog[apt_id] = tran;
311 //if (delta) cout << "New ATIS sequence: " << tran.sequence
312 // << " Delta: " << delta << endl;
313 return(tran.sequence + (delta ? 0 : LTRS*1000));
317 /*****************************************************************************
318 * FGKln89AlignedProjection
319 ****************************************************************************/
321 FGKln89AlignedProjection::FGKln89AlignedProjection() {
322 _origin.setLatitudeRad(0);
323 _origin.setLongitudeRad(0);
324 _origin.setElevationM(0);
325 _correction_factor = cos(_origin.getLatitudeRad());
328 FGKln89AlignedProjection::FGKln89AlignedProjection(const SGGeod& centre, double heading) {
330 _theta = heading * SG_DEGREES_TO_RADIANS;
331 _correction_factor = cos(_origin.getLatitudeRad());
334 FGKln89AlignedProjection::~FGKln89AlignedProjection() {
337 void FGKln89AlignedProjection::Init(const SGGeod& centre, double heading) {
339 _theta = heading * SG_DEGREES_TO_RADIANS;
340 _correction_factor = cos(_origin.getLatitudeRad());
343 SGVec3d FGKln89AlignedProjection::ConvertToLocal(const SGGeod& pt) {
344 // convert from lat/lon to orthogonal
345 double delta_lat = pt.getLatitudeRad() - _origin.getLatitudeRad();
346 double delta_lon = pt.getLongitudeRad() - _origin.getLongitudeRad();
347 double y = sin(delta_lat) * SG_EQUATORIAL_RADIUS_M;
348 double x = sin(delta_lon) * SG_EQUATORIAL_RADIUS_M * _correction_factor;
353 x = x*cos(_theta) - y*sin(_theta);
354 y = (xbar*sin(_theta)) + (y*cos(_theta));
357 return SGVec3d(x, y, pt.getElevationM());
360 SGGeod FGKln89AlignedProjection::ConvertFromLocal(const SGVec3d& pt) {
362 double thi = _theta * -1.0;
363 double x = pt.x()*cos(thi) - pt.y()*sin(thi);
364 double y = (pt.x()*sin(thi)) + (pt.y()*cos(thi));
366 // convert from orthogonal to lat/lon
367 double delta_lat = asin(y / SG_EQUATORIAL_RADIUS_M);
368 double delta_lon = asin(x / SG_EQUATORIAL_RADIUS_M) / _correction_factor;
370 return SGGeod::fromRadM(_origin.getLongitudeRad()+delta_lon, _origin.getLatitudeRad()+delta_lat, pt.z());
373 #endif // #ENABLE_ATCDCL