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 // now draw the runways for real
221 Q_FOREACH(const RunwayData& r, m_runways) {
223 QColor color(Qt::magenta);
224 if ((r.runway == m_selectedRunway) || (r.runway->reciprocalRunway() == m_selectedRunway)) {
231 pen.setWidth(r.widthM);
234 p->drawLine(r.p1, r.p2);
237 QString ident = QString::fromStdString(r.runway->ident());
240 p->rotate(r.runway->headingDeg());
241 // invert scaling factor so we can use screen pixel sizes here
242 p->scale(1.0 / m_scale, 1.0/ m_scale);
244 p->setPen((r.runway == m_selectedRunway) ? Qt::yellow : Qt::magenta);
245 p->drawText(QRect(-100, 5, 200, 200), ident, Qt::AlignHCenter | Qt::AlignTop);
247 FGRunway* recip = r.runway->reciprocalRunway();
248 QString recipIdent = QString::fromStdString(recip->ident());
252 p->rotate(recip->headingDeg());
253 p->scale(1.0 / m_scale, 1.0/ m_scale);
255 p->setPen((r.runway->reciprocalRunway() == m_selectedRunway) ? Qt::yellow : Qt::magenta);
256 p->drawText(QRect(-100, 5, 200, 200), recipIdent, Qt::AlignHCenter | Qt::AlignTop);
259 if (m_selectedRunway && (m_approachDistanceNm > 0.0)) {
261 // draw approach extension point
262 double d = SG_NM_TO_METER * m_approachDistanceNm;
263 QPointF pt = project(m_selectedRunway->pointOnCenterline(-d));
264 QPointF pt2 = project(m_selectedRunway->geod());
265 QPen pen(Qt::yellow);
266 pen.setWidth(2.0 / m_scale);
268 p->drawLine(pt, pt2);
272 // aircraft pos and heading...
273 // paintAirplaneIcon(painter, );
276 void AirportDiagram::drawILS(QPainter* painter, FGRunwayRef runway) const
281 FGNavRecord* loc = runway->ILS();
285 double halfBeamWidth = loc->localizerWidth() * 0.5;
286 QPointF threshold = project(runway->threshold());
287 double rangeM = loc->get_range() * SG_NM_TO_METER;
288 double radial = loc->get_multiuse();
289 SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
291 // compute the three end points at the wide end of the arrow
292 QPointF endCentre = project(SGGeodesy::direct(loc->geod(), radial, -rangeM));
293 QPointF endR = project(SGGeodesy::direct(loc->geod(), radial + halfBeamWidth, -rangeM * 1.1));
294 QPointF endL = project(SGGeodesy::direct(loc->geod(), radial - halfBeamWidth, -rangeM * 1.1));
296 painter->drawLine(threshold, endCentre);
297 painter->drawLine(threshold, endL);
298 painter->drawLine(threshold, endR);
299 painter->drawLine(endL, endCentre);
300 painter->drawLine(endR, endCentre);
303 void AirportDiagram::mouseReleaseEvent(QMouseEvent* me)
306 return; // ignore panning drag+release ops here
308 QTransform t(transform());
309 double minDist = std::numeric_limits<double>::max();
310 FGRunwayRef bestRunway;
312 Q_FOREACH(const RunwayData& r, m_runways) {
313 QPointF p1(t.map(r.p1)), p2(t.map(r.p2));
315 double d = distanceToLineSegment(QVector2D(me->pos()),
320 bestRunway = r.runway->reciprocalRunway();
322 bestRunway = r.runway;
328 if (minDist < 16.0) {
329 emit clickedRunway(bestRunway);
333 void AirportDiagram::buildTaxiways()
336 for (unsigned int tIndex=0; tIndex < m_airport->numTaxiways(); ++tIndex) {
337 FGTaxiwayRef tx = m_airport->getTaxiwayByIndex(tIndex);
340 td.p1 = project(tx->geod());
341 td.p2 = project(tx->pointOnCenterline(tx->lengthM()));
343 td.widthM = tx->widthM();
344 m_taxiways.append(td);
348 void AirportDiagram::buildPavements()
351 for (unsigned int pIndex=0; pIndex < m_airport->numPavements(); ++pIndex) {
352 FGPavementRef pave = m_airport->getPavementByIndex(pIndex);
353 if (pave->getNodeList().empty()) {
360 QPointF p0 = project(pave->getNodeList().front()->mPos);
362 FGPavement::NodeList::const_iterator it;
363 for (it = pave->getNodeList().begin(); it != pave->getNodeList().end(); ) {
364 const FGPavement::BezierNode *bn = dynamic_cast<const FGPavement::BezierNode *>(it->get());
365 bool close = (*it)->mClose;
367 // increment iterator so we can look at the next point
369 QPointF nextPoint = (it == pave->getNodeList().end()) ? startPoint : project((*it)->mPos);
372 QPointF control = project(bn->mControl);
373 QPointF endPoint = close ? startPoint : nextPoint;
374 pp.quadTo(control, endPoint);
376 // straight line segment
388 startPoint = QPointF();
392 } // of nodes iteration
398 m_pavements.append(pp);
399 } // of pavements iteration