1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2023-05-15
 * Description : geolocation engine based on Marble.
 *               (c) 2007-2022 Marble Team
 *               https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
 *
 * SPDX-FileCopyrightText: 2023-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * ============================================================ */

#include "SphericalProjection.h"
#include "AbstractProjection_p.h"

// Qt includes

#include <QIcon>

// KDE includes

#include <klocalizedstring.h>

// Local includes

#include "ViewportParams.h"
#include "GeoDataPoint.h"
#include "GeoDataLineString.h"
#include "GeoDataCoordinates.h"
#include "MarbleGlobal.h"
#include "AzimuthalProjection_p.h"
#include "digikam_debug.h"

#define SAFE_DISTANCE

namespace Marble
{

class Q_DECL_HIDDEN SphericalProjectionPrivate : public AzimuthalProjectionPrivate
{
public:

    explicit SphericalProjectionPrivate(SphericalProjection* parent);

    Q_DECLARE_PUBLIC(SphericalProjection)<--- Derived function 'SphericalProjectionPrivate::d_func'<--- Derived function 'SphericalProjectionPrivate::d_func'
};

SphericalProjection::SphericalProjection()
    : AzimuthalProjection(new SphericalProjectionPrivate(this))
{
    setMinLat(minValidLat());
    setMaxLat(maxValidLat());
}

SphericalProjection::SphericalProjection(SphericalProjectionPrivate* dd)
    : AzimuthalProjection(dd)
{
    setMinLat(minValidLat());
    setMaxLat(maxValidLat());
}

SphericalProjection::~SphericalProjection()
{
}

SphericalProjectionPrivate::SphericalProjectionPrivate(SphericalProjection* parent)
    : AzimuthalProjectionPrivate(parent)
{
}

QString SphericalProjection::name() const
{
    return i18n("Globe");
}

QString SphericalProjection::description() const
{
    return i18n("<p><b>Orthographic Projection</b> (\"orthogonal\")</p><p>Applications: "
                "A perspective projection that is used to display the hemisphere of a "
                "globe as it appears from outer space.</p>");
}

QIcon SphericalProjection::icon() const
{
    return QIcon::fromTheme(QStringLiteral("map-globe"));
}

bool SphericalProjection::screenCoordinates(const GeoDataCoordinates& coordinates,
                                            const ViewportParams* viewport,
                                            qreal& x, qreal& y, bool& globeHidesPoint) const
{
    const qreal altitude = coordinates.altitude();
    const qreal absoluteAltitude = altitude + EARTH_RADIUS;
    Quaternion  qpos             = coordinates.quaternion();

    qpos.rotateAroundAxis(viewport->planetAxisMatrix());

    const qreal radius = viewport->radius();
    const qreal pixelAltitude = (radius / EARTH_RADIUS * absoluteAltitude);

    if (altitude < 10000)
    {
        // Skip placemarks at the other side of the earth.
        if (qpos.v[Q_Z] < 0)
        {
            globeHidesPoint = true;
            return false;
        }
    }

    else
    {
        qreal  earthCenteredX = pixelAltitude * qpos.v[Q_X];
        qreal  earthCenteredY = pixelAltitude * qpos.v[Q_Y];

        // Don't draw high placemarks (e.g. satellites) that aren't visible.
        if (qpos.v[Q_Z] < 0
            && ((earthCenteredX * earthCenteredX
                 + earthCenteredY * earthCenteredY)
                < radius * radius))
        {
            globeHidesPoint = true;
            return false;
        }
    }

    const qreal width = viewport->width();
    const qreal height = viewport->height();

    // Let (x, y) be the position on the screen of the placemark..
    x = (width  / 2 + pixelAltitude * qpos.v[Q_X]);
    y = (height / 2 - pixelAltitude * qpos.v[Q_Y]);

    // Skip placemarks that are outside the screen area
    if (x < 0 || x >= width || y < 0 || y >= height)
    {
        globeHidesPoint = false;
        return false;
    }

    globeHidesPoint = false;
    return true;
}

bool SphericalProjection::screenCoordinates(const GeoDataCoordinates& coordinates,
                                            const ViewportParams* viewport,
                                            qreal* x, qreal& y,
                                            int& pointRepeatNum,
                                            const QSizeF& size,
                                            bool& globeHidesPoint) const
{
    pointRepeatNum = 0;
    bool visible = screenCoordinates(coordinates, viewport, *x, y, globeHidesPoint);

    // Skip placemarks that are outside the screen area
    if (*x + size.width() / 2.0 < 0.0 || *x >= viewport->width() + size.width() / 2.0
        || y + size.height() / 2.0 < 0.0 || y >= viewport->height() + size.height() / 2.0)
    {
        globeHidesPoint = false;
        return false;
    }

    // This projection doesn't have any repetitions,
    // so the number of screen points referring to the geopoint is one.
    pointRepeatNum = 1;
    return visible;
}


bool SphericalProjection::geoCoordinates(const int x, const int y,
                                         const ViewportParams* viewport,
                                         qreal& lon, qreal& lat,
                                         GeoDataCoordinates::Unit unit) const
{
    const qreal  inverseRadius = 1.0 / (qreal)(viewport->radius());

    const qreal qx = +(qreal)(x - viewport->width()  / 2) * inverseRadius;
    const qreal qy = -(qreal)(y - viewport->height() / 2) * inverseRadius;

    if (1 <= qx * qx + qy * qy)
    {
        return false;
    }

    const qreal qz = sqrt(1 - qx * qx - qy * qy);

    Quaternion  qpos(0.0, qx, qy, qz);
    qpos.rotateAroundAxis(viewport->planetAxis());
    qpos.getSpherical(lon, lat);

    if (unit == GeoDataCoordinates::Degree)
    {
        lon *= RAD2DEG;
        lat *= RAD2DEG;
    }

    return true;
}

} // namespace Marble