KOSMIndoorMap

pathutil.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "pathutil.h"
8#include "element.h"
9
10#include <cassert>
11
12using namespace OSM;
13
14static OSM::Id appendNextPath(const DataSet &dataSet, std::vector<const Node*> &nodes, OSM::Id startNode, std::vector<const Way*> &ways)
15{
16 if (ways.empty()) {
17 return {};
18 }
19
20 for (auto it = std::next(ways.begin()); it != ways.end(); ++it) {
21 if ((*it)->nodes.empty()) {
22 continue;
23 }
24 if ((*it)->nodes.front() == startNode) {
25 appendNodesFromWay(dataSet, nodes, (*it)->nodes.begin(), (*it)->nodes.end());
26 const auto lastNodeId = (*it)->nodes.back();
27 ways.erase(it);
28 return lastNodeId;
29 }
30 // path segments can also be backwards
31 if ((*it)->nodes.back() == startNode) {
32 appendNodesFromWay(dataSet, nodes, (*it)->nodes.rbegin(), (*it)->nodes.rend());
33 const auto lastNodeId = (*it)->nodes.front();
34 ways.erase(it);
35 return lastNodeId;
36 }
37 }
38
39 return {};
40}
41
42void OSM::assemblePath(const DataSet &dataSet, std::vector<const Way*> &&ways, std::vector<const Node*> &path)
43{
44 for (auto it = ways.begin(); it != ways.end();) {
45 if ((*it)->nodes.empty()) {
46 ++it;
47 continue;
48 }
49 appendNodesFromWay(dataSet, path, (*it)->nodes.begin(), (*it)->nodes.end());
50 const auto startNode = (*it)->nodes.front();
51 auto lastNode = (*it)->nodes.back();
52
53 do {
54 lastNode = appendNextPath(dataSet, path, lastNode, ways);
55 } while (lastNode && lastNode != startNode);
56
57 it = ways.erase(it);
58 }
59}
60
61void OSM::assemblePath(const OSM::DataSet &dataSet, const std::vector<OSM::Element> &ways, std::vector<const Node*> &path)
62{
63 std::vector<const OSM::Way*> w;
64 w.reserve(ways.size());
65 for (auto e : ways) {
66 if (e.type() == OSM::Type::Way) {
67 w.push_back(e.way());
68 }
69 }
70 assemblePath(dataSet, std::move(w), path);
71}
A set of nodes, ways and relations.
Definition datatypes.h:346
Low-level types and functions to work with raw OSM data as efficiently as possible.
int64_t Id
OSM element identifier.
Definition datatypes.h:30
KOSM_EXPORT void assemblePath(const DataSet &dataSet, std::vector< const Way * > &&ways, std::vector< const Node * > &path)
Assemble a continuous path into path from the given ways.
Definition pathutil.cpp:42
static void appendNodesFromWay(const DataSet &dataSet, std::vector< const Node * > &nodes, const Iter &nodeBegin, const Iter &nodeEnd)
Appends the nodes referenced by nodesBegin and @pnodesEnd into nodes.
Definition pathutil.h:24
iterator begin()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.