]> git.mxchange.org Git - flightgear.git/blob - src/GUI/AirportDiagram.cxx
Navaid diagram work
[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     QTransform t = p->transform();
186
187 // pavements
188     QBrush brush(QColor(0x9f, 0x9f, 0x9f));
189     Q_FOREACH(const QPainterPath& path, m_pavements) {
190         p->drawPath(path);
191     }
192
193 // taxiways
194     Q_FOREACH(const TaxiwayData& t, m_taxiways) {
195         QPen pen(QColor(0x9f, 0x9f, 0x9f));
196         pen.setWidth(t.widthM);
197         p->setPen(pen);
198         p->drawLine(t.p1, t.p2);
199     }
200
201 // runways
202     QFont f;
203     f.setPixelSize(14);
204     p->setFont(f);
205
206     // draw ILS first so underneath all runways
207     QPen pen(Qt::magenta);
208     pen.setWidth(1.0 / m_scale);
209     p->setPen(pen);
210
211     Q_FOREACH(const RunwayData& r, m_runways) {
212         drawILS(p, r.runway);
213         drawILS(p, r.runway->reciprocalRunway());
214     }
215
216     // now draw the runways for real
217     Q_FOREACH(const RunwayData& r, m_runways) {
218
219         QColor color(Qt::magenta);
220         if ((r.runway == m_selectedRunway) || (r.runway->reciprocalRunway() == m_selectedRunway)) {
221             color = Qt::yellow;
222         }
223         
224         p->setTransform(t);
225
226         QPen pen(color);
227         pen.setWidth(r.widthM);
228         p->setPen(pen);
229         
230         p->drawLine(r.p1, r.p2);
231
232     // draw idents
233         QString ident = QString::fromStdString(r.runway->ident());
234
235         p->translate(r.p1);
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);
239         
240         p->setPen((r.runway == m_selectedRunway) ? Qt::yellow : Qt::magenta);
241         p->drawText(QRect(-100, 5, 200, 200), ident, Qt::AlignHCenter | Qt::AlignTop);
242
243         FGRunway* recip = r.runway->reciprocalRunway();
244         QString recipIdent = QString::fromStdString(recip->ident());
245
246         p->setTransform(t);
247         p->translate(r.p2);
248         p->rotate(recip->headingDeg());
249         p->scale(1.0 / m_scale, 1.0/ m_scale);
250
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);
253     }
254
255     if (m_selectedRunway && (m_approachDistanceNm > 0.0)) {
256         p->setTransform(t);
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);
263         p->setPen(pen);
264         p->drawLine(pt, pt2);
265     }
266 }
267
268 void AirportDiagram::drawILS(QPainter* painter, FGRunwayRef runway) const
269 {
270     if (!runway)
271         return;
272
273     FGNavRecord* loc = runway->ILS();
274     if (!loc)
275         return;
276
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);
282
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));
287
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);
293 }
294
295 void AirportDiagram::mouseReleaseEvent(QMouseEvent* me)
296 {
297     if (m_didPan)
298         return; // ignore panning drag+release ops here
299
300     QTransform t(transform());
301     double minDist = std::numeric_limits<double>::max();
302     FGRunwayRef bestRunway;
303     
304     Q_FOREACH(const RunwayData& r, m_runways) {
305         QPointF p1(t.map(r.p1)), p2(t.map(r.p2));
306         double t;
307         double d = distanceToLineSegment(QVector2D(me->pos()),
308                                          QVector2D(p1),
309                                          QVector2D(p2), &t);
310         if (d < minDist) {
311             if (t > 0.5) {
312                 bestRunway = r.runway->reciprocalRunway();
313             } else {
314                 bestRunway = r.runway;
315             }
316             minDist = d;
317         }
318     }
319     
320     if (minDist < 16.0) {
321         emit clickedRunway(bestRunway);
322     }
323 }
324
325 void AirportDiagram::buildTaxiways()
326 {
327     m_taxiways.clear();
328     for (unsigned int tIndex=0; tIndex < m_airport->numTaxiways(); ++tIndex) {
329         FGTaxiwayRef tx = m_airport->getTaxiwayByIndex(tIndex);
330
331         TaxiwayData td;
332         td.p1 = project(tx->geod());
333         td.p2 = project(tx->pointOnCenterline(tx->lengthM()));
334
335         td.widthM = tx->widthM();
336         m_taxiways.append(td);
337     }
338 }
339
340 void AirportDiagram::buildPavements()
341 {
342     m_pavements.clear();
343     for (unsigned int pIndex=0; pIndex < m_airport->numPavements(); ++pIndex) {
344         FGPavementRef pave = m_airport->getPavementByIndex(pIndex);
345         if (pave->getNodeList().empty()) {
346             continue;
347         }
348
349         QPainterPath pp;
350         QPointF startPoint;
351         bool closed = true;
352         QPointF p0 = project(pave->getNodeList().front()->mPos);
353
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;
358
359             // increment iterator so we can look at the next point
360             ++it;
361             QPointF nextPoint = (it == pave->getNodeList().end()) ? startPoint : project((*it)->mPos);
362
363             if (bn) {
364                 QPointF control = project(bn->mControl);
365                 QPointF endPoint = close ? startPoint : nextPoint;
366                 pp.quadTo(control, endPoint);
367             } else {
368                 // straight line segment
369                 if (closed) {
370                     pp.moveTo(p0);
371                     closed = false;
372                     startPoint = p0;
373                 } else
374                     pp.lineTo(p0);
375             }
376
377             if (close) {
378                 closed = true;
379                 pp.closeSubpath();
380                 startPoint = QPointF();
381             }
382
383             p0 = nextPoint;
384         } // of nodes iteration
385
386         if (!closed) {
387             pp.closeSubpath();
388         }
389
390         m_pavements.append(pp);
391     } // of pavements iteration
392 }