Marble

GnomonicProjection.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
4//
5
6// Local
7#include "GnomonicProjection.h"
8
9// Marble
10#include "AzimuthalProjection_p.h"
11#include "GeoDataCoordinates.h"
12#include "GeoDataLineString.h"
13#include "MarbleGlobal.h"
14#include "ViewportParams.h"
15
16#include <QIcon>
17#include <qmath.h>
18
19#define SAFE_DISTANCE
20
21namespace Marble
22{
23
24class GnomonicProjectionPrivate : public AzimuthalProjectionPrivate
25{
26public:
27 explicit GnomonicProjectionPrivate(GnomonicProjection *parent);
28
29 Q_DECLARE_PUBLIC(GnomonicProjection)
30};
31
33 : AzimuthalProjection(new GnomonicProjectionPrivate(this))
34{
35 setMinLat(minValidLat());
36 setMaxLat(maxValidLat());
37}
38
39GnomonicProjection::GnomonicProjection(GnomonicProjectionPrivate *dd)
41{
42 setMinLat(minValidLat());
43 setMaxLat(maxValidLat());
44}
45
46GnomonicProjection::~GnomonicProjection() = default;
47
48GnomonicProjectionPrivate::GnomonicProjectionPrivate(GnomonicProjection *parent)
49 : AzimuthalProjectionPrivate(parent)
50{
51}
52
54{
55 return QObject::tr("Gnomonic");
56}
57
59{
60 return QObject::tr(
61 "<p><b>Gnomonic Projection</b> (\"rectilinear\")</p><p>Applications: Used for displaying panorama photography. Also used for navigation, radio and "
62 "seismic work.</p>");
63}
64
66{
67 return QIcon(QStringLiteral(":/icons/map-gnomonic.png"));
68}
69
70qreal GnomonicProjection::clippingRadius() const
71{
72 return 1;
73}
74
76 const ViewportParams *viewport,
77 qreal &x,
78 qreal &y,
79 bool &globeHidesPoint) const
80{
81 const qreal lambda = coordinates.longitude();
82 const qreal phi = coordinates.latitude();
83 const qreal lambdaPrime = viewport->centerLongitude();
84 const qreal phi1 = viewport->centerLatitude();
85
86 qreal cosC = qSin(phi1) * qSin(phi) + qCos(phi1) * qCos(phi) * qCos(lambda - lambdaPrime);
87
88 if (cosC <= 0) {
89 globeHidesPoint = true;
90 return false;
91 }
92
93 // Let (x, y) be the position on the screen of the placemark..
94 x = (qCos(phi) * qSin(lambda - lambdaPrime)) / cosC;
95 y = (qCos(phi1) * qSin(phi) - qSin(phi1) * qCos(phi) * qCos(lambda - lambdaPrime)) / cosC;
96
97 x *= viewport->radius() / 2;
98 y *= viewport->radius() / 2;
99
100 const qint64 radius = clippingRadius() * viewport->radius();
101
102 if (x * x + y * y > radius * radius) {
103 globeHidesPoint = true;
104 return false;
105 }
106
107 globeHidesPoint = false;
108
109 x += viewport->width() / 2;
110 y = viewport->height() / 2 - y;
111
112 // Skip placemarks that are outside the screen area
113 return !(x < 0 || x >= viewport->width() || y < 0 || y >= viewport->height());
114}
115
117 const ViewportParams *viewport,
118 qreal *x,
119 qreal &y,
120 int &pointRepeatNum,
121 const QSizeF &size,
122 bool &globeHidesPoint) const
123{
124 pointRepeatNum = 0;
125 globeHidesPoint = false;
126
127 bool visible = screenCoordinates(coordinates, viewport, *x, y, globeHidesPoint);
128
129 // Skip placemarks that are outside the screen area
130 if (*x + size.width() / 2.0 < 0.0 || *x >= viewport->width() + size.width() / 2.0 || y + size.height() / 2.0 < 0.0
131 || y >= viewport->height() + size.height() / 2.0) {
132 return false;
133 }
134
135 // This projection doesn't have any repetitions,
136 // so the number of screen points referring to the geopoint is one.
137 pointRepeatNum = 1;
138 return visible;
139}
140
141bool GnomonicProjection::geoCoordinates(const int x, const int y, const ViewportParams *viewport, qreal &lon, qreal &lat, GeoDataCoordinates::Unit unit) const
142{
143 const qint64 radius = viewport->radius();
144 // Calculate how many degrees are being represented per pixel.
145 const qreal centerLon = viewport->centerLongitude();
146 const qreal centerLat = viewport->centerLatitude();
147 const qreal rx = (-viewport->width() / 2 + x);
148 const qreal ry = (viewport->height() / 2 - y);
149 const qreal p = qMax(qSqrt(rx * rx + ry * ry), qreal(0.0001)); // ensure we don't divide by zero
150 const qreal c = qAtan(2 * p / radius);
151 const qreal sinc = qSin(c);
152
153 lon = centerLon + qAtan2(rx * sinc, (p * qCos(centerLat) * qCos(c) - ry * qSin(centerLat) * sinc));
154
155 while (lon < -M_PI)
156 lon += 2 * M_PI;
157 while (lon > M_PI)
158 lon -= 2 * M_PI;
159
160 lat = qAsin(qCos(c) * qSin(centerLat) + ry * sinc * qCos(centerLat) / p);
161
162 if (unit == GeoDataCoordinates::Degree) {
163 lon *= RAD2DEG;
164 lat *= RAD2DEG;
165 }
166
167 return true;
168}
169
170}
This file contains the headers for ViewportParams.
virtual qreal maxValidLat() const
Returns the maximum (northern) latitude that is mathematically defined and reasonable.
virtual qreal minValidLat() const
Returns the minimum (southern) latitude that is mathematically defined and reasonable.
A base class for the Gnomonic and Orthographic (Globe) projections in Marble.
A 3d point representation.
Unit
enum used constructor to specify the units used
bool screenCoordinates(const GeoDataCoordinates &coordinates, const ViewportParams *params, qreal &x, qreal &y, bool &globeHidesPoint) const override
Get the screen coordinates corresponding to geographical coordinates in the map.
bool geoCoordinates(const int x, const int y, const ViewportParams *params, qreal &lon, qreal &lat, GeoDataCoordinates::Unit unit=GeoDataCoordinates::Degree) const override
Get the earth coordinates corresponding to a pixel in the map.
QString name() const override
Returns the user-visible name of the projection.
GnomonicProjection()
Construct a new GnomonicProjection.
QString description() const override
Returns a short user description of the projection that can be used in tooltips or dialogs.
QIcon icon() const override
Returns an icon for the projection.
A public class that controls what is visible in the viewport of a Marble map.
Binds a QML item to a specific geodetic location in screen coordinates.
QString tr(const char *sourceText, const char *disambiguation, int n)
qreal height() const const
qreal width() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:37:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.