Add missing #pragma once + missing include * speeds up incremental builds as changes to a header will not always need the full mocs_compilation.cpp for all the target's headers rebuild, while having a moc file sourced into a source file only adds minor extra costs, due to small own code and the used headers usually already covered by the source file, being for the same class/struct * seems to not slow down clean builds, due to empty mocs_compilation.cpp resulting in those quickly processed, while the minor extra cost of the sourced moc files does not outweigh that in summary. Measured times actually improved by some percent points. (ideally CMake would just skip empty mocs_compilation.cpp & its object file one day) * enables compiler to see all methods of a class in same compilation unit to do some sanity checks * potentially more inlining in general, due to more in the compilation unit * allows to keep using more forward declarations in the header, as with the moc code being sourced into the cpp file there definitions can be ensured and often are already for the needs of the normal class methods
47 lines
1.4 KiB
C++
47 lines
1.4 KiB
C++
// SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
|
|
// SPDX-License-Identifier: LGPL-2.0-or-later
|
|
|
|
#include "locationhelper.h"
|
|
|
|
#include <cmath>
|
|
|
|
QRectF LocationHelper::unite(const QRectF &r1, const QRectF &r2)
|
|
{
|
|
// this looks weird but is actually intentional as we need to handle point-like "rects" as well
|
|
if ((!r1.isEmpty() || r1.isNull()) && (!r2.isEmpty() || r2.isNull())) {
|
|
return r1 | r2;
|
|
}
|
|
return (!r1.isEmpty() || r1.isNull()) ? r1 : r2;
|
|
}
|
|
|
|
QPointF LocationHelper::center(const QRectF &r)
|
|
{
|
|
return r.center();
|
|
}
|
|
|
|
constexpr inline double degToRad(double deg)
|
|
{
|
|
return deg / 180.0 * M_PI;
|
|
}
|
|
|
|
static QPointF mercatorProject(double lat, double lon, double zoom)
|
|
{
|
|
const auto x = (256.0 / (2.0 * M_PI)) * std::pow(2.0, zoom) * (degToRad(lon) + M_PI);
|
|
const auto y = (256.0 / (2.0 * M_PI)) * std::pow(2.0, zoom) * (M_PI - std::log(std::tan(M_PI / 4.0 + degToRad(lat) / 2.0)));
|
|
return QPointF(x, y);
|
|
}
|
|
|
|
float LocationHelper::zoomToFit(const QRectF &r, float mapWidth, float mapHeight)
|
|
{
|
|
const auto p1 = mercatorProject(r.bottomLeft().y(), r.bottomLeft().x(), 1.0);
|
|
const auto p2 = mercatorProject(r.topRight().y(), r.topRight().x(), 1.0);
|
|
|
|
const auto zx = std::log2((mapWidth / (p2.x() - p1.x())));
|
|
const auto zy = std::log2((mapHeight / (p2.y() - p1.y())));
|
|
const auto z = std::min(zx, zy);
|
|
|
|
return std::clamp(z, 5.0, 18.0);
|
|
}
|
|
|
|
#include "moc_locationhelper.cpp"
|