]> git.mxchange.org Git - flightgear.git/blob - src/Airports/runways.cxx
84c064263d6e28fdc4499dfe85e7b9f98fdb804a
[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 using std::string;
44
45 static FGPositioned::Type runwayTypeFromNumber(const std::string& aRwyNo)
46 {
47   return (aRwyNo[0] == 'x') ? FGPositioned::TAXIWAY : FGPositioned::RUNWAY;
48 }
49
50 static std::string cleanRunwayNo(const std::string& aRwyNo)
51 {
52   if (aRwyNo[0] == 'x') {
53     return std::string(); // no ident for taxiways
54   }
55   
56   string result(aRwyNo);
57   // canonicalise runway ident
58   if ((aRwyNo.size() == 1) || !isdigit(aRwyNo[1])) {
59           result = "0" + aRwyNo;
60   }
61
62   // trim off trailing garbage
63   if (result.size() > 2) {
64     char suffix = toupper(result[2]);
65     if (suffix == 'X') {
66        result = result.substr(0, 2);
67     }
68   }
69   
70   return result;
71 }
72
73 FGRunway::FGRunway(FGAirport* aAirport, const string& rwy_no,
74                         const SGGeod& aGeod,
75                         const double heading, const double length,
76                         const double width,
77                         const double displ_thresh,
78                         const double stopway,
79                         const int surface_code,
80                         bool reciprocal) :
81   FGPositioned(runwayTypeFromNumber(rwy_no), cleanRunwayNo(rwy_no), aGeod, 
82     (runwayTypeFromNumber(rwy_no) == FGPositioned::RUNWAY)),
83   _airport(aAirport),
84   _reciprocal(reciprocal)
85 {
86   _heading = heading;
87   _length = length;
88   _width = width;
89   _displ_thresh = displ_thresh;
90   _stopway = stopway;
91
92   _surface_code = surface_code;
93 }
94
95 string FGRunway::reverseIdent(const string& aRunwayIdent)
96 {
97   // Helipads don't have a seperate number per end
98   if (aRunwayIdent.size() && (aRunwayIdent[0] == 'H' || aRunwayIdent[0] == 'h' || aRunwayIdent[0] == 'x')) {
99     return aRunwayIdent;
100   }
101
102   std::string ident(aRunwayIdent);
103     
104   char buf[4];
105   int rn = atoi(ident.substr(0,2).c_str());
106   rn += 18;
107   while(rn > 36) {
108           rn -= 36;
109   }
110   
111   sprintf(buf, "%02i", rn);
112   if(ident.size() == 3) {
113     char suffix = toupper(ident[2]);
114     if(suffix == 'L') {
115       buf[2] = 'R';
116     } else if (suffix == 'R') {
117       buf[2] = 'L';
118     } else {
119       // something else, just copy
120       buf[2] = suffix;
121     }
122     
123     buf[3] = 0;
124   }
125   
126   return buf;
127 }
128
129 double FGRunway::score(double aLengthWt, double aWidthWt, double aSurfaceWt) const
130 {
131   int surface = 1;
132   if (_surface_code == 12 || _surface_code == 5) // dry lakebed & gravel
133     surface = 2;
134   else if (_surface_code == 1 || _surface_code == 2) // asphalt & concrete
135     surface = 3;
136     
137   return _length * aLengthWt + _width * aWidthWt + surface * aSurfaceWt + 1e-20;
138 }
139
140 bool FGRunway::isTaxiway() const
141 {
142   return (type() == TAXIWAY);
143 }
144
145 SGGeod FGRunway::threshold() const
146 {
147   return pointOnCenterline(0.0);
148 }
149
150 SGGeod FGRunway::reverseThreshold() const
151 {
152   return pointOnCenterline(lengthM());
153 }
154
155 SGGeod FGRunway::displacedThreshold() const
156 {
157   return pointOnCenterline(_displ_thresh * SG_FEET_TO_METER);
158 }
159
160 SGGeod FGRunway::pointOnCenterline(double aOffset) const
161 {
162   SGGeod result;
163   double dummyAz2;
164   double halfLengthMetres = lengthM() * 0.5;
165   
166   SGGeodesy::direct(mPosition, _heading, 
167     aOffset - halfLengthMetres,
168     result, dummyAz2);
169   return result;
170 }