]> git.mxchange.org Git - flightgear.git/blob - src/Airports/runways.cxx
Fixes for compiling with gcc 4.3
[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 <cmath>               // fabs()
29 #include <cstdio>              // sprintf()
30 #include <cstdlib>             // atoi()
31
32 #include <simgear/compiler.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <Main/fg_props.hxx>
35
36 #include STL_STRING
37 #include <map>
38
39 #include "runways.hxx"
40
41 SG_USING_NAMESPACE(std);
42 SG_USING_STD(istream);
43 SG_USING_STD(multimap);
44
45
46 // add an entry to the list
47 void FGRunwayList::add( const string& id, const string& rwy_no,
48                         const double longitude, const double latitude,
49                         const double heading, const double length,
50                         const double width,
51                         const double displ_thresh1, const double displ_thresh2,
52                         const double stopway1, const double stopway2,
53                         const string& lighting_flags, const int surface_code,
54                         const string& shoulder_code, const int marking_code,
55                         const double smoothness, const bool dist_remaining )
56 {
57     FGRunway rwy;
58
59     rwy._id = id;
60     rwy._rwy_no = rwy_no;
61     // strip trailing "x" if it exists in runway number
62     string tmp = rwy._rwy_no.substr(2, 1);
63     if ( tmp == "x" ) {
64         rwy._rwy_no = rwy._rwy_no.substr(0, 2);
65     }
66
67     rwy._lon = longitude;
68     rwy._lat = latitude;
69     rwy._heading = heading;
70     rwy._length = length;
71     rwy._width = width;
72     rwy._displ_thresh1 = displ_thresh1;
73     rwy._displ_thresh2 = displ_thresh2;
74     rwy._stopway1 = stopway1;
75     rwy._stopway2 = stopway2;
76
77     rwy._lighting_flags = lighting_flags;
78     rwy._surface_code = surface_code;
79     rwy._shoulder_code = shoulder_code;
80     rwy._marking_code = marking_code;
81     rwy._smoothness = smoothness;
82     rwy._dist_remaining = dist_remaining;
83
84     if ( rwy_no[0] == 'x' ) {
85         rwy._type = "taxiway";
86     } else {
87         rwy._type = "runway";
88     }
89     runways.insert(pair<const string, FGRunway>(rwy._id, rwy));
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' || rwyno[0] == 'x')) {
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 hdg ) {
221     //SG_LOG(SG_GENERAL, SG_ALERT, "searching runway for " << aptid
222     //        << " with target heading " << hdg);
223
224     FGRunway r;
225     if (!search(aptid, &r)) {
226         SG_LOG(SG_GENERAL, SG_ALERT, "Failed to find "
227                 << aptid << " in database.");
228         return "NN";
229     }
230
231     SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
232     double lenwgt = param->getDoubleValue("length-weight", 0.01);
233     double widwgt = param->getDoubleValue("width-weight", 0.01);
234     double surfwgt = param->getDoubleValue("surface-weight", 10);
235     double devwgt = param->getDoubleValue("deviation-weight", 1);
236
237     FGRunway best;
238     double max = 0.0;
239     bool reversed = false;
240
241     do {
242         if (r._id != aptid)
243             break;
244         if (r._type != "runway")
245             continue;
246
247         int surface = 1;
248         if (r._surface_code == 12 || r._surface_code == 5) // dry lakebed & gravel
249             surface = 2;
250         else if (r._surface_code == 1 || r._surface_code == 2) // asphalt & concrete
251             surface = 3;
252
253         double quality, bad, diff;
254         double good = lenwgt * r._length + widwgt * r._width + surfwgt * surface + 1e-20;
255
256         // this side
257         diff = hdg - r._heading;
258         while (diff < -180)
259             diff += 360;
260         while (diff >= 180)
261             diff -= 360;
262         bad = fabs(devwgt * diff) + 1e-20;
263
264         quality = good / bad;
265         //SG_LOG(SG_GENERAL, SG_ALERT, "  runway " << r._rwy_no <<  " -> " << quality);
266         if (quality > max) {
267             max = quality;
268             best = r;
269             reversed = false;
270         }
271
272         // other side
273         diff = hdg - r._heading - 180;
274         while (diff < -180)
275             diff += 360;
276         while (diff >= 180)
277             diff -= 360;
278         bad = fabs(devwgt * diff) + 1e-20;
279
280         quality = good / bad;
281         //SG_LOG(SG_GENERAL, SG_ALERT, "  runway " << GetReverseRunwayNo(r._rwy_no)
282         //        <<  " -> " << quality);
283         if (quality > max) {
284             max = quality;
285             best = r;
286             reversed = true;
287         }
288
289     } while (next(&r));
290
291     return reversed ? GetReverseRunwayNo(best._rwy_no) : best._rwy_no;
292 }
293
294
295 bool FGRunwayList::next( FGRunway* runway ) {
296     ++current;
297     if ( current != runways.end() ) {
298         *runway = current->second;
299         return true;
300     } else {
301         return false;
302     }
303 }
304
305
306 FGRunway FGRunwayList::next() {
307     FGRunway result;
308
309     ++current;
310     if ( current != runways.end() ) {
311         result = current->second;
312     }
313
314     return result;
315 }
316
317
318 // Destructor
319 FGRunwayList::~FGRunwayList( void ) {
320 }