1 // runways.cxx -- a simple class to manage airport runway info
3 // Written by Curtis Olson, started August 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
28 #include <cmath> // fabs()
29 #include <cstdio> // sprintf()
30 #include <cstdlib> // atoi()
32 #include <simgear/compiler.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <Main/fg_props.hxx>
39 #include "runways.hxx"
41 SG_USING_NAMESPACE(std);
42 SG_USING_STD(istream);
43 SG_USING_STD(multimap);
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,
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 )
61 // strip trailing "x" if it exists in runway number
62 string tmp = rwy._rwy_no.substr(2, 1);
64 rwy._rwy_no = rwy._rwy_no.substr(0, 2);
69 rwy._heading = heading;
72 rwy._displ_thresh1 = displ_thresh1;
73 rwy._displ_thresh2 = displ_thresh2;
74 rwy._stopway1 = stopway1;
75 rwy._stopway2 = stopway2;
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;
84 if ( rwy_no[0] == 'x' ) {
85 rwy._type = "taxiway";
89 runways.insert(pair<const string, FGRunway>(rwy._id, rwy));
93 // Return reverse rwy number
96 static string GetReverseRunwayNo(string& rwyno) {
97 // cout << "Original rwyno = " << rwyNo << '\n';
99 // Helipads don't have a seperate number per end
100 if(rwyno.size() && (rwyno[0] == 'H' || rwyno[0] == 'h' || rwyno[0] == 'x')) {
104 // standardize input number
105 string tmp = rwyno.substr(1, 1);
106 if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (rwyno.size() == 1)) {
109 SG_LOG( SG_GENERAL, SG_INFO,
110 "Standardising rwy number from " << tmp << " to " << rwyno );
114 int rn = atoi(rwyno.substr(0,2).c_str());
119 sprintf(buf, "%02i", rn);
120 if(rwyno.size() == 3) {
121 if(rwyno.substr(2,1) == "L") {
124 } else if (rwyno.substr(2,1) == "R") {
127 } else if (rwyno.substr(2,1) == "C") {
130 } else if (rwyno.substr(2,1) == "T") {
134 SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
135 << rwyno << " passed to GetReverseRunwayNo(...)");
142 // search for the specified apt id (wierd!)
143 bool FGRunwayList::search( const string& aptid, FGRunway* r ) {
144 runway_map_iterator pos;
146 pos = runways.lower_bound(aptid);
147 if ( pos != runways.end() ) {
157 // search for the specified apt id and runway no
158 bool FGRunwayList::search( const string& aptid, const string& rwyno,
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))
170 runwayno = "0" + tmp;
171 SG_LOG( SG_GENERAL, SG_INFO, "Standardising rwy number from "
172 << tmp << " to " << runwayno );
174 revrwyno = GetReverseRunwayNo(runwayno);
176 runway_map_iterator pos;
177 for ( pos = runways.lower_bound( aptid );
178 pos != runways.upper_bound( aptid ); ++pos)
180 if ( pos->second._rwy_no == runwayno ) {
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
190 // NOTE - matching revrwyno implies that runwayno was
192 r->_rwy_no = runwayno;
193 r->_heading += 180.0;
203 FGRunway FGRunwayList::search( const string& aptid ) {
210 // Return the runway closest to a given heading
211 bool FGRunwayList::search( const string& aptid, const int tgt_hdg,
214 string rwyNo = search(aptid, tgt_hdg);
215 return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
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);
225 if (!search(aptid, &r)) {
226 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to find "
227 << aptid << " in database.");
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);
239 bool reversed = false;
244 if (r._type != "runway")
248 if (r._surface_code == 12 || r._surface_code == 5) // dry lakebed & gravel
250 else if (r._surface_code == 1 || r._surface_code == 2) // asphalt & concrete
253 double quality, bad, diff;
254 double good = lenwgt * r._length + widwgt * r._width + surfwgt * surface + 1e-20;
257 diff = hdg - r._heading;
262 bad = fabs(devwgt * diff) + 1e-20;
264 quality = good / bad;
265 //SG_LOG(SG_GENERAL, SG_ALERT, " runway " << r._rwy_no << " -> " << quality);
273 diff = hdg - r._heading - 180;
278 bad = fabs(devwgt * diff) + 1e-20;
280 quality = good / bad;
281 //SG_LOG(SG_GENERAL, SG_ALERT, " runway " << GetReverseRunwayNo(r._rwy_no)
282 // << " -> " << quality);
291 return reversed ? GetReverseRunwayNo(best._rwy_no) : best._rwy_no;
295 bool FGRunwayList::next( FGRunway* runway ) {
297 if ( current != runways.end() ) {
298 *runway = current->second;
306 FGRunway FGRunwayList::next() {
310 if ( current != runways.end() ) {
311 result = current->second;
319 FGRunwayList::~FGRunwayList( void ) {