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"
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,
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 )
60 // strip trailing "x" if it exists in runway number
61 string tmp = rwy._rwy_no.substr(2, 1);
63 rwy._rwy_no = rwy._rwy_no.substr(0, 2);
68 rwy._heading = heading;
71 rwy._displ_thresh1 = displ_thresh1;
72 rwy._displ_thresh2 = displ_thresh2;
73 rwy._stopway1 = stopway1;
74 rwy._stopway2 = stopway2;
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;
83 if ( rwy_no[0] == 'x' ) {
84 rwy._type = "taxiway";
88 runways.insert(pair<const string, FGRunway>(rwy._id, rwy));
92 // Return reverse rwy number
95 static string GetReverseRunwayNo(string& rwyno) {
96 // cout << "Original rwyno = " << rwyNo << '\n';
98 // Helipads don't have a seperate number per end
99 if(rwyno.size() && (rwyno[0] == 'H' || rwyno[0] == 'h' || rwyno[0] == 'x')) {
103 // standardize input number
104 string tmp = rwyno.substr(1, 1);
105 if (( tmp == "L" || tmp == "R" || tmp == "C" ) || (rwyno.size() == 1)) {
108 SG_LOG( SG_GENERAL, SG_INFO,
109 "Standardising rwy number from " << tmp << " to " << rwyno );
113 int rn = atoi(rwyno.substr(0,2).c_str());
118 sprintf(buf, "%02i", rn);
119 if(rwyno.size() == 3) {
120 if(rwyno.substr(2,1) == "L") {
123 } else if (rwyno.substr(2,1) == "R") {
126 } else if (rwyno.substr(2,1) == "C") {
129 } else if (rwyno.substr(2,1) == "T") {
133 SG_LOG(SG_GENERAL, SG_ALERT, "Unknown runway code "
134 << rwyno << " passed to GetReverseRunwayNo(...)");
141 // search for the specified apt id (wierd!)
142 bool FGRunwayList::search( const string& aptid, FGRunway* r ) {
143 runway_map_iterator pos;
145 pos = runways.lower_bound(aptid);
146 if ( pos != runways.end() ) {
156 // search for the specified apt id and runway no
157 bool FGRunwayList::search( const string& aptid, const string& rwyno,
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))
169 runwayno = "0" + tmp;
170 SG_LOG( SG_GENERAL, SG_INFO, "Standardising rwy number from "
171 << tmp << " to " << runwayno );
173 revrwyno = GetReverseRunwayNo(runwayno);
175 runway_map_iterator pos;
176 for ( pos = runways.lower_bound( aptid );
177 pos != runways.upper_bound( aptid ); ++pos)
179 if ( pos->second._rwy_no == runwayno ) {
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
189 // NOTE - matching revrwyno implies that runwayno was
191 r->_rwy_no = runwayno;
192 r->_heading += 180.0;
202 FGRunway FGRunwayList::search( const string& aptid ) {
209 // Return the runway closest to a given heading
210 bool FGRunwayList::search( const string& aptid, const int tgt_hdg,
213 string rwyNo = search(aptid, tgt_hdg);
214 return(rwyNo == "NN" ? false : search(aptid, rwyNo, runway));
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);
224 if (!search(aptid, &r)) {
225 SG_LOG(SG_GENERAL, SG_ALERT, "Failed to find "
226 << aptid << " in database.");
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);
238 bool reversed = false;
243 if (r._type != "runway")
247 if (r._surface_code == 12 || r._surface_code == 5) // dry lakebed & gravel
249 else if (r._surface_code == 1 || r._surface_code == 2) // asphalt & concrete
252 double quality, bad, diff;
253 double good = lenwgt * r._length + widwgt * r._width + surfwgt * surface + 1e-20;
256 diff = hdg - r._heading;
261 bad = fabs(devwgt * diff) + 1e-20;
263 quality = good / bad;
264 //SG_LOG(SG_GENERAL, SG_ALERT, " runway " << r._rwy_no << " -> " << quality);
272 diff = hdg - r._heading - 180;
277 bad = fabs(devwgt * diff) + 1e-20;
279 quality = good / bad;
280 //SG_LOG(SG_GENERAL, SG_ALERT, " runway " << GetReverseRunwayNo(r._rwy_no)
281 // << " -> " << quality);
290 return reversed ? GetReverseRunwayNo(best._rwy_no) : best._rwy_no;
294 bool FGRunwayList::next( FGRunway* runway ) {
296 if ( current != runways.end() ) {
297 *runway = current->second;
305 FGRunway FGRunwayList::next() {
309 if ( current != runways.end() ) {
310 result = current->second;
318 FGRunwayList::~FGRunwayList( void ) {