]> git.mxchange.org Git - flightgear.git/blob - src/Airports/runways.cxx
Avoid some useless file accesses
[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 <cstdio>              // sprintf()
29 #include <cstdlib>             // atoi()
30 #include <cassert>
31
32 #include <simgear/compiler.h>
33
34 #include <simgear/props/props.hxx>
35
36 #include <string>
37
38 #include "runways.hxx"
39
40 #include <Airports/simple.hxx>
41 #include <Navaids/procedure.hxx>
42 #include <Navaids/navrecord.hxx>
43 #include <Navaids/PositionedBinding.hxx>
44
45 using std::string;
46
47 static std::string cleanRunwayNo(const std::string& aRwyNo)
48 {
49   if (aRwyNo[0] == 'x') {
50     return std::string(); // no ident for taxiways
51   }
52   
53   string result(aRwyNo);
54   // canonicalise runway ident
55   if ((aRwyNo.size() == 1) || !isdigit(aRwyNo[1])) {
56           result = "0" + aRwyNo;
57   }
58
59   // trim off trailing garbage
60   if (result.size() > 2) {
61     char suffix = toupper(result[2]);
62     if (suffix == 'X') {
63        result = result.substr(0, 2);
64     }
65   }
66   
67   return result;
68 }
69
70 FGRunway::FGRunway(FGAirport* aAirport, const string& aIdent,
71                         const SGGeod& aGeod,
72                         const double heading, const double length,
73                         const double width,
74                         const double displ_thresh,
75                         const double stopway,
76                         const int surface_code,
77                         bool reciprocal) :
78   FGRunwayBase(RUNWAY, cleanRunwayNo(aIdent), aGeod, heading, length, width, surface_code, true),
79   _airport(aAirport),
80   _isReciprocal(reciprocal),
81   _reciprocal(NULL),
82   _displ_thresh(displ_thresh),
83   _stopway(stopway),
84   _ils(NULL)
85 {
86 }
87
88 string FGRunway::reverseIdent(const string& aRunwayIdent)
89 {
90   // Helipads don't have a seperate number per end
91   if (aRunwayIdent.size() && (aRunwayIdent[0] == 'H' || aRunwayIdent[0] == 'h' || aRunwayIdent[0] == 'x')) {
92     return aRunwayIdent;
93   }
94
95   std::string ident(aRunwayIdent);
96     
97   char buf[4];
98   int rn = atoi(ident.substr(0,2).c_str());
99   rn += 18;
100   while(rn > 36) {
101           rn -= 36;
102   }
103   
104   sprintf(buf, "%02i", rn);
105   if(ident.size() == 3) {
106     char suffix = toupper(ident[2]);
107     if(suffix == 'L') {
108       buf[2] = 'R';
109     } else if (suffix == 'R') {
110       buf[2] = 'L';
111     } else {
112       // something else, just copy
113       buf[2] = suffix;
114     }
115     
116     buf[3] = 0;
117   }
118   
119   return buf;
120 }
121
122 double FGRunway::score(double aLengthWt, double aWidthWt, double aSurfaceWt) const
123 {
124   int surface = 1;
125   if (_surface_code == 12 || _surface_code == 5) // dry lakebed & gravel
126     surface = 2;
127   else if (_surface_code == 1 || _surface_code == 2) // asphalt & concrete
128     surface = 3;
129     
130   return _length * aLengthWt + _width * aWidthWt + surface * aSurfaceWt + 1e-20;
131 }
132
133 SGGeod FGRunway::begin() const
134 {
135   return pointOnCenterline(0.0);
136 }
137
138 SGGeod FGRunway::end() const
139 {
140   return pointOnCenterline(lengthM());
141 }
142
143 SGGeod FGRunway::threshold() const
144 {
145   return pointOnCenterline(_displ_thresh * SG_FEET_TO_METER);
146 }
147
148 void FGRunway::processThreshold(SGPropertyNode* aThreshold)
149 {
150   assert(ident() == aThreshold->getStringValue("rwy"));
151   
152   double lon = aThreshold->getDoubleValue("lon"),
153     lat = aThreshold->getDoubleValue("lat");
154   SGGeod newThreshold(SGGeod::fromDegM(lon, lat, mPosition.getElevationM()));
155   
156   _heading = aThreshold->getDoubleValue("hdg-deg");
157   _displ_thresh = aThreshold->getDoubleValue("displ-m") * SG_METER_TO_FEET;
158   _stopway = aThreshold->getDoubleValue("stopw-m") * SG_METER_TO_FEET;
159   
160   // compute the new runway center, based on the threshold lat/lon and length,
161   double offsetFt = (0.5 * _length);
162   SGGeod newCenter;
163   double dummy;
164   SGGeodesy::direct(newThreshold, _heading, offsetFt * SG_FEET_TO_METER, newCenter, dummy);
165   mPosition = newCenter;
166
167
168 void FGRunway::setReciprocalRunway(FGRunway* other)
169 {
170   assert(_reciprocal==NULL);
171   assert((other->_reciprocal == NULL) || (other->_reciprocal == this));
172   
173   _reciprocal = other;
174 }
175
176 std::vector<flightgear::SID*> FGRunway::getSIDs()
177 {
178   std::vector<flightgear::SID*> result;
179   for (unsigned int i=0; i<_airport->numSIDs(); ++i) {
180     flightgear::SID* s = _airport->getSIDByIndex(i);
181     if (s->isForRunway(this)) {
182       result.push_back(s);
183     }
184   } // of SIDs at the airport iteration
185   
186   return result;
187 }
188
189 std::vector<flightgear::STAR*> FGRunway::getSTARs()
190 {
191   std::vector<flightgear::STAR*> result;
192   for (unsigned int i=0; i<_airport->numSTARs(); ++i) {
193     flightgear::STAR* s = _airport->getSTARByIndex(i);
194     if (s->isForRunway(this)) {
195       result.push_back(s);
196     }
197   } // of STARs at the airport iteration
198   
199   return result;
200 }
201
202 flightgear::PositionedBinding*
203 FGRunway::createBinding(SGPropertyNode* nd) const
204 {
205     return new flightgear::RunwayBinding(this, nd);
206 }
207