]> git.mxchange.org Git - flightgear.git/blob - src/GUI/AirportDiagram.cxx
ILS drawing in the airport diagram
[flightgear.git] / src / GUI / AirportDiagram.cxx
1 // AirportDiagram.cxx - part of GUI launcher using Qt5
2 //
3 // Written by James Turner, started December 2014.
4 //
5 // Copyright (C) 2014 James Turner <zakalawe@mac.com>
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 #include "AirportDiagram.hxx"
22
23 #include <limits>
24
25 #include <simgear/sg_inlines.h>
26
27 #include <QPainter>
28 #include <QDebug>
29 #include <QVector2D>
30 #include <QMouseEvent>
31
32 #include <Airports/airport.hxx>
33 #include <Airports/runways.hxx>
34 #include <Airports/parking.hxx>
35 #include <Airports/pavement.hxx>
36
37 #include <Navaids/navrecord.hxx>
38
39 static double distanceToLineSegment(const QVector2D& p, const QVector2D& a,
40                                     const QVector2D& b, double* outT = NULL)
41 {
42     QVector2D ab(b - a);
43     QVector2D ac(p - a);
44     
45     // Squared length, to avoid a sqrt
46     const qreal len2 = ab.lengthSquared();
47     
48     // Line null, the projection can't exist, we return the first point
49     if (qIsNull(len2)) {
50         if (outT) {
51             *outT = 0.0;
52         }
53         return (p - a).length();
54     }
55     
56     // Parametric value of the projection on the line
57     const qreal t = (ac.x() * ab.x() + ac.y() * ab.y()) / len2;
58     
59     if (t < 0.0) {
60         // Point is before the first point
61         if (outT) {
62             *outT = 0.0;
63         }
64         return (p - a).length();
65     } else if (t > 1.0) {
66         // Point is after the second point
67         if (outT) {
68             *outT = 1.0;
69         }
70         return (p - b).length();
71     } else {
72         if (outT) {
73             *outT = t;
74         }
75         
76         const QVector2D proj = a + t * ab;
77         return (proj - p).length();
78     }
79     
80     return 0.0;
81 }
82
83 AirportDiagram::AirportDiagram(QWidget* pr) :
84     BaseDiagram(pr),
85     m_approachDistanceNm(-1.0)
86 {
87 }
88
89 AirportDiagram::~AirportDiagram()
90 {
91
92 }
93
94 void AirportDiagram::setAirport(FGAirportRef apt)
95 {
96     m_airport = apt;
97     m_projectionCenter = apt ? apt->geod() : SGGeod();
98     m_runways.clear();
99     m_approachDistanceNm = -1.0;
100
101     if (apt) {
102         buildTaxiways();
103         buildPavements();
104     }
105
106     recomputeBounds(true);
107     update();
108 }
109
110 FGRunwayRef AirportDiagram::selectedRunway() const
111 {
112     return m_selectedRunway;
113 }
114
115 void AirportDiagram::setSelectedRunway(FGRunwayRef r)
116 {
117     if (r == m_selectedRunway) {
118         return;
119     }
120     
121     m_selectedRunway = r;
122     update();
123 }
124
125 void AirportDiagram::setApproachExtensionDistance(double distanceNm)
126 {
127     m_approachDistanceNm = distanceNm;
128     recomputeBounds(true);
129     update();
130 }
131
132 void AirportDiagram::addRunway(FGRunwayRef rwy)
133 {
134     Q_FOREACH(RunwayData rd, m_runways) {
135         if (rd.runway == rwy->reciprocalRunway()) {
136             return; // only add one end of reciprocal runways
137         }
138     }
139
140     RunwayData r;
141     r.p1 = project(rwy->geod());
142     r.p2 = project(rwy->end());
143     r.widthM = qRound(rwy->widthM());
144     r.runway = rwy;
145     m_runways.append(r);
146
147     recomputeBounds(false);
148     update();
149 }
150
151 void AirportDiagram::doComputeBounds()
152 {
153     Q_FOREACH(const RunwayData& r, m_runways) {
154         extendBounds(r.p1);
155         extendBounds(r.p2);
156     }
157
158     Q_FOREACH(const TaxiwayData& t, m_taxiways) {
159         extendBounds(t.p1);
160         extendBounds(t.p2);
161     }
162
163     Q_FOREACH(const ParkingData& p, m_parking) {
164         extendBounds(p.pt);
165     }
166
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));
170         extendBounds(pt);
171     }
172 }
173
174 void AirportDiagram::addParking(FGParkingRef park)
175 {
176     ParkingData pd = { project(park->geod()), park };
177     m_parking.push_back(pd);
178     recomputeBounds(false);
179     update();
180 }
181
182
183 void AirportDiagram::paintContents(QPainter* p)
184 {
185       // fit bounds within our available space, allowing for a margin
186 //    const int MARGIN = 32; // pixels
187  //   double ratioInX = (width() - MARGIN * 2) / m_bounds.width();
188  //   double ratioInY = (height() - MARGIN * 2) / m_bounds.height();
189   //  double scale = std::min(ratioInX, ratioInY);
190
191     QTransform t(transform());
192     p->setTransform(t);
193
194 // pavements
195     QBrush brush(QColor(0x9f, 0x9f, 0x9f));
196     Q_FOREACH(const QPainterPath& path, m_pavements) {
197         p->drawPath(path);
198     }
199
200 // taxiways
201     Q_FOREACH(const TaxiwayData& t, m_taxiways) {
202         QPen pen(QColor(0x9f, 0x9f, 0x9f));
203         pen.setWidth(t.widthM);
204         p->setPen(pen);
205         p->drawLine(t.p1, t.p2);
206     }
207
208 // runways
209     QFont f;
210     f.setPixelSize(14);
211     p->setFont(f);
212
213     // draw ILS first so underneath all runways
214     QPen pen(Qt::magenta);
215     pen.setWidth(1.0 / m_scale);
216     p->setPen(pen);
217
218     Q_FOREACH(const RunwayData& r, m_runways) {
219         drawILS(p, r.runway);
220         drawILS(p, r.runway->reciprocalRunway());
221     }
222
223     // now draw the runways for real
224     Q_FOREACH(const RunwayData& r, m_runways) {
225
226         QColor color(Qt::magenta);
227         if ((r.runway == m_selectedRunway) || (r.runway->reciprocalRunway() == m_selectedRunway)) {
228             color = Qt::yellow;
229         }
230         
231         p->setTransform(t);
232
233         QPen pen(color);
234         pen.setWidth(r.widthM);
235         p->setPen(pen);
236         
237         p->drawLine(r.p1, r.p2);
238
239     // draw idents
240         QString ident = QString::fromStdString(r.runway->ident());
241
242         p->translate(r.p1);
243         p->rotate(r.runway->headingDeg());
244         // invert scaling factor so we can use screen pixel sizes here
245         p->scale(1.0 / m_scale, 1.0/ m_scale);
246         
247         p->setPen((r.runway == m_selectedRunway) ? Qt::yellow : Qt::magenta);
248         p->drawText(QRect(-100, 5, 200, 200), ident, Qt::AlignHCenter | Qt::AlignTop);
249
250         FGRunway* recip = r.runway->reciprocalRunway();
251         QString recipIdent = QString::fromStdString(recip->ident());
252
253         p->setTransform(t);
254         p->translate(r.p2);
255         p->rotate(recip->headingDeg());
256         p->scale(1.0 / m_scale, 1.0/ m_scale);
257
258         p->setPen((r.runway->reciprocalRunway() == m_selectedRunway) ? Qt::yellow : Qt::magenta);
259         p->drawText(QRect(-100, 5, 200, 200), recipIdent, Qt::AlignHCenter | Qt::AlignTop);
260     }
261
262     if (m_selectedRunway && (m_approachDistanceNm > 0.0)) {
263         p->setTransform(t);
264         // draw approach extension point
265         double d = SG_NM_TO_METER * m_approachDistanceNm;
266         QPointF pt = project(m_selectedRunway->pointOnCenterline(-d));
267         QPointF pt2 = project(m_selectedRunway->geod());
268         QPen pen(Qt::yellow);
269         pen.setWidth(2.0 / m_scale);
270         p->setPen(pen);
271         p->drawLine(pt, pt2);
272     }
273 }
274
275 void AirportDiagram::drawILS(QPainter* painter, FGRunwayRef runway) const
276 {
277     if (!runway)
278         return;
279
280     FGNavRecord* loc = runway->ILS();
281     if (!loc)
282         return;
283
284     double halfBeamWidth = loc->localizerWidth() * 0.5;
285     QPointF threshold = project(runway->threshold());
286     double rangeM = loc->get_range() * SG_NM_TO_METER;
287     double radial = loc->get_multiuse();
288     SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
289
290 // compute the three end points at the wide end of the arrow
291     QPointF endCentre = project(SGGeodesy::direct(loc->geod(), radial, -rangeM));
292     QPointF endR = project(SGGeodesy::direct(loc->geod(), radial + halfBeamWidth, -rangeM * 1.1));
293     QPointF endL = project(SGGeodesy::direct(loc->geod(), radial - halfBeamWidth, -rangeM * 1.1));
294
295     painter->drawLine(threshold, endCentre);
296     painter->drawLine(threshold, endL);
297     painter->drawLine(threshold, endR);
298     painter->drawLine(endL, endCentre);
299     painter->drawLine(endR, endCentre);
300 }
301
302 void AirportDiagram::mouseReleaseEvent(QMouseEvent* me)
303 {
304     if (m_didPan)
305         return; // ignore panning drag+release ops here
306
307     QTransform t(transform());
308     double minDist = std::numeric_limits<double>::max();
309     FGRunwayRef bestRunway;
310     
311     Q_FOREACH(const RunwayData& r, m_runways) {
312         QPointF p1(t.map(r.p1)), p2(t.map(r.p2));
313         double t;
314         double d = distanceToLineSegment(QVector2D(me->pos()),
315                                          QVector2D(p1),
316                                          QVector2D(p2), &t);
317         if (d < minDist) {
318             if (t > 0.5) {
319                 bestRunway = r.runway->reciprocalRunway();
320             } else {
321                 bestRunway = r.runway;
322             }
323             minDist = d;
324         }
325     }
326     
327     if (minDist < 16.0) {
328         emit clickedRunway(bestRunway);
329     }
330 }
331
332 void AirportDiagram::buildTaxiways()
333 {
334     m_taxiways.clear();
335     for (unsigned int tIndex=0; tIndex < m_airport->numTaxiways(); ++tIndex) {
336         FGTaxiwayRef tx = m_airport->getTaxiwayByIndex(tIndex);
337
338         TaxiwayData td;
339         td.p1 = project(tx->geod());
340         td.p2 = project(tx->pointOnCenterline(tx->lengthM()));
341
342         td.widthM = tx->widthM();
343         m_taxiways.append(td);
344     }
345 }
346
347 void AirportDiagram::buildPavements()
348 {
349     m_pavements.clear();
350     for (unsigned int pIndex=0; pIndex < m_airport->numPavements(); ++pIndex) {
351         FGPavementRef pave = m_airport->getPavementByIndex(pIndex);
352         if (pave->getNodeList().empty()) {
353             continue;
354         }
355
356         QPainterPath pp;
357         QPointF startPoint;
358         bool closed = true;
359         QPointF p0 = project(pave->getNodeList().front()->mPos);
360
361         FGPavement::NodeList::const_iterator it;
362         for (it = pave->getNodeList().begin(); it != pave->getNodeList().end(); ) {
363             const FGPavement::BezierNode *bn = dynamic_cast<const FGPavement::BezierNode *>(it->get());
364             bool close = (*it)->mClose;
365
366             // increment iterator so we can look at the next point
367             ++it;
368             QPointF nextPoint = (it == pave->getNodeList().end()) ? startPoint : project((*it)->mPos);
369
370             if (bn) {
371                 QPointF control = project(bn->mControl);
372                 QPointF endPoint = close ? startPoint : nextPoint;
373                 pp.quadTo(control, endPoint);
374             } else {
375                 // straight line segment
376                 if (closed) {
377                     pp.moveTo(p0);
378                     closed = false;
379                     startPoint = p0;
380                 } else
381                     pp.lineTo(p0);
382             }
383
384             if (close) {
385                 closed = true;
386                 pp.closeSubpath();
387                 startPoint = QPointF();
388             }
389
390             p0 = nextPoint;
391         } // of nodes iteration
392
393         if (!closed) {
394             pp.closeSubpath();
395         }
396
397         m_pavements.append(pp);
398     } // of pavements iteration
399 }