]> git.mxchange.org Git - flightgear.git/blob - src/GUI/AirportDiagram.cxx
Add missing GPL boilerplate.
[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 <QPainter>
24 #include <QDebug>
25
26 #include <Airports/airport.hxx>
27 #include <Airports/runways.hxx>
28 #include <Airports/parking.hxx>
29 #include <Airports/pavement.hxx>
30
31 /* equatorial and polar earth radius */
32 const float rec  = 6378137;          // earth radius, equator (?)
33 const float rpol = 6356752.314f;      // earth radius, polar   (?)
34
35 //Returns Earth radius at a given latitude (Ellipsoide equation with two equal axis)
36 static float earth_radius_lat( float lat )
37 {
38     double a = cos(lat)/rec;
39     double b = sin(lat)/rpol;
40     return 1.0f / sqrt( a * a + b * b );
41 }
42
43
44 AirportDiagram::AirportDiagram(QWidget* pr) :
45 QWidget(pr)
46 {
47     setSizePolicy(QSizePolicy::MinimumExpanding,
48                   QSizePolicy::MinimumExpanding);
49     setMinimumSize(100, 100);
50 }
51
52 void AirportDiagram::setAirport(FGAirportRef apt)
53 {
54     m_airport = apt;
55     m_projectionCenter = apt ? apt->geod() : SGGeod();
56     m_scale = 1.0;
57     m_bounds = QRectF(); // clear
58     m_runways.clear();
59
60     if (apt) {
61         buildTaxiways();
62         buildPavements();
63     }
64
65     update();
66 }
67
68 void AirportDiagram::addRunway(FGRunwayRef rwy)
69 {
70     Q_FOREACH(RunwayData rd, m_runways) {
71         if (rd.runway == rwy->reciprocalRunway()) {
72             return; // only add one end of reciprocal runways
73         }
74     }
75
76     QPointF p1 = project(rwy->geod()),
77     p2 = project(rwy->end());
78     extendBounds(p1);
79     extendBounds(p2);
80
81     RunwayData r;
82     r.p1 = p1;
83     r.p2 = p2;
84     r.widthM = qRound(rwy->widthM());
85     r.runway = rwy;
86     m_runways.append(r);
87     update();
88 }
89
90 void AirportDiagram::addParking(FGParking* park)
91 {
92     QPointF p = project(park->geod());
93     extendBounds(p);
94     update();
95 }
96
97 void AirportDiagram::paintEvent(QPaintEvent* pe)
98 {
99     QPainter p(this);
100     p.fillRect(rect(), QColor(0x3f, 0x3f, 0x3f));
101
102     // fit bounds within our available space, allowing for a margin
103     const int MARGIN = 32; // pixels
104     double ratioInX = (width() - MARGIN * 2) / m_bounds.width();
105     double ratioInY = (height() - MARGIN * 2) / m_bounds.height();
106     double scale = std::min(ratioInX, ratioInY);
107
108     QTransform t;
109     t.translate(width() / 2, height() / 2); // center projection origin in the widget
110     t.scale(scale, scale);
111     // center the bounding box (may not be at the origin)
112     t.translate(-m_bounds.center().x(), -m_bounds.center().y());
113     p.setTransform(t);
114
115 // pavements
116     QBrush brush(QColor(0x9f, 0x9f, 0x9f));
117     Q_FOREACH(const QPainterPath& path, m_pavements) {
118         p.drawPath(path);
119     }
120
121 // taxiways
122     Q_FOREACH(const TaxiwayData& t, m_taxiways) {
123         QPen pen(QColor(0x9f, 0x9f, 0x9f));
124         pen.setWidth(t.widthM);
125         p.setPen(pen);
126         p.drawLine(t.p1, t.p2);
127     }
128
129 // runways
130     QPen pen(Qt::magenta);
131     QFont f;
132     f.setPixelSize(14);
133     p.setFont(f);
134
135     Q_FOREACH(const RunwayData& r, m_runways) {
136         p.setTransform(t);
137
138         pen.setWidth(r.widthM);
139         p.setPen(pen);
140         p.drawLine(r.p1, r.p2);
141
142     // draw idents
143         QString ident = QString::fromStdString(r.runway->ident());
144
145         p.translate(r.p1);
146         p.rotate(r.runway->headingDeg());
147         // invert scaling factor so we can use screen pixel sizes here
148         p.scale(1.0 / scale, 1.0/ scale);
149
150         p.drawText(QRect(-100, 5, 200, 200), ident, Qt::AlignHCenter | Qt::AlignTop);
151
152         FGRunway* recip = r.runway->reciprocalRunway();
153         QString recipIdent = QString::fromStdString(recip->ident());
154
155         p.setTransform(t);
156         p.translate(r.p2);
157         p.rotate(recip->headingDeg());
158         p.scale(1.0 / scale, 1.0/ scale);
159
160         p.drawText(QRect(-100, 5, 200, 200), recipIdent, Qt::AlignHCenter | Qt::AlignTop);
161     }
162 }
163
164
165 void AirportDiagram::extendBounds(const QPointF& p)
166 {
167     if (p.x() < m_bounds.left()) {
168         m_bounds.setLeft(p.x());
169     } else if (p.x() > m_bounds.right()) {
170         m_bounds.setRight(p.x());
171     }
172
173     if (p.y() < m_bounds.top()) {
174         m_bounds.setTop(p.y());
175     } else if (p.y() > m_bounds.bottom()) {
176         m_bounds.setBottom(p.y());
177     }
178 }
179
180 QPointF AirportDiagram::project(const SGGeod& geod) const
181 {
182     double r = earth_radius_lat(geod.getLatitudeRad());
183     double ref_lat = m_projectionCenter.getLatitudeRad(),
184     ref_lon = m_projectionCenter.getLongitudeRad(),
185     lat = geod.getLatitudeRad(),
186     lon = geod.getLongitudeRad(),
187     lonDiff = lon - ref_lon;
188
189     double c = acos( sin(ref_lat) * sin(lat) + cos(ref_lat) * cos(lat) * cos(lonDiff) );
190     if (c == 0.0) {
191         // angular distance from center is 0
192         return QPointF(0.0, 0.0);
193     }
194
195     double k = c / sin(c);
196     double x, y;
197     if (ref_lat == (90 * SG_DEGREES_TO_RADIANS))
198     {
199         x = (SGD_PI / 2 - lat) * sin(lonDiff);
200         y = -(SGD_PI / 2 - lat) * cos(lonDiff);
201     }
202     else if (ref_lat == -(90 * SG_DEGREES_TO_RADIANS))
203     {
204         x = (SGD_PI / 2 + lat) * sin(lonDiff);
205         y = (SGD_PI / 2 + lat) * cos(lonDiff);
206     }
207     else
208     {
209         x = k * cos(lat) * sin(lonDiff);
210         y = k * ( cos(ref_lat) * sin(lat) - sin(ref_lat) * cos(lat) * cos(lonDiff) );
211     }
212
213     return QPointF(x, -y) * r * m_scale;
214 }
215
216 void AirportDiagram::buildTaxiways()
217 {
218     m_taxiways.clear();
219     for (unsigned int tIndex=0; tIndex < m_airport->numTaxiways(); ++tIndex) {
220         FGTaxiwayRef tx = m_airport->getTaxiwayByIndex(tIndex);
221
222         TaxiwayData td;
223         td.p1 = project(tx->geod());
224         td.p2 = project(tx->pointOnCenterline(tx->lengthM()));
225         extendBounds(td.p1);
226         extendBounds(td.p2);
227         td.widthM = tx->widthM();
228         m_taxiways.append(td);
229     }
230 }
231
232 void AirportDiagram::buildPavements()
233 {
234     m_pavements.clear();
235     for (unsigned int pIndex=0; pIndex < m_airport->numPavements(); ++pIndex) {
236         FGPavementRef pave = m_airport->getPavementByIndex(pIndex);
237         if (pave->getNodeList().empty()) {
238             continue;
239         }
240
241         QPainterPath pp;
242         QPointF startPoint;
243         bool closed = true;
244         QPointF p0 = project(pave->getNodeList().front()->mPos);
245
246         FGPavement::NodeList::const_iterator it;
247         for (it = pave->getNodeList().begin(); it != pave->getNodeList().end(); ) {
248             const FGPavement::BezierNode *bn = dynamic_cast<const FGPavement::BezierNode *>(it->get());
249             bool close = (*it)->mClose;
250
251             // increment iterator so we can look at the next point
252             ++it;
253             QPointF nextPoint = (it == pave->getNodeList().end()) ? startPoint : project((*it)->mPos);
254
255             if (bn) {
256                 QPointF control = project(bn->mControl);
257                 QPointF endPoint = close ? startPoint : nextPoint;
258                 pp.quadTo(control, endPoint);
259             } else {
260                 // straight line segment
261                 if (closed) {
262                     pp.moveTo(p0);
263                     closed = false;
264                     startPoint = p0;
265                 } else
266                     pp.lineTo(p0);
267             }
268
269             if (close) {
270                 closed = true;
271                 pp.closeSubpath();
272                 startPoint = QPointF();
273             }
274
275             p0 = nextPoint;
276         } // of nodes iteration
277
278         if (!closed) {
279             pp.closeSubpath();
280         }
281
282         m_pavements.append(pp);
283     } // of pavements iteration
284 }