]> git.mxchange.org Git - flightgear.git/blob - src/Airports/runways.cxx
Initial checkin.
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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     // Helipads don't have a seperate number per end
100     if(rwyno.size() && (rwyno[0] == 'H' || rwyno[0] == 'h')) {
101         return rwyno;
102     }
103     
104     // standardize input number
105     string tmp = rwyno.substr(1, 1);
106     if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (rwyno.size() == 1)) {
107         tmp = rwyno;
108         rwyno = "0" + tmp;
109         SG_LOG( SG_GENERAL, SG_INFO,
110                 "Standardising rwy number from " << tmp << " to " << rwyno );
111     }
112     
113     char buf[4];
114     int rn = atoi(rwyno.substr(0,2).c_str());
115     rn += 18;
116     while(rn > 36) {
117         rn -= 36;
118     }
119     sprintf(buf, "%02i", rn);
120     if(rwyno.size() == 3) {
121         if(rwyno.substr(2,1) == "L") {
122             buf[2] = 'R';
123             buf[3] = '\0';
124         } else if (rwyno.substr(2,1) == "R") {
125             buf[2] = 'L';
126             buf[3] = '\0';
127         } else if (rwyno.substr(2,1) == "C") {
128             buf[2] = 'C';
129             buf[3] = '\0';
130         } else if (rwyno.substr(2,1) == "T") {
131             buf[2] = 'T';
132             buf[3] = '\0';
133         } else {
134             SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
135             << rwyno << " passed to GetReverseRunwayNo(...)");
136         }
137     }
138     return(buf);
139 }
140
141
142 // search for the specified apt id (wierd!)
143 bool FGRunwayList::search( const string& aptid, FGRunway* r ) {
144     runway_map_iterator pos;
145
146     pos = runways.lower_bound(aptid);
147     if ( pos != runways.end() ) {
148         current = pos;
149         *r = pos->second;
150         return true;
151     } else {
152         return false;
153     }
154 }
155
156
157 // search for the specified apt id and runway no
158 bool FGRunwayList::search( const string& aptid, const string& rwyno,
159                            FGRunway *r )
160 {
161     string revrwyno = "";
162     string runwayno = rwyno;
163     if ( runwayno.length() ) {
164         // standardize input number
165         string tmp = runwayno.substr(1, 1);
166         if (( tmp == "L" || tmp == "R" || tmp == "C" )
167             || (runwayno.size() == 1))
168         {
169             tmp = runwayno;
170             runwayno = "0" + tmp;
171             SG_LOG( SG_GENERAL, SG_INFO, "Standardising rwy number from "
172                     << tmp << " to " << runwayno );
173         }
174         revrwyno = GetReverseRunwayNo(runwayno);
175     }
176     runway_map_iterator pos;
177     for ( pos = runways.lower_bound( aptid );
178           pos != runways.upper_bound( aptid ); ++pos)
179     {
180         if ( pos->second._rwy_no == runwayno ) {
181             current = pos;
182             *r = pos->second;
183             return true;
184         } else if ( pos->second._rwy_no == revrwyno ) {
185             // Search again with the other-end runway number.
186             // Remember we have to munge the heading and rwy_no
187             // results if this one matches
188             current = pos;
189             *r = pos->second;
190             // NOTE - matching revrwyno implies that runwayno was
191             // actually correct.
192             r->_rwy_no = runwayno;
193             r->_heading += 180.0;
194             return true;
195         }
196     }
197
198     return false;
199 }
200
201
202 // (wierd!)
203 FGRunway FGRunwayList::search( const string& aptid ) {
204     FGRunway a;
205     search( aptid, &a );
206     return a;
207 }
208
209
210 // Return the runway closest to a given heading
211 bool FGRunwayList::search( const string& aptid, const int tgt_hdg,
212                            FGRunway *runway )
213 {
214     string rwyNo = search(aptid, tgt_hdg);
215     return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
216 }
217
218
219 // Return the runway number of the runway closest to a given heading
220 string FGRunwayList::search( const string& aptid, const int tgt_hdg ) {
221     FGRunway r;
222     FGRunway tmp_r;     
223     string rn;
224     double found_dir = 0.0;
225
226     if ( !search( aptid, &tmp_r ) ) {
227         SG_LOG( SG_GENERAL, SG_ALERT,
228                 "Failed to find " << aptid << " in database." );
229         return "NN";
230     }
231     
232     double diff;
233     double min_diff = 360.0;
234     
235     while ( tmp_r._id == aptid ) {
236         // forward direction
237         diff = tgt_hdg - tmp_r._heading;
238         while ( diff < -180.0 ) { diff += 360.0; }
239         while ( diff >  180.0 ) { diff -= 360.0; }
240         diff = fabs(diff);
241         // SG_LOG( SG_GENERAL, SG_INFO,
242         //         "Runway " << tmp_r._rwy_no << " heading = "
243         //         << tmp_r._heading << " diff = " << diff );
244         if ( diff < min_diff ) {
245             min_diff = diff;
246             r = tmp_r;
247             found_dir = 0;
248         }
249         
250         // reverse direction
251         diff = tgt_hdg - tmp_r._heading - 180.0;
252         while ( diff < -180.0 ) { diff += 360.0; }
253         while ( diff >  180.0 ) { diff -= 360.0; }
254         diff = fabs(diff);
255         // SG_LOG( SG_GENERAL, SG_INFO,
256         //         "Runway -" << tmp_r._rwy_no << " heading = " <<
257         //         tmp_r._heading + 180.0 <<
258         //         " diff = " << diff );
259         if ( diff < min_diff ) {
260             min_diff = diff;
261             r = tmp_r;
262             found_dir = 180.0;
263         }
264         
265         next( &tmp_r );
266     }
267     
268     // SG_LOG( SG_GENERAL, SG_INFO, "closest runway = " << r._rwy_no
269     //         << " + " << found_dir );
270     rn = r._rwy_no;
271     if ( found_dir == 180 ) {
272         rn = GetReverseRunwayNo(rn);
273     }   
274     
275     return rn;
276 }
277
278
279 bool FGRunwayList::next( FGRunway* runway ) {
280     ++current;
281     if ( current != runways.end() ) {
282         *runway = current->second;
283         return true;
284     } else {
285         return false;
286     }
287 }
288
289
290 FGRunway FGRunwayList::next() {
291     FGRunway result;
292
293     ++current;
294     if ( current != runways.end() ) {
295         result = current->second;
296     }
297
298     return result;
299 }
300
301
302 // Destructor
303 FGRunwayList::~FGRunwayList( void ) {
304 }