KPublicTransport

pathfilter.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "pathfilter_p.h"
7
8#include <QDebug>
9#include <QLineF>
10#include <QPolygonF>
11
12using namespace KPublicTransport;
13
14[[nodiscard]] static double turnAngle(const QPolygonF &path, qsizetype i)
15{
16 QLineF l1(path[i], path[i+1]);
17 QLineF l2(path[i+1], path[i+2]);
18 const auto turnAngle = l1.angleTo(l2);
19 return std::min(turnAngle, 360.0 - turnAngle);
20}
21
22void PathFilter::removeSpikes(QPolygonF &path, double maxAngle)
23{
24 if (path.size() < 3 || path.isClosed()) {
25 return;
26 }
27
28 for (qsizetype i = 0; i < path.size() - 2; ++i) {
29 const auto a1 = turnAngle(path, i);
30 if (a1 <= maxAngle) {
31 continue;
32 }
33 // look ahead by one, if the next turn is even steeper the current one is likely a side-effect that one, not the cause
34 const auto a2 = (i < path.size() - 3) ? turnAngle(path, i + 1) : 0.0;
35 if (a2 > a1) {
36 path.remove(i + 2, 1);
37 } else {
38 path.remove(i + 1, 1);
39 }
40 --i;
41 }
42}
QString path(const QString &relativePath)
Query operations and data types for accessing realtime public transport information from online servi...
QString & remove(QChar ch, Qt::CaseSensitivity cs)
qsizetype size() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.