QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsgraph.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgraph.cpp
3 --------------------------------------
4 Date : 2011-04-01
5 Copyright : (C) 2010 by Yakushev Sergey
6 Email : YakushevS <at> list.ru
7****************************************************************************
8* *
9* This program is free software; you can redistribute it and/or modify *
10* it under the terms of the GNU General Public License as published by *
11* the Free Software Foundation; either version 2 of the License, or *
12* (at your option) any later version. *
13* *
14***************************************************************************/
15
20
21#include "qgsgraph.h"
22
23#include <QSet>
24
26{
27 mGraphVertices[mNextVertexId] = QgsGraphVertex( pt );
28 return mNextVertexId++;
29}
30
31int QgsGraph::addEdge( int fromVertexIdx, int toVertexIdx, const QVector<QVariant> &strategies )
32{
34
35 e.mStrategies = strategies;
36 e.mToIdx = toVertexIdx;
37 e.mFromIdx = fromVertexIdx;
38
39 mGraphEdges[mNextEdgeId] = e;
40 const int edgeIdx = mGraphEdges.size() - 1;
41
42 mGraphVertices[toVertexIdx].mIncomingEdges.push_back( edgeIdx );
43 mGraphVertices[fromVertexIdx].mOutgoingEdges.push_back( edgeIdx );
44
45 return mNextEdgeId++;
46}
47
48const QgsGraphVertex &QgsGraph::vertex( int idx ) const
49{
50 auto it = mGraphVertices.constFind( idx );
51 if ( it != mGraphVertices.constEnd() )
52 return ( it ).value();
53 Q_ASSERT_X( false, "QgsGraph::vertex()", "Invalid vertex ID" );
54
55 // unreachable...
56 return ( *const_cast<QHash<int, QgsGraphVertex> *>( &mGraphVertices ) )[idx];
57}
58
59void QgsGraph::removeVertex( int index )
60{
61 auto it = mGraphVertices.constFind( index );
62 if ( it != mGraphVertices.constEnd() )
63 {
64 QSet<int> affectedEdges = qgis::listToSet( it->incomingEdges() );
65 affectedEdges.unite( qgis::listToSet( it->outgoingEdges() ) );
66
67 mGraphVertices.erase( it );
68
69 // remove affected edges
70 for ( int edgeId : std::as_const( affectedEdges ) )
71 {
72 mGraphEdges.remove( edgeId );
73 }
74 }
75}
76
77const QgsGraphEdge &QgsGraph::edge( int idx ) const
78{
79 auto it = mGraphEdges.constFind( idx );
80 if ( it != mGraphEdges.constEnd() )
81 return ( it ).value();
82 Q_ASSERT_X( false, "QgsGraph::edge()", "Invalid edge ID" );
83
84 // unreachable...
85 return ( *const_cast<QHash<int, QgsGraphEdge> *>( &mGraphEdges ) )[idx];
86}
87
88void QgsGraph::removeEdge( int index )
89{
90 auto it = mGraphEdges.constFind( index );
91 if ( it != mGraphEdges.constEnd() )
92 {
93 const int fromVertex = it->fromVertex();
94 const int toVertex = it->toVertex();
95 mGraphEdges.erase( it );
96
97 // clean up affected vertices
98 auto vertexIt = mGraphVertices.find( fromVertex );
99 if ( vertexIt != mGraphVertices.end() )
100 {
101 vertexIt->mOutgoingEdges.removeAll( index );
102 if ( vertexIt->mOutgoingEdges.empty() && vertexIt->mIncomingEdges.empty() )
103 mGraphVertices.erase( vertexIt );
104 }
105
106 vertexIt = mGraphVertices.find( toVertex );
107 if ( vertexIt != mGraphVertices.end() )
108 {
109 vertexIt->mIncomingEdges.removeAll( index );
110 if ( vertexIt->mOutgoingEdges.empty() && vertexIt->mIncomingEdges.empty() )
111 mGraphVertices.erase( vertexIt );
112 }
113 }
114}
115
117{
118 return mGraphVertices.size();
119}
120
122{
123 return mGraphEdges.size();
124}
125
126int QgsGraph::findVertex( const QgsPointXY &pt ) const
127{
128 int i = 0;
129 for ( i = 0; i < mGraphVertices.size(); ++i )
130 {
131 if ( mGraphVertices[i].point() == pt )
132 {
133 return i;
134 }
135 }
136 return -1;
137}
138
139bool QgsGraph::hasVertex( int index ) const
140{
141 auto it = mGraphVertices.constFind( index );
142 return it != mGraphVertices.constEnd();
143}
144
145bool QgsGraph::hasEdge( int index ) const
146{
147 auto it = mGraphEdges.constFind( index );
148 return it != mGraphEdges.constEnd();
149}
150
151int QgsGraph::findOppositeEdge( int index ) const
152{
153 auto it = mGraphEdges.constFind( index );
154 if ( it != mGraphEdges.constEnd() )
155 {
156 const int fromVertex = it->fromVertex();
157 const int toVertex = it->toVertex();
158
159 // look for edges which start at toVertex
160 const QgsGraphEdgeIds candidates = mGraphVertices.value( toVertex ).outgoingEdges();
161 for ( int candidate : candidates )
162 {
163 if ( mGraphEdges.value( candidate ).toVertex() == fromVertex )
164 return candidate;
165 }
166 }
167 return -1;
168}
169
170QVariant QgsGraphEdge::cost( int i ) const
171{
172 return mStrategies[i];
173}
174
175QVector<QVariant> QgsGraphEdge::strategies() const
176{
177 return mStrategies;
178}
179
181{
182 return mFromIdx;
183}
184
186{
187 return mToIdx;
188}
189
191 : mCoordinate( point )
192{
193}
194
196{
197 return mIncomingEdges;
198}
199
201{
202 return mOutgoingEdges;
203}
204
206{
207 return mCoordinate;
208}
Represents an edge in a graph.
Definition qgsgraph.h:44
int fromVertex() const
Returns the index of the vertex at the start of this edge.
Definition qgsgraph.cpp:180
QVector< QVariant > strategies() const
Returns array of available strategies.
Definition qgsgraph.cpp:175
int toVertex() const
Returns the index of the vertex at the end of this edge.
Definition qgsgraph.cpp:185
QVariant cost(int strategyIndex) const
Returns edge cost calculated using specified strategy.
Definition qgsgraph.cpp:170
Represents vertex in a graph.
Definition qgsgraph.h:89
QgsGraphEdgeIds outgoingEdges() const
Returns outgoing edge ids, i.e.
Definition qgsgraph.cpp:200
QgsGraphEdgeIds incomingEdges() const
Returns the incoming edge ids, i.e.
Definition qgsgraph.cpp:195
QgsPointXY point() const
Returns point associated with graph vertex.
Definition qgsgraph.cpp:205
QgsGraphVertex()=default
const QgsGraphVertex & vertex(int idx) const
Returns the vertex at the given index.
Definition qgsgraph.cpp:48
int addEdge(int fromVertexIdx, int toVertexIdx, const QVector< QVariant > &strategies)
Add an edge to the graph, going from the fromVertexIdx to toVertexIdx.
Definition qgsgraph.cpp:31
int findOppositeEdge(int index) const
Finds the first edge which is the opposite of the edge with the specified index.
Definition qgsgraph.cpp:151
int findVertex(const QgsPointXY &pt) const
Find vertex by associated point.
Definition qgsgraph.cpp:126
bool hasVertex(int index) const
Returns whether the vertex of the given index exists.
Definition qgsgraph.cpp:139
bool hasEdge(int index) const
Returns whether the edge of the given index exists.
Definition qgsgraph.cpp:145
QHash< int, QgsGraphVertex > mGraphVertices
Graph vertices.
Definition qgsgraph.h:349
int edgeCount() const
Returns number of graph edges.
Definition qgsgraph.cpp:121
const QgsGraphEdge & edge(int idx) const
Returns the edge at the given index.
Definition qgsgraph.cpp:77
void removeEdge(int index)
Removes the edge at specified index.
Definition qgsgraph.cpp:88
int addVertex(const QgsPointXY &pt)
Add a vertex to the graph.
Definition qgsgraph.cpp:25
QHash< int, QgsGraphEdge > mGraphEdges
Graph edges.
Definition qgsgraph.h:352
void removeVertex(int index)
Removes the vertex at specified index.
Definition qgsgraph.cpp:59
int vertexCount() const
Returns number of graph vertices.
Definition qgsgraph.cpp:116
Represents a 2D point.
Definition qgspointxy.h:60
QList< int > QgsGraphEdgeIds
Definition qgsgraph.h:81