]> git.mxchange.org Git - flightgear.git/blob - src/Airports/runways.cxx
- remove the SG_GLxxxx_H #defines, since OSG provides its own versions
[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 <string>
37 #include <map>
38
39 #include "runways.hxx"
40
41 using std::istream;
42 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[0] == 'x' ) {
84         rwy._type = "taxiway";
85     } else {
86         rwy._type = "runway";
87     }
88     runways.insert(pair<const string, FGRunway>(rwy._id, rwy));
89 }
90
91
92 // Return reverse rwy number
93 // eg 01 -> 19
94 // 03L -> 21R
95 static string GetReverseRunwayNo(string& rwyno) {       
96     // cout << "Original rwyno = " << rwyNo << '\n';
97     
98     // Helipads don't have a seperate number per end
99     if(rwyno.size() && (rwyno[0] == 'H' || rwyno[0] == 'h' || rwyno[0] == 'x')) {
100         return rwyno;
101     }
102     
103     // standardize input number
104     string tmp = rwyno.substr(1, 1);
105     if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (rwyno.size() == 1)) {
106         tmp = rwyno;
107         rwyno = "0" + tmp;
108         SG_LOG( SG_GENERAL, SG_INFO,
109                 "Standardising rwy number from " << tmp << " to " << rwyno );
110     }
111     
112     char buf[4];
113     int rn = atoi(rwyno.substr(0,2).c_str());
114     rn += 18;
115     while(rn > 36) {
116         rn -= 36;
117     }
118     sprintf(buf, "%02i", rn);
119     if(rwyno.size() == 3) {
120         if(rwyno.substr(2,1) == "L") {
121             buf[2] = 'R';
122             buf[3] = '\0';
123         } else if (rwyno.substr(2,1) == "R") {
124             buf[2] = 'L';
125             buf[3] = '\0';
126         } else if (rwyno.substr(2,1) == "C") {
127             buf[2] = 'C';
128             buf[3] = '\0';
129         } else if (rwyno.substr(2,1) == "T") {
130             buf[2] = 'T';
131             buf[3] = '\0';
132         } else {
133             SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
134             << rwyno << " passed to GetReverseRunwayNo(...)");
135         }
136     }
137     return(buf);
138 }
139
140
141 // search for the specified apt id (wierd!)
142 bool FGRunwayList::search( const string& aptid, FGRunway* r ) {
143     runway_map_iterator pos;
144
145     pos = runways.lower_bound(aptid);
146     if ( pos != runways.end() ) {
147         current = pos;
148         *r = pos->second;
149         return true;
150     } else {
151         return false;
152     }
153 }
154
155
156 // search for the specified apt id and runway no
157 bool FGRunwayList::search( const string& aptid, const string& rwyno,
158                            FGRunway *r )
159 {
160     string revrwyno = "";
161     string runwayno = rwyno;
162     if ( runwayno.length() ) {
163         // standardize input number
164         string tmp = runwayno.substr(1, 1);
165         if (( tmp == "L" || tmp == "R" || tmp == "C" )
166             || (runwayno.size() == 1))
167         {
168             tmp = runwayno;
169             runwayno = "0" + tmp;
170             SG_LOG( SG_GENERAL, SG_INFO, "Standardising rwy number from "
171                     << tmp << " to " << runwayno );
172         }
173         revrwyno = GetReverseRunwayNo(runwayno);
174     }
175     runway_map_iterator pos;
176     for ( pos = runways.lower_bound( aptid );
177           pos != runways.upper_bound( aptid ); ++pos)
178     {
179         if ( pos->second._rwy_no == runwayno ) {
180             current = pos;
181             *r = pos->second;
182             return true;
183         } else if ( pos->second._rwy_no == revrwyno ) {
184             // Search again with the other-end runway number.
185             // Remember we have to munge the heading and rwy_no
186             // results if this one matches
187             current = pos;
188             *r = pos->second;
189             // NOTE - matching revrwyno implies that runwayno was
190             // actually correct.
191             r->_rwy_no = runwayno;
192             r->_heading += 180.0;
193             return true;
194         }
195     }
196
197     return false;
198 }
199
200
201 // (wierd!)
202 FGRunway FGRunwayList::search( const string& aptid ) {
203     FGRunway a;
204     search( aptid, &a );
205     return a;
206 }
207
208
209 // Return the runway closest to a given heading
210 bool FGRunwayList::search( const string& aptid, const int tgt_hdg,
211                            FGRunway *runway )
212 {
213     string rwyNo = search(aptid, tgt_hdg);
214     return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
215 }
216
217
218 // Return the runway number of the runway closest to a given heading
219 string FGRunwayList::search( const string& aptid, const int hdg ) {
220     //SG_LOG(SG_GENERAL, SG_ALERT, "searching runway for " << aptid
221     //        << " with target heading " << hdg);
222
223     FGRunway r;
224     if (!search(aptid, &r)) {
225         SG_LOG(SG_GENERAL, SG_ALERT, "Failed to find "
226                 << aptid << " in database.");
227         return "NN";
228     }
229
230     SGPropertyNode *param = fgGetNode("/sim/airport/runways/search", true);
231     double lenwgt = param->getDoubleValue("length-weight", 0.01);
232     double widwgt = param->getDoubleValue("width-weight", 0.01);
233     double surfwgt = param->getDoubleValue("surface-weight", 10);
234     double devwgt = param->getDoubleValue("deviation-weight", 1);
235
236     FGRunway best;
237     double max = 0.0;
238     bool reversed = false;
239
240     do {
241         if (r._id != aptid)
242             break;
243         if (r._type != "runway")
244             continue;
245
246         int surface = 1;
247         if (r._surface_code == 12 || r._surface_code == 5) // dry lakebed & gravel
248             surface = 2;
249         else if (r._surface_code == 1 || r._surface_code == 2) // asphalt & concrete
250             surface = 3;
251
252         double quality, bad, diff;
253         double good = lenwgt * r._length + widwgt * r._width + surfwgt * surface + 1e-20;
254
255         // this side
256         diff = hdg - r._heading;
257         while (diff < -180)
258             diff += 360;
259         while (diff >= 180)
260             diff -= 360;
261         bad = fabs(devwgt * diff) + 1e-20;
262
263         quality = good / bad;
264         //SG_LOG(SG_GENERAL, SG_ALERT, "  runway " << r._rwy_no <<  " -> " << quality);
265         if (quality > max) {
266             max = quality;
267             best = r;
268             reversed = false;
269         }
270
271         // other side
272         diff = hdg - r._heading - 180;
273         while (diff < -180)
274             diff += 360;
275         while (diff >= 180)
276             diff -= 360;
277         bad = fabs(devwgt * diff) + 1e-20;
278
279         quality = good / bad;
280         //SG_LOG(SG_GENERAL, SG_ALERT, "  runway " << GetReverseRunwayNo(r._rwy_no)
281         //        <<  " -> " << quality);
282         if (quality > max) {
283             max = quality;
284             best = r;
285             reversed = true;
286         }
287
288     } while (next(&r));
289
290     return reversed ? GetReverseRunwayNo(best._rwy_no) : best._rwy_no;
291 }
292
293
294 bool FGRunwayList::next( FGRunway* runway ) {
295     ++current;
296     if ( current != runways.end() ) {
297         *runway = current->second;
298         return true;
299     } else {
300         return false;
301     }
302 }
303
304
305 FGRunway FGRunwayList::next() {
306     FGRunway result;
307
308     ++current;
309     if ( current != runways.end() ) {
310         result = current->second;
311     }
312
313     return result;
314 }
315
316
317 // Destructor
318 FGRunwayList::~FGRunwayList( void ) {
319 }