1 #include "AirportDiagram.hxx"
6 #include <Airports/airport.hxx>
7 #include <Airports/runways.hxx>
8 #include <Airports/parking.hxx>
9 #include <Airports/pavement.hxx>
11 /* equatorial and polar earth radius */
12 const float rec = 6378137; // earth radius, equator (?)
13 const float rpol = 6356752.314f; // earth radius, polar (?)
15 //Returns Earth radius at a given latitude (Ellipsoide equation with two equal axis)
16 static float earth_radius_lat( float lat )
18 double a = cos(lat)/rec;
19 double b = sin(lat)/rpol;
20 return 1.0f / sqrt( a * a + b * b );
24 AirportDiagram::AirportDiagram(QWidget* pr) :
27 setSizePolicy(QSizePolicy::MinimumExpanding,
28 QSizePolicy::MinimumExpanding);
29 setMinimumSize(100, 100);
32 void AirportDiagram::setAirport(FGAirportRef apt)
35 m_projectionCenter = apt ? apt->geod() : SGGeod();
37 m_bounds = QRectF(); // clear
48 void AirportDiagram::addRunway(FGRunwayRef rwy)
50 Q_FOREACH(RunwayData rd, m_runways) {
51 if (rd.runway == rwy->reciprocalRunway()) {
52 return; // only add one end of reciprocal runways
56 QPointF p1 = project(rwy->geod()),
57 p2 = project(rwy->end());
64 r.widthM = qRound(rwy->widthM());
70 void AirportDiagram::addParking(FGParking* park)
72 QPointF p = project(park->geod());
77 void AirportDiagram::paintEvent(QPaintEvent* pe)
80 p.fillRect(rect(), QColor(0x3f, 0x3f, 0x3f));
82 // fit bounds within our available space, allowing for a margin
83 const int MARGIN = 32; // pixels
84 double ratioInX = (width() - MARGIN * 2) / m_bounds.width();
85 double ratioInY = (height() - MARGIN * 2) / m_bounds.height();
86 double scale = std::min(ratioInX, ratioInY);
89 t.translate(width() / 2, height() / 2); // center projection origin in the widget
90 t.scale(scale, scale);
91 // center the bounding box (may not be at the origin)
92 t.translate(-m_bounds.center().x(), -m_bounds.center().y());
96 QBrush brush(QColor(0x9f, 0x9f, 0x9f));
97 Q_FOREACH(const QPainterPath& path, m_pavements) {
102 Q_FOREACH(const TaxiwayData& t, m_taxiways) {
103 QPen pen(QColor(0x9f, 0x9f, 0x9f));
104 pen.setWidth(t.widthM);
106 p.drawLine(t.p1, t.p2);
110 QPen pen(Qt::magenta);
115 Q_FOREACH(const RunwayData& r, m_runways) {
118 pen.setWidth(r.widthM);
120 p.drawLine(r.p1, r.p2);
123 QString ident = QString::fromStdString(r.runway->ident());
126 p.rotate(r.runway->headingDeg());
127 // invert scaling factor so we can use screen pixel sizes here
128 p.scale(1.0 / scale, 1.0/ scale);
130 p.drawText(QRect(-100, 5, 200, 200), ident, Qt::AlignHCenter | Qt::AlignTop);
132 FGRunway* recip = r.runway->reciprocalRunway();
133 QString recipIdent = QString::fromStdString(recip->ident());
137 p.rotate(recip->headingDeg());
138 p.scale(1.0 / scale, 1.0/ scale);
140 p.drawText(QRect(-100, 5, 200, 200), recipIdent, Qt::AlignHCenter | Qt::AlignTop);
145 void AirportDiagram::extendBounds(const QPointF& p)
147 if (p.x() < m_bounds.left()) {
148 m_bounds.setLeft(p.x());
149 } else if (p.x() > m_bounds.right()) {
150 m_bounds.setRight(p.x());
153 if (p.y() < m_bounds.top()) {
154 m_bounds.setTop(p.y());
155 } else if (p.y() > m_bounds.bottom()) {
156 m_bounds.setBottom(p.y());
160 QPointF AirportDiagram::project(const SGGeod& geod) const
162 double r = earth_radius_lat(geod.getLatitudeRad());
163 double ref_lat = m_projectionCenter.getLatitudeRad(),
164 ref_lon = m_projectionCenter.getLongitudeRad(),
165 lat = geod.getLatitudeRad(),
166 lon = geod.getLongitudeRad(),
167 lonDiff = lon - ref_lon;
169 double c = acos( sin(ref_lat) * sin(lat) + cos(ref_lat) * cos(lat) * cos(lonDiff) );
171 // angular distance from center is 0
172 return QPointF(0.0, 0.0);
175 double k = c / sin(c);
177 if (ref_lat == (90 * SG_DEGREES_TO_RADIANS))
179 x = (SGD_PI / 2 - lat) * sin(lonDiff);
180 y = -(SGD_PI / 2 - lat) * cos(lonDiff);
182 else if (ref_lat == -(90 * SG_DEGREES_TO_RADIANS))
184 x = (SGD_PI / 2 + lat) * sin(lonDiff);
185 y = (SGD_PI / 2 + lat) * cos(lonDiff);
189 x = k * cos(lat) * sin(lonDiff);
190 y = k * ( cos(ref_lat) * sin(lat) - sin(ref_lat) * cos(lat) * cos(lonDiff) );
193 return QPointF(x, -y) * r * m_scale;
196 void AirportDiagram::buildTaxiways()
199 for (unsigned int tIndex=0; tIndex < m_airport->numTaxiways(); ++tIndex) {
200 FGTaxiwayRef tx = m_airport->getTaxiwayByIndex(tIndex);
203 td.p1 = project(tx->geod());
204 td.p2 = project(tx->pointOnCenterline(tx->lengthM()));
207 td.widthM = tx->widthM();
208 m_taxiways.append(td);
212 void AirportDiagram::buildPavements()
215 for (unsigned int pIndex=0; pIndex < m_airport->numPavements(); ++pIndex) {
216 FGPavementRef pave = m_airport->getPavementByIndex(pIndex);
217 if (pave->getNodeList().empty()) {
224 QPointF p0 = project(pave->getNodeList().front()->mPos);
226 FGPavement::NodeList::const_iterator it;
227 for (it = pave->getNodeList().begin(); it != pave->getNodeList().end(); ) {
228 const FGPavement::BezierNode *bn = dynamic_cast<const FGPavement::BezierNode *>(it->get());
229 bool close = (*it)->mClose;
231 // increment iterator so we can look at the next point
233 QPointF nextPoint = (it == pave->getNodeList().end()) ? startPoint : project((*it)->mPos);
236 QPointF control = project(bn->mControl);
237 QPointF endPoint = close ? startPoint : nextPoint;
238 pp.quadTo(control, endPoint);
240 // straight line segment
252 startPoint = QPointF();
256 } // of nodes iteration
262 m_pavements.append(pp);
263 } // of pavements iteration