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 recomputeBounds(true);
110 FGRunwayRef AirportDiagram::selectedRunway() const
112 return m_selectedRunway;
115 void AirportDiagram::setSelectedRunway(FGRunwayRef r)
117 if (r == m_selectedRunway) {
121 m_selectedRunway = r;
125 void AirportDiagram::setApproachExtensionDistance(double distanceNm)
127 m_approachDistanceNm = distanceNm;
128 recomputeBounds(true);
132 void AirportDiagram::addRunway(FGRunwayRef rwy)
134 Q_FOREACH(RunwayData rd, m_runways) {
135 if (rd.runway == rwy->reciprocalRunway()) {
136 return; // only add one end of reciprocal runways
141 r.p1 = project(rwy->geod());
142 r.p2 = project(rwy->end());
143 r.widthM = qRound(rwy->widthM());
147 recomputeBounds(false);
151 void AirportDiagram::doComputeBounds()
153 Q_FOREACH(const RunwayData& r, m_runways) {
158 Q_FOREACH(const TaxiwayData& t, m_taxiways) {
163 Q_FOREACH(const ParkingData& p, m_parking) {
167 if (m_selectedRunway && (m_approachDistanceNm > 0.0)) {
168 double d = SG_NM_TO_METER * m_approachDistanceNm;
169 QPointF pt = project(m_selectedRunway->pointOnCenterline(-d));
174 void AirportDiagram::addParking(FGParkingRef park)
176 ParkingData pd = { project(park->geod()), park };
177 m_parking.push_back(pd);
178 recomputeBounds(false);
183 void AirportDiagram::paintContents(QPainter* p)
185 QTransform t = p->transform();
188 QBrush brush(QColor(0x9f, 0x9f, 0x9f));
189 Q_FOREACH(const QPainterPath& path, m_pavements) {
194 Q_FOREACH(const TaxiwayData& t, m_taxiways) {
195 QPen pen(QColor(0x9f, 0x9f, 0x9f));
196 pen.setWidth(t.widthM);
198 p->drawLine(t.p1, t.p2);
206 // draw ILS first so underneath all runways
207 QPen pen(Qt::magenta);
208 pen.setWidth(1.0 / m_scale);
211 Q_FOREACH(const RunwayData& r, m_runways) {
212 drawILS(p, r.runway);
213 drawILS(p, r.runway->reciprocalRunway());
216 // now draw the runways for real
217 Q_FOREACH(const RunwayData& r, m_runways) {
219 QColor color(Qt::magenta);
220 if ((r.runway == m_selectedRunway) || (r.runway->reciprocalRunway() == m_selectedRunway)) {
227 pen.setWidth(r.widthM);
230 p->drawLine(r.p1, r.p2);
233 QString ident = QString::fromStdString(r.runway->ident());
236 p->rotate(r.runway->headingDeg());
237 // invert scaling factor so we can use screen pixel sizes here
238 p->scale(1.0 / m_scale, 1.0/ m_scale);
240 p->setPen((r.runway == m_selectedRunway) ? Qt::yellow : Qt::magenta);
241 p->drawText(QRect(-100, 5, 200, 200), ident, Qt::AlignHCenter | Qt::AlignTop);
243 FGRunway* recip = r.runway->reciprocalRunway();
244 QString recipIdent = QString::fromStdString(recip->ident());
248 p->rotate(recip->headingDeg());
249 p->scale(1.0 / m_scale, 1.0/ m_scale);
251 p->setPen((r.runway->reciprocalRunway() == m_selectedRunway) ? Qt::yellow : Qt::magenta);
252 p->drawText(QRect(-100, 5, 200, 200), recipIdent, Qt::AlignHCenter | Qt::AlignTop);
255 if (m_selectedRunway && (m_approachDistanceNm > 0.0)) {
257 // draw approach extension point
258 double d = SG_NM_TO_METER * m_approachDistanceNm;
259 QPointF pt = project(m_selectedRunway->pointOnCenterline(-d));
260 QPointF pt2 = project(m_selectedRunway->geod());
261 QPen pen(Qt::yellow);
262 pen.setWidth(2.0 / m_scale);
264 p->drawLine(pt, pt2);
268 void AirportDiagram::drawILS(QPainter* painter, FGRunwayRef runway) const
273 FGNavRecord* loc = runway->ILS();
277 double halfBeamWidth = loc->localizerWidth() * 0.5;
278 QPointF threshold = project(runway->threshold());
279 double rangeM = loc->get_range() * SG_NM_TO_METER;
280 double radial = loc->get_multiuse();
281 SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
283 // compute the three end points at the wide end of the arrow
284 QPointF endCentre = project(SGGeodesy::direct(loc->geod(), radial, -rangeM));
285 QPointF endR = project(SGGeodesy::direct(loc->geod(), radial + halfBeamWidth, -rangeM * 1.1));
286 QPointF endL = project(SGGeodesy::direct(loc->geod(), radial - halfBeamWidth, -rangeM * 1.1));
288 painter->drawLine(threshold, endCentre);
289 painter->drawLine(threshold, endL);
290 painter->drawLine(threshold, endR);
291 painter->drawLine(endL, endCentre);
292 painter->drawLine(endR, endCentre);
295 void AirportDiagram::mouseReleaseEvent(QMouseEvent* me)
298 return; // ignore panning drag+release ops here
300 QTransform t(transform());
301 double minDist = std::numeric_limits<double>::max();
302 FGRunwayRef bestRunway;
304 Q_FOREACH(const RunwayData& r, m_runways) {
305 QPointF p1(t.map(r.p1)), p2(t.map(r.p2));
307 double d = distanceToLineSegment(QVector2D(me->pos()),
312 bestRunway = r.runway->reciprocalRunway();
314 bestRunway = r.runway;
320 if (minDist < 16.0) {
321 emit clickedRunway(bestRunway);
325 void AirportDiagram::buildTaxiways()
328 for (unsigned int tIndex=0; tIndex < m_airport->numTaxiways(); ++tIndex) {
329 FGTaxiwayRef tx = m_airport->getTaxiwayByIndex(tIndex);
332 td.p1 = project(tx->geod());
333 td.p2 = project(tx->pointOnCenterline(tx->lengthM()));
335 td.widthM = tx->widthM();
336 m_taxiways.append(td);
340 void AirportDiagram::buildPavements()
343 for (unsigned int pIndex=0; pIndex < m_airport->numPavements(); ++pIndex) {
344 FGPavementRef pave = m_airport->getPavementByIndex(pIndex);
345 if (pave->getNodeList().empty()) {
352 QPointF p0 = project(pave->getNodeList().front()->mPos);
354 FGPavement::NodeList::const_iterator it;
355 for (it = pave->getNodeList().begin(); it != pave->getNodeList().end(); ) {
356 const FGPavement::BezierNode *bn = dynamic_cast<const FGPavement::BezierNode *>(it->get());
357 bool close = (*it)->mClose;
359 // increment iterator so we can look at the next point
361 QPointF nextPoint = (it == pave->getNodeList().end()) ? startPoint : project((*it)->mPos);
364 QPointF control = project(bn->mControl);
365 QPointF endPoint = close ? startPoint : nextPoint;
366 pp.quadTo(control, endPoint);
368 // straight line segment
380 startPoint = QPointF();
384 } // of nodes iteration
390 m_pavements.append(pp);
391 } // of pavements iteration