]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atcutils.cxx
Because the new code is a stub, it really ought to be doing nothing, not even print...
[flightgear.git] / src / ATC / atcutils.cxx
1 // commlist.cxx -- comm frequency lookup class
2 //
3 // Written by David Luff and Alexander Kappes, started Jan 2003.
4 // Based on navlist.cxx by Curtis Olson, started April 2000.
5 //
6 // Copyright (C) 2000  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
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.
12 //
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.
17 //
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.
21
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27
28
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>
36
37 #include "atcutils.hxx"
38
39 #if !ENABLE_ATCDCL
40
41
42 FGCommList *current_commlist;
43
44
45 // Constructor
46 FGCommList::FGCommList( void ) {
47       sg_srandom_time();
48 }
49
50
51 // Destructor
52 FGCommList::~FGCommList( void ) {
53 }
54
55
56 // load the navaids and build the map
57 bool FGCommList::init( const SGPath& path ) {
58 /*
59     SGPath temp = 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" );
63     LoadComms(temp);
64     temp = path;
65     temp.append( "ATC/default.tower" );
66     LoadComms(temp);
67     temp = path;
68     temp.append( "ATC/default.ground" );
69     LoadComms(temp);
70     temp = path;
71     temp.append( "ATC/default.approach" );
72     LoadComms(temp);*/
73     return true;
74 }
75     
76
77 bool FGCommList::LoadComms(const SGPath& path) {
78 /*
79     sg_gzifstream fin( path.str() );
80     if ( !fin.is_open() ) {
81         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
82         exit(-1);
83     }
84     
85     // read in each line of the file
86     fin >> skipcomment;
87
88     while ( !fin.eof() ) {
89         ATCData a;
90         fin >> a;
91         if(a.type == INVALID) {
92             SG_LOG(SG_GENERAL, SG_DEBUG, "WARNING - INVALID type found in " << path.str() << '\n');
93         } else {        
94             // Push all stations onto frequency map
95             commlist_freq[a.freq].push_back(a);
96             
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?) {
100                 // get bucket number
101                 SGBucket bucket(a.geod);
102                 int bucknum = bucket.gen_index();
103                 commlist_bck[bucknum].push_back(a);
104             //}
105         }
106         
107         fin >> skipcomment;
108     }
109     
110     fin.close();*/
111     return true;    
112 }
113
114
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 )
121 {
122 /*
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();
127     
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;
132     double 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);
137         
138         //cout << "  dist = " << sqrt(d)
139         //     << "  range = " << current->range * SG_NM_TO_METER << endl;
140         
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)) {
148                 if(d < max_d) {
149                     max_d = d;
150                     *ad = *current;
151                 }
152             }
153         }
154     }
155     
156     if(max_d < orig_max_d) {
157         return true;
158     } else {
159         return false;
160     }
161 */
162     return false;
163 }
164
165 int FGCommList::FindByPos(const SGGeod& aPos, double range, comm_list_type* stations, atc_type tp)
166 {
167      // number of relevant stations found within range
168      int found = 0;
169 /*
170      stations->erase(stations->begin(), stations->end());
171      
172      // get bucket number for plane position
173      SGBucket buck(aPos);
174  
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;
178      
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();
188              
189              
190              // double az1, az2, s;
191              SGVec3d aircraft = SGVec3d::fromGeod(aPos);
192              double d;
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);
200                          ++found;
201                      }
202                  }
203              }
204          }
205      }
206 */
207      return found;
208 }
209
210
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) {
219 /*
220     int num_stations = 0;
221     int range = 10;
222     comm_list_type stations;
223     comm_list_iterator itr;
224     double distance = -9999.0;
225     
226     while(num_stations == 0) {
227         num_stations = FindByPos(aPos, range, &stations, tp);
228         if(num_stations) {
229             double closest = max_range * SG_NM_TO_METER;
230             double tmp;
231             for(itr = stations.begin(); itr != stations.end(); ++itr) { 
232                 ATCData ad2 = *itr;    
233                 const FGAirport *a = fgFindAirportID(ad2.ident);
234                 if (a) {
235                     tmp = dclGetHorizontalSeparation(ad2.geod, aPos);
236                     if(tmp <= closest) {
237                         closest = tmp;
238                         distance = tmp;
239                         ad = *itr;
240                     }
241                 }
242             }
243             //cout << "Closest station is " << ad.ident << " at a range of " << distance << " meters\n";
244             return(distance);
245         }
246         if(range > max_range) {
247             break;
248         }
249         range *= 10;
250     }
251 */
252     return(-9999.0);
253 }
254
255
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 ) {
260 /*
261     const FGAirport *a = fgFindAirportID( ICAO);
262     if ( a) {
263         comm_list_type stations;
264         int found = FindByPos(a->geod(), 10.0, &stations, tp);
265         if(found) {
266             comm_list_iterator itr = stations.begin();
267             while(itr != stations.end()) {
268                 if(((*itr).ident == ICAO) && ((*itr).type == tp)) {
269                     ad = *itr;
270                     //cout << "FindByCode returns " << ICAO
271                     //     << "  type: " << tp
272                     //   << "  freq: " << itr->freq
273                     //   << endl;
274                     return true;
275                 }
276                 ++itr;
277             }
278         }
279     }
280 */
281     return false;
282 }
283
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 
287 // last time.
288 int FGCommList::GetAtisSequence( const string& apt_id, 
289         const double tstamp, const int interval, const int special)
290 {
291 /*
292     atis_transmission_type tran;
293     
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;
301     } 
302
303 // calculate the appropriate identifier and update the log
304     tran = atislog[apt_id];
305
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));
314 */
315     return 2600;
316 }
317 /*****************************************************************************
318  * FGKln89AlignedProjection 
319  ****************************************************************************/
320
321 FGKln89AlignedProjection::FGKln89AlignedProjection() {
322     _origin.setLatitudeRad(0);
323     _origin.setLongitudeRad(0);
324     _origin.setElevationM(0);
325     _correction_factor = cos(_origin.getLatitudeRad());
326 }
327
328 FGKln89AlignedProjection::FGKln89AlignedProjection(const SGGeod& centre, double heading) {
329     _origin = centre;
330     _theta = heading * SG_DEGREES_TO_RADIANS;
331     _correction_factor = cos(_origin.getLatitudeRad());
332 }
333
334 FGKln89AlignedProjection::~FGKln89AlignedProjection() {
335 }
336
337 void FGKln89AlignedProjection::Init(const SGGeod& centre, double heading) {
338     _origin = centre;
339     _theta = heading * SG_DEGREES_TO_RADIANS;
340     _correction_factor = cos(_origin.getLatitudeRad());
341 }
342
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;
349
350     // Align
351     if(_theta != 0.0) {
352         double xbar = x;
353         x = x*cos(_theta) - y*sin(_theta);
354         y = (xbar*sin(_theta)) + (y*cos(_theta));
355     }
356
357     return SGVec3d(x, y, pt.getElevationM());
358 }
359
360 SGGeod FGKln89AlignedProjection::ConvertFromLocal(const SGVec3d& pt) {
361     // de-align
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));
365
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;
369
370     return SGGeod::fromRadM(_origin.getLongitudeRad()+delta_lon, _origin.getLatitudeRad()+delta_lat, pt.z());
371 }
372
373 #endif // #ENABLE_ATCDCL