]> git.mxchange.org Git - flightgear.git/blob - src/GUI/AirportDiagram.cxx
Work on launcher diagrams.
[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     clearIgnoredNavaids();
107     addIgnoredNavaid(apt);
108
109     recomputeBounds(true);
110     update();
111 }
112
113 FGRunwayRef AirportDiagram::selectedRunway() const
114 {
115     return m_selectedRunway;
116 }
117
118 void AirportDiagram::setSelectedRunway(FGRunwayRef r)
119 {
120     if (r == m_selectedRunway) {
121         return;
122     }
123     
124     m_selectedRunway = r;
125     update();
126 }
127
128 void AirportDiagram::setApproachExtensionDistance(double distanceNm)
129 {
130     m_approachDistanceNm = distanceNm;
131     recomputeBounds(true);
132     update();
133 }
134
135 void AirportDiagram::addRunway(FGRunwayRef rwy)
136 {
137     Q_FOREACH(RunwayData rd, m_runways) {
138         if (rd.runway == rwy->reciprocalRunway()) {
139             return; // only add one end of reciprocal runways
140         }
141     }
142
143     RunwayData r;
144     r.p1 = project(rwy->geod());
145     r.p2 = project(rwy->end());
146     r.widthM = qRound(rwy->widthM());
147     r.runway = rwy;
148     m_runways.append(r);
149
150     recomputeBounds(false);
151     update();
152 }
153
154 void AirportDiagram::doComputeBounds()
155 {
156     Q_FOREACH(const RunwayData& r, m_runways) {
157         extendBounds(r.p1);
158         extendBounds(r.p2);
159     }
160
161     Q_FOREACH(const TaxiwayData& t, m_taxiways) {
162         extendBounds(t.p1);
163         extendBounds(t.p2);
164     }
165
166     Q_FOREACH(const ParkingData& p, m_parking) {
167         extendBounds(p.pt);
168     }
169
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));
173         extendBounds(pt);
174     }
175 }
176
177 void AirportDiagram::addParking(FGParkingRef park)
178 {
179     ParkingData pd = { project(park->geod()), park };
180     m_parking.push_back(pd);
181     recomputeBounds(false);
182     update();
183 }
184
185
186 void AirportDiagram::paintContents(QPainter* p)
187 {
188     QTransform t = p->transform();
189
190 // pavements
191     QBrush brush(QColor(0x9f, 0x9f, 0x9f));
192     Q_FOREACH(const QPainterPath& path, m_pavements) {
193         p->drawPath(path);
194     }
195
196 // taxiways
197     Q_FOREACH(const TaxiwayData& t, m_taxiways) {
198         QPen pen(QColor(0x9f, 0x9f, 0x9f));
199         pen.setWidth(t.widthM);
200         p->setPen(pen);
201         p->drawLine(t.p1, t.p2);
202     }
203
204 // runways
205     QFont f;
206     f.setPixelSize(14);
207     p->setFont(f);
208
209     // draw ILS first so underneath all runways
210     QPen pen(QColor(0x5f, 0x5f, 0x5f));
211     pen.setWidth(1);
212     pen.setCosmetic(true);
213     p->setPen(pen);
214
215     Q_FOREACH(const RunwayData& r, m_runways) {
216         drawILS(p, r.runway);
217         drawILS(p, r.runway->reciprocalRunway());
218     }
219
220     // now draw the runways for real
221     Q_FOREACH(const RunwayData& r, m_runways) {
222
223         QColor color(Qt::magenta);
224         if ((r.runway == m_selectedRunway) || (r.runway->reciprocalRunway() == m_selectedRunway)) {
225             color = Qt::yellow;
226         }
227         
228         p->setTransform(t);
229
230         QPen pen(color);
231         pen.setWidth(r.widthM);
232         p->setPen(pen);
233         
234         p->drawLine(r.p1, r.p2);
235
236     // draw idents
237         QString ident = QString::fromStdString(r.runway->ident());
238
239         p->translate(r.p1);
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);
243         
244         p->setPen((r.runway == m_selectedRunway) ? Qt::yellow : Qt::magenta);
245         p->drawText(QRect(-100, 5, 200, 200), ident, Qt::AlignHCenter | Qt::AlignTop);
246
247         FGRunway* recip = r.runway->reciprocalRunway();
248         QString recipIdent = QString::fromStdString(recip->ident());
249
250         p->setTransform(t);
251         p->translate(r.p2);
252         p->rotate(recip->headingDeg());
253         p->scale(1.0 / m_scale, 1.0/ m_scale);
254
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);
257     }
258
259     if (m_selectedRunway && (m_approachDistanceNm > 0.0)) {
260         p->setTransform(t);
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);
267         p->setPen(pen);
268         p->drawLine(pt, pt2);
269     }
270 }
271
272 void AirportDiagram::drawILS(QPainter* painter, FGRunwayRef runway) const
273 {
274     if (!runway)
275         return;
276
277     FGNavRecord* loc = runway->ILS();
278     if (!loc)
279         return;
280
281     double halfBeamWidth = loc->localizerWidth() * 0.5;
282     QPointF threshold = project(runway->threshold());
283     double rangeM = loc->get_range() * SG_NM_TO_METER;
284     double radial = loc->get_multiuse();
285     SG_NORMALIZE_RANGE(radial, 0.0, 360.0);
286
287 // compute the three end points at the wide end of the arrow
288     QPointF endCentre = project(SGGeodesy::direct(loc->geod(), radial, -rangeM));
289     QPointF endR = project(SGGeodesy::direct(loc->geod(), radial + halfBeamWidth, -rangeM * 1.1));
290     QPointF endL = project(SGGeodesy::direct(loc->geod(), radial - halfBeamWidth, -rangeM * 1.1));
291
292     painter->drawLine(threshold, endCentre);
293     painter->drawLine(threshold, endL);
294     painter->drawLine(threshold, endR);
295     painter->drawLine(endL, endCentre);
296     painter->drawLine(endR, endCentre);
297 }
298
299 void AirportDiagram::mouseReleaseEvent(QMouseEvent* me)
300 {
301     if (m_didPan)
302         return; // ignore panning drag+release ops here
303
304     QTransform t(transform());
305     double minDist = std::numeric_limits<double>::max();
306     FGRunwayRef bestRunway;
307     
308     Q_FOREACH(const RunwayData& r, m_runways) {
309         QPointF p1(t.map(r.p1)), p2(t.map(r.p2));
310         double t;
311         double d = distanceToLineSegment(QVector2D(me->pos()),
312                                          QVector2D(p1),
313                                          QVector2D(p2), &t);
314         if (d < minDist) {
315             if (t > 0.5) {
316                 bestRunway = r.runway->reciprocalRunway();
317             } else {
318                 bestRunway = r.runway;
319             }
320             minDist = d;
321         }
322     }
323     
324     if (minDist < 16.0) {
325         emit clickedRunway(bestRunway);
326     }
327 }
328
329 void AirportDiagram::buildTaxiways()
330 {
331     m_taxiways.clear();
332     for (unsigned int tIndex=0; tIndex < m_airport->numTaxiways(); ++tIndex) {
333         FGTaxiwayRef tx = m_airport->getTaxiwayByIndex(tIndex);
334
335         TaxiwayData td;
336         td.p1 = project(tx->geod());
337         td.p2 = project(tx->pointOnCenterline(tx->lengthM()));
338
339         td.widthM = tx->widthM();
340         m_taxiways.append(td);
341     }
342 }
343
344 void AirportDiagram::buildPavements()
345 {
346     m_pavements.clear();
347     for (unsigned int pIndex=0; pIndex < m_airport->numPavements(); ++pIndex) {
348         FGPavementRef pave = m_airport->getPavementByIndex(pIndex);
349         if (pave->getNodeList().empty()) {
350             continue;
351         }
352
353         QPainterPath pp;
354         QPointF startPoint;
355         bool closed = true;
356         QPointF p0 = project(pave->getNodeList().front()->mPos);
357
358         FGPavement::NodeList::const_iterator it;
359         for (it = pave->getNodeList().begin(); it != pave->getNodeList().end(); ) {
360             const FGPavement::BezierNode *bn = dynamic_cast<const FGPavement::BezierNode *>(it->get());
361             bool close = (*it)->mClose;
362
363             // increment iterator so we can look at the next point
364             ++it;
365             QPointF nextPoint = (it == pave->getNodeList().end()) ? startPoint : project((*it)->mPos);
366
367             if (bn) {
368                 QPointF control = project(bn->mControl);
369                 QPointF endPoint = close ? startPoint : nextPoint;
370                 pp.quadTo(control, endPoint);
371             } else {
372                 // straight line segment
373                 if (closed) {
374                     pp.moveTo(p0);
375                     closed = false;
376                     startPoint = p0;
377                 } else
378                     pp.lineTo(p0);
379             }
380
381             if (close) {
382                 closed = true;
383                 pp.closeSubpath();
384                 startPoint = QPointF();
385             }
386
387             p0 = nextPoint;
388         } // of nodes iteration
389
390         if (!closed) {
391             pp.closeSubpath();
392         }
393
394         m_pavements.append(pp);
395     } // of pavements iteration
396 }