1 // AirportDiagram.cxx - part of GUI launcher using Qt5
3 // Written by James Turner, started December 2014.
5 // Copyright (C) 2014 James Turner <zakalawe@mac.com>
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.
21 #include "AirportDiagram.hxx"
25 #include <simgear/sg_inlines.h>
30 #include <QMouseEvent>
32 #include <Airports/airport.hxx>
33 #include <Airports/runways.hxx>
34 #include <Airports/parking.hxx>
35 #include <Airports/pavement.hxx>
37 #include <Navaids/navrecord.hxx>
39 static double distanceToLineSegment(const QVector2D& p, const QVector2D& a,
40 const QVector2D& b, double* outT = NULL)
45 // Squared length, to avoid a sqrt
46 const qreal len2 = ab.lengthSquared();
48 // Line null, the projection can't exist, we return the first point
53 return (p - a).length();
56 // Parametric value of the projection on the line
57 const qreal t = (ac.x() * ab.x() + ac.y() * ab.y()) / len2;
60 // Point is before the first point
64 return (p - a).length();
66 // Point is after the second point
70 return (p - b).length();
76 const QVector2D proj = a + t * ab;
77 return (proj - p).length();
83 AirportDiagram::AirportDiagram(QWidget* pr) :
85 m_approachDistanceNm(-1.0)
89 AirportDiagram::~AirportDiagram()
94 void AirportDiagram::setAirport(FGAirportRef apt)
97 m_projectionCenter = apt ? apt->geod() : SGGeod();
99 m_approachDistanceNm = -1.0;
106 clearIgnoredNavaids();
107 addIgnoredNavaid(apt);
109 recomputeBounds(true);
113 FGRunwayRef AirportDiagram::selectedRunway() const
115 return m_selectedRunway;
118 void AirportDiagram::setSelectedRunway(FGRunwayRef r)
120 if (r == m_selectedRunway) {
124 m_selectedRunway = r;
128 void AirportDiagram::setApproachExtensionDistance(double distanceNm)
130 m_approachDistanceNm = distanceNm;
131 recomputeBounds(true);
135 void AirportDiagram::addRunway(FGRunwayRef rwy)
137 Q_FOREACH(RunwayData rd, m_runways) {
138 if (rd.runway == rwy->reciprocalRunway()) {
139 return; // only add one end of reciprocal runways
144 r.p1 = project(rwy->geod());
145 r.p2 = project(rwy->end());
146 r.widthM = qRound(rwy->widthM());
150 recomputeBounds(false);
154 void AirportDiagram::doComputeBounds()
156 Q_FOREACH(const RunwayData& r, m_runways) {
161 Q_FOREACH(const TaxiwayData& t, m_taxiways) {
166 Q_FOREACH(const ParkingData& p, m_parking) {
170 if (m_selectedRunway && (m_approachDistanceNm > 0.0)) {
171 double d = SG_NM_TO_METER * m_approachDistanceNm;
172 QPointF pt = project(m_selectedRunway->pointOnCenterline(-d));
177 void AirportDiagram::addParking(FGParkingRef park)
179 ParkingData pd = { project(park->geod()), park };
180 m_parking.push_back(pd);
181 recomputeBounds(false);
186 void AirportDiagram::paintContents(QPainter* p)
188 QTransform t = p->transform();
191 QBrush brush(QColor(0x9f, 0x9f, 0x9f));
192 Q_FOREACH(const QPainterPath& path, m_pavements) {
197 Q_FOREACH(const TaxiwayData& t, m_taxiways) {
198 QPen pen(QColor(0x9f, 0x9f, 0x9f));
199 pen.setWidth(t.widthM);
201 p->drawLine(t.p1, t.p2);
209 // draw ILS first so underneath all runways
210 QPen pen(QColor(0x5f, 0x5f, 0x5f));
212 pen.setCosmetic(true);
215 Q_FOREACH(const RunwayData& r, m_runways) {
216 drawILS(p, r.runway);
217 drawILS(p, r.runway->reciprocalRunway());
220 bool drawAircraft = false;
224 // now draw the runways for real
225 Q_FOREACH(const RunwayData& r, m_runways) {
227 QColor color(Qt::magenta);
228 if ((r.runway == m_selectedRunway) || (r.runway->reciprocalRunway() == m_selectedRunway)) {
235 pen.setWidth(r.widthM);
238 p->drawLine(r.p1, r.p2);
241 QString ident = QString::fromStdString(r.runway->ident());
244 p->rotate(r.runway->headingDeg());
245 // invert scaling factor so we can use screen pixel sizes here
246 p->scale(1.0 / m_scale, 1.0/ m_scale);
248 p->setPen((r.runway == m_selectedRunway) ? Qt::yellow : Qt::magenta);
249 p->drawText(QRect(-100, 5, 200, 200), ident, Qt::AlignHCenter | Qt::AlignTop);
251 FGRunway* recip = r.runway->reciprocalRunway();
252 QString recipIdent = QString::fromStdString(recip->ident());
256 p->rotate(recip->headingDeg());
257 p->scale(1.0 / m_scale, 1.0/ m_scale);
259 p->setPen((r.runway->reciprocalRunway() == m_selectedRunway) ? Qt::yellow : Qt::magenta);
260 p->drawText(QRect(-100, 5, 200, 200), recipIdent, Qt::AlignHCenter | Qt::AlignTop);
263 if (m_selectedRunway) {
265 aircraftPos = m_selectedRunway->geod();
266 headingDeg = m_selectedRunway->headingDeg();
269 if (m_selectedRunway && (m_approachDistanceNm > 0.0)) {
271 // draw approach extension point
272 double d = SG_NM_TO_METER * m_approachDistanceNm;
273 QPointF pt = project(m_selectedRunway->pointOnCenterline(-d));
274 QPointF pt2 = project(m_selectedRunway->geod());
275 QPen pen(Qt::yellow);
276 pen.setWidth(2.0 / m_scale);
278 p->drawLine(pt, pt2);
280 aircraftPos = m_selectedRunway->pointOnCenterline(-d);
285 paintAirplaneIcon(p, aircraftPos, headingDeg);
289 void AirportDiagram::drawILS(QPainter* painter, FGRunwayRef runway) const
294 FGNavRecord* loc = runway->ILS();
298 double halfBeamWidth = loc->localizerWidth() * 0.5;
299 QPointF threshold = project(runway->threshold());
300 double rangeM = loc->get_range() * SG_NM_TO_METER;
301 double radial = loc->get_multiuse();
302 SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
304 // compute the three end points at the wide end of the arrow
305 QPointF endCentre = project(SGGeodesy::direct(loc->geod(), radial, -rangeM));
306 QPointF endR = project(SGGeodesy::direct(loc->geod(), radial + halfBeamWidth, -rangeM * 1.1));
307 QPointF endL = project(SGGeodesy::direct(loc->geod(), radial - halfBeamWidth, -rangeM * 1.1));
309 painter->drawLine(threshold, endCentre);
310 painter->drawLine(threshold, endL);
311 painter->drawLine(threshold, endR);
312 painter->drawLine(endL, endCentre);
313 painter->drawLine(endR, endCentre);
316 void AirportDiagram::mouseReleaseEvent(QMouseEvent* me)
319 return; // ignore panning drag+release ops here
321 QTransform t(transform());
322 double minDist = std::numeric_limits<double>::max();
323 FGRunwayRef bestRunway;
325 Q_FOREACH(const RunwayData& r, m_runways) {
326 QPointF p1(t.map(r.p1)), p2(t.map(r.p2));
328 double d = distanceToLineSegment(QVector2D(me->pos()),
333 bestRunway = r.runway->reciprocalRunway();
335 bestRunway = r.runway;
341 if (minDist < 16.0) {
342 emit clickedRunway(bestRunway);
346 void AirportDiagram::buildTaxiways()
349 for (unsigned int tIndex=0; tIndex < m_airport->numTaxiways(); ++tIndex) {
350 FGTaxiwayRef tx = m_airport->getTaxiwayByIndex(tIndex);
353 td.p1 = project(tx->geod());
354 td.p2 = project(tx->pointOnCenterline(tx->lengthM()));
356 td.widthM = tx->widthM();
357 m_taxiways.append(td);
361 void AirportDiagram::buildPavements()
364 for (unsigned int pIndex=0; pIndex < m_airport->numPavements(); ++pIndex) {
365 FGPavementRef pave = m_airport->getPavementByIndex(pIndex);
366 if (pave->getNodeList().empty()) {
373 QPointF p0 = project(pave->getNodeList().front()->mPos);
375 FGPavement::NodeList::const_iterator it;
376 for (it = pave->getNodeList().begin(); it != pave->getNodeList().end(); ) {
377 const FGPavement::BezierNode *bn = dynamic_cast<const FGPavement::BezierNode *>(it->get());
378 bool close = (*it)->mClose;
380 // increment iterator so we can look at the next point
382 QPointF nextPoint = (it == pave->getNodeList().end()) ? startPoint : project((*it)->mPos);
385 QPointF control = project(bn->mControl);
386 QPointF endPoint = close ? startPoint : nextPoint;
387 pp.quadTo(control, endPoint);
389 // straight line segment
401 startPoint = QPointF();
405 } // of nodes iteration
411 m_pavements.append(pp);
412 } // of pavements iteration