| 1 | /* |
| 2 | * SPDX-FileCopyrightText: (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt |
| 3 | * SPDX_FileComment: a simple class to manage airport runway info |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include <config.h> |
| 8 | |
| 9 | #include <cassert> |
| 10 | #include <simgear/compiler.h> |
| 11 | |
| 12 | #include <simgear/props/props.hxx> |
| 13 | |
| 14 | #include <string> |
| 15 | |
| 16 | #include "runways.hxx" |
| 17 | |
| 18 | #include <Airports/airport.hxx> |
| 19 | #include <Navaids/NavDataCache.hxx> |
| 20 | #include <Navaids/navrecord.hxx> |
| 21 | #include <Navaids/procedure.hxx> |
| 22 | |
| 23 | using std::string; |
| 24 | |
| 25 | FGRunway::FGRunway(PositionedID aGuid, |
| 26 | PositionedID aAirport, const string& aIdent, |
| 27 | const SGGeod& aGeod, |
| 28 | const double heading, const double length, |
| 29 | const double width, |
| 30 | const double displ_thresh, |
| 31 | const double stopway, |
| 32 | const int surface_code) : FGRunwayBase(aGuid, RUNWAY, aIdent, aGeod, |
| 33 | heading, length, width, surface_code, aAirport), |
| 34 | _reciprocal(0), |
| 35 | _displ_thresh(displ_thresh), |
| 36 | _stopway(stopway), |
| 37 | _ils(0) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | string FGRunway::reverseIdent(const string& aRunwayIdent) |
| 42 | { |
| 43 | // Helipads don't have a seperate number per end |
| 44 | if (aRunwayIdent.size() && (aRunwayIdent[0] == 'H' || aRunwayIdent[0] == 'h' || aRunwayIdent[0] == 'x')) { |
| 45 | return aRunwayIdent; |
| 46 | } |
| 47 | |
| 48 | std::string ident(aRunwayIdent); |
| 49 | |
| 50 | int rn = std::stoi(ident.substr(0, 2)); |
| 51 | rn += 18; |
| 52 | while (rn > 36) { |
| 53 | rn -= 36; |
| 54 | } |
| 55 | |
| 56 | char buf[16]; |
| 57 | snprintf(buf, sizeof(buf) - 1, "%02i", rn); |
| 58 | |
| 59 | if (ident.size() == 3) { |
| 60 | char suffix = toupper(ident[2]); |
| 61 | if (suffix == 'L') { |
| 62 | buf[2] = 'R'; |
| 63 | } else if (suffix == 'R') { |
| 64 | buf[2] = 'L'; |
| 65 | } else { |
| 66 | // something else, just copy |
| 67 | buf[2] = suffix; |
| 68 | } |
| 69 | |
| 70 | buf[3] = 0; |
| 71 | } |
| 72 | |
| 73 | return buf; |
| 74 | } |
| 75 | |
| 76 | double FGRunway::score(double aLengthWt, double aWidthWt, double aSurfaceWt, double aIlsWt) const |
| 77 | { |
| 78 | int surface = 1; |
| 79 | if (_surface_code == 12 || _surface_code == 5) // dry lakebed & gravel |
| 80 | surface = 2; |
| 81 | else if (_surface_code == 1 || _surface_code == 2) // asphalt & concrete |
| 82 | surface = 3; |
| 83 | |
| 84 | int ils = (_ils != 0); |
| 85 | |
| 86 | return _length * aLengthWt + _width * aWidthWt + surface * aSurfaceWt + ils * aIlsWt + 1e-20; |
| 87 | } |
| 88 | |
| 89 | SGGeod FGRunway::begin() const |
| 90 | { |
| 91 | return pointOnCenterline(0.0); |
| 92 | } |
| 93 | |
| 94 | SGGeod FGRunway::end() const |
| 95 | { |
| 96 | return pointOnCenterline(lengthM()); |
| 97 | } |
| 98 | |
| 99 | SGGeod FGRunway::threshold() const |
| 100 | { |
| 101 | return pointOnCenterline(_displ_thresh); |
| 102 | } |
| 103 | |
| 104 | SGGeod FGRunway::pointOnCenterlineDisplaced(double aOffset) const |
| 105 | { |
| 106 | return pointOnCenterline(_displ_thresh+aOffset); |
| 107 | } |
| 108 | |
| 109 | void FGRunway::setReciprocalRunway(PositionedID other) |
| 110 | { |
| 111 | assert(_reciprocal == 0); |
| 112 | _reciprocal = other; |
| 113 | } |
| 114 | |
| 115 | FGRunway* FGRunway::reciprocalRunway() const |
| 116 | { |
| 117 | return loadById<FGRunway>(_reciprocal); |
| 118 | } |
| 119 | |
| 120 | FGNavRecord* FGRunway::ILS() const |
| 121 | { |
| 122 | if (_ils == 0) { |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | return loadById<FGNavRecord>(_ils); |
| 127 | } |
| 128 | |
| 129 | FGNavRecord* FGRunway::glideslope() const |
| 130 | { |
| 131 | flightgear::NavDataCache* cache = flightgear::NavDataCache::instance(); |
| 132 | PositionedID gsId = cache->findNavaidForRunway(guid(), FGPositioned::GS); |
| 133 | if (gsId == 0) { |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | return loadById<FGNavRecord>(gsId); |
| 138 | } |
| 139 | |
| 140 | flightgear::SIDList FGRunway::getSIDs() const |
| 141 | { |
| 142 | FGAirport* apt = airport(); |
| 143 | flightgear::SIDList result; |
| 144 | for (unsigned int i = 0; i < apt->numSIDs(); ++i) { |
| 145 | flightgear::SID* s = apt->getSIDByIndex(i); |
| 146 | if (s->isForRunway(this)) { |
| 147 | result.push_back(s); |
| 148 | } |
| 149 | } // of SIDs at the airport iteration |
| 150 | |
| 151 | return result; |
| 152 | } |
| 153 | |
| 154 | flightgear::STARList FGRunway::getSTARs() const |
| 155 | { |
| 156 | FGAirport* apt = airport(); |
| 157 | flightgear::STARList result; |
| 158 | for (unsigned int i = 0; i < apt->numSTARs(); ++i) { |
| 159 | flightgear::STAR* s = apt->getSTARByIndex(i); |
| 160 | if (s->isForRunway(this)) { |
| 161 | result.push_back(s); |
| 162 | } |
| 163 | } // of STARs at the airport iteration |
| 164 | |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | flightgear::ApproachList |
| 169 | FGRunway::getApproaches(flightgear::ProcedureType type) const |
| 170 | { |
| 171 | FGAirport* apt = airport(); |
| 172 | flightgear::ApproachList result; |
| 173 | for (unsigned int i = 0; i < apt->numApproaches(); ++i) { |
| 174 | flightgear::Approach* s = apt->getApproachByIndex(i); |
| 175 | if (s->runway() == this && (type == flightgear::PROCEDURE_INVALID || type == s->type())) { |
| 176 | result.push_back(s); |
| 177 | } |
| 178 | } // of approaches at the airport iteration |
| 179 | |
| 180 | return result; |
| 181 | } |
| 182 | |
| 183 | void FGRunway::updateThreshold(const SGGeod& newThreshold, double newHeading, |
| 184 | double newDisplacedThreshold, |
| 185 | double newStopway) |
| 186 | { |
| 187 | modifyPosition(newThreshold); |
| 188 | _heading = newHeading; |
| 189 | _stopway = newStopway; |
| 190 | _displ_thresh = newDisplacedThreshold; |
| 191 | } |
| 192 | |
| 193 | FGHelipad::FGHelipad(PositionedID aGuid, |
| 194 | PositionedID aAirport, const string& aIdent, |
| 195 | const SGGeod& aGeod, |
| 196 | const double heading, const double length, |
| 197 | const double width, |
| 198 | const int surface_code) : FGRunwayBase(aGuid, HELIPAD, aIdent, aGeod, |
| 199 | heading, length, width, surface_code, aAirport) |
| 200 | { |
| 201 | } |