]> git.mxchange.org Git - flightgear.git/blob - src/Airports/runways.cxx
Alex Romosan:
[flightgear.git] / src / Airports / runways.cxx
1 // runways.cxx -- a simple class to manage airport runway info
2 //
3 // Written by Curtis Olson, started August 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
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 <math.h>               // fabs()
29 #include <stdio.h>              // sprintf()
30
31 #include <simgear/compiler.h>
32
33 #include <simgear/debug/logstream.hxx>
34
35 #include STL_STRING
36 #include <map>
37
38 #include "runways.hxx"
39
40 SG_USING_NAMESPACE(std);
41 SG_USING_STD(istream);
42 SG_USING_STD(multimap);
43
44
45 // add an entry to the list
46 void FGRunwayList::add( const string& id, const string& rwy_no,
47                         const double longitude, const double latitude,
48                         const double heading, const double length,
49                         const double width,
50                         const double displ_thresh1, const double displ_thresh2,
51                         const double stopway1, const double stopway2,
52                         const string& lighting_flags, const int surface_code,
53                         const string& shoulder_code, const int marking_code,
54                         const double smoothness, const bool dist_remaining )
55 {
56     FGRunway rwy;
57
58     rwy._id = id;
59     rwy._rwy_no = rwy_no;
60     // strip trailing "x" if it exists in runway number
61     string tmp = rwy._rwy_no.substr(2, 1);
62     if ( tmp == "x" ) {
63         rwy._rwy_no = rwy._rwy_no.substr(0, 2);
64     }
65
66     rwy._lon = longitude;
67     rwy._lat = latitude;
68     rwy._heading = heading;
69     rwy._length = length;
70     rwy._width = width;
71     rwy._displ_thresh1 = displ_thresh1;
72     rwy._displ_thresh2 = displ_thresh2;
73     rwy._stopway1 = stopway1;
74     rwy._stopway2 = stopway2;
75
76     rwy._lighting_flags = lighting_flags;
77     rwy._surface_code = surface_code;
78     rwy._shoulder_code = shoulder_code;
79     rwy._marking_code = marking_code;
80     rwy._smoothness = smoothness;
81     rwy._dist_remaining = dist_remaining;
82
83     if ( rwy_no == "xxx" ) {
84         rwy._type = "taxiway";
85         // don't insert taxiways into the DB for now
86     } else {
87         rwy._type = "runway";
88         runways.insert(pair<const string, FGRunway>(rwy._id, rwy));
89     }
90 }
91
92
93 // Return reverse rwy number
94 // eg 01 -> 19
95 // 03L -> 21R
96 static string GetReverseRunwayNo(string& rwyno) {       
97     // cout << "Original rwyno = " << rwyNo << '\n';
98     
99     // standardize input number
100     string tmp = rwyno.substr(1, 1);
101     if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (rwyno.size() == 1)) {
102         tmp = rwyno;
103         rwyno = "0" + tmp;
104         SG_LOG( SG_GENERAL, SG_INFO,
105                 "Standardising rwy number from " << tmp << " to " << rwyno );
106     }
107     
108     char buf[4];
109     int rn = atoi(rwyno.substr(0,2).c_str());
110     rn += 18;
111     while(rn > 36) {
112         rn -= 36;
113     }
114     sprintf(buf, "%02i", rn);
115     if(rwyno.size() == 3) {
116         if(rwyno.substr(2,1) == "L") {
117             buf[2] = 'R';
118             buf[3] = '\0';
119         } else if (rwyno.substr(2,1) == "R") {
120             buf[2] = 'L';
121             buf[3] = '\0';
122         } else if (rwyno.substr(2,1) == "C") {
123             buf[2] = 'C';
124             buf[3] = '\0';
125         } else if (rwyno.substr(2,1) == "T") {
126             buf[2] = 'T';
127             buf[3] = '\0';
128         } else {
129             SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
130             << rwyno << " passed to GetReverseRunwayNo(...)");
131         }
132     }
133     return(buf);
134 }
135
136
137 // search for the specified apt id (wierd!)
138 bool FGRunwayList::search( const string& aptid, FGRunway* r ) {
139     runway_map_iterator pos;
140
141     pos = runways.lower_bound(aptid);
142     if ( pos != runways.end() ) {
143         current = pos;
144         *r = pos->second;
145         return true;
146     } else {
147         return false;
148     }
149 }
150
151
152 // search for the specified apt id and runway no
153 bool FGRunwayList::search( const string& aptid, const string& rwyno,
154                            FGRunway *r )
155 {
156     string revrwyno = "";
157     string runwayno = rwyno;
158     if ( runwayno.length() ) {
159         // standardize input number
160         string tmp = runwayno.substr(1, 1);
161         if (( tmp == "L" || tmp == "R" || tmp == "C" )
162             || (runwayno.size() == 1))
163         {
164             tmp = runwayno;
165             runwayno = "0" + tmp;
166             SG_LOG( SG_GENERAL, SG_INFO, "Standardising rwy number from "
167                     << tmp << " to " << runwayno );
168         }
169         revrwyno = GetReverseRunwayNo(runwayno);
170     }
171     runway_map_iterator pos;
172     for ( pos = runways.lower_bound( aptid );
173           pos != runways.upper_bound( aptid ); ++pos)
174     {
175         if ( pos->second._rwy_no == runwayno ) {
176             current = pos;
177             *r = pos->second;
178             return true;
179         } else if ( pos->second._rwy_no == revrwyno ) {
180             // Search again with the other-end runway number.
181             // Remember we have to munge the heading and rwy_no
182             // results if this one matches
183             current = pos;
184             *r = pos->second;
185             // NOTE - matching revrwyno implies that runwayno was
186             // actually correct.
187             r->_rwy_no = runwayno;
188             r->_heading += 180.0;
189             return true;
190         }
191     }
192
193     return false;
194 }
195
196
197 // (wierd!)
198 FGRunway FGRunwayList::search( const string& aptid ) {
199     FGRunway a;
200     search( aptid, &a );
201     return a;
202 }
203
204
205 // Return the runway closest to a given heading
206 bool FGRunwayList::search( const string& aptid, const int tgt_hdg,
207                            FGRunway *runway )
208 {
209     string rwyNo = search(aptid, tgt_hdg);
210     return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
211 }
212
213
214 // Return the runway number of the runway closest to a given heading
215 string FGRunwayList::search( const string& aptid, const int tgt_hdg ) {
216     FGRunway r;
217     FGRunway tmp_r;     
218     string rn;
219     double found_dir = 0.0;
220
221     if ( !search( aptid, &tmp_r ) ) {
222         SG_LOG( SG_GENERAL, SG_ALERT,
223                 "Failed to find " << aptid << " in database." );
224         return "NN";
225     }
226     
227     double diff;
228     double min_diff = 360.0;
229     
230     while ( tmp_r._id == aptid ) {
231         // forward direction
232         diff = tgt_hdg - tmp_r._heading;
233         while ( diff < -180.0 ) { diff += 360.0; }
234         while ( diff >  180.0 ) { diff -= 360.0; }
235         diff = fabs(diff);
236         // SG_LOG( SG_GENERAL, SG_INFO,
237         //         "Runway " << tmp_r._rwy_no << " heading = "
238         //         << tmp_r._heading << " diff = " << diff );
239         if ( diff < min_diff ) {
240             min_diff = diff;
241             r = tmp_r;
242             found_dir = 0;
243         }
244         
245         // reverse direction
246         diff = tgt_hdg - tmp_r._heading - 180.0;
247         while ( diff < -180.0 ) { diff += 360.0; }
248         while ( diff >  180.0 ) { diff -= 360.0; }
249         diff = fabs(diff);
250         // SG_LOG( SG_GENERAL, SG_INFO,
251         //         "Runway -" << tmp_r._rwy_no << " heading = " <<
252         //         tmp_r._heading + 180.0 <<
253         //         " diff = " << diff );
254         if ( diff < min_diff ) {
255             min_diff = diff;
256             r = tmp_r;
257             found_dir = 180.0;
258         }
259         
260         next( &tmp_r );
261     }
262     
263     // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r._rwy_no
264     //         << " + " << found_dir );
265     rn = r._rwy_no;
266     if ( found_dir == 180 ) {
267         rn = GetReverseRunwayNo(rn);
268     }   
269     
270     return rn;
271 }
272
273
274 bool FGRunwayList::next( FGRunway* runway ) {
275     ++current;
276     if ( current != runways.end() ) {
277         *runway = current->second;
278         return true;
279     } else {
280         return false;
281     }
282 }
283
284
285 FGRunway FGRunwayList::next() {
286     FGRunway result;
287
288     ++current;
289     if ( current != runways.end() ) {
290         result = current->second;
291     }
292
293     return result;
294 }
295
296
297 // Destructor
298 FGRunwayList::~FGRunwayList( void ) {
299 }