KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
spanningtree.h
1/**
2 * @brief A Dijkstra-based spanning tree for finding shortest paths through a line network.
3 */
4#ifndef SPANNINGTREE_H
5#define SPANNINGTREE_H
6#include "treepathvertice.h"
7#include "line.h"
8
9/**
10 * @brief Computes the shortest path through a network of Line segments using Dijkstra's algorithm.
11 *
12 * Construct with a Line::List representing the navigable network, set the origin and
13 * destination points, then call computePath() to retrieve the ordered list of lines
14 * that form the shortest route.
15 */
16class KANOOP_EXPORT SpanningTree
17{
18public:
19 /** @brief Default constructor — creates an empty, invalid spanning tree. */
21 _origin(nullptr), _destination(nullptr) {}
22
23 /**
24 * @brief Construct a SpanningTree from a network of line segments.
25 * @param lines Line segments forming the navigable graph
26 */
27 SpanningTree(const Line::List& lines);
28
29 /** @brief Destructor — frees all dynamically allocated vertex objects. */
30 virtual ~SpanningTree();
31
32 /**
33 * @brief Compute the shortest path from origin to destination through the line network.
34 * @return Ordered list of lines forming the path, or an empty list if no path exists
35 */
37
38 /**
39 * @brief Set the origin point from which to route.
40 * @param origin Starting point in the line network's coordinate space
41 * @param preferredDirection Initial preferred travel direction (default NoDirection)
42 */
43 void setOrigin(const QPointF& origin, Geo::Direction preferredDirection = Geo::NoDirection);
44
45 /**
46 * @brief Set the destination point to route towards.
47 * @param destination Target point in the line network's coordinate space
48 */
49 void setDestination(const QPointF& destination);
50
51 /**
52 * @brief Return all vertices in the spanning tree.
53 * @return List of all TreePathVertice objects in the graph
54 */
56 {
57 return TreePathVertice::List(_vertices.values());
58 }
59
60 /**
61 * @brief Return true if the spanning tree contains at least one line segment.
62 * @return true if the line network is non-empty
63 */
64 bool isValid() const { return _lines.count() > 0; }
65
66private:
67 void initializeFromLines();
68 void initializeVertices();
69
70 TreePathVertice *addAdHocVertice(const QPointF point, TreePathVertice::VerticeType type, Geo::Direction preferredDirection = Geo::NoDirection);
71 void clearAdHocVertices(TreePathVertice::VerticeType type);
72 QPointF findClosestPointInLines(const QPointF& point, Line& resultLine, double& closestDistance, Geo::Direction preferredDirection = Geo::NoDirection);
73
74 bool cycle();
75 Line::List getPath();
76
77 void debugLog(const char* file, int line, int level, const QString& text);
78
79 Line::List _lines;
80
81 TreePathVertice::Map _vertices;
82 TreePathVertice::List _visited;
83 TreePathVertice::List _unvisited;
84 TreePathVertice* _origin;
85 TreePathVertice* _destination;
86
87 int _debugLevel;
88};
89
90#endif // SPANNINGTREE_H
A list of Line objects with spatial query helpers.
Definition line.h:127
Represents a 2D line segment between two Point endpoints.
Definition line.h:28
A Dijkstra-based spanning tree for finding shortest paths through a line network.
virtual ~SpanningTree()
Destructor — frees all dynamically allocated vertex objects.
TreePathVertice::List vertices() const
Return all vertices in the spanning tree.
void setOrigin(const QPointF &origin, Geo::Direction preferredDirection=Geo::NoDirection)
Set the origin point from which to route.
bool isValid() const
Return true if the spanning tree contains at least one line segment.
SpanningTree()
Default constructor — creates an empty, invalid spanning tree.
void setDestination(const QPointF &destination)
Set the destination point to route towards.
Line::List computePath()
Compute the shortest path from origin to destination through the line network.
SpanningTree(const Line::List &lines)
Construct a SpanningTree from a network of line segments.
A list of TreePathVertice pointers.
A map from hash-name string to TreePathVertice pointer.
Represents a single vertex in the spanning-tree graph used by PathRouter.
VerticeType
Classifies a vertice's role in the path graph.
Direction
Cardinal directions, aliased to the corresponding Side values.
Definition geo.h:37
@ NoDirection
No direction.
Definition geo.h:38