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"
26 #include <Airports/airport.hxx>
27 #include <Airports/runways.hxx>
28 #include <Airports/parking.hxx>
29 #include <Airports/pavement.hxx>
31 /* equatorial and polar earth radius */
32 const float rec = 6378137; // earth radius, equator (?)
33 const float rpol = 6356752.314f; // earth radius, polar (?)
35 //Returns Earth radius at a given latitude (Ellipsoide equation with two equal axis)
36 static float earth_radius_lat( float lat )
38 double a = cos(lat)/rec;
39 double b = sin(lat)/rpol;
40 return 1.0f / sqrt( a * a + b * b );
44 AirportDiagram::AirportDiagram(QWidget* pr) :
47 setSizePolicy(QSizePolicy::MinimumExpanding,
48 QSizePolicy::MinimumExpanding);
49 setMinimumSize(100, 100);
52 void AirportDiagram::setAirport(FGAirportRef apt)
55 m_projectionCenter = apt ? apt->geod() : SGGeod();
57 m_bounds = QRectF(); // clear
68 void AirportDiagram::addRunway(FGRunwayRef rwy)
70 Q_FOREACH(RunwayData rd, m_runways) {
71 if (rd.runway == rwy->reciprocalRunway()) {
72 return; // only add one end of reciprocal runways
76 QPointF p1 = project(rwy->geod()),
77 p2 = project(rwy->end());
84 r.widthM = qRound(rwy->widthM());
90 void AirportDiagram::addParking(FGParking* park)
92 QPointF p = project(park->geod());
97 void AirportDiagram::paintEvent(QPaintEvent* pe)
100 p.fillRect(rect(), QColor(0x3f, 0x3f, 0x3f));
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);
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());
116 QBrush brush(QColor(0x9f, 0x9f, 0x9f));
117 Q_FOREACH(const QPainterPath& path, m_pavements) {
122 Q_FOREACH(const TaxiwayData& t, m_taxiways) {
123 QPen pen(QColor(0x9f, 0x9f, 0x9f));
124 pen.setWidth(t.widthM);
126 p.drawLine(t.p1, t.p2);
130 QPen pen(Qt::magenta);
135 Q_FOREACH(const RunwayData& r, m_runways) {
138 pen.setWidth(r.widthM);
140 p.drawLine(r.p1, r.p2);
143 QString ident = QString::fromStdString(r.runway->ident());
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);
150 p.drawText(QRect(-100, 5, 200, 200), ident, Qt::AlignHCenter | Qt::AlignTop);
152 FGRunway* recip = r.runway->reciprocalRunway();
153 QString recipIdent = QString::fromStdString(recip->ident());
157 p.rotate(recip->headingDeg());
158 p.scale(1.0 / scale, 1.0/ scale);
160 p.drawText(QRect(-100, 5, 200, 200), recipIdent, Qt::AlignHCenter | Qt::AlignTop);
165 void AirportDiagram::extendBounds(const QPointF& p)
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());
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());
180 QPointF AirportDiagram::project(const SGGeod& geod) const
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;
189 double c = acos( sin(ref_lat) * sin(lat) + cos(ref_lat) * cos(lat) * cos(lonDiff) );
191 // angular distance from center is 0
192 return QPointF(0.0, 0.0);
195 double k = c / sin(c);
197 if (ref_lat == (90 * SG_DEGREES_TO_RADIANS))
199 x = (SGD_PI / 2 - lat) * sin(lonDiff);
200 y = -(SGD_PI / 2 - lat) * cos(lonDiff);
202 else if (ref_lat == -(90 * SG_DEGREES_TO_RADIANS))
204 x = (SGD_PI / 2 + lat) * sin(lonDiff);
205 y = (SGD_PI / 2 + lat) * cos(lonDiff);
209 x = k * cos(lat) * sin(lonDiff);
210 y = k * ( cos(ref_lat) * sin(lat) - sin(ref_lat) * cos(lat) * cos(lonDiff) );
213 return QPointF(x, -y) * r * m_scale;
216 void AirportDiagram::buildTaxiways()
219 for (unsigned int tIndex=0; tIndex < m_airport->numTaxiways(); ++tIndex) {
220 FGTaxiwayRef tx = m_airport->getTaxiwayByIndex(tIndex);
223 td.p1 = project(tx->geod());
224 td.p2 = project(tx->pointOnCenterline(tx->lengthM()));
227 td.widthM = tx->widthM();
228 m_taxiways.append(td);
232 void AirportDiagram::buildPavements()
235 for (unsigned int pIndex=0; pIndex < m_airport->numPavements(); ++pIndex) {
236 FGPavementRef pave = m_airport->getPavementByIndex(pIndex);
237 if (pave->getNodeList().empty()) {
244 QPointF p0 = project(pave->getNodeList().front()->mPos);
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;
251 // increment iterator so we can look at the next point
253 QPointF nextPoint = (it == pave->getNodeList().end()) ? startPoint : project((*it)->mPos);
256 QPointF control = project(bn->mControl);
257 QPointF endPoint = close ? startPoint : nextPoint;
258 pp.quadTo(control, endPoint);
260 // straight line segment
272 startPoint = QPointF();
276 } // of nodes iteration
282 m_pavements.append(pp);
283 } // of pavements iteration